From 73bbc9af2ac5c42549a6a932d435d5ea055c330d Mon Sep 17 00:00:00 2001 From: Dmitry Selyutin Date: Fri, 8 Sep 2023 09:47:31 +0300 Subject: [PATCH] libsvp64: rename into libopid --- src/libopid/.gitignore | 3 ++ src/{libsvp64 => libopid}/Makefile | 20 ++++---- src/{libsvp64 => libopid}/codegen.py | 72 ++++++++++++++-------------- src/libopid/opid.h | 61 +++++++++++++++++++++++ src/libsvp64/.gitignore | 3 -- src/libsvp64/svp64.h | 61 ----------------------- 6 files changed, 110 insertions(+), 110 deletions(-) create mode 100644 src/libopid/.gitignore rename src/{libsvp64 => libopid}/Makefile (57%) rename src/{libsvp64 => libopid}/codegen.py (85%) create mode 100644 src/libopid/opid.h delete mode 100644 src/libsvp64/.gitignore delete mode 100644 src/libsvp64/svp64.h diff --git a/src/libopid/.gitignore b/src/libopid/.gitignore new file mode 100644 index 00000000..d72be3e8 --- /dev/null +++ b/src/libopid/.gitignore @@ -0,0 +1,3 @@ +*-gen.* +*.a +*.so diff --git a/src/libsvp64/Makefile b/src/libopid/Makefile similarity index 57% rename from src/libsvp64/Makefile rename to src/libopid/Makefile index 8121b9ff..46bfbf19 100644 --- a/src/libsvp64/Makefile +++ b/src/libopid/Makefile @@ -5,14 +5,14 @@ PYTHON?=python3 GENS:=\ - svp64-dis-gen.c \ - svp64-opc-gen.c \ + opid-dis-gen.c \ + opid-opc-gen.c \ SRCS:=$(GENS) OBJS:=\ - svp64-dis-gen.o \ - svp64-opc-gen.o \ + opid-dis-gen.o \ + opid-opc-gen.o \ .SUFFIX: .so .a .o .c @@ -23,22 +23,22 @@ all: build .PHONY: build -build: libsvp64.so +build: libopid.so .PHONY: clean clean: rm -f $(GENS) rm -f $(OBJS) - rm -f libsvp64.so - rm -f libsvp64.a + rm -f libopid.so + rm -f libopid.a -libsvp64.so: libsvp64.a - $(CC) -fPIC -shared -o $@ -L. -Wl,--whole-archive libsvp64.a -Wl,--no-whole-archive +libopid.so: libopid.a + $(CC) -fPIC -shared -o $@ -L. -Wl,--whole-archive libopid.a -Wl,--no-whole-archive -libsvp64.a: $(OBJS) +libopid.a: $(OBJS) $(AR) rcs $@ $^ diff --git a/src/libsvp64/codegen.py b/src/libopid/codegen.py similarity index 85% rename from src/libsvp64/codegen.py rename to src/libopid/codegen.py index 7577d507..1d0f11d7 100644 --- a/src/libsvp64/codegen.py +++ b/src/libopid/codegen.py @@ -37,8 +37,8 @@ def fetch(span): class Mode(enum.Enum): - PPC_DIS_GEN_C = "svp64-dis-gen.c" - PPC_OPC_GEN_C = "svp64-opc-gen.c" + PPC_DIS_GEN_C = "opid-dis-gen.c" + PPC_OPC_GEN_C = "opid-opc-gen.c" def __call__(self, db, **arguments): def pairwise(iterable): @@ -230,8 +230,8 @@ class DisGenSource(Source): @mdis.dispatcher.Hook(DynamicOperands) @contextlib.contextmanager def dispatch_operands(self, node): - self.emit("static inline enum svp64_state") - self.emit("svp64_disassemble_operand(struct svp64_ctx *ctx, uint32_t insn, size_t id) {") + self.emit("static inline enum opid_state") + self.emit("opid_disassemble_operand(struct opid_ctx *ctx, uint32_t insn, size_t id) {") with self: self.emit("uint32_t value;") self.emit("uint32_t flags;") @@ -240,31 +240,31 @@ class DisGenSource(Source): yield node self.emit("default:") with self: - self.emit("return (enum svp64_state)((size_t)SVP64_ERROR_OPERAND_0 + id);") + self.emit("return (enum opid_state)((size_t)OPID_ERROR_OPERAND_0 + id);") self.emit("}") self.emit("") with self: self.emit("ctx->operands[id].value = value;") self.emit("ctx->operands[id].flags = flags;") self.emit("") - self.emit("return SVP64_SUCCESS;") + self.emit("return OPID_SUCCESS;") self.emit("}") self.emit("") - self.emit("static inline enum svp64_state") - self.emit("svp64_disassemble_operands(struct svp64_ctx *ctx, uint32_t insn) {") + self.emit("static inline enum opid_state") + self.emit("opid_disassemble_operands(struct opid_ctx *ctx, uint32_t insn) {") with self: - self.emit(f"for (size_t id = 0; ((id != SVP64_OPERANDS) && ctx->record->operands[id]); ++id) {{") + self.emit(f"for (size_t id = 0; ((id != OPID_OPERANDS) && ctx->record->operands[id]); ++id) {{") with self: - self.emit("enum svp64_state state;") + self.emit("enum opid_state state;") self.emit("") - self.emit("state = svp64_disassemble_operand(ctx, insn, id);") - self.emit("if (state != SVP64_SUCCESS)") + self.emit("state = opid_disassemble_operand(ctx, insn, id);") + self.emit("if (state != OPID_SUCCESS)") with self: self.emit("return state;") self.emit("}") self.emit("") - self.emit("return SVP64_SUCCESS;") + self.emit("return OPID_SUCCESS;") self.emit("}") @mdis.dispatcher.Hook(DynamicOperand) @@ -279,9 +279,9 @@ class DisGenSource(Source): self.emit("break;") def nonzero_handler(span): - yield from generic_handler(span, "SVP64_OPERAND_NONZERO") + yield from generic_handler(span, "OPID_OPERAND_NONZERO") - def signed_handler(span, flags="SVP64_OPERAND_SIGNED"): + def signed_handler(span, flags="OPID_OPERAND_SIGNED"): mask = f"(UINT32_C(1) << (UINT32_C({len(span)}) - 1))" yield "value = (" with self: @@ -301,25 +301,25 @@ class DisGenSource(Source): self.emit("break;") def address_handler(span): - yield from signed_handler(span, "(SVP64_OPERAND_ADDRESS | SVP64_OPERAND_SIGNED)") + yield from signed_handler(span, "(OPID_OPERAND_ADDRESS | OPID_OPERAND_SIGNED)") def gpr_handler(span, pair=False): if not pair: - yield from generic_handler(span, "SVP64_OPERAND_GPR") + yield from generic_handler(span, "OPID_OPERAND_GPR") else: - yield from generic_handler(span, "(SVP64_OPERAND_GPR | SVP64_OPERAND_PAIR)") + yield from generic_handler(span, "(OPID_OPERAND_GPR | OPID_OPERAND_PAIR)") def fpr_handler(span, pair=False): if not pair: - yield from generic_handler(span, "SVP64_OPERAND_FPR") + yield from generic_handler(span, "OPID_OPERAND_FPR") else: - yield from generic_handler(span, "(SVP64_OPERAND_FPR | SVP64_OPERAND_PAIR)") + yield from generic_handler(span, "(OPID_OPERAND_FPR | OPID_OPERAND_PAIR)") def cr3_handler(span): - yield from generic_handler(span, "SVP64_OPERAND_CR3") + yield from generic_handler(span, "OPID_OPERAND_CR3") def cr5_handler(span): - yield from generic_handler(span, "SVP64_OPERAND_CR5") + yield from generic_handler(span, "OPID_OPERAND_CR5") handlers = { insndb.GPRPairOperand: lambda span: gpr_handler(span, True), @@ -358,19 +358,19 @@ class DisGenSource(Source): self.emit("#include ") self.emit("#include ") self.emit("") - self.emit("#include \"svp64.h\"") + self.emit("#include \"opid.h\"") self.emit("") yield node - self.emit("enum svp64_state") - self.emit("svp64_disassemble(struct svp64_ctx *ctx, uint32_t insn) {") + self.emit("enum opid_state") + self.emit("opid_disassemble(struct opid_ctx *ctx, uint32_t insn) {") with self: - self.emit("ctx->record = svp64_lookup_insn(insn);") + self.emit("ctx->record = opid_lookup_insn(insn);") self.emit("") self.emit("if (ctx->record == NULL)") with self: - self.emit("return SVP64_ERROR_LOOKUP;") + self.emit("return OPID_ERROR_LOOKUP;") self.emit("") - self.emit("return svp64_disassemble_operands(ctx, insn);") + self.emit("return opid_disassemble_operands(ctx, insn);") self.emit("}") @@ -442,7 +442,7 @@ class OpcGenSource(Source): heads[index] = (counter - node[index]) tails[index] = counter heads = [(tail - node[index]) for (index, tail) in enumerate(tails)] - self.emit("static uint16_t const svp64_opcode_hash[64][2] = {") + self.emit("static uint16_t const opid_opcode_hash[64][2] = {") with self: for index in range(64): head = heads[index] @@ -455,7 +455,7 @@ class OpcGenSource(Source): @mdis.dispatcher.Hook(Records) @contextlib.contextmanager def dispatch_records(self, node): - self.emit("static struct svp64_record const svp64_records[] = {") + self.emit("static struct opid_record const opid_records[] = {") with self: yield node self.emit("};") self.emit("") @@ -471,23 +471,23 @@ class OpcGenSource(Source): self.emit("#include ") self.emit("#include ") self.emit("") - self.emit("#include \"svp64.h\"") + self.emit("#include \"opid.h\"") self.emit("") yield node - self.emit("struct svp64_record const *") - self.emit("svp64_lookup_insn(uint32_t insn) {") + self.emit("struct opid_record const *") + self.emit("opid_lookup_insn(uint32_t insn) {") with self: self.emit("uint32_t PO = (") with self: for line in fetch(range(6)): self.emit(line) self.emit(");") - self.emit("struct svp64_record const *iter = &svp64_records[svp64_opcode_hash[PO][0]];") - self.emit("struct svp64_record const *tail = &svp64_records[svp64_opcode_hash[PO][1]];") + self.emit("struct opid_record const *iter = &opid_records[opid_opcode_hash[PO][0]];") + self.emit("struct opid_record const *tail = &opid_records[opid_opcode_hash[PO][1]];") self.emit("") self.emit("for (; iter != tail; ++iter) {") with self: - self.emit("struct svp64_opcode const *opcode = &iter->opcode;") + self.emit("struct opid_opcode const *opcode = &iter->opcode;") self.emit("") self.emit("if ((opcode->value & opcode->mask) == (insn & opcode->mask))") with self: diff --git a/src/libopid/opid.h b/src/libopid/opid.h new file mode 100644 index 00000000..f0d01f88 --- /dev/null +++ b/src/libopid/opid.h @@ -0,0 +1,61 @@ +#pragma once + +#include +#include + +enum opid_state { + OPID_SUCCESS, + OPID_ERROR_LOOKUP, + OPID_ERROR_OPERAND_0, + OPID_ERROR_OPERAND_1, + OPID_ERROR_OPERAND_2, + OPID_ERROR_OPERAND_3, + OPID_ERROR_OPERAND_4, + OPID_ERROR_OPERAND_5, + OPID_ERROR_OPERAND_6, + OPID_ERROR_OPERAND_7, +}; + +#define OPID_OPERANDS 8 + +struct opid_opcode { + uint32_t value; + uint32_t mask; +}; + +struct opid_record { + struct opid_opcode opcode; + uint8_t operands[OPID_OPERANDS]; + char name[16]; +}; + +struct opid_operand { + uint32_t value; + uint32_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)) + +struct opid_ctx { + struct opid_record const *record; + struct opid_operand operands[OPID_OPERANDS]; +}; + +#define opid_foreach_operand(ctx, operand) \ + for (size_t id = 0; \ + (((operand = &(ctx)->operands[id]), 1) && \ + ((id != OPID_OPERANDS) && (ctx)->record->operands[id])); \ + operand = &(ctx)->operands[++id]) + +enum opid_state +opid_disassemble(struct opid_ctx *ctx, uint32_t insn); + +struct opid_record const * +opid_lookup_insn(uint32_t insn); diff --git a/src/libsvp64/.gitignore b/src/libsvp64/.gitignore deleted file mode 100644 index 42376305..00000000 --- a/src/libsvp64/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -svp64-*-gen.* -*.a -*.so diff --git a/src/libsvp64/svp64.h b/src/libsvp64/svp64.h deleted file mode 100644 index 43e9717d..00000000 --- a/src/libsvp64/svp64.h +++ /dev/null @@ -1,61 +0,0 @@ -#pragma once - -#include -#include - -enum svp64_state { - SVP64_SUCCESS, - SVP64_ERROR_LOOKUP, - SVP64_ERROR_OPERAND_0, - SVP64_ERROR_OPERAND_1, - SVP64_ERROR_OPERAND_2, - SVP64_ERROR_OPERAND_3, - SVP64_ERROR_OPERAND_4, - SVP64_ERROR_OPERAND_5, - SVP64_ERROR_OPERAND_6, - SVP64_ERROR_OPERAND_7, -}; - -#define SVP64_OPERANDS 8 - -struct svp64_opcode { - uint32_t value; - uint32_t mask; -}; - -struct svp64_record { - struct svp64_opcode opcode; - uint8_t operands[SVP64_OPERANDS]; - char name[16]; -}; - -struct svp64_operand { - uint32_t value; - uint32_t flags; -}; - -#define SVP64_OPERAND_SIGNED (UINT32_C(1) << UINT32_C(0)) -#define SVP64_OPERAND_GPR (UINT32_C(1) << UINT32_C(1)) -#define SVP64_OPERAND_FPR (UINT32_C(1) << UINT32_C(2)) -#define SVP64_OPERAND_PAIR (UINT32_C(1) << UINT32_C(3)) -#define SVP64_OPERAND_CR3 (UINT32_C(1) << UINT32_C(4)) -#define SVP64_OPERAND_CR5 (UINT32_C(1) << UINT32_C(5)) -#define SVP64_OPERAND_NONZERO (UINT32_C(1) << UINT32_C(6)) -#define SVP64_OPERAND_ADDRESS (UINT32_C(1) << UINT32_C(7)) - -struct svp64_ctx { - struct svp64_record const *record; - struct svp64_operand operands[SVP64_OPERANDS]; -}; - -#define svp64_foreach_operand(ctx, operand) \ - for (size_t id = 0; \ - (((operand = &(ctx)->operands[id]), 1) && \ - ((id != SVP64_OPERANDS) && (ctx)->record->operands[id])); \ - operand = &(ctx)->operands[++id]) - -enum svp64_state -svp64_disassemble(struct svp64_ctx *ctx, uint32_t insn); - -struct svp64_record const * -svp64_lookup_insn(uint32_t insn); -- 2.30.2