aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2023-10-22 20:57:29 +0100
committerAryadev Chavali <aryadev@aryadevchavali.com>2023-10-22 20:57:29 +0100
commit7f1994c7aa1e3f18bc8baf7cb37f9fe093c884eb (patch)
tree055cb12056a3be08dcadad1f8fff59a2292c701c /src
parentd8e45fce0435dc0961db2f1b8f0f4a5805347118 (diff)
downloadovm-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')
-rw-r--r--src/inst.c31
-rw-r--r--src/inst.h4
-rw-r--r--src/runtime.c14
3 files changed, 39 insertions, 10 deletions
diff --git a/src/inst.c b/src/inst.c
index 0428979..a935d8a 100644
--- a/src/inst.c
+++ b/src/inst.c
@@ -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);
diff --git a/src/inst.h b/src/inst.h
index a9709fd..487a41b 100644
--- a/src/inst.h
+++ b/src/inst.h
@@ -68,6 +68,8 @@ typedef enum
// Mathematical operations
// Program control flow
OP_JUMP_ABS,
+ OP_JUMP_STACK,
+ OP_JUMP_REGISTER,
// Should not be an opcode
NUMBER_OF_OPCODES,
@@ -129,5 +131,7 @@ inst_t *insts_read_bytecode_file(FILE *, size_t *);
#define INST_JUMP_ABS(OP) \
((inst_t){.opcode = OP_JUMP_ABS, .operand = DWORD(OP)})
+#define INST_JUMP_STACK ((inst_t){.opcode = OP_JUMP_STACK})
+#define INST_JUMP_REGISTER ((inst_t){.opcode = OP_JUMP_REGISTER})
#endif
diff --git a/src/runtime.c b/src/runtime.c
index 6b708ee..e68dd25 100644
--- a/src/runtime.c
+++ b/src/runtime.c
@@ -19,7 +19,7 @@
void vm_execute(vm_t *vm)
{
- static_assert(NUMBER_OF_OPCODES == 32, "vm_execute: Out of date");
+ static_assert(NUMBER_OF_OPCODES == 34, "vm_execute: Out of date");
struct Program *prog = &vm->program;
if (prog->ptr >= prog->max)
// TODO: Error (Went past end of program)
@@ -96,6 +96,18 @@ void vm_execute(vm_t *vm)
// Set prog->ptr to the jump point requested
prog->ptr = instruction.operand.as_word;
}
+ else if (instruction.opcode == OP_JUMP_STACK)
+ {
+ // Set prog->ptr to the word on top of the stack
+ prog->ptr = vm_pop_word(vm).as_word;
+ }
+ else if (instruction.opcode == OP_JUMP_REGISTER)
+ {
+ if (instruction.operand.as_byte >= 8)
+ // TODO: Error register is not a valid word register
+ return;
+ prog->ptr = vm->registers.reg[instruction.operand.as_byte];
+ }
else if (instruction.opcode == OP_HALT)
{
// Do nothing here. Should be caught by callers of vm_execute