3 #include <llvm/CodeGen/InstrForest.h>
5 typedef 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)
15 %term Ret=1 /* return void from a function */
16 %term RetValue=101 /* return a value from a function */
30 %term SetCC=113 /* use this to match all SetCC instructions */
40 %term AllocaN=121 /* alloca with arg N */
42 %term LoadIdx=122 /* load with index vector */
45 %term GetElemPtrIdx=124 /* getElemPtr with index vector */
49 %term Cast=26 /* cast that will be ignored. others are made explicit */
67 /* 30...46 are unused */
69 * The foll. values should match the constants in InstrForest.h
75 /* 50+i is a variant of i, as defined above */
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 *-----------------------------------------------------------------------*/
86 * The top-level statements
89 stmt: RetValue(reg) = 2 (3);
90 stmt: Store(reg,reg) = 3 (1);
91 stmt: Store(reg,ptrreg) = 4 (1);
92 stmt: BrUncond = 5 (2);
93 stmt: BrCond(boolconst) = 6 (1); /* may save one instruction */
94 stmt: BrCond(bool) = 7 (2);
95 stmt: BrCond(boolreg) = 8 (2);
96 stmt: Switch(reg) = 9 (3); /* cost = load + branch */
99 stmt: boolconst = 112 (0);
100 stmt: bool = 113 (0);
103 * List node used for nodes with more than 2 children
105 reg: VRegList(reg,reg) = 10 (0);
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.
116 reg: tobool = 122 (0);
117 reg: toubyte = 123 (0);
118 reg: tosbyte = 124 (0);
119 reg: toushort = 125 (0);
120 reg: toshort = 126 (0);
121 reg: touint = 127 (0);
122 reg: toint = 128 (0);
123 reg: toulong = 129 (0);
124 reg: tolong = 130 (0);
125 reg: tofloat = 131 (0);
126 reg: todouble = 132 (0);
128 not: Not(reg) = 21 (1);
129 tobool: ToBoolTy(reg) = 22 (1);
130 toubyte: ToUByteTy(reg) = 23 (1);
131 tosbyte: ToSByteTy(reg) = 24 (1);
132 toushort: ToUShortTy(reg) = 25 (1);
133 toshort: ToShortTy(reg) = 26 (1);
134 touint: ToUIntTy(reg) = 27 (1);
135 toint: ToIntTy(reg) = 28 (1);
136 toulong: ToULongTy(reg) = 29 (1);
137 tolong: ToLongTy(reg) = 30 (1);
138 tofloat: ToFloatTy(reg) = 31 (1);
139 todouble: ToDoubleTy(reg) = 32 (1);
141 reg: ToArrayTy(reg) = 19 (1);
142 reg: ToPointerTy(reg) = 20 (1);
145 * The binary operators.
147 reg: Add(reg,reg) = 33 (1);
148 reg: Sub(reg,reg) = 34 (1);
149 reg: Mul(reg,reg) = 35 (3);
150 reg: Mul(todouble,todouble) = 135 (2); /* avoids 1-2 type converts */
151 reg: Div(reg,reg) = 36 (6);
152 reg: Rem(reg,reg) = 37 (6);
153 reg: And(reg,reg) = 38 (1);
154 reg: And(reg,not) = 138 (0); /* cost is counted for not */
155 reg: Or (reg,reg) = 39 (1);
156 reg: Or (reg,not) = 139 (0); /* cost is counted for not */
157 reg: Xor(reg,reg) = 40 (1);
158 reg: Xor(reg,not) = 140 (0); /* cost is counted for not */
161 * The SetCC instructions and other boolean values
163 boolconst: SetCC(reg,Constant) = 41 (1);
164 bool: SetCC(reg,reg) = 42 (1);
165 boolreg: VReg = 43 (0);
166 boolreg: Constant = 44 (0);
169 * Memory access instructions
171 reg: Load(reg) = 51 (3);
172 reg: Load(ptrreg) = 52 (2); /* 1 counted for ptrreg */
173 reg: LoadIdx(reg,reg) = 53 (3);
174 reg: LoadIdx(ptrreg,reg) = 54 (2); /* 1 counted for ptrreg */
175 reg: ptrreg = 155 (0);
176 ptrreg: GetElemPtr(reg) = 55 (1);
177 ptrreg: GetElemPtrIdx(reg,reg) = 56 (1);
178 reg: Alloca = 57 (1);
179 reg: AllocaN(reg) = 58 (1);
182 * Other operators producing register values
185 reg: Shl(reg,reg) = 62 (1);
186 reg: Shr(reg,reg) = 63 (1);
187 reg: Phi(reg,reg) = 64 (0);
190 * Finally, leaf nodes of expression trees (other than boolreg)
193 reg: Constant = 72 (0);
198 /*-----------------------------------------------------------------------*
199 * The rest of this file provides code to print the cover produced
200 * by BURG and information about computed tree cost and matches.
201 * This code was taken from sample.gr provided with BURG.
202 *-----------------------------------------------------------------------*/
204 static char rcsid[] = "$Id$";
207 void printcover(NODEPTR_TYPE p, int goalnt, int indent) {
209 void printcover(p, goalnt, indent) NODEPTR_TYPE p; int goalnt; int indent; {
211 int eruleno = burm_rule(STATE_LABEL(p), goalnt);
212 short *nts = burm_nts[eruleno];
213 NODEPTR_TYPE kids[10];
217 printf("no cover\n");
220 for (i = 0; i < indent; i++)
222 printf("%s\n", burm_string[eruleno]);
223 burm_kids(p, eruleno, kids);
224 for (i = 0; nts[i]; i++)
225 printcover(kids[i], nts[i], indent+1);
229 void printtree(NODEPTR_TYPE p) {
231 void printtree(p) NODEPTR_TYPE p; {
233 int op = burm_op_label(p);
235 printf("%s", burm_opname[op]);
236 switch (burm_arity[op]) {
241 printtree(burm_child(p, 0));
246 printtree(burm_child(p, 0));
248 printtree(burm_child(p, 1));
255 int treecost(NODEPTR_TYPE p, int goalnt, int costindex) {
257 int treecost(p, goalnt, costindex) NODEPTR_TYPE p; int goalnt; int costindex; {
259 int eruleno = burm_rule(STATE_LABEL(p), goalnt);
260 int cost = burm_cost[eruleno][costindex], i;
261 short *nts = burm_nts[eruleno];
262 NODEPTR_TYPE kids[10];
264 burm_kids(p, eruleno, kids);
265 for (i = 0; nts[i]; i++)
266 cost += treecost(kids[i], nts[i], costindex);
271 void printMatches(NODEPTR_TYPE p) {
273 void printMatches(p) NODEPTR_TYPE p; {
278 printf("Node 0x%lx= ", (unsigned long)p);
280 printf(" matched rules:\n");
281 for (nt = 1; burm_ntname[nt] != (char*)NULL; nt++)
282 if ((eruleno = burm_rule(STATE_LABEL(p), nt)) != 0)
283 printf("\t%s\n", burm_string[eruleno]);