TCPU/src/instructions/instructions.c
2025-03-12 12:57:42 +05:00

98 lines
2.5 KiB
C

#include "instructions.h"
#include "../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);
Array_declare(Instruction);
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 >= instructions_array.len)
return NULL;
return instructions_array.data + opcode;
}
HashMap_declare(Instruction);
HashMap_define(Instruction, HashMap_DESTROY_VALUE_FUNC_NULL);
static HashMap_Instruction* instructions_map = NULL;
const Instruction* Instruction_getByName(str name){
if(instructions_map == NULL){
instructions_map = malloc(sizeof(HashMap_Instruction));
HashMap_Instruction_alloc(instructions_map);
for(u32 i = 0; i < instructions_array.len; i++){
HashMap_Instruction_tryPush(instructions_map, instructions_array.data[i].name, instructions_array.data[i]);
}
}
str name_upper = str_toUpper(name);
Instruction* iptr = HashMap_Instruction_tryGetPtr(instructions_map, name_upper);
free(name_upper.data);
return iptr;
}
void Instruction_freeSearchStructs(){
if(instructions_map != NULL){
HashMap_Instruction_free(instructions_map);
free(instructions_map);
}
}