libopid: expand operands and opcodes
authorDmitry Selyutin <ghostmansd@gmail.com>
Mon, 11 Sep 2023 18:44:36 +0000 (21:44 +0300)
committerDmitry Selyutin <ghostmansd@gmail.com>
Wed, 13 Sep 2023 17:26:19 +0000 (20:26 +0300)
src/libopid/codegen.py
src/libopid/opid-check.c
src/libopid/opid-dis.c
src/libopid/opid-opc.c
src/libopid/opid.h

index 7f30bb52a8817ac19dba38ce2096681cf08bf370..ce9ce78498ae5ba6b2092b12e5a06c975e36600f 100644 (file)
@@ -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)
index de04e125d34ecf179e3c63563b0bb09ae32b1b49..fe51ca11e9ec1d92f2895e025e4a4b7498188728 100644 (file)
         (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;
index 81f1d9eab71cdbe784ee17da411ec0194ea36595..945dcd0d4f7bb01888a4043b7cae841a8a3b1515 100644 (file)
@@ -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)
index 0d2d6c2a31acd9ff69df77c8238e4bc902ac9247..85179781b807ad0704952719c9ddc65c84e55cc4 100644 (file)
@@ -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]];
index f0d01f88c98b81c515be387adcbe4c3742da86ec..ff32fb2f25aa3ba19de5dc72c887de49f50304c5 100644 (file)
@@ -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);