TCPU/src/instructions/instructions.c

102 lines
2.5 KiB
C

#include "instructions.h"
#include "tlibc/collections/HashMap.h"
i32 NOP_impl(VM* vm);
i32 EXIT_impl(VM* vm);
i32 SYS_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);
i32 DIV_impl(VM* vm);
i32 MOD_impl(VM* vm);
i32 EQ_impl(VM* vm);
i32 NE_impl(VM* vm);
i32 LT_impl(VM* vm);
i32 LE_impl(VM* vm);
i32 GT_impl(VM* vm);
i32 GE_impl(VM* vm);
i32 NOT_impl(VM* vm);
i32 INV_impl(VM* vm);
i32 OR_impl(VM* vm);
i32 XOR_impl(VM* vm);
i32 AND_impl(VM* vm);
i32 JMP_impl(VM* vm);
i32 JNZ_impl(VM* vm);
i32 JZ_impl(VM* vm);
static const Array(Instruction) instructions_array = ARRAY(Instruction, {
Instruction_construct(NOP),
Instruction_construct(EXIT),
Instruction_construct(SYS),
Instruction_construct(MOVC),
Instruction_construct(MOVR),
Instruction_construct(ADD),
Instruction_construct(SUB),
Instruction_construct(MUL),
Instruction_construct(DIV),
Instruction_construct(MOD),
Instruction_construct(EQ),
Instruction_construct(NE),
Instruction_construct(LT),
Instruction_construct(LE),
Instruction_construct(GT),
Instruction_construct(GE),
Instruction_construct(NOT),
Instruction_construct(INV),
Instruction_construct(OR),
Instruction_construct(XOR),
Instruction_construct(AND),
Instruction_construct(JMP),
Instruction_construct(JNZ),
Instruction_construct(JZ),
});
const Instruction* Instruction_getByOpcode(Opcode opcode){
if(opcode >= Array_len(&instructions_array, Instruction))
return NULL;
return (Instruction*)instructions_array.data + opcode;
}
static HashMap(Opcode)* opcode_map = NULL;
static void _opcode_map_construct(){
opcode_map = malloc(sizeof(*opcode_map));
HashMap_construct(opcode_map, Opcode, NULL);
for(u32 i = 0; i < Array_len(&instructions_array, Instruction); i++){
Instruction* instr_ptr = (Instruction*)instructions_array.data + i;
HashMap_tryPush(opcode_map, instr_ptr->name, &instr_ptr->opcode);
}
}
const Instruction* Instruction_getByName(str name){
if(opcode_map == NULL)
_opcode_map_construct();
str name_upper = str_toUpper(name);
Opcode* op_ptr = HashMap_tryGetPtr(opcode_map, name_upper);
free(name_upper.data);
if(op_ptr == NULL)
return NULL;
return Instruction_getByOpcode(*op_ptr);
}
void Instruction_destroySearchStructs(){
if(opcode_map != NULL){
HashMap_destroy(opcode_map);
free(opcode_map);
}
}