More grammar changes.

This commit is contained in:
David Given 2016-09-24 19:03:55 +02:00
parent 2acc4ed29d
commit c8fcbe282a
4 changed files with 29 additions and 97 deletions

View file

@ -1,37 +1,6 @@
%{ PATTERNS
#if 0
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <stdbool.h>
#define TRACE STORE4(addr:address, value:reg)
#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)
ins value:GPR ins value:GPR
emit "str %value, %addr" emit "str %value, %addr"
cost 4; cost 4;
@ -49,7 +18,7 @@ static void burm_trace(NODEPTR_TYPE p, int eruleno, int cost, int bestcost) {
ins addr:GPR ins addr:GPR
fragment "[%addr]"; fragment "[%addr]";
stm = reg; reg;
reg = ADD4(left:reg, right:aluparam) reg = ADD4(left:reg, right:aluparam)
ins left:GPR, right:GPR ins left:GPR, right:GPR

View file

@ -7,14 +7,14 @@
#define YYDEBUG 1 #define YYDEBUG 1
static char rcsid[] = "$Id$"; static char rcsid[] = "$Id$";
static int nextesn = 0; static int nextern = 1;
static int nextern = 0;
%} %}
%union { %union {
int n; int n;
char* string; char* string;
Tree tree; Tree tree;
Rule rule;
Stringlist stringlist; Stringlist stringlist;
char* stringpair[2]; char* stringpair[2];
} }
@ -36,37 +36,16 @@ static int nextern = 0;
%token <string> STRING %token <string> STRING
%type <n> cost %type <n> cost
%type <string> lhs %type <rule> pattern
%type <stringlist> stringlist %type <stringlist> stringlist
%type <stringlist> when %type <stringlist> when
%type <tree> rhs %type <tree> rhs
%type <stringpair> labelledid %type <stringpair> labelledid
%% %%
spec spec
: decls PPERCENT patterns : PATTERNS patterns
| decls
; ;
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 patterns
: /* nothing */ : /* nothing */
| patterns pattern ';' | patterns pattern ';'
@ -75,13 +54,16 @@ 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 rhs
: labelledid { $$ = tree($1[1], $1[0], NULL, NULL); } : labelledid { $$ = tree($1[1], $1[0], NULL, NULL); }
| labelledid '(' rhs ')' { $$ = tree($1[1], $1[0], $3, NULL); } | labelledid '(' rhs ')' { $$ = tree($1[1], $1[0], $3, NULL); }
@ -104,12 +86,7 @@ stringlist
; ;
ins ins
: /* nothing */ : ins ',' in
| INS inslist
;
inslist
: inslist ',' in
| in | in
; ;
@ -118,12 +95,7 @@ in
; ;
outs outs
: /* nothing */ : outs ',' out
| OUTS outslist
;
outslist
: outslist ',' out
| out | out
; ;
@ -131,22 +103,6 @@ out
: ID ':' ID : 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 <stdarg.h>
#include <ctype.h> #include <ctype.h>

View file

@ -60,7 +60,7 @@ int main(int argc, char* argv[])
for (;;) for (;;)
{ {
int opt = getopt(argc, argv, "p:i:o:"); int opt = getopt(argc, argv, "p:i:o:y");
if (opt == -1) if (opt == -1)
break; break;
@ -88,6 +88,13 @@ int main(int argc, char* argv[])
} }
break; break;
case 'y':
{
extern int yydebug;
yydebug = 1;
break;
}
default: default:
yyerror("usage: %s [-p prefix] < input > output\n", argv[0]); yyerror("usage: %s [-p prefix] < input > output\n", argv[0]);
exit(1); exit(1);
@ -97,6 +104,8 @@ int main(int argc, char* argv[])
emitheader(); emitheader();
registerterminals(); registerterminals();
start = nonterm("stmt");
yyin = infp; yyin = infp;
yyparse(); yyparse();
@ -308,14 +317,13 @@ Tree tree(const char* id, const char* label, Tree left, Tree right)
} }
/* rule - create & initialize a rule with the given fields */ /* 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 r = calloc(1, sizeof *r);
Rule *q; Rule *q;
Term p = pattern->op; Term p = pattern->op;
nrules++; nrules++;
r->when = when;
r->lhs = nonterm(id); r->lhs = nonterm(id);
r->packed = ++r->lhs->lhscount; r->packed = ++r->lhs->lhscount;
for (q = &r->lhs->rules; *q; q = &(*q)->decode) for (q = &r->lhs->rules; *q; q = &(*q)->decode)
@ -323,7 +331,6 @@ Rule rule(char* id, Tree pattern, int ern, Stringlist when, int cost)
*q = r; *q = r;
r->pattern = pattern; r->pattern = pattern;
r->ern = ern; r->ern = ern;
r->cost = cost;
if (p->kind == TERM) if (p->kind == TERM)
{ {
r->next = p->rules; r->next = p->rules;

View file

@ -68,7 +68,7 @@ struct rule
Rule kids; /* next rule with same burm_kids pattern */ Rule kids; /* next rule with same burm_kids pattern */
Stringlist when; /* C predicate string */ 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 */ extern int maxcost; /* maximum cost */
/* gram.y: */ /* gram.y: */