diff options
author | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-10-22 20:40:42 +0100 |
---|---|---|
committer | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-10-22 20:54:04 +0100 |
commit | fffad9aea3594c7de6ba918058c1e033c725a274 (patch) | |
tree | e52f626d31fde264cb578af854c84cf697d36f1c | |
parent | bc0e0fce252630110d6b0d18e55aac129b3d6d35 (diff) | |
download | ovm-fffad9aea3594c7de6ba918058c1e033c725a274.tar.gz ovm-fffad9aea3594c7de6ba918058c1e033c725a274.tar.bz2 ovm-fffad9aea3594c7de6ba918058c1e033c725a274.zip |
Added and implemented OP_JUMP_ABS
Jumps to the operand given, interpreted as a word, an absolute
address.
-rw-r--r-- | src/inst.c | 21 | ||||
-rw-r--r-- | src/inst.h | 7 | ||||
-rw-r--r-- | src/runtime.c | 7 |
3 files changed, 26 insertions, 9 deletions
@@ -114,6 +114,10 @@ const char *opcode_as_cstr(opcode_t code) case OP_EQ_WORD: return "EQ_WORD"; break; + case OP_JUMP_ABS: + return "JUMP_ABS"; + break; + break; case OP_HALT: return "HALT"; break; @@ -205,7 +209,7 @@ void inst_print(inst_t instruction, FILE *fp) size_t inst_bytecode_size(inst_t inst) { - static_assert(NUMBER_OF_OPCODES == 31, "inst_bytecode_size: Out of date"); + static_assert(NUMBER_OF_OPCODES == 32, "inst_bytecode_size: Out of date"); size_t size = 1; // for opcode if (OPCODE_IS_TYPE(inst.opcode, OP_PUSH)) { @@ -220,15 +224,16 @@ size_t inst_bytecode_size(inst_t inst) OPCODE_IS_TYPE(inst.opcode, OP_MOV)) // Only need a byte for the register ++size; - else if (OPCODE_IS_TYPE(inst.opcode, OP_POP)) - // No operand or register so leave as is - {} + else if (OPCODE_IS_TYPE(inst.opcode, OP_DUP)) + size += WORD_SIZE; + else if (inst.opcode == OP_JUMP_ABS) + size += WORD_SIZE; return size; } void inst_write_bytecode(inst_t inst, darr_t *darr) { - static_assert(NUMBER_OF_OPCODES == 31, "inst_write_bytecode: Out of date"); + static_assert(NUMBER_OF_OPCODES == 32, "inst_write_bytecode: Out of date"); // Append opcode darr_append_byte(darr, inst.opcode); // Then append 0 or more operands @@ -238,7 +243,7 @@ void inst_write_bytecode(inst_t inst, darr_t *darr) else if (OPCODE_IS_TYPE(inst.opcode, OP_PUSH_REGISTER) || OPCODE_IS_TYPE(inst.opcode, OP_MOV)) to_append = DATA_TYPE_BYTE; - else if (OPCODE_IS_TYPE(inst.opcode, OP_DUP)) + else if (OPCODE_IS_TYPE(inst.opcode, OP_DUP) || inst.opcode == OP_JUMP) to_append = DATA_TYPE_WORD; switch (to_append) @@ -300,7 +305,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 == 31, "inst_read_bytecode: Out of date"); + static_assert(NUMBER_OF_OPCODES == 32, "inst_read_bytecode: Out of date"); if (darr->used >= darr->available) return (inst_t){0}; inst_t inst = {0}; @@ -315,7 +320,7 @@ inst_t inst_read_bytecode(darr_t *darr) else if (OPCODE_IS_TYPE(opcode, OP_PUSH_REGISTER) || OPCODE_IS_TYPE(opcode, OP_MOV)) inst.operand = read_type_from_darr(darr, DATA_TYPE_BYTE); - else if (OPCODE_IS_TYPE(opcode, OP_DUP)) + else if (OPCODE_IS_TYPE(opcode, OP_DUP) || opcode == OP_JUMP_ABS) inst.operand = read_type_from_darr(darr, DATA_TYPE_WORD); // Otherwise opcode doesn't take operands @@ -65,6 +65,10 @@ typedef enum OP_EQ_HWORD, OP_EQ_WORD, + // Mathematical operations + // Program control flow + OP_JUMP_ABS, + // Should not be an opcode NUMBER_OF_OPCODES, OP_HALT = 0b11111111, // top of the byte is a HALT @@ -123,4 +127,7 @@ inst_t *insts_read_bytecode_file(FILE *, size_t *); #define INST_XOR(TYPE) ((inst_t){.opcode = OP_XOR_##TYPE}) #define INST_EQ(TYPE) ((inst_t){.opcode = OP_EQ_##TYPE}) +#define INST_JUMP_ABS(OP) \ + ((inst_t){.opcode = OP_JUMP_ABS, .operand = DWORD(OP)}) + #endif diff --git a/src/runtime.c b/src/runtime.c index c2c793c..5b6c547 100644 --- a/src/runtime.c +++ b/src/runtime.c @@ -19,7 +19,7 @@ void vm_execute(vm_t *vm) { - static_assert(NUMBER_OF_OPCODES == 31, "vm_execute: Out of date"); + static_assert(NUMBER_OF_OPCODES == 32, "vm_execute: Out of date"); struct Program *prog = &vm->program; if (prog->ptr >= prog->max) // TODO: Error (Went past end of program) @@ -110,6 +110,11 @@ void vm_execute(vm_t *vm) vm_peek(vm, OPCODE_DATA_TYPE(instruction.opcode, OP_EQ)).as_word; prog->ptr++; } + else if (instruction.opcode == OP_JUMP_ABS) + { + // Set prog->ptr to the jump point requested + prog->ptr = instruction.operand.as_word; + } else if (instruction.opcode == OP_HALT) { // Do nothing here. Should be caught by callers of vm_execute |