From cc30a97ab90a8fa40707936d3d089d81c49559b6 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 23 Jan 2020 03:09:54 +0100 Subject: Add bytecode decoding.. starting on program execution now --- src/program.c | 122 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 src/program.c (limited to 'src/program.c') diff --git a/src/program.c b/src/program.c new file mode 100644 index 0000000..862931a --- /dev/null +++ b/src/program.c @@ -0,0 +1,122 @@ +#include "../include/program.h" +#include "../include/bytecode.h" +#include + +void tsl_program_init(TslProgram *self) { + tsl_buffer_init(&self->function_bytecode_list); + tsl_hash_map_init(&self->variables); +} + +void tsl_program_deinit(TslProgram *self) { + TslBytecode *bytecode_writer = tsl_buffer_begin(&self->function_bytecode_list); + TslBytecode *bytecode_writer_end = tsl_buffer_end(&self->function_bytecode_list); + while(bytecode_writer != bytecode_writer_end) { + tsl_bytecode_deinit(bytecode_writer); + ++bytecode_writer; + } + tsl_buffer_deinit(&self->function_bytecode_list); + tsl_hash_map_deinit(&self->variables); +} + +TslProgramResult tsl_program_run(TslProgram *self) { + TslBytecode *file_scope_bytecode = tsl_buffer_begin(&self->function_bytecode_list); + char *instruction = tsl_buffer_begin(&file_scope_bytecode->buffer); + char *instruction_end = tsl_buffer_end(&file_scope_bytecode->buffer); + printf("#############################\n"); + /* TODO: Verify if these don't cause unaligned memory access on non-x86 platforms */ + while(instruction != instruction_end) { + TslOpcode opcode = *(TslOpcode*)instruction; + TslInstructionType1 *instruction_type1 = (TslInstructionType1*)instruction; + TslInstructionType2 *instruction_type2 = (TslInstructionType2*)instruction; + TslInstructionType3 *instruction_type3 = (TslInstructionType3*)instruction; + TslInstructionType4 *instruction_type4 = (TslInstructionType4*)instruction; + switch(opcode) { + case TSL_OPCODE_LOADN: { + printf("loadn %f\n", instruction_type2->value); + instruction += sizeof(TslInstructionType2); + break; + } + case TSL_OPCODE_LOADB: { + printf("loadb %s\n", instruction_type3->value ? "true" : "false"); + instruction += sizeof(TslInstructionType3); + break; + } + case TSL_OPCODE_LOADS: { + printf("loads \"%.*s\"\n", (int)instruction_type4->value.size, instruction_type4->value.data); + instruction += sizeof(TslInstructionType4); + break; + } + case TSL_OPCODE_LOADF: { + printf("loadf %d\n", instruction_type1->value); + instruction += sizeof(TslInstructionType1); + break; + } + case TSL_OPCODE_LOADV: { + printf("loadv \"%.*s\"\n", (int)instruction_type4->value.size, instruction_type4->value.data); + instruction += sizeof(TslInstructionType4); + break; + } + case TSL_OPCODE_LOADNULL: { + printf("loadnull\n"); + instruction += sizeof(TslInstructionType5); + break; + } + case TSL_OPCODE_SETV: { + printf("setv \"%.*s\"\n", (int)instruction_type4->value.size, instruction_type4->value.data); + instruction += sizeof(TslInstructionType4); + break; + } + case TSL_OPCODE_LIST: { + printf("list %d\n", instruction_type1->value); + instruction += sizeof(TslInstructionType1); + break; + } + case TSL_OPCODE_MAP: { + printf("map %d\n", instruction_type1->value); + instruction += sizeof(TslInstructionType1); + break; + } + case TSL_OPCODE_MINDEX: { + printf("mindex\n"); + instruction += sizeof(TslInstructionType5); + break; + } + case TSL_OPCODE_CALLF: { + printf("callf %d\n", instruction_type1->value); + instruction += sizeof(TslInstructionType1); + break; + } + case TSL_OPCODE_ADD: { + printf("add\n"); + instruction += sizeof(TslInstructionType5); + break; + } + case TSL_OPCODE_SUB: { + printf("sub\n"); + instruction += sizeof(TslInstructionType5); + break; + } + case TSL_OPCODE_MUL: { + printf("mul\n"); + instruction += sizeof(TslInstructionType5); + break; + } + case TSL_OPCODE_DIV: { + printf("div\n"); + instruction += sizeof(TslInstructionType5); + break; + } + case TSL_OPCODE_LOADCA: { + printf("loadca \"%.*s\"\n", (int)instruction_type4->value.size, instruction_type4->value.data); + instruction += sizeof(TslInstructionType4); + break; + } + case TSL_OPCODE_CALLC: { + printf("callc %d\n", instruction_type1->value); + instruction += sizeof(TslInstructionType1); + break; + } + } + } + return TSL_PROGRAM_RESULT_OK; +} -- cgit v1.2.3