diff options
author | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-10-22 20:57:29 +0100 |
---|---|---|
committer | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-10-22 20:57:29 +0100 |
commit | 7f1994c7aa1e3f18bc8baf7cb37f9fe093c884eb (patch) | |
tree | 055cb12056a3be08dcadad1f8fff59a2292c701c /src/inst.c | |
parent | d8e45fce0435dc0961db2f1b8f0f4a5805347118 (diff) | |
download | ovm-7f1994c7aa1e3f18bc8baf7cb37f9fe093c884eb.tar.gz ovm-7f1994c7aa1e3f18bc8baf7cb37f9fe093c884eb.tar.bz2 ovm-7f1994c7aa1e3f18bc8baf7cb37f9fe093c884eb.zip |
Added and implemented OP_JUMP_(STACK|REGISTER)
Uses the stack and register, respectively, to jump to an absolute
address. The stack based jump pops a word off the stack to perform a
jump, while the register based one uses the operand to figure out
which register to use.
Diffstat (limited to 'src/inst.c')
-rw-r--r-- | src/inst.c | 31 |
1 files changed, 22 insertions, 9 deletions
@@ -117,6 +117,11 @@ const char *opcode_as_cstr(opcode_t code) case OP_JUMP_ABS: return "JUMP_ABS"; break; + case OP_JUMP_STACK: + return "JUMP_STACK"; + break; + case OP_JUMP_REGISTER: + return "JUMP_REGISTER"; break; case OP_HALT: return "HALT"; @@ -186,6 +191,7 @@ data_type_t get_opcode_data_type(opcode_t opcode) void inst_print(inst_t instruction, FILE *fp) { + static_assert(NUMBER_OF_OPCODES == 34, "inst_bytecode_size: Out of date"); fprintf(fp, "%s(", opcode_as_cstr(instruction.opcode)); if (OPCODE_IS_TYPE(instruction.opcode, OP_PUSH)) { @@ -194,14 +200,19 @@ void inst_print(inst_t instruction, FILE *fp) data_print(instruction.operand, type, fp); } else if (OPCODE_IS_TYPE(instruction.opcode, OP_PUSH_REGISTER) || - OPCODE_IS_TYPE(instruction.opcode, OP_MOV)) + OPCODE_IS_TYPE(instruction.opcode, OP_MOV) || + instruction.opcode == OP_JUMP_REGISTER) { fprintf(fp, "reg=0x"); data_print(instruction.operand, DATA_TYPE_BYTE, fp); } else if (OPCODE_IS_TYPE(instruction.opcode, OP_DUP)) { - fprintf(fp, "n="); + fprintf(fp, "n=%lu", instruction.operand.as_word); + } + else if (instruction.opcode == OP_JUMP_ABS) + { + fprintf(fp, "address=0x"); data_print(instruction.operand, DATA_TYPE_WORD, fp); } fprintf(fp, ")"); @@ -209,7 +220,7 @@ void inst_print(inst_t instruction, FILE *fp) size_t inst_bytecode_size(inst_t inst) { - static_assert(NUMBER_OF_OPCODES == 32, "inst_bytecode_size: Out of date"); + static_assert(NUMBER_OF_OPCODES == 34, "inst_bytecode_size: Out of date"); size_t size = 1; // for opcode if (OPCODE_IS_TYPE(inst.opcode, OP_PUSH)) { @@ -221,7 +232,8 @@ size_t inst_bytecode_size(inst_t inst) 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_MOV) || + inst.opcode == OP_JUMP_REGISTER) // Only need a byte for the register ++size; else if (OPCODE_IS_TYPE(inst.opcode, OP_DUP)) @@ -233,7 +245,7 @@ size_t inst_bytecode_size(inst_t inst) void inst_write_bytecode(inst_t inst, darr_t *darr) { - static_assert(NUMBER_OF_OPCODES == 32, "inst_write_bytecode: Out of date"); + static_assert(NUMBER_OF_OPCODES == 34, "inst_write_bytecode: Out of date"); // Append opcode darr_append_byte(darr, inst.opcode); // Then append 0 or more operands @@ -241,9 +253,10 @@ void inst_write_bytecode(inst_t inst, darr_t *darr) if (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_MOV) || + inst.opcode == OP_JUMP_REGISTER) to_append = DATA_TYPE_BYTE; - else if (OPCODE_IS_TYPE(inst.opcode, OP_DUP) || inst.opcode == OP_JUMP) + else if (OPCODE_IS_TYPE(inst.opcode, OP_DUP) || inst.opcode == OP_JUMP_ABS) to_append = DATA_TYPE_WORD; switch (to_append) @@ -305,7 +318,7 @@ data_t read_type_from_darr(darr_t *darr, data_type_t type) inst_t inst_read_bytecode(darr_t *darr) { - static_assert(NUMBER_OF_OPCODES == 32, "inst_read_bytecode: Out of date"); + static_assert(NUMBER_OF_OPCODES == 34, "inst_read_bytecode: Out of date"); if (darr->used >= darr->available) return (inst_t){0}; inst_t inst = {0}; @@ -318,7 +331,7 @@ inst_t inst_read_bytecode(darr_t *darr) inst.operand = read_type_from_darr(darr, get_opcode_data_type(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_MOV) || inst.opcode == OP_JUMP_STACK) inst.operand = read_type_from_darr(darr, DATA_TYPE_BYTE); else if (OPCODE_IS_TYPE(opcode, OP_DUP) || opcode == OP_JUMP_ABS) inst.operand = read_type_from_darr(darr, DATA_TYPE_WORD); |