From b20ad511a0a375da9dd92542e58a4a2cd0721d9f Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Sun, 22 Oct 2023 20:26:17 +0100 Subject: 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. --- src/inst.c | 18 ++++++++++++++++++ src/inst.h | 7 +++++++ 2 files changed, 25 insertions(+) (limited to 'src') diff --git a/src/inst.c b/src/inst.c index fc02b66..8a5ce2c 100644 --- a/src/inst.c +++ b/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; diff --git a/src/inst.h b/src/inst.h index 1073593..16374ee 100644 --- a/src/inst.h +++ b/src/inst.h @@ -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}) -- cgit v1.2.3-13-gbd6f