Skip to content

Commit

Permalink
Merge branch 'main' into kate/rx
Browse files Browse the repository at this point in the history
  • Loading branch information
katef committed Aug 16, 2024
2 parents e6950b8 + 0a36f1b commit a435074
Show file tree
Hide file tree
Showing 17 changed files with 179 additions and 63 deletions.
4 changes: 2 additions & 2 deletions include/fsm/print.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ struct fsm_hooks {

int (*comment)(FILE *, const struct fsm_options *opt,
const fsm_end_id_t *ids, size_t count,
void *lang_opaque, void *hook_opaque);
void *hook_opaque);

/* only called for AMBIG_ERROR; see opt.ambig */
int (*conflict)(FILE *, const struct fsm_options *opt,
Expand All @@ -94,7 +94,7 @@ struct fsm_hooks {
* but simply not yet implemented, where fsm_print() will print a message
* to stderr and exit.
*
* The code generation for the typical case of matching input require the FSM
* The code generation for the typical case of matching input requires the FSM
* to be a DFA, and will EINVAL if the FSM is not a DFA. As opposed to e.g.
* FSM_PRINT_API, which generates code for other purposes, and does not place
* particular expecations on the FSM.
Expand Down
29 changes: 19 additions & 10 deletions src/libfsm/print.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ print_hook_accept(FILE *f,
void *lang_opaque, void *hook_opaque),
void *lang_opaque)
{
int r;

assert(f != NULL);
assert(opt != NULL);
assert(hooks != NULL);
Expand All @@ -74,25 +72,36 @@ print_hook_accept(FILE *f,
}

if (hooks->accept != NULL) {
r = hooks->accept(f, opt, ids, count,
return hooks->accept(f, opt, ids, count,
lang_opaque, hooks->hook_opaque);
} else if (default_accept != NULL) {
r = default_accept(f, opt, ids, count,
return default_accept(f, opt, ids, count,
lang_opaque, hooks->hook_opaque);
} else {
r = 0;
}

if (r != 0) {
return r;
return 0;
}

int
print_hook_comment(FILE *f,
const struct fsm_options *opt,
const struct fsm_hooks *hooks,
const fsm_end_id_t *ids, size_t count)
{
assert(f != NULL);
assert(opt != NULL);
assert(hooks != NULL);

if (opt->ambig == AMBIG_ERROR) {
assert(count <= 1);
}

if (opt->comments && hooks->comment != NULL) {
/* this space is a polyglot */
fprintf(f, " ");

r = hooks->comment(f, opt, ids, count,
lang_opaque, hooks->hook_opaque);
return hooks->comment(f, opt, ids, count,
hooks->hook_opaque);
}

return 0;
Expand Down
6 changes: 6 additions & 0 deletions src/libfsm/print.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ print_hook_accept(FILE *f,
void *lang_opaque, void *hook_opaque),
void *lang_opaque);

int
print_hook_comment(FILE *f,
const struct fsm_options *opt,
const struct fsm_hooks *hooks,
const fsm_end_id_t *ids, size_t count);

int
print_hook_reject(FILE *f,
const struct fsm_options *opt,
Expand Down
15 changes: 13 additions & 2 deletions src/libfsm/print/awk.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,21 @@ print_end(FILE *f, const struct dfavm_op_ir *op,
return print_hook_reject(f, opt, hooks, default_reject, NULL);

case VM_END_SUCC:
return print_hook_accept(f, opt, hooks,
if (-1 == print_hook_accept(f, opt, hooks,
op->ret->ids, op->ret->count,
default_accept,
NULL);
NULL))
{
return -1;
}

if (-1 == print_hook_comment(f, opt, hooks,
op->ret->ids, op->ret->count))
{
return -1;
}

return 0;

default:
assert(!"unreached");
Expand Down
28 changes: 18 additions & 10 deletions src/libfsm/print/c.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,17 @@ print_ids(FILE *f,
return -1;
}

fprintf(f, "return %u;", ids[0]);
break;
/* fallthrough */

case AMBIG_EARLIEST:
/*
* The libfsm api guarentees these ids are unique,
* and only appear once each, and are sorted.
*/
fprintf(f, "return %u;", ids[0]);
fprintf(f, "{\n");
fprintf(f, "\t\t*id = %u;\n", ids[0]);
fprintf(f, "\t\treturn 1;\n");
fprintf(f, "\t}");
break;

case AMBIG_MULTIPLE:
Expand All @@ -101,7 +103,7 @@ print_ids(FILE *f,
fprintf(f, " };\n");
fprintf(f, "\t\t*ids = a;\n");
fprintf(f, "\t\t*count = %zu;\n", count);
fprintf(f, "\t\treturn 0;\n");
fprintf(f, "\t\treturn 1;\n");
fprintf(f, "\t}");
break;

Expand Down Expand Up @@ -380,6 +382,12 @@ print_endstates(FILE *f,
return -1;
}

if (-1 == print_hook_comment(f, opt, hooks,
ir->states[i].endids.ids, ir->states[i].endids.count))
{
return -1;
}

fprintf(f, "\n");
}

Expand Down Expand Up @@ -555,13 +563,13 @@ fsm_print_c(FILE *f,

case AMBIG_ERROR:
case AMBIG_EARLIEST:
fprintf(stdout, ",\n");
fprintf(stdout, "\tconst unsigned *id");
fprintf(f, ",\n");
fprintf(f, "\tconst unsigned *id");
break;

case AMBIG_MULTIPLE:
fprintf(stdout, ",\n");
fprintf(stdout, "\tconst unsigned **ids, size_t *count");
fprintf(f, ",\n");
fprintf(f, "\tconst unsigned **ids, size_t *count");
break;

default:
Expand All @@ -570,8 +578,8 @@ fsm_print_c(FILE *f,
}

if (hooks->args != NULL) {
fprintf(stdout, ",\n");
fprintf(stdout, "\t");
fprintf(f, ",\n");
fprintf(f, "\t");

if (-1 == print_hook_args(f, opt, hooks, NULL, NULL)) {
return -1;
Expand Down
10 changes: 10 additions & 0 deletions src/libfsm/print/dot.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,16 @@ print_dotfrag(FILE *f,
return -1;
}

if (opt->comments && hooks->comment != NULL) {
fprintf(f, ",");

if (-1 == print_hook_comment(f, opt, hooks,
ids, count))
{
return -1;
}
}

fprintf(f, " ];\n");

f_free(fsm->alloc, ids);
Expand Down
8 changes: 7 additions & 1 deletion src/libfsm/print/fsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,13 @@ fsm_print_fsm(FILE *f,
if (-1 == print_hook_accept(f, opt, hooks,
ids, count,
default_accept,
NULL))
NULL))
{
return -1;
}

if (-1 == print_hook_comment(f, opt, hooks,
ids, count))
{
return -1;
}
Expand Down
10 changes: 8 additions & 2 deletions src/libfsm/print/go.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,12 @@ print_end(FILE *f, const struct dfavm_op_ir *op,
return -1;
}

if (-1 == print_hook_comment(f, opt, hooks,
op->ret->ids, op->ret->count))
{
return -1;
}

fprintf(f, "\n\t}\n");

return 0;
Expand Down Expand Up @@ -413,7 +419,7 @@ fsm_print_go(FILE *f,
}

if (hooks->args != NULL) {
fprintf(stdout, ", ");
fprintf(f, ", ");

if (-1 == print_hook_args(f, opt, hooks, NULL, NULL)) {
return -1;
Expand All @@ -429,7 +435,7 @@ fsm_print_go(FILE *f,

case AMBIG_ERROR:
case AMBIG_EARLIEST:
fprintf(stdout, "(bool, uint)");
fprintf(f, "(bool, uint)");
break;

case AMBIG_MULTIPLE:
Expand Down
2 changes: 0 additions & 2 deletions src/libfsm/print/irdot.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,6 @@ print_state(FILE *f,
fprintf(f, "</TD></TR>\n");
}

/* TODO: leaf callback for dot output */

/* showing hook in addition to existing content */
if (cs->isend && hooks->accept != NULL) {
fprintf(f, "\t\t <TR><TD COLSPAN='3' ALIGN='LEFT'>");
Expand Down
18 changes: 17 additions & 1 deletion src/libfsm/print/llvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,13 @@ fsm_print_llvm(FILE *f,
{
return -1;
}

if (-1 == print_hook_comment(f, opt, hooks,
retlist->a[i].ids, retlist->a[i].count))
{
return -1;
}

fprintf(f, "\n");
}
}
Expand All @@ -678,15 +685,24 @@ fsm_print_llvm(FILE *f,
if (opt->ambig == AMBIG_MULTIPLE) {
fprintf(f, "%%rt { %s bitcast ([%zu x i32]* @%sr%zu to %s), i64 %zu }",
ptr_i32, retlist->a[i].count, prefix, i, ptr_i32, retlist->a[i].count);
fprintf(f, ",");
} else {
if (-1 == print_hook_accept(f, opt, hooks,
retlist->a[i].ids, retlist->a[i].count,
default_accept, NULL))
{
return -1;
}

fprintf(f, ",");

if (-1 == print_hook_comment(f, opt, hooks,
retlist->a[i].ids, retlist->a[i].count))
{
return -1;
}
}
fprintf(f, ",\n");
fprintf(f, "\n");
}
fprintf(f, "\t ");
if (-1 == print_hook_reject(f, opt, hooks, default_reject, NULL)) {
Expand Down
8 changes: 8 additions & 0 deletions src/libfsm/print/rust.c
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,14 @@ fsm_print_rustfrag(FILE *f,
fprintf(f, " }");
}

if (op->u.stop.end_bits == VM_END_SUCC) {
if (-1 == print_hook_comment(f, opt, hooks,
op->ret->ids, op->ret->count))
{
return -1;
}
}

if (op->cmp == VM_CMP_ALWAYS) {
/* the code for fallthrough would be unreachable */
fallthrough = false;
Expand Down
15 changes: 13 additions & 2 deletions src/libfsm/print/sh.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,21 @@ print_end(FILE *f, const struct dfavm_op_ir *op,
return print_hook_reject(f, opt, hooks, default_reject, NULL);

case VM_END_SUCC:
return print_hook_accept(f, opt, hooks,
if (-1 == print_hook_accept(f, opt, hooks,
op->ret->ids, op->ret->count,
default_accept,
NULL);
NULL))
{
return -1;
}

if (-1 == print_hook_comment(f, opt, hooks,
op->ret->ids, op->ret->count))
{
return -1;
}

return 0;

default:
assert(!"unreached");
Expand Down
5 changes: 5 additions & 0 deletions src/libfsm/print/vmasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ print_end(FILE *f, const struct dfavm_op_ir *op,
{
return -1;
}
if (-1 == print_hook_comment(f, opt, hooks,
op->ret->ids, op->ret->count))
{
return -1;
}
break;

default:
Expand Down
Loading

0 comments on commit a435074

Please sign in to comment.