Skip to content

Commit

Permalink
lx: Switch from opaques API to endid API for zone tracking.
Browse files Browse the repository at this point in the history
Also, add some debug logging.
  • Loading branch information
silentbicycle committed Mar 1, 2021
1 parent d78d0e5 commit f5e5a1f
Show file tree
Hide file tree
Showing 16 changed files with 196 additions and 42 deletions.
3 changes: 2 additions & 1 deletion include/fsm/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ struct fsm_options {
const char *cp;

/* TODO: explain. for C code fragment output */
int (*leaf)(FILE *, const void *state_opaque, const void *leaf_opaque);
int (*leaf)(FILE *, const struct fsm_end_ids *ids,
const void *leaf_opaque);
void *leaf_opaque;

/* TODO: explain. for C code fragment output */
Expand Down
6 changes: 3 additions & 3 deletions src/libfsm/print/awk.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@
#define START UINT32_MAX

static int
leaf(FILE *f, const void *state_opaque, const void *leaf_opaque)
leaf(FILE *f, const struct fsm_end_ids *ids, const void *leaf_opaque)
{
assert(f != NULL);
assert(leaf_opaque == NULL);

(void) state_opaque;
(void) leaf_opaque;
(void) ids;

/* XXX: this should be FSM_UNKNOWN or something non-EOF,
* maybe user defined */
Expand Down Expand Up @@ -120,7 +120,7 @@ print_branch(FILE *f, const struct dfavm_op_ir *op, const char *prefix)
static int
fsm_print_awkfrag(FILE *f, const struct ir *ir, const struct fsm_options *opt,
const char *cp, const char *prefix,
int (*leaf)(FILE *, const void *state_opaque, const void *leaf_opaque),
int (*leaf)(FILE *, const struct fsm_end_ids *ids, const void *leaf_opaque),
const void *leaf_opaque)
{
static const struct dfavm_assembler_ir zero;
Expand Down
8 changes: 4 additions & 4 deletions src/libfsm/print/c.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ ir_hasend(const struct ir *ir)
}

static int
leaf(FILE *f, const void *state_opaque, const void *leaf_opaque)
leaf(FILE *f, const struct fsm_end_ids *ids, const void *leaf_opaque)
{
assert(f != NULL);
assert(leaf_opaque == NULL);

(void) state_opaque;
(void) ids;
(void) leaf_opaque;

/* XXX: this should be FSM_UNKNOWN or something non-EOF,
Expand Down Expand Up @@ -140,7 +140,7 @@ static void
print_singlecase(FILE *f, const struct ir *ir, const struct fsm_options *opt,
const char *cp,
struct ir_state *cs,
int (*leaf)(FILE *, const void *state_opaque, const void *leaf_opaque),
int (*leaf)(FILE *, const struct fsm_end_ids *ids, const void *leaf_opaque),
const void *leaf_opaque)
{
assert(ir != NULL);
Expand Down Expand Up @@ -298,7 +298,7 @@ endstates(FILE *f, const struct fsm_options *opt, const struct ir *ir)
int
fsm_print_cfrag(FILE *f, const struct ir *ir, const struct fsm_options *opt,
const char *cp,
int (*leaf)(FILE *, const void *state_opaque, const void *leaf_opaque),
int (*leaf)(FILE *, const struct fsm_end_ids *ids, const void *leaf_opaque),
const void *leaf_opaque)
{
unsigned i;
Expand Down
6 changes: 3 additions & 3 deletions src/libfsm/print/go.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
#include "ir.h"

static int
leaf(FILE *f, const void *state_opaque, const void *leaf_opaque)
leaf(FILE *f, const struct fsm_end_ids *ids, const void *leaf_opaque)
{
assert(f != NULL);
assert(leaf_opaque == NULL);

(void) state_opaque;
(void) ids;
(void) leaf_opaque;

/* XXX: this should be FSM_UNKNOWN or something non-EOF,
Expand Down Expand Up @@ -126,7 +126,7 @@ print_fetch(FILE *f, const struct fsm_options *opt)
static int
fsm_print_gofrag(FILE *f, const struct ir *ir, const struct fsm_options *opt,
const char *cp,
int (*leaf)(FILE *, const void *state_opaque, const void *leaf_opaque),
int (*leaf)(FILE *, const struct fsm_end_ids *ids, const void *leaf_opaque),
const void *leaf_opaque)
{
static const struct dfavm_assembler_ir zero;
Expand Down
2 changes: 1 addition & 1 deletion src/libfsm/print/ir.c
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ make_ir(const struct fsm *fsm)
size_t written;
const size_t count = fsm_getendidcount(fsm, i);
struct fsm_end_ids *ids = f_malloc(fsm->opt->alloc,
sizeof(*ids) + ((count - 1) * sizeof(ids->ids)));
sizeof(*ids) + ((count - 1) * sizeof(ids->ids[0])));
if (ids == NULL) {
goto error;
}
Expand Down
20 changes: 19 additions & 1 deletion src/libfsm/print/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,16 +244,34 @@ fsm_print_json(FILE *f, const struct fsm *fsm)
fprintf(f, " \"endleaf\": [ ");
for (i = 0; i < fsm->statecount; i++) {
if (fsm_isend(fsm, i)) {
enum fsm_getendids_res res;
size_t written;
const size_t count = fsm_getendidcount(fsm, i);
struct fsm_end_ids *ids = f_malloc(fsm->opt->alloc,
sizeof(*ids) + ((count - 1) * sizeof(ids->ids[0])));
assert(ids != NULL);

res = fsm_getendids(fsm, i, count,
ids->ids, &written);
if (res == FSM_GETENDIDS_FOUND) {
ids->count = (unsigned)written;
} else {
assert(res == FSM_GETENDIDS_NOT_FOUND);
ids->count = 0;
}

if (notfirst) {
fprintf(f, ", ");
}

fprintf(f, "{ %u, ", i);

fsm->opt->endleaf(f, &fsm->states[i], fsm->opt->endleaf_opaque);
fsm->opt->endleaf(f, ids, fsm->opt->endleaf_opaque);

fprintf(f, " }");

f_free(fsm->opt->alloc, ids);

notfirst = 1;
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/libfsm/print/rust.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@
#define START UINT32_MAX

static int
leaf(FILE *f, const void *state_opaque, const void *leaf_opaque)
leaf(FILE *f, const struct fsm_end_ids *ids, const void *leaf_opaque)
{
assert(f != NULL);
assert(leaf_opaque == NULL);

(void) state_opaque;
(void) ids;
(void) leaf_opaque;

/* XXX: this should be FSM_UNKNOWN or something non-EOF,
Expand Down Expand Up @@ -126,7 +126,7 @@ print_fetch(FILE *f)
static int
fsm_print_rustfrag(FILE *f, const struct ir *ir, const struct fsm_options *opt,
const char *cp,
int (*leaf)(FILE *, const void *state_opaque, const void *leaf_opaque),
int (*leaf)(FILE *, const struct fsm_end_ids *ids, const void *leaf_opaque),
const void *leaf_opaque)
{
static const struct dfavm_assembler_ir zero;
Expand Down
6 changes: 3 additions & 3 deletions src/libfsm/print/vmc.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@
#include "ir.h"

static int
leaf(FILE *f, const void *state_opaque, const void *leaf_opaque)
leaf(FILE *f, const struct fsm_end_ids *ids, const void *leaf_opaque)
{
assert(f != NULL);
assert(leaf_opaque == NULL);

(void) state_opaque;
(void) ids;
(void) leaf_opaque;

/* XXX: this should be FSM_UNKNOWN or something non-EOF,
Expand Down Expand Up @@ -137,7 +137,7 @@ print_fetch(FILE *f, const struct fsm_options *opt)
static int
fsm_print_cfrag(FILE *f, const struct ir *ir, const struct fsm_options *opt,
const char *cp,
int (*leaf)(FILE *, const void *state_opaque, const void *leaf_opaque),
int (*leaf)(FILE *, const struct fsm_end_ids *ids, const void *leaf_opaque),
const void *leaf_opaque)
{
static const struct dfavm_assembler_ir zero;
Expand Down
59 changes: 59 additions & 0 deletions src/lx/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,62 @@ ast_addconflict(struct mapping_set **head, struct ast_mapping *m)
return new;
}

#define MAX_MAPPINGS 64 /* FIXME */
static fsm_end_id_t mapping_count = 0;
static struct ast_mapping *mappings[MAX_MAPPINGS];

int
ast_setendmapping(struct fsm *fsm, struct ast_mapping *m)
{
const fsm_end_id_t id = mapping_count;

if (fsm_setendid(fsm, id)) {
if (LOG()) {
fprintf(stderr, "ast_setendmapping: saving mapping %p at mappings[%d]\n",
(void *)m, id);
}
mappings[id] = m;
mapping_count++;
return 1;
}
return 0;
}

struct ast_mapping *
ast_getendmappingbyendid(fsm_end_id_t id)
{
assert(id < mapping_count);
return mappings[id];
}

struct ast_mapping *
ast_getendmapping(const struct fsm *fsm, fsm_state_t s)
{
#define ID_BUF_COUNT 8
fsm_end_id_t id_buf[ID_BUF_COUNT];
enum fsm_getendids_res res;
size_t written;
struct ast_mapping *m;

res = fsm_getendids(fsm,
s, ID_BUF_COUNT, id_buf, &written);
if (res == FSM_GETENDIDS_NOT_FOUND) {
return NULL;
}

if (res == FSM_GETENDIDS_ERROR_INSUFFICIENT_SPACE) {
assert(!"FIXME: capacity");
}

assert(res == FSM_GETENDIDS_FOUND);
assert(written < ID_BUF_COUNT);
assert(written > 0);

m = ast_getendmappingbyendid(id_buf[0]);

if (LOG()) {
fprintf(stderr, "ast_getendmapping: got mapping %p mappings[%d]\n",
(void *)m, id_buf[0]);
}
return m;
}
12 changes: 12 additions & 0 deletions src/lx/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
struct fsm;
struct var;

#include <unistd.h>
#define LOG() getenv("LOG")

struct ast_mapping {
struct fsm *fsm;
struct ast_token *token;
Expand Down Expand Up @@ -62,5 +65,14 @@ ast_addmapping(struct ast_zone *z, struct fsm *fsm,
struct mapping_set *
ast_addconflict(struct mapping_set **head, struct ast_mapping *n);

int
ast_setendmapping(struct fsm *fsm, struct ast_mapping *m);

struct ast_mapping *
ast_getendmappingbyendid(fsm_end_id_t id);

struct ast_mapping *
ast_getendmapping(const struct fsm *fsm, fsm_state_t s);

#endif

43 changes: 34 additions & 9 deletions src/lx/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,12 @@ lang_exclude(const char *name)
exit(EXIT_FAILURE);
}

#if 0
static void
carryopaque(const struct fsm *src_fsm, const fsm_state_t *src_set, size_t n,
struct fsm *dst_fsm, fsm_state_t dst_state)
{

struct mapping_set *conflict;
struct ast_mapping *m;
size_t i;
Expand Down Expand Up @@ -367,6 +369,7 @@ carryopaque(const struct fsm *src_fsm, const fsm_state_t *src_set, size_t n,

return;
}
#endif

static int
zone_equal(const struct ast_zone *a, const struct ast_zone *b)
Expand Down Expand Up @@ -410,14 +413,14 @@ zone_equal(const struct ast_zone *a, const struct ast_zone *b)
}

{
opt.carryopaque = carryopaque;
/* opt.carryopaque = carryopaque; */

if (!fsm_determinise(q)) {
fsm_free(q);
return -1;
}

opt.carryopaque = NULL;
/* opt.carryopaque = NULL; */
}

{
Expand All @@ -429,7 +432,11 @@ zone_equal(const struct ast_zone *a, const struct ast_zone *b)
continue;
}

m = fsm_getopaque(q, i);
m = ast_getendmapping(q, i);

if (LOG()) {
fprintf(stderr, "zone_equal: asserting ast_getendmapping(q, state %d) != NULL: %p\n", i, (void *)m);
}
assert(m != NULL);

if (m->conflict != NULL) {
Expand Down Expand Up @@ -509,7 +516,15 @@ zone_minimise(void *arg)
}

/* Attach this mapping to each end state for this FSM */
fsm_setendopaque(m->fsm, m);
/* fsm_setendopaque(m->fsm, m); */
if (LOG()) {
fprintf(stderr, "zone_minimise: ast_setendmapping(m->fsm, m: %p)\n",
(void *)m);
}

if (!ast_setendmapping(m->fsm, m)) {
assert(!"failed");
}

(void) fsm_getstart(m->fsm, &ms);

Expand Down Expand Up @@ -781,12 +796,12 @@ main(int argc, char *argv[])
zn = 0;
}

opt.carryopaque = carryopaque;
/* opt.carryopaque = carryopaque; */
cur_zone = ast->zl;
if (run_threads(concurrency, zone_determinise)) {
return EXIT_FAILURE;
}
opt.carryopaque = NULL;
/* opt.carryopaque = NULL; */

if (print_progress) {
fprintf(stderr, "\n");
Expand Down Expand Up @@ -848,7 +863,10 @@ main(int argc, char *argv[])
continue;
}

m = fsm_getopaque(q->fsm, i);
m = ast_getendmapping(q->fsm, i);
if (LOG()) {
fprintf(stderr, "main: m <- ast_getendmapping(dst_fsm, i]: %d) = %p // remove duplicate zones?\n", i, (void *)m);
}
assert(m != NULL);

if (m->to == *p) {
Expand Down Expand Up @@ -939,7 +957,11 @@ main(int argc, char *argv[])
continue;
}

m = fsm_getopaque(z->fsm, i);
/* FIXME: get mapping for end state i */
m = ast_getendmapping(z->fsm, i);
if (LOG()) {
fprintf(stderr, "main: m <- ast_getendmapping(dst_fsm, i]: %d) = %p // pick up conflicts\n", i, (void *)m);
}
assert(m != NULL);

if (m->conflict != NULL) {
Expand Down Expand Up @@ -1018,7 +1040,10 @@ main(int argc, char *argv[])
continue;
}

m = fsm_getopaque(z->fsm, i);
m = ast_getendmapping(z->fsm, i);
if (LOG()) {
fprintf(stderr, "main: m <- ast_getendmapping(dst_fsm, i]: %d) = %p // free conflicts\n", i, (void *)m);
}
if (m != NULL) {
assert(m->fsm == NULL);

Expand Down
Loading

0 comments on commit f5e5a1f

Please sign in to comment.