Added and implemented OP_JUMP_IF_*
Performs a jump when the (BYTE|HWORD|WORD) at the top of the stack is non zero.
This commit is contained in:
31
src/inst.c
31
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
|
||||
|
||||
|
||||
@@ -82,6 +82,9 @@ typedef enum
|
||||
OP_JUMP_ABS,
|
||||
OP_JUMP_STACK,
|
||||
OP_JUMP_REGISTER,
|
||||
OP_JUMP_IF_BYTE,
|
||||
OP_JUMP_IF_HWORD,
|
||||
OP_JUMP_IF_WORD,
|
||||
|
||||
// Should not be an opcode
|
||||
NUMBER_OF_OPCODES,
|
||||
@@ -146,6 +149,8 @@ inst_t *insts_read_bytecode_file(FILE *, size_t *);
|
||||
((inst_t){.opcode = OP_JUMP_ABS, .operand = DWORD(OP)})
|
||||
#define INST_JUMP_STACK ((inst_t){.opcode = OP_JUMP_STACK})
|
||||
#define INST_JUMP_REGISTER ((inst_t){.opcode = OP_JUMP_REGISTER})
|
||||
#define INST_JUMP_IF(TYPE, OP) \
|
||||
((inst_t){.opcode = OP_JUMP_IF_##TYPE, .operand = DWORD(OP)})
|
||||
|
||||
#define INST_PRINT(TYPE) ((inst_t){.opcode = OP_PRINT_##TYPE})
|
||||
#endif
|
||||
|
||||
@@ -55,7 +55,7 @@ const char *err_as_cstr(err_t err)
|
||||
|
||||
err_t vm_execute(vm_t *vm)
|
||||
{
|
||||
static_assert(NUMBER_OF_OPCODES == 43, "vm_execute: Out of date");
|
||||
static_assert(NUMBER_OF_OPCODES == 46, "vm_execute: Out of date");
|
||||
struct Program *prog = &vm->program;
|
||||
if (prog->ptr >= prog->max)
|
||||
return ERR_END_OF_PROGRAM;
|
||||
@@ -109,13 +109,7 @@ err_t vm_execute(vm_t *vm)
|
||||
return STACK_ROUTINES[instruction.opcode](vm);
|
||||
}
|
||||
else if (instruction.opcode == OP_JUMP_ABS)
|
||||
{
|
||||
// Set prog->ptr to the jump point requested
|
||||
if (instruction.operand.as_word >= vm->program.max)
|
||||
return ERR_INVALID_PROGRAM_ADDRESS;
|
||||
prog->ptr = instruction.operand.as_word;
|
||||
return ERR_OK;
|
||||
}
|
||||
return vm_jump(vm, instruction.operand.as_word);
|
||||
else if (instruction.opcode == OP_JUMP_STACK)
|
||||
{
|
||||
// Set prog->ptr to the word on top of the stack
|
||||
@@ -132,6 +126,26 @@ err_t vm_execute(vm_t *vm)
|
||||
word addr = vm->registers.reg[instruction.operand.as_byte];
|
||||
return vm_jump(vm, addr);
|
||||
}
|
||||
else if (OPCODE_IS_TYPE(instruction.opcode, OP_JUMP_IF))
|
||||
{
|
||||
data_t datum = {0};
|
||||
err_t err = ERR_OK;
|
||||
if (instruction.opcode == OP_JUMP_IF_BYTE)
|
||||
err = vm_pop_byte(vm, &datum);
|
||||
else if (instruction.opcode == OP_JUMP_IF_HWORD)
|
||||
err = vm_pop_hword(vm, &datum);
|
||||
else if (instruction.opcode == OP_JUMP_IF_WORD)
|
||||
err = vm_pop_word(vm, &datum);
|
||||
|
||||
if (err)
|
||||
return err;
|
||||
|
||||
// If datum != 0 then jump, else go to the next instruction
|
||||
if (datum.as_word != 0)
|
||||
return vm_jump(vm, instruction.operand.as_word);
|
||||
else
|
||||
++prog->ptr;
|
||||
}
|
||||
else if (instruction.opcode >= OP_PRINT_CHAR &&
|
||||
instruction.opcode <= OP_PRINT_WORD)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user