35a00372ac120e3672cf493e37cb051f1a6b2cc2
[oota-llvm.git] / lib / CodeGen / TargetMachine / Sparc / Sparc.burg
1 %{
2 #include <stdio.h>
3 #include <llvm/CodeGen/InstrForest.h>
4
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)
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 %term Phi=25
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          */
88 stmt:   Ret                     =   1 (3);
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 */
97
98 stmt:   reg                     =  111 (0);
99 stmt:   boolconst               =  112 (0);
100 stmt:   bool                    =  113 (0);
101
102         /*
103          * List node used for nodes with more than 2 children
104          */
105 reg:    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          */
115 reg:    not                     =  121 (0);
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);
127
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);
140
141 reg:      ToArrayTy(reg)        =  19 (1);
142 reg:      ToPointerTy(reg)      =  20 (1);
143
144         /*
145          * The binary operators.
146          */
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 */
159
160         /*
161          * The SetCC instructions and other boolean values
162          */
163 boolconst: SetCC(reg,Constant)  =   41 (1);
164 bool:      SetCC(reg,reg)       =   42 (1);
165 boolreg:   VReg                 =   43 (0);
166 boolreg:   Constant             =   44 (0);
167
168         /*
169          * Memory access instructions
170          */
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);
180
181         /*
182          * Other operators producing register values
183          */
184 reg:    Call                    =   61 (0);
185 reg:    Shl(reg,reg)            =   62 (1);
186 reg:    Shr(reg,reg)            =   63 (1);
187 reg:    Phi(reg,reg)            =   64 (0);
188
189         /*
190          * Finally, leaf nodes of expression trees (other than boolreg)
191          */
192 reg:    VReg                    =   71 (0);
193 reg:    Constant                =   72 (0);
194
195
196
197 %%
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  *-----------------------------------------------------------------------*/
203
204 static char rcsid[] = "$Id$";
205
206 #ifdef __STDC__
207 void printcover(NODEPTR_TYPE p, int goalnt, int indent) {
208 #else
209 void printcover(p, goalnt, indent) NODEPTR_TYPE p; int goalnt; int indent; {
210 #endif
211         int eruleno = burm_rule(STATE_LABEL(p), goalnt);
212         short *nts = burm_nts[eruleno];
213         NODEPTR_TYPE kids[10];
214         int i;
215         
216         if (eruleno == 0) {
217                 printf("no cover\n");
218                 return;
219         }
220         for (i = 0; i < indent; i++)
221                 printf(".");
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);
226 }
227
228 #ifdef __STDC__
229 void printtree(NODEPTR_TYPE p) {
230 #else
231 void printtree(p) NODEPTR_TYPE p; {
232 #endif
233         int op = burm_op_label(p);
234
235         printf("%s", burm_opname[op]);
236         switch (burm_arity[op]) {
237         case 0:
238                 break;
239         case 1:
240                 printf("(");
241                 printtree(burm_child(p, 0));
242                 printf(")");
243                 break;
244         case 2:
245                 printf("(");
246                 printtree(burm_child(p, 0));
247                 printf(", ");
248                 printtree(burm_child(p, 1));
249                 printf(")");
250                 break;
251         }
252 }
253
254 #ifdef __STDC__
255 int treecost(NODEPTR_TYPE p, int goalnt, int costindex) {
256 #else
257 int treecost(p, goalnt, costindex) NODEPTR_TYPE p; int goalnt; int costindex; {
258 #endif
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];
263
264         burm_kids(p, eruleno, kids);
265         for (i = 0; nts[i]; i++)
266                 cost += treecost(kids[i], nts[i], costindex);
267         return cost;
268 }
269
270 #ifdef __STDC__
271 void printMatches(NODEPTR_TYPE p) {
272 #else
273 void printMatches(p) NODEPTR_TYPE p; {
274 #endif
275         int nt;
276         int eruleno;
277
278         printf("Node 0x%lx= ", (unsigned long)p);
279         printtree(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]);
284 }