Compare commits

..

2 Commits

Author SHA1 Message Date
715a2cd82e temporarely disabled variable size reading 2025-02-05 17:26:01 +05:00
3fd45311c5 constant data names linking 2025-02-05 17:25:12 +05:00
6 changed files with 121 additions and 13 deletions

View File

@ -1,6 +1,7 @@
#include "Binary.h" #include "Binary.h"
List_define(ConstDataProps); List_define(ConstDataProps);
HashMap_define(ConstDataProps, HashMap_DESTROY_VALUE_FUNC_NULL);
List_define(NamedRef); List_define(NamedRef);
List_define(CompiledSection); List_define(CompiledSection);
HashMap_define(CompiledSectionPtr, HashMap_DESTROY_VALUE_FUNC_NULL); HashMap_define(CompiledSectionPtr, HashMap_DESTROY_VALUE_FUNC_NULL);
@ -10,13 +11,13 @@ void CompiledSection_construct(CompiledSection* ptr, str name){
ptr->name = name; ptr->name = name;
ptr->next = NULL; ptr->next = NULL;
ptr->offset = 0; ptr->offset = 0;
ptr->const_data_offsets = List_ConstDataProps_construct(NULL, 0, 0); ptr->const_data_props_list = List_ConstDataProps_construct(NULL, 0, 0);
ptr->named_refs = List_NamedRef_construct(NULL, 0, 0); ptr->named_refs = List_NamedRef_construct(NULL, 0, 0);
ptr->bytes = List_u8_alloc(64); ptr->bytes = List_u8_alloc(64);
} }
void CompiledSection_free(CompiledSection* ptr){ void CompiledSection_free(CompiledSection* ptr){
free(ptr->const_data_offsets.data); free(ptr->const_data_props_list.data);
free(ptr->named_refs.data); free(ptr->named_refs.data);
free(ptr->bytes.data); free(ptr->bytes.data);
} }
@ -25,6 +26,7 @@ void CompiledSection_free(CompiledSection* ptr){
void BinaryObject_construct(BinaryObject* ptr){ void BinaryObject_construct(BinaryObject* ptr){
ptr->section_list = List_CompiledSection_alloc(64); ptr->section_list = List_CompiledSection_alloc(64);
HashMap_CompiledSectionPtr_alloc(&ptr->section_map); HashMap_CompiledSectionPtr_alloc(&ptr->section_map);
HashMap_ConstDataProps_alloc(&ptr->const_data_map);
} }
void BinaryObject_free(BinaryObject* ptr){ void BinaryObject_free(BinaryObject* ptr){
@ -32,5 +34,7 @@ void BinaryObject_free(BinaryObject* ptr){
CompiledSection_free(&ptr->section_list.data[i]); CompiledSection_free(&ptr->section_list.data[i]);
} }
free(ptr->section_list.data); free(ptr->section_list.data);
HashMap_CompiledSectionPtr_free(&ptr->section_map); HashMap_CompiledSectionPtr_free(&ptr->section_map);
HashMap_ConstDataProps_free(&ptr->const_data_map);
} }

View File

