aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/inst.c21
-rw-r--r--src/inst.h7
-rw-r--r--src/runtime.c7
3 files changed, 26 insertions, 9 deletions
diff --git a/src/inst.c b/src/inst.c
index 9ca20bd..0428979 100644
--- a/src/inst.c
+++ b/src/inst.c
@@ -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
diff --git a/src/inst.h b/src/inst.h
index 61d7ede..a9709fd 100644
--- a/src/inst.h
+++ b/src/inst.h
@@ -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