fixed bugs
This commit is contained in:
parent
ad5c2b856a
commit
b9fa669fd1
@ -27,6 +27,8 @@ void BinaryObject_construct(BinaryObject* ptr){
|
||||
ptr->section_list = List_CompiledSection_alloc(64);
|
||||
HashMap_CompiledSectionPtr_alloc(&ptr->section_map);
|
||||
HashMap_ConstDataProps_alloc(&ptr->const_data_map);
|
||||
ptr->main_section = NULL;
|
||||
ptr->total_size = 0;
|
||||
}
|
||||
|
||||
void BinaryObject_free(BinaryObject* ptr){
|
||||
|
||||
@ -57,6 +57,7 @@ HashMap_declare(CompiledSectionPtr);
|
||||
typedef struct BinaryObject {
|
||||
List_CompiledSection section_list;
|
||||
HashMap_CompiledSectionPtr section_map;
|
||||
NULLABLE(CompiledSection*) main_section;
|
||||
HashMap_ConstDataProps const_data_map;
|
||||
u32 total_size;
|
||||
} BinaryObject;
|
||||
|
||||
@ -124,6 +124,9 @@ static bool compileSection(Compiler* cmp, Section* sec){
|
||||
}
|
||||
|
||||
static bool compileBinary(Compiler* cmp){
|
||||
returnErrorIf_auto(cmp->state != CompilerState_Parsing);
|
||||
cmp->state = CompilerState_Compiling;
|
||||
|
||||
for(u32 i = 0; i < cmp->ast.sections.len; i++){
|
||||
SectionPtr sec = &cmp->ast.sections.data[i];
|
||||
if(!compileSection(cmp, sec)){
|
||||
@ -137,6 +140,7 @@ static bool compileBinary(Compiler* cmp){
|
||||
if(main_sec_ptrptr == NULL){
|
||||
returnError("no 'main' section was defined");
|
||||
}
|
||||
cmp->binary.main_section = *main_sec_ptrptr;
|
||||
|
||||
// create linked list of CompiledSection where main is the first
|
||||
CompiledSection* prev_sec = *main_sec_ptrptr;
|
||||
@ -160,6 +164,8 @@ static bool compileBinary(Compiler* cmp){
|
||||
returnError("duplicate named data '%s'", str_copy(cd.name).data);
|
||||
}
|
||||
}
|
||||
|
||||
prev_sec = sec;
|
||||
}
|
||||
|
||||
// insert calculated offsets into sections
|
||||
@ -194,26 +200,16 @@ static bool compileBinary(Compiler* cmp){
|
||||
}
|
||||
|
||||
static bool writeBinaryFile(Compiler* cmp, FILE* f){
|
||||
returnErrorIf_auto(cmp->state != CompilerState_Parsing);
|
||||
cmp->state = CompilerState_Compiling;
|
||||
returnErrorIf_auto(cmp->state != CompilerState_Compiling);
|
||||
|
||||
if(!compileBinary(cmp)){
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
CompiledSection** main_sec_ptrptr = HashMap_CompiledSectionPtr_tryGetPtr(&cmp->binary.section_map, STR("main"));
|
||||
if(main_sec_ptrptr == NULL){
|
||||
returnError("no 'main' section was defined");
|
||||
}
|
||||
CompiledSection* sec = *main_sec_ptrptr;
|
||||
CompiledSection* sec = cmp->binary.main_section;
|
||||
while(sec){
|
||||
fwrite(sec->bytes.data, 1, sec->bytes.len, f);
|
||||
fflush(f);
|
||||
sec = sec->next;
|
||||
}
|
||||
|
||||
//TODO: print warnings for unused sections
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -289,6 +285,7 @@ bool Compiler_compile(Compiler* cmp, cstr source_file_name, cstr out_file_name,
|
||||
if(debug_log)
|
||||
printf("===================================[parsing]===================================\n");
|
||||
success = Compiler_parse(cmp);
|
||||
|
||||
if (debug_log){
|
||||
printf("-------------------------------------[AST]-------------------------------------\n");
|
||||
for(u32 i = 0; i < cmp->ast.sections.len; i++){
|
||||
@ -309,6 +306,7 @@ bool Compiler_compile(Compiler* cmp, cstr source_file_name, cstr out_file_name,
|
||||
Operation* op = &sec->code.data[j];
|
||||
const Instruction* instr = Instruction_getByOpcode(op->opcode);
|
||||
if(instr == NULL){
|
||||
fclose(f);
|
||||
returnError("unknown opcode: %i", op->opcode)
|
||||
}
|
||||
|
||||
@ -352,6 +350,7 @@ bool Compiler_compile(Compiler* cmp, cstr source_file_name, cstr out_file_name,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(!success){
|
||||
fclose(f);
|
||||
return false;
|
||||
@ -359,6 +358,25 @@ bool Compiler_compile(Compiler* cmp, cstr source_file_name, cstr out_file_name,
|
||||
|
||||
if(debug_log)
|
||||
printf("==================================[compiling]==================================\n");
|
||||
success = compileBinary(cmp);
|
||||
|
||||
if(debug_log){
|
||||
for(u32 i = 0; i < cmp->binary.section_list.len; i++){
|
||||
CompiledSection* sec = &cmp->binary.section_list.data[i];
|
||||
str tmpstr = str_copy(sec->name);
|
||||
printf("compiled section '%s' to %u bytes with offset 0x%x\n", tmpstr.data, sec->bytes.len, sec->offset);
|
||||
free(tmpstr.data);
|
||||
}
|
||||
}
|
||||
|
||||
if(!success){
|
||||
fclose(f);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
if(debug_log)
|
||||
printf("----------------------------[writing output to file]---------------------------\n");
|
||||
success = writeBinaryFile(cmp, f);
|
||||
fclose(f);
|
||||
if(success){
|
||||
|
||||
@ -130,7 +130,7 @@ static void readArguments(Compiler* cmp){
|
||||
|
||||
// string argument reading
|
||||
if(quot != '\0'){
|
||||
if(c == quot && cmp->code.data[cmp->pos - 1] != '\\'){
|
||||
if(c == quot && (cmp->code.data[cmp->pos - 1] != '\\' || cmp->code.data[cmp->pos - 2] == '\\')){
|
||||
quot = '\0';
|
||||
}
|
||||
else if(c == '\r' || c == '\n'){
|
||||
|
||||
@ -44,7 +44,8 @@ static NULLABLE(str) resolveEscapeSequences(Compiler* cmp, str src){
|
||||
c = src.data[i];
|
||||
if(c == '\\'){
|
||||
escaped = !escaped;
|
||||
continue;
|
||||
if(escaped)
|
||||
continue;
|
||||
}
|
||||
|
||||
if(!escaped){
|
||||
@ -69,11 +70,17 @@ static NULLABLE(str) resolveEscapeSequences(Compiler* cmp, str src){
|
||||
case 'e':
|
||||
StringBuilder_append_char(&sb, '\e');
|
||||
break;
|
||||
case '"':
|
||||
case '\'':
|
||||
StringBuilder_append_char(&sb, c);
|
||||
break;
|
||||
default:
|
||||
setError_unexpectedTokenChar(cmp->tokens.data[cmp->tok_i], i);
|
||||
StringBuilder_free(&sb);
|
||||
return str_null;
|
||||
}
|
||||
|
||||
escaped = false;
|
||||
}
|
||||
|
||||
return StringBuilder_getStr(&sb);
|
||||
|
||||
@ -22,7 +22,7 @@ i32 NAME##_impl (VM* vm) {\
|
||||
vm->registers.array[src_register_i].u64v = OPERATOR vm->registers.array[src_register_i].u64v;\
|
||||
break;\
|
||||
}\
|
||||
return sizeof(src_register_i) + sizeof(value_size);\
|
||||
return sizeof(src_register_i) /*+ sizeof(value_size)*/;\
|
||||
}
|
||||
|
||||
#define logicalOperator2Impl(NAME, OPERATOR)\
|
||||
@ -48,7 +48,7 @@ i32 NAME##_impl (VM* vm) {\
|
||||
vm->registers.array[dst_register_i].u64v OPERATOR##= vm->registers.array[src_register_i].u64v;\
|
||||
break;\
|
||||
}\
|
||||
return sizeof(dst_register_i) + sizeof(src_register_i) + sizeof(value_size);\
|
||||
return sizeof(dst_register_i) + sizeof(src_register_i) /*+ sizeof(value_size)*/;\
|
||||
}
|
||||
|
||||
#define logicalOperator3Impl(NAME, OPERATOR)\
|
||||
@ -75,7 +75,7 @@ i32 NAME##_impl (VM* vm) {\
|
||||
vm->registers.array[dst_register_i].u64v = vm->registers.array[src0_register_i].u64v OPERATOR vm->registers.array[src1_register_i].u64v;\
|
||||
break;\
|
||||
}\
|
||||
return sizeof(dst_register_i) + sizeof(src0_register_i) + sizeof(src1_register_i) + sizeof(value_size);\
|
||||
return sizeof(dst_register_i) + sizeof(src0_register_i) + sizeof(src1_register_i) /*+ sizeof(value_size)*/;\
|
||||
}
|
||||
|
||||
logicalOperator3Impl(EQ, ==)
|
||||
|
||||
@ -23,7 +23,7 @@ i32 NAME##_impl (VM* vm) {\
|
||||
vm->registers.array[dst_register_i].u64v OPERATOR##= vm->registers.array[src_register_i].u64v;\
|
||||
break;\
|
||||
}\
|
||||
return sizeof(dst_register_i) + sizeof(src_register_i) + sizeof(value_size);\
|
||||
return sizeof(dst_register_i) + sizeof(src_register_i) /*+ sizeof(value_size)*/;\
|
||||
}
|
||||
|
||||
/// ADD [dst_register] [src_register]
|
||||
|
||||
46
src/main.c
46
src/main.c
@ -10,53 +10,7 @@
|
||||
i32 compileSources(cstr source_file, cstr out_file, bool debug_log);
|
||||
i32 bootFromImage(cstr image_file);
|
||||
|
||||
#define assert_sdl(EXPR) if(!(EXPR)) { printf("assert failed: %s\nSDL_Error: %s\n", #EXPR, SDL_GetError()); return false; }
|
||||
|
||||
bool test_display(){
|
||||
Display* display = Display_create(STR("le display"), 1600, 900, DisplayFlags_Default);
|
||||
assert_sdl(display != NULL);
|
||||
|
||||
i64 fps = 60;
|
||||
i64 nsec_per_frame = 1e9 / fps;
|
||||
for(u32 i = 0; i < 600; i++){
|
||||
static i64 last_frame_time = 0;
|
||||
i64 current_frame_time = SDL_GetTicksNS();
|
||||
i64 delta_time = current_frame_time - last_frame_time;
|
||||
last_frame_time = current_frame_time;
|
||||
|
||||
SDL_Event event;
|
||||
while(SDL_PollEvent(&event)) {
|
||||
if(event.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED){
|
||||
printfe("event WINDOW_CLOSE");
|
||||
return 0;
|
||||
}
|
||||
if(event.type == SDL_EVENT_QUIT){
|
||||
printfe("event QUIT");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
assert_sdl(Display_setDrawingColor(display, ColorRGBA_create(200, 200, 200, 255)));
|
||||
assert_sdl(Display_clear(display));
|
||||
assert_sdl(Display_setDrawingColor(display, ColorRGBA_create(240, 40, 40, 255)));
|
||||
assert_sdl(Display_fillRect(display, Rect_create(i, i, 64, 64)));
|
||||
assert_sdl(Display_swapBuffers(display));
|
||||
|
||||
i64 time_ellapsed = SDL_GetTicksNS() - last_frame_time;
|
||||
i64 delay = nsec_per_frame - time_ellapsed;
|
||||
if (delay > 0) {
|
||||
SDL_DelayNS(delay);
|
||||
}
|
||||
}
|
||||
|
||||
Display_destroy(display);
|
||||
return true;
|
||||
}
|
||||
|
||||
i32 main(const i32 argc, cstr* argv){
|
||||
test_display();
|
||||
return 0;
|
||||
|
||||
if(argc < 2){
|
||||
printfe("ERROR: no arguments provided. Use --help to know more.\n");
|
||||
return 1;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user