-%{
-#if 0
-#include <stdio.h>
-#include <assert.h>
-#include <stdlib.h>
-#include <stdbool.h>
+PATTERNS
-#define TRACE
-
-#define STATE_TYPE void*
-typedef struct tree {
- int op;
- struct tree *kids[2];
- STATE_TYPE state_label;
-} *NODEPTR_TYPE;
-#define OP_LABEL(p) ((p)->op)
-#define LEFT_CHILD(p) ((p)->kids[0])
-#define RIGHT_CHILD(p) ((p)->kids[1])
-#define STATE_LABEL(p) ((p)->state_label)
-#define PANIC printf
-
-static void burm_trace(NODEPTR_TYPE p, int eruleno, int cost, int bestcost) {
-#ifdef TRACE
- extern const char *burm_string[];
-
- fprintf(stderr, "0x%p matched %s with cost %d vs. %d\n", p,
- burm_string[eruleno], cost, bestcost);
-#endif
-}
-#endif
-%}
-%%
-
- stm = STORE4(addr:address, value:reg)
+ STORE4(addr:address, value:reg)
ins value:GPR
emit "str %value, %addr"
cost 4;
ins addr:GPR
fragment "[%addr]";
- stm = reg;
+ reg;
reg = ADD4(left:reg, right:aluparam)
ins left:GPR, right:GPR
#define YYDEBUG 1
static char rcsid[] = "$Id$";
-static int nextesn = 0;
-static int nextern = 0;
+static int nextern = 1;
%}
%union {
int n;
char* string;
Tree tree;
+ Rule rule;
Stringlist stringlist;
char* stringpair[2];
}
%token <string> STRING
%type <n> cost
-%type <string> lhs
+%type <rule> pattern
%type <stringlist> stringlist
%type <stringlist> when
%type <tree> rhs
%type <stringpair> labelledid
%%
spec
- : decls PPERCENT patterns
- | decls
+ : PATTERNS patterns
;
-decls : /* lambda */
- | decls decl
- ;
-
-decl
- : TERMINAL blist ';'
- | START lhs ';'
- {
- if (nonterm($2)->number != 1)
- yyerror("redeclaration of the start symbol\n");
- }
- | ';'
- | error ';' { yyerrok; }
- ;
-
-blist
- : /* nothing */
- | blist ID { term($2, nextesn++); }
- ;
-
patterns
: /* nothing */
| patterns pattern ';'
;
pattern
- : lhs '=' rhs when ins outs emits cost { rule($1, $3, nextern++, $4, $8); }
+ : ID '=' rhs { nonterm($1); $$ = rule($1, $3, nextern++); }
+ | rhs { $$ = rule("stmt", $1, nextern++); }
+ | pattern WHEN stringlist { $$ = $1; $$->when = $3; }
+ | pattern INS ins { $$ = $1; }
+ | pattern OUTS outs { $$ = $1; }
+ | pattern EMIT STRING { $$ = $1; }
+ | pattern FRAGMENT STRING { $$ = $1; }
+ | pattern COST INT { $$ = $1; $$->cost = $3; }
;
-lhs
- : ID { $$ = $1; nonterm($$); }
- ;
-
rhs
: labelledid { $$ = tree($1[1], $1[0], NULL, NULL); }
| labelledid '(' rhs ')' { $$ = tree($1[1], $1[0], $3, NULL); }
;
ins
- : /* nothing */
- | INS inslist
- ;
-
-inslist
- : inslist ',' in
+ : ins ',' in
| in
;
;
outs
- : /* nothing */
- | OUTS outslist
- ;
-
-outslist
- : outslist ',' out
+ : outs ',' out
| out
;
: ID ':' ID
;
-emits
- : /* nothing */
- | EMIT STRING
- | FRAGMENT STRING
- ;
-
-cost
- : /* lambda */ { $$ = 0; }
- | COST INT {
- if ($2 > maxcost) {
- yyerror("%d exceeds maximum cost of %d\n", $2, maxcost);
- $$ = maxcost;
- } else
- $$ = $2;
- }
- ;
%%
#include <stdarg.h>
#include <ctype.h>
for (;;)
{
- int opt = getopt(argc, argv, "p:i:o:");
+ int opt = getopt(argc, argv, "p:i:o:y");
if (opt == -1)
break;
}
break;
+ case 'y':
+ {
+ extern int yydebug;
+ yydebug = 1;
+ break;
+ }
+
default:
yyerror("usage: %s [-p prefix] < input > output\n", argv[0]);
exit(1);
emitheader();
registerterminals();
+ start = nonterm("stmt");
+
yyin = infp;
yyparse();
}
/* rule - create & initialize a rule with the given fields */
-Rule rule(char* id, Tree pattern, int ern, Stringlist when, int cost)
+Rule rule(char* id, Tree pattern, int ern)
{
Rule r = calloc(1, sizeof *r);
Rule *q;
Term p = pattern->op;
nrules++;
- r->when = when;
r->lhs = nonterm(id);
r->packed = ++r->lhs->lhscount;
for (q = &r->lhs->rules; *q; q = &(*q)->decode)
*q = r;
r->pattern = pattern;
r->ern = ern;
- r->cost = cost;
if (p->kind == TERM)
{
r->next = p->rules;
Rule kids; /* next rule with same burm_kids pattern */
Stringlist when; /* C predicate string */
};
-extern Rule rule(char* id, Tree pattern, int ern, Stringlist when, int cost);
+extern Rule rule(char* id, Tree pattern, int ern);
extern int maxcost; /* maximum cost */
/* gram.y: */