blob: 09642661fdb951357d2691a978c1a8499778314b [file] [log] [blame]
Reid Spencer96839be2006-11-30 16:50:26 +00001//===-- UpgradeParser.y - Upgrade parser for llvm assmbly -------*- C++ -*-===//
Reid Spencere7c3c602006-11-30 06:36:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Reid Spencer and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Reid Spencer96839be2006-11-30 16:50:26 +000010// This file implements the bison parser for LLVM 1.9 assembly language.
Reid Spencere7c3c602006-11-30 06:36:44 +000011//
12//===----------------------------------------------------------------------===//
13
14%{
Reid Spencere7c3c602006-11-30 06:36:44 +000015#include "ParserInternals.h"
Reid Spencere7c3c602006-11-30 06:36:44 +000016#include <algorithm>
Reid Spencera50d5962006-12-02 04:11:07 +000017#include <map>
Reid Spencere7c3c602006-11-30 06:36:44 +000018#include <utility>
19#include <iostream>
20
Reid Spencere77e35e2006-12-01 20:26:20 +000021#define YYERROR_VERBOSE 1
Reid Spencer96839be2006-11-30 16:50:26 +000022#define YYINCLUDED_STDLIB_H
Reid Spencere77e35e2006-12-01 20:26:20 +000023#define YYDEBUG 1
Reid Spencere7c3c602006-11-30 06:36:44 +000024
25int yylex(); // declaration" of xxx warnings.
26int yyparse();
Reid Spencere77e35e2006-12-01 20:26:20 +000027extern int yydebug;
Reid Spencere7c3c602006-11-30 06:36:44 +000028
29static std::string CurFilename;
Reid Spencere7c3c602006-11-30 06:36:44 +000030static std::ostream *O = 0;
Reid Spencer96839be2006-11-30 16:50:26 +000031std::istream* LexInput = 0;
Reid Spencere77e35e2006-12-01 20:26:20 +000032unsigned SizeOfPointer = 32;
Reid Spencerf459d392006-12-02 16:19:52 +000033static uint64_t unique = 1;
Reid Spencer96839be2006-11-30 16:50:26 +000034
Reid Spencer71d2ec92006-12-31 06:02:26 +000035// This bool controls whether attributes are ever added to function declarations
36// definitions and calls.
37static bool AddAttributes = false;
38
Reid Spencerf5626a32007-01-01 01:20:41 +000039// This bool is used to communicate between the InstVal and Inst rules about
40// whether or not a cast should be deleted. When the flag is set, InstVal has
41// determined that the cast is a candidate. However, it can only be deleted if
42// the value being casted is the same value name as the instruction. The Inst
43// rule makes that comparison if the flag is set and comments out the
44// instruction if they match.
45static bool deleteUselessCastFlag = false;
46static std::string* deleteUselessCastName = 0;
47
Reid Spencera50d5962006-12-02 04:11:07 +000048typedef std::vector<TypeInfo> TypeVector;
49static TypeVector EnumeratedTypes;
50typedef std::map<std::string,TypeInfo> TypeMap;
51static TypeMap NamedTypes;
Reid Spencerf12ee422006-12-05 19:21:25 +000052static TypeMap Globals;
Reid Spencera50d5962006-12-02 04:11:07 +000053
Reid Spencerf8483652006-12-02 15:16:01 +000054void destroy(ValueList* VL) {
55 while (!VL->empty()) {
56 ValueInfo& VI = VL->back();
57 VI.destroy();
58 VL->pop_back();
59 }
60 delete VL;
61}
62
Reid Spencer96839be2006-11-30 16:50:26 +000063void UpgradeAssembly(const std::string &infile, std::istream& in,
Reid Spencer71d2ec92006-12-31 06:02:26 +000064 std::ostream &out, bool debug, bool addAttrs)
Reid Spencere7c3c602006-11-30 06:36:44 +000065{
66 Upgradelineno = 1;
67 CurFilename = infile;
Reid Spencer96839be2006-11-30 16:50:26 +000068 LexInput = &in;
Reid Spencere77e35e2006-12-01 20:26:20 +000069 yydebug = debug;
Reid Spencer71d2ec92006-12-31 06:02:26 +000070 AddAttributes = addAttrs;
Reid Spencere7c3c602006-11-30 06:36:44 +000071 O = &out;
72
73 if (yyparse()) {
74 std::cerr << "Parse failed.\n";
75 exit(1);
76 }
77}
78
Reid Spencer52402b02007-01-02 05:45:11 +000079TypeInfo* ResolveType(TypeInfo*& Ty) {
80 if (Ty->isUnresolved()) {
81 TypeMap::iterator I = NamedTypes.find(Ty->getNewTy());
Reid Spencer78720742006-12-02 20:21:22 +000082 if (I != NamedTypes.end()) {
Reid Spencer52402b02007-01-02 05:45:11 +000083 Ty = I->second.clone();
84 return Ty;
Reid Spencer78720742006-12-02 20:21:22 +000085 } else {
Reid Spencer52402b02007-01-02 05:45:11 +000086 std::string msg("Cannot resolve type: ");
87 msg += Ty->getNewTy();
Reid Spencera50d5962006-12-02 04:11:07 +000088 yyerror(msg.c_str());
Reid Spencer280d8012006-12-01 23:40:53 +000089 }
Reid Spencer52402b02007-01-02 05:45:11 +000090 } else if (Ty->isNumeric()) {
91 unsigned ref = atoi(&((Ty->getNewTy().c_str())[1])); // Skip the '\\'
Reid Spencera50d5962006-12-02 04:11:07 +000092 if (ref < EnumeratedTypes.size()) {
Reid Spencer52402b02007-01-02 05:45:11 +000093 Ty = EnumeratedTypes[ref].clone();
94 return Ty;
Reid Spencera50d5962006-12-02 04:11:07 +000095 } else {
96 std::string msg("Can't resolve type: ");
Reid Spencer52402b02007-01-02 05:45:11 +000097 msg += Ty->getNewTy();
Reid Spencera50d5962006-12-02 04:11:07 +000098 yyerror(msg.c_str());
99 }
Reid Spencer280d8012006-12-01 23:40:53 +0000100 }
Reid Spencera50d5962006-12-02 04:11:07 +0000101 // otherwise its already resolved.
Reid Spencer52402b02007-01-02 05:45:11 +0000102 return Ty;
Reid Spencer280d8012006-12-01 23:40:53 +0000103}
104
Reid Spencera50d5962006-12-02 04:11:07 +0000105static const char* getCastOpcode(
Reid Spencer52402b02007-01-02 05:45:11 +0000106 std::string& Source, const TypeInfo* SrcTy, const TypeInfo* DstTy)
Reid Spencera50d5962006-12-02 04:11:07 +0000107{
Reid Spencer52402b02007-01-02 05:45:11 +0000108 unsigned SrcBits = SrcTy->getBitWidth();
109 unsigned DstBits = DstTy->getBitWidth();
Reid Spencere77e35e2006-12-01 20:26:20 +0000110 const char* opcode = "bitcast";
111 // Run through the possibilities ...
Reid Spencer52402b02007-01-02 05:45:11 +0000112 if (DstTy->isIntegral()) { // Casting to integral
113 if (SrcTy->isIntegral()) { // Casting from integral
Reid Spencere77e35e2006-12-01 20:26:20 +0000114 if (DstBits < SrcBits)
115 opcode = "trunc";
116 else if (DstBits > SrcBits) { // its an extension
Reid Spencer52402b02007-01-02 05:45:11 +0000117 if (SrcTy->isSigned())
Reid Spencere77e35e2006-12-01 20:26:20 +0000118 opcode ="sext"; // signed -> SEXT
119 else
120 opcode = "zext"; // unsigned -> ZEXT
121 } else {
122 opcode = "bitcast"; // Same size, No-op cast
123 }
Reid Spencer52402b02007-01-02 05:45:11 +0000124 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
125 if (DstTy->isSigned())
Reid Spencere77e35e2006-12-01 20:26:20 +0000126 opcode = "fptosi"; // FP -> sint
127 else
128 opcode = "fptoui"; // FP -> uint
Reid Spencer52402b02007-01-02 05:45:11 +0000129 } else if (SrcTy->isPacked()) {
130 assert(DstBits == SrcTy->getBitWidth() &&
Reid Spencere77e35e2006-12-01 20:26:20 +0000131 "Casting packed to integer of different width");
132 opcode = "bitcast"; // same size, no-op cast
133 } else {
Reid Spencer52402b02007-01-02 05:45:11 +0000134 assert(SrcTy->isPointer() &&
Reid Spencere77e35e2006-12-01 20:26:20 +0000135 "Casting from a value that is not first-class type");
136 opcode = "ptrtoint"; // ptr -> int
137 }
Reid Spencer52402b02007-01-02 05:45:11 +0000138 } else if (DstTy->isFloatingPoint()) { // Casting to floating pt
139 if (SrcTy->isIntegral()) { // Casting from integral
140 if (SrcTy->isSigned())
Reid Spencere77e35e2006-12-01 20:26:20 +0000141 opcode = "sitofp"; // sint -> FP
142 else
143 opcode = "uitofp"; // uint -> FP
Reid Spencer52402b02007-01-02 05:45:11 +0000144 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
Reid Spencere77e35e2006-12-01 20:26:20 +0000145 if (DstBits < SrcBits) {
146 opcode = "fptrunc"; // FP -> smaller FP
147 } else if (DstBits > SrcBits) {
148 opcode = "fpext"; // FP -> larger FP
149 } else {
150 opcode ="bitcast"; // same size, no-op cast
151 }
Reid Spencer52402b02007-01-02 05:45:11 +0000152 } else if (SrcTy->isPacked()) {
153 assert(DstBits == SrcTy->getBitWidth() &&
Reid Spencere77e35e2006-12-01 20:26:20 +0000154 "Casting packed to floating point of different width");
155 opcode = "bitcast"; // same size, no-op cast
156 } else {
157 assert(0 && "Casting pointer or non-first class to float");
158 }
Reid Spencer52402b02007-01-02 05:45:11 +0000159 } else if (DstTy->isPacked()) {
160 if (SrcTy->isPacked()) {
161 assert(DstTy->getBitWidth() == SrcTy->getBitWidth() &&
Reid Spencere77e35e2006-12-01 20:26:20 +0000162 "Casting packed to packed of different widths");
163 opcode = "bitcast"; // packed -> packed
Reid Spencer52402b02007-01-02 05:45:11 +0000164 } else if (DstTy->getBitWidth() == SrcBits) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000165 opcode = "bitcast"; // float/int -> packed
166 } else {
167 assert(!"Illegal cast to packed (wrong type or size)");
168 }
Reid Spencer52402b02007-01-02 05:45:11 +0000169 } else if (DstTy->isPointer()) {
170 if (SrcTy->isPointer()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000171 opcode = "bitcast"; // ptr -> ptr
Reid Spencer52402b02007-01-02 05:45:11 +0000172 } else if (SrcTy->isIntegral()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000173 opcode = "inttoptr"; // int -> ptr
174 } else {
Reid Spencera50d5962006-12-02 04:11:07 +0000175 assert(!"Casting invalid type to pointer");
Reid Spencere77e35e2006-12-01 20:26:20 +0000176 }
177 } else {
178 assert(!"Casting to type that is not first-class");
179 }
180 return opcode;
181}
182
Reid Spencer52402b02007-01-02 05:45:11 +0000183static std::string getCastUpgrade(const std::string& Src, TypeInfo* SrcTy,
184 TypeInfo* DstTy, bool isConst)
Reid Spencera50d5962006-12-02 04:11:07 +0000185{
186 std::string Result;
187 std::string Source = Src;
Reid Spencer52402b02007-01-02 05:45:11 +0000188 if (SrcTy->isFloatingPoint() && DstTy->isPointer()) {
Reid Spencera50d5962006-12-02 04:11:07 +0000189 // fp -> ptr cast is no longer supported but we must upgrade this
190 // by doing a double cast: fp -> int -> ptr
191 if (isConst)
Reid Spencer71d2ec92006-12-31 06:02:26 +0000192 Source = "i64 fptoui(" + Source + " to i64)";
Reid Spencera50d5962006-12-02 04:11:07 +0000193 else {
Reid Spencerf459d392006-12-02 16:19:52 +0000194 *O << " %cast_upgrade" << unique << " = fptoui " << Source
Reid Spencer71d2ec92006-12-31 06:02:26 +0000195 << " to i64\n";
196 Source = "i64 %cast_upgrade" + llvm::utostr(unique);
Reid Spencera50d5962006-12-02 04:11:07 +0000197 }
198 // Update the SrcTy for the getCastOpcode call below
Reid Spencer52402b02007-01-02 05:45:11 +0000199 delete SrcTy;
200 SrcTy = new TypeInfo("i64", ULongTy);
201 } else if (DstTy->isBool()) {
202 // cast type %x to bool was previously defined as setne type %x, null
203 // The cast semantic is now to truncate, not compare so we must retain
204 // the original intent by replacing the cast with a setne
205 const char* comparator = SrcTy->isPointer() ? ", null" :
206 (SrcTy->isFloatingPoint() ? ", 0.0" :
207 (SrcTy->isBool() ? ", false" : ", 0"));
208 const char* compareOp = SrcTy->isFloatingPoint() ? "fcmp one " : "icmp ne ";
Reid Spencer187ccf82006-12-09 16:57:22 +0000209 if (isConst) {
210 Result = "(" + Source + comparator + ")";
211 Result = compareOp + Result;
212 } else
213 Result = compareOp + Source + comparator;
Reid Spencera50d5962006-12-02 04:11:07 +0000214 return Result; // skip cast processing below
215 }
216 ResolveType(SrcTy);
217 ResolveType(DstTy);
218 std::string Opcode(getCastOpcode(Source, SrcTy, DstTy));
219 if (isConst)
Reid Spencer52402b02007-01-02 05:45:11 +0000220 Result += Opcode + "( " + Source + " to " + DstTy->getNewTy() + ")";
Reid Spencera50d5962006-12-02 04:11:07 +0000221 else
Reid Spencer52402b02007-01-02 05:45:11 +0000222 Result += Opcode + " " + Source + " to " + DstTy->getNewTy();
Reid Spencera50d5962006-12-02 04:11:07 +0000223 return Result;
224}
225
Reid Spencer52402b02007-01-02 05:45:11 +0000226const char* getDivRemOpcode(const std::string& opcode, TypeInfo* TI) {
Reid Spencer78720742006-12-02 20:21:22 +0000227 const char* op = opcode.c_str();
Reid Spencer52402b02007-01-02 05:45:11 +0000228 const TypeInfo* Ty = ResolveType(TI);
229 if (Ty->isPacked())
230 Ty = Ty->getElementType();
Reid Spencer78720742006-12-02 20:21:22 +0000231 if (opcode == "div")
Reid Spencer52402b02007-01-02 05:45:11 +0000232 if (Ty->isFloatingPoint())
Reid Spencer78720742006-12-02 20:21:22 +0000233 op = "fdiv";
Reid Spencer52402b02007-01-02 05:45:11 +0000234 else if (Ty->isUnsigned())
Reid Spencer78720742006-12-02 20:21:22 +0000235 op = "udiv";
Reid Spencer52402b02007-01-02 05:45:11 +0000236 else if (Ty->isSigned())
Reid Spencer78720742006-12-02 20:21:22 +0000237 op = "sdiv";
238 else
239 yyerror("Invalid type for div instruction");
240 else if (opcode == "rem")
Reid Spencer52402b02007-01-02 05:45:11 +0000241 if (Ty->isFloatingPoint())
Reid Spencer78720742006-12-02 20:21:22 +0000242 op = "frem";
Reid Spencer52402b02007-01-02 05:45:11 +0000243 else if (Ty->isUnsigned())
Reid Spencer78720742006-12-02 20:21:22 +0000244 op = "urem";
Reid Spencer52402b02007-01-02 05:45:11 +0000245 else if (Ty->isSigned())
Reid Spencer78720742006-12-02 20:21:22 +0000246 op = "srem";
247 else
248 yyerror("Invalid type for rem instruction");
249 return op;
250}
Reid Spencer229e9362006-12-02 22:14:11 +0000251
252std::string
Reid Spencer52402b02007-01-02 05:45:11 +0000253getCompareOp(const std::string& setcc, const TypeInfo* TI) {
Reid Spencer229e9362006-12-02 22:14:11 +0000254 assert(setcc.length() == 5);
255 char cc1 = setcc[3];
256 char cc2 = setcc[4];
257 assert(cc1 == 'e' || cc1 == 'n' || cc1 == 'l' || cc1 == 'g');
258 assert(cc2 == 'q' || cc2 == 'e' || cc2 == 'e' || cc2 == 't');
259 std::string result("xcmp xxx");
260 result[6] = cc1;
261 result[7] = cc2;
Reid Spencer52402b02007-01-02 05:45:11 +0000262 if (TI->isFloatingPoint()) {
Reid Spencer229e9362006-12-02 22:14:11 +0000263 result[0] = 'f';
Reid Spencere4d87aa2006-12-23 06:05:41 +0000264 result[5] = 'o';
Reid Spencerf0cf1322006-12-07 04:23:03 +0000265 if (cc1 == 'n')
266 result[5] = 'u'; // NE maps to unordered
267 else
268 result[5] = 'o'; // everything else maps to ordered
Reid Spencer52402b02007-01-02 05:45:11 +0000269 } else if (TI->isIntegral() || TI->isPointer()) {
Reid Spencer229e9362006-12-02 22:14:11 +0000270 result[0] = 'i';
271 if ((cc1 == 'e' && cc2 == 'q') || (cc1 == 'n' && cc2 == 'e'))
272 result.erase(5,1);
Reid Spencer52402b02007-01-02 05:45:11 +0000273 else if (TI->isSigned())
Reid Spencer229e9362006-12-02 22:14:11 +0000274 result[5] = 's';
Reid Spencer52402b02007-01-02 05:45:11 +0000275 else if (TI->isUnsigned() || TI->isPointer() || TI->isBool())
Reid Spencer229e9362006-12-02 22:14:11 +0000276 result[5] = 'u';
277 else
278 yyerror("Invalid integral type for setcc");
279 }
280 return result;
281}
282
Reid Spencer52402b02007-01-02 05:45:11 +0000283static TypeInfo* getFunctionReturnType(TypeInfo* PFTy) {
284 ResolveType(PFTy);
285 if (PFTy->isPointer()) {
286 TypeInfo* ElemTy = PFTy->getElementType();
287 ResolveType(ElemTy);
288 if (ElemTy->isFunction())
289 return ElemTy->getResultType()->clone();
290 } else if (PFTy->isFunction()) {
291 return PFTy->getResultType()->clone();
292 }
293 return PFTy->clone();
294}
295
296static TypeInfo* getGEPIndexedType(TypeInfo* PTy, ValueList* idxs) {
297 ResolveType(PTy);
298 assert(PTy->isPointer() && "GEP Operand is not a pointer?");
299 TypeInfo* Result = PTy->getElementType(); // just skip first index
300 ResolveType(Result);
301 for (unsigned i = 1; i < idxs->size(); ++i) {
302 if (Result->isComposite()) {
303 Result = Result->getIndexedType((*idxs)[i]);
304 ResolveType(Result);
305 } else
306 yyerror("Invalid type for index");
307 }
308 return Result->getPointerType();
309}
310
311static std::string makeUniqueName(const std::string *Name, bool isSigned) {
312 const char *suffix = ".u";
313 if (isSigned)
314 suffix = ".s";
315 if ((*Name)[Name->size()-1] == '"') {
316 std::string Result(*Name);
317 Result.insert(Name->size()-1, suffix);
318 return Result;
319 }
320 return *Name + suffix;
321}
322
323// This function handles appending .u or .s to integer value names that
324// were previously unsigned or signed, respectively. This avoids name
325// collisions since the unsigned and signed type planes have collapsed
326// into a single signless type plane.
327static std::string getUniqueName(const std::string *Name, TypeInfo* Ty) {
328 // If its not a symbolic name, don't modify it, probably a constant val.
329 if ((*Name)[0] != '%' && (*Name)[0] != '"')
330 return *Name;
331 // If its a numeric reference, just leave it alone.
332 if (isdigit((*Name)[1]))
333 return *Name;
334
335 // Resolve the type
336 ResolveType(Ty);
337
338 // Default the result to the current name
339 std::string Result = *Name;
340
341 if (Ty->isInteger()) {
342 // If its an integer type, make the name unique
343 Result = makeUniqueName(Name, Ty->isSigned());
344 } else if (Ty->isPointer()) {
345 while (Ty->isPointer())
346 Ty = Ty->getElementType();
347 if (Ty->isInteger())
348 Result = makeUniqueName(Name, Ty->isSigned());
349 }
350 return Result;
351}
352
Reid Spencere7c3c602006-11-30 06:36:44 +0000353%}
354
Reid Spencerf0cf1322006-12-07 04:23:03 +0000355// %file-prefix="UpgradeParser"
Reid Spencere77e35e2006-12-01 20:26:20 +0000356
357%union {
358 std::string* String;
Reid Spencer52402b02007-01-02 05:45:11 +0000359 TypeInfo* Type;
Reid Spencere77e35e2006-12-01 20:26:20 +0000360 ValueInfo Value;
361 ConstInfo Const;
Reid Spencerf8483652006-12-02 15:16:01 +0000362 ValueList* ValList;
Reid Spencer52402b02007-01-02 05:45:11 +0000363 TypeList* TypeVec;
Reid Spencere77e35e2006-12-01 20:26:20 +0000364}
365
Reid Spencerf2d55322006-12-01 21:52:30 +0000366%token <Type> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
Reid Spencera50d5962006-12-02 04:11:07 +0000367%token <Type> FLOAT DOUBLE LABEL
368%token <String> OPAQUE ESINT64VAL EUINT64VAL SINTVAL UINTVAL FPVAL
Reid Spencerf2d55322006-12-01 21:52:30 +0000369%token <String> NULL_TOK UNDEF ZEROINITIALIZER TRUETOK FALSETOK
Reid Spencere77e35e2006-12-01 20:26:20 +0000370%token <String> TYPE VAR_ID LABELSTR STRINGCONSTANT
371%token <String> IMPLEMENTATION BEGINTOK ENDTOK
Reid Spencer71d2ec92006-12-31 06:02:26 +0000372%token <String> DECLARE GLOBAL CONSTANT SECTION VOLATILE
Reid Spencere77e35e2006-12-01 20:26:20 +0000373%token <String> TO DOTDOTDOT CONST INTERNAL LINKONCE WEAK
374%token <String> DLLIMPORT DLLEXPORT EXTERN_WEAK APPENDING
375%token <String> NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG
Reid Spencer78720742006-12-02 20:21:22 +0000376%token <String> ALIGN UNINITIALIZED
Reid Spencere77e35e2006-12-01 20:26:20 +0000377%token <String> DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
378%token <String> CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
379%token <String> X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
380%token <String> DATALAYOUT
Reid Spencer78720742006-12-02 20:21:22 +0000381%token <String> RET BR SWITCH INVOKE EXCEPT UNWIND UNREACHABLE
382%token <String> ADD SUB MUL DIV UDIV SDIV FDIV REM UREM SREM FREM AND OR XOR
Reid Spencere77e35e2006-12-01 20:26:20 +0000383%token <String> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comparators
Reid Spencer229e9362006-12-02 22:14:11 +0000384%token <String> ICMP FCMP EQ NE SLT SGT SLE SGE OEQ ONE OLT OGT OLE OGE
385%token <String> ORD UNO UEQ UNE ULT UGT ULE UGE
Reid Spencere77e35e2006-12-01 20:26:20 +0000386%token <String> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Reid Spencerf7bde222006-12-01 22:26:37 +0000387%token <String> PHI_TOK SELECT SHL SHR ASHR LSHR VAARG
Reid Spencere77e35e2006-12-01 20:26:20 +0000388%token <String> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Reid Spencerfcb5df82006-12-01 22:34:43 +0000389%token <String> CAST TRUNC ZEXT SEXT FPTRUNC FPEXT FPTOUI FPTOSI UITOFP SITOFP
390%token <String> PTRTOINT INTTOPTR BITCAST
Reid Spencere77e35e2006-12-01 20:26:20 +0000391
392%type <String> OptAssign OptLinkage OptCallingConv OptAlign OptCAlign
393%type <String> SectionString OptSection GlobalVarAttributes GlobalVarAttribute
Reid Spencer52402b02007-01-02 05:45:11 +0000394%type <String> ConstExpr DefinitionList
Reid Spencere77e35e2006-12-01 20:26:20 +0000395%type <String> ConstPool TargetDefinition LibrariesDefinition LibList OptName
396%type <String> ArgVal ArgListH ArgList FunctionHeaderH BEGIN FunctionHeader END
Reid Spencer52402b02007-01-02 05:45:11 +0000397%type <String> Function FunctionProto BasicBlock
398%type <String> InstructionList BBTerminatorInst JumpTable Inst
399%type <String> OptTailCall OptVolatile Unwind
400%type <String> SymbolicValueRef OptSideEffect GlobalType
Reid Spencere77e35e2006-12-01 20:26:20 +0000401%type <String> FnDeclareLinkage BasicBlockList BigOrLittle AsmBlock
Reid Spencer78720742006-12-02 20:21:22 +0000402%type <String> Name ConstValueRef ConstVector External
Reid Spencer57f28f92006-12-03 07:10:26 +0000403%type <String> ShiftOps SetCondOps LogicalOps ArithmeticOps CastOps
404%type <String> IPredicates FPredicates
Reid Spencere77e35e2006-12-01 20:26:20 +0000405
Reid Spencerf8483652006-12-02 15:16:01 +0000406%type <ValList> ValueRefList ValueRefListE IndexList
Reid Spencer52402b02007-01-02 05:45:11 +0000407%type <TypeVec> TypeListI ArgTypeListI
Reid Spencere77e35e2006-12-01 20:26:20 +0000408
409%type <Type> IntType SIntType UIntType FPType TypesV Types
410%type <Type> PrimType UpRTypesV UpRTypes
411
Reid Spencerf2d55322006-12-01 21:52:30 +0000412%type <String> IntVal EInt64Val
413%type <Const> ConstVal
Reid Spencere77e35e2006-12-01 20:26:20 +0000414
Reid Spencer52402b02007-01-02 05:45:11 +0000415%type <Value> ValueRef ResolvedVal InstVal PHIList MemoryInst
Reid Spencere7c3c602006-11-30 06:36:44 +0000416
417%start Module
418
419%%
420
421// Handle constant integer size restriction and conversion...
Reid Spencerf2d55322006-12-01 21:52:30 +0000422IntVal : SINTVAL | UINTVAL ;
Reid Spencere77e35e2006-12-01 20:26:20 +0000423EInt64Val : ESINT64VAL | EUINT64VAL;
Reid Spencere7c3c602006-11-30 06:36:44 +0000424
425// Operations that are notably excluded from this list include:
426// RET, BR, & SWITCH because they end basic blocks and are treated specially.
Reid Spencer78720742006-12-02 20:21:22 +0000427ArithmeticOps: ADD | SUB | MUL | DIV | UDIV | SDIV | FDIV
428 | REM | UREM | SREM | FREM;
Reid Spencere7c3c602006-11-30 06:36:44 +0000429LogicalOps : AND | OR | XOR;
430SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
Reid Spencer57f28f92006-12-03 07:10:26 +0000431IPredicates : EQ | NE | SLT | SGT | SLE | SGE | ULT | UGT | ULE | UGE;
432FPredicates : OEQ | ONE | OLT | OGT | OLE | OGE | ORD | UNO | UEQ | UNE
433 | ULT | UGT | ULE | UGE | TRUETOK | FALSETOK;
Reid Spencerf7bde222006-12-01 22:26:37 +0000434ShiftOps : SHL | SHR | ASHR | LSHR;
Reid Spencerfcb5df82006-12-01 22:34:43 +0000435CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | FPTOUI | FPTOSI |
436 UITOFP | SITOFP | PTRTOINT | INTTOPTR | BITCAST | CAST
437 ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000438
439// These are some types that allow classification if we only want a particular
440// thing... for example, only a signed, unsigned, or integral type.
441SIntType : LONG | INT | SHORT | SBYTE;
442UIntType : ULONG | UINT | USHORT | UBYTE;
443IntType : SIntType | UIntType;
444FPType : FLOAT | DOUBLE;
445
446// OptAssign - Value producing statements have an optional assignment component
447OptAssign : Name '=' {
Reid Spencere7c3c602006-11-30 06:36:44 +0000448 $$ = $1;
449 }
450 | /*empty*/ {
451 $$ = new std::string("");
452 };
453
454OptLinkage
455 : INTERNAL | LINKONCE | WEAK | APPENDING | DLLIMPORT | DLLEXPORT
456 | EXTERN_WEAK
457 | /*empty*/ { $$ = new std::string(""); } ;
458
459OptCallingConv
460 : CCC_TOK | CSRETCC_TOK | FASTCC_TOK | COLDCC_TOK | X86_STDCALLCC_TOK
Reid Spencer16244f42006-12-01 21:10:07 +0000461 | X86_FASTCALLCC_TOK
462 | CC_TOK EUINT64VAL {
Reid Spencerf2d55322006-12-01 21:52:30 +0000463 *$1 += *$2;
464 delete $2;
Reid Spencer16244f42006-12-01 21:10:07 +0000465 $$ = $1;
466 }
Reid Spencere7c3c602006-11-30 06:36:44 +0000467 | /*empty*/ { $$ = new std::string(""); } ;
468
469// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
470// a comma before it.
471OptAlign
472 : /*empty*/ { $$ = new std::string(); }
Reid Spencerf2d55322006-12-01 21:52:30 +0000473 | ALIGN EUINT64VAL { *$1 += " " + *$2; delete $2; $$ = $1; };
Reid Spencerf0cf1322006-12-07 04:23:03 +0000474
Reid Spencere7c3c602006-11-30 06:36:44 +0000475OptCAlign
476 : /*empty*/ { $$ = new std::string(); }
477 | ',' ALIGN EUINT64VAL {
478 $2->insert(0, ", ");
Reid Spencerf2d55322006-12-01 21:52:30 +0000479 *$2 += " " + *$3;
480 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000481 $$ = $2;
482 };
483
484SectionString
485 : SECTION STRINGCONSTANT {
486 *$1 += " " + *$2;
487 delete $2;
488 $$ = $1;
489 };
490
491OptSection : /*empty*/ { $$ = new std::string(); }
492 | SectionString;
493
494GlobalVarAttributes
495 : /* empty */ { $$ = new std::string(); }
496 | ',' GlobalVarAttribute GlobalVarAttributes {
497 $2->insert(0, ", ");
498 if (!$3->empty())
499 *$2 += " " + *$3;
500 delete $3;
501 $$ = $2;
502 };
503
504GlobalVarAttribute
505 : SectionString
506 | ALIGN EUINT64VAL {
Reid Spencerf2d55322006-12-01 21:52:30 +0000507 *$1 += " " + *$2;
508 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000509 $$ = $1;
510 };
511
512//===----------------------------------------------------------------------===//
513// Types includes all predefined types... except void, because it can only be
514// used in specific contexts (function returning void for example). To have
515// access to it, a user must explicitly use TypesV.
516//
517
518// TypesV includes all of 'Types', but it also includes the void type.
519TypesV : Types | VOID ;
520UpRTypesV : UpRTypes | VOID ;
521Types : UpRTypes ;
522
523// Derived types are added later...
524//
525PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
Reid Spencere77e35e2006-12-01 20:26:20 +0000526PrimType : LONG | ULONG | FLOAT | DOUBLE | LABEL;
Reid Spencera50d5962006-12-02 04:11:07 +0000527UpRTypes
528 : OPAQUE {
Reid Spencer52402b02007-01-02 05:45:11 +0000529 $$ = new TypeInfo($1, OpaqueTy);
Reid Spencera50d5962006-12-02 04:11:07 +0000530 }
531 | SymbolicValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +0000532 $$ = new TypeInfo($1, UnresolvedTy);
Reid Spencera50d5962006-12-02 04:11:07 +0000533 }
Reid Spencer78720742006-12-02 20:21:22 +0000534 | PrimType {
535 $$ = $1;
536 }
537 | '\\' EUINT64VAL { // Type UpReference
Reid Spencerf2d55322006-12-01 21:52:30 +0000538 $2->insert(0, "\\");
Reid Spencer52402b02007-01-02 05:45:11 +0000539 $$ = new TypeInfo($2, NumericTy);
Reid Spencere7c3c602006-11-30 06:36:44 +0000540 }
541 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Reid Spencer52402b02007-01-02 05:45:11 +0000542 std::string newTy( $1->getNewTy() + "(");
543 for (unsigned i = 0; i < $3->size(); ++i) {
544 if (i != 0)
545 newTy += ", ";
546 if ((*$3)[i]->isVoid())
547 newTy += "...";
548 else
549 newTy += (*$3)[i]->getNewTy();
550 }
551 newTy += ")";
552 $$ = new TypeInfo(new std::string(newTy), $1, $3);
553 EnumeratedTypes.push_back(*$$);
Reid Spencere7c3c602006-11-30 06:36:44 +0000554 }
555 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Reid Spencerf2d55322006-12-01 21:52:30 +0000556 $2->insert(0,"[ ");
Reid Spencer52402b02007-01-02 05:45:11 +0000557 *$2 += " x " + $4->getNewTy() + " ]";
558 uint64_t elems = atoi($2->c_str());
559 $$ = new TypeInfo($2, ArrayTy, $4, elems);
560 EnumeratedTypes.push_back(*$$);
Reid Spencere7c3c602006-11-30 06:36:44 +0000561 }
562 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
Reid Spencerf2d55322006-12-01 21:52:30 +0000563 $2->insert(0,"< ");
Reid Spencer52402b02007-01-02 05:45:11 +0000564 *$2 += " x " + $4->getNewTy() + " >";
565 uint64_t elems = atoi($2->c_str());
566 $$ = new TypeInfo($2, PackedTy, $4, elems);
567 EnumeratedTypes.push_back(*$$);
Reid Spencere7c3c602006-11-30 06:36:44 +0000568 }
569 | '{' TypeListI '}' { // Structure type?
Reid Spencer52402b02007-01-02 05:45:11 +0000570 std::string newTy("{");
571 for (unsigned i = 0; i < $2->size(); ++i) {
572 if (i != 0)
573 newTy += ", ";
574 newTy += (*$2)[i]->getNewTy();
575 }
576 newTy += "}";
577 $$ = new TypeInfo(new std::string(newTy), StructTy, $2);
578 EnumeratedTypes.push_back(*$$);
Reid Spencere7c3c602006-11-30 06:36:44 +0000579 }
580 | '{' '}' { // Empty structure type?
Reid Spencer52402b02007-01-02 05:45:11 +0000581 $$ = new TypeInfo(new std::string("{}"), StructTy, new TypeList());
582 EnumeratedTypes.push_back(*$$);
Reid Spencere7c3c602006-11-30 06:36:44 +0000583 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +0000584 | '<' '{' TypeListI '}' '>' { // Packed Structure type?
Reid Spencer52402b02007-01-02 05:45:11 +0000585 std::string newTy("<{");
586 for (unsigned i = 0; i < $3->size(); ++i) {
587 if (i != 0)
588 newTy += ", ";
589 newTy += (*$3)[i]->getNewTy();
590 }
591 newTy += "}>";
592 $$ = new TypeInfo(new std::string(newTy), PackedStructTy, $3);
593 EnumeratedTypes.push_back(*$$);
Reid Spencer6fd36ab2006-12-29 20:35:03 +0000594 }
595 | '<' '{' '}' '>' { // Empty packed structure type?
Reid Spencer52402b02007-01-02 05:45:11 +0000596 $$ = new TypeInfo(new std::string("<{}>"), PackedStructTy, new TypeList());
597 EnumeratedTypes.push_back(*$$);
Reid Spencer6fd36ab2006-12-29 20:35:03 +0000598 }
Reid Spencere7c3c602006-11-30 06:36:44 +0000599 | UpRTypes '*' { // Pointer type?
Reid Spencer52402b02007-01-02 05:45:11 +0000600 $$ = $1->getPointerType();
601 EnumeratedTypes.push_back(*$$);
Reid Spencere7c3c602006-11-30 06:36:44 +0000602 };
603
604// TypeList - Used for struct declarations and as a basis for function type
605// declaration type lists
606//
Reid Spencere77e35e2006-12-01 20:26:20 +0000607TypeListI
608 : UpRTypes {
Reid Spencer52402b02007-01-02 05:45:11 +0000609 $$ = new TypeList();
610 $$->push_back($1);
Reid Spencere77e35e2006-12-01 20:26:20 +0000611 }
612 | TypeListI ',' UpRTypes {
Reid Spencere7c3c602006-11-30 06:36:44 +0000613 $$ = $1;
Reid Spencer52402b02007-01-02 05:45:11 +0000614 $$->push_back($3);
Reid Spencere7c3c602006-11-30 06:36:44 +0000615 };
616
617// ArgTypeList - List of types for a function type declaration...
Reid Spencere77e35e2006-12-01 20:26:20 +0000618ArgTypeListI
619 : TypeListI
Reid Spencere7c3c602006-11-30 06:36:44 +0000620 | TypeListI ',' DOTDOTDOT {
Reid Spencere7c3c602006-11-30 06:36:44 +0000621 $$ = $1;
Reid Spencer52402b02007-01-02 05:45:11 +0000622 $$->push_back(new TypeInfo("void",VoidTy));
623 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000624 }
625 | DOTDOTDOT {
Reid Spencer52402b02007-01-02 05:45:11 +0000626 $$ = new TypeList();
627 $$->push_back(new TypeInfo("void",VoidTy));
628 delete $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000629 }
630 | /*empty*/ {
Reid Spencer52402b02007-01-02 05:45:11 +0000631 $$ = new TypeList();
Reid Spencere7c3c602006-11-30 06:36:44 +0000632 };
633
634// ConstVal - The various declarations that go into the constant pool. This
635// production is used ONLY to represent constants that show up AFTER a 'const',
636// 'constant' or 'global' token at global scope. Constants that can be inlined
637// into other expressions (such as integers and constexprs) are handled by the
638// ResolvedVal, ValueRef and ConstValueRef productions.
639//
640ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencere77e35e2006-12-01 20:26:20 +0000641 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +0000642 $$.cnst = new std::string($1->getNewTy());
Reid Spencere77e35e2006-12-01 20:26:20 +0000643 *$$.cnst += " [ " + *$3 + " ]";
Reid Spencere7c3c602006-11-30 06:36:44 +0000644 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000645 }
646 | Types '[' ']' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000647 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +0000648 $$.cnst = new std::string($1->getNewTy());
Reid Spencere77e35e2006-12-01 20:26:20 +0000649 *$$.cnst += "[ ]";
Reid Spencere7c3c602006-11-30 06:36:44 +0000650 }
651 | Types 'c' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +0000652 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +0000653 $$.cnst = new std::string($1->getNewTy());
Reid Spencere77e35e2006-12-01 20:26:20 +0000654 *$$.cnst += " c" + *$3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000655 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000656 }
657 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencere77e35e2006-12-01 20:26:20 +0000658 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +0000659 $$.cnst = new std::string($1->getNewTy());
Reid Spencere77e35e2006-12-01 20:26:20 +0000660 *$$.cnst += " < " + *$3 + " >";
Reid Spencere7c3c602006-11-30 06:36:44 +0000661 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000662 }
663 | Types '{' ConstVector '}' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000664 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +0000665 $$.cnst = new std::string($1->getNewTy());
Reid Spencere77e35e2006-12-01 20:26:20 +0000666 *$$.cnst += " { " + *$3 + " }";
Reid Spencere7c3c602006-11-30 06:36:44 +0000667 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000668 }
669 | Types '{' '}' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000670 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +0000671 $$.cnst = new std::string($1->getNewTy());
Reid Spencer0b7e5072006-12-01 22:42:01 +0000672 *$$.cnst += " {}";
Reid Spencere7c3c602006-11-30 06:36:44 +0000673 }
674 | Types NULL_TOK {
Reid Spencere77e35e2006-12-01 20:26:20 +0000675 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +0000676 $$.cnst = new std::string($1->getNewTy());
Reid Spencerf2d55322006-12-01 21:52:30 +0000677 *$$.cnst += " " + *$2;
678 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000679 }
680 | Types UNDEF {
Reid Spencere77e35e2006-12-01 20:26:20 +0000681 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +0000682 $$.cnst = new std::string($1->getNewTy());
Reid Spencerf2d55322006-12-01 21:52:30 +0000683 *$$.cnst += " " + *$2;
684 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000685 }
686 | Types SymbolicValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +0000687 std::string Name = getUniqueName($2,$1);
Reid Spencere77e35e2006-12-01 20:26:20 +0000688 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +0000689 $$.cnst = new std::string($1->getNewTy());
690 *$$.cnst += " " + Name;
Reid Spencere7c3c602006-11-30 06:36:44 +0000691 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000692 }
693 | Types ConstExpr {
Reid Spencere77e35e2006-12-01 20:26:20 +0000694 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +0000695 $$.cnst = new std::string($1->getNewTy());
Reid Spencere77e35e2006-12-01 20:26:20 +0000696 *$$.cnst += " " + *$2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000697 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000698 }
699 | Types ZEROINITIALIZER {
Reid Spencere77e35e2006-12-01 20:26:20 +0000700 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +0000701 $$.cnst = new std::string($1->getNewTy());
Reid Spencerf2d55322006-12-01 21:52:30 +0000702 *$$.cnst += " " + *$2;
703 delete $2;
Reid Spencere77e35e2006-12-01 20:26:20 +0000704 }
705 | SIntType EInt64Val { // integral constants
706 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +0000707 $$.cnst = new std::string($1->getNewTy());
Reid Spencerf2d55322006-12-01 21:52:30 +0000708 *$$.cnst += " " + *$2;
709 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000710 }
Reid Spencer7356ae42007-01-02 06:34:08 +0000711 | UIntType EInt64Val { // integral constants
Reid Spencere77e35e2006-12-01 20:26:20 +0000712 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +0000713 $$.cnst = new std::string($1->getNewTy());
Reid Spencerf2d55322006-12-01 21:52:30 +0000714 *$$.cnst += " " + *$2;
715 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000716 }
717 | BOOL TRUETOK { // Boolean constants
Reid Spencere77e35e2006-12-01 20:26:20 +0000718 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +0000719 $$.cnst = new std::string($1->getNewTy());
Reid Spencerf2d55322006-12-01 21:52:30 +0000720 *$$.cnst += " " + *$2;
721 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000722 }
723 | BOOL FALSETOK { // Boolean constants
Reid Spencere77e35e2006-12-01 20:26:20 +0000724 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +0000725 $$.cnst = new std::string($1->getNewTy());
Reid Spencerf2d55322006-12-01 21:52:30 +0000726 *$$.cnst += " " + *$2;
727 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000728 }
729 | FPType FPVAL { // Float & Double constants
Reid Spencere77e35e2006-12-01 20:26:20 +0000730 $$.type = $1;
Reid Spencer52402b02007-01-02 05:45:11 +0000731 $$.cnst = new std::string($1->getNewTy());
Reid Spencerf2d55322006-12-01 21:52:30 +0000732 *$$.cnst += " " + *$2;
733 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000734 };
735
736
Reid Spencerfcb5df82006-12-01 22:34:43 +0000737ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencer280d8012006-12-01 23:40:53 +0000738 std::string source = *$3.cnst;
Reid Spencer52402b02007-01-02 05:45:11 +0000739 TypeInfo* DstTy = ResolveType($5);
Reid Spencer280d8012006-12-01 23:40:53 +0000740 if (*$1 == "cast") {
Reid Spencera50d5962006-12-02 04:11:07 +0000741 // Call getCastUpgrade to upgrade the old cast
Reid Spencer52402b02007-01-02 05:45:11 +0000742 $$ = new std::string(getCastUpgrade(source, $3.type, DstTy, true));
Reid Spencera50d5962006-12-02 04:11:07 +0000743 } else {
744 // Nothing to upgrade, just create the cast constant expr
745 $$ = new std::string(*$1);
Reid Spencer52402b02007-01-02 05:45:11 +0000746 *$$ += "( " + source + " to " + $5->getNewTy() + ")";
Reid Spencer280d8012006-12-01 23:40:53 +0000747 }
Reid Spencer52402b02007-01-02 05:45:11 +0000748 delete $1; $3.destroy(); delete $4; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +0000749 }
750 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencerf8483652006-12-02 15:16:01 +0000751 *$1 += "(" + *$3.cnst;
752 for (unsigned i = 0; i < $4->size(); ++i) {
753 ValueInfo& VI = (*$4)[i];
754 *$1 += ", " + *VI.val;
755 VI.destroy();
756 }
757 *$1 += ")";
Reid Spencere77e35e2006-12-01 20:26:20 +0000758 $$ = $1;
759 $3.destroy();
760 delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +0000761 }
762 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000763 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
764 $3.destroy(); $5.destroy(); $7.destroy();
765 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000766 }
767 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer78720742006-12-02 20:21:22 +0000768 const char* op = getDivRemOpcode(*$1, $3.type);
769 $$ = new std::string(op);
770 *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
771 delete $1; $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000772 }
773 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000774 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
775 $3.destroy(); $5.destroy();
776 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000777 }
778 | SetCondOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer229e9362006-12-02 22:14:11 +0000779 *$1 = getCompareOp(*$1, $3.type);
Reid Spencere77e35e2006-12-01 20:26:20 +0000780 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
781 $3.destroy(); $5.destroy();
782 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000783 }
Reid Spencer57f28f92006-12-03 07:10:26 +0000784 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
785 *$1 += "(" + *$2 + "," + *$4.cnst + "," + *$6.cnst + ")";
786 delete $2; $4.destroy(); $6.destroy();
787 $$ = $1;
788 }
789 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
Reid Spencer229e9362006-12-02 22:14:11 +0000790 *$1 += "(" + *$2 + "," + *$4.cnst + "," + *$6.cnst + ")";
791 delete $2; $4.destroy(); $6.destroy();
792 $$ = $1;
793 }
Reid Spencere7c3c602006-11-30 06:36:44 +0000794 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Reid Spencerf7bde222006-12-01 22:26:37 +0000795 const char* shiftop = $1->c_str();
796 if (*$1 == "shr")
Reid Spencer52402b02007-01-02 05:45:11 +0000797 shiftop = ($3.type->isUnsigned()) ? "lshr" : "ashr";
Reid Spencerf7bde222006-12-01 22:26:37 +0000798 $$ = new std::string(shiftop);
799 *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
800 delete $1; $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000801 }
802 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000803 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
804 $3.destroy(); $5.destroy();
805 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000806 }
807 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000808 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
809 $3.destroy(); $5.destroy(); $7.destroy();
810 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000811 }
812 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000813 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
814 $3.destroy(); $5.destroy(); $7.destroy();
815 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000816 };
817
818
819// ConstVector - A list of comma separated constants.
Reid Spencere77e35e2006-12-01 20:26:20 +0000820
821ConstVector
822 : ConstVector ',' ConstVal {
823 *$1 += ", " + *$3.cnst;
824 $3.destroy();
825 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000826 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000827 | ConstVal { $$ = new std::string(*$1.cnst); $1.destroy(); }
828 ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000829
830
831// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Reid Spencere77e35e2006-12-01 20:26:20 +0000832GlobalType : GLOBAL | CONSTANT ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000833
834
835//===----------------------------------------------------------------------===//
836// Rules to match Modules
837//===----------------------------------------------------------------------===//
838
839// Module rule: Capture the result of parsing the whole file into a result
840// variable...
841//
842Module : DefinitionList {
843};
844
845// DefinitionList - Top level definitions
846//
847DefinitionList : DefinitionList Function {
848 $$ = 0;
849 }
850 | DefinitionList FunctionProto {
Reid Spencer6fd36ab2006-12-29 20:35:03 +0000851 *O << *$2 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +0000852 delete $2;
853 $$ = 0;
854 }
855 | DefinitionList MODULE ASM_TOK AsmBlock {
Reid Spencer6fd36ab2006-12-29 20:35:03 +0000856 *O << "module asm " << ' ' << *$4 << '\n';
Reid Spencerd154b572006-12-01 20:36:40 +0000857 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000858 }
859 | DefinitionList IMPLEMENTATION {
860 *O << "implementation\n";
Reid Spencerd154b572006-12-01 20:36:40 +0000861 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000862 }
Reid Spencera50d5962006-12-02 04:11:07 +0000863 | ConstPool { $$ = 0; }
Reid Spencere7c3c602006-11-30 06:36:44 +0000864
Reid Spencer78720742006-12-02 20:21:22 +0000865External : EXTERNAL | UNINITIALIZED { $$ = $1; *$$ = "external"; }
866
Reid Spencere7c3c602006-11-30 06:36:44 +0000867// ConstPool - Constants with optional names assigned to them.
868ConstPool : ConstPool OptAssign TYPE TypesV {
Reid Spencer52402b02007-01-02 05:45:11 +0000869 EnumeratedTypes.push_back(*$4);
Reid Spencera50d5962006-12-02 04:11:07 +0000870 if (!$2->empty()) {
Reid Spencer52402b02007-01-02 05:45:11 +0000871 NamedTypes[*$2] = *$4;
Reid Spencera50d5962006-12-02 04:11:07 +0000872 *O << *$2 << " = ";
873 }
Reid Spencer52402b02007-01-02 05:45:11 +0000874 *O << "type " << $4->getNewTy() << '\n';
875 delete $2; delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000876 $$ = 0;
877 }
878 | ConstPool FunctionProto { // Function prototypes can be in const pool
Reid Spencer6fd36ab2006-12-29 20:35:03 +0000879 *O << *$2 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +0000880 delete $2;
881 $$ = 0;
882 }
883 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
Reid Spencer6fd36ab2006-12-29 20:35:03 +0000884 *O << *$2 << ' ' << *$3 << ' ' << *$4 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +0000885 delete $2; delete $3; delete $4;
886 $$ = 0;
887 }
888 | ConstPool OptAssign OptLinkage GlobalType ConstVal GlobalVarAttributes {
Reid Spencerf12ee422006-12-05 19:21:25 +0000889 if (!$2->empty()) {
Reid Spencer52402b02007-01-02 05:45:11 +0000890 std::string Name = getUniqueName($2,$5.type);
891 *O << Name << " = ";
892 Globals[Name] = *$5.type;
Reid Spencerf12ee422006-12-05 19:21:25 +0000893 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +0000894 *O << *$3 << ' ' << *$4 << ' ' << *$5.cnst << ' ' << *$6 << '\n';
Reid Spencer52402b02007-01-02 05:45:11 +0000895 delete $2; delete $3; delete $4; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000896 $$ = 0;
897 }
Reid Spencer78720742006-12-02 20:21:22 +0000898 | ConstPool OptAssign External GlobalType Types GlobalVarAttributes {
Reid Spencerf12ee422006-12-05 19:21:25 +0000899 if (!$2->empty()) {
Reid Spencer52402b02007-01-02 05:45:11 +0000900 std::string Name = getUniqueName($2,$5);
901 *O << Name << " = ";
902 Globals[Name] = *$5;
Reid Spencerf12ee422006-12-05 19:21:25 +0000903 }
Reid Spencer52402b02007-01-02 05:45:11 +0000904 *O << *$3 << ' ' << *$4 << ' ' << $5->getNewTy() << ' ' << *$6 << '\n';
905 delete $2; delete $3; delete $4; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000906 $$ = 0;
907 }
908 | ConstPool OptAssign DLLIMPORT GlobalType Types GlobalVarAttributes {
Reid Spencerf12ee422006-12-05 19:21:25 +0000909 if (!$2->empty()) {
Reid Spencer52402b02007-01-02 05:45:11 +0000910 std::string Name = getUniqueName($2,$5);
911 *O << Name << " = ";
912 Globals[Name] = *$5;
Reid Spencerf12ee422006-12-05 19:21:25 +0000913 }
Reid Spencer52402b02007-01-02 05:45:11 +0000914 *O << *$3 << ' ' << *$4 << ' ' << $5->getNewTy() << ' ' << *$6 << '\n';
915 delete $2; delete $3; delete $4; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000916 $$ = 0;
917 }
918 | ConstPool OptAssign EXTERN_WEAK GlobalType Types GlobalVarAttributes {
Reid Spencerf12ee422006-12-05 19:21:25 +0000919 if (!$2->empty()) {
Reid Spencer52402b02007-01-02 05:45:11 +0000920 std::string Name = getUniqueName($2,$5);
921 *O << Name << " = ";
922 Globals[Name] = *$5;
Reid Spencerf12ee422006-12-05 19:21:25 +0000923 }
Reid Spencer52402b02007-01-02 05:45:11 +0000924 *O << *$3 << ' ' << *$4 << ' ' << $5->getNewTy() << ' ' << *$6 << '\n';
925 delete $2; delete $3; delete $4; delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000926 $$ = 0;
927 }
928 | ConstPool TARGET TargetDefinition {
Reid Spencer6fd36ab2006-12-29 20:35:03 +0000929 *O << *$2 << ' ' << *$3 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +0000930 delete $2; delete $3;
931 $$ = 0;
932 }
933 | ConstPool DEPLIBS '=' LibrariesDefinition {
Reid Spencer6fd36ab2006-12-29 20:35:03 +0000934 *O << *$2 << " = " << *$4 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +0000935 delete $2; delete $4;
936 $$ = 0;
937 }
938 | /* empty: end of list */ {
939 $$ = 0;
940 };
941
942
943AsmBlock : STRINGCONSTANT ;
944
945BigOrLittle : BIG | LITTLE
946
947TargetDefinition
948 : ENDIAN '=' BigOrLittle {
Reid Spencere77e35e2006-12-01 20:26:20 +0000949 *$1 += " = " + *$3;
950 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000951 $$ = $1;
952 }
953 | POINTERSIZE '=' EUINT64VAL {
Reid Spencerf2d55322006-12-01 21:52:30 +0000954 *$1 += " = " + *$3;
955 if (*$3 == "64")
Reid Spencere77e35e2006-12-01 20:26:20 +0000956 SizeOfPointer = 64;
Reid Spencerf2d55322006-12-01 21:52:30 +0000957 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000958 $$ = $1;
959 }
960 | TRIPLE '=' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +0000961 *$1 += " = " + *$3;
962 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000963 $$ = $1;
964 }
965 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +0000966 *$1 += " = " + *$3;
967 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000968 $$ = $1;
969 };
970
971LibrariesDefinition
972 : '[' LibList ']' {
973 $2->insert(0, "[ ");
974 *$2 += " ]";
975 $$ = $2;
976 };
977
978LibList
979 : LibList ',' STRINGCONSTANT {
980 *$1 += ", " + *$3;
981 delete $3;
982 $$ = $1;
983 }
984 | STRINGCONSTANT
985 | /* empty: end of list */ {
986 $$ = new std::string();
987 };
988
989//===----------------------------------------------------------------------===//
990// Rules to match Function Headers
991//===----------------------------------------------------------------------===//
992
993Name : VAR_ID | STRINGCONSTANT;
994OptName : Name | /*empty*/ { $$ = new std::string(); };
995
996ArgVal : Types OptName {
Reid Spencer52402b02007-01-02 05:45:11 +0000997 $$ = new std::string($1->getNewTy());
998 if (!$2->empty()) {
999 std::string Name = getUniqueName($2, $1);
1000 *$$ += " " + Name;
1001 }
Reid Spencere77e35e2006-12-01 20:26:20 +00001002 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001003};
1004
1005ArgListH : ArgListH ',' ArgVal {
1006 *$1 += ", " + *$3;
Reid Spencere77e35e2006-12-01 20:26:20 +00001007 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001008 }
1009 | ArgVal {
1010 $$ = $1;
1011 };
1012
1013ArgList : ArgListH {
1014 $$ = $1;
1015 }
1016 | ArgListH ',' DOTDOTDOT {
1017 *$1 += ", ...";
1018 $$ = $1;
Reid Spencere77e35e2006-12-01 20:26:20 +00001019 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001020 }
1021 | DOTDOTDOT {
1022 $$ = $1;
1023 }
Reid Spencerd154b572006-12-01 20:36:40 +00001024 | /* empty */ { $$ = new std::string(); };
Reid Spencere7c3c602006-11-30 06:36:44 +00001025
Reid Spencer71d2ec92006-12-31 06:02:26 +00001026FunctionHeaderH
1027 : OptCallingConv TypesV Name '(' ArgList ')' OptSection OptAlign {
Reid Spencere7c3c602006-11-30 06:36:44 +00001028 if (!$1->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +00001029 *$1 += " ";
Reid Spencere7c3c602006-11-30 06:36:44 +00001030 }
Reid Spencer52402b02007-01-02 05:45:11 +00001031 *$1 += $2->getNewTy() + " " + *$3 + "(" + *$5 + ")";
Reid Spencere7c3c602006-11-30 06:36:44 +00001032 if (!$7->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +00001033 *$1 += " " + *$7;
Reid Spencere7c3c602006-11-30 06:36:44 +00001034 }
1035 if (!$8->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +00001036 *$1 += " " + *$8;
Reid Spencere7c3c602006-11-30 06:36:44 +00001037 }
Reid Spencere77e35e2006-12-01 20:26:20 +00001038 delete $3;
1039 delete $5;
1040 delete $7;
1041 delete $8;
1042 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001043 };
1044
Reid Spencer78720742006-12-02 20:21:22 +00001045BEGIN : BEGINTOK { $$ = new std::string("{"); delete $1; }
1046 | '{' { $$ = new std::string ("{"); }
Reid Spencere7c3c602006-11-30 06:36:44 +00001047
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001048FunctionHeader
1049 : OptLinkage FunctionHeaderH BEGIN {
1050 *O << "define ";
1051 if (!$1->empty()) {
1052 *O << *$1 << ' ';
1053 }
1054 *O << *$2 << ' ' << *$3 << '\n';
1055 delete $1; delete $2; delete $3;
1056 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001057 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001058 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001059
Reid Spencer78720742006-12-02 20:21:22 +00001060END : ENDTOK { $$ = new std::string("}"); delete $1; }
Reid Spencere7c3c602006-11-30 06:36:44 +00001061 | '}' { $$ = new std::string("}"); };
1062
1063Function : FunctionHeader BasicBlockList END {
1064 if ($2)
1065 *O << *$2;
Reid Spencer71d2ec92006-12-31 06:02:26 +00001066 *O << *$3 << "\n\n";
Reid Spencer52402b02007-01-02 05:45:11 +00001067 delete $1; delete $2; delete $3;
Reid Spencere77e35e2006-12-01 20:26:20 +00001068 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001069};
1070
Reid Spencere77e35e2006-12-01 20:26:20 +00001071FnDeclareLinkage
1072 : /*default*/ { $$ = new std::string(); }
Reid Spencere7c3c602006-11-30 06:36:44 +00001073 | DLLIMPORT
1074 | EXTERN_WEAK
1075 ;
1076
1077FunctionProto
1078 : DECLARE FnDeclareLinkage FunctionHeaderH {
Reid Spencere77e35e2006-12-01 20:26:20 +00001079 if (!$2->empty())
1080 *$1 += " " + *$2;
1081 *$1 += " " + *$3;
1082 delete $2;
1083 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001084 $$ = $1;
1085 };
1086
1087//===----------------------------------------------------------------------===//
1088// Rules to match Basic Blocks
1089//===----------------------------------------------------------------------===//
1090
Reid Spencerd154b572006-12-01 20:36:40 +00001091OptSideEffect : /* empty */ { $$ = new std::string(); }
1092 | SIDEEFFECT;
Reid Spencere7c3c602006-11-30 06:36:44 +00001093
Reid Spencere77e35e2006-12-01 20:26:20 +00001094ConstValueRef
Reid Spencerf2d55322006-12-01 21:52:30 +00001095 : ESINT64VAL | EUINT64VAL | FPVAL | TRUETOK | FALSETOK | NULL_TOK | UNDEF
1096 | ZEROINITIALIZER
Reid Spencere7c3c602006-11-30 06:36:44 +00001097 | '<' ConstVector '>' {
1098 $2->insert(0, "<");
1099 *$2 += ">";
1100 $$ = $2;
1101 }
1102 | ConstExpr
1103 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
1104 if (!$2->empty()) {
1105 *$1 += " " + *$2;
1106 }
Reid Spencere77e35e2006-12-01 20:26:20 +00001107 *$1 += " " + *$3 + ", " + *$5;
1108 delete $2; delete $3; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +00001109 $$ = $1;
1110 };
1111
Reid Spencerf2d55322006-12-01 21:52:30 +00001112SymbolicValueRef : IntVal | Name ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001113
1114// ValueRef - A reference to a definition... either constant or symbolic
Reid Spencerf459d392006-12-02 16:19:52 +00001115ValueRef
1116 : SymbolicValueRef {
1117 $$.val = $1;
1118 $$.constant = false;
Reid Spencer52402b02007-01-02 05:45:11 +00001119 $$.type = new TypeInfo();
Reid Spencerf459d392006-12-02 16:19:52 +00001120 }
1121 | ConstValueRef {
1122 $$.val = $1;
1123 $$.constant = true;
Reid Spencer52402b02007-01-02 05:45:11 +00001124 $$.type = new TypeInfo();
Reid Spencerf459d392006-12-02 16:19:52 +00001125 }
1126 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001127
1128// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1129// type immediately preceeds the value reference, and allows complex constant
1130// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
1131ResolvedVal : Types ValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +00001132 std::string Name = getUniqueName($2.val, $1);
Reid Spencerf459d392006-12-02 16:19:52 +00001133 $$ = $2;
Reid Spencer52402b02007-01-02 05:45:11 +00001134 delete $$.val;
1135 delete $$.type;
1136 $$.val = new std::string($1->getNewTy() + " " + Name);
Reid Spencere77e35e2006-12-01 20:26:20 +00001137 $$.type = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001138 };
1139
1140BasicBlockList : BasicBlockList BasicBlock {
Reid Spencerf2d55322006-12-01 21:52:30 +00001141 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001142 }
1143 | BasicBlock { // Do not allow functions with 0 basic blocks
Reid Spencerf2d55322006-12-01 21:52:30 +00001144 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001145 };
1146
1147
1148// Basic blocks are terminated by branching instructions:
1149// br, br/cc, switch, ret
1150//
Reid Spencer16244f42006-12-01 21:10:07 +00001151BasicBlock : InstructionList BBTerminatorInst {
Reid Spencerf2d55322006-12-01 21:52:30 +00001152 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001153 };
1154
1155InstructionList : InstructionList Inst {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001156 *O << " " << *$2 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001157 delete $2;
1158 $$ = 0;
1159 }
1160 | /* empty */ {
1161 $$ = 0;
1162 }
1163 | LABELSTR {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001164 *O << *$1 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001165 delete $1;
1166 $$ = 0;
1167 };
1168
Reid Spencer78720742006-12-02 20:21:22 +00001169Unwind : UNWIND | EXCEPT { $$ = $1; *$$ = "unwind"; }
1170
Reid Spencere7c3c602006-11-30 06:36:44 +00001171BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001172 *O << " " << *$1 << ' ' << *$2.val << '\n';
Reid Spencere77e35e2006-12-01 20:26:20 +00001173 delete $1; $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001174 $$ = 0;
1175 }
1176 | RET VOID { // Return with no result...
Reid Spencer52402b02007-01-02 05:45:11 +00001177 *O << " " << *$1 << ' ' << $2->getNewTy() << '\n';
1178 delete $1; delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001179 $$ = 0;
1180 }
1181 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer52402b02007-01-02 05:45:11 +00001182 *O << " " << *$1 << ' ' << $2->getNewTy() << ' ' << *$3.val << '\n';
1183 delete $1; delete $2; $3.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001184 $$ = 0;
1185 } // Conditional Branch...
1186 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +00001187 std::string Name = getUniqueName($3.val, $2);
1188 *O << " " << *$1 << ' ' << $2->getNewTy() << ' ' << Name << ", "
1189 << $5->getNewTy() << ' ' << *$6.val << ", " << $8->getNewTy() << ' '
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001190 << *$9.val << '\n';
Reid Spencer52402b02007-01-02 05:45:11 +00001191 delete $1; delete $2; $3.destroy(); delete $5; $6.destroy();
1192 delete $8; $9.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001193 $$ = 0;
1194 }
1195 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencer52402b02007-01-02 05:45:11 +00001196 std::string Name = getUniqueName($3.val, $2);
1197 *O << " " << *$1 << ' ' << $2->getNewTy() << ' ' << Name << ", "
1198 << $5->getNewTy() << ' ' << *$6.val << " [" << *$8 << " ]\n";
1199 delete $1; delete $2; $3.destroy(); delete $5; $6.destroy();
Reid Spencerf459d392006-12-02 16:19:52 +00001200 delete $8;
Reid Spencere7c3c602006-11-30 06:36:44 +00001201 $$ = 0;
1202 }
1203 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencer52402b02007-01-02 05:45:11 +00001204 std::string Name = getUniqueName($3.val, $2);
1205 *O << " " << *$1 << ' ' << $2->getNewTy() << ' ' << Name << ", "
1206 << $5->getNewTy() << ' ' << *$6.val << "[]\n";
1207 delete $1; delete $2; $3.destroy(); delete $5; $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001208 $$ = 0;
1209 }
Reid Spencer16244f42006-12-01 21:10:07 +00001210 | OptAssign INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
Reid Spencer78720742006-12-02 20:21:22 +00001211 TO LABEL ValueRef Unwind LABEL ValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +00001212 TypeInfo* ResTy = getFunctionReturnType($4);
Reid Spencer16244f42006-12-01 21:10:07 +00001213 *O << " ";
Reid Spencer52402b02007-01-02 05:45:11 +00001214 if (!$1->empty()) {
1215 std::string Name = getUniqueName($1, ResTy);
1216 *O << Name << " = ";
1217 }
1218 *O << *$2 << ' ' << *$3 << ' ' << $4->getNewTy() << ' ' << *$5.val << " (";
Reid Spencerf8483652006-12-02 15:16:01 +00001219 for (unsigned i = 0; i < $7->size(); ++i) {
1220 ValueInfo& VI = (*$7)[i];
1221 *O << *VI.val;
1222 if (i+1 < $7->size())
1223 *O << ", ";
1224 VI.destroy();
1225 }
Reid Spencer52402b02007-01-02 05:45:11 +00001226 *O << ") " << *$9 << ' ' << $10->getNewTy() << ' ' << *$11.val << ' '
1227 << *$12 << ' ' << $13->getNewTy() << ' ' << *$14.val << '\n';
1228 delete $1; delete $2; delete $3; delete $4; $5.destroy(); delete $7;
1229 delete $9; delete $10; $11.destroy(); delete $12; delete $13;
Reid Spencerf459d392006-12-02 16:19:52 +00001230 $14.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001231 $$ = 0;
1232 }
Reid Spencer78720742006-12-02 20:21:22 +00001233 | Unwind {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001234 *O << " " << *$1 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001235 delete $1;
1236 $$ = 0;
1237 }
1238 | UNREACHABLE {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001239 *O << " " << *$1 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001240 delete $1;
1241 $$ = 0;
1242 };
1243
1244JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +00001245 *$1 += " " + $2->getNewTy() + " " + *$3 + ", " + $5->getNewTy() + " " +
1246 *$6.val;
1247 delete $2; delete $3; delete $5; $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001248 $$ = $1;
1249 }
1250 | IntType ConstValueRef ',' LABEL ValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +00001251 $2->insert(0, $1->getNewTy() + " " );
1252 *$2 += ", " + $4->getNewTy() + " " + *$5.val;
1253 delete $1; delete $4; $5.destroy();
Reid Spencere77e35e2006-12-01 20:26:20 +00001254 $$ = $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001255 };
1256
1257Inst
1258 : OptAssign InstVal {
Reid Spencerf5626a32007-01-01 01:20:41 +00001259 if (!$1->empty()) {
1260 if (deleteUselessCastFlag && *deleteUselessCastName == *$1) {
1261 *$1 += " = ";
1262 $1->insert(0, "; "); // don't actually delete it, just comment it out
1263 delete deleteUselessCastName;
1264 } else {
Reid Spencer52402b02007-01-02 05:45:11 +00001265 // Get a unique name for the name of this value, based on its type.
1266 *$1 = getUniqueName($1, $2.type) + " = ";
Reid Spencerf5626a32007-01-01 01:20:41 +00001267 }
1268 }
Reid Spencer52402b02007-01-02 05:45:11 +00001269 *$1 += *$2.val;
1270 $2.destroy();
Reid Spencerf5626a32007-01-01 01:20:41 +00001271 deleteUselessCastFlag = false;
Reid Spencere7c3c602006-11-30 06:36:44 +00001272 $$ = $1;
1273 };
1274
1275PHIList
1276 : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencer52402b02007-01-02 05:45:11 +00001277 std::string Name = getUniqueName($3.val, $1);
1278 Name.insert(0, $1->getNewTy() + "[");
1279 Name += "," + *$5.val + "]";
1280 $$.val = new std::string(Name);
1281 $$.type = $1;
1282 $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001283 }
1284 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
Reid Spencer52402b02007-01-02 05:45:11 +00001285 std::string Name = getUniqueName($4.val, $1.type);
1286 *$1.val += ", [" + Name + "," + *$6.val + "]";
Reid Spencerf459d392006-12-02 16:19:52 +00001287 $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001288 $$ = $1;
1289 };
1290
1291
1292ValueRefList
Reid Spencer52402b02007-01-02 05:45:11 +00001293 : ResolvedVal {
Reid Spencerf8483652006-12-02 15:16:01 +00001294 $$ = new ValueList();
1295 $$->push_back($1);
1296 }
Reid Spencere7c3c602006-11-30 06:36:44 +00001297 | ValueRefList ',' ResolvedVal {
Reid Spencere7c3c602006-11-30 06:36:44 +00001298 $$ = $1;
Reid Spencer52402b02007-01-02 05:45:11 +00001299 $$->push_back($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00001300 };
1301
1302// ValueRefListE - Just like ValueRefList, except that it may also be empty!
1303ValueRefListE
Reid Spencerf8483652006-12-02 15:16:01 +00001304 : ValueRefList { $$ = $1; }
1305 | /*empty*/ { $$ = new ValueList(); }
Reid Spencere7c3c602006-11-30 06:36:44 +00001306 ;
1307
1308OptTailCall
1309 : TAIL CALL {
1310 *$1 += " " + *$2;
1311 delete $2;
1312 $$ = $1;
1313 }
1314 | CALL
1315 ;
1316
1317InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencer78720742006-12-02 20:21:22 +00001318 const char* op = getDivRemOpcode(*$1, $2);
Reid Spencer52402b02007-01-02 05:45:11 +00001319 std::string Name1 = getUniqueName($3.val, $2);
1320 std::string Name2 = getUniqueName($5.val, $2);
1321 $$.val = new std::string(op);
1322 *$$.val += " " + $2->getNewTy() + " " + Name1 + ", " + Name2;
1323 $$.type = $2;
1324 delete $1; $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001325 }
1326 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +00001327 std::string Name1 = getUniqueName($3.val, $2);
1328 std::string Name2 = getUniqueName($5.val, $2);
1329 *$1 += " " + $2->getNewTy() + " " + Name1 + ", " + Name2;
1330 $$.val = $1;
1331 $$.type = $2;
1332 $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001333 }
1334 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +00001335 std::string Name1 = getUniqueName($3.val, $2);
1336 std::string Name2 = getUniqueName($5.val, $2);
Reid Spencer229e9362006-12-02 22:14:11 +00001337 *$1 = getCompareOp(*$1, $2);
Reid Spencer52402b02007-01-02 05:45:11 +00001338 *$1 += " " + $2->getNewTy() + " " + Name1 + ", " + Name2;
1339 $$.val = $1;
1340 $$.type = new TypeInfo("bool",BoolTy);
1341 $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001342 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001343 | ICMP IPredicates Types ValueRef ',' ValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +00001344 std::string Name1 = getUniqueName($4.val, $3);
1345 std::string Name2 = getUniqueName($6.val, $3);
1346 *$1 += " " + *$2 + " " + $3->getNewTy() + " " + Name1 + "," + Name2;
1347 $$.val = $1;
1348 $$.type = new TypeInfo("bool",BoolTy);
Reid Spencer57f28f92006-12-03 07:10:26 +00001349 delete $2; $4.destroy(); $6.destroy();
Reid Spencer57f28f92006-12-03 07:10:26 +00001350 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001351 | FCMP FPredicates Types ValueRef ',' ValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +00001352 std::string Name1 = getUniqueName($4.val, $3);
1353 std::string Name2 = getUniqueName($6.val, $3);
1354 *$1 += " " + *$2 + " " + $3->getNewTy() + " " + Name1 + "," + Name2;
1355 $$.val = $1;
1356 $$.type = new TypeInfo("bool",BoolTy);
Reid Spencer229e9362006-12-02 22:14:11 +00001357 delete $2; $4.destroy(); $6.destroy();
Reid Spencer229e9362006-12-02 22:14:11 +00001358 }
Reid Spencere7c3c602006-11-30 06:36:44 +00001359 | NOT ResolvedVal {
Reid Spencer52402b02007-01-02 05:45:11 +00001360 $$ = $2;
1361 $$.val->insert(0, *$1 + " ");
1362 delete $1;
Reid Spencere7c3c602006-11-30 06:36:44 +00001363 }
1364 | ShiftOps ResolvedVal ',' ResolvedVal {
Reid Spencerf7bde222006-12-01 22:26:37 +00001365 const char* shiftop = $1->c_str();
1366 if (*$1 == "shr")
Reid Spencer52402b02007-01-02 05:45:11 +00001367 shiftop = ($2.type->isUnsigned()) ? "lshr" : "ashr";
1368 $$.val = new std::string(shiftop);
1369 *$$.val += " " + *$2.val + ", " + *$4.val;
1370 $$.type = $2.type;
1371 delete $1; delete $2.val; $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001372 }
Reid Spencerfcb5df82006-12-01 22:34:43 +00001373 | CastOps ResolvedVal TO Types {
Reid Spencer280d8012006-12-01 23:40:53 +00001374 std::string source = *$2.val;
Reid Spencer52402b02007-01-02 05:45:11 +00001375 TypeInfo* SrcTy = $2.type;
1376 TypeInfo* DstTy = ResolveType($4);
1377 $$.val = new std::string();
Reid Spencer280d8012006-12-01 23:40:53 +00001378 if (*$1 == "cast") {
Reid Spencer52402b02007-01-02 05:45:11 +00001379 *$$.val += getCastUpgrade(source, SrcTy, DstTy, false);
Reid Spencera50d5962006-12-02 04:11:07 +00001380 } else {
Reid Spencer52402b02007-01-02 05:45:11 +00001381 *$$.val += *$1 + " " + source + " to " + DstTy->getNewTy();
Reid Spencer280d8012006-12-01 23:40:53 +00001382 }
Reid Spencer52402b02007-01-02 05:45:11 +00001383 $$.type = $4;
Reid Spencerf5626a32007-01-01 01:20:41 +00001384 // Check to see if this is a useless cast of a value to the same name
1385 // and the same type. Such casts will probably cause redefinition errors
1386 // when assembled and perform no code gen action so just remove them.
1387 if (*$1 == "cast" || *$1 == "bitcast")
Reid Spencer52402b02007-01-02 05:45:11 +00001388 if ($2.type->isInteger() && DstTy->isInteger() &&
1389 $2.type->getBitWidth() == DstTy->getBitWidth()) {
Reid Spencerf5626a32007-01-01 01:20:41 +00001390 deleteUselessCastFlag = true; // Flag the "Inst" rule
1391 deleteUselessCastName = new std::string(*$2.val); // save the name
1392 size_t pos = deleteUselessCastName->find_first_of("%\"",0);
1393 if (pos != std::string::npos) {
1394 // remove the type portion before val
1395 deleteUselessCastName->erase(0, pos);
1396 }
1397 }
Reid Spencere77e35e2006-12-01 20:26:20 +00001398 delete $1; $2.destroy();
Reid Spencer52402b02007-01-02 05:45:11 +00001399 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001400 }
1401 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001402 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
Reid Spencer52402b02007-01-02 05:45:11 +00001403 $$.val = $1;
1404 $$.type = $4.type;
1405 $2.destroy(); delete $4.val; $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001406 }
1407 | VAARG ResolvedVal ',' Types {
Reid Spencer52402b02007-01-02 05:45:11 +00001408 *$1 += " " + *$2.val + ", " + $4->getNewTy();
1409 $$.val = $1;
1410 $$.type = $4;
1411 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001412 }
1413 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001414 *$1 += " " + *$2.val + ", " + *$4.val;
Reid Spencer52402b02007-01-02 05:45:11 +00001415 $$.val = $1;
1416 ResolveType($2.type);
1417 $$.type = $2.type->getElementType()->clone();
1418 delete $2.val; $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001419 }
1420 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001421 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
Reid Spencer52402b02007-01-02 05:45:11 +00001422 $$.val = $1;
1423 $$.type = $2.type;
1424 delete $2.val; $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001425 }
1426 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001427 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
Reid Spencer52402b02007-01-02 05:45:11 +00001428 $$.val = $1;
1429 $$.type = $2.type;
1430 delete $2.val; $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001431 }
1432 | PHI_TOK PHIList {
Reid Spencer52402b02007-01-02 05:45:11 +00001433 *$1 += " " + *$2.val;
1434 $$.val = $1;
1435 $$.type = $2.type;
1436 delete $2.val;
Reid Spencere7c3c602006-11-30 06:36:44 +00001437 }
1438 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
1439 if (!$2->empty())
1440 *$1 += " " + *$2;
1441 if (!$1->empty())
1442 *$1 += " ";
Reid Spencer52402b02007-01-02 05:45:11 +00001443 *$1 += $3->getNewTy() + " " + *$4.val + "(";
Reid Spencerf8483652006-12-02 15:16:01 +00001444 for (unsigned i = 0; i < $6->size(); ++i) {
1445 ValueInfo& VI = (*$6)[i];
1446 *$1 += *VI.val;
1447 if (i+1 < $6->size())
1448 *$1 += ", ";
1449 VI.destroy();
1450 }
1451 *$1 += ")";
Reid Spencer52402b02007-01-02 05:45:11 +00001452 $$.val = $1;
1453 $$.type = getFunctionReturnType($3);
1454 delete $2; delete $3; $4.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001455 }
1456 | MemoryInst ;
1457
1458
1459// IndexList - List of indices for GEP based instructions...
1460IndexList
Reid Spencerf8483652006-12-02 15:16:01 +00001461 : ',' ValueRefList { $$ = $2; }
1462 | /* empty */ { $$ = new ValueList(); }
Reid Spencere7c3c602006-11-30 06:36:44 +00001463 ;
1464
1465OptVolatile
1466 : VOLATILE
1467 | /* empty */ { $$ = new std::string(); }
1468 ;
1469
1470MemoryInst : MALLOC Types OptCAlign {
Reid Spencer52402b02007-01-02 05:45:11 +00001471 *$1 += " " + $2->getNewTy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001472 if (!$3->empty())
1473 *$1 += " " + *$3;
Reid Spencer52402b02007-01-02 05:45:11 +00001474 $$.val = $1;
1475 $$.type = $2->getPointerType();
1476 delete $2; delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001477 }
1478 | MALLOC Types ',' UINT ValueRef OptCAlign {
Reid Spencer52402b02007-01-02 05:45:11 +00001479 std::string Name = getUniqueName($5.val, $4);
1480 *$1 += " " + $2->getNewTy() + ", " + $4->getNewTy() + " " + Name;
Reid Spencere7c3c602006-11-30 06:36:44 +00001481 if (!$6->empty())
1482 *$1 += " " + *$6;
Reid Spencer52402b02007-01-02 05:45:11 +00001483 $$.val = $1;
1484 $$.type = $2->getPointerType();
1485 delete $2; delete $4; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001486 }
1487 | ALLOCA Types OptCAlign {
Reid Spencer52402b02007-01-02 05:45:11 +00001488 *$1 += " " + $2->getNewTy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001489 if (!$3->empty())
1490 *$1 += " " + *$3;
Reid Spencer52402b02007-01-02 05:45:11 +00001491 $$.val = $1;
1492 $$.type = $2->getPointerType();
1493 delete $2; delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001494 }
1495 | ALLOCA Types ',' UINT ValueRef OptCAlign {
Reid Spencer52402b02007-01-02 05:45:11 +00001496 std::string Name = getUniqueName($5.val, $4);
1497 *$1 += " " + $2->getNewTy() + ", " + $4->getNewTy() + " " + Name;
Reid Spencere7c3c602006-11-30 06:36:44 +00001498 if (!$6->empty())
1499 *$1 += " " + *$6;
Reid Spencer52402b02007-01-02 05:45:11 +00001500 $$.val = $1;
1501 $$.type = $2->getPointerType();
1502 delete $2; delete $4; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001503 }
1504 | FREE ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001505 *$1 += " " + *$2.val;
Reid Spencer52402b02007-01-02 05:45:11 +00001506 $$.val = $1;
1507 $$.type = new TypeInfo("void", VoidTy);
Reid Spencere77e35e2006-12-01 20:26:20 +00001508 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001509 }
1510 | OptVolatile LOAD Types ValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +00001511 std::string Name = getUniqueName($4.val, $3);
Reid Spencere7c3c602006-11-30 06:36:44 +00001512 if (!$1->empty())
1513 *$1 += " ";
Reid Spencer52402b02007-01-02 05:45:11 +00001514 *$1 += *$2 + " " + $3->getNewTy() + " " + Name;
1515 $$.val = $1;
1516 $$.type = $3->getElementType()->clone();
1517 delete $2; delete $3; $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001518 }
1519 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
Reid Spencer52402b02007-01-02 05:45:11 +00001520 std::string Name = getUniqueName($6.val, $5);
Reid Spencere7c3c602006-11-30 06:36:44 +00001521 if (!$1->empty())
1522 *$1 += " ";
Reid Spencer52402b02007-01-02 05:45:11 +00001523 *$1 += *$2 + " " + *$3.val + ", " + $5->getNewTy() + " " + Name;
1524 $$.val = $1;
1525 $$.type = new TypeInfo("void", VoidTy);
1526 delete $2; $3.destroy(); delete $5; $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001527 }
1528 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencer52402b02007-01-02 05:45:11 +00001529 std::string Name = getUniqueName($3.val, $2);
Reid Spencerf459d392006-12-02 16:19:52 +00001530 // Upgrade the indices
1531 for (unsigned i = 0; i < $4->size(); ++i) {
1532 ValueInfo& VI = (*$4)[i];
Reid Spencer52402b02007-01-02 05:45:11 +00001533 if (VI.type->isUnsigned() && !VI.isConstant() &&
1534 VI.type->getBitWidth() < 64) {
Reid Spencerf459d392006-12-02 16:19:52 +00001535 std::string* old = VI.val;
1536 *O << " %gep_upgrade" << unique << " = zext " << *old
Reid Spencer71d2ec92006-12-31 06:02:26 +00001537 << " to i64\n";
1538 VI.val = new std::string("i64 %gep_upgrade" + llvm::utostr(unique++));
Reid Spencer52402b02007-01-02 05:45:11 +00001539 VI.type->setOldTy(ULongTy);
Reid Spencerf459d392006-12-02 16:19:52 +00001540 }
1541 }
Reid Spencer52402b02007-01-02 05:45:11 +00001542 *$1 += " " + $2->getNewTy() + " " + Name;
Reid Spencerf8483652006-12-02 15:16:01 +00001543 for (unsigned i = 0; i < $4->size(); ++i) {
1544 ValueInfo& VI = (*$4)[i];
1545 *$1 += ", " + *VI.val;
Reid Spencerf8483652006-12-02 15:16:01 +00001546 }
Reid Spencer52402b02007-01-02 05:45:11 +00001547 $$.val = $1;
1548 $$.type = getGEPIndexedType($2,$4);
1549 $3.destroy(); delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00001550 };
1551
1552%%
1553
1554int yyerror(const char *ErrorMsg) {
1555 std::string where
1556 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
1557 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
1558 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
1559 if (yychar == YYEMPTY || yychar == 0)
1560 errMsg += "end-of-file.";
1561 else
1562 errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
Reid Spencer71d2ec92006-12-31 06:02:26 +00001563 std::cerr << "llvm-upgrade: " << errMsg << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001564 exit(1);
1565}