Change the predicate stuff to use costs instead; now you can use when clauses

on leaves. Remove an iburg premature optimisation (required for above).
This commit is contained in:
David Given 2016-09-24 13:33:59 +02:00
parent d96ceea08a
commit 434eafd35d
2 changed files with 23 additions and 77 deletions

View file

@ -24,6 +24,7 @@ static void print(char* fmt, ...);
static void ckreach(Nonterm p); static void ckreach(Nonterm p);
static void emitclosure(Nonterm nts); static void emitclosure(Nonterm nts);
static void emitcost(Tree t, char* v); static void emitcost(Tree t, char* v);
static void emitcostcalc(Rule r);
static void emitdefs(Nonterm nts, int ntnumber); static void emitdefs(Nonterm nts, int ntnumber);
static void emitfuncs(void); static void emitfuncs(void);
static void emitheader(void); static void emitheader(void);
@ -34,7 +35,6 @@ static void emitnts(Rule rules, int nrules);
static void emitrecord(char* pre, Rule r, int cost); static void emitrecord(char* pre, Rule r, int cost);
static void emitrule(Nonterm nts); static void emitrule(Nonterm nts);
static void emitpredicatedefinitions(Rule rules); static void emitpredicatedefinitions(Rule rules);
static void emitpredicatecall(Rule rule);
static void emitstate(Term terms, Nonterm start, int ntnumber); static void emitstate(Term terms, Nonterm start, int ntnumber);
static void emitstring(Rule rules); static void emitstring(Rule rules);
static void emitstruct(Nonterm nts, int ntnumber); static void emitstruct(Nonterm nts, int ntnumber);
@ -283,9 +283,6 @@ Rule rule(char* id, Tree pattern, int ern, Stringlist when, int cost)
Rule *q; Rule *q;
Term p = pattern->op; Term p = pattern->op;
if (when && (p->arity == 0))
yyerror("can't have a when clause on leaf nodes");
nrules++; nrules++;
r->when = when; r->when = when;
r->lhs = nonterm(id); r->lhs = nonterm(id);
@ -421,11 +418,6 @@ static void emitcase(Term p, int ntnumber)
{ {
case 0: case 0:
case -1: case -1:
if (!Tflag)
{
emitleaf(p, ntnumber);
return;
}
break; break;
case 1: case 1:
print("%2assert(l);\n"); print("%2assert(l);\n");
@ -442,26 +434,21 @@ static void emitcase(Term p, int ntnumber)
{ {
case 0: case 0:
case -1: case -1:
print("%2if ("); print("%2{%1/* %R */\n%3c = ", r);
emitpredicatecall(r); emitcostcalc(r);
print(")\n%2{%1/* %R */\n%3c = ", r);
break; break;
case 1: case 1:
if (r->pattern->nterms > 1) if (r->pattern->nterms > 1)
{ {
print("%2if (%1/* %R */\n", r); print("%2if (%1/* %R */\n", r);
emittest(r->pattern->left, "l", " "); emittest(r->pattern->left, "l", " ");
print("%3&& ");
emitpredicatecall(r);
print("\n");
print("%2) {\n%3c = "); print("%2) {\n%3c = ");
} }
else else
{ {
print("%2if ("); print("%2{%1/* %R */\n%3c = ", r);
emitpredicatecall(r);
print(")\n%2{%1/* %R */\n%3c = ", r);
} }
emitcostcalc(r);
emitcost(r->pattern->left, "l"); emitcost(r->pattern->left, "l");
break; break;
case 2: case 2:
@ -471,17 +458,13 @@ static void emitcase(Term p, int ntnumber)
emittest(r->pattern->left, "l", emittest(r->pattern->left, "l",
r->pattern->right->nterms ? " && " : " "); r->pattern->right->nterms ? " && " : " ");
emittest(r->pattern->right, "r", " "); emittest(r->pattern->right, "r", " ");
print("%3&& ");
emitpredicatecall(r);
print("\n");
print("%2) {\n%3c = "); print("%2) {\n%3c = ");
} }
else else
{ {
print("%2if ("); print("%2{%1/* %R */\n%3c = ", r);
emitpredicatecall(r);
print(")\n%2{%1/* %R */\n%3c = ", r);
} }
emitcostcalc(r);
emitcost(r->pattern->left, "l"); emitcost(r->pattern->left, "l");
emitcost(r->pattern->right, "r"); emitcost(r->pattern->right, "r");
break; break;
@ -668,46 +651,6 @@ static void closure(int cost[], Rule rule[], Nonterm p, int c)
} }
} }
/* emitleaf - emit state code for a leaf */
static void emitleaf(Term p, int ntnumber)
{
int i;
Rule r;
static int* cost;
static Rule* rule;
if (cost == NULL)
{
cost = calloc(ntnumber+1, sizeof *cost);
rule = calloc(ntnumber+1, sizeof *rule);
}
for (i = 0; i <= ntnumber; i++)
{
cost[i] = maxcost;
rule[i] = NULL;
}
for (r = p->rules; r; r = r->next)
if (r->pattern->left == NULL && r->pattern->right == NULL)
{
cost[r->lhs->number] = r->cost;
rule[r->lhs->number] = r;
closure(cost, rule, r->lhs, r->cost);
}
print("%2{\n%3static struct %Pstate z = { %d, 0, 0,\n%4{%10,\n", p->esn);
for (i = 1; i <= ntnumber; i++)
if (cost[i] < maxcost)
print("%5%d,%1/* %R */\n", cost[i], rule[i]);
else
print("%5%d,\n", cost[i]);
print("%4},{\n");
for (i = 1; i <= ntnumber; i++)
if (rule[i])
print("%5%d,%1/* %R */\n", rule[i]->packed, rule[i]);
else
print("%50,\n");
print("%4}\n%3};\n%3return (STATE_TYPE)&z;\n%2}\n");
}
/* computents - fill in bp with burm_nts vector for tree t */ /* computents - fill in bp with burm_nts vector for tree t */
static char* computents(Tree t, char* bp) static char* computents(Tree t, char* bp)
{ {
@ -815,13 +758,11 @@ static void emitpredicatedefinitions(Rule r)
} }
} }
/* emitpredicatecall - emit a call to a predicate */ /* emitcost - emit a cost calculation via a predicate */
static void emitpredicatecall(Rule r) static void emitcostcalc(Rule r)
{ {
if (r->when) if (r->when)
print("%Ppredicate_%d(node)", r->ern); print("!%Ppredicate_%d(node) ? %d : ", r->ern, maxcost);
else
print("1");
} }
/* emitstate - emit state function */ /* emitstate - emit state function */
@ -837,15 +778,17 @@ static void emitstate(Term terms, Nonterm start, int ntnumber)
"%1struct %Pstate* r = (struct %Pstate *)right;\n" "%1struct %Pstate* r = (struct %Pstate *)right;\n"
"\n" "\n"
"%1assert(sizeof (STATE_TYPE) >= sizeof (void *));\n%1"); "%1assert(sizeof (STATE_TYPE) >= sizeof (void *));\n%1");
if (!Tflag) print("%1p = ALLOC(sizeof *p);\n"
print("if (%Parity[op] > 0) "); "%1%Passert(p, PANIC(\"ALLOC returned NULL in %Pstate\\n\"));\n"
print("{\n%2p = ALLOC(sizeof *p);\n" "%1p->op = op;\n"
"%2%Passert(p, PANIC(\"ALLOC returned NULL in %Pstate\\n\"));\n" "%1p->left = l;\n"
"%2p->op = op;\n%2p->left = l;\n%2p->right = r;\n%2p->rule.%P%S = 0;\n", "%1p->right = r;\n"
"%1p->rule.%P%S = 0;\n",
start); start);
for (i = 1; i <= ntnumber; i++) for (i = 1; i <= ntnumber; i++)
print("%2p->cost[%d] =\n", i); print("%1p->cost[%d] =\n", i);
print("%3%d;\n%1}\n%1switch (op) {\n", maxcost); print("%2%d;\n"
"%1switch (op) {\n", maxcost);
for (p = terms; p; p = p->link) for (p = terms; p; p = p->link)
emitcase(p, ntnumber); emitcase(p, ntnumber);
print("%1default:\n" print("%1default:\n"

View file

@ -32,7 +32,10 @@ int main(void) {
NODEPTR_TYPE p; NODEPTR_TYPE p;
p = tree(STORE, p = tree(STORE,
tree(LABEL, 0, 0), tree(ADD,
tree(LABEL, 0, 0),
tree(CONST, 0, 0)
),
tree(ADD, tree(ADD,
tree(LOAD, tree(LOAD,
tree(LABEL, 0, 0), tree(LABEL, 0, 0),