#include "../../include/bytecode/bytecode.h" #include "../../include/std/mem.h" #include "../../include/std/log.h" #include "../../include/ir/ir.h" #include "../../include/parser.h" #include "../../include/ast.h" #include "../../include/compiler.h" #include #include /* TODO: Remove this */ #include #include #define throw(result) do { throw_debug_msg; longjmp(self->env, (result)); } while(0) #define throw_if_error(result) \ do { \ int return_if_result; \ return_if_result = (result); \ if((return_if_result) != 0) \ throw(return_if_result); \ } while(0) int bytecode_init(Bytecode *self, ArenaAllocator *allocator) { self->import_index = 0; self->funcs_index = 0; self->extern_funcs_index = 0; self->offset = 0; return buffer_init(&self->data, allocator); } /*doc(Bytecode) The layout of the full bytecode is: Header (X Intermediates X Strings X Functions X External_Functions X Exported_Functions X Imports X Instructions)*\ Where the X is a magic number to make it easier to find errors while decoding the bytecode.\ The value of the magic number is @AMAL_BYTECODE_SECTION_MAGIC_NUMBER */ CHECK_RESULT int buffer_append_header(Buffer *program_data) { /*doc(Bytecode header) # Header layout |Type|Field |Description | |----|-------------|----------------------------------------------------------------------------| |u32 |Magic number |The magic number used to identify an amalgam bytecode file. | |u8 |Major version|The major version of the bytecode. Updates in this is a breaking change. | |u8 |Minor version|The minor version of the bytecode. Updates in this are backwards compatible.| |u8 |Patch version|The patch version of the bytecode. Updates in this are only minor bug fixes.| |u8 |Endian |Endian of the program. 0 = little endian, 1 = big endian. | The versions in the header only changes for every release, not every change. */ BytecodeHeader header; am_memcpy(header.magic_number, AMAL_BYTECODE_MAGIC_NUMBER, AMAL_BYTECODE_MAGIC_NUMBER_SIZE); header.major_version = AMAL_BYTECODE_MAJOR_VERSION; header.minor_version = AMAL_BYTECODE_MINOR_VERSION; header.patch_version = AMAL_BYTECODE_PATCH_VERSION; #if defined(AMAL_BIG_ENDIAN) #error TODO: convert bytecode to little endian #endif return_if_error(buffer_append(program_data, &header, sizeof(header))); return 0; } static CHECK_RESULT usize ir_extract_data(u8 *instruction_data, void *result, usize size) { am_memcpy(result, instruction_data, size); return size; } static void add_intermediates(BytecodeCompilerContext *self) { /*doc(Bytecode intermediates) # Intermediates layout |Type |Field |Description | |--------------|------------------|-------------------------------------------------------------------------------| |u32 |Intermediates size|The size of all intermediates, in bytes. | |Intermediate[]|Intermediate data |Multiple intermediates, where the total size is defined by @Intermediates size.| # Intermediate |Type|Field|Description | |----|-----|----------------------------------------------------| |u8 |Type |The type of the number. 0=integer, 1=float. | |u64 |Value|The type of the value depends on the value of @Type.| */ Ir *ir = self->parser->ir; Buffer *instructions = &self->bytecode->data; IrNumber *intermediate = buffer_begin(&ir->intermediates); IrNumber *intermediates_end = buffer_end(&ir->intermediates); int i = 0; u32 intemediates_size = (sizeof(u8) + sizeof(u64)) * buffer_get_size(&ir->intermediates, IrNumber); throw_if_error(buffer_expand(instructions, sizeof(u32) + intemediates_size)); throw_if_error(buffer_append(instructions, &intemediates_size, sizeof(u32))); for(; intermediate != intermediates_end; ++intermediate) { throw_if_error(buffer_append(instructions, &intermediate->type, sizeof(u8))); /* TODO: Store value using an encoding that will save space when using low numbers */ throw_if_error(buffer_append(instructions, &intermediate->value.integer, sizeof(u64))); fprintf(stderr, "i%d = %ld\n", i++, intermediate->value.integer); } } static void add_strings(BytecodeCompilerContext *self) { /*doc(Bytecode strings) # Strings layout |Type |Field |Description | |--------|-----------------|------------------------------------------------------------------| |u16 |Number of strings|The number of strings. | |u32 |Strings size |The size of all strings, in bytes. | |String[]|Strings data |Multiple strings, where the total size is defined by @Strings size| # String |Type|Field|Description | |----|----|----------------------------------------------------------------------------------------| |u16 |Size|The size of the string, in bytes. Excluding the null-terminate character. | |u8* |Data|The data of the string, where the size is defined by @Size. Strings are null-terminated.| */ Ir *ir = self->parser->ir; Buffer *instructions = &self->bytecode->data; BufferView *string = buffer_begin(&ir->strings); BufferView *strings_end = buffer_end(&ir->strings); u32 strings_size = 0; for(; string != strings_end; ++string) { strings_size += sizeof(u16) + string->size + 1; /* +1 for null-termination of string */ } string = buffer_begin(&ir->strings); throw_if_error(buffer_expand(instructions, sizeof(u16) + sizeof(u32) + strings_size)); throw_if_error(buffer_append(instructions, &ir->string_counter, sizeof(u16))); throw_if_error(buffer_append(instructions, &strings_size, sizeof(u32))); for(; string != strings_end; ++string) { const char null_s = '\0'; throw_if_error(buffer_append(instructions, &string->size, sizeof(u16))); throw_if_error(buffer_append(instructions, string->data, string->size)); throw_if_error(buffer_append(instructions, &null_s, sizeof(char))); } } static TypeSize function_signature_get_params_size(FunctionSignature *self) { FunctionParameter *param = buffer_begin(&self->parameters); FunctionParameter *param_end = buffer_end(&self->parameters); TypeSize params_total_size; params_total_size.num_pointers = 0; params_total_size.fixed_size = 0; for(; param != param_end; ++param) { if(param->type.variable_type_flags & VARIABLE_TYPE_FLAG_BORROW) params_total_size.num_pointers += 1; else { TypeSize param_size = resolved_type_get_byte_size(¶m->resolve_data.type); params_total_size.num_pointers += param_size.num_pointers; params_total_size.fixed_size += param_size.fixed_size; } } return params_total_size; } static TypeSize function_signature_get_return_types_size(FunctionSignature *self) { FunctionReturnType *return_type = buffer_begin(&self->return_types); FunctionReturnType *return_type_end = buffer_end(&self->return_types); TypeSize return_types_total_size; return_types_total_size.num_pointers = 0; return_types_total_size.fixed_size = 0; for(; return_type != return_type_end; ++return_type) { 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; } static void add_functions(BytecodeCompilerContext *self) { /*doc(Bytecode functions) # Functions layout |Type |Field |Description | |----------|----------|--------------------------------------------------------------------------------------| |u16 |num_funcs |The number of non-extern functions. | |u32 |funcs_size|The size of all functions, in bytes. | |Function[]|Functions |Multiple non-extern functions, where the number of functions is defined by @num_funcs.| # Function |Type|Field |Description | |----|-------------------------|-------------------------------------------------------------------------------------------------------------------------| |u32 |func_offset |The offset in the program code (machine code) where the function starts. Is always ~0 until the program has been started.| |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. | */ Ir *ir = self->parser->ir; Buffer *instructions = &self->bytecode->data; IrFunc *func = buffer_begin(&ir->funcs); IrFunc *func_end = buffer_end(&ir->funcs); u32 funcs_size = (u32)ir->func_counter * sizeof(BytecodeHeaderFunction); assert(sizeof(BytecodeHeaderFunction) == 22); self->bytecode->funcs_index = instructions->size; throw_if_error(buffer_expand(instructions, sizeof(u16) + sizeof(u32) + funcs_size)); throw_if_error(buffer_append(instructions, &ir->func_counter, sizeof(u16))); throw_if_error(buffer_append(instructions, &funcs_size, sizeof(u32))); for(; func != func_end; ++func) { BytecodeHeaderFunction header_func; TypeSize params_total_size = function_signature_get_params_size(func->func_sig); TypeSize return_types_total_size = function_signature_get_return_types_size(func->func_sig); header_func.func_offset = ~(u32)0UL; header_func.num_params = buffer_get_size(&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(&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))); } assert(sizeof(ir->func_counter) == sizeof(u16) && "Program decoder needs to be updated since size of func index has changed"); } static void add_extern_functions(BytecodeCompilerContext *self) { /*doc(Bytecode external functions) # External functions layout |Type |Field |Description | |-------------------|------------------|-----------------------------------------------------------------------------------------| |u16 |num_extern_func |The number of external functions. | |u32 |extern_funcs_size |The size of all external functions, in bytes. | |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. | |u8 |num_return_types |The number of return values. | |u8 |name_len |The length of the external function name, in bytes. Excluding the null-terminate character. | |u8 |flags |The flags for the external function. The values are defined in @amal_func_flag. | |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. | |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 |The name of the external function, where the size is defined by @name_len. Names are null-terminated.| */ Ir *ir = self->parser->ir; Buffer *instructions = &self->bytecode->data; IrExternFunc *extern_func = buffer_begin(&ir->extern_funcs); IrExternFunc *extern_func_end = buffer_end(&ir->extern_funcs); u32 extern_funcs_size = (u32)ir->extern_func_counter * sizeof(BytecodeHeaderExternFunction); assert(sizeof(BytecodeHeaderExternFunction) == 20); for(; extern_func != extern_func_end; ++extern_func) { extern_funcs_size += extern_func->name.size + 1; /* +1 for null-termination of string */ } extern_func = buffer_begin(&ir->extern_funcs); self->bytecode->extern_funcs_index = instructions->size; throw_if_error(buffer_expand(instructions, sizeof(u16) + sizeof(u32) + extern_funcs_size)); throw_if_error(buffer_append(instructions, &ir->extern_func_counter, sizeof(u16))); throw_if_error(buffer_append(instructions, &extern_funcs_size, sizeof(u32))); for(; extern_func != extern_func_end; ++extern_func) { const char null_s = '\0'; 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.num_return_types = buffer_get_size(&extern_func->func_sig->return_types, FunctionReturnType); header_func.name_len = extern_func->name.size; header_func.flags = 0; if(header_func.num_params > 0) { amal_default_type *vararg_type = self->parser->compiler->default_types.c_varargs; FunctionParameter *last_param = buffer_begin(&extern_func->func_sig->parameters); last_param += (header_func.num_params - 1); if(last_param->resolve_data.type.value.data == &vararg_type->lhs_expr) header_func.flags |= FUNC_FLAG_VARARGS; } header_func.params_num_pointers = params_total_size.num_pointers; header_func.params_fixed_size = params_total_size.fixed_size; 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.data, extern_func->name.size)); throw_if_error(buffer_append(instructions, &null_s, sizeof(char))); } assert(sizeof(IrExternFuncIndex) == sizeof(u16) && "Program decoder needs to be updated since size of extern func index has changed"); } static void add_export_functions(BytecodeCompilerContext *self) { /*doc(Bytecode exported functions) # Exported functions layout |Type |Field |Description | |-------------------|------------------|-----------------------------------------------------------------------------------------| |u16 |num_export_func |The number of exported functions. | |u32 |export_funcs_size |The size of all exported functions, in bytes. | |Exported function[]|Exported functions|Multiple exported functions, where the number of functions is defined by @num_export_func| # Exported function |Type|Field |Description | |----|------------------|---------------------------------------------------------------------------------------------------------------------------| |u32 |instruction_offset|The offset in the instruction data where the exported function is defined. Is always ~0 until the program has been started.| |u8 |num_args |The number of arguments the functions has. | |u8 |name_len |The length of the exported function name, in bytes. Excluding the null-terminate character. | |u8[]|name |The name of the exported function, where the size is defined by @name_len. Names are null-terminated. | */ Ir *ir = self->parser->ir; Buffer *instructions = &self->bytecode->data; IrExportFunc *export_func = buffer_begin(&ir->export_funcs); IrExportFunc *export_func_end = buffer_end(&ir->export_funcs); u32 export_funcs_size = (u32)ir->export_func_counter * (sizeof(u32) + sizeof(u8) + sizeof(u8)); for(; export_func != export_func_end; ++export_func) { export_funcs_size += export_func->name.size + 1; /* +1 for null-termination of string */ } export_func = buffer_begin(&ir->export_funcs); throw_if_error(buffer_expand(instructions, sizeof(u16) + sizeof(u32) + export_funcs_size)); throw_if_error(buffer_append(instructions, &ir->export_func_counter, sizeof(u16))); throw_if_error(buffer_append(instructions, &export_funcs_size, sizeof(u32))); for(; export_func != export_func_end; ++export_func) { const char null_s = '\0'; const u32 instruction_offset = ~(u32)0UL; u8 num_args = buffer_get_size(&export_func->func_sig->parameters, FunctionParameter); throw_if_error(buffer_append(instructions, &instruction_offset, sizeof(instruction_offset))); throw_if_error(buffer_append(instructions, &num_args, sizeof(num_args))); throw_if_error(buffer_append(instructions, &export_func->name.size, sizeof(u8))); throw_if_error(buffer_append(instructions, export_func->name.data, export_func->name.size)); throw_if_error(buffer_append(instructions, &null_s, sizeof(char))); } assert(sizeof(IrExportFuncIndex) == sizeof(u16) && "Program decoder needs to be updated since size of export func index has changed"); } static void add_imports(BytecodeCompilerContext *self) { /*doc(Bytecode imports) # Imports layout |Type |Field |Description | |--------|------------|-------------------------------------------------------------------------| |u8 |num_imports |The number of imports. | |u32 |imports_size|The size of all imports, in bytes. | |Import[]|Import |Multiple imports, where the number of imports is defined by @num_imports.| # Import |Type|Field |Description | |----|---------------------|----------------------------------------------------------------------------------------| |u32 |function_index |The index in the bytecode where function header begins for the imported file. | |u32 |extern_function_index|The index in the bytecode where the extern function header begins for the imported file.| */ Parser *parser = self->parser; Buffer *instructions = &self->bytecode->data; ParserFileScopeReference **import = buffer_begin(&parser->imports); ParserFileScopeReference **import_end = buffer_end(&parser->imports); u8 num_imports = 1 + (import_end - import); u32 imports_size = num_imports * sizeof(BytecodeHeaderImport); assert(sizeof(BytecodeHeaderImport) == 8); self->bytecode->import_index = instructions->size; throw_if_error(buffer_expand(instructions, sizeof(u8) + sizeof(u32) + imports_size)); throw_if_error(buffer_append(instructions, &num_imports, sizeof(num_imports))); throw_if_error(buffer_append(instructions, &imports_size, sizeof(imports_size))); /* The first import is always a reference to itself */ throw_if_error(buffer_append(instructions, &self->bytecode->funcs_index, sizeof(self->bytecode->funcs_index))); throw_if_error(buffer_append(instructions, &self->bytecode->extern_funcs_index, sizeof(self->bytecode->extern_funcs_index))); for(; import != import_end; ++import) { /* We don't know the index to the functions yet, so first fill them with the parser index that owns them and after bytecode has been generated for each parser (file), modify these function indices to point to the parsers function index in the bytecode. */ u32 parser_index = (*import)->file_scope_ref->parser->index; throw_if_error(buffer_append(instructions, &parser_index, sizeof(parser_index))); throw_if_error(buffer_append(instructions, &parser_index, sizeof(parser_index))); } } static void add_ins1(BytecodeCompilerContext *self, AmalOpcode opcode, const char *fmt) { throw_if_error(buffer_append(&self->bytecode->data, &opcode, sizeof(AmalOpcodeType))); if(fmt) { fprintf(stderr, fmt); fputc('\n', stderr); } } static void add_ins2(BytecodeCompilerContext *self, AmalOpcode opcode, AmalReg reg, const char *fmt) { Buffer *instructions = &self->bytecode->data; size_t index = instructions->size; throw_if_error(buffer_append_empty(instructions, sizeof(AmalOpcodeType) + sizeof(reg))); instructions->data[index] = opcode; memcpy(instructions->data + index + sizeof(AmalOpcodeType), ®, sizeof(reg)); fprintf(stderr, fmt, reg); fputc('\n', stderr); } static void add_ins3(BytecodeCompilerContext *self, AmalOpcode opcode, AmalReg dst_reg, AmalReg src_reg, const char *fmt) { Buffer *instructions = &self->bytecode->data; size_t index = instructions->size; throw_if_error(buffer_append_empty(instructions, sizeof(AmalOpcodeType) + sizeof(dst_reg) + sizeof(src_reg))); instructions->data[index] = opcode; memcpy(instructions->data + index + sizeof(AmalOpcodeType), &dst_reg, sizeof(dst_reg)); memcpy(instructions->data + index + sizeof(AmalOpcodeType) + sizeof(dst_reg), &src_reg, sizeof(src_reg)); fprintf(stderr, fmt, dst_reg, src_reg); fputc('\n', stderr); } static void add_ins4(BytecodeCompilerContext *self, AmalOpcode opcode, u16 data, const char *fmt) { Buffer *instructions = &self->bytecode->data; size_t index = instructions->size; throw_if_error(buffer_append_empty(instructions, sizeof(AmalOpcodeType) + sizeof(data))); instructions->data[index] = opcode; memcpy(instructions->data + index + sizeof(AmalOpcodeType), &data, sizeof(data)); fprintf(stderr, fmt, data); fputc('\n', stderr); } static void add_ins5(BytecodeCompilerContext *self, AmalOpcode opcode, AmalReg dst_reg, AmalReg reg1, AmalReg reg2, const char *fmt) { Buffer *instructions = &self->bytecode->data; size_t index = instructions->size; throw_if_error(buffer_append_empty(instructions, sizeof(AmalOpcodeType) + sizeof(dst_reg) + sizeof(reg1) + sizeof(reg2))); instructions->data[index] = opcode; memcpy(instructions->data + index + sizeof(AmalOpcodeType), &dst_reg, sizeof(dst_reg)); memcpy(instructions->data + index + sizeof(AmalOpcodeType) + sizeof(dst_reg), ®1, sizeof(reg1)); memcpy(instructions->data + index + sizeof(AmalOpcodeType) + sizeof(dst_reg) + sizeof(reg1), ®2, sizeof(reg2)); fprintf(stderr, fmt, dst_reg, reg1, reg2); fputc('\n', stderr); } static void add_ins6(BytecodeCompilerContext *self, AmalOpcode opcode, AmalReg dst_reg, u16 data, const char *fmt) { Buffer *instructions = &self->bytecode->data; size_t index = instructions->size; throw_if_error(buffer_append_empty(instructions, sizeof(AmalOpcodeType) + sizeof(dst_reg) + sizeof(data))); instructions->data[index] = opcode; memcpy(instructions->data + index + sizeof(AmalOpcodeType), &dst_reg, sizeof(dst_reg)); memcpy(instructions->data + index + sizeof(AmalOpcodeType) + sizeof(dst_reg), &data, sizeof(data)); fprintf(stderr, fmt, dst_reg, data); fputc('\n', stderr); } static void add_instructions(BytecodeCompilerContext *self) { /*doc(Bytecode instructions) # Instructions layout |Type |Field |Description | |-------------|-----------------|---------------------------------------------------------------------------| |u32 |Instructions size|The size of the instructions section, in bytes. | |Instruction[]|Instructions data|The instructions data. Each instructions begins with an opcode, see #Opcode| */ IrInsForm1 ir_ins_form1; IrInsForm2 ir_ins_form2; IrInsFuncStart ir_ins_func_start; IrInsFuncCall ir_ins_func_call; IrInsFuncCallExtern ir_ins_func_call_extern; IrInsCallStart ir_ins_call_start; IrInsJumpZero ir_ins_jump_zero; IrInsJump ir_ins_jump; Ir *ir = self->parser->ir; u8 *instruction = buffer_begin(&ir->instructions); u8 *instructions_end = buffer_end(&ir->instructions); u16 label_counter = 0; u32 num_instructions_index = self->bytecode->data.size; throw_if_error(buffer_append_empty(&self->bytecode->data, sizeof(num_instructions_index))); /* TODO: Limit registers from u16 to u7. Right now they are downcasted and will cause bugs if there are too many registers used in the program. */ while(instruction != instructions_end) { IrInstruction ins = (IrInstruction)*instruction++; switch(ins) { case IR_ASSIGN_INTER: { instruction += ir_extract_data(instruction, &ir_ins_form1, sizeof(ir_ins_form1)); add_ins6(self, AMAL_OP_MOVI, ir_ins_form1.lhs, ir_ins_form1.rhs, "movi r%d, i%d"); break; } case IR_ASSIGN_STRING: { instruction += ir_extract_data(instruction, &ir_ins_form1, sizeof(ir_ins_form1)); add_ins6(self, AMAL_OP_MOVD, ir_ins_form1.lhs, ir_ins_form1.rhs, "movd r%d, s%d"); break; } case IR_ASSIGN_REG: { instruction += ir_extract_data(instruction, &ir_ins_form1, sizeof(ir_ins_form1)); add_ins3(self, AMAL_OP_MOV, ir_ins_form1.lhs, ir_ins_form1.rhs, "mov r%d, r%d"); break; } case IR_ASSIGN_FUNC: { instruction += ir_extract_data(instruction, &ir_ins_form1, sizeof(ir_ins_form1)); add_ins6(self, AMAL_OP_LOADF, ir_ins_form1.lhs, ir_ins_form1.rhs, "loadf r%d, f%d"); break; } case IR_ADD: { instruction += ir_extract_data(instruction, &ir_ins_form2, sizeof(ir_ins_form2)); add_ins5(self, AMAL_OP_ADD, ir_ins_form2.result, ir_ins_form2.lhs, ir_ins_form2.rhs, "add r%d, r%d, r%d"); break; } case IR_SUB: { instruction += ir_extract_data(instruction, &ir_ins_form2, sizeof(ir_ins_form2)); add_ins5(self, AMAL_OP_SUB, ir_ins_form2.result, ir_ins_form2.lhs, ir_ins_form2.rhs, "sub r%d, r%d, r%d"); break; } case IR_IMUL: { instruction += ir_extract_data(instruction, &ir_ins_form2, sizeof(ir_ins_form2)); add_ins5(self, AMAL_OP_IMUL, ir_ins_form2.result, ir_ins_form2.lhs, ir_ins_form2.rhs, "imul r%d, r%d, r%d"); break; } case IR_MUL: { instruction += ir_extract_data(instruction, &ir_ins_form2, sizeof(ir_ins_form2)); add_ins5(self, AMAL_OP_MUL, ir_ins_form2.result, ir_ins_form2.lhs, ir_ins_form2.rhs, "mul r%d, r%d, r%d"); break; } case IR_IDIV: { instruction += ir_extract_data(instruction, &ir_ins_form2, sizeof(ir_ins_form2)); add_ins5(self, AMAL_OP_IDIV, ir_ins_form2.result, ir_ins_form2.lhs, ir_ins_form2.rhs, "idiv r%d, r%d, r%d"); break; } case IR_DIV: { instruction += ir_extract_data(instruction, &ir_ins_form2, sizeof(ir_ins_form2)); add_ins5(self, AMAL_OP_DIV, ir_ins_form2.result, ir_ins_form2.lhs, ir_ins_form2.rhs, "div r%d, r%d, r%d"); break; } case IR_EQUALS: { instruction += ir_extract_data(instruction, &ir_ins_form2, sizeof(ir_ins_form2)); add_ins5(self, AMAL_OP_EQ, ir_ins_form2.result, ir_ins_form2.lhs, ir_ins_form2.rhs, "eq r%d, r%d, r%d"); break; } case IR_NOT_EQUAL: { instruction += ir_extract_data(instruction, &ir_ins_form2, sizeof(ir_ins_form2)); add_ins5(self, AMAL_OP_NEQ, ir_ins_form2.result, ir_ins_form2.lhs, ir_ins_form2.rhs, "neq r%d, r%d, r%d"); break; } case IR_AND: { instruction += ir_extract_data(instruction, &ir_ins_form2, sizeof(ir_ins_form2)); add_ins5(self, AMAL_OP_BIT_AND, ir_ins_form2.result, ir_ins_form2.lhs, ir_ins_form2.rhs, "and r%d, r%d, r%d"); break; } case IR_ILT: { instruction += ir_extract_data(instruction, &ir_ins_form2, sizeof(ir_ins_form2)); add_ins5(self, AMAL_OP_ILT, ir_ins_form2.result, ir_ins_form2.lhs, ir_ins_form2.rhs, "ilt r%d, r%d, r%d"); break; } case IR_ILE: { instruction += ir_extract_data(instruction, &ir_ins_form2, sizeof(ir_ins_form2)); add_ins5(self, AMAL_OP_ILE, ir_ins_form2.result, ir_ins_form2.lhs, ir_ins_form2.rhs, "ile r%d, r%d, r%d"); break; } case IR_IGT: { instruction += ir_extract_data(instruction, &ir_ins_form2, sizeof(ir_ins_form2)); add_ins5(self, AMAL_OP_IGT, ir_ins_form2.result, ir_ins_form2.lhs, ir_ins_form2.rhs, "igt r%d, r%d, r%d"); break; } case IR_IGE: { instruction += ir_extract_data(instruction, &ir_ins_form2, sizeof(ir_ins_form2)); add_ins5(self, AMAL_OP_IGE, ir_ins_form2.result, ir_ins_form2.lhs, ir_ins_form2.rhs, "ige r%d, r%d, r%d"); break; } case IR_LT: { instruction += ir_extract_data(instruction, &ir_ins_form2, sizeof(ir_ins_form2)); add_ins5(self, AMAL_OP_LT, ir_ins_form2.result, ir_ins_form2.lhs, ir_ins_form2.rhs, "lt r%d, r%d, r%d"); break; } case IR_LE: { instruction += ir_extract_data(instruction, &ir_ins_form2, sizeof(ir_ins_form2)); add_ins5(self, AMAL_OP_LE, ir_ins_form2.result, ir_ins_form2.lhs, ir_ins_form2.rhs, "le r%d, r%d, r%d"); break; } case IR_GT: { instruction += ir_extract_data(instruction, &ir_ins_form2, sizeof(ir_ins_form2)); add_ins5(self, AMAL_OP_GT, ir_ins_form2.result, ir_ins_form2.lhs, ir_ins_form2.rhs, "gt r%d, r%d, r%d"); break; } case IR_GE: { instruction += ir_extract_data(instruction, &ir_ins_form2, sizeof(ir_ins_form2)); add_ins5(self, AMAL_OP_GE, ir_ins_form2.result, ir_ins_form2.lhs, ir_ins_form2.rhs, "ge r%d, r%d, r%d"); break; } case IR_FUNC_START: { instruction += ir_extract_data(instruction, &ir_ins_func_start, sizeof(ir_ins_func_start)); add_ins6(self, AMAL_OP_FUNC_START, ir_ins_func_start.flags, ir_ins_func_start.num_local_vars_regs, "func_start 0x%02x, %u"); label_counter = 0; break; } case IR_FUNC_END: { add_ins1(self, AMAL_OP_FUNC_END, "func_end"); break; } case IR_PUSH: { IrRegister reg; am_memcpy(®, instruction, sizeof(IrRegister)); instruction += sizeof(IrRegister); add_ins2(self, AMAL_OP_PUSH, reg, "push r%d"); break; } case IR_PUSH_RET: { IrRegister reg; am_memcpy(®, instruction, sizeof(IrRegister)); instruction += sizeof(IrRegister); add_ins2(self, AMAL_OP_PUSH_RET, reg, "push_ret r%d"); break; } case IR_CALL_START: { instruction += ir_extract_data(instruction, &ir_ins_call_start, sizeof(ir_ins_call_start)); add_ins2(self, AMAL_OP_CALL_START, ir_ins_call_start.num_args, "call_start %d"); break; } case IR_CALL: { instruction += ir_extract_data(instruction, &ir_ins_func_call, sizeof(ir_ins_func_call)); add_ins6(self, AMAL_OP_CALL, ir_ins_func_call.import_index, ir_ins_func_call.func_decl->ir_func_index, "call f(%d,%d)"); break; } case IR_CALL_EXTERN: { instruction += ir_extract_data(instruction, &ir_ins_func_call_extern, sizeof(ir_ins_func_call_extern)); add_ins6(self, AMAL_OP_CALLE, ir_ins_func_call_extern.import_index, ir_ins_func_call_extern.func_decl_lhs->extern_index, "calle ef(%d,%d)"); break; } case IR_CALLR: { IrRegister reg; instruction += ir_extract_data(instruction, ®, sizeof(reg)); add_ins2(self, AMAL_OP_CALLR, reg, "callr r%d"); break; } case IR_JUMP_ZERO: { instruction += ir_extract_data(instruction, &ir_ins_jump_zero, sizeof(ir_ins_jump_zero)); add_ins6(self, AMAL_OP_JZ, ir_ins_jump_zero.condition_reg, ir_ins_jump_zero.target_label, "jz r%d, l%d"); break; } case IR_JUMP: { instruction += ir_extract_data(instruction, &ir_ins_jump, sizeof(ir_ins_jump)); add_ins4(self, AMAL_OP_JMP, ir_ins_jump.target_label, "jmp l%d"); break; } case IR_RET: { IrRegister reg; am_memcpy(®, instruction, sizeof(IrRegister)); instruction += sizeof(IrRegister); add_ins2(self, AMAL_OP_RET, reg, "ret r%d"); break; } case IR_LABEL: { add_ins1(self, AMAL_OP_LABEL, NULL); fprintf(stderr, "label l%d\n", label_counter++); break; } } } /* Prepend instructions with its size */ { /* -sizeof to Remove the count itself from the size of the instructions size */ const u32 instructions_size = self->bytecode->data.size - num_instructions_index - sizeof(instructions_size); am_memcpy(self->bytecode->data.data + num_instructions_index, &instructions_size, sizeof(instructions_size)); } } static void add_section_magic_number(BytecodeCompilerContext *self) { const u32 section_magic_number = AMAL_BYTECODE_SECTION_MAGIC_NUMBER; throw_if_error(buffer_append(&self->bytecode->data, §ion_magic_number, sizeof(section_magic_number))); } void generate_bytecode_from_ir(BytecodeCompilerContext *self) { add_section_magic_number(self); add_intermediates(self); add_section_magic_number(self); add_strings(self); add_section_magic_number(self); add_functions(self); add_section_magic_number(self); add_extern_functions(self); add_section_magic_number(self); add_export_functions(self); add_section_magic_number(self); add_imports(self); add_section_magic_number(self); add_instructions(self); }