@ -17,6 +17,7 @@ typedef struct ConstDataProps {
#define ConstDataProps_construct(NAME, SIZE, OFFSET) ((ConstDataProps){ .name = NAME, .size = SIZE, .offset = OFFSET}) #define ConstDataProps_construct(NAME, SIZE, OFFSET) ((ConstDataProps){ .name = NAME, .size = SIZE, .offset = OFFSET})
List_declare(ConstDataProps); List_declare(ConstDataProps);
HashMap_declare(ConstDataProps);
typedef enum NamedRefType { typedef enum NamedRefType {
@ -35,11 +36,12 @@ typedef struct NamedRef {
List_declare(NamedRef); List_declare(NamedRef);
typedef struct CompiledSection { typedef struct CompiledSection {
str name; str name;
CompiledSection* next; CompiledSection* next;
u32 offset; u32 offset;
List_ConstDataProps const_data_offsets; List_ConstDataProps const_data_props_list;
List_NamedRef named_refs; List_NamedRef named_refs;
List_u8 bytes; List_u8 bytes;
} CompiledSection; } CompiledSection;
@ -47,15 +49,15 @@ typedef struct CompiledSection {
void CompiledSection_construct(CompiledSection* ptr, str name); void CompiledSection_construct(CompiledSection* ptr, str name);
void CompiledSection_free(CompiledSection* ptr); void CompiledSection_free(CompiledSection* ptr);
List_declare(CompiledSection); List_declare(CompiledSection);
typedef CompiledSection* CompiledSectionPtr; typedef CompiledSection* CompiledSectionPtr;
HashMap_declare(CompiledSectionPtr); HashMap_declare(CompiledSectionPtr);
typedef struct BinaryObject { typedef struct BinaryObject {
List_CompiledSection section_list; List_CompiledSection section_list;
HashMap_CompiledSectionPtr section_map; HashMap_CompiledSectionPtr section_map;
HashMap_ConstDataProps const_data_map;
u32 total_size; u32 total_size;
} BinaryObject; } BinaryObject;

View File

@ -77,7 +77,7 @@ static bool compileSection(Compiler* cmp, Section* sec){
Operation* op = &sec->code.data[i]; Operation* op = &sec->code.data[i];
List_u8_pushMany(&cs->bytes, (void*)&op->opcode, sizeof(op->opcode)); List_u8_pushMany(&cs->bytes, (void*)&op->opcode, sizeof(op->opcode));
for(u32 j = 0; j < op->args.len; j++){ for(u32 j = 0; j < op->args.len; j++){
Argument* arg = &op->args.data[i]; Argument* arg = &op->args.data[j];
switch(arg->type){ switch(arg->type){
case ArgumentType_VarDataName: case ArgumentType_VarDataName:
returnError("argument type 'VarDataName' is not supported yet"); returnError("argument type 'VarDataName' is not supported yet");
@ -113,9 +113,9 @@ static bool compileSection(Compiler* cmp, Section* sec){
// compile data // compile data
for(u32 i = 0; i < sec->data.len; i++){ for(u32 i = 0; i < sec->data.len; i++){
DataDefinition* ddf = &sec->data.data[i]; DataDefinition* dd = &sec->data.data[i];
List_ConstDataProps_push(&cs->const_data_offsets, ConstDataProps_construct(ddf->name, ddf->data.len, cs->bytes.len)); List_ConstDataProps_push(&cs->const_data_props_list, ConstDataProps_construct(dd->name, dd->data.len, cs->bytes.len));
List_u8_pushMany(&cs->bytes, ddf->data.data, ddf->data.len); List_u8_pushMany(&cs->bytes, dd->data.data, dd->data.len);
} }
// TODO: push padding // TODO: push padding
@ -148,6 +148,45 @@ static bool compileBinary(Compiler* cmp){
continue; continue;
prev_sec->next = sec; prev_sec->next = sec;
sec->offset = prev_sec->offset + prev_sec->bytes.len; sec->offset = prev_sec->offset + prev_sec->bytes.len;
ConstDataProps cd = ConstDataProps_construct(sec->name, sec->bytes.len, sec->offset);
if(!HashMap_ConstDataProps_tryPush(&cmp->binary.const_data_map, cd.name, cd)){
returnError("duplicate named data '%s'", str_copy(cd.name).data);
}
for(u32 j = 0; j < sec->const_data_props_list.len; j++){
cd = sec->const_data_props_list.data[j];
cd.offset += sec->offset;
if(!HashMap_ConstDataProps_tryPush(&cmp->binary.const_data_map, cd.name, cd)){
returnError("duplicate named data '%s'", str_copy(cd.name).data);
}
}
}
// insert calculated offsets into sections
for(u32 i = 0; i < cmp->binary.section_list.len; i++){
CompiledSection* sec = &cmp->binary.section_list.data[i];
for(u32 j = 0; j < sec->named_refs.len; j++){
NamedRef* ref = &sec->named_refs.data[j];
ConstDataProps* target_data = HashMap_ConstDataProps_tryGetPtr(
&cmp->binary.const_data_map, ref->name);
if(target_data == NULL){
returnError("can't find named data '%s'", str_copy(ref->name).data);
}
void* ref_value_ptr = sec->bytes.data + ref->offset;
switch(ref->type){
default:
returnError("invalid NamedRefType %i", ref->type);
case NamedRefType_Size:
*((u32*)ref_value_ptr) = target_data->size;
break;
case NamedRefType_Ptr:
*((u32*)ref_value_ptr) = target_data->offset;
break;
}
}
} }
cmp->binary.total_size = total_size; cmp->binary.total_size = total_size;
@ -250,6 +289,65 @@ bool Compiler_compile(Compiler* cmp, cstr source_file_name, cstr out_file_name,
if(debug_log) if(debug_log)
printf("===================================[parsing]===================================\n"); printf("===================================[parsing]===================================\n");
success = Compiler_parse(cmp); success = Compiler_parse(cmp);
if (debug_log){
printf("-------------------------------------[AST]-------------------------------------\n");
for(u32 i = 0; i < cmp->ast.sections.len; i++){
Section* sec = &cmp->ast.sections.data[i];
str tmpstr = str_copy(sec->name);
printf("section '%s'\n", tmpstr.data);
free(tmpstr.data);
for(u32 j = 0; j < sec->data.len; j++){
DataDefinition* dd = &sec->data.data[j];
tmpstr = str_copy(dd->name);
printf(" const%u %s (len %u)\n", dd->element_size * 8, tmpstr.data, dd->data.len/dd->element_size);
free(tmpstr.data);
}
for(u32 j = 0; j < sec->code.len; j++){
Operation* op = &sec->code.data[j];
const Instruction* instr = Instruction_getByOpcode(op->opcode);
printf(" %s", instr->name.data);
for(u32 k = 0; k < op->args.len; k++){
Argument* arg = &op->args.data[k];
printf(" %s(", ArgumentType_toString(arg->type).data);
switch(arg->type){
default:
fclose(f);
returnError("invalid argument type %i", arg->type);
case ArgumentType_Register:
const char* register_names[] = {"null", "ax", "bx", "cx", "dx"};
printf("%s", register_names[arg->value.register_code]);
break;
case ArgumentType_ConstValue:
printf(IFWIN("%lli", "%li"), arg->value.i);
break;
case ArgumentType_ConstDataPointer:
tmpstr = str_copy(arg->value.data_name);
printf("@%s", tmpstr.data);
free(tmpstr.data);
break;
case ArgumentType_ConstDataSize:
tmpstr = str_copy(arg->value.data_name);
printf("#%s", tmpstr.data);
free(tmpstr.data);
break;
case ArgumentType_VarDataName:
tmpstr = str_copy(arg->value.data_name);
printf("%s", tmpstr.data);
free(tmpstr.data);
break;
}
printf(")");
}
printf("\n");
}
}
}
if(!success){ if(!success){
fclose(f); fclose(f);
return false; return false;

View File

@ -4,8 +4,9 @@
i32 PUSH_impl(VM* vm){ i32 PUSH_impl(VM* vm){
u8 dst_register_i = 0; u8 dst_register_i = 0;
readRegisterVar(dst_register_i); readRegisterVar(dst_register_i);
u8 value_size = 0; /*u8 value_size = 0;
readValueSizeVar(value_size); readValueSizeVar(value_size);*/
u8 value_size = 4;\
vm->registers[dst_register_i].u32v = 0; vm->registers[dst_register_i].u32v = 0;
if(!VM_dataRead(vm, &vm->registers[dst_register_i].u32v, vm->current_pos, value_size)) if(!VM_dataRead(vm, &vm->registers[dst_register_i].u32v, vm->current_pos, value_size))

View File

@ -21,6 +21,7 @@
validateRegisterIndex(VAR);\ validateRegisterIndex(VAR);\
} }
/*
#define validateValueSize(VAR) {\ #define validateValueSize(VAR) {\
if(VAR < 1 || VAR > 4){\ if(VAR < 1 || VAR > 4){\
VM_setError(vm, "invalid value_size (%x)", VAR);\ VM_setError(vm, "invalid value_size (%x)", VAR);\
@ -32,3 +33,4 @@
readVar(VAR);\ readVar(VAR);\
validateValueSize(VAR);\ validateValueSize(VAR);\
} }
*/

View File

@ -4,8 +4,9 @@
u8 dst_register_i = 0, src_register_i = 0;\ u8 dst_register_i = 0, src_register_i = 0;\
readRegisterVar(dst_register_i);\ readRegisterVar(dst_register_i);\
readRegisterVar(src_register_i);\ readRegisterVar(src_register_i);\
u8 value_size = 0;\ /*u8 value_size = 0;\
readValueSizeVar(value_size);\ readValueSizeVar(value_size);*/\
u8 value_size = 4;\
\ \
switch(value_size){\ switch(value_size){\
case 1: \ case 1: \