From 027e8446a638b467f61d418e9efba92161c7e9c8 Mon Sep 17 00:00:00 2001 From: Dmitry Selyutin Date: Mon, 11 Sep 2023 21:56:15 +0300 Subject: [PATCH] libopid: refactor disassembly API --- src/libopid/opid-check.c | 2 +- src/libopid/opid-dis.c | 26 +++++++++++++++++++------- src/libopid/opid.h | 5 +++-- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/libopid/opid-check.c b/src/libopid/opid-check.c index fe51ca11..6640bde4 100644 --- a/src/libopid/opid-check.c +++ b/src/libopid/opid-check.c @@ -34,7 +34,7 @@ main(void) { return EXIT_FAILURE; } - printf("%s [", ctx.record->name); + printf("%s [", ctx.name); opid_foreach_operand(&ctx, operand) { printf("(%" PRIi32 " ", (int32_t)operand->value); diff --git a/src/libopid/opid-dis.c b/src/libopid/opid-dis.c index 945dcd0d..37eae875 100644 --- a/src/libopid/opid-dis.c +++ b/src/libopid/opid-dis.c @@ -11,18 +11,22 @@ opid_disassemble_operand(uint64_t insn, #include "opid-dis-gen.c" static inline enum opid_state -opid_disassemble_operands(struct opid_ctx *ctx, uint64_t insn) { - for (size_t id = 0; ((id != OPID_OPERANDS) && ctx->record->operands[id]); ++id) { +opid_disassemble_operands(struct opid_ctx *ctx, + struct opid_record const *record, + uint64_t insn) { + for (size_t id = 0; ((id != OPID_OPERANDS) && record->operands[id]); ++id) { enum opid_state state; state = opid_disassemble_operand(insn, - ctx->record->operands[id], + record->operands[id], &ctx->operands[id]); if (state != OPID_SUCCESS) { if (state == OPID_ERROR_OPERAND_0) state = (enum opid_state)((size_t)state + id); return state; } + + ctx->nr_operands = (id + 1); } return OPID_SUCCESS; @@ -30,10 +34,18 @@ opid_disassemble_operands(struct opid_ctx *ctx, uint64_t insn) { enum opid_state opid_disassemble(struct opid_ctx *ctx, uint64_t insn) { - ctx->record = opid_lookup_insn(insn); - - if (ctx->record == NULL) + enum opid_state state; + struct opid_record const *record; + + record = opid_lookup_insn(insn); + if (record == NULL) return OPID_ERROR_LOOKUP; - return opid_disassemble_operands(ctx, insn); + state = opid_disassemble_operands(ctx, record, insn); + if (state != OPID_SUCCESS) + return state; + + ctx->name = record->name; + + return OPID_SUCCESS; } diff --git a/src/libopid/opid.h b/src/libopid/opid.h index ff32fb2f..a6a94597 100644 --- a/src/libopid/opid.h +++ b/src/libopid/opid.h @@ -44,14 +44,15 @@ struct opid_operand { #define OPID_OPERAND_ADDRESS (UINT64_C(1) << UINT64_C(7)) struct opid_ctx { - struct opid_record const *record; + char const *name; + size_t nr_operands; 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])); \ + ((id != OPID_OPERANDS) && (id != (ctx)->nr_operands))); \ operand = &(ctx)->operands[++id]) enum opid_state -- 2.30.2