first commit
This commit is contained in:
8
src/instructions/impl/EXIT.c
Normal file
8
src/instructions/impl/EXIT.c
Normal file
@@ -0,0 +1,8 @@
|
||||
#include "impl_macros.h"
|
||||
|
||||
/// EXIT
|
||||
/// ax - exit code
|
||||
i32 EXIT_impl(VM* vm, size_t pos){
|
||||
vm->state = VMState_Exited;
|
||||
return 0;
|
||||
}
|
||||
16
src/instructions/impl/MOV.c
Normal file
16
src/instructions/impl/MOV.c
Normal file
@@ -0,0 +1,16 @@
|
||||
#include "impl_macros.h"
|
||||
|
||||
/// MOV [dst_register] [src_register]
|
||||
i32 MOV_impl(VM* vm, size_t pos){
|
||||
u8 dst_register_i = 0;
|
||||
readRegisterVar(dst_register_i);
|
||||
u8 src_register_i = 0;
|
||||
readRegisterVar(src_register_i);
|
||||
if(dst_register_i == src_register_i){
|
||||
VM_setErrorMessage(vm, "[%p] dst_register_i == src_register_i (%x) ", (void*)pos, src_register_i);
|
||||
return -1;
|
||||
}
|
||||
|
||||
vm->registers[dst_register_i].u32v = vm->registers[src_register_i].u32v;
|
||||
return sizeof(dst_register_i) + sizeof(src_register_i);
|
||||
}
|
||||
6
src/instructions/impl/NOP.c
Normal file
6
src/instructions/impl/NOP.c
Normal file
@@ -0,0 +1,6 @@
|
||||
#include "impl_macros.h"
|
||||
|
||||
/// NOP
|
||||
i32 NOP_impl(VM* vm, size_t pos){
|
||||
return 0;
|
||||
}
|
||||
15
src/instructions/impl/PUSH.c
Normal file
15
src/instructions/impl/PUSH.c
Normal file
@@ -0,0 +1,15 @@
|
||||
#include "impl_macros.h"
|
||||
|
||||
/// PUSH [dst_register] [value_size] [value]
|
||||
i32 PUSH_impl(VM* vm, size_t pos){
|
||||
u8 dst_register_i = 0;
|
||||
readRegisterVar(dst_register_i);
|
||||
u8 value_size = 0;
|
||||
readValueSizeVar(value_size);
|
||||
|
||||
vm->registers[dst_register_i].u32v = 0;
|
||||
if(!VM_dataRead(vm, &vm->registers[dst_register_i].u32v, pos, value_size))
|
||||
return -1;
|
||||
|
||||
return sizeof(dst_register_i) + sizeof(value_size) + value_size;
|
||||
}
|
||||
44
src/instructions/impl/SYS.c
Normal file
44
src/instructions/impl/SYS.c
Normal file
@@ -0,0 +1,44 @@
|
||||
#include "impl_macros.h"
|
||||
|
||||
FILE* fileFromN(VM* vm, size_t pos, u32 file_n){
|
||||
FILE* f = NULL;
|
||||
switch(file_n){
|
||||
case 0: f = stdin; break;
|
||||
case 1: f = stdout; break;
|
||||
case 2: f = stderr; break;
|
||||
default:
|
||||
VM_setErrorMessage(vm, "[%p] invalid file_n (%x) ", (void*)pos, file_n);
|
||||
break;
|
||||
}
|
||||
|
||||
return f;
|
||||
}
|
||||
|
||||
/// SYS
|
||||
/// ax - func code
|
||||
i32 SYS_impl(VM* vm, size_t pos){
|
||||
u8 func_code = vm->ax.u8v0;
|
||||
size_t result_code = 0;
|
||||
switch(func_code){
|
||||
// sys_read
|
||||
// bx - file n
|
||||
// cx - buffer ptr
|
||||
// dx - buffer size
|
||||
case 0:;
|
||||
result_code = fread(vm->data + vm->cx.u32v, 1, vm->dx.u32v, fileFromN(vm, pos, vm->bx.u32v));
|
||||
break;
|
||||
// sys_write
|
||||
// bx - file n
|
||||
// cx - buffer ptr
|
||||
// dx - buffer size
|
||||
case 1:;
|
||||
result_code = fwrite(vm->data + vm->cx.u32v, 1, vm->dx.u32v, fileFromN(vm, pos, vm->bx.u32v));
|
||||
break;
|
||||
default:
|
||||
VM_setErrorMessage(vm, "[%p] invalid system call (%x) ", (void*)pos, func_code);
|
||||
return -1;
|
||||
}
|
||||
|
||||
vm->ax.u32v = result_code;
|
||||
return 0;
|
||||
}
|
||||
32
src/instructions/impl/impl_macros.h
Normal file
32
src/instructions/impl/impl_macros.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
#include "../instructions.h"
|
||||
|
||||
#define readVar(VAR) {\
|
||||
if(!VM_dataRead(vm, &VAR, pos, sizeof(VAR))) \
|
||||
return -1;\
|
||||
pos += sizeof(VAR);\
|
||||
}
|
||||
|
||||
#define validateRegisterIndex(VAR) {\
|
||||
if(VAR > sizeof(vm->registers)){\
|
||||
VM_setErrorMessage(vm, "[%p] invalid register index (%x)", (void*)pos, VAR);\
|
||||
return -1;\
|
||||
}\
|
||||
}
|
||||
|
||||
#define readRegisterVar(VAR) {\
|
||||
readVar(VAR);\
|
||||
validateRegisterIndex(VAR);\
|
||||
}
|
||||
|
||||
#define validateValueSize(VAR) {\
|
||||
if(VAR < 1 || VAR > 4){\
|
||||
VM_setErrorMessage(vm, "[%p] invalid value_size (%x)", (void*)pos, VAR);\
|
||||
return -1;\
|
||||
}\
|
||||
}
|
||||
|
||||
#define readValueSizeVar(VAR) {\
|
||||
readVar(VAR);\
|
||||
validateValueSize(VAR);\
|
||||
}
|
||||
47
src/instructions/impl/math_operators.c
Normal file
47
src/instructions/impl/math_operators.c
Normal file
@@ -0,0 +1,47 @@
|
||||
#include "impl_macros.h"
|
||||
|
||||
#define mathOperatorImpl(OPERATOR){\
|
||||
u8 dst_register_i = 0, src_register_i = 0;\
|
||||
readRegisterVar(dst_register_i);\
|
||||
readRegisterVar(src_register_i);\
|
||||
u8 value_size = 0;\
|
||||
readValueSizeVar(value_size);\
|
||||
\
|
||||
switch(value_size){\
|
||||
case 1: \
|
||||
vm->registers[dst_register_i].u8v0 OPERATOR##= vm->registers[src_register_i].u8v0;\
|
||||
break;\
|
||||
case 2: \
|
||||
vm->registers[dst_register_i].u16v0 OPERATOR##= vm->registers[src_register_i].u16v0;\
|
||||
break;\
|
||||
case 4: \
|
||||
vm->registers[dst_register_i].u32v OPERATOR##= vm->registers[src_register_i].u32v;\
|
||||
break;\
|
||||
}\
|
||||
return sizeof(dst_register_i) + sizeof(src_register_i) + sizeof(value_size);\
|
||||
}
|
||||
|
||||
/// ADD [dst_register] [src_register]
|
||||
i32 ADD_impl(VM* vm, size_t pos){
|
||||
mathOperatorImpl(+);
|
||||
}
|
||||
|
||||
/// SUB [dst_register] [src_register]
|
||||
i32 SUB_impl(VM* vm, size_t pos){
|
||||
mathOperatorImpl(-);
|
||||
}
|
||||
|
||||
/// MUL [dst_register] [src_register]
|
||||
i32 MUL_impl(VM* vm, size_t pos){
|
||||
mathOperatorImpl(*)
|
||||
}
|
||||
|
||||
/// DIV [dst_register] [src_register]
|
||||
i32 DIV_impl(VM* vm, size_t pos){
|
||||
mathOperatorImpl(/)
|
||||
}
|
||||
|
||||
/// MOD [dst_register] [src_register]
|
||||
i32 MOD_impl(VM* vm, size_t pos){
|
||||
mathOperatorImpl(%)
|
||||
}
|
||||
24
src/instructions/instructions.c
Normal file
24
src/instructions/instructions.c
Normal file
@@ -0,0 +1,24 @@
|
||||
#include "instructions.h"
|
||||
|
||||
const Instruction instructions[] = {
|
||||
Instruction_construct(NOP),
|
||||
Instruction_construct(PUSH),
|
||||
Instruction_construct(MOV),
|
||||
Instruction_construct(ADD),
|
||||
Instruction_construct(SUB),
|
||||
Instruction_construct(MUL),
|
||||
Instruction_construct(DIV),
|
||||
Instruction_construct(MOD),
|
||||
Instruction_construct(SYS),
|
||||
Instruction_construct(EXIT),
|
||||
// Instruction_construct(JMP),
|
||||
// Instruction_construct(CALL),
|
||||
};
|
||||
const size_t instructions_count = sizeof(instructions)/sizeof(instructions[0]);
|
||||
|
||||
const Instruction* Instruction_getFromOpcode(u8 opcode){
|
||||
if(opcode >= instructions_count)
|
||||
return NULL;
|
||||
|
||||
return instructions + opcode;
|
||||
}
|
||||
34
src/instructions/instructions.h
Normal file
34
src/instructions/instructions.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
#include "../VM/VM.h"
|
||||
|
||||
///@param program_pos position in vm->program next afrer opcode
|
||||
///@returns number of bytes read
|
||||
typedef i32 (*InstructionImplFunc_t)(VM* vm, size_t program_pos);
|
||||
|
||||
typedef struct Instruction {
|
||||
const char* name;
|
||||
InstructionImplFunc_t implementation;
|
||||
} Instruction;
|
||||
|
||||
#define Instruction_construct(NAME) {\
|
||||
.name = #NAME, \
|
||||
.implementation = NAME##_impl \
|
||||
}
|
||||
|
||||
/// @brief get instruction info from table
|
||||
/// @param opcode any byte
|
||||
/// @return ptr to struct or NULL
|
||||
const Instruction* Instruction_getFromOpcode(u8 opcode);
|
||||
|
||||
i32 NOP_impl(VM* vm, size_t pos);
|
||||
i32 PUSH_impl(VM* vm, size_t pos);
|
||||
i32 MOV_impl(VM* vm, size_t pos);
|
||||
i32 ADD_impl(VM* vm, size_t pos);
|
||||
i32 SUB_impl(VM* vm, size_t pos);
|
||||
i32 MUL_impl(VM* vm, size_t pos);
|
||||
i32 DIV_impl(VM* vm, size_t pos);
|
||||
i32 MOD_impl(VM* vm, size_t pos);
|
||||
i32 SYS_impl(VM* vm, size_t pos);
|
||||
i32 EXIT_impl(VM* vm, size_t pos);
|
||||
i32 JMP_impl(VM* vm, size_t pos);
|
||||
i32 CALL_impl(VM* vm, size_t pos);
|
||||
Reference in New Issue
Block a user