aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/ast.c5
-rw-r--r--src/bytecode/bytecode.c48
-rw-r--r--src/compiler.c11
-rw-r--r--src/parser.c2
-rw-r--r--src/program.c162
-rw-r--r--src/std/arena_allocator.c5
-rw-r--r--src/std/buffer.c8
-rw-r--r--src/std/buffer_view.c8
-rw-r--r--src/tokenizer.c11
9 files changed, 131 insertions, 129 deletions
diff --git a/src/ast.c b/src/ast.c
index 912b9c7..8e102ee 100644
--- a/src/ast.c
+++ b/src/ast.c
@@ -560,6 +560,9 @@ static void lhsexpr_resolve_rhs(LhsExpr *self, AstCompilerContext *context, AstR
if(ast_is_decl(self->rhs_expr)) {
result->type = RESOLVED_TYPE_LHS_EXPR;
result->value.lhs_expr = self;
+ /* Structs resolved type becomes the lhs while functions resolved type becomes the function signature they own */
+ if (self->rhs_expr->type == AST_STRUCT_DECL)
+ self->rhs_expr->resolve_data.type = *result;
} else {
*result = self->rhs_expr->resolve_data.type;
}
@@ -753,6 +756,7 @@ static TypeSize variable_type_get_byte_size(VariableType *self) {
type_size.fixed_size = 0;
switch(self->type) {
case VARIABLE_TYPE_NONE:
+ assert(bool_false && "Variable type not resolved!");
break;
case VARIABLE_TYPE_VARIABLE:
type_size = resolved_type_get_byte_size(&self->value.variable->resolved_var.resolve_data->type);
@@ -943,6 +947,7 @@ TypeSize resolved_type_get_byte_size(AstResolvedType *self) {
type_size.fixed_size = 0;
switch(self->type) {
case RESOLVED_TYPE_NONE:
+ assert(bool_false && "Type not resolved!");
break;
case RESOLVED_TYPE_LHS_EXPR: {
/* Resolved type until rhs is StructDecl or FunctionSignature */
diff --git a/src/bytecode/bytecode.c b/src/bytecode/bytecode.c
index 2c43471..c968743 100644
--- a/src/bytecode/bytecode.c
+++ b/src/bytecode/bytecode.c
@@ -155,9 +155,9 @@ static TypeSize function_signature_get_return_types_size(FunctionSignature *self
return_types_total_size.num_pointers = 0;
return_types_total_size.fixed_size = 0;
for(; return_type != return_type_end; ++return_type) {
- TypeSize param_size = resolved_type_get_byte_size(&return_type->resolved_type);
- return_types_total_size.num_pointers += param_size.num_pointers;
- return_types_total_size.fixed_size += param_size.fixed_size;
+ TypeSize return_size = resolved_type_get_byte_size(&return_type->resolved_type);
+ return_types_total_size.num_pointers += return_size.num_pointers;
+ return_types_total_size.fixed_size += return_size.fixed_size;
}
return return_types_total_size;
}
@@ -187,7 +187,7 @@ static void add_functions(BytecodeCompilerContext *self) {
Buffer *instructions = &self->bytecode->data;
SsaFunc *func = buffer_begin(&ssa->funcs);
SsaFunc *func_end = buffer_end(&ssa->funcs);
- u32 funcs_size = ssa->func_counter * sizeof(BytecodeHeaderFunction);
+ u32 funcs_size = (u32)ssa->func_counter * sizeof(BytecodeHeaderFunction);
assert(sizeof(BytecodeHeaderFunction) == 22);
self->bytecode->funcs_index = instructions->size;
@@ -224,20 +224,26 @@ static void add_extern_functions(BytecodeCompilerContext *self) {
|External function[]|External functions|Multiple external functions, where the number of functions is defined by @num_extern_func|
# External function
- |Type|Field |Description |
- |----|----------|-----------------------------------------------------------------------------------------------------|
- |u8 |num_params|The number of parameters the functions has. |
- |u8 |name_len |The length of the external function name, in bytes. Excluding the null-terminate character. |
- |u8[]|name |The name of the external function, where the size is defined by @name_len. Names are null-terminated.|
+ |Type|Field |Description |
+ |----|-------------------------|-----------------------------------------------------------------------------------------------------|
+ |u8 |num_params |The number of parameters. |
+ |u32 |params_num_pointers |The number of pointers in the parameters. |
+ |u32 |params_fixed_size |The size of all non-pointer type parameters, in bytes. |
+ |u8 |num_return_types |The number of return values. |
+ |u32 |return_types_num_pointers|The number of pointers in the return types. |
+ |u32 |return_types_fixed_size |The size of all non-pointer type return types, in bytes. |
+ |u8 |name_len |The length of the external function name, in bytes. Excluding the null-terminate character. |
+ |u8[]|name |The name of the external function, where the size is defined by @name_len. Names are null-terminated.|
*/
Ssa *ssa = self->parser->ssa;
Buffer *instructions = &self->bytecode->data;
SsaExternFunc *extern_func = buffer_begin(&ssa->extern_funcs);
SsaExternFunc *extern_func_end = buffer_end(&ssa->extern_funcs);
- u32 extern_funcs_size = 0;
+ u32 extern_funcs_size = (u32)ssa->extern_func_counter * (sizeof(BytecodeHeaderExternFunction) + sizeof(u8));
+ assert(sizeof(BytecodeHeaderExternFunction) == 18);
for(; extern_func != extern_func_end; ++extern_func) {
- extern_funcs_size += sizeof(u8) + sizeof(u8) + extern_func->name.size + 1; /* +1 for null-termination of string */
+ extern_funcs_size += extern_func->name.size + 1; /* +1 for null-termination of string */
}
extern_func = buffer_begin(&ssa->extern_funcs);
@@ -247,8 +253,19 @@ static void add_extern_functions(BytecodeCompilerContext *self) {
throw_if_error(buffer_append(instructions, &extern_funcs_size, sizeof(u32)));
for(; extern_func != extern_func_end; ++extern_func) {
const char null_s = '\0';
- u8 num_params = buffer_get_size(&extern_func->func_sig->parameters, FunctionParameter);
- throw_if_error(buffer_append(instructions, &num_params, sizeof(num_params)));
+ TypeSize params_total_size = function_signature_get_params_size(extern_func->func_sig);
+ TypeSize return_types_total_size = function_signature_get_return_types_size(extern_func->func_sig);
+ BytecodeHeaderExternFunction header_func;
+
+ header_func.num_params = buffer_get_size(&extern_func->func_sig->parameters, FunctionParameter);
+ header_func.params_num_pointers = params_total_size.num_pointers;
+ header_func.params_fixed_size = params_total_size.fixed_size;
+
+ header_func.num_return_types = buffer_get_size(&extern_func->func_sig->return_types, FunctionReturnType);
+ header_func.return_types_num_pointers = return_types_total_size.num_pointers;
+ header_func.return_types_fixed_size = return_types_total_size.fixed_size;
+ throw_if_error(buffer_append(instructions, &header_func, sizeof(header_func)));
+
/* TODO: Add namespace to the function name */
/* u8 is fine, because the max length of a variable is 255 */
throw_if_error(buffer_append(instructions, &extern_func->name.size, sizeof(u8)));
@@ -280,10 +297,10 @@ static void add_export_functions(BytecodeCompilerContext *self) {
Buffer *instructions = &self->bytecode->data;
SsaExportFunc *export_func = buffer_begin(&ssa->export_funcs);
SsaExportFunc *export_func_end = buffer_end(&ssa->export_funcs);
- u32 export_funcs_size = 0;
+ u32 export_funcs_size = (u32)ssa->export_func_counter * (sizeof(u32) + sizeof(u8) + sizeof(u8));
for(; export_func != export_func_end; ++export_func) {
- export_funcs_size += sizeof(u32) + sizeof(u8) + sizeof(u8) + export_func->name.size + 1; /* +1 for null-termination of string */
+ export_funcs_size += export_func->name.size + 1; /* +1 for null-termination of string */
}
export_func = buffer_begin(&ssa->export_funcs);
@@ -452,7 +469,6 @@ static void add_instructions(BytecodeCompilerContext *self) {
u32 num_instructions_index = self->bytecode->data.size;
throw_if_error(buffer_append_empty(&self->bytecode->data, sizeof(num_instructions_index)));
- /* TODO: Keep all registers under 256 */
while(instruction != instructions_end) {
SsaInstruction ins = (SsaInstruction)*instruction++;
switch(ins) {
diff --git a/src/compiler.c b/src/compiler.c
index 8dda1c6..6e6bc4b 100644
--- a/src/compiler.c
+++ b/src/compiler.c
@@ -44,6 +44,7 @@ static CHECK_RESULT int create_default_type(amal_compiler *compiler, const char
expr->resolve_data.type.type = RESOLVED_TYPE_LHS_EXPR;
expr->resolve_data.type.value.lhs_expr = lhs_expr;
expr->resolve_data.status = AST_RESOLVED;
+ lhs_expr->rhs_expr->resolve_data = expr->resolve_data;
return scope_add_child(&compiler->root_scope, expr);
}
@@ -58,12 +59,12 @@ static CHECK_RESULT int create_default_type_fixed_size(amal_compiler *compiler,
static CHECK_RESULT int init_default_types(amal_compiler *compiler) {
return_if_error(create_default_type_fixed_size(compiler, "i8", 1, &compiler->default_types.i8));
return_if_error(create_default_type_fixed_size(compiler, "i16", 2, &compiler->default_types.i16));
- return_if_error(create_default_type_fixed_size(compiler, "i32", 3, &compiler->default_types.i32));
- return_if_error(create_default_type_fixed_size(compiler, "i64", 4, &compiler->default_types.i64));
+ return_if_error(create_default_type_fixed_size(compiler, "i32", 4, &compiler->default_types.i32));
+ return_if_error(create_default_type_fixed_size(compiler, "i64", 8, &compiler->default_types.i64));
return_if_error(create_default_type_fixed_size(compiler, "u8", 1, &compiler->default_types.u8));
return_if_error(create_default_type_fixed_size(compiler, "u16", 2, &compiler->default_types.u16));
- return_if_error(create_default_type_fixed_size(compiler, "u32", 3, &compiler->default_types.u32));
- return_if_error(create_default_type_fixed_size(compiler, "u64", 4, &compiler->default_types.u64));
+ return_if_error(create_default_type_fixed_size(compiler, "u32", 4, &compiler->default_types.u32));
+ return_if_error(create_default_type_fixed_size(compiler, "u64", 8, &compiler->default_types.u64));
return_if_error(create_default_type_num_pointers(compiler, "isize", 1, &compiler->default_types.isize));
return_if_error(create_default_type_num_pointers(compiler, "usize", 1, &compiler->default_types.usize));
return_if_error(create_default_type_fixed_size(compiler, "f32", 4, &compiler->default_types.f32));
@@ -185,7 +186,7 @@ static CHECK_RESULT int amal_compiler_load_in_this_thread(amal_compiler *compile
result = AMAL_COMPILER_ERR;
cleanup_if_error(amal_mutex_lock(&compiler->mutex, "amal_compiler_load_in_this_thread, create arena allocator"));
- return_if_error(arena_allocator_alloc(&compiler->allocator, sizeof(ArenaAllocator), (void**)&parser_allocator));
+ cleanup_if_error(arena_allocator_alloc(&compiler->allocator, sizeof(ArenaAllocator), (void**)&parser_allocator));
amal_mutex_tryunlock(&compiler->mutex);
return_if_error(arena_allocator_init(parser_allocator));
diff --git a/src/parser.c b/src/parser.c
index 01c1d9f..ed99eb0 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -383,7 +383,7 @@ static CHECK_RESULT FunctionDecl* parser_parse_closure(Parser *self) {
TODO: Implement function declaration inside other functions.
Such functions should be moved to the file scope in the bytecode generation
*/
- assert(parser_is_current_scope_file_scope(self));
+ assert(parser_is_current_scope_file_scope(self) && "TODO: Implement function declaration inside other functions.");
throw_if_error(arena_allocator_alloc(self->allocator, sizeof(FunctionDecl), (void**)&result));
throw_if_error(funcdecl_init(result, signature, self->current_scope, self->allocator));
diff --git a/src/program.c b/src/program.c
index a0a2407..5c07c5d 100644
--- a/src/program.c
+++ b/src/program.c
@@ -42,7 +42,6 @@ static usize hash_u64(const u8 *data, usize size) {
int amal_program_init(amal_program *self) {
ignore_result_int(buffer_init(&self->data, NULL));
self->string_indices = NULL;
- self->extern_func_indices = NULL;
self->intermediates_start = NULL;
self->strings_start = NULL;
self->extern_funcs_start = NULL;
@@ -72,15 +71,13 @@ int amal_program_init(amal_program *self) {
void amal_program_deinit(amal_program *self) {
arena_allocator_deinit(&self->allocator);
- am_free(self->extern_func_indices);
am_free(self->string_indices);
- self->extern_func_indices = NULL;
self->string_indices = NULL;
if(self->data.data)
buffer_deinit(&self->data);
}
-int amal_program_add_extern_func(amal_program *self, BufferView name, void *func_ptr, int args_byte_size) {
+int amal_program_register_extern_func(amal_program *self, BufferView name, void *func_ptr, int args_byte_size) {
ProgramExternFunc extern_func;
extern_func.func = func_ptr;
extern_func.args_byte_size = args_byte_size;
@@ -93,44 +90,52 @@ int amal_program_add_extern_func(amal_program *self, BufferView name, void *func
static u8* amal_program_get_extern_funcs_start_by_import_index(amal_program *self, u8 import_index) {
BytecodeHeaderImport *header_import = (BytecodeHeaderImport*)self->imports_start;
+ u32 extern_function_index = 0;
header_import += import_index;
- return (u8*)self->data.data + header_import->extern_function_index;
+ assert(sizeof(extern_function_index) == sizeof(header_import->extern_function_index));
+ am_memcpy(&extern_function_index, &header_import->extern_function_index, sizeof(extern_function_index));
+ return (u8*)self->data.data + extern_function_index;
+}
+
+static u32 extern_func_get_total_param_size(BytecodeHeaderExternFunction *self) {
+ return self->params_fixed_size + self->params_num_pointers * sizeof(void*);
}
typedef struct {
- u8 num_params;
+ BytecodeHeaderExternFunction extern_func;
BufferView name;
-} BytecodeHeaderExternFunction;
+} BytecodeHeaderExternFunctionFull;
/* TODO: Optimize this */
-static void amal_program_get_header_extern_function_by_index(amal_program *self, u8 import_index, u16 index, BytecodeHeaderExternFunction *result) {
+static void amal_program_get_header_extern_function_by_index(amal_program *self, u8 import_index, u16 index, BytecodeHeaderExternFunctionFull *result) {
u32 i;
u8 *extern_funcs_start = amal_program_get_extern_funcs_start_by_import_index(self, import_index);
extern_funcs_start += sizeof(u16) + sizeof(u32);
for(i = 0; i < (u32)index; ++i) {
- u8 name_len = *(extern_funcs_start + sizeof(u8));
- /* +1 for the null-terminated character */
- extern_funcs_start += sizeof(u8) + sizeof(u8) + name_len + 1;
+ u8 name_len;
+ extern_funcs_start += sizeof(BytecodeHeaderExternFunction);
+ name_len = *extern_funcs_start;
+ extern_funcs_start += 1 + name_len + 1; /* +1 for skipping length byte and the null-terminated character */
}
- result->num_params = extern_funcs_start[0];
- result->name.size = extern_funcs_start[1];
- result->name.data = (const char*)extern_funcs_start + sizeof(u8) + sizeof(u8);
+ am_memcpy(&result->extern_func, extern_funcs_start, sizeof(result->extern_func));
+ result->name.size = *(extern_funcs_start + sizeof(result->extern_func));
+ result->name.data = (const char*)extern_funcs_start + sizeof(result->extern_func) + sizeof(u8);
}
static CHECK_RESULT int amal_program_get_extern_func_by_index(amal_program *self, u8 import_index, u16 index, ProgramExternFunc *result) {
- BytecodeHeaderExternFunction extern_func;
+ BytecodeHeaderExternFunctionFull extern_func;
amal_program_get_header_extern_function_by_index(self, import_index, index, &extern_func);
if(!hash_map_get(&self->extern_funcs_map, extern_func.name, result)) {
- amal_log_error("No such extern function: %.*s", extern_func.name.size, extern_func.name.data);
+ amal_log_error("Extern function \"%.*s\" has not been registered", extern_func.name.size, extern_func.name.data);
return AMAL_PROGRAM_NO_SUCH_EXTERNAL_FUNCTION;
}
/* TODO: This assumes all arguments are of size sizeof(isize) */
- if(result->args_byte_size != -1 && result->args_byte_size != extern_func.num_params * (int)sizeof(isize)) {
+ if(result->args_byte_size != -1 && result->args_byte_size != (int)extern_func_get_total_param_size(&extern_func.extern_func)) {
amal_log_error("Extern function %.*s was registered to take %d byte(s), but the program says it takes %d byte(s)",
- extern_func.name.size, extern_func.name.data, result->args_byte_size, extern_func.num_params * sizeof(isize));
+ extern_func.name.size, extern_func.name.data, result->args_byte_size, (int)extern_func_get_total_param_size(&extern_func.extern_func));
return AMAL_PROGRAM_NO_SUCH_EXTERNAL_FUNCTION;
}
return 0;
@@ -234,9 +239,6 @@ static CHECK_RESULT int amal_program_advance_section_magic_number(amal_program *
static CHECK_RESULT int amal_program_read_intermediates(amal_program *self) {
u32 intermediates_size;
- /*u32 read_end;*/
-
- return_if_error(amal_program_advance_section_magic_number(self));
if(bytes_left_to_read(self) < sizeof(intermediates_size)) {
amal_log_error("Not enough space in program to intermediates size");
@@ -262,8 +264,6 @@ static CHECK_RESULT int amal_program_read_strings(amal_program *self) {
u32 strings_size;
u32 *string_index_ptr;
- return_if_error(amal_program_advance_section_magic_number(self));
-
if(!amal_program_read_advance(self, &self->num_strings, sizeof(u16)))
return AMAL_PROGRAM_INVALID_STRINGS;
@@ -309,8 +309,6 @@ static CHECK_RESULT int amal_program_read_strings(amal_program *self) {
static CHECK_RESULT int amal_program_read_functions(amal_program *self) {
u32 funcs_size;
- return_if_error(amal_program_advance_section_magic_number(self));
-
if(!amal_program_read_advance(self, &self->num_functions, sizeof(u16)))
return AMAL_PROGRAM_INVALID_FUNCTIONS;
@@ -324,9 +322,6 @@ static CHECK_RESULT int amal_program_read_functions(amal_program *self) {
static CHECK_RESULT int amal_program_read_external_functions(amal_program *self) {
u32 extern_funcs_size;
- u32 *extern_func_index_ptr;
-
- return_if_error(amal_program_advance_section_magic_number(self));
if(!amal_program_read_advance(self, &self->num_extern_functions, sizeof(u16)))
return AMAL_PROGRAM_INVALID_EXTERNAL_FUNCTIONS;
@@ -337,46 +332,13 @@ static CHECK_RESULT int amal_program_read_external_functions(amal_program *self)
if(bytes_left_to_read(self) < extern_funcs_size)
return AMAL_PROGRAM_INVALID_EXTERNAL_FUNCTIONS_SIZE;
- am_free(self->extern_func_indices);
- self->extern_func_indices = NULL;
- if(am_malloc(sizeof(u32) * self->num_extern_functions, (void**)&self->extern_func_indices) != 0)
- return AMAL_PROGRAM_ALLOC_FAILURE;
- extern_func_index_ptr = self->extern_func_indices;
-
- {
- const u32 read_start = self->read_index;
- const u32 read_end = read_start + extern_funcs_size;
- self->extern_funcs_start = (u8*)(self->data.data + self->read_index);
- while(self->read_index < read_end) {
- u8 num_params;
- u8 func_name_size;
-
- if(bytes_left_to_read(self) < sizeof(num_params) + sizeof(func_name_size))
- return AMAL_PROGRAM_INVALID_EXTERNAL_FUNCTIONS;
-
- *extern_func_index_ptr = self->read_index - read_start;
- ++extern_func_index_ptr;
- num_params = self->data.data[self->read_index];
- func_name_size = self->data.data[self->read_index + sizeof(num_params)];
- self->read_index += sizeof(num_params) + sizeof(func_name_size);
-
- /* +1 to skip null-termination character */
- if(bytes_left_to_read(self) < func_name_size + 1U)
- return AMAL_PROGRAM_INVALID_EXTERNAL_FUNCTIONS;
-
- self->read_index += func_name_size + 1; /* +1 to skip null-termination character */
- }
- assert(self->read_index == read_end);
- }
-
+ self->read_index += extern_funcs_size;
return AMAL_PROGRAM_OK;
}
static CHECK_RESULT int amal_program_read_exported_functions(amal_program *self) {
u32 export_funcs_size;
- return_if_error(amal_program_advance_section_magic_number(self));
-
if(!amal_program_read_advance(self, &self->num_exported_functions, sizeof(u16)))
return AMAL_PROGRAM_INVALID_EXPORTED_FUNCTIONS;
@@ -399,8 +361,6 @@ static CHECK_RESULT int amal_program_read_exported_functions(amal_program *self)
static CHECK_RESULT int amal_program_read_imports(amal_program *self) {
u32 imports_size;
- return_if_error(amal_program_advance_section_magic_number(self));
-
if(!amal_program_read_advance(self, &self->num_imports, sizeof(u8)))
return AMAL_PROGRAM_INVALID_IMPORTS;
@@ -437,20 +397,26 @@ static CHECK_RESULT int amal_program_get_data_by_index(amal_program *self, u16 i
static u8* amal_program_get_funcs_start_by_import_index(amal_program *self, u8 import_index) {
BytecodeHeaderImport *header_import = (BytecodeHeaderImport*)self->imports_start;
+ u32 function_index = 0;
header_import += import_index;
- return (u8*)self->data.data + header_import->function_index;
+ assert(sizeof(function_index) == sizeof(header_import->function_index));
+ am_memcpy(&function_index, &header_import->function_index, sizeof(function_index));
+ return (u8*)self->data.data + function_index;
}
-static BytecodeHeaderFunction* amal_program_get_header_function_by_index(amal_program *self, u8 import_index, u16 index) {
+static void amal_program_get_header_function_by_index(amal_program *self, u8 import_index, u16 index, BytecodeHeaderFunction *result) {
u8 *funcs_start = amal_program_get_funcs_start_by_import_index(self, import_index);
BytecodeHeaderFunction *header_func = (BytecodeHeaderFunction*)(funcs_start + sizeof(u16) + sizeof(u32));
- return header_func + index;
+ am_memcpy(result, header_func + index, sizeof(BytecodeHeaderFunction));
}
static u64 deferred_func_call_get_key(amal_program *self, u8 import_index, u16 func_index) {
BytecodeHeaderImport *header_import = (BytecodeHeaderImport*)self->imports_start;
+ u32 function_index = 0;
header_import += import_index;
- return ((u64)func_index << 32) | (u64)header_import->function_index;
+ assert(sizeof(function_index) == sizeof(header_import->function_index));
+ am_memcpy(&function_index, &header_import->function_index, sizeof(function_index));
+ return ((u64)func_index << 32) | (u64)function_index;
}
static CHECK_RESULT int resolve_deferred_func_calls(amal_program *self, amal_executor *executor, u16 func_index) {
@@ -474,7 +440,8 @@ static CHECK_RESULT int resolve_deferred_func_calls(amal_program *self, amal_exe
static void header_func_set_offset(amal_program *self, u16 func_index, u32 code_offset) {
BytecodeHeaderFunction *header_func = ((BytecodeHeaderFunction*)self->funcs_start) + func_index;
- header_func->func_offset = code_offset;
+ assert(sizeof(header_func->func_offset) == sizeof(code_offset));
+ am_memcpy(&header_func->func_offset, &code_offset, sizeof(code_offset));
}
static CHECK_RESULT int amal_program_read_instructions(amal_program *self, amal_executor *executor) {
@@ -489,8 +456,6 @@ static CHECK_RESULT int amal_program_read_instructions(amal_program *self, amal_
func_counter = 0;
self->return_value_index = 0;
- return_if_error(amal_program_advance_section_magic_number(self));
-
if(!amal_program_read_advance(self, &instructions_size, sizeof(instructions_size)))
return AMAL_PROGRAM_INVALID_INSTRUCTIONS_SIZE;
@@ -614,20 +579,22 @@ static CHECK_RESULT int amal_program_read_instructions(amal_program *self, amal_
u8 import_index;
u16 func_index;
u8 num_args;
- BytecodeHeaderFunction *func_def;
+ BytecodeHeaderFunction func_def;
i8 dst_reg;
am_memcpy(&import_index, self->data.data + self->read_index, sizeof(import_index));
am_memcpy(&func_index, self->data.data + self->read_index + sizeof(import_index), sizeof(func_index));
am_memcpy(&num_args, self->data.data + self->read_index + sizeof(import_index) + sizeof(func_index), sizeof(num_args));
- func_def = amal_program_get_header_function_by_index(self, import_index, func_index);
- assert(func_def->num_return_types == 1 && "TODO: Support 0 and more than 1 return values");
+ amal_program_get_header_function_by_index(self, import_index, func_index, &func_def);
+ assert(func_def.num_return_types == 1 && "TODO: Support 0 and more than 1 return values");
assert(self->return_value_index == 1);
dst_reg = self->return_values_stack[0];
- self->return_value_index -= func_def->num_return_types;
+ self->return_value_index -= func_def.num_return_types;
- if((char*)func_def < self->data.data + self->read_index) {
- return_if_error(amal_exec_call(executor, func_def->func_offset, num_args, dst_reg));
+ /* func_offset will only be non-zero when the function has been decoded (FUNC_START) */
+ if(func_def.func_offset != 0) {
+ /* TODO: Instead of pushing num args, push the sum of sizeof the last num_args */
+ return_if_error(amal_exec_call(executor, func_def.func_offset, num_args, dst_reg));
} else {
/*
The code for the function has not been generated yet (the function is defined after the current location).
@@ -746,6 +713,7 @@ static CHECK_RESULT int amal_program_read_instructions(amal_program *self, amal_
int amal_program_run(amal_program *self) {
int result;
amal_executor *executor;
+
if(self->data.size > PROGRAM_MAX_SIZE) {
amal_log_error("Program is too large. Max size is 1GB");
return AMAL_PROGRAM_ERR;
@@ -755,12 +723,25 @@ int amal_program_run(amal_program *self) {
cleanup_if_error(amal_program_read_header(self));
while(bytes_left_to_read(self) > 0) {
+ cleanup_if_error(amal_program_advance_section_magic_number(self));
cleanup_if_error(amal_program_read_intermediates(self));
+
+ cleanup_if_error(amal_program_advance_section_magic_number(self));
cleanup_if_error(amal_program_read_strings(self));
+
+ cleanup_if_error(amal_program_advance_section_magic_number(self));
cleanup_if_error(amal_program_read_functions(self));
+
+ cleanup_if_error(amal_program_advance_section_magic_number(self));
cleanup_if_error(amal_program_read_external_functions(self));
+
+ cleanup_if_error(amal_program_advance_section_magic_number(self));
cleanup_if_error(amal_program_read_exported_functions(self));
+
+ cleanup_if_error(amal_program_advance_section_magic_number(self));
cleanup_if_error(amal_program_read_imports(self));
+
+ cleanup_if_error(amal_program_advance_section_magic_number(self));
cleanup_if_error(amal_program_read_instructions(self, executor));
}
if(self->main_func_instruction_offset == ~(u32)0U) {
@@ -776,21 +757,22 @@ int amal_program_run(amal_program *self) {
}
int amal_program_save(amal_program *self, const char *filepath) {
- FILE *file;
- file = fopen(filepath, "wb");
+ int err = 0;
+ FILE *file = fopen(filepath, "wb");
if(!file) {
- int err;
- err = errno;
- perror(filepath);
- return -err;
+ err = -errno;
+ goto cleanup;
}
+
if(fwrite(self->data.data, 1, self->data.size, file) != self->data.size) {
- int err;
- err = errno;
- perror(filepath);
- return -err;
+ err = -errno;
+ goto cleanup;
}
- fclose(file);
- return 0;
-}
+ cleanup:
+ if(err != 0)
+ perror(filepath);
+ if(file)
+ fclose(file);
+ return err;
+}
diff --git a/src/std/arena_allocator.c b/src/std/arena_allocator.c
index 11fb40d..8b0083d 100644
--- a/src/std/arena_allocator.c
+++ b/src/std/arena_allocator.c
@@ -74,7 +74,8 @@ static usize align_ptr_ceil_offset(void *ptr, uintptr_t alignment) {
return (uintptr_t)align_ptr_ceil(ptr, alignment) - (uintptr_t)ptr;
}
-#define ALLOC_ALIGNMENT 8
+/* 16-byte alignment allows SIMD instructions to be operated on the data */
+#define ALLOC_ALIGNMENT 16
int arena_allocator_alloc(ArenaAllocator *self, usize size, void **mem) {
ArenaAllocatorNode *current;
@@ -103,6 +104,6 @@ int arena_allocator_alloc(ArenaAllocator *self, usize size, void **mem) {
int arena_allocator_add_mem(ArenaAllocator *self, usize *result_index) {
void *null_data;
null_data = NULL;
- *result_index = buffer_get_size(&self->mems, void*);
+ *result_index = self->mems.size;
return buffer_append(&self->mems, &null_data, sizeof(void*));
}
diff --git a/src/std/buffer.c b/src/std/buffer.c
index 021fce8..dca3b26 100644
--- a/src/std/buffer.c
+++ b/src/std/buffer.c
@@ -28,7 +28,6 @@ void buffer_deinit(Buffer *self) {
static CHECK_RESULT int buffer_ensure_capacity(Buffer *self, usize new_capacity) {
usize capacity;
void *new_mem;
- int alloc_result;
if(self->capacity >= new_capacity)
return BUFFER_OK;
@@ -45,15 +44,14 @@ static CHECK_RESULT int buffer_ensure_capacity(Buffer *self, usize new_capacity)
capacity = cap;
}
- alloc_result = am_realloc(self->data, capacity, &new_mem);
- if(alloc_result != ALLOC_OK)
+ if(am_realloc(self->data, capacity, &new_mem) != ALLOC_OK)
return BUFFER_ALLOC_FAIL;
self->data = new_mem;
self->capacity = capacity;
/* Update list of buffers in the allocator with the new address of the buffer data */
if(self->allocator)
- am_memcpy(self->allocator->mems.data + sizeof(void*) * self->allocator_index, &self->data, sizeof(void*));
+ am_memcpy(self->allocator->mems.data + self->allocator_index, &self->data, sizeof(void*));
return BUFFER_OK;
}
@@ -103,7 +101,7 @@ int buffer_set_capacity(Buffer *self, usize new_capacity) {
self->size = self->capacity;
/* Update list of buffers in the allocator with the new address of the buffer data */
if(self->allocator)
- am_memcpy(self->allocator->mems.data + sizeof(void*) * self->allocator_index, &self->data, sizeof(void*));
+ am_memcpy(self->allocator->mems.data + self->allocator_index, &self->data, sizeof(void*));
return BUFFER_OK;
}
diff --git a/src/std/buffer_view.c b/src/std/buffer_view.c
index 249928b..8763be2 100644
--- a/src/std/buffer_view.c
+++ b/src/std/buffer_view.c
@@ -1,5 +1,6 @@
#include "../../include/std/buffer_view.h"
#include "../../include/std/mem.h"
+#include <string.h>
BufferView create_buffer_view_null(void) {
BufferView buffer_view;
@@ -15,6 +16,13 @@ BufferView create_buffer_view(const char *data, usize size) {
return buffer_view;
}
+BufferView create_buffer_view_auto(const char *data) {
+ BufferView buffer_view;
+ buffer_view.data = data;
+ buffer_view.size = strlen(data);
+ return buffer_view;
+}
+
bool buffer_view_equals(const BufferView *self, const BufferView *other) {
return self->size == other->size && am_memeql(self->data, other->data, self->size);
}
diff --git a/src/tokenizer.c b/src/tokenizer.c
index fd516f6..de467c3 100644
--- a/src/tokenizer.c
+++ b/src/tokenizer.c
@@ -7,6 +7,7 @@
#include <limits.h>
#include <stdio.h>
#include <stdarg.h>
+#include <string.h>
static int isAlpha(int c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
@@ -399,16 +400,6 @@ int tokenizer_next(Tokenizer *self, Token *token) {
return result;
}
-static usize strlen(const char *str) {
- usize len;
- len = 0;
- while(*str != '\0') {
- ++len;
- ++str;
- }
- return len;
-}
-
/*
static const char* binop_to_string(BinopType binop_type) {
switch(binop_type) {