Remove JUMP_STACK and CALL_STACK
Any program that generates addresses at runtime can be easily expressed through fixed address jumps and calls. No need for them.
This commit is contained in:
12
lib/inst.c
12
lib/inst.c
@@ -242,8 +242,6 @@ const char *opcode_as_cstr(opcode_t code)
|
||||
return "PRINT_SWORD";
|
||||
case OP_JUMP_ABS:
|
||||
return "JUMP_ABS";
|
||||
case OP_JUMP_STACK:
|
||||
return "JUMP_STACK";
|
||||
case OP_JUMP_IF_BYTE:
|
||||
return "JUMP_IF_BYTE";
|
||||
case OP_JUMP_IF_SHORT:
|
||||
@@ -254,8 +252,6 @@ const char *opcode_as_cstr(opcode_t code)
|
||||
return "JUMP_IF_WORD";
|
||||
case OP_CALL:
|
||||
return "CALL";
|
||||
case OP_CALL_STACK:
|
||||
return "CALL_STACK";
|
||||
case OP_RET:
|
||||
return "RET";
|
||||
case NUMBER_OF_OPCODES:
|
||||
@@ -287,7 +283,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 == 117, "inst_print: Out of date");
|
||||
static_assert(NUMBER_OF_OPCODES == 115, "inst_print: Out of date");
|
||||
fprintf(fp, "%s(", opcode_as_cstr(instruction.opcode));
|
||||
if (UNSIGNED_OPCODE_IS_TYPE(instruction.opcode, OP_PUSH))
|
||||
{
|
||||
@@ -317,7 +313,7 @@ void inst_print(inst_t instruction, FILE *fp)
|
||||
|
||||
size_t opcode_bytecode_size(opcode_t opcode)
|
||||
{
|
||||
static_assert(NUMBER_OF_OPCODES == 117, "inst_bytecode_size: Out of date");
|
||||
static_assert(NUMBER_OF_OPCODES == 115, "inst_bytecode_size: Out of date");
|
||||
size_t size = 1; // for opcode
|
||||
if (UNSIGNED_OPCODE_IS_TYPE(opcode, OP_PUSH))
|
||||
{
|
||||
@@ -342,7 +338,7 @@ size_t opcode_bytecode_size(opcode_t opcode)
|
||||
|
||||
size_t inst_write_bytecode(inst_t inst, byte_t *bytes)
|
||||
{
|
||||
static_assert(NUMBER_OF_OPCODES == 117, "inst_write_bytecode: Out of date");
|
||||
static_assert(NUMBER_OF_OPCODES == 115, "inst_write_bytecode: Out of date");
|
||||
|
||||
bytes[0] = inst.opcode;
|
||||
size_t written = 1;
|
||||
@@ -426,7 +422,7 @@ bool read_type_from_darr(byte_t *bytes, size_t size, data_type_t type,
|
||||
|
||||
int inst_read_bytecode(inst_t *ptr, byte_t *bytes, size_t size_bytes)
|
||||
{
|
||||
static_assert(NUMBER_OF_OPCODES == 117, "inst_read_bytecode: Out of date");
|
||||
static_assert(NUMBER_OF_OPCODES == 115, "inst_read_bytecode: Out of date");
|
||||
|
||||
opcode_t opcode = *(bytes++);
|
||||
if (opcode >= NUMBER_OF_OPCODES || opcode < OP_NOOP)
|
||||
|
||||
@@ -168,7 +168,6 @@ typedef enum
|
||||
|
||||
// Program control flow
|
||||
OP_JUMP_ABS,
|
||||
OP_JUMP_STACK,
|
||||
OP_JUMP_IF_BYTE,
|
||||
OP_JUMP_IF_SHORT,
|
||||
OP_JUMP_IF_HWORD,
|
||||
@@ -176,7 +175,6 @@ typedef enum
|
||||
|
||||
// Subroutines
|
||||
OP_CALL,
|
||||
OP_CALL_STACK,
|
||||
OP_RET,
|
||||
|
||||
// Should not be an opcode
|
||||
|
||||
22
vm/runtime.c
22
vm/runtime.c
@@ -61,7 +61,7 @@ const char *err_as_cstr(err_t err)
|
||||
}
|
||||
}
|
||||
|
||||
static_assert(NUMBER_OF_OPCODES == 117, "vm_execute: Out of date");
|
||||
static_assert(NUMBER_OF_OPCODES == 115, "vm_execute: Out of date");
|
||||
|
||||
err_t vm_execute(vm_t *vm)
|
||||
{
|
||||
@@ -135,15 +135,6 @@ err_t vm_execute(vm_t *vm)
|
||||
// Opcodes defined in loop
|
||||
else if (instruction.opcode == OP_JUMP_ABS)
|
||||
return vm_jump(vm, instruction.operand.as_word);
|
||||
else if (instruction.opcode == OP_JUMP_STACK)
|
||||
{
|
||||
data_t ret = {0};
|
||||
// Set prog->ptr to the word on top of the stack
|
||||
err_t err = vm_pop_word(vm, &ret);
|
||||
if (err)
|
||||
return err;
|
||||
return vm_jump(vm, ret.as_word);
|
||||
}
|
||||
else if (UNSIGNED_OPCODE_IS_TYPE(instruction.opcode, OP_JUMP_IF))
|
||||
{
|
||||
static_assert(DATA_TYPE_NIL == -1 && DATA_TYPE_WORD == 3,
|
||||
@@ -172,17 +163,6 @@ err_t vm_execute(vm_t *vm)
|
||||
vm->call_stack.address_pointers[vm->call_stack.ptr++] = vm->program.ptr + 1;
|
||||
return vm_jump(vm, instruction.operand.as_word);
|
||||
}
|
||||
else if (instruction.opcode == OP_CALL_STACK)
|
||||
{
|
||||
if (vm->call_stack.ptr >= vm->call_stack.max)
|
||||
return ERR_CALL_STACK_OVERFLOW;
|
||||
vm->call_stack.address_pointers[vm->call_stack.ptr++] = vm->program.ptr + 1;
|
||||
data_t ret = {0};
|
||||
err_t err = vm_pop_word(vm, &ret);
|
||||
if (err)
|
||||
return err;
|
||||
return vm_jump(vm, ret.as_word);
|
||||
}
|
||||
else if (instruction.opcode == OP_RET)
|
||||
{
|
||||
if (vm->call_stack.ptr == 0)
|
||||
|
||||
Reference in New Issue
Block a user