From ef53c672a2d168329b1a73884018be6f1e556136 Mon Sep 17 00:00:00 2001 From: Dmitry Selyutin Date: Mon, 11 Sep 2023 21:44:36 +0300 Subject: [PATCH] libopid: expand operands and opcodes --- src/libopid/codegen.py | 18 +++++++++--------- src/libopid/opid-check.c | 9 --------- src/libopid/opid-dis.c | 6 +++--- src/libopid/opid-opc.c | 16 ++++++++-------- src/libopid/opid.h | 28 ++++++++++++++-------------- 5 files changed, 34 insertions(+), 43 deletions(-) diff --git a/src/libopid/codegen.py b/src/libopid/codegen.py index 7f30bb52..ce9ce784 100644 --- a/src/libopid/codegen.py +++ b/src/libopid/codegen.py @@ -26,14 +26,14 @@ def traverse(root, visitor, walker, **kwargs): def fetch(span): bits = len(span) - one = "UINT32_C(1)" + one = "UINT64_C(1)" for (dst, origin) in enumerate(span): src = (32 - (origin + 1)) dst = (bits - (dst + 1)) - dst = f"UINT32_C({dst})" - src = f"UINT32_C({src})" + dst = f"UINT64_C({dst})" + src = f"UINT64_C({src})" yield f"/* {origin:<2} */ (((insn >> {src}) & {one}) << {dst}) |" - yield f"UINT32_C(0)" + yield f"UINT64_C(0)" class Mode(enum.Enum): @@ -246,7 +246,7 @@ class DisGenSource(Source): @contextlib.contextmanager def dispatch_operands(self, node): self.emit("static inline enum opid_state") - self.emit("opid_disassemble_operand(uint32_t insn,") + self.emit("opid_disassemble_operand(uint64_t insn,") with self: with self: self.emit("size_t category,") @@ -266,7 +266,7 @@ class DisGenSource(Source): @mdis.dispatcher.Hook(DynamicOperand) @contextlib.contextmanager def dispatch_operand(self, node, *, path, pathcls): - def generic_handler(span, flags="UINT32_C(0)"): + def generic_handler(span, flags="UINT64_C(0)"): yield f"operand->value = (" with self: yield from fetch(span) @@ -275,7 +275,7 @@ class DisGenSource(Source): self.emit("break;") def nonzero_handler(span): - yield f"operand->value = (UINT32_C(1) + (" + yield f"operand->value = (UINT64_C(1) + (" with self: yield from fetch(span) yield f"));" @@ -283,7 +283,7 @@ class DisGenSource(Source): self.emit("break;") def signed_handler(span, flags="OPID_OPERAND_SIGNED"): - mask = f"(UINT32_C(1) << (UINT32_C({len(span)}) - 1))" + mask = f"(UINT64_C(1) << (UINT64_C({len(span)}) - 1))" yield "operand->value = (" with self: yield "(" @@ -397,7 +397,7 @@ class OpcGenSource(Source): @mdis.dispatcher.Hook(insndb.Record.Opcode.Value, insndb.Record.Opcode.Mask) @contextlib.contextmanager def dispatch_opcode_parts(self, node, *, path, pathcls): - self.emit(f"{pathcls(path)} = UINT32_C(0x{node:016x}),") + self.emit(f"{pathcls(path)} = UINT64_C(0x{node:016x}),") with self: yield node @mdis.dispatcher.Hook(insndb.Record.Opcode) diff --git a/src/libopid/opid-check.c b/src/libopid/opid-check.c index de04e125..fe51ca11 100644 --- a/src/libopid/opid-check.c +++ b/src/libopid/opid-check.c @@ -14,15 +14,6 @@ (void)fprintf(stderr, FORMAT, ##__VA_ARGS__); \ } while (0) -#define OPID_OPERAND_SIGNED (UINT32_C(1) << UINT32_C(0)) -#define OPID_OPERAND_GPR (UINT32_C(1) << UINT32_C(1)) -#define OPID_OPERAND_FPR (UINT32_C(1) << UINT32_C(2)) -#define OPID_OPERAND_PAIR (UINT32_C(1) << UINT32_C(3)) -#define OPID_OPERAND_CR3 (UINT32_C(1) << UINT32_C(4)) -#define OPID_OPERAND_CR5 (UINT32_C(1) << UINT32_C(5)) -#define OPID_OPERAND_NONZERO (UINT32_C(1) << UINT32_C(6)) -#define OPID_OPERAND_ADDRESS (UINT32_C(1) << UINT32_C(7)) - int main(void) { ssize_t size; diff --git a/src/libopid/opid-dis.c b/src/libopid/opid-dis.c index 81f1d9ea..945dcd0d 100644 --- a/src/libopid/opid-dis.c +++ b/src/libopid/opid-dis.c @@ -4,14 +4,14 @@ #include "opid.h" static inline enum opid_state -opid_disassemble_operand(uint32_t insn, +opid_disassemble_operand(uint64_t insn, size_t category, struct opid_operand *operand); #include "opid-dis-gen.c" static inline enum opid_state -opid_disassemble_operands(struct opid_ctx *ctx, uint32_t insn) { +opid_disassemble_operands(struct opid_ctx *ctx, uint64_t insn) { for (size_t id = 0; ((id != OPID_OPERANDS) && ctx->record->operands[id]); ++id) { enum opid_state state; @@ -29,7 +29,7 @@ opid_disassemble_operands(struct opid_ctx *ctx, uint32_t insn) { } enum opid_state -opid_disassemble(struct opid_ctx *ctx, uint32_t insn) { +opid_disassemble(struct opid_ctx *ctx, uint64_t insn) { ctx->record = opid_lookup_insn(insn); if (ctx->record == NULL) diff --git a/src/libopid/opid-opc.c b/src/libopid/opid-opc.c index 0d2d6c2a..85179781 100644 --- a/src/libopid/opid-opc.c +++ b/src/libopid/opid-opc.c @@ -9,15 +9,15 @@ static uint16_t const opid_opcode_table[64][2]; #include "opid-opc-gen.c" struct opid_record const * -opid_lookup_insn(uint32_t insn) { +opid_lookup_insn(uint64_t insn) { uint32_t PO = ( - /* 0 */ (((insn >> UINT32_C(31)) & UINT32_C(1)) << UINT32_C(5)) | - /* 1 */ (((insn >> UINT32_C(30)) & UINT32_C(1)) << UINT32_C(4)) | - /* 2 */ (((insn >> UINT32_C(29)) & UINT32_C(1)) << UINT32_C(3)) | - /* 3 */ (((insn >> UINT32_C(28)) & UINT32_C(1)) << UINT32_C(2)) | - /* 4 */ (((insn >> UINT32_C(27)) & UINT32_C(1)) << UINT32_C(1)) | - /* 5 */ (((insn >> UINT32_C(26)) & UINT32_C(1)) << UINT32_C(0)) | - UINT32_C(0) + /* 0 */ (((insn >> UINT64_C(31)) & UINT64_C(1)) << UINT64_C(5)) | + /* 1 */ (((insn >> UINT64_C(30)) & UINT64_C(1)) << UINT64_C(4)) | + /* 2 */ (((insn >> UINT64_C(29)) & UINT64_C(1)) << UINT64_C(3)) | + /* 3 */ (((insn >> UINT64_C(28)) & UINT64_C(1)) << UINT64_C(2)) | + /* 4 */ (((insn >> UINT64_C(27)) & UINT64_C(1)) << UINT64_C(1)) | + /* 5 */ (((insn >> UINT64_C(26)) & UINT64_C(1)) << UINT64_C(0)) | + UINT64_C(0) ); struct opid_record const *iter = &opid_record_table[opid_opcode_table[PO][0]]; struct opid_record const *tail = &opid_record_table[opid_opcode_table[PO][1]]; diff --git a/src/libopid/opid.h b/src/libopid/opid.h index f0d01f88..ff32fb2f 100644 --- a/src/libopid/opid.h +++ b/src/libopid/opid.h @@ -19,8 +19,8 @@ enum opid_state { #define OPID_OPERANDS 8 struct opid_opcode { - uint32_t value; - uint32_t mask; + uint64_t value; + uint64_t mask; }; struct opid_record { @@ -30,18 +30,18 @@ struct opid_record { }; struct opid_operand { - uint32_t value; - uint32_t flags; + uint64_t value; + uint64_t flags; }; -#define OPID_OPERAND_SIGNED (UINT32_C(1) << UINT32_C(0)) -#define OPID_OPERAND_GPR (UINT32_C(1) << UINT32_C(1)) -#define OPID_OPERAND_FPR (UINT32_C(1) << UINT32_C(2)) -#define OPID_OPERAND_PAIR (UINT32_C(1) << UINT32_C(3)) -#define OPID_OPERAND_CR3 (UINT32_C(1) << UINT32_C(4)) -#define OPID_OPERAND_CR5 (UINT32_C(1) << UINT32_C(5)) -#define OPID_OPERAND_NONZERO (UINT32_C(1) << UINT32_C(6)) -#define OPID_OPERAND_ADDRESS (UINT32_C(1) << UINT32_C(7)) +#define OPID_OPERAND_SIGNED (UINT64_C(1) << UINT64_C(0)) +#define OPID_OPERAND_GPR (UINT64_C(1) << UINT64_C(1)) +#define OPID_OPERAND_FPR (UINT64_C(1) << UINT64_C(2)) +#define OPID_OPERAND_PAIR (UINT64_C(1) << UINT64_C(3)) +#define OPID_OPERAND_CR3 (UINT64_C(1) << UINT64_C(4)) +#define OPID_OPERAND_CR5 (UINT64_C(1) << UINT64_C(5)) +#define OPID_OPERAND_NONZERO (UINT64_C(1) << UINT64_C(6)) +#define OPID_OPERAND_ADDRESS (UINT64_C(1) << UINT64_C(7)) struct opid_ctx { struct opid_record const *record; @@ -55,7 +55,7 @@ struct opid_ctx { operand = &(ctx)->operands[++id]) enum opid_state -opid_disassemble(struct opid_ctx *ctx, uint32_t insn); +opid_disassemble(struct opid_ctx *ctx, uint64_t insn); struct opid_record const * -opid_lookup_insn(uint32_t insn); +opid_lookup_insn(uint64_t insn); -- 2.30.2