Skip to content

Commit

Permalink
ir: Wrap ids and count in a struct, to namespace them as endids.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
silentbicycle committed Sep 24, 2024
1 parent 4758734 commit 1295005
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/libfsm/print.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/libfsm/print/c.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 5 additions & 5 deletions src/libfsm/print/ir.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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:
Expand Down
6 changes: 4 additions & 2 deletions src/libfsm/print/ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
12 changes: 6 additions & 6 deletions src/libfsm/print/irdot.c
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,13 @@ print_state(FILE *f,
fprintf(f, "\t\t <TR><TD COLSPAN='2' ALIGN='LEFT'>S%u</TD><TD ALIGN='LEFT'>%s</TD></TR>\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 <TR><TD COLSPAN='2' ALIGN='LEFT'>end id</TD><TD ALIGN='LEFT'>");

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, " ");
}
}
Expand All @@ -220,8 +220,8 @@ print_state(FILE *f,
fprintf(f, "\t\t <TR><TD COLSPAN='3' ALIGN='LEFT'>");

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,
Expand Down
12 changes: 6 additions & 6 deletions src/libfsm/print/irjson.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,21 +127,21 @@ 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, ", ");
}
}
fprintf(f, "],\n");
}

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 */
Expand Down
2 changes: 1 addition & 1 deletion src/libfsm/vm/ir.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/libfsm/vm/retlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down

0 comments on commit 1295005

Please sign in to comment.