From a4b057f20a0dbbbe64487301788a56183fc522c3 Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Thu, 25 Apr 2024 02:55:54 +0530 Subject: [PATCH] Refactored inst.c and runtime.c for OPCODE_IS_TYPE change Pretty simple, there are fewer opcode types that use signed types than unsigned so it was a pretty simple rename. --- lib/inst.c | 66 ++++++++++++++++++++++++++++------------------------ vm/runtime.c | 50 +++++++++++++++++++-------------------- 2 files changed, 60 insertions(+), 56 deletions(-) diff --git a/lib/inst.c b/lib/inst.c index 3c8f3c5..cc5c104 100644 --- a/lib/inst.c +++ b/lib/inst.c @@ -247,27 +247,27 @@ void inst_print(inst_t instruction, FILE *fp) { static_assert(NUMBER_OF_OPCODES == 98, "inst_bytecode_size: Out of date"); fprintf(fp, "%s(", opcode_as_cstr(instruction.opcode)); - if (OPCODE_IS_TYPE(instruction.opcode, OP_PUSH)) + if (UNSIGNED_OPCODE_IS_TYPE(instruction.opcode, OP_PUSH)) { data_type_t type = (data_type_t)instruction.opcode; fprintf(fp, "datum=0x"); data_print(instruction.operand, type, fp); } - else if (OPCODE_IS_TYPE(instruction.opcode, OP_PUSH_REGISTER) || - OPCODE_IS_TYPE(instruction.opcode, OP_MOV)) + else if (UNSIGNED_OPCODE_IS_TYPE(instruction.opcode, OP_PUSH_REGISTER) || + UNSIGNED_OPCODE_IS_TYPE(instruction.opcode, OP_MOV)) { fprintf(fp, "reg=0x"); data_print(instruction.operand, DATA_TYPE_BYTE, fp); } - else if (OPCODE_IS_TYPE(instruction.opcode, OP_DUP) || - OPCODE_IS_TYPE(instruction.opcode, OP_MALLOC) || - OPCODE_IS_TYPE(instruction.opcode, OP_MSET) || - OPCODE_IS_TYPE(instruction.opcode, OP_MGET)) + else if (UNSIGNED_OPCODE_IS_TYPE(instruction.opcode, OP_DUP) || + UNSIGNED_OPCODE_IS_TYPE(instruction.opcode, OP_MALLOC) || + UNSIGNED_OPCODE_IS_TYPE(instruction.opcode, OP_MSET) || + UNSIGNED_OPCODE_IS_TYPE(instruction.opcode, OP_MGET)) { fprintf(fp, "n=%lu", instruction.operand.as_word); } else if (instruction.opcode == OP_JUMP_ABS || - OPCODE_IS_TYPE(instruction.opcode, OP_JUMP_IF) || + UNSIGNED_OPCODE_IS_TYPE(instruction.opcode, OP_JUMP_IF) || instruction.opcode == OP_CALL) { fprintf(fp, "address=0x"); @@ -280,7 +280,7 @@ size_t inst_bytecode_size(inst_t inst) { static_assert(NUMBER_OF_OPCODES == 98, "inst_bytecode_size: Out of date"); size_t size = 1; // for opcode - if (OPCODE_IS_TYPE(inst.opcode, OP_PUSH)) + if (UNSIGNED_OPCODE_IS_TYPE(inst.opcode, OP_PUSH)) { if (inst.opcode == OP_PUSH_BYTE) ++size; @@ -289,13 +289,14 @@ size_t inst_bytecode_size(inst_t inst) else if (inst.opcode == OP_PUSH_WORD) size += WORD_SIZE; } - else if (OPCODE_IS_TYPE(inst.opcode, OP_PUSH_REGISTER) || - OPCODE_IS_TYPE(inst.opcode, OP_MOV) || - OPCODE_IS_TYPE(inst.opcode, OP_DUP) || - OPCODE_IS_TYPE(inst.opcode, OP_MALLOC) || - OPCODE_IS_TYPE(inst.opcode, OP_MSET) || - OPCODE_IS_TYPE(inst.opcode, OP_MGET) || inst.opcode == OP_JUMP_ABS || - OPCODE_IS_TYPE(inst.opcode, OP_JUMP_IF) || inst.opcode == OP_CALL) + else if (UNSIGNED_OPCODE_IS_TYPE(inst.opcode, OP_PUSH_REGISTER) || + UNSIGNED_OPCODE_IS_TYPE(inst.opcode, OP_MOV) || + UNSIGNED_OPCODE_IS_TYPE(inst.opcode, OP_DUP) || + UNSIGNED_OPCODE_IS_TYPE(inst.opcode, OP_MALLOC) || + UNSIGNED_OPCODE_IS_TYPE(inst.opcode, OP_MSET) || + UNSIGNED_OPCODE_IS_TYPE(inst.opcode, OP_MGET) || + UNSIGNED_OPCODE_IS_TYPE(inst.opcode, OP_JUMP_IF) || + inst.opcode == OP_JUMP_ABS || inst.opcode == OP_CALL) size += WORD_SIZE; return size; } @@ -307,15 +308,16 @@ void inst_write_bytecode(inst_t inst, darr_t *darr) darr_append_byte(darr, inst.opcode); // Then append 0 or more operands data_type_t to_append = DATA_TYPE_NIL; - if (OPCODE_IS_TYPE(inst.opcode, OP_PUSH)) + if (UNSIGNED_OPCODE_IS_TYPE(inst.opcode, OP_PUSH)) to_append = (data_type_t)inst.opcode; - else if (OPCODE_IS_TYPE(inst.opcode, OP_PUSH_REGISTER) || - OPCODE_IS_TYPE(inst.opcode, OP_MOV) || - OPCODE_IS_TYPE(inst.opcode, OP_DUP) || - OPCODE_IS_TYPE(inst.opcode, OP_MALLOC) || - OPCODE_IS_TYPE(inst.opcode, OP_MSET) || - OPCODE_IS_TYPE(inst.opcode, OP_MGET) || inst.opcode == OP_JUMP_ABS || - OPCODE_IS_TYPE(inst.opcode, OP_JUMP_IF) || inst.opcode == OP_CALL) + else if (UNSIGNED_OPCODE_IS_TYPE(inst.opcode, OP_PUSH_REGISTER) || + UNSIGNED_OPCODE_IS_TYPE(inst.opcode, OP_MOV) || + UNSIGNED_OPCODE_IS_TYPE(inst.opcode, OP_DUP) || + UNSIGNED_OPCODE_IS_TYPE(inst.opcode, OP_MALLOC) || + UNSIGNED_OPCODE_IS_TYPE(inst.opcode, OP_MSET) || + UNSIGNED_OPCODE_IS_TYPE(inst.opcode, OP_MGET) || + UNSIGNED_OPCODE_IS_TYPE(inst.opcode, OP_JUMP_IF) || + inst.opcode == OP_JUMP_ABS || inst.opcode == OP_CALL) to_append = DATA_TYPE_WORD; switch (to_append) @@ -387,15 +389,17 @@ inst_t inst_read_bytecode(darr_t *darr) if (opcode > OP_HALT || opcode == NUMBER_OF_OPCODES || opcode < OP_NOOP) return INST_NOOP; // Read operands - if (OPCODE_IS_TYPE(opcode, OP_PUSH)) + if (UNSIGNED_OPCODE_IS_TYPE(opcode, OP_PUSH)) inst.operand = read_type_from_darr(darr, (data_type_t)opcode); // Read register (as a byte) - else if (OPCODE_IS_TYPE(opcode, OP_PUSH_REGISTER) || - OPCODE_IS_TYPE(opcode, OP_MOV) || OPCODE_IS_TYPE(opcode, OP_DUP) || - OPCODE_IS_TYPE(opcode, OP_MALLOC) || - OPCODE_IS_TYPE(opcode, OP_MSET) || OPCODE_IS_TYPE(opcode, OP_MGET) || - opcode == OP_JUMP_ABS || OPCODE_IS_TYPE(opcode, OP_JUMP_IF) || - opcode == OP_CALL) + else if (UNSIGNED_OPCODE_IS_TYPE(opcode, OP_PUSH_REGISTER) || + UNSIGNED_OPCODE_IS_TYPE(opcode, OP_MOV) || + UNSIGNED_OPCODE_IS_TYPE(opcode, OP_DUP) || + UNSIGNED_OPCODE_IS_TYPE(opcode, OP_MALLOC) || + UNSIGNED_OPCODE_IS_TYPE(opcode, OP_MSET) || + UNSIGNED_OPCODE_IS_TYPE(opcode, OP_MGET) || + UNSIGNED_OPCODE_IS_TYPE(opcode, OP_JUMP_IF) || + opcode == OP_JUMP_ABS || opcode == OP_CALL) inst.operand = read_type_from_darr(darr, DATA_TYPE_WORD); // Otherwise opcode doesn't take operands diff --git a/vm/runtime.c b/vm/runtime.c index f560c9b..d6cb149 100644 --- a/vm/runtime.c +++ b/vm/runtime.c @@ -67,17 +67,17 @@ err_t vm_execute(vm_t *vm) return ERR_END_OF_PROGRAM; inst_t instruction = program_data->instructions[prog->ptr]; - if (OPCODE_IS_TYPE(instruction.opcode, OP_PUSH)) + if (UNSIGNED_OPCODE_IS_TYPE(instruction.opcode, OP_PUSH)) { prog->ptr++; return PUSH_ROUTINES[instruction.opcode](vm, instruction.operand); } - else if (OPCODE_IS_TYPE(instruction.opcode, OP_MOV) || - OPCODE_IS_TYPE(instruction.opcode, OP_PUSH_REGISTER) || - OPCODE_IS_TYPE(instruction.opcode, OP_DUP) || - OPCODE_IS_TYPE(instruction.opcode, OP_MALLOC) || - OPCODE_IS_TYPE(instruction.opcode, OP_MSET) || - OPCODE_IS_TYPE(instruction.opcode, OP_MGET)) + else if (UNSIGNED_OPCODE_IS_TYPE(instruction.opcode, OP_MOV) || + UNSIGNED_OPCODE_IS_TYPE(instruction.opcode, OP_PUSH_REGISTER) || + UNSIGNED_OPCODE_IS_TYPE(instruction.opcode, OP_DUP) || + UNSIGNED_OPCODE_IS_TYPE(instruction.opcode, OP_MALLOC) || + UNSIGNED_OPCODE_IS_TYPE(instruction.opcode, OP_MSET) || + UNSIGNED_OPCODE_IS_TYPE(instruction.opcode, OP_MGET)) { err_t err = WORD_ROUTINES[instruction.opcode](vm, instruction.operand.as_word); @@ -85,7 +85,7 @@ err_t vm_execute(vm_t *vm) return err; prog->ptr++; } - else if (OPCODE_IS_TYPE(instruction.opcode, OP_POP)) + else if (UNSIGNED_OPCODE_IS_TYPE(instruction.opcode, OP_POP)) { // NOTE: We always use the first register to hold the result of // this pop. @@ -100,21 +100,21 @@ err_t vm_execute(vm_t *vm) return err; prog->ptr++; } - else if (OPCODE_IS_TYPE(instruction.opcode, OP_NOT) || - OPCODE_IS_TYPE(instruction.opcode, OP_OR) || - OPCODE_IS_TYPE(instruction.opcode, OP_AND) || - OPCODE_IS_TYPE(instruction.opcode, OP_XOR) || - OPCODE_IS_TYPE(instruction.opcode, OP_EQ) || - OPCODE_IS_TYPE(instruction.opcode, OP_LT) || - OPCODE_IS_TYPE(instruction.opcode, OP_LTE) || - OPCODE_IS_TYPE(instruction.opcode, OP_GT) || - OPCODE_IS_TYPE(instruction.opcode, OP_GTE) || - OPCODE_IS_TYPE(instruction.opcode, OP_PLUS) || - OPCODE_IS_TYPE(instruction.opcode, OP_SUB) || - OPCODE_IS_TYPE(instruction.opcode, OP_MULT) || - OPCODE_IS_TYPE(instruction.opcode, OP_MALLOC_STACK) || - OPCODE_IS_TYPE(instruction.opcode, OP_MSET_STACK) || - OPCODE_IS_TYPE(instruction.opcode, OP_MGET_STACK) || + else if (UNSIGNED_OPCODE_IS_TYPE(instruction.opcode, OP_NOT) || + UNSIGNED_OPCODE_IS_TYPE(instruction.opcode, OP_OR) || + UNSIGNED_OPCODE_IS_TYPE(instruction.opcode, OP_AND) || + UNSIGNED_OPCODE_IS_TYPE(instruction.opcode, OP_XOR) || + UNSIGNED_OPCODE_IS_TYPE(instruction.opcode, OP_EQ) || + UNSIGNED_OPCODE_IS_TYPE(instruction.opcode, OP_PLUS) || + UNSIGNED_OPCODE_IS_TYPE(instruction.opcode, OP_SUB) || + UNSIGNED_OPCODE_IS_TYPE(instruction.opcode, OP_MULT) || + UNSIGNED_OPCODE_IS_TYPE(instruction.opcode, OP_MALLOC_STACK) || + UNSIGNED_OPCODE_IS_TYPE(instruction.opcode, OP_MSET_STACK) || + UNSIGNED_OPCODE_IS_TYPE(instruction.opcode, OP_MGET_STACK) || + SIGNED_OPCODE_IS_TYPE(instruction.opcode, OP_LT) || + SIGNED_OPCODE_IS_TYPE(instruction.opcode, OP_LTE) || + SIGNED_OPCODE_IS_TYPE(instruction.opcode, OP_GT) || + SIGNED_OPCODE_IS_TYPE(instruction.opcode, OP_GTE) || instruction.opcode == OP_MDELETE || instruction.opcode == OP_MSIZE) { err_t err = STACK_ROUTINES[instruction.opcode](vm); @@ -133,7 +133,7 @@ err_t vm_execute(vm_t *vm) return err; return vm_jump(vm, ret.as_word); } - else if (OPCODE_IS_TYPE(instruction.opcode, OP_JUMP_IF)) + else if (UNSIGNED_OPCODE_IS_TYPE(instruction.opcode, OP_JUMP_IF)) { data_t datum = {0}; err_t err = ERR_OK; @@ -177,7 +177,7 @@ err_t vm_execute(vm_t *vm) return ERR_CALL_STACK_UNDERFLOW; return vm_jump(vm, vm->call_stack.address_pointers[--vm->call_stack.ptr]); } - else if (OPCODE_IS_TYPE(instruction.opcode, OP_PRINT)) + else if (SIGNED_OPCODE_IS_TYPE(instruction.opcode, OP_PRINT)) { data_t datum = {0}; enum