Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

print API: Box end_ids and end_id_count in a struct for callbacks. #497

Merged
merged 2 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 */
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This loses the :31 for packing, but if the goal is to save memory per IR state, then this could instead be a separately allocated struct like struct ir_state_endids { size_t count; fsm_end_ids_t ids[]; } and states without endids (which will be most states) save storing the count entirely. Thoughts?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need, the packing isn't that important.

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