aboutsummaryrefslogtreecommitdiff
path: root/src/inst.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/inst.c')
-rw-r--r--src/inst.c31
1 files changed, 22 insertions, 9 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);