aboutsummaryrefslogtreecommitdiff
path: root/src/bytecode
diff options
context:
space:
mode:
Diffstat (limited to 'src/bytecode')
-rw-r--r--src/bytecode/bytecode.c48
1 files changed, 32 insertions, 16 deletions
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) {