Added and implemented OP_JUMP_ABS

Jumps to the operand given, interpreted as a word, an absolute
address.
This commit is contained in:
2023-10-22 20:40:42 +01:00
parent bc0e0fce25
commit fffad9aea3
3 changed files with 26 additions and 9 deletions

View File

@@ -114,6 +114,10 @@ const char *opcode_as_cstr(opcode_t code)
case OP_EQ_WORD: case OP_EQ_WORD:
return "EQ_WORD"; return "EQ_WORD";
break; break;
case OP_JUMP_ABS:
return "JUMP_ABS";
break;
break;
case OP_HALT: case OP_HALT:
return "HALT"; return "HALT";
break; break;
@@ -205,7 +209,7 @@ void inst_print(inst_t instruction, FILE *fp)
size_t inst_bytecode_size(inst_t inst) 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 size_t size = 1; // for opcode
if (OPCODE_IS_TYPE(inst.opcode, OP_PUSH)) 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)) OPCODE_IS_TYPE(inst.opcode, OP_MOV))
// Only need a byte for the register // Only need a byte for the register
++size; ++size;
else if (OPCODE_IS_TYPE(inst.opcode, OP_POP)) else if (OPCODE_IS_TYPE(inst.opcode, OP_DUP))
// No operand or register so leave as is size += WORD_SIZE;
{} else if (inst.opcode == OP_JUMP_ABS)
size += WORD_SIZE;
return size; return size;
} }
void inst_write_bytecode(inst_t inst, darr_t *darr) 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 // Append opcode
darr_append_byte(darr, inst.opcode); darr_append_byte(darr, inst.opcode);
// Then append 0 or more operands // 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) || else if (OPCODE_IS_TYPE(inst.opcode, OP_PUSH_REGISTER) ||
OPCODE_IS_TYPE(inst.opcode, OP_MOV)) OPCODE_IS_TYPE(inst.opcode, OP_MOV))
to_append = DATA_TYPE_BYTE; 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; to_append = DATA_TYPE_WORD;
switch (to_append) 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) 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) if (darr->used >= darr->available)
return (inst_t){0}; return (inst_t){0};
inst_t inst = {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) || else if (OPCODE_IS_TYPE(opcode, OP_PUSH_REGISTER) ||
OPCODE_IS_TYPE(opcode, OP_MOV)) OPCODE_IS_TYPE(opcode, OP_MOV))
inst.operand = read_type_from_darr(darr, DATA_TYPE_BYTE); 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); inst.operand = read_type_from_darr(darr, DATA_TYPE_WORD);
// Otherwise opcode doesn't take operands // Otherwise opcode doesn't take operands

View File

@@ -65,6 +65,10 @@ typedef enum
OP_EQ_HWORD, OP_EQ_HWORD,
OP_EQ_WORD, OP_EQ_WORD,
// Mathematical operations
// Program control flow
OP_JUMP_ABS,
// Should not be an opcode // Should not be an opcode
NUMBER_OF_OPCODES, NUMBER_OF_OPCODES,
OP_HALT = 0b11111111, // top of the byte is a HALT 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_XOR(TYPE) ((inst_t){.opcode = OP_XOR_##TYPE})
#define INST_EQ(TYPE) ((inst_t){.opcode = OP_EQ_##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 #endif

View File

@@ -19,7 +19,7 @@
void vm_execute(vm_t *vm) 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; struct Program *prog = &vm->program;
if (prog->ptr >= prog->max) if (prog->ptr >= prog->max)
// TODO: Error (Went past end of program) // 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; vm_peek(vm, OPCODE_DATA_TYPE(instruction.opcode, OP_EQ)).as_word;
prog->ptr++; 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) else if (instruction.opcode == OP_HALT)
{ {
// Do nothing here. Should be caught by callers of vm_execute // Do nothing here. Should be caught by callers of vm_execute