blob: d2e3e8c00115cbf677f2730058f90cf8d0c9f37e [file] [log] [blame]
Vikram S. Adve65682392001-07-21 12:42:19 +00001%{
2#include <stdio.h>
3#include <llvm/Codegen/InstrForest.h>
4
5typedef BasicTreeNode* NODEPTR_TYPE;
6#define OP_LABEL(p) ((p)->opLabel)
7#define LEFT_CHILD(p) ((p)->leftChild)
8#define RIGHT_CHILD(p) ((p)->rightChild)
9#define STATE_LABEL(p) ((p)->state)
10#define PANIC printf
11%}
12
13%start stmt
14
15%term Ret=1 /* return void from a function */
16%term RetValue=101 /* return a value from a function */
17%term BrUncond=2
18%term BrCond=102
19%term Switch=3
20 /* 4 is unused */
21%term Not=4
22%term Add=5
23%term Sub=6
24%term Mul=7
25%term Div=8
26%term Rem=9
27%term And=10
28%term Or=11
29%term Xor=12
30%term SetCC=113 /* use this to match all SetCC instructions */
31 /* %term SetEQ=13 */
32 /* %term SetNE=14 */
33 /* %term SetLE=15 */
34 /* %term SetGE=16 */
35 /* %term SetLT=17 */
36 /* %term SetGT=18 */
37%term Malloc=19
38%term Free=20
39%term Alloca=21
40%term AllocaN=121 /* alloca with arg N */
41%term Load=22
42%term LoadIdx=122 /* load with index vector */
43%term Store=23
44%term GetElemPtr=24
45%term GetElemPtrIdx=124 /* getElemPtr with index vector */
46
47 /* 25 is PHINode, which is never a real tree node */
48
49%term Cast=26 /* cast that will be ignored. others are made explicit */
50%term ToBoolTy=126
51%term ToUByteTy=127
52%term ToSByteTy=128
53%term ToUShortTy=129
54%term ToShortTy=130
55%term ToUIntTy=131
56%term ToIntTy=132
57%term ToULongTy=133
58%term ToLongTy=134
59%term ToFloatTy=135
60%term ToDoubleTy=136
61%term ToArrayTy=137
62%term ToPointerTy=138
63
64%term Call=27
65%term Shl=28
66%term Shr=29
67 /* 30...46 are unused */
68 /*
69 * The foll. values should match the constants in InstrForest.h
70 */
71%term VRegList=97
72%term VReg=98
73%term Constant=99
74%term Label=100
75 /* 50+i is a variant of i, as defined above */
76
77
78%%
79/*-----------------------------------------------------------------------*
80 * The productions of the grammar.
81 * Note that all chain rules are numbered 101 and above.
82 * Also, a special case of production X is numbered 100+X.
83 *-----------------------------------------------------------------------*/
84
85 /*
86 * The top-level statements
87 */
88stmt: Ret = 1 (3);
89stmt: RetValue(reg) = 2 (3);
90stmt: Store(reg,reg) = 3 (1);
91stmt: Store(reg,ptrreg) = 4 (1);
92stmt: BrUncond = 5 (2);
93stmt: BrCond(boolconst) = 6 (1); /* may save one instruction */
94stmt: BrCond(bool) = 7 (2);
95stmt: BrCond(boolreg) = 8 (2);
96stmt: Switch(reg) = 9 (3); /* cost = load + branch */
97
98stmt: reg = 111 (0);
99stmt: boolconst = 112 (0);
100stmt: bool = 113 (0);
101
102 /*
103 * List node used for nodes with more than 2 children
104 */
105reg: VRegList(reg,reg) = 10 (0);
106
107 /*
108 * The unary operators. We encode NOT and individual casts into
109 * separate non-terminals to combine instructions for some cases:
110 * Eg1: zdouble <- todouble(xfloat) * todouble(yfloat)
111 * Eg2: c <- a AND (NOT b).
112 * Note that the costs are counted for the individual non-terminals
113 * below, not for reg.
114 */
115reg: not = 121 (0);
116reg: tobool = 122 (0);
117reg: toubyte = 123 (0);
118reg: tosbyte = 124 (0);
119reg: toushort = 125 (0);
120reg: toshort = 126 (0);
121reg: touint = 127 (0);
122reg: toint = 128 (0);
123reg: toulong = 129 (0);
124reg: tolong = 130 (0);
125reg: tofloat = 131 (0);
126reg: todouble = 132 (0);
127
128not: Not(reg) = 21 (1);
129tobool: ToBoolTy(reg) = 22 (1);
130toubyte: ToUByteTy(reg) = 23 (1);
131tosbyte: ToSByteTy(reg) = 24 (1);
132toushort: ToUShortTy(reg) = 25 (1);
133toshort: ToShortTy(reg) = 26 (1);
134touint: ToUIntTy(reg) = 27 (1);
135toint: ToIntTy(reg) = 28 (1);
136toulong: ToULongTy(reg) = 29 (1);
137tolong: ToLongTy(reg) = 30 (1);
138tofloat: ToFloatTy(reg) = 31 (1);
139todouble: ToDoubleTy(reg) = 32 (1);
140
141reg: ToArrayTy(reg) = 19 (1);
142reg: ToPointerTy(reg) = 20 (1);
143
144 /*
145 * The binary operators.
146 */
147reg: Add(reg,reg) = 33 (1);
148reg: Sub(reg,reg) = 34 (1);
149reg: Mul(reg,reg) = 35 (3);
150reg: Mul(todouble,todouble) = 135 (2); /* avoids 1-2 type converts */
151reg: Div(reg,reg) = 36 (6);
152reg: Rem(reg,reg) = 37 (6);
153reg: And(reg,reg) = 38 (1);
154reg: And(reg,not) = 138 (0); /* cost is counted for not */
155reg: Or (reg,reg) = 39 (1);
156reg: Or (reg,not) = 139 (0); /* cost is counted for not */
157reg: Xor(reg,reg) = 40 (1);
158reg: Xor(reg,not) = 140 (0); /* cost is counted for not */
159
160 /*
161 * The SetCC instructions and other boolean values
162 */
163boolconst: SetCC(reg,Constant) = 41 (1);
164bool: SetCC(reg,reg) = 42 (1);
165boolreg: VReg = 43 (0);
166
167 /*
168 * Memory access instructions
169 */
170reg: Load(reg) = 51 (3);
171reg: Load(ptrreg) = 52 (2); /* 1 counted for ptrreg */
172reg: LoadIdx(reg,reg) = 53 (3);
173reg: LoadIdx(ptrreg,reg) = 54 (2); /* 1 counted for ptrreg */
174reg: ptrreg = 155 (0);
175ptrreg: GetElemPtr(reg) = 55 (1);
176ptrreg: GetElemPtrIdx(reg,reg) = 56 (1);
177reg: Alloca = 57 (1);
178reg: AllocaN(reg) = 58 (1);
179
180 /*
181 * Other operators producing register values
182 */
183reg: Call = 61 (0);
184reg: Shl(reg,reg) = 62 (1);
185reg: Shr(reg,reg) = 63 (1);
186
187 /*
188 * Finally, leaf nodes of expression trees (other than boolreg)
189 */
190reg: VReg = 71 (0);
191reg: Constant = 72 (0);
192
193
194
195%%
196/*-----------------------------------------------------------------------*
197 * The rest of this file provides code to print the cover produced
198 * by BURG and information about computed tree cost and matches.
199 * This code was taken from sample.gr provided with BURG.
200 *-----------------------------------------------------------------------*/
201
202static char rcsid[] = "$Id$";
203
204#ifdef __STDC__
205void printcover(NODEPTR_TYPE p, int goalnt, int indent) {
206#else
207void printcover(p, goalnt, indent) NODEPTR_TYPE p; int goalnt; int indent; {
208#endif
209 int eruleno = burm_rule(STATE_LABEL(p), goalnt);
210 short *nts = burm_nts[eruleno];
211 NODEPTR_TYPE kids[10];
212 int i;
213
214 if (eruleno == 0) {
215 printf("no cover\n");
216 return;
217 }
218 for (i = 0; i < indent; i++)
219 printf(".");
220 printf("%s\n", burm_string[eruleno]);
221 burm_kids(p, eruleno, kids);
222 for (i = 0; nts[i]; i++)
223 printcover(kids[i], nts[i], indent+1);
224}
225
226#ifdef __STDC__
227void printtree(NODEPTR_TYPE p) {
228#else
229void printtree(p) NODEPTR_TYPE p; {
230#endif
231 int op = burm_op_label(p);
232
233 printf("%s", burm_opname[op]);
234 switch (burm_arity[op]) {
235 case 0:
236 break;
237 case 1:
238 printf("(");
239 printtree(burm_child(p, 0));
240 printf(")");
241 break;
242 case 2:
243 printf("(");
244 printtree(burm_child(p, 0));
245 printf(", ");
246 printtree(burm_child(p, 1));
247 printf(")");
248 break;
249 }
250}
251
252#ifdef __STDC__
253int treecost(NODEPTR_TYPE p, int goalnt, int costindex) {
254#else
255int treecost(p, goalnt, costindex) NODEPTR_TYPE p; int goalnt; int costindex; {
256#endif
257 int eruleno = burm_rule(STATE_LABEL(p), goalnt);
258 int cost = burm_cost[eruleno][costindex], i;
259 short *nts = burm_nts[eruleno];
260 NODEPTR_TYPE kids[10];
261
262 burm_kids(p, eruleno, kids);
263 for (i = 0; nts[i]; i++)
264 cost += treecost(kids[i], nts[i], costindex);
265 return cost;
266}
267
268#ifdef __STDC__
269void printMatches(NODEPTR_TYPE p) {
270#else
271void printMatches(p) NODEPTR_TYPE p; {
272#endif
273 int nt;
274 int eruleno;
275
276 printf("Node 0x%lx= ", (unsigned long)p);
277 printtree(p);
278 printf(" matched rules:\n");
279 for (nt = 1; burm_ntname[nt] != (char*)NULL; nt++)
280 if ((eruleno = burm_rule(STATE_LABEL(p), nt)) != 0)
281 printf("\t%s\n", burm_string[eruleno]);
282}