Added opcode OP_DUP_*
Duplicates the nth datum off the stack, pushing it to the top. Useful for operations such as MOV which eat the stack.
This commit is contained in:
18
src/inst.c
18
src/inst.c
@@ -59,6 +59,15 @@ const char *opcode_as_cstr(opcode_t code)
|
||||
case OP_MOV_HWORD:
|
||||
return "MOV_HWORD";
|
||||
break;
|
||||
case OP_DUP_BYTE:
|
||||
return "DUP_BYTE";
|
||||
break;
|
||||
case OP_DUP_HWORD:
|
||||
return "DUP_HWORD";
|
||||
break;
|
||||
case OP_DUP_WORD:
|
||||
return "DUP_WORD";
|
||||
break;
|
||||
case OP_NOT_BYTE:
|
||||
return "NOT_BYTE";
|
||||
break;
|
||||
@@ -182,6 +191,11 @@ void inst_print(inst_t instruction, FILE *fp)
|
||||
fprintf(fp, "reg=0x");
|
||||
data_print(instruction.operand, DATA_TYPE_BYTE, fp);
|
||||
}
|
||||
else if (OPCODE_IS_TYPE(instruction.opcode, OP_DUP))
|
||||
{
|
||||
fprintf(fp, "n=");
|
||||
data_print(instruction.operand, DATA_TYPE_WORD, fp);
|
||||
}
|
||||
fprintf(fp, ")");
|
||||
}
|
||||
|
||||
@@ -218,6 +232,8 @@ void inst_write_bytecode(inst_t inst, darr_t *darr)
|
||||
else if (OPCODE_IS_TYPE(inst.opcode, OP_PUSH_REGISTER) ||
|
||||
OPCODE_IS_TYPE(inst.opcode, OP_MOV))
|
||||
to_append = DATA_TYPE_BYTE;
|
||||
else if (OPCODE_IS_TYPE(inst.opcode, OP_DUP))
|
||||
to_append = DATA_TYPE_WORD;
|
||||
|
||||
switch (to_append)
|
||||
{
|
||||
@@ -292,6 +308,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.operand = read_type_from_darr(darr, DATA_TYPE_BYTE);
|
||||
else if (OPCODE_IS_TYPE(opcode, OP_DUP))
|
||||
inst.operand = read_type_from_darr(darr, DATA_TYPE_WORD);
|
||||
// Otherwise opcode doesn't take operands
|
||||
|
||||
inst.opcode = opcode;
|
||||
|
||||
@@ -40,6 +40,10 @@ typedef enum
|
||||
OP_MOV_HWORD,
|
||||
OP_MOV_WORD,
|
||||
|
||||
OP_DUP_BYTE,
|
||||
OP_DUP_HWORD,
|
||||
OP_DUP_WORD,
|
||||
|
||||
// Boolean operations
|
||||
OP_NOT_BYTE,
|
||||
OP_NOT_HWORD,
|
||||
@@ -108,6 +112,9 @@ inst_t *insts_read_bytecode_file(FILE *, size_t *);
|
||||
#define INST_PUSH_REG(TYPE, REG) \
|
||||
((inst_t){.opcode = OP_PUSH_REGISTER_##TYPE, .operand = D##TYPE(REG)})
|
||||
|
||||
#define INST_DUP(TYPE, OP) \
|
||||
((inst_t){.opcode = OP_DUP_##TYPE, .operand = DWORD(OP)})
|
||||
|
||||
#define INST_NOT(TYPE) ((inst_t){.opcode = OP_NOT_##TYPE})
|
||||
#define INST_OR(TYPE) ((inst_t){.opcode = OP_OR_##TYPE})
|
||||
#define INST_AND(TYPE) ((inst_t){.opcode = OP_AND_##TYPE})
|
||||
|
||||
Reference in New Issue
Block a user