Introduced a new mathematical operator MULT
Thankfully multiplication, like addition, is the same under 2s complement as it is for unsigned numbers. So I just need to implement those versions to be fine.
This commit is contained in:
16
lib/inst.c
16
lib/inst.c
@@ -194,6 +194,14 @@ const char *opcode_as_cstr(opcode_t code)
|
|||||||
break;
|
break;
|
||||||
case OP_PLUS_WORD:
|
case OP_PLUS_WORD:
|
||||||
return "PLUS_WORD";
|
return "PLUS_WORD";
|
||||||
|
case OP_MULT_BYTE:
|
||||||
|
return "MULT_BYTE";
|
||||||
|
break;
|
||||||
|
case OP_MULT_HWORD:
|
||||||
|
return "MULT_HWORD";
|
||||||
|
break;
|
||||||
|
case OP_MULT_WORD:
|
||||||
|
return "MULT_WORD";
|
||||||
break;
|
break;
|
||||||
case OP_JUMP_ABS:
|
case OP_JUMP_ABS:
|
||||||
return "JUMP_ABS";
|
return "JUMP_ABS";
|
||||||
@@ -261,7 +269,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 == 70, "inst_bytecode_size: Out of date");
|
static_assert(NUMBER_OF_OPCODES == 73, "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))
|
||||||
{
|
{
|
||||||
@@ -291,7 +299,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 == 70, "inst_bytecode_size: Out of date");
|
static_assert(NUMBER_OF_OPCODES == 73, "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))
|
||||||
{
|
{
|
||||||
@@ -315,7 +323,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 == 70, "inst_write_bytecode: Out of date");
|
static_assert(NUMBER_OF_OPCODES == 73, "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
|
||||||
@@ -388,7 +396,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 == 70, "inst_read_bytecode: Out of date");
|
static_assert(NUMBER_OF_OPCODES == 73, "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};
|
||||||
|
|||||||
@@ -98,6 +98,10 @@ typedef enum
|
|||||||
OP_PLUS_HWORD,
|
OP_PLUS_HWORD,
|
||||||
OP_PLUS_WORD,
|
OP_PLUS_WORD,
|
||||||
|
|
||||||
|
OP_MULT_BYTE,
|
||||||
|
OP_MULT_HWORD,
|
||||||
|
OP_MULT_WORD,
|
||||||
|
|
||||||
// Simple I/O
|
// Simple I/O
|
||||||
OP_PRINT_BYTE,
|
OP_PRINT_BYTE,
|
||||||
OP_PRINT_CHAR,
|
OP_PRINT_CHAR,
|
||||||
@@ -176,6 +180,7 @@ inst_t *insts_read_bytecode_file(FILE *, size_t *);
|
|||||||
#define INST_GT(TYPE) ((inst_t){.opcode = OP_GT_##TYPE})
|
#define INST_GT(TYPE) ((inst_t){.opcode = OP_GT_##TYPE})
|
||||||
#define INST_GTE(TYPE) ((inst_t){.opcode = OP_GTE_##TYPE})
|
#define INST_GTE(TYPE) ((inst_t){.opcode = OP_GTE_##TYPE})
|
||||||
#define INST_PLUS(TYPE) ((inst_t){.opcode = OP_PLUS_##TYPE})
|
#define INST_PLUS(TYPE) ((inst_t){.opcode = OP_PLUS_##TYPE})
|
||||||
|
#define INST_MULT(TYPE) ((inst_t){.opcode = OP_MULT_##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)})
|
||||||
|
|||||||
Reference in New Issue
Block a user