aboutsummaryrefslogtreecommitdiff
path: root/lib/inst.c
diff options
context:
space:
mode:
authorAryadev Chavali <aryadev@aryadevchavali.com>2023-11-02 21:01:21 +0000
committerAryadev Chavali <aryadev@aryadevchavali.com>2023-11-02 21:01:21 +0000
commit86977fe3c140e6b5d6c09bc1d26d9f97d89a6488 (patch)
treea55acd39b074f472c0173b48b067c9abcb7440e8 /lib/inst.c
parent9afeed6d617da4841a031423286a0b3f3804f774 (diff)
downloadovm-86977fe3c140e6b5d6c09bc1d26d9f97d89a6488.tar.gz
ovm-86977fe3c140e6b5d6c09bc1d26d9f97d89a6488.tar.bz2
ovm-86977fe3c140e6b5d6c09bc1d26d9f97d89a6488.zip
Introduced instructions to engage with a call stack
Essentially you may "call" an absolute program address, which pushes the current address onto the call stack. CALL_STACK does the same thing but the absolute program address is taken from the data stack. RET pops an address off the call stack then jumps to that address.
Diffstat (limited to 'lib/inst.c')
-rw-r--r--lib/inst.c24
1 files changed, 16 insertions, 8 deletions
diff --git a/lib/inst.c b/lib/inst.c
index da951fe..30612d8 100644
--- a/lib/inst.c
+++ b/lib/inst.c
@@ -199,6 +199,12 @@ const char *opcode_as_cstr(opcode_t code)
return "JUMP_IF_HWORD";
case OP_JUMP_IF_WORD:
return "JUMP_IF_WORD";
+ case OP_CALL:
+ return "CALL";
+ case OP_CALL_STACK:
+ return "CALL_STACK";
+ case OP_RET:
+ return "RET";
case OP_PRINT_CHAR:
return "PRINT_CHAR";
case OP_PRINT_BYTE:
@@ -239,7 +245,7 @@ void data_print(data_t datum, data_type_t type, FILE *fp)
void inst_print(inst_t instruction, FILE *fp)
{
- static_assert(NUMBER_OF_OPCODES == 95, "inst_bytecode_size: Out of date");
+ static_assert(NUMBER_OF_OPCODES == 98, "inst_bytecode_size: Out of date");
fprintf(fp, "%s(", opcode_as_cstr(instruction.opcode));
if (OPCODE_IS_TYPE(instruction.opcode, OP_PUSH))
{
@@ -261,7 +267,8 @@ void inst_print(inst_t instruction, FILE *fp)
fprintf(fp, "n=%lu", instruction.operand.as_word);
}
else if (instruction.opcode == OP_JUMP_ABS ||
- OPCODE_IS_TYPE(instruction.opcode, OP_JUMP_IF))
+ OPCODE_IS_TYPE(instruction.opcode, OP_JUMP_IF) ||
+ instruction.opcode == OP_CALL)
{
fprintf(fp, "address=0x");
data_print(instruction.operand, DATA_TYPE_WORD, fp);
@@ -271,7 +278,7 @@ void inst_print(inst_t instruction, FILE *fp)
size_t inst_bytecode_size(inst_t inst)
{
- static_assert(NUMBER_OF_OPCODES == 95, "inst_bytecode_size: Out of date");
+ static_assert(NUMBER_OF_OPCODES == 98, "inst_bytecode_size: Out of date");
size_t size = 1; // for opcode
if (OPCODE_IS_TYPE(inst.opcode, OP_PUSH))
{
@@ -288,14 +295,14 @@ size_t inst_bytecode_size(inst_t inst)
OPCODE_IS_TYPE(inst.opcode, OP_MALLOC) ||
OPCODE_IS_TYPE(inst.opcode, OP_MSET) ||
OPCODE_IS_TYPE(inst.opcode, OP_MGET) || inst.opcode == OP_JUMP_ABS ||
- OPCODE_IS_TYPE(inst.opcode, OP_JUMP_IF))
+ OPCODE_IS_TYPE(inst.opcode, OP_JUMP_IF) || inst.opcode == OP_CALL)
size += WORD_SIZE;
return size;
}
void inst_write_bytecode(inst_t inst, darr_t *darr)
{
- static_assert(NUMBER_OF_OPCODES == 95, "inst_write_bytecode: Out of date");
+ static_assert(NUMBER_OF_OPCODES == 98, "inst_write_bytecode: Out of date");
// Append opcode
darr_append_byte(darr, inst.opcode);
// Then append 0 or more operands
@@ -308,7 +315,7 @@ void inst_write_bytecode(inst_t inst, darr_t *darr)
OPCODE_IS_TYPE(inst.opcode, OP_MALLOC) ||
OPCODE_IS_TYPE(inst.opcode, OP_MSET) ||
OPCODE_IS_TYPE(inst.opcode, OP_MGET) || inst.opcode == OP_JUMP_ABS ||
- OPCODE_IS_TYPE(inst.opcode, OP_JUMP_IF))
+ OPCODE_IS_TYPE(inst.opcode, OP_JUMP_IF) || inst.opcode == OP_CALL)
to_append = DATA_TYPE_WORD;
switch (to_append)
@@ -370,7 +377,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 == 95, "inst_read_bytecode: Out of date");
+ static_assert(NUMBER_OF_OPCODES == 98, "inst_read_bytecode: Out of date");
if (darr->used >= darr->available)
return (inst_t){0};
inst_t inst = {0};
@@ -385,7 +392,8 @@ inst_t inst_read_bytecode(darr_t *darr)
OPCODE_IS_TYPE(opcode, OP_MOV) || OPCODE_IS_TYPE(opcode, OP_DUP) ||
OPCODE_IS_TYPE(opcode, OP_MALLOC) ||
OPCODE_IS_TYPE(opcode, OP_MSET) || OPCODE_IS_TYPE(opcode, OP_MGET) ||
- opcode == OP_JUMP_ABS || OPCODE_IS_TYPE(opcode, OP_JUMP_IF))
+ opcode == OP_JUMP_ABS || OPCODE_IS_TYPE(opcode, OP_JUMP_IF) ||
+ opcode == OP_CALL)
inst.operand = read_type_from_darr(darr, DATA_TYPE_WORD);
// Otherwise opcode doesn't take operands