Added OP_PLUS_*
Takes two operands from the stack then pushes their sum.
This commit is contained in:
17
src/inst.c
17
src/inst.c
@@ -114,6 +114,15 @@ const char *opcode_as_cstr(opcode_t code)
|
|||||||
case OP_EQ_WORD:
|
case OP_EQ_WORD:
|
||||||
return "EQ_WORD";
|
return "EQ_WORD";
|
||||||
break;
|
break;
|
||||||
|
case OP_PLUS_BYTE:
|
||||||
|
return "PLUS_BYTE";
|
||||||
|
break;
|
||||||
|
case OP_PLUS_HWORD:
|
||||||
|
return "PLUS_HWORD";
|
||||||
|
break;
|
||||||
|
case OP_PLUS_WORD:
|
||||||
|
return "PLUS_WORD";
|
||||||
|
break;
|
||||||
case OP_JUMP_ABS:
|
case OP_JUMP_ABS:
|
||||||
return "JUMP_ABS";
|
return "JUMP_ABS";
|
||||||
break;
|
break;
|
||||||
@@ -191,7 +200,7 @@ data_type_t get_opcode_data_type(opcode_t opcode)
|
|||||||
|
|
||||||
void inst_print(inst_t instruction, FILE *fp)
|
void inst_print(inst_t instruction, FILE *fp)
|
||||||
{
|
{
|
||||||
static_assert(NUMBER_OF_OPCODES == 34, "inst_bytecode_size: Out of date");
|
static_assert(NUMBER_OF_OPCODES == 37, "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))
|
||||||
{
|
{
|
||||||
@@ -220,7 +229,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 == 34, "inst_bytecode_size: Out of date");
|
static_assert(NUMBER_OF_OPCODES == 37, "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))
|
||||||
{
|
{
|
||||||
@@ -245,7 +254,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 == 34, "inst_write_bytecode: Out of date");
|
static_assert(NUMBER_OF_OPCODES == 37, "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
|
||||||
@@ -318,7 +327,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 == 34, "inst_read_bytecode: Out of date");
|
static_assert(NUMBER_OF_OPCODES == 37, "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};
|
||||||
|
|||||||
14
src/inst.h
14
src/inst.h
@@ -28,14 +28,14 @@ typedef enum
|
|||||||
OP_PUSH_HWORD,
|
OP_PUSH_HWORD,
|
||||||
OP_PUSH_WORD,
|
OP_PUSH_WORD,
|
||||||
|
|
||||||
OP_PUSH_REGISTER_BYTE,
|
|
||||||
OP_PUSH_REGISTER_HWORD,
|
|
||||||
OP_PUSH_REGISTER_WORD,
|
|
||||||
|
|
||||||
OP_POP_BYTE,
|
OP_POP_BYTE,
|
||||||
OP_POP_HWORD,
|
OP_POP_HWORD,
|
||||||
OP_POP_WORD,
|
OP_POP_WORD,
|
||||||
|
|
||||||
|
OP_PUSH_REGISTER_BYTE,
|
||||||
|
OP_PUSH_REGISTER_HWORD,
|
||||||
|
OP_PUSH_REGISTER_WORD,
|
||||||
|
|
||||||
OP_MOV_BYTE,
|
OP_MOV_BYTE,
|
||||||
OP_MOV_HWORD,
|
OP_MOV_HWORD,
|
||||||
OP_MOV_WORD,
|
OP_MOV_WORD,
|
||||||
@@ -66,6 +66,10 @@ typedef enum
|
|||||||
OP_EQ_WORD,
|
OP_EQ_WORD,
|
||||||
|
|
||||||
// Mathematical operations
|
// Mathematical operations
|
||||||
|
OP_PLUS_BYTE,
|
||||||
|
OP_PLUS_HWORD,
|
||||||
|
OP_PLUS_WORD,
|
||||||
|
|
||||||
// Program control flow
|
// Program control flow
|
||||||
OP_JUMP_ABS,
|
OP_JUMP_ABS,
|
||||||
OP_JUMP_STACK,
|
OP_JUMP_STACK,
|
||||||
@@ -129,6 +133,8 @@ inst_t *insts_read_bytecode_file(FILE *, size_t *);
|
|||||||
#define INST_XOR(TYPE) ((inst_t){.opcode = OP_XOR_##TYPE})
|
#define INST_XOR(TYPE) ((inst_t){.opcode = OP_XOR_##TYPE})
|
||||||
#define INST_EQ(TYPE) ((inst_t){.opcode = OP_EQ_##TYPE})
|
#define INST_EQ(TYPE) ((inst_t){.opcode = OP_EQ_##TYPE})
|
||||||
|
|
||||||
|
#define INST_PLUS(TYPE) ((inst_t){.opcode = OP_PLUS_##TYPE})
|
||||||
|
|
||||||
#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})
|
||||||
|
|||||||
Reference in New Issue
Block a user