From 17baabbd95e150fdec49306e9eaaee89898282ab Mon Sep 17 00:00:00 2001 From: Timerix Date: Thu, 24 Jul 2025 18:26:51 +0300 Subject: [PATCH] properly named constructors and destructors --- README.md | 2 +- dependencies/tlibc | 2 +- src/VM/Display/Display.c | 8 +++----- src/VM/Display/Display.h | 2 +- src/VM/VM.c | 2 +- src/VM/VM.h | 2 +- src/compiler/AST.c | 10 +++++----- src/compiler/AST.h | 8 ++++---- src/compiler/Binary.c | 10 +++++----- src/compiler/Binary.h | 4 ++-- src/compiler/Compiler.c | 10 +++++----- src/compiler/Compiler.h | 4 ++-- src/compiler/Parser.c | 2 +- src/instructions/instructions.c | 8 ++++---- src/instructions/instructions.h | 2 +- src/main.c | 10 +++++----- 16 files changed, 42 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index 3164634..abddb06 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Machine code interpreter written in pure C. Can execute programs up to 1 MEGABYT ## Building 1. Clone repo ``` - git clone --recurse-submodules --depth 1 https://timerix.ddns.net/git/Timerix/tcpu.git + git clone --recurse-submodules https://timerix.ddns.net/git/Timerix/tcpu.git ``` 2. Install [cbuild](https://timerix.ddns.net/git/Timerix/cbuild.git) 3. Install [SDL3](https://github.com/libsdl-org/SDL) and [SDL3_image](https://github.com/libsdl-org/SDL_image) from package manager or source. diff --git a/dependencies/tlibc b/dependencies/tlibc index 51980eb..0e80a56 160000 --- a/dependencies/tlibc +++ b/dependencies/tlibc @@ -1 +1 @@ -Subproject commit 51980ebb0b5d220c0e2c7bffe853d4d3e2628a56 +Subproject commit 0e80a568922ae579213d5a99329fa32420f0bcb0 diff --git a/src/VM/Display/Display.c b/src/VM/Display/Display.c index 3dd1ee5..305dfe5 100644 --- a/src/VM/Display/Display.c +++ b/src/VM/Display/Display.c @@ -13,9 +13,7 @@ typedef struct Display { static SDL_InitState sdl_init_state = {0}; -Display* Display_create(str name, i32 w, i32 h, DisplayFlags flags){ - Display* d = malloc(sizeof(Display)); - +bool Display_tryConstruct(Display* d, str name, i32 w, i32 h, DisplayFlags flags){ d->name = str_copy(name); d->width = w; d->height = h; @@ -26,12 +24,12 @@ Display* Display_create(str name, i32 w, i32 h, DisplayFlags flags){ bool sdl_initialized = SDL_Init(SDL_INIT_VIDEO); SDL_SetInitialized(&sdl_init_state, sdl_initialized); if(!sdl_initialized) - return false; + return NULL; } SDL_WindowFlags window_flags = SDL_WINDOW_ALWAYS_ON_TOP; if(!SDL_CreateWindowAndRenderer(d->name.data, d->width, d->height, window_flags, &d->window, &d->renderer)){ - return false; + return NULL; } return d; diff --git a/src/VM/Display/Display.h b/src/VM/Display/Display.h index 010cd66..fb7bb70 100644 --- a/src/VM/Display/Display.h +++ b/src/VM/Display/Display.h @@ -20,7 +20,7 @@ typedef enum DisplayFlags { typedef struct Display Display; -Display* Display_create(str name, i32 w, i32 h, DisplayFlags flags); +bool Display_tryConstruct(Display* d, str name, i32 w, i32 h, DisplayFlags flags); void Display_destroy(Display* d); bool Display_setName(Display* d, str name); diff --git a/src/VM/VM.c b/src/VM/VM.c index 766b8c7..a6bd0e2 100644 --- a/src/VM/VM.c +++ b/src/VM/VM.c @@ -1,7 +1,7 @@ #include "VM.h" #include "../instructions/instructions.h" -void VM_init(VM* vm){ +void VM_construct(VM* vm){ memset(vm, 0, sizeof(VM)); vm->state = VMState_Initialized; } diff --git a/src/VM/VM.h b/src/VM/VM.h index 41707b2..f5301c7 100644 --- a/src/VM/VM.h +++ b/src/VM/VM.h @@ -43,7 +43,7 @@ typedef struct VM { size_t current_pos; } VM; -void VM_init(VM* vm); +void VM_construct(VM* vm); /// @brief Loads a program from the buffer. /// @param data buffer starting with machine code diff --git a/src/compiler/AST.c b/src/compiler/AST.c index a152725..3e2f092 100644 --- a/src/compiler/AST.c +++ b/src/compiler/AST.c @@ -16,13 +16,13 @@ str ArgumentType_toString(ArgumentType t){ } -void Section_init(Section* sec, str name){ +void Section_construct(Section* sec, str name){ sec->name = name; sec->data_definitions_list = List_alloc(DataDefinition, 256); sec->operations_list = List_alloc(Operation, 1024); } -void Section_free(Section* sec){ +void Section_destroy(Section* sec){ for(u32 i = 0; i < Array_len(&sec->data_definitions_list, DataDefinition); i++){ DataDefinition* dd = (DataDefinition*)sec->data_definitions_list.data + i; free(dd->data_bytes.data); @@ -37,13 +37,13 @@ void Section_free(Section* sec){ } -void AST_init(AST* ast){ +void AST_construct(AST* ast){ ast->sections = List_alloc(Section, 32); } -void AST_free(AST* ast){ +void AST_destroy(AST* ast){ for(u32 i = 0; i != Array_len(&ast->sections, Section); i++){ - Section_free((Section*)ast->sections.data + i); + Section_destroy((Section*)ast->sections.data + i); } free(ast->sections.data); } diff --git a/src/compiler/AST.h b/src/compiler/AST.h index 5355d17..5a2fd17 100644 --- a/src/compiler/AST.h +++ b/src/compiler/AST.h @@ -46,12 +46,12 @@ typedef struct Section { List(Operation) operations_list; } Section; -void Section_init(Section* Section, str name); -void Section_free(Section* Section); +void Section_construct(Section* Section, str name); +void Section_destroy(Section* Section); typedef struct AST { List(Section) sections; } AST; -void AST_init(AST* ast); -void AST_free(AST* ast); +void AST_construct(AST* ast); +void AST_destroy(AST* ast); diff --git a/src/compiler/Binary.c b/src/compiler/Binary.c index 2fc1ce0..d26c8eb 100644 --- a/src/compiler/Binary.c +++ b/src/compiler/Binary.c @@ -9,7 +9,7 @@ void CompiledSection_construct(CompiledSection* ptr, str name){ ptr->bytes = List_alloc(u8, 64); } -void CompiledSection_free(CompiledSection* ptr){ +void CompiledSection_destroy(CompiledSection* ptr){ free(ptr->const_data_props_list.data); free(ptr->named_refs.data); free(ptr->bytes.data); @@ -18,16 +18,16 @@ void CompiledSection_free(CompiledSection* ptr){ void BinaryObject_construct(BinaryObject* ptr){ ptr->comp_sec_list = List_alloc(CompiledSection, 64); - HashMap_create(&ptr->comp_sec_i_map, u32, NULL); - HashMap_create(&ptr->const_data_props_map, ConstDataProps, NULL); + HashMap_construct(&ptr->comp_sec_i_map, u32, NULL); + HashMap_construct(&ptr->const_data_props_map, ConstDataProps, NULL); ptr->main_sec = NULL; ptr->total_size = 0; } -void BinaryObject_free(BinaryObject* ptr){ +void BinaryObject_destroy(BinaryObject* ptr){ for(u32 i = 0; i < List_len(&ptr->comp_sec_list, CompiledSection); i++){ CompiledSection* sec_ptr = (CompiledSection*)ptr->comp_sec_list.data + i; - CompiledSection_free(sec_ptr); + CompiledSection_destroy(sec_ptr); } free(ptr->comp_sec_list.data); diff --git a/src/compiler/Binary.h b/src/compiler/Binary.h index 8eeb2bd..08c12d6 100644 --- a/src/compiler/Binary.h +++ b/src/compiler/Binary.h @@ -42,7 +42,7 @@ typedef struct CompiledSection { } CompiledSection; void CompiledSection_construct(CompiledSection* ptr, str name); -void CompiledSection_free(CompiledSection* ptr); +void CompiledSection_destroy(CompiledSection* ptr); typedef struct BinaryObject { @@ -54,4 +54,4 @@ typedef struct BinaryObject { } BinaryObject; void BinaryObject_construct(BinaryObject* ptr); -void BinaryObject_free(BinaryObject* ptr); +void BinaryObject_destroy(BinaryObject* ptr); diff --git a/src/compiler/Compiler.c b/src/compiler/Compiler.c index 732be9b..8b4f9a6 100644 --- a/src/compiler/Compiler.c +++ b/src/compiler/Compiler.c @@ -1,20 +1,20 @@ #include "Compiler_internal.h" -void Compiler_init(Compiler* cmp){ +void Compiler_construct(Compiler* cmp){ memset(cmp, 0, sizeof(Compiler)); cmp->state = CompilerState_Initial; cmp->tokens = List_alloc(Token, 4096); cmp->line_lengths = List_alloc(u32, 1024); - AST_init(&cmp->ast); + AST_construct(&cmp->ast); BinaryObject_construct(&cmp->binary); } -void Compiler_free(Compiler* cmp){ +void Compiler_destroy(Compiler* cmp){ free(cmp->code.data); free(cmp->tokens.data); free(cmp->line_lengths.data); - AST_free(&cmp->ast); - BinaryObject_free(&cmp->binary); + AST_destroy(&cmp->ast); + BinaryObject_destroy(&cmp->binary); } CodePos Compiler_getLineAndColumn(Compiler* cmp, u32 pos){ diff --git a/src/compiler/Compiler.h b/src/compiler/Compiler.h index c9e42fc..0c050bf 100644 --- a/src/compiler/Compiler.h +++ b/src/compiler/Compiler.h @@ -33,8 +33,8 @@ typedef struct Compiler { BinaryObject binary; } Compiler; -void Compiler_init(Compiler* cmp); -void Compiler_free(Compiler* cmp); +void Compiler_construct(Compiler* cmp); +void Compiler_destroy(Compiler* cmp); /// @brief compile assembly language code to machine code /// @return true if no errors, false if any error occured (check cmp->error_message) diff --git a/src/compiler/Parser.c b/src/compiler/Parser.c index 8d6f545..c92c838 100644 --- a/src/compiler/Parser.c +++ b/src/compiler/Parser.c @@ -253,7 +253,7 @@ bool Compiler_parse(Compiler* cmp){ case TokenType_Label: // create new section sec = List_expand_size(&cmp->ast.sections, sizeof(Section)); - Section_init(sec, Compiler_constructTokenStr(cmp, tok)); + Section_construct(sec, Compiler_constructTokenStr(cmp, tok)); break; case TokenType_Instruction: if(sec == NULL) diff --git a/src/instructions/instructions.c b/src/instructions/instructions.c index b05effb..8f1027a 100644 --- a/src/instructions/instructions.c +++ b/src/instructions/instructions.c @@ -72,9 +72,9 @@ const Instruction* Instruction_getByOpcode(Opcode opcode){ static HashMap(Opcode)* opcode_map = NULL; -static void _opcode_map_init(){ +static void _opcode_map_construct(){ opcode_map = malloc(sizeof(*opcode_map)); - HashMap_create(opcode_map, Opcode, NULL); + HashMap_construct(opcode_map, Opcode, NULL); for(u32 i = 0; i < Array_len(&instructions_array, Instruction); i++){ Instruction* instr_ptr = (Instruction*)instructions_array.data + i; HashMap_tryPush(opcode_map, instr_ptr->name, &instr_ptr->opcode); @@ -83,7 +83,7 @@ static void _opcode_map_init(){ const Instruction* Instruction_getByName(str name){ if(opcode_map == NULL) - _opcode_map_init(); + _opcode_map_construct(); str name_upper = str_toUpper(name); Opcode* op_ptr = HashMap_tryGetPtr(opcode_map, name_upper); @@ -93,7 +93,7 @@ const Instruction* Instruction_getByName(str name){ return Instruction_getByOpcode(*op_ptr); } -void Instruction_freeSearchStructs(){ +void Instruction_destroySearchStructs(){ if(opcode_map != NULL){ HashMap_destroy(opcode_map); free(opcode_map); diff --git a/src/instructions/instructions.h b/src/instructions/instructions.h index 5ec7b72..053c521 100644 --- a/src/instructions/instructions.h +++ b/src/instructions/instructions.h @@ -53,4 +53,4 @@ typedef struct Instruction { /// @return ptr to struct or NULL const Instruction* NULLABLE(Instruction_getByOpcode)(Opcode opcode); const Instruction* NULLABLE(Instruction_getByName)(str name); -void Instruction_freeSearchStructs(); +void Instruction_destroySearchStructs(); diff --git a/src/main.c b/src/main.c index ef177c0..fc943f8 100644 --- a/src/main.c +++ b/src/main.c @@ -94,7 +94,7 @@ i32 main(const i32 argc, cstr* argv){ } // frees global variables to supress valgrind memory leak errors - Instruction_freeSearchStructs(); + Instruction_destroySearchStructs(); return exit_code; } @@ -118,7 +118,7 @@ i32 bootFromImage(cstr image_file){ } VM vm; - VM_init(&vm); + VM_construct(&vm); i32 exit_code = 1; if(VM_setMemory(&vm, vm_memory, bytes_read)){ @@ -142,7 +142,7 @@ i32 bootFromImage(cstr image_file){ i32 compileSources(cstr source_file, cstr out_file, bool debug_log){ Compiler cmp; - Compiler_init(&cmp); + Compiler_construct(&cmp); bool success = Compiler_compile(&cmp, source_file, out_file, debug_log); if(!success){ if(cmp.error_message){ @@ -150,10 +150,10 @@ i32 compileSources(cstr source_file, cstr out_file, bool debug_log){ free(cmp.error_message); } else printfe("COMPILER ERROR: unknown (error_message is null)\n"); - Compiler_free(&cmp); + Compiler_destroy(&cmp); return 111; } - Compiler_free(&cmp); + Compiler_destroy(&cmp); return 0; }