From a69d68f69c57204719690030de8bddeea0ed62c3 Mon Sep 17 00:00:00 2001 From: Timerix Date: Wed, 12 Feb 2025 14:27:50 +0500 Subject: [PATCH] instructions rename and uninitialized memory error fix --- README.md | 4 ++-- examples/s.tasm | 12 ++++++------ src/compiler/Compiler.c | 6 +++++- src/compiler/Parser.c | 2 ++ src/instructions/impl/MOV.c | 4 ++-- src/instructions/impl/PUSH.c | 4 ++-- src/instructions/instructions.c | 8 ++++---- src/instructions/instructions.h | 4 ++-- 8 files changed, 25 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index fffe84f..93f6baa 100644 --- a/README.md +++ b/README.md @@ -12,8 +12,8 @@ Machine code interpreter written in pure C. Can execute programs up to 1 MEGABYT | code | name | arguments | details | |------|------|-----------|---------| | 00 | NOP | | ignored instruction | -| 01 | PUSH | `dst_register`, `value_size(bytes)`, `value` | push constant value into `dst_register` | -| 02 | MOV | `dst_register`, `src_register` | copy value from `src_register` to `dst_register` +| 01 | MOVC | `dst_register`, `value` | push constant value into `dst_register` | +| 02 | MOVR | `dst_register`, `src_register` | copy value from `src_register` to `dst_register` | 03 | ADD | `dst_register`, `src_register` | `dst` += `src` | | 04 | SUB | `dst_register`, `src_register` | `dst` -= `src` | | 05 | MUL | `dst_register`, `src_register` | `dst` *= `src` | diff --git a/examples/s.tasm b/examples/s.tasm index 39348a5..03d5ce0 100644 --- a/examples/s.tasm +++ b/examples/s.tasm @@ -4,13 +4,13 @@ .data: // named array of 8-bit values -const8 msg "Hello, World :3\0" +const8 msg "Hello, World :3\n\0" .main: -push ax 1; // sys_write -push bx 1; // stdout -push cx @msg; // address of msg data -push dx #msg; // size of msg data +movc ax 1; // sys_write +movc bx 1; // stdout +movc cx @msg; // address of msg data +movc dx #msg; // size of msg data sys -push ax 0 +movc ax 0 exit diff --git a/src/compiler/Compiler.c b/src/compiler/Compiler.c index f0e2701..a61742e 100644 --- a/src/compiler/Compiler.c +++ b/src/compiler/Compiler.c @@ -68,7 +68,7 @@ static bool compileSection(Compiler* cmp, Section* sec){ CompiledSection* cs = List_CompiledSection_expand(&cmp->binary.section_list, 1); CompiledSection_construct(cs, sec->name); if(!HashMap_CompiledSectionPtr_tryPush(&cmp->binary.section_map, cs->name, cs)){ - returnError("duplicate section '%s'", str_copy(sec->name)); + returnError("duplicate section '%s'", str_copy(sec->name).data); } // compile code @@ -308,6 +308,10 @@ bool Compiler_compile(Compiler* cmp, cstr source_file_name, cstr out_file_name, for(u32 j = 0; j < sec->code.len; j++){ Operation* op = &sec->code.data[j]; const Instruction* instr = Instruction_getByOpcode(op->opcode); + if(instr == NULL){ + returnError("unknown opcode: %i", op->opcode) + } + printf(" %s", instr->name.data); for(u32 k = 0; k < op->args.len; k++){ Argument* arg = &op->args.data[k]; diff --git a/src/compiler/Parser.c b/src/compiler/Parser.c index e343484..d0c1573 100644 --- a/src/compiler/Parser.c +++ b/src/compiler/Parser.c @@ -259,10 +259,12 @@ bool Compiler_parse(Compiler* cmp){ // data definition starts with const if(str_startsWith(instr_name, STR("const"))){ DataDefinition* dataDefPtr = List_DataDefinition_expand(&sec->data, 1); + memset(dataDefPtr, 0, sizeof(DataDefinition)); parseDataDefinition(cmp, instr_name, dataDefPtr); } else { Operation* operPtr = List_Operation_expand(&sec->code, 1); + memset(operPtr, 0, sizeof(Operation)); parseOperation(cmp, instr_name, operPtr); } break; diff --git a/src/instructions/impl/MOV.c b/src/instructions/impl/MOV.c index fd20e1d..2913dce 100644 --- a/src/instructions/impl/MOV.c +++ b/src/instructions/impl/MOV.c @@ -1,7 +1,7 @@ #include "impl_macros.h" -/// MOV [dst_register] [src_register] -i32 MOV_impl(VM* vm){ +/// MOVR [dst_register] [src_register] +i32 MOVR_impl(VM* vm){ u8 dst_register_i = 0; readRegisterVar(dst_register_i); u8 src_register_i = 0; diff --git a/src/instructions/impl/PUSH.c b/src/instructions/impl/PUSH.c index c5784ba..61a528b 100644 --- a/src/instructions/impl/PUSH.c +++ b/src/instructions/impl/PUSH.c @@ -1,7 +1,7 @@ #include "impl_macros.h" -/// PUSH [dst_register] [value_size] [value] -i32 PUSH_impl(VM* vm){ +/// MOVC [dst_register] [value_size] [value] +i32 MOVC_impl(VM* vm){ u8 dst_register_i = 0; readRegisterVar(dst_register_i); /*u8 value_size = 0; diff --git a/src/instructions/instructions.c b/src/instructions/instructions.c index fde7f5c..52b1ede 100644 --- a/src/instructions/instructions.c +++ b/src/instructions/instructions.c @@ -2,8 +2,8 @@ #include "../collections/HashMap.h" i32 NOP_impl(VM* vm); -i32 PUSH_impl(VM* vm); -i32 MOV_impl(VM* vm); +i32 MOVC_impl(VM* vm); +i32 MOVR_impl(VM* vm); i32 ADD_impl(VM* vm); i32 SUB_impl(VM* vm); i32 MUL_impl(VM* vm); @@ -17,8 +17,8 @@ i32 CALL_impl(VM* vm); Array_declare(Instruction); static const Array_Instruction instructions_array = ARRAY(Instruction, { Instruction_construct(NOP), - Instruction_construct(PUSH), - Instruction_construct(MOV), + Instruction_construct(MOVC), + Instruction_construct(MOVR), Instruction_construct(ADD), Instruction_construct(SUB), Instruction_construct(MUL), diff --git a/src/instructions/instructions.h b/src/instructions/instructions.h index 82bb2ab..b142175 100644 --- a/src/instructions/instructions.h +++ b/src/instructions/instructions.h @@ -7,8 +7,8 @@ typedef i32 (*InstructionImplFunc_t)(VM* vm); typedef enum __attribute__((__packed__)) Opcode { Opcode_NOP, - Opcode_PUSH, - Opcode_MOV, + Opcode_MOVC, + Opcode_MOVR, Opcode_ADD, Opcode_SUB, Opcode_MUL,