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:
2024-07-07 02:57:19 +01:00
parent ebdb1a948d
commit edf90780af
3 changed files with 5 additions and 31 deletions

View File

@@ -242,8 +242,6 @@ const char *opcode_as_cstr(opcode_t code)
return "PRINT_SWORD"; return "PRINT_SWORD";
case OP_JUMP_ABS: case OP_JUMP_ABS:
return "JUMP_ABS"; return "JUMP_ABS";
case OP_JUMP_STACK:
return "JUMP_STACK";
case OP_JUMP_IF_BYTE: case OP_JUMP_IF_BYTE:
return "JUMP_IF_BYTE"; return "JUMP_IF_BYTE";
case OP_JUMP_IF_SHORT: case OP_JUMP_IF_SHORT:
@@ -254,8 +252,6 @@ const char *opcode_as_cstr(opcode_t code)
return "JUMP_IF_WORD"; return "JUMP_IF_WORD";
case OP_CALL: case OP_CALL:
return "CALL"; return "CALL";
case OP_CALL_STACK:
return "CALL_STACK";
case OP_RET: case OP_RET:
return "RET"; return "RET";
case NUMBER_OF_OPCODES: 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) 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)); fprintf(fp, "%s(", opcode_as_cstr(instruction.opcode));
if (UNSIGNED_OPCODE_IS_TYPE(instruction.opcode, OP_PUSH)) 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) 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 size_t size = 1; // for opcode
if (UNSIGNED_OPCODE_IS_TYPE(opcode, OP_PUSH)) 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) 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; bytes[0] = inst.opcode;
size_t written = 1; 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) 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++); opcode_t opcode = *(bytes++);
if (opcode >= NUMBER_OF_OPCODES || opcode < OP_NOOP) if (opcode >= NUMBER_OF_OPCODES || opcode < OP_NOOP)

View File

@@ -168,7 +168,6 @@ typedef enum
// Program control flow // Program control flow
OP_JUMP_ABS, OP_JUMP_ABS,
OP_JUMP_STACK,
OP_JUMP_IF_BYTE, OP_JUMP_IF_BYTE,
OP_JUMP_IF_SHORT, OP_JUMP_IF_SHORT,
OP_JUMP_IF_HWORD, OP_JUMP_IF_HWORD,
@@ -176,7 +175,6 @@ typedef enum
// Subroutines // Subroutines
OP_CALL, OP_CALL,
OP_CALL_STACK,
OP_RET, OP_RET,
// Should not be an opcode // Should not be an opcode

View File

@@ -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) err_t vm_execute(vm_t *vm)
{ {
@@ -135,15 +135,6 @@ err_t vm_execute(vm_t *vm)
// Opcodes defined in loop // Opcodes defined in loop
else if (instruction.opcode == OP_JUMP_ABS) else if (instruction.opcode == OP_JUMP_ABS)
return vm_jump(vm, instruction.operand.as_word); 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)) else if (UNSIGNED_OPCODE_IS_TYPE(instruction.opcode, OP_JUMP_IF))
{ {
static_assert(DATA_TYPE_NIL == -1 && DATA_TYPE_WORD == 3, 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; vm->call_stack.address_pointers[vm->call_stack.ptr++] = vm->program.ptr + 1;
return vm_jump(vm, instruction.operand.as_word); 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) else if (instruction.opcode == OP_RET)
{ {
if (vm->call_stack.ptr == 0) if (vm->call_stack.ptr == 0)