#ifndef UNDAR_VM_H #define UNDAR_VM_H #include "libc.h" typedef enum { OP_NOOP, /* noop : no nothing for 1 cycle */ OP_CALL, /* call : creates a new frame */ OP_RETURN, /* return : returns from a frame to the parent frame */ OP_SYSCALL, /* syscall : syscall(u8 src1 count_args, u32 ptr_memory) */ OP_LOAD_IMM, /* load_immediate : locals[dest] = constant */ OP_LOAD_IND_8, /* load_indirect_8 : locals[dest] = memory[locals[src1]] as u8 */ OP_LOAD_IND_16, /* load_indirect_16 : locals[dest] = memory[locals[src1]] as u16 */ OP_LOAD_IND_32, /* load_indirect_32 : locals[dest] = memory[locals[src1]] as u32 */ OP_LOAD_ABS_8, /* load_absolute_8 : locals[dest] = memory[src1] as u8 */ OP_LOAD_ABS_16, /* load_absolute_16 : locals[dest] = memory[src1] as u16 */ OP_LOAD_ABS_32, /* load_absolute_32 : locals[dest] = memory[src1] as u32 */ OP_LOAD_OFF_8, /* load_offset_8 : locals[dest] = memory[locals[src1] + offset] as u8 */ OP_LOAD_OFF_16, /* load_offset_16 : locals[dest] = memory[locals[src1] + offset] as u16 */ OP_LOAD_OFF_32, /* load_offset_32 : locals[dest] = memory[locals[src1] + offset] as u32 */ OP_STORE_ABS_8, /* store_absolute_8 : memory[dest] = src1 && 0xFF */ OP_STORE_ABS_16, /* store_absolute_16 : memory[dest] = src1 && 0xFFFF */ OP_STORE_ABS_32, /* store_absolute_32 : memory[dest] = src1 */ OP_STORE_IND_8, /* store_indirect_8 : memory[dest] = locals[src1] && 0xFF */ OP_STORE_IND_16, /* store_indirect_16 : memory[dest] = locals[src1] && 0xFFFF*/ OP_STORE_IND_32, /* store_indirect_32 : memory[dest] = locals[src1] */ OP_STORE_OFF_8, /* store_offset_8 : memory[locals[dest] + offset] = locals[src1] && 0xFF */ OP_STORE_OFF_16, /* store_offset_16 : memory[locals[dest] + offset] = locals[src1] && 0xFFFF */ OP_STORE_OFF_32, /* store_offset_32 : memory[locals[dest] + offset] = locals[src1] */ OP_MALLOC, /* malloc : dest = fat ptr to memory of ((src1 as size) + 4) */ OP_MEMSET_8, /* memset_8 : dest <-> dest+count = src1 as u8 */ OP_MEMSET_16, /* memset_16 : dest <-> dest+count = src1 as u16 */ OP_MEMSET_32, /* memset_32 : dest <-> dest+count = src1 as u32 */ OP_REG_MOV, /* register_move : locals[dest] = locals[src1] */ OP_ADD_INT, /* add_int : locals[dest] = locals[src1] + locals[src2] */ OP_SUB_INT, /* sub_int : locals[dest] = locals[src1] _ locals[src2] */ OP_MUL_INT, /* mul_int : locals[dest] = locals[src1] * locals[src2] */ OP_DIV_INT, /* div_int : locals[dest] = locals[src1] / locals[src2] */ OP_ABS_INT, /* abs_int : locals[dest] = | locals[src1] | */ OP_NEG_INT, /* neg_int : locals[dest] = -locals[src1] */ OP_ADD_NAT, /* add_nat : locals[dest] = locals[src1] + locals[src2] */ OP_SUB_NAT, /* sub_nat : locals[dest] = locals[src1] _ locals[src2] */ OP_MUL_NAT, /* mul_nat : locals[dest] = locals[src1] * locals[src2] */ OP_DIV_NAT, /* div_nat : locals[dest] = locals[src1] / locals[src2] */ OP_ABS_NAT, /* abs_nat : locals[dest] = | locals[src1] | */ OP_NEG_NAT, /* neg_nat : locals[dest] = -locals[src1] */ OP_ADD_REAL, /* add_real : locals[dest] = locals[src1] + locals[src2] */ OP_SUB_REAL, /* sub_real : locals[dest] = locals[src1] _ locals[src2] */ OP_MUL_REAL, /* mul_real : locals[dest] = locals[src1] * locals[src2] */ OP_DIV_REAL, /* div_real : locals[dest] = locals[src1] / locals[src2] */ OP_ABS_REAL, /* abs_real : locals[dest] = | locals[src1] | */ OP_NEG_REAL, /* neg_real : locals[dest] = _locals[src1] */ OP_INT_TO_REAL, /* int_to_real : locals[dest] = locals[src1] as real */ OP_INT_TO_NAT, /* int_to_nat : locals[dest] = locals[src1] as nat */ OP_NAT_TO_REAL, /* nat_to_real : locals[dest] = locals[src1] as real */ OP_NAT_TO_INT, /* nat_to_int : locals[dest] = locals[src1] as int */ OP_REAL_TO_INT, /* real_to_int : locals[dest] = locals[src1] as int */ OP_REAL_TO_NAT, /* real_to_nat : locals[dest] = locals[src1] as nat */ OP_BIT_SHIFT_LEFT, /* bit_shift_left : locals[dest] = locals[src1] << locals[src2] */ OP_BIT_SHIFT_RIGHT,/* bit_shift_right : locals[dest] = locals[src1] >> locals[src2] */ OP_BIT_SHIFT_R_EXT,/* bit_shift_r_ext : locals[dest] as i32 = locals[src1] >> locals[src2] */ OP_BAND, /* bit_and : locals[dest] = locals[src1] & locals[src2] */ OP_BOR, /* bit_or : locals[dest] = locals[src1] | locals[src2] */ OP_BXOR, /* bit_xor : locals[dest] = locals[src1] ^ locals[src2] */ OP_JMP, /* jump : jump to &dest unconditionally */ OP_JMPF, /* jump_if_flag : jump to &dest if flag != 0 */ OP_JEQ_INT, /* jump_eq_int : jump to &dest if locals[src1] as int == locals[src2] as int */ OP_JNEQ_INT, /* jump_neq_int : jump to &dest if locals[src1] as int != locals[src2] as int */ OP_JGT_INT, /* jump_gt_int : jump to &dest if locals[src1] as int > locals[src2] as int */ OP_JLT_INT, /* jump_lt_int : jump to &dest if locals[src1] as int < locals[src2] as int */ OP_JLE_INT, /* jump_le_int : jump to &dest if locals[src1] as int <= locals[src2] as int */ OP_JGE_INT, /* jump_ge_int : jump to &dest if locals[src1] as int >= locals[src2] as int */ OP_JEQ_NAT, /* jump_eq_nat : jump to &dest if locals[src1] as nat == locals[src2] as nat */ OP_JNEQ_NAT, /* jump_neq_nat : jump to &dest if locals[src1] as nat != locals[src2] as nat */ OP_JGT_NAT, /* jump_gt_nat : jump to &dest if locals[src1] as nat > locals[src2] as nat */ OP_JLT_NAT, /* jump_lt_nat : jump to &dest if locals[src1] as nat < locals[src2] as nat */ OP_JLE_NAT, /* jump_le_nat : jump to &dest if locals[src1] as nat <= locals[src2] as nat */ OP_JGE_NAT, /* jump_ge_nat : jump to &dest if locals[src1] as nat >= locals[src2] as nat */ OP_JEQ_REAL, /* jump_eq_real : jump to &dest if locals[src1] as real == locals[src2] as real */ OP_JNEQ_REAL, /* jump_neq_real : jump to &dest if locals[src1] as real != locals[src2] as real */ OP_JGE_REAL, /* jump_ge_real : jump to &dest if locals[src1] as real >= locals[src2] as real */ OP_JGT_REAL, /* jump_gt_real : jump to &dest if locals[src1] as real > locals[src2] as real */ OP_JLT_REAL, /* jump_lt_real : jump to &dest if locals[src1] as real < locals[src2] as real */ OP_JLE_REAL, /* jump_le_real : jump to &dest if locals[src1] as real <= locals[src2] as real */ OP_MAX_OPCODE /* not really an opcode but used to check max length of ops */ } Opcode; typedef struct vm_s VM; #define MEM_SIZE 65536 struct vm_s { u32 pc; u8 mem[MEM_SIZE]; }; extern bool init_vm(VM *vm); bool step_vm(VM *vm); #endif