Initial checkin of the V9 relocation types
[oota-llvm.git] / lib / Target / SparcV9 / SparcV9.burg.in
1 %{               // -*- C++ -*-
2 /* ===----------------------------------------------------------------------===
3 // 
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file was developed by the LLVM research group and is distributed under
7 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 // 
9 //===----------------------------------------------------------------------===*/
10
11 Xinclude <cstdio>
12 Xinclude "SparcV9InstrForest.h"
13
14 typedef llvm::InstrTreeNode* NODEPTR_TYPE;
15 Xdefine OP_LABEL(p)     ((p)->opLabel)
16 Xdefine LEFT_CHILD(p)   ((p)->LeftChild)
17 Xdefine RIGHT_CHILD(p)  ((p)->RightChild)
18 Xdefine STATE_LABEL(p)  ((p)->state)
19 Xdefine PANIC           printf
20
21 // Get definitions for various instruction values that we will need...
22 #define HANDLE_TERM_INST(N, OPC, CLASS)   Ydefine OPC##OPCODE N
23 #define HANDLE_UNARY_INST(N, OPC, CLASS)  Ydefine OPC##OPCODE N
24 #define HANDLE_BINARY_INST(N, OPC, CLASS) Ydefine OPC##OPCODE N
25 #define HANDLE_MEMORY_INST(N, OPC, CLASS) Ydefine OPC##OPCODE N
26 #define HANDLE_OTHER_INST(N, OPC, CLASS)  Ydefine OPC##OPCODE N
27
28 #include "llvm/Instruction.def"
29
30 %}
31
32 %start stmt
33
34 %term Ret=RetOPCODE             /* return void from a function */
35 %term RetValue=101              /* return a value from a function */
36 %term BrUncond=BrOPCODE
37 %term BrCond=102
38 %term Switch=SwitchOPCODE
39                 /* 4 is unused */
40 %term Add=AddOPCODE
41 %term Sub=SubOPCODE
42 %term Mul=MulOPCODE
43 %term Div=DivOPCODE
44 %term Rem=RemOPCODE
45 %term And=AndOPCODE
46 %term Or=OrOPCODE
47 %term Xor=XorOPCODE
48                 /* Use the next 4 to distinguish bitwise operators from
49                  * logical operators.  This is no longer used for SparcV9,
50                  * but may be useful for other target machines.
51                  * The last one is the bitwise Not(val) == XOR val, 11..1.
52                  * Note that it is also a binary operator, not unary.
53                  */
54 %term BAnd=112
55 %term BOr=113
56 %term BXor=114
57 %term BNot=214
58                 /* The next one is the boolean Not(val) == bool XOR val, true
59                  * Note that it is also a binary operator, not unary.
60                  */
61 %term  Not=314
62
63 %term SetCC=115 /* use this to match all SetCC instructions */
64         /* %term SetEQ=13 */
65         /* %term SetNE=14 */
66         /* %term SetLE=15 */
67         /* %term SetGE=16 */
68         /* %term SetLT=17 */
69         /* %term SetGT=18 */
70 %term Malloc=MallocOPCODE
71 %term Free=FreeOPCODE
72 %term Alloca=AllocaOPCODE
73 %term AllocaN=123               /* alloca with arg N */
74 %term Load=LoadOPCODE
75 %term Store=StoreOPCODE
76 %term GetElemPtr=GetElementPtrOPCODE
77 %term GetElemPtrIdx=126         /* getElemPtr with index vector */
78
79 %term Phi=PHIOPCODE
80
81 %term Cast=CastOPCODE /* cast that will be ignored. others are made explicit */
82 %term ToBoolTy=128
83 %term ToUByteTy=129
84 %term ToSByteTy=130
85 %term ToUShortTy=131
86 %term ToShortTy=132
87 %term ToUIntTy=133
88 %term ToIntTy=134
89 %term ToULongTy=135
90 %term ToLongTy=136
91 %term ToFloatTy=137
92 %term ToDoubleTy=138
93 %term ToArrayTy=139
94 %term ToPointerTy=140
95
96 %term Call=CallOPCODE
97 %term Shl=ShlOPCODE
98 %term Shr=ShrOPCODE
99 %term VANext=VANextOPCODE
100 %term VAArg=VAArgOPCODE
101                 /* 33...46 are unused */
102     /*
103      * The foll. values should match the constants in InstrForest.h
104      */
105 %term VRegList=97
106 %term VReg=98
107 %term Constant=99
108 %term Label=100
109                 /* 50+i is a variant of i, as defined above */
110
111
112 %%
113 /*-----------------------------------------------------------------------*
114  * The productions of the grammar.
115  * Note that all chain rules are numbered 101 and above.
116  * Also, a special case of production X is numbered 100+X, 200+X, etc.
117  * The cost of a 1-cycle operation is represented as 10, to allow
118  * finer comparisons of costs (effectively, fractions of 1/10).
119  *-----------------------------------------------------------------------*/
120
121         /*
122          * The top-level statements
123          */
124 stmt:   Ret                     =   1 (30);
125 stmt:   RetValue(reg)           =   2 (30);
126 stmt:   Store(reg,reg)          =   3 (10);
127 stmt:   Store(reg,ptrreg)       =   4 (10);
128 stmt:   BrUncond                =   5 (20);
129 stmt:   BrCond(setCC)           =   6 (20);     /* branch on cond. code */
130 stmt:   BrCond(setCCconst)      = 206 (10);     /* may save one instruction */
131 stmt:   BrCond(reg)             =   8 (20);     /* may avoid an extra instr */
132 stmt:   BrCond(Constant)        = 208 (20);     /* may avoid an extra instr */
133 stmt:   Switch(reg)             =   9 (30);     /* cost = load + branch */
134
135 stmt:   reg                     =  111 (0);
136
137         /*
138          * List node used for nodes with more than 2 children
139          */
140 reg:    VRegList(reg,reg)       =  10 (0);
141
142         /*
143          * Special case non-terminals to help combine unary instructions.
144          *      Eg1:  zdouble <- todouble(xfloat) * todouble(yfloat)
145          *      Eg2:  c       <- a AND (NOT b).
146          * Note that the costs are counted for the special non-terminals here,
147          * and should not be counted again for the reg productions later.
148          */
149 not:      Not(reg,reg)          =   21 (10);
150 tobool:   ToBoolTy(reg)         =   22 (10);
151 not:      Not(tobool, reg)      =  322 (10); // fold cast-to-bool into not
152 toubyte:  ToUByteTy(reg)        =   23 (10);
153 tosbyte:  ToSByteTy(reg)        =   24 (10);
154 toushort: ToUShortTy(reg)       =   25 (10);
155 toshort:  ToShortTy(reg)        =   26 (10);
156 touint:   ToUIntTy(reg)         =   27 (10);
157 toint:    ToIntTy(reg)          =   28 (10);
158 toulong:  ToULongTy(reg)        =   29 (10);
159 tolong:   ToLongTy(reg)         =   30 (10);
160 tofloat:  ToFloatTy(reg)        =   31 (10);
161 todouble: ToDoubleTy(reg)       =   32 (10);
162 todoubleConst: ToDoubleTy(Constant) = 232 (10);
163
164         /*
165          * All the ways to produce a boolean value (Not and ToBoolTy are above):
166          * -- boolean operators: Not, And, Or, ..., ToBoolTy, SetCC
167          * -- an existing boolean register not in the same tree
168          * -- a boolean constant
169          * 
170          * For And, Or, Xor, we add special cases for when:
171          * (a) one operand is a constant.
172          * (b) one operand is a NOT, to use the ANDN, ORN, and XORN instrns.
173          * We do not need the cases when both operands are constant
174          * because constant folding should take care of that beforehand.
175          */
176 reg:    And(reg,reg)            =   38 (10);
177 reg:    And(reg,not)            =  138 (0);     /* cost is counted for not */
178 reg:    And(reg,Constant)       =  238 (10);
179 reg:    Or (reg,reg)            =   39 (10);
180 reg:    Or (reg,not)            =  139 (0);     /* cost is counted for not */
181 reg:    Or (reg,Constant)       =  239 (10);
182 reg:    Xor(reg,reg)            =   40 (10);
183 reg:    Xor(reg,not)            =  140 (0);     /* cost is counted for not */
184 reg:    Xor(reg,Constant)       =  240 (10);
185
186         /* Special case non-terms for BrCond(setCC) and BrCond(setCCconst) */
187 setCCconst: SetCC(reg,Constant) =   41 (5);
188 setCC:      SetCC(reg,reg)      =   42 (10);
189
190 reg:    not                     =  221 (0);
191 reg:    tobool                  =  222 (0);
192 reg:    setCCconst              =  241 (0);
193 reg:    setCC                   =  242 (0);
194
195         /*
196          * Special case non-terminals for the unary cast operators.
197          * Some of these can be folded into other operations (e.g., todouble).
198          * The rest are just for uniformity.
199          */
200 reg:    toubyte                 =  123 (0);
201 reg:    tosbyte                 =  124 (0);
202 reg:    toushort                =  125 (0);
203 reg:    toshort                 =  126 (0);
204 reg:    touint                  =  127 (0);
205 reg:    toint                   =  128 (0);
206 reg:    toulong                 =  129 (0);
207 reg:    tolong                  =  130 (0);
208 reg:    tofloat                 =  131 (0);
209 reg:    todouble                =  132 (0);
210 reg:    todoubleConst           =  133 (0);
211
212 reg:    ToArrayTy(reg)          =  19 (10);
213 reg:    ToPointerTy(reg)        =  20 (10);
214
215         /*
216          * The binary arithmetic operators.
217          */
218 reg:    Add(reg,reg)            =   33 (10);
219 reg:    Sub(reg,reg)            =   34 (10);
220 reg:    Mul(reg,reg)            =   35 (30);
221 reg:    Mul(todouble,todouble)  =  135 (20);    /* avoids 1-2 type converts */
222 reg:    Div(reg,reg)            =   36 (60);
223 reg:    Rem(reg,reg)            =   37 (60);
224
225         /*
226          * The binary bitwise logical operators.
227          */
228 reg:    BAnd(reg,reg)           =  338 (10);
229 reg:    BAnd(reg,bnot)          =  438 ( 0);    /* cost is counted for not */
230 reg:    BOr( reg,reg)           =  339 (10);
231 reg:    BOr( reg,bnot)          =  439 ( 0);    /* cost is counted for not */
232 reg:    BXor(reg,reg)           =  340 (10);
233 reg:    BXor(reg,bnot)          =  440 ( 0);    /* cost is counted for not */
234
235 reg:    bnot                    =  321 ( 0);
236 bnot:   BNot(reg,reg)           =  421 (10);
237
238         /*
239          * Special cases for the binary operators with one constant argument.
240          * Not and BNot are effectively just one argument, so not needed here.
241          */
242 reg:    Add(reg,Constant)       =  233 (10);
243 reg:    Sub(reg,Constant)       =  234 (10);
244 reg:    Mul(reg,Constant)       =  235 (30);
245 reg:    Mul(todouble,todoubleConst) = 335 (20); /* avoids 1-2 type converts */
246 reg:    Div(reg,Constant)       =  236 (60);
247 reg:    Rem(reg,Constant)       =  237 (60);
248
249 reg:    BAnd(reg,Constant)      =  538 (0);
250 reg:    BOr( reg,Constant)      =  539 (0);
251 reg:    BXor(reg,Constant)      =  540 (0);
252
253         /*
254          * Memory access instructions
255          */
256 reg:    Load(reg)               =   51 (30);
257 reg:    Load(ptrreg)            =   52 (20);    /* 1 counted for ptrreg */
258 reg:    ptrreg                  =  155 (0);
259 ptrreg: GetElemPtr(reg)         =   55 (10);
260 ptrreg: GetElemPtrIdx(reg,reg)  =   56 (10);
261 reg:    Alloca                  =   57 (10);
262 reg:    AllocaN(reg)            =   58 (10);
263
264         /*
265          * Other operators producing register values
266          */
267 reg:    Call                    =   61 (20);    /* just ignore the operands! */
268 reg:    Shl(reg,reg)            =   62 (20);    /* 1 for issue restrictions */
269 reg:    Shr(reg,reg)            =   63 (20);    /* 1 for issue restrictions */
270 reg:    Phi(reg,reg)            =   64 (0);
271 reg:    VANext(reg)             =   65 (40);    /* incr stack slot pointer */
272 reg:    VAArg(reg)              =   66 (40);    /* get a vararg */
273
274         /*
275          * Finally, leaf nodes of expression trees.
276          */
277 reg:    VReg                    =   71 (0);
278 reg:    Constant                =   72 (3);     /* prefer direct use */
279
280
281
282 %%
283 /*-----------------------------------------------------------------------*
284  * The rest of this file provides code to print the cover produced
285  * by BURG and information about computed tree cost and matches.
286  * This code was taken from sample.gr provided with BURG.
287  *-----------------------------------------------------------------------*/
288
289 void printcover(NODEPTR_TYPE p, int goalnt, int indent) {
290         int eruleno = burm_rule(STATE_LABEL(p), goalnt);
291         short *nts = burm_nts[eruleno];
292         NODEPTR_TYPE kids[10];
293         int i;
294         
295         if (eruleno == 0) {
296                 printf("no cover\n");
297                 return;
298         }
299         for (i = 0; i < indent; i++)
300                 printf(".");
301         printf("%s\n", burm_string[eruleno]);
302         burm_kids(p, eruleno, kids);
303         for (i = 0; nts[i]; i++)
304                 printcover(kids[i], nts[i], indent+1);
305 }
306
307 void printtree(NODEPTR_TYPE p) {
308         int op = burm_op_label(p);
309
310         printf("%s", burm_opname[op]);
311         switch (burm_arity[op]) {
312         case 0:
313                 break;
314         case 1:
315                 printf("(");
316                 printtree(burm_child(p, 0));
317                 printf(")");
318                 break;
319         case 2:
320                 printf("(");
321                 printtree(burm_child(p, 0));
322                 printf(", ");
323                 printtree(burm_child(p, 1));
324                 printf(")");
325                 break;
326         }
327 }
328
329 int treecost(NODEPTR_TYPE p, int goalnt, int costindex) {
330         int eruleno = burm_rule(STATE_LABEL(p), goalnt);
331         int cost = burm_cost[eruleno][costindex], i;
332         short *nts = burm_nts[eruleno];
333         NODEPTR_TYPE kids[10];
334
335         burm_kids(p, eruleno, kids);
336         for (i = 0; nts[i]; i++)
337                 cost += treecost(kids[i], nts[i], costindex);
338         return cost;
339 }
340
341 void printMatches(NODEPTR_TYPE p) {
342         int nt;
343         int eruleno;
344
345         printf("Node 0x%lx= ", (unsigned long)p);
346         printtree(p);
347         printf(" matched rules:\n");
348         for (nt = 1; burm_ntname[nt] != (char*)NULL; nt++)
349                 if ((eruleno = burm_rule(STATE_LABEL(p), nt)) != 0)
350                         printf("\t%s\n", burm_string[eruleno]);
351 }