aboutsummaryrefslogtreecommitdiff
path: root/src/inst.c
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2023-10-23 01:45:54 +0100
committerAryadev Chavali <aryadev@aryadevchavali.com>2023-10-23 01:45:54 +0100
commit9496da9d93234ea0e48e54bd6760bf3118063826 (patch)
tree5d609b503bac938a35ef70d6416b2753be0d2d68 /src/inst.c
parentb93a4af4952906e544f35805720c6b547f1f062e (diff)
downloadovm-9496da9d93234ea0e48e54bd6760bf3118063826.tar.gz
ovm-9496da9d93234ea0e48e54bd6760bf3118063826.tar.bz2
ovm-9496da9d93234ea0e48e54bd6760bf3118063826.zip
Added and implemented OP_JUMP_IF_*
Performs a jump when the (BYTE|HWORD|WORD) at the top of the stack is non zero.
Diffstat (limited to 'src/inst.c')
-rw-r--r--src/inst.c31
1 files changed, 21 insertions, 10 deletions
diff --git a/src/inst.c b/src/inst.c
index dbfb885..3980173 100644
--- a/src/inst.c
+++ b/src/inst.c
@@ -132,6 +132,15 @@ const char *opcode_as_cstr(opcode_t code)
case OP_JUMP_REGISTER:
return "JUMP_REGISTER";
break;
+ case OP_JUMP_IF_BYTE:
+ return "JUMP_IF_BYTE";
+ break;
+ case OP_JUMP_IF_HWORD:
+ return "JUMP_IF_HWORD";
+ break;
+ case OP_JUMP_IF_WORD:
+ return "JUMP_IF_WORD";
+ break;
case OP_PRINT_CHAR:
return "PRINT_CHAR";
break;
@@ -204,7 +213,7 @@ word convert_bytes_to_word(byte *bytes)
void inst_print(inst_t instruction, FILE *fp)
{
- static_assert(NUMBER_OF_OPCODES == 43, "inst_bytecode_size: Out of date");
+ static_assert(NUMBER_OF_OPCODES == 46, "inst_bytecode_size: Out of date");
fprintf(fp, "%s(", opcode_as_cstr(instruction.opcode));
if (OPCODE_IS_TYPE(instruction.opcode, OP_PUSH))
{
@@ -223,7 +232,8 @@ void inst_print(inst_t instruction, FILE *fp)
{
fprintf(fp, "n=%lu", instruction.operand.as_word);
}
- else if (instruction.opcode == OP_JUMP_ABS)
+ else if (instruction.opcode == OP_JUMP_ABS ||
+ OPCODE_IS_TYPE(instruction.opcode, OP_JUMP_IF))
{
fprintf(fp, "address=0x");
data_print(instruction.operand, DATA_TYPE_WORD, fp);
@@ -233,7 +243,7 @@ void inst_print(inst_t instruction, FILE *fp)
size_t inst_bytecode_size(inst_t inst)
{
- static_assert(NUMBER_OF_OPCODES == 43, "inst_bytecode_size: Out of date");
+ static_assert(NUMBER_OF_OPCODES == 46, "inst_bytecode_size: Out of date");
size_t size = 1; // for opcode
if (OPCODE_IS_TYPE(inst.opcode, OP_PUSH))
{
@@ -249,16 +259,15 @@ size_t inst_bytecode_size(inst_t inst)
inst.opcode == OP_JUMP_REGISTER)
// Only need a byte for the register
++size;
- else if (OPCODE_IS_TYPE(inst.opcode, OP_DUP))
- size += WORD_SIZE;
- else if (inst.opcode == OP_JUMP_ABS)
+ else if (OPCODE_IS_TYPE(inst.opcode, OP_DUP) || inst.opcode == OP_JUMP_ABS ||
+ OPCODE_IS_TYPE(inst.opcode, OP_JUMP_IF))
size += WORD_SIZE;
return size;
}
void inst_write_bytecode(inst_t inst, darr_t *darr)
{
- static_assert(NUMBER_OF_OPCODES == 43, "inst_write_bytecode: Out of date");
+ static_assert(NUMBER_OF_OPCODES == 46, "inst_write_bytecode: Out of date");
// Append opcode
darr_append_byte(darr, inst.opcode);
// Then append 0 or more operands
@@ -269,7 +278,8 @@ void inst_write_bytecode(inst_t inst, darr_t *darr)
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_ABS)
+ else if (OPCODE_IS_TYPE(inst.opcode, OP_DUP) || inst.opcode == OP_JUMP_ABS ||
+ OPCODE_IS_TYPE(inst.opcode, OP_JUMP_IF))
to_append = DATA_TYPE_WORD;
switch (to_append)
@@ -331,7 +341,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 == 43, "inst_read_bytecode: Out of date");
+ static_assert(NUMBER_OF_OPCODES == 46, "inst_read_bytecode: Out of date");
if (darr->used >= darr->available)
return (inst_t){0};
inst_t inst = {0};
@@ -346,7 +356,8 @@ inst_t inst_read_bytecode(darr_t *darr)
else if (OPCODE_IS_TYPE(opcode, OP_PUSH_REGISTER) ||
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)
+ else if (OPCODE_IS_TYPE(opcode, OP_DUP) || opcode == OP_JUMP_ABS ||
+ OPCODE_IS_TYPE(opcode, OP_JUMP_IF))
inst.operand = read_type_from_darr(darr, DATA_TYPE_WORD);
// Otherwise opcode doesn't take operands