instructions rename and uninitialized memory error fix

This commit is contained in:
Timerix 2025-02-12 14:27:50 +05:00
parent 715a2cd82e
commit a69d68f69c
8 changed files with 25 additions and 19 deletions

View File

@ -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` |

View File

@ -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

View File

@ -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];

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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),

View File

@ -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,