properly named constructors and destructors
This commit is contained in:
parent
72f8e196a7
commit
17baabbd95
@ -4,7 +4,7 @@ Machine code interpreter written in pure C. Can execute programs up to 1 MEGABYT
|
|||||||
## Building
|
## Building
|
||||||
1. Clone repo
|
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)
|
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.
|
3. Install [SDL3](https://github.com/libsdl-org/SDL) and [SDL3_image](https://github.com/libsdl-org/SDL_image) from package manager or source.
|
||||||
|
|||||||
2
dependencies/tlibc
vendored
2
dependencies/tlibc
vendored
@ -1 +1 @@
|
|||||||
Subproject commit 51980ebb0b5d220c0e2c7bffe853d4d3e2628a56
|
Subproject commit 0e80a568922ae579213d5a99329fa32420f0bcb0
|
||||||
@ -13,9 +13,7 @@ typedef struct Display {
|
|||||||
static SDL_InitState sdl_init_state = {0};
|
static SDL_InitState sdl_init_state = {0};
|
||||||
|
|
||||||
|
|
||||||
Display* Display_create(str name, i32 w, i32 h, DisplayFlags flags){
|
bool Display_tryConstruct(Display* d, str name, i32 w, i32 h, DisplayFlags flags){
|
||||||
Display* d = malloc(sizeof(Display));
|
|
||||||
|
|
||||||
d->name = str_copy(name);
|
d->name = str_copy(name);
|
||||||
d->width = w;
|
d->width = w;
|
||||||
d->height = h;
|
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);
|
bool sdl_initialized = SDL_Init(SDL_INIT_VIDEO);
|
||||||
SDL_SetInitialized(&sdl_init_state, sdl_initialized);
|
SDL_SetInitialized(&sdl_init_state, sdl_initialized);
|
||||||
if(!sdl_initialized)
|
if(!sdl_initialized)
|
||||||
return false;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_WindowFlags window_flags = SDL_WINDOW_ALWAYS_ON_TOP;
|
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)){
|
if(!SDL_CreateWindowAndRenderer(d->name.data, d->width, d->height, window_flags, &d->window, &d->renderer)){
|
||||||
return false;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
return d;
|
return d;
|
||||||
|
|||||||
@ -20,7 +20,7 @@ typedef enum DisplayFlags {
|
|||||||
|
|
||||||
typedef struct Display Display;
|
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);
|
void Display_destroy(Display* d);
|
||||||
|
|
||||||
bool Display_setName(Display* d, str name);
|
bool Display_setName(Display* d, str name);
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
#include "VM.h"
|
#include "VM.h"
|
||||||
#include "../instructions/instructions.h"
|
#include "../instructions/instructions.h"
|
||||||
|
|
||||||
void VM_init(VM* vm){
|
void VM_construct(VM* vm){
|
||||||
memset(vm, 0, sizeof(VM));
|
memset(vm, 0, sizeof(VM));
|
||||||
vm->state = VMState_Initialized;
|
vm->state = VMState_Initialized;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -43,7 +43,7 @@ typedef struct VM {
|
|||||||
size_t current_pos;
|
size_t current_pos;
|
||||||
} VM;
|
} VM;
|
||||||
|
|
||||||
void VM_init(VM* vm);
|
void VM_construct(VM* vm);
|
||||||
|
|
||||||
/// @brief Loads a program from the buffer.
|
/// @brief Loads a program from the buffer.
|
||||||
/// @param data buffer starting with machine code
|
/// @param data buffer starting with machine code
|
||||||
|
|||||||
@ -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->name = name;
|
||||||
sec->data_definitions_list = List_alloc(DataDefinition, 256);
|
sec->data_definitions_list = List_alloc(DataDefinition, 256);
|
||||||
sec->operations_list = List_alloc(Operation, 1024);
|
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++){
|
for(u32 i = 0; i < Array_len(&sec->data_definitions_list, DataDefinition); i++){
|
||||||
DataDefinition* dd = (DataDefinition*)sec->data_definitions_list.data + i;
|
DataDefinition* dd = (DataDefinition*)sec->data_definitions_list.data + i;
|
||||||
free(dd->data_bytes.data);
|
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);
|
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++){
|
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);
|
free(ast->sections.data);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -46,12 +46,12 @@ typedef struct Section {
|
|||||||
List(Operation) operations_list;
|
List(Operation) operations_list;
|
||||||
} Section;
|
} Section;
|
||||||
|
|
||||||
void Section_init(Section* Section, str name);
|
void Section_construct(Section* Section, str name);
|
||||||
void Section_free(Section* Section);
|
void Section_destroy(Section* Section);
|
||||||
|
|
||||||
typedef struct AST {
|
typedef struct AST {
|
||||||
List(Section) sections;
|
List(Section) sections;
|
||||||
} AST;
|
} AST;
|
||||||
|
|
||||||
void AST_init(AST* ast);
|
void AST_construct(AST* ast);
|
||||||
void AST_free(AST* ast);
|
void AST_destroy(AST* ast);
|
||||||
|
|||||||
@ -9,7 +9,7 @@ void CompiledSection_construct(CompiledSection* ptr, str name){
|
|||||||
ptr->bytes = List_alloc(u8, 64);
|
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->const_data_props_list.data);
|
||||||
free(ptr->named_refs.data);
|
free(ptr->named_refs.data);
|
||||||
free(ptr->bytes.data);
|
free(ptr->bytes.data);
|
||||||
@ -18,16 +18,16 @@ void CompiledSection_free(CompiledSection* ptr){
|
|||||||
|
|
||||||
void BinaryObject_construct(BinaryObject* ptr){
|
void BinaryObject_construct(BinaryObject* ptr){
|
||||||
ptr->comp_sec_list = List_alloc(CompiledSection, 64);
|
ptr->comp_sec_list = List_alloc(CompiledSection, 64);
|
||||||
HashMap_create(&ptr->comp_sec_i_map, u32, NULL);
|
HashMap_construct(&ptr->comp_sec_i_map, u32, NULL);
|
||||||
HashMap_create(&ptr->const_data_props_map, ConstDataProps, NULL);
|
HashMap_construct(&ptr->const_data_props_map, ConstDataProps, NULL);
|
||||||
ptr->main_sec = NULL;
|
ptr->main_sec = NULL;
|
||||||
ptr->total_size = 0;
|
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++){
|
for(u32 i = 0; i < List_len(&ptr->comp_sec_list, CompiledSection); i++){
|
||||||
CompiledSection* sec_ptr = (CompiledSection*)ptr->comp_sec_list.data + 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);
|
free(ptr->comp_sec_list.data);
|
||||||
|
|
||||||
|
|||||||
@ -42,7 +42,7 @@ typedef struct CompiledSection {
|
|||||||
} CompiledSection;
|
} CompiledSection;
|
||||||
|
|
||||||
void CompiledSection_construct(CompiledSection* ptr, str name);
|
void CompiledSection_construct(CompiledSection* ptr, str name);
|
||||||
void CompiledSection_free(CompiledSection* ptr);
|
void CompiledSection_destroy(CompiledSection* ptr);
|
||||||
|
|
||||||
|
|
||||||
typedef struct BinaryObject {
|
typedef struct BinaryObject {
|
||||||
@ -54,4 +54,4 @@ typedef struct BinaryObject {
|
|||||||
} BinaryObject;
|
} BinaryObject;
|
||||||
|
|
||||||
void BinaryObject_construct(BinaryObject* ptr);
|
void BinaryObject_construct(BinaryObject* ptr);
|
||||||
void BinaryObject_free(BinaryObject* ptr);
|
void BinaryObject_destroy(BinaryObject* ptr);
|
||||||
|
|||||||
@ -1,20 +1,20 @@
|
|||||||
#include "Compiler_internal.h"
|
#include "Compiler_internal.h"
|
||||||
|
|
||||||
void Compiler_init(Compiler* cmp){
|
void Compiler_construct(Compiler* cmp){
|
||||||
memset(cmp, 0, sizeof(Compiler));
|
memset(cmp, 0, sizeof(Compiler));
|
||||||
cmp->state = CompilerState_Initial;
|
cmp->state = CompilerState_Initial;
|
||||||
cmp->tokens = List_alloc(Token, 4096);
|
cmp->tokens = List_alloc(Token, 4096);
|
||||||
cmp->line_lengths = List_alloc(u32, 1024);
|
cmp->line_lengths = List_alloc(u32, 1024);
|
||||||
AST_init(&cmp->ast);
|
AST_construct(&cmp->ast);
|
||||||
BinaryObject_construct(&cmp->binary);
|
BinaryObject_construct(&cmp->binary);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Compiler_free(Compiler* cmp){
|
void Compiler_destroy(Compiler* cmp){
|
||||||
free(cmp->code.data);
|
free(cmp->code.data);
|
||||||
free(cmp->tokens.data);
|
free(cmp->tokens.data);
|
||||||
free(cmp->line_lengths.data);
|
free(cmp->line_lengths.data);
|
||||||
AST_free(&cmp->ast);
|
AST_destroy(&cmp->ast);
|
||||||
BinaryObject_free(&cmp->binary);
|
BinaryObject_destroy(&cmp->binary);
|
||||||
}
|
}
|
||||||
|
|
||||||
CodePos Compiler_getLineAndColumn(Compiler* cmp, u32 pos){
|
CodePos Compiler_getLineAndColumn(Compiler* cmp, u32 pos){
|
||||||
|
|||||||
@ -33,8 +33,8 @@ typedef struct Compiler {
|
|||||||
BinaryObject binary;
|
BinaryObject binary;
|
||||||
} Compiler;
|
} Compiler;
|
||||||
|
|
||||||
void Compiler_init(Compiler* cmp);
|
void Compiler_construct(Compiler* cmp);
|
||||||
void Compiler_free(Compiler* cmp);
|
void Compiler_destroy(Compiler* cmp);
|
||||||
|
|
||||||
/// @brief compile assembly language code to machine code
|
/// @brief compile assembly language code to machine code
|
||||||
/// @return true if no errors, false if any error occured (check cmp->error_message)
|
/// @return true if no errors, false if any error occured (check cmp->error_message)
|
||||||
|
|||||||
@ -253,7 +253,7 @@ bool Compiler_parse(Compiler* cmp){
|
|||||||
case TokenType_Label:
|
case TokenType_Label:
|
||||||
// create new section
|
// create new section
|
||||||
sec = List_expand_size(&cmp->ast.sections, sizeof(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;
|
break;
|
||||||
case TokenType_Instruction:
|
case TokenType_Instruction:
|
||||||
if(sec == NULL)
|
if(sec == NULL)
|
||||||
|
|||||||
@ -72,9 +72,9 @@ const Instruction* Instruction_getByOpcode(Opcode opcode){
|
|||||||
|
|
||||||
static HashMap(Opcode)* opcode_map = NULL;
|
static HashMap(Opcode)* opcode_map = NULL;
|
||||||
|
|
||||||
static void _opcode_map_init(){
|
static void _opcode_map_construct(){
|
||||||
opcode_map = malloc(sizeof(*opcode_map));
|
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++){
|
for(u32 i = 0; i < Array_len(&instructions_array, Instruction); i++){
|
||||||
Instruction* instr_ptr = (Instruction*)instructions_array.data + i;
|
Instruction* instr_ptr = (Instruction*)instructions_array.data + i;
|
||||||
HashMap_tryPush(opcode_map, instr_ptr->name, &instr_ptr->opcode);
|
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){
|
const Instruction* Instruction_getByName(str name){
|
||||||
if(opcode_map == NULL)
|
if(opcode_map == NULL)
|
||||||
_opcode_map_init();
|
_opcode_map_construct();
|
||||||
|
|
||||||
str name_upper = str_toUpper(name);
|
str name_upper = str_toUpper(name);
|
||||||
Opcode* op_ptr = HashMap_tryGetPtr(opcode_map, name_upper);
|
Opcode* op_ptr = HashMap_tryGetPtr(opcode_map, name_upper);
|
||||||
@ -93,7 +93,7 @@ const Instruction* Instruction_getByName(str name){
|
|||||||
return Instruction_getByOpcode(*op_ptr);
|
return Instruction_getByOpcode(*op_ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Instruction_freeSearchStructs(){
|
void Instruction_destroySearchStructs(){
|
||||||
if(opcode_map != NULL){
|
if(opcode_map != NULL){
|
||||||
HashMap_destroy(opcode_map);
|
HashMap_destroy(opcode_map);
|
||||||
free(opcode_map);
|
free(opcode_map);
|
||||||
|
|||||||
@ -53,4 +53,4 @@ typedef struct Instruction {
|
|||||||
/// @return ptr to struct or NULL
|
/// @return ptr to struct or NULL
|
||||||
const Instruction* NULLABLE(Instruction_getByOpcode)(Opcode opcode);
|
const Instruction* NULLABLE(Instruction_getByOpcode)(Opcode opcode);
|
||||||
const Instruction* NULLABLE(Instruction_getByName)(str name);
|
const Instruction* NULLABLE(Instruction_getByName)(str name);
|
||||||
void Instruction_freeSearchStructs();
|
void Instruction_destroySearchStructs();
|
||||||
|
|||||||
10
src/main.c
10
src/main.c
@ -94,7 +94,7 @@ i32 main(const i32 argc, cstr* argv){
|
|||||||
}
|
}
|
||||||
|
|
||||||
// frees global variables to supress valgrind memory leak errors
|
// frees global variables to supress valgrind memory leak errors
|
||||||
Instruction_freeSearchStructs();
|
Instruction_destroySearchStructs();
|
||||||
return exit_code;
|
return exit_code;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -118,7 +118,7 @@ i32 bootFromImage(cstr image_file){
|
|||||||
}
|
}
|
||||||
|
|
||||||
VM vm;
|
VM vm;
|
||||||
VM_init(&vm);
|
VM_construct(&vm);
|
||||||
|
|
||||||
i32 exit_code = 1;
|
i32 exit_code = 1;
|
||||||
if(VM_setMemory(&vm, vm_memory, bytes_read)){
|
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){
|
i32 compileSources(cstr source_file, cstr out_file, bool debug_log){
|
||||||
Compiler cmp;
|
Compiler cmp;
|
||||||
Compiler_init(&cmp);
|
Compiler_construct(&cmp);
|
||||||
bool success = Compiler_compile(&cmp, source_file, out_file, debug_log);
|
bool success = Compiler_compile(&cmp, source_file, out_file, debug_log);
|
||||||
if(!success){
|
if(!success){
|
||||||
if(cmp.error_message){
|
if(cmp.error_message){
|
||||||
@ -150,10 +150,10 @@ i32 compileSources(cstr source_file, cstr out_file, bool debug_log){
|
|||||||
free(cmp.error_message);
|
free(cmp.error_message);
|
||||||
}
|
}
|
||||||
else printfe("COMPILER ERROR: unknown (error_message is null)\n");
|
else printfe("COMPILER ERROR: unknown (error_message is null)\n");
|
||||||
Compiler_free(&cmp);
|
Compiler_destroy(&cmp);
|
||||||
return 111;
|
return 111;
|
||||||
}
|
}
|
||||||
|
|
||||||
Compiler_free(&cmp);
|
Compiler_destroy(&cmp);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user