instructions rename and uninitialized memory error fix
This commit is contained in:
parent
715a2cd82e
commit
a69d68f69c
@ -12,8 +12,8 @@ Machine code interpreter written in pure C. Can execute programs up to 1 MEGABYT
|
|||||||
| code | name | arguments | details |
|
| code | name | arguments | details |
|
||||||
|------|------|-----------|---------|
|
|------|------|-----------|---------|
|
||||||
| 00 | NOP | | ignored instruction |
|
| 00 | NOP | | ignored instruction |
|
||||||
| 01 | PUSH | `dst_register`, `value_size(bytes)`, `value` | push constant value into `dst_register` |
|
| 01 | MOVC | `dst_register`, `value` | push constant value into `dst_register` |
|
||||||
| 02 | MOV | `dst_register`, `src_register` | copy value from `src_register` to `dst_register`
|
| 02 | MOVR | `dst_register`, `src_register` | copy value from `src_register` to `dst_register`
|
||||||
| 03 | ADD | `dst_register`, `src_register` | `dst` += `src` |
|
| 03 | ADD | `dst_register`, `src_register` | `dst` += `src` |
|
||||||
| 04 | SUB | `dst_register`, `src_register` | `dst` -= `src` |
|
| 04 | SUB | `dst_register`, `src_register` | `dst` -= `src` |
|
||||||
| 05 | MUL | `dst_register`, `src_register` | `dst` *= `src` |
|
| 05 | MUL | `dst_register`, `src_register` | `dst` *= `src` |
|
||||||
|
|||||||
@ -4,13 +4,13 @@
|
|||||||
|
|
||||||
.data:
|
.data:
|
||||||
// named array of 8-bit values
|
// named array of 8-bit values
|
||||||
const8 msg "Hello, World :3\0"
|
const8 msg "Hello, World :3\n\0"
|
||||||
|
|
||||||
.main:
|
.main:
|
||||||
push ax 1; // sys_write
|
movc ax 1; // sys_write
|
||||||
push bx 1; // stdout
|
movc bx 1; // stdout
|
||||||
push cx @msg; // address of msg data
|
movc cx @msg; // address of msg data
|
||||||
push dx #msg; // size of msg data
|
movc dx #msg; // size of msg data
|
||||||
sys
|
sys
|
||||||
push ax 0
|
movc ax 0
|
||||||
exit
|
exit
|
||||||
|
|||||||
@ -68,7 +68,7 @@ static bool compileSection(Compiler* cmp, Section* sec){
|
|||||||
CompiledSection* cs = List_CompiledSection_expand(&cmp->binary.section_list, 1);
|
CompiledSection* cs = List_CompiledSection_expand(&cmp->binary.section_list, 1);
|
||||||
CompiledSection_construct(cs, sec->name);
|
CompiledSection_construct(cs, sec->name);
|
||||||
if(!HashMap_CompiledSectionPtr_tryPush(&cmp->binary.section_map, cs->name, cs)){
|
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
|
// 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++){
|
for(u32 j = 0; j < sec->code.len; j++){
|
||||||
Operation* op = &sec->code.data[j];
|
Operation* op = &sec->code.data[j];
|
||||||
const Instruction* instr = Instruction_getByOpcode(op->opcode);
|
const Instruction* instr = Instruction_getByOpcode(op->opcode);
|
||||||
|
if(instr == NULL){
|
||||||
|
returnError("unknown opcode: %i", op->opcode)
|
||||||
|
}
|
||||||
|
|
||||||
printf(" %s", instr->name.data);
|
printf(" %s", instr->name.data);
|
||||||
for(u32 k = 0; k < op->args.len; k++){
|
for(u32 k = 0; k < op->args.len; k++){
|
||||||
Argument* arg = &op->args.data[k];
|
Argument* arg = &op->args.data[k];
|
||||||
|
|||||||
@ -259,10 +259,12 @@ bool Compiler_parse(Compiler* cmp){
|
|||||||
// data definition starts with const
|
// data definition starts with const
|
||||||
if(str_startsWith(instr_name, STR("const"))){
|
if(str_startsWith(instr_name, STR("const"))){
|
||||||
DataDefinition* dataDefPtr = List_DataDefinition_expand(&sec->data, 1);
|
DataDefinition* dataDefPtr = List_DataDefinition_expand(&sec->data, 1);
|
||||||
|
memset(dataDefPtr, 0, sizeof(DataDefinition));
|
||||||
parseDataDefinition(cmp, instr_name, dataDefPtr);
|
parseDataDefinition(cmp, instr_name, dataDefPtr);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Operation* operPtr = List_Operation_expand(&sec->code, 1);
|
Operation* operPtr = List_Operation_expand(&sec->code, 1);
|
||||||
|
memset(operPtr, 0, sizeof(Operation));
|
||||||
parseOperation(cmp, instr_name, operPtr);
|
parseOperation(cmp, instr_name, operPtr);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
#include "impl_macros.h"
|
#include "impl_macros.h"
|
||||||
|
|
||||||
/// MOV [dst_register] [src_register]
|
/// MOVR [dst_register] [src_register]
|
||||||
i32 MOV_impl(VM* vm){
|
i32 MOVR_impl(VM* vm){
|
||||||
u8 dst_register_i = 0;
|
u8 dst_register_i = 0;
|
||||||
readRegisterVar(dst_register_i);
|
readRegisterVar(dst_register_i);
|
||||||
u8 src_register_i = 0;
|
u8 src_register_i = 0;
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
#include "impl_macros.h"
|
#include "impl_macros.h"
|
||||||
|
|
||||||
/// PUSH [dst_register] [value_size] [value]
|
/// MOVC [dst_register] [value_size] [value]
|
||||||
i32 PUSH_impl(VM* vm){
|
i32 MOVC_impl(VM* vm){
|
||||||
u8 dst_register_i = 0;
|
u8 dst_register_i = 0;
|
||||||
readRegisterVar(dst_register_i);
|
readRegisterVar(dst_register_i);
|
||||||
/*u8 value_size = 0;
|
/*u8 value_size = 0;
|
||||||
|
|||||||
@ -2,8 +2,8 @@
|
|||||||
#include "../collections/HashMap.h"
|
#include "../collections/HashMap.h"
|
||||||
|
|
||||||
i32 NOP_impl(VM* vm);
|
i32 NOP_impl(VM* vm);
|
||||||
i32 PUSH_impl(VM* vm);
|
i32 MOVC_impl(VM* vm);
|
||||||
i32 MOV_impl(VM* vm);
|
i32 MOVR_impl(VM* vm);
|
||||||
i32 ADD_impl(VM* vm);
|
i32 ADD_impl(VM* vm);
|
||||||
i32 SUB_impl(VM* vm);
|
i32 SUB_impl(VM* vm);
|
||||||
i32 MUL_impl(VM* vm);
|
i32 MUL_impl(VM* vm);
|
||||||
@ -17,8 +17,8 @@ i32 CALL_impl(VM* vm);
|
|||||||
Array_declare(Instruction);
|
Array_declare(Instruction);
|
||||||
static const Array_Instruction instructions_array = ARRAY(Instruction, {
|
static const Array_Instruction instructions_array = ARRAY(Instruction, {
|
||||||
Instruction_construct(NOP),
|
Instruction_construct(NOP),
|
||||||
Instruction_construct(PUSH),
|
Instruction_construct(MOVC),
|
||||||
Instruction_construct(MOV),
|
Instruction_construct(MOVR),
|
||||||
Instruction_construct(ADD),
|
Instruction_construct(ADD),
|
||||||
Instruction_construct(SUB),
|
Instruction_construct(SUB),
|
||||||
Instruction_construct(MUL),
|
Instruction_construct(MUL),
|
||||||
|
|||||||
@ -7,8 +7,8 @@ typedef i32 (*InstructionImplFunc_t)(VM* vm);
|
|||||||
|
|
||||||
typedef enum __attribute__((__packed__)) Opcode {
|
typedef enum __attribute__((__packed__)) Opcode {
|
||||||
Opcode_NOP,
|
Opcode_NOP,
|
||||||
Opcode_PUSH,
|
Opcode_MOVC,
|
||||||
Opcode_MOV,
|
Opcode_MOVR,
|
||||||
Opcode_ADD,
|
Opcode_ADD,
|
||||||
Opcode_SUB,
|
Opcode_SUB,
|
||||||
Opcode_MUL,
|
Opcode_MUL,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user