2016-09-20 18:37:16 +00:00
|
|
|
#include <assert.h>
|
2016-09-24 14:59:49 +00:00
|
|
|
#include <unistd.h>
|
2016-09-20 18:37:16 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2016-09-24 22:21:46 +00:00
|
|
|
#include <stdbool.h>
|
2016-09-25 09:49:51 +00:00
|
|
|
#include <stdint.h>
|
2016-09-20 18:37:16 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <limits.h>
|
2016-09-24 16:31:35 +00:00
|
|
|
#include <errno.h>
|
2016-10-01 10:17:14 +00:00
|
|
|
#include <inttypes.h>
|
2016-09-20 18:37:16 +00:00
|
|
|
#include "iburg.h"
|
2016-09-24 16:31:35 +00:00
|
|
|
#include "ircodes.h"
|
2016-09-30 17:10:30 +00:00
|
|
|
#include "astring.h"
|
2016-10-01 10:17:14 +00:00
|
|
|
#include "smap.h"
|
2016-10-05 20:56:25 +00:00
|
|
|
#include "mcgg.h"
|
2016-09-20 18:37:16 +00:00
|
|
|
|
|
|
|
static char rcsid[] = "$Id$";
|
|
|
|
|
2016-09-24 14:59:49 +00:00
|
|
|
int maxcost = SHRT_MAX / 2;
|
2016-09-20 18:37:16 +00:00
|
|
|
|
2016-10-15 16:38:46 +00:00
|
|
|
FILE* infp = NULL;
|
|
|
|
FILE* outfp = NULL;
|
|
|
|
FILE* hdrfp = NULL;
|
|
|
|
|
2016-09-20 19:00:16 +00:00
|
|
|
static char* prefix = "burm";
|
2016-09-24 14:59:49 +00:00
|
|
|
static int Tflag = 1; /* tracing */
|
2016-09-20 18:37:16 +00:00
|
|
|
static int ntnumber = 0;
|
|
|
|
static Nonterm start = 0;
|
|
|
|
static Term terms;
|
|
|
|
static Nonterm nts;
|
|
|
|
static Rule rules;
|
|
|
|
static int nrules;
|
|
|
|
|
2016-10-01 10:17:14 +00:00
|
|
|
static SMAPOF(struct reg) registers;
|
2016-10-05 19:00:28 +00:00
|
|
|
static SMAPOF(struct regattr) registerattrs;
|
2016-10-01 10:17:14 +00:00
|
|
|
|
2016-10-15 16:38:46 +00:00
|
|
|
static void print(const char* fmt, ...);
|
|
|
|
static void printh(const char* fmt, ...);
|
2016-09-24 16:31:35 +00:00
|
|
|
static void registerterminals(void);
|
2016-10-05 19:00:28 +00:00
|
|
|
static struct regattr* makeregattr(const char* id);
|
2016-09-20 18:37:16 +00:00
|
|
|
static void emitclosure(Nonterm nts);
|
2016-09-30 17:10:30 +00:00
|
|
|
static void emitcost(Tree t, const char* v);
|
2016-09-24 11:33:59 +00:00
|
|
|
static void emitcostcalc(Rule r);
|
2016-09-20 18:37:16 +00:00
|
|
|
static void emitdefs(Nonterm nts, int ntnumber);
|
|
|
|
static void emitfuncs(void);
|
|
|
|
static void emitheader(void);
|
2016-09-25 20:17:14 +00:00
|
|
|
static void emitinsndata(Rule rules);
|
2016-09-20 18:37:16 +00:00
|
|
|
static void emitkids(Rule rules, int nrules);
|
|
|
|
static void emitlabel(Nonterm start);
|
|
|
|
static void emitleaf(Term p, int ntnumber);
|
|
|
|
static void emitnts(Rule rules, int nrules);
|
2016-09-25 20:17:14 +00:00
|
|
|
static void emitpredicatedefinitions(Rule rules);
|
2016-09-20 19:00:16 +00:00
|
|
|
static void emitrecord(char* pre, Rule r, int cost);
|
2016-10-05 19:00:28 +00:00
|
|
|
static void emitregisterattrs();
|
2016-10-01 10:17:14 +00:00
|
|
|
static void emitregisters();
|
2016-09-20 18:37:16 +00:00
|
|
|
static void emitrule(Nonterm nts);
|
|
|
|
static void emitstate(Term terms, Nonterm start, int ntnumber);
|
|
|
|
static void emitstring(Rule rules);
|
|
|
|
static void emitstruct(Nonterm nts, int ntnumber);
|
|
|
|
static void emitterms(Term terms);
|
2016-09-30 17:10:30 +00:00
|
|
|
static void emittest(Tree t, const char* v, const char* suffix);
|
2016-09-20 18:37:16 +00:00
|
|
|
|
2016-09-24 22:21:46 +00:00
|
|
|
extern int yy_flex_debug;
|
|
|
|
|
2016-09-20 19:00:16 +00:00
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2016-09-20 18:37:16 +00:00
|
|
|
int c, i;
|
|
|
|
Nonterm p;
|
2016-09-20 19:00:16 +00:00
|
|
|
|
2016-09-20 22:43:10 +00:00
|
|
|
#if 0
|
|
|
|
extern int yydebug;
|
|
|
|
yydebug = 1;
|
|
|
|
#endif
|
|
|
|
|
2016-09-24 16:31:35 +00:00
|
|
|
infp = stdin;
|
|
|
|
outfp = stdout;
|
2016-10-15 16:38:46 +00:00
|
|
|
hdrfp = NULL;
|
2016-09-24 22:21:46 +00:00
|
|
|
yy_flex_debug = 0;
|
2016-09-24 16:31:35 +00:00
|
|
|
|
2016-09-24 14:59:49 +00:00
|
|
|
for (;;)
|
|
|
|
{
|
2016-10-15 16:38:46 +00:00
|
|
|
int opt = getopt(argc, argv, "p:i:o:h:yf");
|
2016-09-24 14:59:49 +00:00
|
|
|
if (opt == -1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (opt)
|
2016-09-20 19:00:16 +00:00
|
|
|
{
|
2016-09-24 14:59:49 +00:00
|
|
|
case 'p':
|
|
|
|
prefix = optarg;
|
|
|
|
break;
|
|
|
|
|
2016-09-24 16:31:35 +00:00
|
|
|
case 'i':
|
|
|
|
infp = fopen(optarg, "r");
|
|
|
|
if (!infp)
|
|
|
|
{
|
|
|
|
yyerror("cannot open input file: %s\n", strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'o':
|
|
|
|
outfp = fopen(optarg, "w");
|
|
|
|
if (!outfp)
|
|
|
|
{
|
|
|
|
yyerror("cannot open output file: %s\n", strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2016-10-15 16:38:46 +00:00
|
|
|
case 'h':
|
|
|
|
hdrfp = fopen(optarg, "w");
|
|
|
|
if (!hdrfp)
|
|
|
|
{
|
|
|
|
yyerror("cannot open output header file: %s\n", strerror(errno));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2016-09-24 17:03:55 +00:00
|
|
|
case 'y':
|
|
|
|
{
|
|
|
|
extern int yydebug;
|
|
|
|
yydebug = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-09-24 22:21:46 +00:00
|
|
|
case 'f':
|
|
|
|
{
|
|
|
|
yy_flex_debug = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-09-24 14:59:49 +00:00
|
|
|
default:
|
|
|
|
yyerror("usage: %s [-p prefix] < input > output\n", argv[0]);
|
2016-09-20 18:37:16 +00:00
|
|
|
exit(1);
|
2016-09-20 19:00:16 +00:00
|
|
|
}
|
2016-09-24 14:59:49 +00:00
|
|
|
}
|
2017-08-06 10:42:17 +00:00
|
|
|
|
2016-09-24 15:20:40 +00:00
|
|
|
emitheader();
|
2016-09-24 16:31:35 +00:00
|
|
|
registerterminals();
|
2016-09-24 15:20:40 +00:00
|
|
|
|
2016-09-25 20:17:14 +00:00
|
|
|
start = nonterm("stmt", true);
|
2016-09-24 17:03:55 +00:00
|
|
|
|
2016-10-05 20:56:25 +00:00
|
|
|
/* Define some standard terms. */
|
|
|
|
|
|
|
|
{
|
|
|
|
const static struct terminfo reg = { "reg", NULL, "" };
|
|
|
|
const static struct terminfo REG = { "REG", NULL, NULL };
|
2016-10-23 19:54:14 +00:00
|
|
|
const static struct terminfo NOPI = { "NOP.I", NULL, NULL };
|
|
|
|
const static struct terminfo NOPF = { "NOP.F", NULL, NULL };
|
|
|
|
const static struct terminfo NOPL = { "NOP.L", NULL, NULL };
|
|
|
|
const static struct terminfo NOPD = { "NOP.D", NULL, NULL };
|
2016-10-15 16:38:46 +00:00
|
|
|
const static struct terminfo RET = { "RET", NULL, NULL };
|
2016-10-05 20:56:25 +00:00
|
|
|
|
|
|
|
nonterm("reg", true);
|
|
|
|
|
|
|
|
rule(NULL, tree(®, NULL, NULL))->cost = 1;
|
|
|
|
rule(®, tree(®, NULL, NULL))->cost = 1;
|
2016-10-23 19:54:14 +00:00
|
|
|
rule(®, tree(&NOPI, tree(®, NULL, NULL), NULL))->cost = 1;
|
|
|
|
rule(®, tree(&NOPF, tree(®, NULL, NULL), NULL))->cost = 1;
|
|
|
|
rule(®, tree(&NOPL, tree(®, NULL, NULL), NULL))->cost = 1;
|
|
|
|
rule(®, tree(&NOPD, tree(®, NULL, NULL), NULL))->cost = 1;
|
2016-10-15 16:38:46 +00:00
|
|
|
rule(NULL, tree(&RET, NULL, NULL))->cost = 1;
|
2016-10-05 20:56:25 +00:00
|
|
|
}
|
|
|
|
|
2016-09-20 22:43:10 +00:00
|
|
|
yyin = infp;
|
2016-09-20 18:37:16 +00:00
|
|
|
yyparse();
|
2016-09-20 22:43:10 +00:00
|
|
|
|
2016-10-05 19:00:28 +00:00
|
|
|
emitregisterattrs();
|
2016-10-01 10:17:14 +00:00
|
|
|
emitregisters();
|
2016-09-20 18:37:16 +00:00
|
|
|
emitdefs(nts, ntnumber);
|
|
|
|
emitstruct(nts, ntnumber);
|
|
|
|
emitnts(rules, nrules);
|
|
|
|
emitterms(terms);
|
2016-09-24 14:59:49 +00:00
|
|
|
emitstring(rules);
|
2016-09-20 18:37:16 +00:00
|
|
|
emitrule(nts);
|
|
|
|
emitclosure(nts);
|
2016-09-20 22:43:10 +00:00
|
|
|
emitpredicatedefinitions(rules);
|
2016-09-25 15:14:54 +00:00
|
|
|
emitinsndata(rules);
|
2016-09-20 18:37:16 +00:00
|
|
|
if (start)
|
|
|
|
emitstate(terms, start, ntnumber);
|
|
|
|
print("#ifdef STATE_LABEL\n");
|
|
|
|
if (start)
|
|
|
|
emitlabel(start);
|
|
|
|
emitkids(rules, nrules);
|
|
|
|
emitfuncs();
|
|
|
|
print("#endif\n");
|
2016-09-20 22:43:10 +00:00
|
|
|
print("#include \"mcgg_generated_footer.h\"\n");
|
2016-10-15 16:38:46 +00:00
|
|
|
printh("#endif\n");
|
|
|
|
|
|
|
|
if (outfp)
|
|
|
|
fclose(outfp);
|
|
|
|
if (hdrfp)
|
|
|
|
fclose(hdrfp);
|
|
|
|
|
2016-09-20 18:37:16 +00:00
|
|
|
return errcnt > 0;
|
|
|
|
}
|
|
|
|
|
2016-10-23 19:54:14 +00:00
|
|
|
static void registerterminal(const struct ir_data* data, int iropcode, char type)
|
2016-09-24 16:31:35 +00:00
|
|
|
{
|
2016-10-23 19:54:14 +00:00
|
|
|
const char* s = (type == 0) ? data->name : aprintf("%s.%c", data->name, type);
|
|
|
|
int esn = ir_to_esn(iropcode, type);
|
2016-09-24 16:31:35 +00:00
|
|
|
|
|
|
|
term(s, esn);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void registerterminals(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i=0; i<IR__COUNT; i++)
|
|
|
|
{
|
2016-10-23 19:54:14 +00:00
|
|
|
registerterminal(&ir_data[i], i, 'I');
|
|
|
|
registerterminal(&ir_data[i], i, 'F');
|
|
|
|
registerterminal(&ir_data[i], i, 'L');
|
|
|
|
registerterminal(&ir_data[i], i, 'D');
|
|
|
|
registerterminal(&ir_data[i], i, 0);
|
2016-09-24 16:31:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-20 19:00:16 +00:00
|
|
|
struct entry
|
|
|
|
{
|
|
|
|
union
|
|
|
|
{
|
2016-09-24 10:11:30 +00:00
|
|
|
const char* name;
|
2016-09-20 18:37:16 +00:00
|
|
|
struct term t;
|
|
|
|
struct nonterm nt;
|
2016-09-25 20:17:14 +00:00
|
|
|
struct reg r;
|
2016-10-05 19:00:28 +00:00
|
|
|
struct regattr rc;
|
2016-09-20 18:37:16 +00:00
|
|
|
} sym;
|
2016-09-20 19:00:16 +00:00
|
|
|
struct entry* link;
|
|
|
|
} * table[211];
|
|
|
|
#define HASHSIZE (sizeof table / sizeof table[0])
|
2016-09-20 18:37:16 +00:00
|
|
|
|
|
|
|
/* hash - return hash number for str */
|
2016-09-24 10:11:30 +00:00
|
|
|
static unsigned hash(const char* str)
|
2016-09-20 19:00:16 +00:00
|
|
|
{
|
2016-09-20 18:37:16 +00:00
|
|
|
unsigned h = 0;
|
|
|
|
|
|
|
|
while (*str)
|
2016-09-20 19:00:16 +00:00
|
|
|
h = (h << 1) + *str++;
|
2016-09-20 18:37:16 +00:00
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* lookup - lookup symbol name */
|
2016-09-25 20:17:14 +00:00
|
|
|
void* lookup(const char* name)
|
2016-09-20 19:00:16 +00:00
|
|
|
{
|
|
|
|
struct entry* p = table[hash(name) % HASHSIZE];
|
2016-09-20 18:37:16 +00:00
|
|
|
|
2016-09-20 19:00:16 +00:00
|
|
|
for (; p; p = p->link)
|
2016-09-20 18:37:16 +00:00
|
|
|
if (strcmp(name, p->sym.name) == 0)
|
|
|
|
return &p->sym;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* install - install symbol name */
|
2016-09-24 10:11:30 +00:00
|
|
|
static void* install(const char* name)
|
2016-09-20 19:00:16 +00:00
|
|
|
{
|
2016-09-20 22:43:10 +00:00
|
|
|
struct entry* p = calloc(1, sizeof *p);
|
2016-09-20 19:00:16 +00:00
|
|
|
int i = hash(name) % HASHSIZE;
|
2016-09-20 18:37:16 +00:00
|
|
|
|
|
|
|
p->sym.name = name;
|
|
|
|
p->link = table[i];
|
|
|
|
table[i] = p;
|
|
|
|
return &p->sym;
|
|
|
|
}
|
|
|
|
|
2016-10-21 21:31:00 +00:00
|
|
|
struct reg* makereg(const char* id)
|
2016-09-25 20:17:14 +00:00
|
|
|
{
|
2016-10-01 10:17:14 +00:00
|
|
|
struct reg* p = smap_get(®isters, id);
|
2016-10-05 19:00:28 +00:00
|
|
|
static int number = 0;
|
2016-09-25 20:17:14 +00:00
|
|
|
|
|
|
|
if (p)
|
|
|
|
yyerror("redefinition of '%s'", id);
|
2016-10-01 10:17:14 +00:00
|
|
|
p = calloc(1, sizeof(*p));
|
|
|
|
p->name = id;
|
2016-09-25 20:17:14 +00:00
|
|
|
p->number = number++;
|
2016-10-20 19:47:28 +00:00
|
|
|
array_append(&p->aliases, p);
|
2016-10-01 10:17:14 +00:00
|
|
|
smap_put(®isters, id, p);
|
2016-09-25 20:17:14 +00:00
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2016-10-21 21:31:00 +00:00
|
|
|
void setregnames(struct reg* reg, struct stringlist* names)
|
|
|
|
{
|
|
|
|
if (reg->names)
|
|
|
|
yyerror("you can only set one set of register names");
|
|
|
|
|
|
|
|
reg->names = names;
|
|
|
|
}
|
|
|
|
|
2016-10-05 19:00:28 +00:00
|
|
|
struct regattr* makeregattr(const char* id)
|
2016-09-25 20:17:14 +00:00
|
|
|
{
|
2016-10-05 19:00:28 +00:00
|
|
|
struct regattr* p = smap_get(®isterattrs, id);
|
|
|
|
static int number = 0;
|
|
|
|
|
|
|
|
if (p)
|
|
|
|
yyerror("redefinition of '%s'", id);
|
|
|
|
p = calloc(1, sizeof(*p));
|
|
|
|
p->name = id;
|
|
|
|
p->number = number++;
|
|
|
|
smap_put(®isterattrs, id, p);
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2016-10-25 21:04:20 +00:00
|
|
|
void addregattr(struct reg* reg, const char* id)
|
2016-10-05 19:00:28 +00:00
|
|
|
{
|
|
|
|
struct regattr* p = smap_get(®isterattrs, id);
|
2016-09-25 20:17:14 +00:00
|
|
|
|
|
|
|
if (!p)
|
2016-10-05 19:00:28 +00:00
|
|
|
p = makeregattr(id);
|
2016-09-29 20:32:43 +00:00
|
|
|
|
2016-10-05 19:00:28 +00:00
|
|
|
reg->attrs |= 1<<(p->number);
|
2016-09-25 20:17:14 +00:00
|
|
|
}
|
|
|
|
|
2016-10-20 19:47:28 +00:00
|
|
|
void addregalias(struct reg* r1, struct reg* r2)
|
|
|
|
{
|
|
|
|
if (!array_appendu(&r1->aliases, r2))
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i=0; i<r1->aliases.count; i++)
|
|
|
|
addregalias(r1->aliases.item[i], r2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void addregaliases(struct reg* reg, struct stringlist* aliases)
|
|
|
|
{
|
|
|
|
struct stringfragment* f = aliases->first;
|
|
|
|
|
|
|
|
while (f)
|
|
|
|
{
|
|
|
|
struct reg* r = smap_get(®isters, f->data);
|
|
|
|
if (!r)
|
|
|
|
yyerror("register '%s' is not defined here", f->data);
|
|
|
|
|
|
|
|
array_appendu(®->aliases, r);
|
|
|
|
array_appendu(&r->aliases, reg);
|
|
|
|
|
|
|
|
f = f->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-05 19:00:28 +00:00
|
|
|
struct regattr* getregattr(const char* id)
|
2016-09-25 20:17:14 +00:00
|
|
|
{
|
2016-10-05 19:00:28 +00:00
|
|
|
struct regattr* p = smap_get(®isterattrs, id);
|
2016-10-01 10:17:14 +00:00
|
|
|
if (!p)
|
2016-09-29 20:06:04 +00:00
|
|
|
yyerror("'%s' is not the name of a register class", id);
|
2016-09-25 20:17:14 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2016-09-20 18:37:16 +00:00
|
|
|
/* nonterm - create a new terminal id, if necessary */
|
2016-09-25 20:17:14 +00:00
|
|
|
Nonterm nonterm(const char* id, bool allocate)
|
2016-09-20 19:00:16 +00:00
|
|
|
{
|
2016-09-25 20:17:14 +00:00
|
|
|
Nonterm p = lookup(id);
|
|
|
|
Nonterm* q = &nts;
|
2016-09-20 18:37:16 +00:00
|
|
|
|
|
|
|
if (p && p->kind == NONTERM)
|
|
|
|
return p;
|
2016-09-25 20:17:14 +00:00
|
|
|
if (p)
|
|
|
|
yyerror("redefinition of '%s' as something else\n", id);
|
|
|
|
if (!allocate)
|
|
|
|
yyerror("'%s' has not been declared\n", id);
|
|
|
|
|
2016-09-20 18:37:16 +00:00
|
|
|
p = install(id);
|
|
|
|
p->kind = NONTERM;
|
|
|
|
p->number = ++ntnumber;
|
|
|
|
if (p->number == 1)
|
|
|
|
start = p;
|
|
|
|
while (*q && (*q)->number < p->number)
|
|
|
|
q = &(*q)->link;
|
|
|
|
assert(*q == 0 || (*q)->number != p->number);
|
|
|
|
p->link = *q;
|
|
|
|
*q = p;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* term - create a new terminal id with external symbol number esn */
|
2016-09-24 10:11:30 +00:00
|
|
|
Term term(const char* id, int esn)
|
2016-09-20 19:00:16 +00:00
|
|
|
{
|
2016-09-24 16:31:35 +00:00
|
|
|
Term p = lookup(id);
|
|
|
|
Term* q = &terms;
|
2016-09-20 18:37:16 +00:00
|
|
|
|
|
|
|
if (p)
|
2016-09-25 20:17:14 +00:00
|
|
|
yyerror("redefinition of '%s'\n", id);
|
|
|
|
|
|
|
|
p = install(id);
|
2016-09-20 18:37:16 +00:00
|
|
|
p->kind = TERM;
|
|
|
|
p->esn = esn;
|
|
|
|
p->arity = -1;
|
|
|
|
while (*q && (*q)->esn < p->esn)
|
|
|
|
q = &(*q)->link;
|
|
|
|
if (*q && (*q)->esn == p->esn)
|
|
|
|
yyerror("duplicate external symbol number `%s=%d'\n",
|
2016-09-20 19:00:16 +00:00
|
|
|
p->name, p->esn);
|
2016-09-20 18:37:16 +00:00
|
|
|
p->link = *q;
|
|
|
|
*q = p;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* tree - create & initialize a tree node with the given fields */
|
2016-10-05 20:56:25 +00:00
|
|
|
Tree tree(const struct terminfo* ti, Tree left, Tree right)
|
2016-09-20 19:00:16 +00:00
|
|
|
{
|
2016-09-20 22:43:10 +00:00
|
|
|
Tree t = calloc(1, sizeof *t);
|
2016-10-01 10:17:14 +00:00
|
|
|
Term p = lookup(ti->name);
|
2016-09-20 18:37:16 +00:00
|
|
|
int arity = 0;
|
|
|
|
|
|
|
|
if (left && right)
|
|
|
|
arity = 2;
|
|
|
|
else if (left)
|
|
|
|
arity = 1;
|
2016-09-20 19:00:16 +00:00
|
|
|
if (p == NULL && arity > 0)
|
|
|
|
{
|
2016-10-01 10:17:14 +00:00
|
|
|
yyerror("undefined terminal `%s'\n", ti->name);
|
|
|
|
p = term(ti->name, -1);
|
2016-09-20 19:00:16 +00:00
|
|
|
}
|
|
|
|
else if (p == NULL && arity == 0)
|
2016-10-01 10:17:14 +00:00
|
|
|
p = (Term)nonterm(ti->name, false);
|
2016-09-20 19:00:16 +00:00
|
|
|
else if (p && p->kind == NONTERM && arity > 0)
|
|
|
|
{
|
2016-10-01 10:17:14 +00:00
|
|
|
yyerror("`%s' is a non-terminal\n", ti->name);
|
|
|
|
p = term(ti->name, -1);
|
2016-09-20 18:37:16 +00:00
|
|
|
}
|
|
|
|
if (p->kind == TERM && p->arity == -1)
|
|
|
|
p->arity = arity;
|
|
|
|
if (p->kind == TERM && arity != p->arity)
|
2016-10-01 10:17:14 +00:00
|
|
|
yyerror("inconsistent arity for terminal `%s'\n", ti->name);
|
2016-09-20 18:37:16 +00:00
|
|
|
t->op = p;
|
|
|
|
t->nterms = p->kind == TERM;
|
|
|
|
if (t->left = left)
|
|
|
|
t->nterms += left->nterms;
|
|
|
|
if (t->right = right)
|
|
|
|
t->nterms += right->nterms;
|
2016-10-05 20:56:25 +00:00
|
|
|
|
|
|
|
/* Special rules that have no output register attribute use "" as the
|
|
|
|
* attribute name; these can't be made by the grammar. */
|
|
|
|
|
|
|
|
t->label = ti->label;
|
|
|
|
if ((p->kind == TERM) && (ti->attr))
|
|
|
|
yyerror("can't specify an input register attribute for terminal '%s'", ti->name);
|
|
|
|
if (p->kind == NONTERM)
|
|
|
|
{
|
|
|
|
Nonterm nt = (Nonterm)p;
|
|
|
|
if (nt->is_fragment && ti->attr)
|
|
|
|
yyerror("can't specify an input register attribute for fragment '%s'", ti->name);
|
|
|
|
if (!nt->is_fragment && !ti->attr)
|
|
|
|
yyerror("must specify an input register attribute for non-fragment '%s'", ti->name);
|
|
|
|
|
|
|
|
if (ti->attr && ti->attr[0])
|
|
|
|
{
|
2016-10-09 13:09:34 +00:00
|
|
|
t->attr = smap_get(®isterattrs, ti->attr);
|
|
|
|
if (!t->attr)
|
2016-10-05 20:56:25 +00:00
|
|
|
yyerror("'%s' doesn't seem to be a known register attribute", ti->attr);
|
|
|
|
}
|
|
|
|
}
|
2016-09-20 18:37:16 +00:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* rule - create & initialize a rule with the given fields */
|
2016-10-05 20:56:25 +00:00
|
|
|
Rule rule(const struct terminfo* ti, Tree pattern)
|
2016-09-20 19:00:16 +00:00
|
|
|
{
|
2016-10-05 20:56:25 +00:00
|
|
|
static int number = 1;
|
|
|
|
static const struct terminfo stmt = { "stmt", NULL, NULL };
|
2016-09-20 22:43:10 +00:00
|
|
|
Rule r = calloc(1, sizeof *r);
|
|
|
|
Rule *q;
|
2016-09-20 18:37:16 +00:00
|
|
|
Term p = pattern->op;
|
|
|
|
|
2016-10-05 20:56:25 +00:00
|
|
|
if (!ti)
|
|
|
|
ti = &stmt;
|
|
|
|
|
2016-09-20 18:37:16 +00:00
|
|
|
nrules++;
|
2016-09-25 09:49:51 +00:00
|
|
|
r->lineno = yylineno;
|
2016-10-05 20:56:25 +00:00
|
|
|
r->lhs = nonterm(ti->name, false);
|
2016-09-20 18:37:16 +00:00
|
|
|
r->packed = ++r->lhs->lhscount;
|
|
|
|
for (q = &r->lhs->rules; *q; q = &(*q)->decode)
|
|
|
|
;
|
|
|
|
*q = r;
|
|
|
|
r->pattern = pattern;
|
2016-10-05 20:56:25 +00:00
|
|
|
r->ern = number++;
|
2016-09-20 19:00:16 +00:00
|
|
|
if (p->kind == TERM)
|
|
|
|
{
|
2016-09-20 18:37:16 +00:00
|
|
|
r->next = p->rules;
|
|
|
|
p->rules = r;
|
2016-09-20 19:00:16 +00:00
|
|
|
}
|
|
|
|
else if (pattern->left == NULL && pattern->right == NULL)
|
|
|
|
{
|
2016-09-20 18:37:16 +00:00
|
|
|
Nonterm p = pattern->op;
|
|
|
|
r->chain = p->chain;
|
2016-09-20 19:00:16 +00:00
|
|
|
p->chain = r;
|
2016-09-20 18:37:16 +00:00
|
|
|
}
|
|
|
|
for (q = &rules; *q && (*q)->ern < r->ern; q = &(*q)->link)
|
|
|
|
;
|
|
|
|
if (*q && (*q)->ern == r->ern)
|
|
|
|
yyerror("duplicate external rule number `%d'\n", r->ern);
|
|
|
|
r->link = *q;
|
|
|
|
*q = r;
|
2016-10-05 20:56:25 +00:00
|
|
|
|
|
|
|
r->label = ti->label;
|
|
|
|
if (r->lhs->is_fragment && ti->attr)
|
|
|
|
yyerror("can't specify an output register attribute for a fragment");
|
|
|
|
if (!r->lhs->is_fragment && !ti->attr && (r->lhs->number != NONTERM_STMT))
|
|
|
|
yyerror("must specify an output register attribute for non-fragments");
|
|
|
|
|
|
|
|
/* Special rules that have no output register attribute use "" as the
|
|
|
|
* attribute name; these can't be made by the grammar. */
|
|
|
|
|
|
|
|
if (ti->attr && ti->attr[0])
|
|
|
|
{
|
|
|
|
r->attr = smap_get(®isterattrs, ti->attr);
|
|
|
|
if (!r->attr)
|
|
|
|
yyerror("'%s' doesn't seem to be a known register attribute", ti->attr);
|
|
|
|
}
|
|
|
|
|
2016-09-20 18:37:16 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* print - formatted output */
|
2016-10-15 16:38:46 +00:00
|
|
|
|
|
|
|
static void printto(FILE* fp, const char* fmt, va_list ap)
|
2016-09-20 19:00:16 +00:00
|
|
|
{
|
2016-10-15 16:38:46 +00:00
|
|
|
if (!fp)
|
|
|
|
return;
|
2016-09-20 18:37:16 +00:00
|
|
|
|
2016-09-20 19:00:16 +00:00
|
|
|
for (; *fmt; fmt++)
|
2016-09-20 18:37:16 +00:00
|
|
|
if (*fmt == '%')
|
2016-09-20 19:00:16 +00:00
|
|
|
switch (*++fmt)
|
|
|
|
{
|
|
|
|
case 'd':
|
2016-10-15 16:38:46 +00:00
|
|
|
fprintf(fp, "%d", va_arg(ap, int));
|
2016-09-20 19:00:16 +00:00
|
|
|
break;
|
2016-09-29 20:32:43 +00:00
|
|
|
|
|
|
|
case 'x':
|
2016-10-15 16:38:46 +00:00
|
|
|
fprintf(fp, "%x", va_arg(ap, uint32_t));
|
2016-09-29 20:32:43 +00:00
|
|
|
break;
|
|
|
|
|
2016-09-20 19:00:16 +00:00
|
|
|
case 's':
|
2016-10-15 16:38:46 +00:00
|
|
|
fputs(va_arg(ap, char*), fp);
|
2016-09-20 19:00:16 +00:00
|
|
|
break;
|
2016-09-29 20:32:43 +00:00
|
|
|
|
2016-09-20 19:00:16 +00:00
|
|
|
case 'P':
|
2016-10-15 16:38:46 +00:00
|
|
|
fprintf(fp, "%s_", prefix);
|
2016-09-20 19:00:16 +00:00
|
|
|
break;
|
2016-09-29 20:32:43 +00:00
|
|
|
|
2016-09-20 19:00:16 +00:00
|
|
|
case 'T':
|
|
|
|
{
|
|
|
|
Tree t = va_arg(ap, Tree);
|
|
|
|
print("%S", t->op);
|
|
|
|
if (t->left && t->right)
|
|
|
|
print("(%T,%T)", t->left, t->right);
|
|
|
|
else if (t->left)
|
|
|
|
print("(%T)", t->left);
|
|
|
|
break;
|
2016-09-20 18:37:16 +00:00
|
|
|
}
|
2016-09-29 20:32:43 +00:00
|
|
|
|
2016-09-20 19:00:16 +00:00
|
|
|
case 'R':
|
|
|
|
{
|
|
|
|
Rule r = va_arg(ap, Rule);
|
|
|
|
print("%S: %T", r->lhs, r->pattern);
|
|
|
|
break;
|
2016-09-20 18:37:16 +00:00
|
|
|
}
|
2016-09-29 20:32:43 +00:00
|
|
|
|
2016-09-20 19:00:16 +00:00
|
|
|
case 'S':
|
2016-10-15 16:38:46 +00:00
|
|
|
fputs(va_arg(ap, Term)->name, fp);
|
2016-09-20 19:00:16 +00:00
|
|
|
break;
|
2016-09-29 20:32:43 +00:00
|
|
|
|
2016-09-20 19:00:16 +00:00
|
|
|
case '1':
|
|
|
|
case '2':
|
|
|
|
case '3':
|
|
|
|
case '4':
|
|
|
|
case '5':
|
|
|
|
{
|
|
|
|
int n = *fmt - '0';
|
|
|
|
while (n-- > 0)
|
2016-10-15 16:38:46 +00:00
|
|
|
putc('\t', fp);
|
2016-09-20 19:00:16 +00:00
|
|
|
break;
|
2016-09-20 18:37:16 +00:00
|
|
|
}
|
2016-09-29 20:32:43 +00:00
|
|
|
|
2016-09-20 19:00:16 +00:00
|
|
|
default:
|
2016-10-15 16:38:46 +00:00
|
|
|
putc(*fmt, fp);
|
2016-09-20 19:00:16 +00:00
|
|
|
break;
|
2016-09-20 18:37:16 +00:00
|
|
|
}
|
|
|
|
else
|
2016-10-15 16:38:46 +00:00
|
|
|
putc(*fmt, fp);
|
2016-09-20 18:37:16 +00:00
|
|
|
}
|
|
|
|
|
2016-10-15 16:38:46 +00:00
|
|
|
static void print(const char* fmt, ...)
|
2016-09-20 22:43:10 +00:00
|
|
|
{
|
2016-10-15 16:38:46 +00:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
printto(outfp, fmt, ap);
|
|
|
|
va_end(ap);
|
2016-09-20 18:37:16 +00:00
|
|
|
}
|
|
|
|
|
2016-10-15 16:38:46 +00:00
|
|
|
static void printh(const char* fmt, ...)
|
2016-09-20 19:00:16 +00:00
|
|
|
{
|
2016-10-15 16:38:46 +00:00
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
printto(hdrfp, fmt, ap);
|
|
|
|
va_end(ap);
|
2016-09-20 18:37:16 +00:00
|
|
|
}
|
|
|
|
|
2016-10-05 19:00:28 +00:00
|
|
|
static void emitregisterattrs(void)
|
2016-09-25 20:17:14 +00:00
|
|
|
{
|
2016-10-01 10:17:14 +00:00
|
|
|
int i;
|
|
|
|
|
2016-09-25 20:17:14 +00:00
|
|
|
print("const char* %Pregister_class_names[] = {\n");
|
2016-10-05 19:00:28 +00:00
|
|
|
for (i=0; i<registerattrs.count; i++)
|
2016-09-25 20:17:14 +00:00
|
|
|
{
|
2016-10-05 19:00:28 +00:00
|
|
|
struct regattr* rc = registerattrs.item[i].right;
|
|
|
|
assert(rc->number == i);
|
2016-09-25 20:17:14 +00:00
|
|
|
|
|
|
|
print("%1\"%s\",\n", rc->name);
|
2016-10-15 16:38:46 +00:00
|
|
|
printh("#define %P%s_ATTR (1U<<%d)\n", rc->name, rc->number);
|
2016-09-25 20:17:14 +00:00
|
|
|
}
|
|
|
|
print("};\n\n");
|
2016-10-15 16:38:46 +00:00
|
|
|
printh("\n");
|
2016-09-25 20:17:14 +00:00
|
|
|
}
|
|
|
|
|
2016-10-01 10:17:14 +00:00
|
|
|
static void emitregisters(void)
|
2016-09-25 20:17:14 +00:00
|
|
|
{
|
2016-10-20 19:47:28 +00:00
|
|
|
int i, j;
|
|
|
|
|
|
|
|
for (i=0; i<registers.count; i++)
|
|
|
|
{
|
|
|
|
struct reg* r = registers.item[i].right;
|
|
|
|
|
|
|
|
print("const struct %Pregister_data* %Pregister_aliases_%d_%s[] = {\n%1", i, r->name);
|
|
|
|
for (j=0; j<r->aliases.count; j++)
|
|
|
|
print("&%Pregister_data[%d], ", r->aliases.item[j]->number);
|
|
|
|
print("NULL\n};\n");
|
|
|
|
}
|
2016-10-01 10:17:14 +00:00
|
|
|
|
2016-10-21 21:31:00 +00:00
|
|
|
for (i=0; i<registers.count; i++)
|
|
|
|
{
|
|
|
|
struct reg* r = registers.item[i].right;
|
|
|
|
|
|
|
|
print("const char* %Pregister_names_%d_%s[] = {\n%1", i, r->name);
|
|
|
|
if (r->names)
|
|
|
|
{
|
|
|
|
struct stringfragment* f = r->names->first;
|
|
|
|
while (f)
|
|
|
|
{
|
|
|
|
print("\"%s\", ", f->data);
|
|
|
|
f = f->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
print("\"%s\", ", r->name);
|
|
|
|
print("NULL\n};\n");
|
|
|
|
}
|
|
|
|
|
2016-09-29 20:32:43 +00:00
|
|
|
print("const struct %Pregister_data %Pregister_data[] = {\n");
|
2016-10-01 10:17:14 +00:00
|
|
|
for (i=0; i<registers.count; i++)
|
2016-09-25 20:17:14 +00:00
|
|
|
{
|
2016-10-01 10:17:14 +00:00
|
|
|
struct reg* r = registers.item[i].right;
|
2016-10-05 19:00:28 +00:00
|
|
|
assert(r->number == i);
|
2016-09-25 20:17:14 +00:00
|
|
|
|
2016-10-25 21:04:20 +00:00
|
|
|
print("%1{ \"%s\", 0x%x, %Pregister_names_%d_%s, %Pregister_aliases_%d_%s },\n",
|
|
|
|
r->name, r->attrs, i, r->name, i, r->name);
|
2016-09-25 20:17:14 +00:00
|
|
|
}
|
2016-10-05 19:00:28 +00:00
|
|
|
print("%1{ NULL }\n");
|
2016-09-25 20:17:14 +00:00
|
|
|
print("};\n\n");
|
|
|
|
}
|
|
|
|
|
2016-09-20 18:37:16 +00:00
|
|
|
/* emitcase - emit one case in function state */
|
2016-09-20 19:00:16 +00:00
|
|
|
static void emitcase(Term p, int ntnumber)
|
|
|
|
{
|
2016-09-20 18:37:16 +00:00
|
|
|
Rule r;
|
|
|
|
|
2016-09-24 20:46:08 +00:00
|
|
|
if (!p->rules)
|
|
|
|
return;
|
|
|
|
|
2016-09-20 18:37:16 +00:00
|
|
|
print("%1case %d: /* %S */\n", p->esn, p);
|
2016-09-20 19:00:16 +00:00
|
|
|
switch (p->arity)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
case -1:
|
2016-09-20 18:37:16 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
2016-09-20 19:00:16 +00:00
|
|
|
print("%2assert(l);\n");
|
2016-09-20 18:37:16 +00:00
|
|
|
break;
|
|
|
|
case 2:
|
2016-09-20 19:00:16 +00:00
|
|
|
print("%2assert(l && r);\n");
|
2016-09-20 18:37:16 +00:00
|
|
|
break;
|
2016-09-20 19:00:16 +00:00
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
for (r = p->rules; r; r = r->next)
|
|
|
|
{
|
|
|
|
switch (p->arity)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
case -1:
|
2016-09-24 11:33:59 +00:00
|
|
|
print("%2{%1/* %R */\n%3c = ", r);
|
|
|
|
emitcostcalc(r);
|
2016-09-20 19:00:16 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if (r->pattern->nterms > 1)
|
|
|
|
{
|
|
|
|
print("%2if (%1/* %R */\n", r);
|
|
|
|
emittest(r->pattern->left, "l", " ");
|
|
|
|
print("%2) {\n%3c = ");
|
|
|
|
}
|
|
|
|
else
|
2016-09-20 22:43:10 +00:00
|
|
|
{
|
2016-09-24 11:33:59 +00:00
|
|
|
print("%2{%1/* %R */\n%3c = ", r);
|
2016-09-20 22:43:10 +00:00
|
|
|
}
|
2016-09-24 11:33:59 +00:00
|
|
|
emitcostcalc(r);
|
2016-09-20 19:00:16 +00:00
|
|
|
emitcost(r->pattern->left, "l");
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if (r->pattern->nterms > 1)
|
|
|
|
{
|
|
|
|
print("%2if (%1/* %R */\n", r);
|
|
|
|
emittest(r->pattern->left, "l",
|
|
|
|
r->pattern->right->nterms ? " && " : " ");
|
|
|
|
emittest(r->pattern->right, "r", " ");
|
|
|
|
print("%2) {\n%3c = ");
|
|
|
|
}
|
|
|
|
else
|
2016-09-20 22:43:10 +00:00
|
|
|
{
|
2016-09-24 11:33:59 +00:00
|
|
|
print("%2{%1/* %R */\n%3c = ", r);
|
2016-09-20 22:43:10 +00:00
|
|
|
}
|
2016-09-24 11:33:59 +00:00
|
|
|
emitcostcalc(r);
|
2016-09-20 19:00:16 +00:00
|
|
|
emitcost(r->pattern->left, "l");
|
|
|
|
emitcost(r->pattern->right, "r");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
2016-09-20 18:37:16 +00:00
|
|
|
}
|
2016-10-01 11:56:52 +00:00
|
|
|
print("%d);\n", r->cost);
|
2016-09-20 18:37:16 +00:00
|
|
|
emitrecord("\t\t\t", r, 0);
|
|
|
|
print("%2}\n");
|
|
|
|
}
|
|
|
|
print("%2break;\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* emitclosure - emit the closure functions */
|
2016-09-20 19:00:16 +00:00
|
|
|
static void emitclosure(Nonterm nts)
|
|
|
|
{
|
2016-09-20 18:37:16 +00:00
|
|
|
Nonterm p;
|
|
|
|
|
|
|
|
for (p = nts; p; p = p->link)
|
|
|
|
if (p->chain)
|
|
|
|
print("static void %Pclosure_%S(struct %Pstate *, int);\n", p);
|
|
|
|
print("\n");
|
|
|
|
for (p = nts; p; p = p->link)
|
2016-09-20 19:00:16 +00:00
|
|
|
if (p->chain)
|
|
|
|
{
|
2016-09-20 18:37:16 +00:00
|
|
|
Rule r;
|
|
|
|
print("static void %Pclosure_%S(struct %Pstate *p, int c) {\n", p);
|
|
|
|
for (r = p->chain; r; r = r->chain)
|
|
|
|
emitrecord("\t", r, r->cost);
|
|
|
|
print("}\n\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* emitcost - emit cost computation for tree t */
|
2016-09-30 17:10:30 +00:00
|
|
|
static void emitcost(Tree t, const char* v)
|
2016-09-20 19:00:16 +00:00
|
|
|
{
|
2016-09-20 18:37:16 +00:00
|
|
|
Nonterm p = t->op;
|
|
|
|
|
2016-09-20 19:00:16 +00:00
|
|
|
if (p->kind == TERM)
|
|
|
|
{
|
2016-09-20 18:37:16 +00:00
|
|
|
if (t->left)
|
2016-09-30 17:10:30 +00:00
|
|
|
emitcost(t->left, aprintf("%s->left", v));
|
2016-09-20 18:37:16 +00:00
|
|
|
if (t->right)
|
2016-09-30 17:10:30 +00:00
|
|
|
emitcost(t->right, aprintf("%s->right", v));
|
2016-09-20 19:00:16 +00:00
|
|
|
}
|
|
|
|
else
|
2016-09-20 18:37:16 +00:00
|
|
|
print("%s->cost[%P%S_NT] + ", v, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* emitdefs - emit non-terminal defines and data structures */
|
2016-09-20 19:00:16 +00:00
|
|
|
static void emitdefs(Nonterm nts, int ntnumber)
|
|
|
|
{
|
2016-09-20 18:37:16 +00:00
|
|
|
Nonterm p;
|
|
|
|
|
2016-10-15 16:38:46 +00:00
|
|
|
printh("enum {\n");
|
2016-09-20 18:37:16 +00:00
|
|
|
for (p = nts; p; p = p->link)
|
2016-10-15 16:38:46 +00:00
|
|
|
printh("%1%P%S_NT = %d,\n", p, p->number);
|
|
|
|
printh("%1%Pmax_nt = %d\n", ntnumber);
|
|
|
|
printh("};\n\n");
|
|
|
|
|
2016-09-24 14:59:49 +00:00
|
|
|
print("const char *%Pntname[] = {\n%10,\n");
|
|
|
|
for (p = nts; p; p = p->link)
|
|
|
|
print("%1\"%S\",\n", p);
|
|
|
|
print("%10\n};\n\n");
|
2016-09-20 18:37:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* emitfuncs - emit functions to access node fields */
|
2016-09-20 19:00:16 +00:00
|
|
|
static void emitfuncs(void)
|
|
|
|
{
|
2016-09-20 18:37:16 +00:00
|
|
|
print("int %Pop_label(NODEPTR_TYPE p) {\n"
|
2016-09-20 19:00:16 +00:00
|
|
|
"%1%Passert(p, PANIC(\"NULL tree in %Pop_label\\n\"));\n"
|
|
|
|
"%1return OP_LABEL(p);\n}\n\n");
|
2016-09-20 18:37:16 +00:00
|
|
|
print("STATE_TYPE %Pstate_label(NODEPTR_TYPE p) {\n"
|
2016-09-20 19:00:16 +00:00
|
|
|
"%1%Passert(p, PANIC(\"NULL tree in %Pstate_label\\n\"));\n"
|
|
|
|
"%1return STATE_LABEL(p);\n}\n\n");
|
2016-09-20 18:37:16 +00:00
|
|
|
print("NODEPTR_TYPE %Pchild(NODEPTR_TYPE p, int index) {\n"
|
2016-09-20 19:00:16 +00:00
|
|
|
"%1%Passert(p, PANIC(\"NULL tree in %Pchild\\n\"));\n"
|
|
|
|
"%1switch (index) {\n%1case 0:%1return LEFT_CHILD(p);\n"
|
|
|
|
"%1case 1:%1return RIGHT_CHILD(p);\n%1}\n"
|
|
|
|
"%1%Passert(0, PANIC(\"Bad index %%d in %Pchild\\n\", index));\n%1return 0;\n}\n\n");
|
2016-09-20 18:37:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* emitheader - emit initial definitions */
|
2016-09-20 19:00:16 +00:00
|
|
|
static void emitheader(void)
|
|
|
|
{
|
2016-09-24 15:20:40 +00:00
|
|
|
print("#include \"mcgg_generated_header.h\"\n");
|
2016-09-20 18:37:16 +00:00
|
|
|
if (Tflag)
|
|
|
|
print("static NODEPTR_TYPE %Pnp;\n\n");
|
2016-10-15 16:38:46 +00:00
|
|
|
|
|
|
|
printh("#ifndef MCG_DEFS_H\n");
|
|
|
|
printh("#define MCG_DEFS_H\n\n");
|
2016-09-20 18:37:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* computekids - compute paths to kids in tree t */
|
2016-10-03 22:16:06 +00:00
|
|
|
static char* computekids(Tree node, const char* v, char* bp, int* ip)
|
2016-09-20 19:00:16 +00:00
|
|
|
{
|
2016-10-03 22:16:06 +00:00
|
|
|
Term t = node->op;
|
2016-09-20 18:37:16 +00:00
|
|
|
|
2016-10-03 22:16:06 +00:00
|
|
|
if (!node->left && !node->right)
|
2016-09-20 19:00:16 +00:00
|
|
|
{
|
2016-09-20 18:37:16 +00:00
|
|
|
sprintf(bp, "\t\tkids[%d] = %s;\n", (*ip)++, v);
|
|
|
|
bp += strlen(bp);
|
2016-09-20 19:00:16 +00:00
|
|
|
}
|
2016-10-03 22:16:06 +00:00
|
|
|
|
|
|
|
if (t->kind == TERM)
|
2016-09-20 19:00:16 +00:00
|
|
|
{
|
2016-10-03 22:16:06 +00:00
|
|
|
if (t->arity >= 1)
|
|
|
|
bp = computekids(node->left, aprintf("LEFT_CHILD(%s)", v), bp, ip);
|
|
|
|
if (t->arity == 2)
|
|
|
|
bp = computekids(node->right, aprintf("RIGHT_CHILD(%s)", v), bp, ip);
|
2016-09-20 18:37:16 +00:00
|
|
|
}
|
|
|
|
return bp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* emitkids - emit burm_kids */
|
2016-09-20 19:00:16 +00:00
|
|
|
static void emitkids(Rule rules, int nrules)
|
|
|
|
{
|
2016-09-20 18:37:16 +00:00
|
|
|
int i;
|
2016-09-20 22:43:10 +00:00
|
|
|
Rule r, * rc = calloc(nrules+1, sizeof *rc);
|
|
|
|
char** str = calloc(nrules+1, sizeof *str);
|
2016-09-20 18:37:16 +00:00
|
|
|
|
2016-09-20 19:00:16 +00:00
|
|
|
for (i = 0, r = rules; r; r = r->link)
|
|
|
|
{
|
2016-09-20 18:37:16 +00:00
|
|
|
int j = 0;
|
2016-09-20 19:00:16 +00:00
|
|
|
char buf[1024], * bp = buf;
|
2016-09-20 18:37:16 +00:00
|
|
|
*computekids(r->pattern, "p", bp, &j) = 0;
|
|
|
|
for (j = 0; str[j] && strcmp(str[j], buf); j++)
|
|
|
|
;
|
|
|
|
if (str[j] == NULL)
|
2016-09-20 22:43:10 +00:00
|
|
|
str[j] = strdup(buf);
|
2016-09-20 18:37:16 +00:00
|
|
|
r->kids = rc[j];
|
|
|
|
rc[j] = r;
|
|
|
|
}
|
|
|
|
print("NODEPTR_TYPE *%Pkids(NODEPTR_TYPE p, int eruleno, NODEPTR_TYPE kids[]) {\n"
|
2016-09-20 19:00:16 +00:00
|
|
|
"%1%Passert(p, PANIC(\"NULL tree in %Pkids\\n\"));\n"
|
|
|
|
"%1%Passert(kids, PANIC(\"NULL kids in %Pkids\\n\"));\n"
|
|
|
|
"%1switch (eruleno) {\n");
|
|
|
|
for (i = 0; r = rc[i]; i++)
|
|
|
|
{
|
|
|
|
for (; r; r = r->kids)
|
2016-09-20 18:37:16 +00:00
|
|
|
print("%1case %d: /* %R */\n", r->ern, r);
|
|
|
|
print("%s%2break;\n", str[i]);
|
|
|
|
}
|
|
|
|
print("%1default:\n%2%Passert(0, PANIC(\"Bad external rule number %%d in %Pkids\\n\", eruleno));\n%1}\n%1return kids;\n}\n\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* emitlabel - emit the labelling functions */
|
2016-09-20 19:00:16 +00:00
|
|
|
static void emitlabel(Nonterm start)
|
|
|
|
{
|
2016-09-20 18:37:16 +00:00
|
|
|
print("static void %Plabel1(NODEPTR_TYPE p) {\n"
|
2016-09-20 19:00:16 +00:00
|
|
|
"%1%Passert(p, PANIC(\"NULL tree in %Plabel\\n\"));\n"
|
|
|
|
"%1switch (%Parity[OP_LABEL(p)]) {\n"
|
|
|
|
"%1case 0:\n");
|
2016-09-20 18:37:16 +00:00
|
|
|
if (Tflag)
|
|
|
|
print("%2%Pnp = p;\n");
|
2016-09-20 22:43:10 +00:00
|
|
|
print("%2STATE_LABEL(p) = %Pstate(p, 0, 0);\n%2break;\n"
|
2016-09-20 19:00:16 +00:00
|
|
|
"%1case 1:\n%2%Plabel1(LEFT_CHILD(p));\n");
|
2016-09-20 18:37:16 +00:00
|
|
|
if (Tflag)
|
|
|
|
print("%2%Pnp = p;\n");
|
2016-09-20 22:43:10 +00:00
|
|
|
print("%2STATE_LABEL(p) = %Pstate(p,\n"
|
2016-09-20 19:00:16 +00:00
|
|
|
"%3STATE_LABEL(LEFT_CHILD(p)), 0);\n%2break;\n"
|
|
|
|
"%1case 2:\n%2%Plabel1(LEFT_CHILD(p));\n%2%Plabel1(RIGHT_CHILD(p));\n");
|
2016-09-20 18:37:16 +00:00
|
|
|
if (Tflag)
|
|
|
|
print("%2%Pnp = p;\n");
|
2016-09-20 22:43:10 +00:00
|
|
|
print("%2STATE_LABEL(p) = %Pstate(p,\n"
|
2016-09-20 19:00:16 +00:00
|
|
|
"%3STATE_LABEL(LEFT_CHILD(p)),\n%3STATE_LABEL(RIGHT_CHILD(p)));\n%2break;\n"
|
|
|
|
"%1}\n}\n\n");
|
2016-09-20 18:37:16 +00:00
|
|
|
print(
|
2016-09-20 19:00:16 +00:00
|
|
|
"STATE_TYPE %Plabel(NODEPTR_TYPE p) {\n%1%Plabel1(p);\n"
|
|
|
|
"%1return ((struct %Pstate *)STATE_LABEL(p))->rule.%P%S ? STATE_LABEL(p) : 0;\n"
|
|
|
|
"}\n\n",
|
|
|
|
start);
|
2016-09-20 18:37:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* closure - fill in cost & rule with results of chain rules w/p as rhs */
|
2016-09-20 19:00:16 +00:00
|
|
|
static void closure(int cost[], Rule rule[], Nonterm p, int c)
|
|
|
|
{
|
2016-09-20 18:37:16 +00:00
|
|
|
Rule r;
|
|
|
|
|
|
|
|
for (r = p->chain; r; r = r->chain)
|
2016-09-20 19:00:16 +00:00
|
|
|
if (c + r->cost < cost[r->lhs->number])
|
|
|
|
{
|
2016-09-20 18:37:16 +00:00
|
|
|
cost[r->lhs->number] = c + r->cost;
|
|
|
|
rule[r->lhs->number] = r;
|
|
|
|
closure(cost, rule, r->lhs, c + r->cost);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* computents - fill in bp with burm_nts vector for tree t */
|
2016-09-20 19:00:16 +00:00
|
|
|
static char* computents(Tree t, char* bp)
|
|
|
|
{
|
|
|
|
if (t)
|
|
|
|
{
|
2016-09-20 18:37:16 +00:00
|
|
|
Nonterm p = t->op;
|
2016-10-04 19:32:28 +00:00
|
|
|
if (!t->left && !t->right)
|
2016-09-20 19:00:16 +00:00
|
|
|
{
|
2016-10-04 19:32:28 +00:00
|
|
|
if (p->kind == NONTERM)
|
|
|
|
sprintf(bp, "%s_%s_NT, ", prefix, p->name);
|
|
|
|
else
|
|
|
|
sprintf(bp, "0, ");
|
2016-09-20 18:37:16 +00:00
|
|
|
bp += strlen(bp);
|
2016-09-20 19:00:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
bp = computents(t->right, computents(t->left, bp));
|
2016-09-20 18:37:16 +00:00
|
|
|
}
|
|
|
|
return bp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* emitnts - emit burm_nts ragged array */
|
2016-09-20 19:00:16 +00:00
|
|
|
static void emitnts(Rule rules, int nrules)
|
|
|
|
{
|
2016-09-20 18:37:16 +00:00
|
|
|
Rule r;
|
2016-09-20 22:43:10 +00:00
|
|
|
int i, j, * nts = calloc(nrules, sizeof *nts);
|
|
|
|
char** str = calloc(nrules, sizeof *str);
|
2016-09-20 18:37:16 +00:00
|
|
|
|
2016-09-20 19:00:16 +00:00
|
|
|
for (i = 0, r = rules; r; r = r->link)
|
|
|
|
{
|
2016-09-20 18:37:16 +00:00
|
|
|
char buf[1024];
|
|
|
|
*computents(r->pattern, buf) = 0;
|
|
|
|
for (j = 0; str[j] && strcmp(str[j], buf); j++)
|
|
|
|
;
|
2016-09-20 19:00:16 +00:00
|
|
|
if (str[j] == NULL)
|
|
|
|
{
|
2016-09-24 14:59:49 +00:00
|
|
|
print("static const short %Pnts_%d[] = { %s0 };\n", j, buf);
|
2016-09-20 22:43:10 +00:00
|
|
|
str[j] = strdup(buf);
|
2016-09-20 18:37:16 +00:00
|
|
|
}
|
|
|
|
nts[i++] = j;
|
|
|
|
}
|
2016-09-24 14:59:49 +00:00
|
|
|
print("\nconst short *%Pnts[] = {\n");
|
2016-09-20 19:00:16 +00:00
|
|
|
for (i = j = 0, r = rules; r; r = r->link)
|
|
|
|
{
|
|
|
|
for (; j < r->ern; j++)
|
2016-09-20 18:37:16 +00:00
|
|
|
print("%10,%1/* %d */\n", j);
|
|
|
|
print("%1%Pnts_%d,%1/* %d */\n", nts[i++], j++);
|
|
|
|
}
|
|
|
|
print("};\n\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* emitrecord - emit code that tests for a winning match of rule r */
|
2016-09-20 19:00:16 +00:00
|
|
|
static void emitrecord(char* pre, Rule r, int cost)
|
|
|
|
{
|
2016-09-20 18:37:16 +00:00
|
|
|
print("%sif (", pre);
|
|
|
|
if (Tflag)
|
|
|
|
print("%Ptrace(%Pnp, %d, c + %d, p->cost[%P%S_NT]), ",
|
2016-09-20 19:00:16 +00:00
|
|
|
r->ern, cost, r->lhs);
|
2016-09-20 18:37:16 +00:00
|
|
|
print("c + %d < p->cost[%P%S_NT]) {\n"
|
2016-09-20 19:00:16 +00:00
|
|
|
"%s%1p->cost[%P%S_NT] = c + %d;\n%s%1p->rule.%P%S = %d;\n",
|
|
|
|
cost, r->lhs, pre, r->lhs, cost, pre, r->lhs,
|
|
|
|
r->packed);
|
2016-09-20 18:37:16 +00:00
|
|
|
if (r->lhs->chain)
|
|
|
|
print("%s%1%Pclosure_%S(p, c + %d);\n", pre, r->lhs, cost);
|
|
|
|
print("%s}\n", pre);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* emitrule - emit decoding vectors and burm_rule */
|
2016-09-20 19:00:16 +00:00
|
|
|
static void emitrule(Nonterm nts)
|
|
|
|
{
|
2016-09-20 18:37:16 +00:00
|
|
|
Nonterm p;
|
|
|
|
|
2016-09-20 19:00:16 +00:00
|
|
|
for (p = nts; p; p = p->link)
|
|
|
|
{
|
2016-09-20 18:37:16 +00:00
|
|
|
Rule r;
|
2016-09-24 14:59:49 +00:00
|
|
|
print("static const short %Pdecode_%S[] = {\n%10,\n", p);
|
2016-09-20 18:37:16 +00:00
|
|
|
for (r = p->rules; r; r = r->decode)
|
|
|
|
print("%1%d,\n", r->ern);
|
|
|
|
print("};\n\n");
|
|
|
|
}
|
|
|
|
print("int %Prule(STATE_TYPE state, int goalnt) {\n"
|
2016-09-20 19:00:16 +00:00
|
|
|
"%1%Passert(goalnt >= 1 && goalnt <= %d, PANIC(\"Bad goal nonterminal %%d in %Prule\\n\", goalnt));\n"
|
|
|
|
"%1if (!state)\n%2return 0;\n%1switch (goalnt) {\n",
|
|
|
|
ntnumber);
|
2016-09-20 18:37:16 +00:00
|
|
|
for (p = nts; p; p = p->link)
|
|
|
|
print("%1case %P%S_NT:"
|
2016-09-20 19:00:16 +00:00
|
|
|
"%1return %Pdecode_%S[((struct %Pstate *)state)->rule.%P%S];\n",
|
|
|
|
p, p, p);
|
2016-09-20 18:37:16 +00:00
|
|
|
print("%1default:\n%2%Passert(0, PANIC(\"Bad goal nonterminal %%d in %Prule\\n\", goalnt));\n%1}\n%1return 0;\n}\n\n");
|
|
|
|
}
|
|
|
|
|
2016-09-25 15:14:54 +00:00
|
|
|
static void print_path(uint32_t path)
|
2016-09-25 09:49:51 +00:00
|
|
|
{
|
2016-09-25 15:14:54 +00:00
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
while (path > 0)
|
2016-09-25 09:49:51 +00:00
|
|
|
{
|
2016-09-25 15:14:54 +00:00
|
|
|
switch (path % 3)
|
2016-09-25 09:49:51 +00:00
|
|
|
{
|
2016-09-25 15:14:54 +00:00
|
|
|
case 1: print("LEFT_CHILD("); break;
|
|
|
|
case 2: print("RIGHT_CHILD("); break;
|
2016-09-25 09:49:51 +00:00
|
|
|
}
|
2016-09-25 15:14:54 +00:00
|
|
|
path /= 3;
|
|
|
|
i++;
|
|
|
|
}
|
2016-09-25 09:49:51 +00:00
|
|
|
|
2016-09-25 15:14:54 +00:00
|
|
|
print("node");
|
2016-09-25 09:49:51 +00:00
|
|
|
|
2016-09-25 15:14:54 +00:00
|
|
|
while (i > 0)
|
|
|
|
{
|
|
|
|
print(")");
|
|
|
|
i--;
|
2016-09-25 09:49:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-25 15:14:54 +00:00
|
|
|
static const uint32_t PATH_MISSING = 0xffffffff;
|
|
|
|
|
2016-09-29 20:06:04 +00:00
|
|
|
static uint32_t find_label(Tree root, const char* name, uint32_t path, Tree* found)
|
2016-09-25 09:49:51 +00:00
|
|
|
{
|
2016-09-25 15:14:54 +00:00
|
|
|
uint32_t p;
|
2016-09-25 09:49:51 +00:00
|
|
|
|
|
|
|
if (root->label && (strcmp(root->label, name) == 0))
|
2016-09-29 20:06:04 +00:00
|
|
|
{
|
|
|
|
if (found)
|
|
|
|
*found = root;
|
2016-09-25 15:14:54 +00:00
|
|
|
return path;
|
2016-09-29 20:06:04 +00:00
|
|
|
}
|
2016-09-25 09:49:51 +00:00
|
|
|
|
2016-09-25 15:14:54 +00:00
|
|
|
p = PATH_MISSING;
|
|
|
|
if (root->left && (p == PATH_MISSING))
|
2016-09-29 20:06:04 +00:00
|
|
|
p = find_label(root->left, name, path*3 + 1, found);
|
2016-09-25 15:14:54 +00:00
|
|
|
if (root->right && (p == PATH_MISSING))
|
2016-09-29 20:06:04 +00:00
|
|
|
p = find_label(root->right, name, path*3 + 2, found);
|
2016-09-25 15:14:54 +00:00
|
|
|
return p;
|
2016-09-25 09:49:51 +00:00
|
|
|
}
|
|
|
|
|
2016-09-25 20:17:14 +00:00
|
|
|
static void label_not_found(Rule rule, const char* label)
|
|
|
|
{
|
|
|
|
yylineno = rule->lineno;
|
|
|
|
yyerror("label '%s' not found", label);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2016-10-03 22:16:06 +00:00
|
|
|
static bool find_child_index(Tree node, const char* name, int* index, Tree* found)
|
|
|
|
{
|
|
|
|
/* This must return the same ordering as the burm_kids() function uses. */
|
|
|
|
|
|
|
|
if (node->label && strcmp(node->label, name) == 0)
|
|
|
|
{
|
|
|
|
if (found)
|
|
|
|
*found = node;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!node->left && !node->right)
|
|
|
|
(*index)++;
|
|
|
|
|
|
|
|
if (node->left && find_child_index(node->left, name, index, found))
|
|
|
|
return true;
|
|
|
|
if (node->right && find_child_index(node->right, name, index, found))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
2017-08-06 10:42:17 +00:00
|
|
|
|
2016-10-08 09:35:33 +00:00
|
|
|
static void emit_predicate_expr(Rule r, struct expr* p)
|
|
|
|
{
|
|
|
|
bool first = true;
|
|
|
|
|
2016-10-09 10:32:36 +00:00
|
|
|
assert(p->type == PREDICATE_FUNCTION);
|
|
|
|
print("%1if (%Ppredicate_%s(", p->u.name);
|
2016-10-08 09:35:33 +00:00
|
|
|
|
|
|
|
p = p->next;
|
|
|
|
while (p)
|
|
|
|
{
|
|
|
|
if (!first)
|
|
|
|
print(", ");
|
|
|
|
else
|
|
|
|
first = false;
|
|
|
|
|
2016-10-09 10:32:36 +00:00
|
|
|
switch (p->type)
|
|
|
|
{
|
|
|
|
case PREDICATE_NODE:
|
|
|
|
{
|
|
|
|
uint32_t path = find_label(r->pattern, p->u.name, 0, NULL);
|
|
|
|
if (path == PATH_MISSING)
|
|
|
|
label_not_found(r, p->u.name);
|
|
|
|
|
|
|
|
print_path(path);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case PREDICATE_NUMBER:
|
|
|
|
{
|
|
|
|
print("%d", p->u.number);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-08 09:35:33 +00:00
|
|
|
p = p->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
print("))");
|
|
|
|
}
|
|
|
|
|
2016-10-01 11:56:52 +00:00
|
|
|
/* emitpredicates - emit predicates for rules */
|
|
|
|
static void emitpredicatedefinitions(Rule r)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
while (r)
|
|
|
|
{
|
|
|
|
print("/* %R */\n", r);
|
|
|
|
print("static int %Padjust_cost_%d(NODEPTR_TYPE node, int cost) {\n", r->ern);
|
|
|
|
|
|
|
|
for (i=0; i<r->prefers.count; i++)
|
|
|
|
{
|
2016-10-08 09:35:33 +00:00
|
|
|
emit_predicate_expr(r, r->prefers.item[i]);
|
|
|
|
print(" cost -= 1;\n");
|
|
|
|
}
|
2016-10-01 11:56:52 +00:00
|
|
|
|
2016-10-08 09:35:33 +00:00
|
|
|
for (i=0; i<r->requires.count; i++)
|
|
|
|
{
|
|
|
|
emit_predicate_expr(r, r->requires.item[i]);
|
|
|
|
print(" {} else return %d;\n", maxcost);
|
2016-10-01 11:56:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
print("%1if (cost > %d) return %d;\n", maxcost, maxcost);
|
|
|
|
print("%1if (cost < 1) return 1;\n");
|
|
|
|
print("%1return cost;\n");
|
|
|
|
print("}\n\n");
|
|
|
|
r = r->link;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-05 20:56:25 +00:00
|
|
|
static void emit_input_regs(Tree node, int* index)
|
|
|
|
{
|
|
|
|
/* This must return the same ordering as the burm_kids() function uses. */
|
|
|
|
|
|
|
|
Nonterm nt = node->op;
|
|
|
|
if ((nt->kind == NONTERM) && !nt->is_fragment && !node->left && !node->right)
|
|
|
|
{
|
2016-10-09 13:09:34 +00:00
|
|
|
if (node->attr)
|
|
|
|
{
|
|
|
|
uint32_t attr = 1<<node->attr->number;
|
|
|
|
print("%1data->constrain_input_reg(%d, 0x%x /* %s */);\n",
|
|
|
|
*index, attr, node->attr->name);
|
|
|
|
}
|
2016-10-05 20:56:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!node->left && !node->right)
|
|
|
|
(*index)++;
|
|
|
|
|
|
|
|
if (node->left)
|
|
|
|
emit_input_regs(node->left, index);
|
|
|
|
if (node->right)
|
|
|
|
emit_input_regs(node->right, index);
|
|
|
|
}
|
|
|
|
|
2016-10-14 20:17:02 +00:00
|
|
|
static void emit_output_constraints(Rule r)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct constraint* outputc = NULL;
|
|
|
|
|
|
|
|
for (i=0; i<r->constraints.count; i++)
|
|
|
|
{
|
|
|
|
struct constraint* c = r->constraints.item[i];
|
|
|
|
|
|
|
|
if (c->type == CONSTRAINT_EQUALS)
|
|
|
|
{
|
|
|
|
if (strcmp(c->left, r->label) != 0)
|
|
|
|
yyerror("equality register constraints must have an output register on the left hand side");
|
|
|
|
if (outputc != NULL)
|
|
|
|
yyerror("you can't specify more than one output register constraint");
|
|
|
|
outputc = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (outputc)
|
|
|
|
{
|
|
|
|
int index = 0;
|
|
|
|
|
|
|
|
if (!find_child_index(r->pattern, outputc->right, &index, NULL))
|
|
|
|
label_not_found(r, outputc->right);
|
|
|
|
|
|
|
|
print("%1data->constrain_output_reg_equal_to(%d);\n", index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-29 18:22:44 +00:00
|
|
|
static void emit_input_constraints(Rule r)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i=0; i<r->constraints.count; i++)
|
|
|
|
{
|
|
|
|
int index;
|
|
|
|
struct constraint* c = r->constraints.item[i];
|
|
|
|
|
|
|
|
if (c->type == CONSTRAINT_PRESERVED)
|
|
|
|
{
|
|
|
|
if (strcmp(c->left, r->label) == 0)
|
|
|
|
yyerror("cannot preserve an output register!");
|
|
|
|
|
|
|
|
index = 0;
|
|
|
|
if (!find_child_index(r->pattern, c->left, &index, NULL))
|
|
|
|
label_not_found(r, c->left);
|
|
|
|
|
|
|
|
print("%1data->constrain_input_reg_preserved(%d);\n", index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-25 15:14:54 +00:00
|
|
|
/* emitinsndata - emit the code generation data */
|
|
|
|
static void emitinsndata(Rule rules)
|
2016-09-25 09:49:51 +00:00
|
|
|
{
|
2016-09-25 15:14:54 +00:00
|
|
|
int k;
|
2016-09-25 09:49:51 +00:00
|
|
|
Rule r;
|
|
|
|
|
|
|
|
r = rules;
|
|
|
|
while (r)
|
|
|
|
{
|
2016-09-25 10:18:39 +00:00
|
|
|
struct stringfragment* f = r->code.first;
|
2016-10-03 22:16:06 +00:00
|
|
|
yylineno = r->lineno;
|
2016-10-02 21:25:54 +00:00
|
|
|
|
|
|
|
if (!f)
|
|
|
|
{
|
|
|
|
/* This instruction has no code; make sure it's not a fragment. */
|
|
|
|
if (r->lhs->is_fragment)
|
|
|
|
{
|
|
|
|
yylineno = r->lineno;
|
|
|
|
yyerror("rule is a fragment, but doesn't emit anything");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
print("/* %R */\n", r);
|
2016-10-03 22:16:06 +00:00
|
|
|
print("static void %Pemitter_%d(const struct %Pemitter_data* data) {\n", r->ern);
|
2016-09-25 15:14:54 +00:00
|
|
|
|
2016-10-05 20:56:25 +00:00
|
|
|
if (r->attr)
|
2016-10-09 13:09:34 +00:00
|
|
|
print("%1data->constrain_output_reg(0x%x /* %s */);\n",
|
|
|
|
1<<r->attr->number, r->attr->name);
|
2016-10-05 20:56:25 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
int index = 0;
|
|
|
|
emit_input_regs(r->pattern, &index);
|
|
|
|
}
|
2017-08-06 10:42:17 +00:00
|
|
|
|
2016-10-14 20:17:02 +00:00
|
|
|
emit_output_constraints(r);
|
2016-10-29 18:22:44 +00:00
|
|
|
emit_input_constraints(r);
|
2017-08-06 10:42:17 +00:00
|
|
|
|
2016-10-02 21:25:54 +00:00
|
|
|
while (f)
|
|
|
|
{
|
2016-10-21 21:31:00 +00:00
|
|
|
char* data = strdup(f->data);
|
|
|
|
int type = *data++;
|
|
|
|
char* label = strtok(data, ".");
|
|
|
|
char* nameindex_s = strtok(NULL, ".");
|
|
|
|
int nameindex = nameindex_s ? atoi(nameindex_s) : 0;
|
|
|
|
|
|
|
|
switch (type)
|
2016-10-02 21:25:54 +00:00
|
|
|
{
|
|
|
|
case '%':
|
|
|
|
{
|
2016-10-05 20:56:25 +00:00
|
|
|
if (r->label && (strcmp(label, r->label) == 0))
|
2016-10-21 21:31:00 +00:00
|
|
|
print("%1data->emit_return_reg(%d);\n", nameindex);
|
2016-10-02 21:25:54 +00:00
|
|
|
else
|
2016-09-25 20:17:14 +00:00
|
|
|
{
|
2016-10-02 21:25:54 +00:00
|
|
|
Tree node;
|
2016-10-03 22:16:06 +00:00
|
|
|
int index = 0;
|
2017-08-06 10:42:17 +00:00
|
|
|
Nonterm nt;
|
|
|
|
|
2016-10-03 22:16:06 +00:00
|
|
|
if (!find_child_index(r->pattern, label, &index, &node))
|
2016-09-25 20:17:14 +00:00
|
|
|
label_not_found(r, label);
|
2017-11-08 04:52:52 +00:00
|
|
|
nt = node->op;
|
2016-10-03 22:16:06 +00:00
|
|
|
if (nt->kind == NONTERM)
|
|
|
|
{
|
|
|
|
if (nt->is_fragment)
|
2016-10-21 21:31:00 +00:00
|
|
|
print("%1data->emit_fragment(%d);\n", index);
|
2016-10-03 22:16:06 +00:00
|
|
|
else
|
2016-10-21 21:31:00 +00:00
|
|
|
print("%1data->emit_reg(%d, %d);\n", index, nameindex);
|
2016-10-03 22:16:06 +00:00
|
|
|
}
|
2016-10-02 21:25:54 +00:00
|
|
|
else
|
2016-10-21 21:31:00 +00:00
|
|
|
print("%1data->emit_reg(%d, %d);\n", index, nameindex);
|
2016-09-25 09:49:51 +00:00
|
|
|
}
|
2016-10-02 21:25:54 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-09-25 10:18:39 +00:00
|
|
|
|
2016-10-02 21:25:54 +00:00
|
|
|
case '$':
|
|
|
|
{
|
2016-10-03 22:16:06 +00:00
|
|
|
int index = 0;
|
|
|
|
if (!find_child_index(r->pattern, label, &index, NULL))
|
2016-10-02 21:25:54 +00:00
|
|
|
label_not_found(r, label);
|
2016-10-03 22:16:06 +00:00
|
|
|
|
2016-10-21 21:31:00 +00:00
|
|
|
if (nameindex != 0)
|
|
|
|
yyerror("indices other than 0 make no sense for $-values");
|
|
|
|
|
2016-10-03 22:16:06 +00:00
|
|
|
print("%1data->emit_value(%d);\n", index);
|
2016-10-02 21:25:54 +00:00
|
|
|
break;
|
2016-09-25 09:49:51 +00:00
|
|
|
}
|
|
|
|
|
2016-10-02 21:25:54 +00:00
|
|
|
case '\n':
|
|
|
|
assert(f->data[1] == 0);
|
|
|
|
print("%1data->emit_eoi();\n");
|
|
|
|
break;
|
2017-08-06 10:42:17 +00:00
|
|
|
|
2016-10-02 21:25:54 +00:00
|
|
|
default:
|
|
|
|
print("%1data->emit_string(\"%s\");\n", f->data);
|
2016-09-25 09:49:51 +00:00
|
|
|
}
|
|
|
|
|
2016-10-02 21:25:54 +00:00
|
|
|
f = f->next;
|
2016-09-29 20:14:11 +00:00
|
|
|
}
|
2016-09-25 09:49:51 +00:00
|
|
|
|
2016-10-02 21:25:54 +00:00
|
|
|
print("}\n\n");
|
2016-09-25 09:49:51 +00:00
|
|
|
r = r->link;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = rules;
|
2016-09-25 15:14:54 +00:00
|
|
|
print("const struct %Pinstruction_data %Pinstruction_data[] = {\n");
|
|
|
|
k = 0;
|
2016-09-25 09:49:51 +00:00
|
|
|
while (r)
|
|
|
|
{
|
2016-09-25 15:14:54 +00:00
|
|
|
for (; k < r->ern; k++)
|
|
|
|
print("%1{ 0 }, /* %d */\n", k);
|
|
|
|
k++;
|
2016-09-25 10:18:39 +00:00
|
|
|
|
2016-09-25 15:14:54 +00:00
|
|
|
print("%1{ /* %d: %R */\n", r->ern, r);
|
|
|
|
|
|
|
|
print("%2\"%R\",\n", r);
|
|
|
|
|
2016-10-02 21:25:54 +00:00
|
|
|
print("%2&%Pemitter_%d,\n", r->ern);
|
2016-09-25 09:49:51 +00:00
|
|
|
|
2016-09-25 20:17:14 +00:00
|
|
|
print("%2%s,\n", r->lhs->is_fragment ? "true" : "false");
|
2016-09-25 15:14:54 +00:00
|
|
|
|
2016-10-14 23:15:08 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
uint32_t attrs = 0;
|
|
|
|
|
|
|
|
for (i=0; i<r->constraints.count; i++)
|
|
|
|
{
|
|
|
|
struct constraint* c = r->constraints.item[i];
|
|
|
|
|
|
|
|
if (c->type == CONSTRAINT_CORRUPTED_ATTR)
|
|
|
|
{
|
|
|
|
struct regattr* p = smap_get(®isterattrs, c->left);
|
|
|
|
if (!p)
|
|
|
|
yyerror("no such register attribute '%s'", c->left);
|
|
|
|
|
|
|
|
attrs |= 1<<(p->number);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
print("%2%d, /* corruption attrs */\n", attrs);
|
|
|
|
}
|
|
|
|
|
2016-09-25 15:14:54 +00:00
|
|
|
print("%1},\n");
|
2016-09-25 09:49:51 +00:00
|
|
|
r = r->link;
|
|
|
|
}
|
|
|
|
print("};\n\n");
|
|
|
|
}
|
|
|
|
|
2016-09-24 11:33:59 +00:00
|
|
|
/* emitcost - emit a cost calculation via a predicate */
|
|
|
|
static void emitcostcalc(Rule r)
|
2016-09-20 22:43:10 +00:00
|
|
|
{
|
2016-10-01 11:56:52 +00:00
|
|
|
print("%Padjust_cost_%d(node, ", r->ern);
|
2016-09-20 22:43:10 +00:00
|
|
|
}
|
|
|
|
|
2016-09-20 18:37:16 +00:00
|
|
|
/* emitstate - emit state function */
|
2016-09-20 19:00:16 +00:00
|
|
|
static void emitstate(Term terms, Nonterm start, int ntnumber)
|
|
|
|
{
|
2016-09-20 18:37:16 +00:00
|
|
|
int i;
|
|
|
|
Term p;
|
|
|
|
|
2016-09-20 22:43:10 +00:00
|
|
|
print("STATE_TYPE %Pstate(NODEPTR_TYPE node, STATE_TYPE left, STATE_TYPE right) {\n%1int c;\n"
|
|
|
|
"%1int op = OP_LABEL(node);\n"
|
|
|
|
"%1struct %Pstate* p;\n"
|
|
|
|
"%1struct %Pstate* l = (struct %Pstate *)left;\n"
|
|
|
|
"%1struct %Pstate* r = (struct %Pstate *)right;\n"
|
|
|
|
"\n"
|
|
|
|
"%1assert(sizeof (STATE_TYPE) >= sizeof (void *));\n%1");
|
2016-09-24 15:20:40 +00:00
|
|
|
print("%1p = malloc(sizeof *p);\n"
|
2016-09-24 11:33:59 +00:00
|
|
|
"%1p->op = op;\n"
|
|
|
|
"%1p->left = l;\n"
|
|
|
|
"%1p->right = r;\n"
|
|
|
|
"%1p->rule.%P%S = 0;\n",
|
2016-09-20 19:00:16 +00:00
|
|
|
start);
|
2016-09-20 18:37:16 +00:00
|
|
|
for (i = 1; i <= ntnumber; i++)
|
2016-09-24 11:33:59 +00:00
|
|
|
print("%1p->cost[%d] =\n", i);
|
|
|
|
print("%2%d;\n"
|
|
|
|
"%1switch (op) {\n", maxcost);
|
2016-09-20 18:37:16 +00:00
|
|
|
for (p = terms; p; p = p->link)
|
|
|
|
emitcase(p, ntnumber);
|
|
|
|
print("%1default:\n"
|
2016-09-24 20:46:08 +00:00
|
|
|
"%2%Ppanic_cannot_match(node);\n"
|
|
|
|
"%1}\n"
|
2016-09-20 19:00:16 +00:00
|
|
|
"%1return (STATE_TYPE)p;\n}\n\n");
|
2016-09-20 18:37:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* emitstring - emit array of rules and costs */
|
2016-09-20 19:00:16 +00:00
|
|
|
static void emitstring(Rule rules)
|
|
|
|
{
|
2016-09-20 18:37:16 +00:00
|
|
|
Rule r;
|
|
|
|
int k;
|
|
|
|
|
2016-09-24 14:59:49 +00:00
|
|
|
print("static const short %Pcost[][4] = {\n");
|
2016-09-20 19:00:16 +00:00
|
|
|
for (k = 0, r = rules; r; r = r->link)
|
|
|
|
{
|
|
|
|
for (; k < r->ern; k++)
|
2016-09-20 18:37:16 +00:00
|
|
|
print("%1{ 0 },%1/* %d */\n", k);
|
|
|
|
print("%1{ %d },%1/* %d = %R */\n", r->cost, k++, r);
|
|
|
|
}
|
|
|
|
print("};\n\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* emitstruct - emit the definition of the state structure */
|
2016-09-20 19:00:16 +00:00
|
|
|
static void emitstruct(Nonterm nts, int ntnumber)
|
|
|
|
{
|
2016-09-20 18:37:16 +00:00
|
|
|
print("struct %Pstate {\n%1int op;\n%1struct %Pstate *left, *right;\n"
|
2016-09-20 19:00:16 +00:00
|
|
|
"%1short cost[%d];\n%1struct {\n",
|
|
|
|
ntnumber + 1);
|
|
|
|
for (; nts; nts = nts->link)
|
|
|
|
{
|
2016-09-20 18:37:16 +00:00
|
|
|
int n = 1, m = nts->lhscount;
|
|
|
|
while (m >>= 1)
|
2016-09-20 19:00:16 +00:00
|
|
|
n++;
|
2016-09-20 18:37:16 +00:00
|
|
|
print("%2unsigned %P%S:%d;\n", nts, n);
|
|
|
|
}
|
|
|
|
print("%1} rule;\n};\n\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* emitterms - emit terminal data structures */
|
2016-09-20 19:00:16 +00:00
|
|
|
static void emitterms(Term terms)
|
|
|
|
{
|
2016-09-20 18:37:16 +00:00
|
|
|
Term p;
|
|
|
|
int k;
|
|
|
|
|
2016-09-24 14:59:49 +00:00
|
|
|
print("static const char %Parity[] = {\n");
|
2016-09-20 19:00:16 +00:00
|
|
|
for (k = 0, p = terms; p; p = p->link)
|
|
|
|
{
|
|
|
|
for (; k < p->esn; k++)
|
2016-09-20 18:37:16 +00:00
|
|
|
print("%10,%1/* %d */\n", k);
|
|
|
|
print("%1%d,%1/* %d=%S */\n", p->arity < 0 ? 0 : p->arity, k++, p);
|
|
|
|
}
|
|
|
|
print("};\n\n");
|
2016-09-24 14:59:49 +00:00
|
|
|
|
|
|
|
print("static const char *%Popname[] = {\n");
|
|
|
|
for (k = 0, p = terms; p; p = p->link)
|
2016-09-20 19:00:16 +00:00
|
|
|
{
|
2016-09-24 14:59:49 +00:00
|
|
|
for (; k < p->esn; k++)
|
|
|
|
print("%1/* %d */%10,\n", k);
|
|
|
|
print("%1/* %d */%1\"%S\",\n", k++, p);
|
2016-09-20 18:37:16 +00:00
|
|
|
}
|
2016-09-24 14:59:49 +00:00
|
|
|
print("};\n\n");
|
2016-09-20 18:37:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* emittest - emit clause for testing a match */
|
2016-09-30 17:10:30 +00:00
|
|
|
static void emittest(Tree t, const char* v, const char* suffix)
|
2016-09-20 19:00:16 +00:00
|
|
|
{
|
2016-09-20 18:37:16 +00:00
|
|
|
Term p = t->op;
|
|
|
|
|
2016-09-20 19:00:16 +00:00
|
|
|
if (p->kind == TERM)
|
|
|
|
{
|
2016-09-20 18:37:16 +00:00
|
|
|
print("%3%s->op == %d%s/* %S */\n", v, p->esn,
|
2016-09-20 19:00:16 +00:00
|
|
|
t->nterms > 1 ? " && " : suffix, p);
|
2016-09-20 18:37:16 +00:00
|
|
|
if (t->left)
|
2016-09-30 17:10:30 +00:00
|
|
|
emittest(t->left, aprintf("%s->left", v),
|
2016-09-20 19:00:16 +00:00
|
|
|
t->right && t->right->nterms ? " && " : suffix);
|
2016-09-20 18:37:16 +00:00
|
|
|
if (t->right)
|
2016-09-30 17:10:30 +00:00
|
|
|
emittest(t->right, aprintf("%s->right", v), suffix);
|
2016-09-20 18:37:16 +00:00
|
|
|
}
|
|
|
|
}
|