From 1295005ed710f5a631fc40c4c214bc7417f7effe Mon Sep 17 00:00:00 2001 From: Scott Vokes Date: Tue, 24 Sep 2024 10:49:10 -0400 Subject: [PATCH] ir: Wrap ids and count in a struct, to namespace them as endids. The IR struct is about to get another id & count pair. This loses storing the count as a 31-bit bitfield, but if the goal for that is saving memory then the ids array allocation could be replaced with a struct that contains the count, and then each IR without endids will save more space than the current approach. --- src/libfsm/print.c | 2 +- src/libfsm/print/c.c | 4 ++-- src/libfsm/print/ir.c | 10 +++++----- src/libfsm/print/ir.h | 6 ++++-- src/libfsm/print/irdot.c | 12 ++++++------ src/libfsm/print/irjson.c | 12 ++++++------ src/libfsm/vm/ir.c | 2 +- src/libfsm/vm/retlist.c | 2 +- 8 files changed, 26 insertions(+), 24 deletions(-) diff --git a/src/libfsm/print.c b/src/libfsm/print.c index c555c5e48..e1a425cc0 100644 --- a/src/libfsm/print.c +++ b/src/libfsm/print.c @@ -358,7 +358,7 @@ fsm_print(FILE *f, const struct fsm *fsm, continue; } - assert(ir->states[i].count <= 1); + assert(ir->states[i].endids.count <= 1); } } diff --git a/src/libfsm/print/c.c b/src/libfsm/print/c.c index 1eefaf231..22b03963e 100644 --- a/src/libfsm/print/c.c +++ b/src/libfsm/print/c.c @@ -375,8 +375,8 @@ print_endstates(FILE *f, fprintf(f, "\tcase S%u: ", i); const struct fsm_state_metadata state_metadata = { - .end_ids = ir->states[i].ids, - .end_id_count = ir->states[i].count, + .end_ids = ir->states[i].endids.ids, + .end_id_count = ir->states[i].endids.count, }; if (-1 == print_hook_accept(f, opt, hooks, diff --git a/src/libfsm/print/ir.c b/src/libfsm/print/ir.c index b3831b164..457716dcc 100644 --- a/src/libfsm/print/ir.c +++ b/src/libfsm/print/ir.c @@ -541,8 +541,8 @@ make_ir(const struct fsm *fsm, const struct fsm_options *opt) assert(i < ir->n); ir->states[i].isend = fsm_isend(fsm, i); - ir->states[i].ids = NULL; - ir->states[i].count = 0; + ir->states[i].endids.ids = NULL; + ir->states[i].endids.count = 0; if (fsm_isend(fsm, i)) { fsm_end_id_t *ids; @@ -563,8 +563,8 @@ make_ir(const struct fsm *fsm, const struct fsm_options *opt) assert(res == 1); } - ir->states[i].ids = ids; - ir->states[i].count = count; + ir->states[i].endids.ids = ids; + ir->states[i].endids.count = count; } if (make_state(fsm, i, &ir->states[i]) == -1) { @@ -629,7 +629,7 @@ free_ir(const struct fsm *fsm, struct ir *ir) for (i = 0; i < ir->n; i++) { f_free(fsm->alloc, (void *) ir->states[i].example); - f_free(fsm->alloc, (void *) ir->states[i].ids); + f_free(fsm->alloc, (void *) ir->states[i].endids.ids); switch (ir->states[i].strategy) { case IR_TABLE: diff --git a/src/libfsm/print/ir.h b/src/libfsm/print/ir.h index bbfd5006e..b375ba850 100644 --- a/src/libfsm/print/ir.h +++ b/src/libfsm/print/ir.h @@ -54,8 +54,10 @@ struct ir_error { struct ir_state { const char *example; - fsm_end_id_t *ids; /* NULL -> 0 */ - size_t count:31; // :31 for packing + struct ir_state_endids { + fsm_end_id_t *ids; /* NULL -> 0 */ + size_t count; + } endids; unsigned int isend:1; diff --git a/src/libfsm/print/irdot.c b/src/libfsm/print/irdot.c index f8e62bc3b..37dc34296 100644 --- a/src/libfsm/print/irdot.c +++ b/src/libfsm/print/irdot.c @@ -195,13 +195,13 @@ print_state(FILE *f, fprintf(f, "\t\t S%u%s\n", ir_indexof(ir, cs), strategy_name(cs->strategy)); - if (cs->isend && cs->count > 0) { + if (cs->isend && cs->endids.count > 0) { fprintf(f, "\t\t end id"); - for (size_t i = 0; i < cs->count; i++) { - fprintf(f, "#%u", cs->ids[i]); + for (size_t i = 0; i < cs->endids.count; i++) { + fprintf(f, "#%u", cs->endids.ids[i]); - if (i < (size_t) cs->count - 1) { + if (i < (size_t) cs->endids.count - 1) { fprintf(f, " "); } } @@ -220,8 +220,8 @@ print_state(FILE *f, fprintf(f, "\t\t "); const struct fsm_state_metadata state_metadata = { - .end_ids = cs->ids, - .end_id_count = cs->count, + .end_ids = cs->endids.ids, + .end_id_count = cs->endids.count, }; if (-1 == print_hook_accept(f, opt, hooks, diff --git a/src/libfsm/print/irjson.c b/src/libfsm/print/irjson.c index 89c4e52b3..a96250e0f 100644 --- a/src/libfsm/print/irjson.c +++ b/src/libfsm/print/irjson.c @@ -127,12 +127,12 @@ print_state(FILE *f, fprintf(f, "\t\t{\n"); fprintf(f, "\t\t\t\"end\": %s,\n", cs->isend ? "true" : "false"); - if (cs->isend && cs->count > 0) { + if (cs->isend && cs->endids.count > 0) { fprintf(f, "\t\t\t\"end_id\": ["); - for (size_t i = 0; i < cs->count; i++) { - fprintf(f, "%u", cs->ids[i]); + for (size_t i = 0; i < cs->endids.count; i++) { + fprintf(f, "%u", cs->endids.ids[i]); - if (i < (size_t) cs->count - 1) { + if (i < (size_t) cs->endids.count - 1) { fprintf(f, ", "); } } @@ -140,8 +140,8 @@ print_state(FILE *f, } const struct fsm_state_metadata state_metadata = { - .end_ids = cs->ids, - .end_id_count = cs->count, + .end_ids = cs->endids.ids, + .end_id_count = cs->endids.count, }; /* showing hook in addition to existing content */ diff --git a/src/libfsm/vm/ir.c b/src/libfsm/vm/ir.c index a32bca463..4930f4bc1 100644 --- a/src/libfsm/vm/ir.c +++ b/src/libfsm/vm/ir.c @@ -309,7 +309,7 @@ opasm_new(struct dfavm_assembler_ir *a, const struct ret_list *retlist, if (ir_state != NULL) { op->example = ir_state->example; op->ret = ir_state->isend - ? find_ret(retlist, ir_state->ids, ir_state->count) + ? find_ret(retlist, ir_state->endids.ids, ir_state->endids.count) : NULL; } diff --git a/src/libfsm/vm/retlist.c b/src/libfsm/vm/retlist.c index 031d1d101..97d4eb3f0 100644 --- a/src/libfsm/vm/retlist.c +++ b/src/libfsm/vm/retlist.c @@ -105,7 +105,7 @@ build_retlist(struct ret_list *list, const struct ir *ir) continue; } - if (!append_ret(list, ir->states[i].ids, ir->states[i].count)) { + if (!append_ret(list, ir->states[i].endids.ids, ir->states[i].endids.count)) { return false; } }