Removed instruction OP_JUMP_REGISTER
Not necessary when you can just push the relevant word onto the stack then just do OP_JUMP_STACK.
This commit is contained in:
@@ -137,7 +137,7 @@ bool is_valid_hex_char(char c)
|
|||||||
|
|
||||||
token_t tokenise_symbol(buffer_t *buffer, size_t *column)
|
token_t tokenise_symbol(buffer_t *buffer, size_t *column)
|
||||||
{
|
{
|
||||||
static_assert(NUMBER_OF_OPCODES == 96, "tokenise_buffer: Out of date!");
|
static_assert(NUMBER_OF_OPCODES == 95, "tokenise_buffer: Out of date!");
|
||||||
|
|
||||||
size_t sym_size = 0;
|
size_t sym_size = 0;
|
||||||
for (; sym_size < space_left(buffer) &&
|
for (; sym_size < space_left(buffer) &&
|
||||||
|
|||||||
10
asm/parser.c
10
asm/parser.c
@@ -413,16 +413,6 @@ perr_t parse_next(token_stream_t *stream, presult_t *ret)
|
|||||||
return PERR_EXPECTED_OPERAND;
|
return PERR_EXPECTED_OPERAND;
|
||||||
return parse_word_label_or_relative(stream, ret);
|
return parse_word_label_or_relative(stream, ret);
|
||||||
}
|
}
|
||||||
else if (token.str_size == 9 && strncmp(token.str, ".REGISTER", 9) == 0)
|
|
||||||
{
|
|
||||||
*ret = (presult_t){.instruction = INST_JUMP_REGISTER(0),
|
|
||||||
.type = PRES_COMPLETE_RESULT};
|
|
||||||
++stream->used;
|
|
||||||
if (stream->used >= stream->available)
|
|
||||||
return PERR_EXPECTED_OPERAND;
|
|
||||||
return parse_word(TOKEN_STREAM_AT(stream->data, stream->used),
|
|
||||||
&ret->instruction.operand.as_word);
|
|
||||||
}
|
|
||||||
else if (token.str_size == 6 && strncmp(token.str, ".STACK", 6) == 0)
|
else if (token.str_size == 6 && strncmp(token.str, ".STACK", 6) == 0)
|
||||||
*ret = (presult_t){.instruction = INST_JUMP_STACK,
|
*ret = (presult_t){.instruction = INST_JUMP_STACK,
|
||||||
.type = PRES_COMPLETE_RESULT};
|
.type = PRES_COMPLETE_RESULT};
|
||||||
|
|||||||
20
lib/inst.c
20
lib/inst.c
@@ -193,8 +193,6 @@ const char *opcode_as_cstr(opcode_t code)
|
|||||||
return "JUMP_ABS";
|
return "JUMP_ABS";
|
||||||
case OP_JUMP_STACK:
|
case OP_JUMP_STACK:
|
||||||
return "JUMP_STACK";
|
return "JUMP_STACK";
|
||||||
case OP_JUMP_REGISTER:
|
|
||||||
return "JUMP_REGISTER";
|
|
||||||
case OP_JUMP_IF_BYTE:
|
case OP_JUMP_IF_BYTE:
|
||||||
return "JUMP_IF_BYTE";
|
return "JUMP_IF_BYTE";
|
||||||
case OP_JUMP_IF_HWORD:
|
case OP_JUMP_IF_HWORD:
|
||||||
@@ -241,7 +239,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 == 96, "inst_bytecode_size: Out of date");
|
static_assert(NUMBER_OF_OPCODES == 95, "inst_bytecode_size: Out of date");
|
||||||
fprintf(fp, "%s(", opcode_as_cstr(instruction.opcode));
|
fprintf(fp, "%s(", opcode_as_cstr(instruction.opcode));
|
||||||
if (OPCODE_IS_TYPE(instruction.opcode, OP_PUSH))
|
if (OPCODE_IS_TYPE(instruction.opcode, OP_PUSH))
|
||||||
{
|
{
|
||||||
@@ -250,8 +248,7 @@ void inst_print(inst_t instruction, FILE *fp)
|
|||||||
data_print(instruction.operand, type, fp);
|
data_print(instruction.operand, type, fp);
|
||||||
}
|
}
|
||||||
else if (OPCODE_IS_TYPE(instruction.opcode, OP_PUSH_REGISTER) ||
|
else if (OPCODE_IS_TYPE(instruction.opcode, OP_PUSH_REGISTER) ||
|
||||||
OPCODE_IS_TYPE(instruction.opcode, OP_MOV) ||
|
OPCODE_IS_TYPE(instruction.opcode, OP_MOV))
|
||||||
instruction.opcode == OP_JUMP_REGISTER)
|
|
||||||
{
|
{
|
||||||
fprintf(fp, "reg=0x");
|
fprintf(fp, "reg=0x");
|
||||||
data_print(instruction.operand, DATA_TYPE_BYTE, fp);
|
data_print(instruction.operand, DATA_TYPE_BYTE, fp);
|
||||||
@@ -274,7 +271,7 @@ void inst_print(inst_t instruction, FILE *fp)
|
|||||||
|
|
||||||
size_t inst_bytecode_size(inst_t inst)
|
size_t inst_bytecode_size(inst_t inst)
|
||||||
{
|
{
|
||||||
static_assert(NUMBER_OF_OPCODES == 96, "inst_bytecode_size: Out of date");
|
static_assert(NUMBER_OF_OPCODES == 95, "inst_bytecode_size: Out of date");
|
||||||
size_t size = 1; // for opcode
|
size_t size = 1; // for opcode
|
||||||
if (OPCODE_IS_TYPE(inst.opcode, OP_PUSH))
|
if (OPCODE_IS_TYPE(inst.opcode, OP_PUSH))
|
||||||
{
|
{
|
||||||
@@ -291,7 +288,6 @@ size_t inst_bytecode_size(inst_t inst)
|
|||||||
OPCODE_IS_TYPE(inst.opcode, OP_MALLOC) ||
|
OPCODE_IS_TYPE(inst.opcode, OP_MALLOC) ||
|
||||||
OPCODE_IS_TYPE(inst.opcode, OP_MSET) ||
|
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_MGET) || inst.opcode == OP_JUMP_ABS ||
|
||||||
inst.opcode == OP_JUMP_REGISTER ||
|
|
||||||
OPCODE_IS_TYPE(inst.opcode, OP_JUMP_IF))
|
OPCODE_IS_TYPE(inst.opcode, OP_JUMP_IF))
|
||||||
size += WORD_SIZE;
|
size += WORD_SIZE;
|
||||||
return size;
|
return size;
|
||||||
@@ -299,7 +295,7 @@ size_t inst_bytecode_size(inst_t inst)
|
|||||||
|
|
||||||
void inst_write_bytecode(inst_t inst, darr_t *darr)
|
void inst_write_bytecode(inst_t inst, darr_t *darr)
|
||||||
{
|
{
|
||||||
static_assert(NUMBER_OF_OPCODES == 96, "inst_write_bytecode: Out of date");
|
static_assert(NUMBER_OF_OPCODES == 95, "inst_write_bytecode: Out of date");
|
||||||
// Append opcode
|
// Append opcode
|
||||||
darr_append_byte(darr, inst.opcode);
|
darr_append_byte(darr, inst.opcode);
|
||||||
// Then append 0 or more operands
|
// Then append 0 or more operands
|
||||||
@@ -311,8 +307,7 @@ void inst_write_bytecode(inst_t inst, darr_t *darr)
|
|||||||
OPCODE_IS_TYPE(inst.opcode, OP_DUP) ||
|
OPCODE_IS_TYPE(inst.opcode, OP_DUP) ||
|
||||||
OPCODE_IS_TYPE(inst.opcode, OP_MALLOC) ||
|
OPCODE_IS_TYPE(inst.opcode, OP_MALLOC) ||
|
||||||
OPCODE_IS_TYPE(inst.opcode, OP_MSET) ||
|
OPCODE_IS_TYPE(inst.opcode, OP_MSET) ||
|
||||||
OPCODE_IS_TYPE(inst.opcode, OP_MGET) ||
|
OPCODE_IS_TYPE(inst.opcode, OP_MGET) || inst.opcode == OP_JUMP_ABS ||
|
||||||
inst.opcode == OP_JUMP_REGISTER || inst.opcode == OP_JUMP_ABS ||
|
|
||||||
OPCODE_IS_TYPE(inst.opcode, OP_JUMP_IF))
|
OPCODE_IS_TYPE(inst.opcode, OP_JUMP_IF))
|
||||||
to_append = DATA_TYPE_WORD;
|
to_append = DATA_TYPE_WORD;
|
||||||
|
|
||||||
@@ -375,7 +370,7 @@ data_t read_type_from_darr(darr_t *darr, data_type_t type)
|
|||||||
|
|
||||||
inst_t inst_read_bytecode(darr_t *darr)
|
inst_t inst_read_bytecode(darr_t *darr)
|
||||||
{
|
{
|
||||||
static_assert(NUMBER_OF_OPCODES == 96, "inst_read_bytecode: Out of date");
|
static_assert(NUMBER_OF_OPCODES == 95, "inst_read_bytecode: Out of date");
|
||||||
if (darr->used >= darr->available)
|
if (darr->used >= darr->available)
|
||||||
return (inst_t){0};
|
return (inst_t){0};
|
||||||
inst_t inst = {0};
|
inst_t inst = {0};
|
||||||
@@ -387,8 +382,7 @@ inst_t inst_read_bytecode(darr_t *darr)
|
|||||||
inst.operand = read_type_from_darr(darr, (data_type_t)opcode);
|
inst.operand = read_type_from_darr(darr, (data_type_t)opcode);
|
||||||
// Read register (as a byte)
|
// Read register (as a byte)
|
||||||
else if (OPCODE_IS_TYPE(opcode, OP_PUSH_REGISTER) ||
|
else if (OPCODE_IS_TYPE(opcode, OP_PUSH_REGISTER) ||
|
||||||
OPCODE_IS_TYPE(opcode, OP_MOV) || opcode == OP_JUMP_REGISTER ||
|
OPCODE_IS_TYPE(opcode, OP_MOV) || OPCODE_IS_TYPE(opcode, OP_DUP) ||
|
||||||
OPCODE_IS_TYPE(opcode, OP_DUP) ||
|
|
||||||
OPCODE_IS_TYPE(opcode, OP_MALLOC) ||
|
OPCODE_IS_TYPE(opcode, OP_MALLOC) ||
|
||||||
OPCODE_IS_TYPE(opcode, OP_MSET) || OPCODE_IS_TYPE(opcode, OP_MGET) ||
|
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))
|
||||||
|
|||||||
@@ -145,7 +145,6 @@ typedef enum
|
|||||||
// Program control flow
|
// Program control flow
|
||||||
OP_JUMP_ABS,
|
OP_JUMP_ABS,
|
||||||
OP_JUMP_STACK,
|
OP_JUMP_STACK,
|
||||||
OP_JUMP_REGISTER,
|
|
||||||
OP_JUMP_IF_BYTE,
|
OP_JUMP_IF_BYTE,
|
||||||
OP_JUMP_IF_HWORD,
|
OP_JUMP_IF_HWORD,
|
||||||
OP_JUMP_IF_WORD,
|
OP_JUMP_IF_WORD,
|
||||||
@@ -230,8 +229,6 @@ inst_t *insts_read_bytecode_file(FILE *, size_t *);
|
|||||||
#define INST_JUMP_ABS(OP) \
|
#define INST_JUMP_ABS(OP) \
|
||||||
((inst_t){.opcode = OP_JUMP_ABS, .operand = DWORD(OP)})
|
((inst_t){.opcode = OP_JUMP_ABS, .operand = DWORD(OP)})
|
||||||
#define INST_JUMP_STACK ((inst_t){.opcode = OP_JUMP_STACK})
|
#define INST_JUMP_STACK ((inst_t){.opcode = OP_JUMP_STACK})
|
||||||
#define INST_JUMP_REGISTER(OP) \
|
|
||||||
((inst_t){.opcode = OP_JUMP_REGISTER, .operand = DWORD(OP)})
|
|
||||||
#define INST_JUMP_IF(TYPE, OP) \
|
#define INST_JUMP_IF(TYPE, OP) \
|
||||||
((inst_t){.opcode = OP_JUMP_IF_##TYPE, .operand = DWORD(OP)})
|
((inst_t){.opcode = OP_JUMP_IF_##TYPE, .operand = DWORD(OP)})
|
||||||
|
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ const char *err_as_cstr(err_t err)
|
|||||||
|
|
||||||
err_t vm_execute(vm_t *vm)
|
err_t vm_execute(vm_t *vm)
|
||||||
{
|
{
|
||||||
static_assert(NUMBER_OF_OPCODES == 96, "vm_execute: Out of date");
|
static_assert(NUMBER_OF_OPCODES == 95, "vm_execute: Out of date");
|
||||||
struct Program *prog = &vm->program;
|
struct Program *prog = &vm->program;
|
||||||
if (prog->ptr >= prog->max)
|
if (prog->ptr >= prog->max)
|
||||||
return ERR_END_OF_PROGRAM;
|
return ERR_END_OF_PROGRAM;
|
||||||
@@ -133,13 +133,6 @@ err_t vm_execute(vm_t *vm)
|
|||||||
return err;
|
return err;
|
||||||
return vm_jump(vm, ret.as_word);
|
return vm_jump(vm, ret.as_word);
|
||||||
}
|
}
|
||||||
else if (instruction.opcode == OP_JUMP_REGISTER)
|
|
||||||
{
|
|
||||||
if (instruction.operand.as_word >= vm->registers.available)
|
|
||||||
return ERR_INVALID_REGISTER_WORD;
|
|
||||||
word addr = vm->registers.data[instruction.operand.as_word];
|
|
||||||
return vm_jump(vm, addr);
|
|
||||||
}
|
|
||||||
else if (OPCODE_IS_TYPE(instruction.opcode, OP_JUMP_IF))
|
else if (OPCODE_IS_TYPE(instruction.opcode, OP_JUMP_IF))
|
||||||
{
|
{
|
||||||
data_t datum = {0};
|
data_t datum = {0};
|
||||||
|
|||||||
Reference in New Issue
Block a user