| Vikram S. Adve | 6568239 | 2001-07-21 12:42:19 +0000 | [diff] [blame^] | 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 | /* 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 | */ | 
|  | 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 |  | 
|  | 167 | /* | 
|  | 168 | * Memory access instructions | 
|  | 169 | */ | 
|  | 170 | reg:	Load(reg)		=   51 (3); | 
|  | 171 | reg:	Load(ptrreg)		=   52 (2);	/* 1 counted for ptrreg */ | 
|  | 172 | reg:	LoadIdx(reg,reg)	=   53 (3); | 
|  | 173 | reg:	LoadIdx(ptrreg,reg)	=   54 (2);	/* 1 counted for ptrreg */ | 
|  | 174 | reg:	ptrreg			=  155 (0); | 
|  | 175 | ptrreg:	GetElemPtr(reg)		=   55 (1); | 
|  | 176 | ptrreg:	GetElemPtrIdx(reg,reg)	=   56 (1); | 
|  | 177 | reg:	Alloca			=   57 (1); | 
|  | 178 | reg:	AllocaN(reg)		=   58 (1); | 
|  | 179 |  | 
|  | 180 | /* | 
|  | 181 | * Other operators producing register values | 
|  | 182 | */ | 
|  | 183 | reg:	Call			=   61 (0); | 
|  | 184 | reg:	Shl(reg,reg)		=   62 (1); | 
|  | 185 | reg:	Shr(reg,reg)		=   63 (1); | 
|  | 186 |  | 
|  | 187 | /* | 
|  | 188 | * Finally, leaf nodes of expression trees (other than boolreg) | 
|  | 189 | */ | 
|  | 190 | reg:	VReg			=   71 (0); | 
|  | 191 | reg:	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 |  | 
|  | 202 | static char rcsid[] = "$Id$"; | 
|  | 203 |  | 
|  | 204 | #ifdef __STDC__ | 
|  | 205 | void printcover(NODEPTR_TYPE p, int goalnt, int indent) { | 
|  | 206 | #else | 
|  | 207 | void 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__ | 
|  | 227 | void printtree(NODEPTR_TYPE p) { | 
|  | 228 | #else | 
|  | 229 | void 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__ | 
|  | 253 | int treecost(NODEPTR_TYPE p, int goalnt, int costindex) { | 
|  | 254 | #else | 
|  | 255 | int 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__ | 
|  | 269 | void printMatches(NODEPTR_TYPE p) { | 
|  | 270 | #else | 
|  | 271 | void 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 | } |