blob: 9f17b92e6d47bf7397966e814c9b43d3a36f9b70 [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"
16#include <llvm/ADT/StringExtras.h>
Reid Spencere7c3c602006-11-30 06:36:44 +000017#include <algorithm>
Reid Spencera50d5962006-12-02 04:11:07 +000018#include <map>
Reid Spencere7c3c602006-11-30 06:36:44 +000019#include <utility>
20#include <iostream>
Reid Spencerbec0b592006-12-03 03:16:48 +000021#include <cassert>
Reid Spencere7c3c602006-11-30 06:36:44 +000022
Reid Spencere77e35e2006-12-01 20:26:20 +000023#define YYERROR_VERBOSE 1
Reid Spencer96839be2006-11-30 16:50:26 +000024#define YYINCLUDED_STDLIB_H
Reid Spencere77e35e2006-12-01 20:26:20 +000025#define YYDEBUG 1
Reid Spencere7c3c602006-11-30 06:36:44 +000026
27int yylex(); // declaration" of xxx warnings.
28int yyparse();
Reid Spencere77e35e2006-12-01 20:26:20 +000029extern int yydebug;
Reid Spencere7c3c602006-11-30 06:36:44 +000030
31static std::string CurFilename;
Reid Spencere7c3c602006-11-30 06:36:44 +000032static std::ostream *O = 0;
Reid Spencer96839be2006-11-30 16:50:26 +000033std::istream* LexInput = 0;
Reid Spencere77e35e2006-12-01 20:26:20 +000034unsigned SizeOfPointer = 32;
Reid Spencerf459d392006-12-02 16:19:52 +000035static uint64_t unique = 1;
Reid Spencer96839be2006-11-30 16:50:26 +000036
Reid Spencera50d5962006-12-02 04:11:07 +000037typedef std::vector<TypeInfo> TypeVector;
38static TypeVector EnumeratedTypes;
39typedef std::map<std::string,TypeInfo> TypeMap;
40static TypeMap NamedTypes;
Reid Spencerf12ee422006-12-05 19:21:25 +000041static TypeMap Globals;
Reid Spencera50d5962006-12-02 04:11:07 +000042
Reid Spencerf8483652006-12-02 15:16:01 +000043void destroy(ValueList* VL) {
44 while (!VL->empty()) {
45 ValueInfo& VI = VL->back();
46 VI.destroy();
47 VL->pop_back();
48 }
49 delete VL;
50}
51
Reid Spencer96839be2006-11-30 16:50:26 +000052void UpgradeAssembly(const std::string &infile, std::istream& in,
Reid Spencere77e35e2006-12-01 20:26:20 +000053 std::ostream &out, bool debug)
Reid Spencere7c3c602006-11-30 06:36:44 +000054{
55 Upgradelineno = 1;
56 CurFilename = infile;
Reid Spencer96839be2006-11-30 16:50:26 +000057 LexInput = &in;
Reid Spencere77e35e2006-12-01 20:26:20 +000058 yydebug = debug;
Reid Spencere7c3c602006-11-30 06:36:44 +000059 O = &out;
60
61 if (yyparse()) {
62 std::cerr << "Parse failed.\n";
63 exit(1);
64 }
65}
66
Reid Spencera50d5962006-12-02 04:11:07 +000067static void ResolveType(TypeInfo& Ty) {
68 if (Ty.oldTy == UnresolvedTy) {
69 TypeMap::iterator I = NamedTypes.find(*Ty.newTy);
Reid Spencer78720742006-12-02 20:21:22 +000070 if (I != NamedTypes.end()) {
Reid Spencera50d5962006-12-02 04:11:07 +000071 Ty.oldTy = I->second.oldTy;
Reid Spencer78720742006-12-02 20:21:22 +000072 Ty.elemTy = I->second.elemTy;
73 } else {
Reid Spencera50d5962006-12-02 04:11:07 +000074 std::string msg("Can't resolve type: ");
75 msg += *Ty.newTy;
76 yyerror(msg.c_str());
Reid Spencer280d8012006-12-01 23:40:53 +000077 }
Reid Spencera50d5962006-12-02 04:11:07 +000078 } else if (Ty.oldTy == NumericTy) {
79 unsigned ref = atoi(&((Ty.newTy->c_str())[1])); // Skip the '\\'
80 if (ref < EnumeratedTypes.size()) {
81 Ty.oldTy = EnumeratedTypes[ref].oldTy;
Reid Spencer78720742006-12-02 20:21:22 +000082 Ty.elemTy = EnumeratedTypes[ref].elemTy;
Reid Spencera50d5962006-12-02 04:11:07 +000083 } else {
84 std::string msg("Can't resolve type: ");
85 msg += *Ty.newTy;
86 yyerror(msg.c_str());
87 }
Reid Spencer280d8012006-12-01 23:40:53 +000088 }
Reid Spencera50d5962006-12-02 04:11:07 +000089 // otherwise its already resolved.
Reid Spencer280d8012006-12-01 23:40:53 +000090}
91
Reid Spencera50d5962006-12-02 04:11:07 +000092static const char* getCastOpcode(
93 std::string& Source, const TypeInfo& SrcTy, const TypeInfo& DstTy)
94{
Reid Spencere77e35e2006-12-01 20:26:20 +000095 unsigned SrcBits = SrcTy.getBitWidth();
96 unsigned DstBits = DstTy.getBitWidth();
97 const char* opcode = "bitcast";
98 // Run through the possibilities ...
99 if (DstTy.isIntegral()) { // Casting to integral
100 if (SrcTy.isIntegral()) { // Casting from integral
101 if (DstBits < SrcBits)
102 opcode = "trunc";
103 else if (DstBits > SrcBits) { // its an extension
104 if (SrcTy.isSigned())
105 opcode ="sext"; // signed -> SEXT
106 else
107 opcode = "zext"; // unsigned -> ZEXT
108 } else {
109 opcode = "bitcast"; // Same size, No-op cast
110 }
111 } else if (SrcTy.isFloatingPoint()) { // Casting from floating pt
112 if (DstTy.isSigned())
113 opcode = "fptosi"; // FP -> sint
114 else
115 opcode = "fptoui"; // FP -> uint
116 } else if (SrcTy.isPacked()) {
117 assert(DstBits == SrcTy.getBitWidth() &&
118 "Casting packed to integer of different width");
119 opcode = "bitcast"; // same size, no-op cast
120 } else {
121 assert(SrcTy.isPointer() &&
122 "Casting from a value that is not first-class type");
123 opcode = "ptrtoint"; // ptr -> int
124 }
125 } else if (DstTy.isFloatingPoint()) { // Casting to floating pt
126 if (SrcTy.isIntegral()) { // Casting from integral
127 if (SrcTy.isSigned())
128 opcode = "sitofp"; // sint -> FP
129 else
130 opcode = "uitofp"; // uint -> FP
131 } else if (SrcTy.isFloatingPoint()) { // Casting from floating pt
132 if (DstBits < SrcBits) {
133 opcode = "fptrunc"; // FP -> smaller FP
134 } else if (DstBits > SrcBits) {
135 opcode = "fpext"; // FP -> larger FP
136 } else {
137 opcode ="bitcast"; // same size, no-op cast
138 }
139 } else if (SrcTy.isPacked()) {
140 assert(DstBits == SrcTy.getBitWidth() &&
141 "Casting packed to floating point of different width");
142 opcode = "bitcast"; // same size, no-op cast
143 } else {
144 assert(0 && "Casting pointer or non-first class to float");
145 }
146 } else if (DstTy.isPacked()) {
147 if (SrcTy.isPacked()) {
148 assert(DstTy.getBitWidth() == SrcTy.getBitWidth() &&
149 "Casting packed to packed of different widths");
150 opcode = "bitcast"; // packed -> packed
151 } else if (DstTy.getBitWidth() == SrcBits) {
152 opcode = "bitcast"; // float/int -> packed
153 } else {
154 assert(!"Illegal cast to packed (wrong type or size)");
155 }
156 } else if (DstTy.isPointer()) {
157 if (SrcTy.isPointer()) {
158 opcode = "bitcast"; // ptr -> ptr
159 } else if (SrcTy.isIntegral()) {
160 opcode = "inttoptr"; // int -> ptr
161 } else {
Reid Spencera50d5962006-12-02 04:11:07 +0000162 assert(!"Casting invalid type to pointer");
Reid Spencere77e35e2006-12-01 20:26:20 +0000163 }
164 } else {
165 assert(!"Casting to type that is not first-class");
166 }
167 return opcode;
168}
169
Reid Spencera50d5962006-12-02 04:11:07 +0000170static std::string getCastUpgrade(
171 const std::string& Src, TypeInfo& SrcTy, TypeInfo& DstTy, bool isConst)
172{
173 std::string Result;
174 std::string Source = Src;
175 if (SrcTy.isFloatingPoint() && DstTy.isPointer()) {
176 // fp -> ptr cast is no longer supported but we must upgrade this
177 // by doing a double cast: fp -> int -> ptr
178 if (isConst)
179 Source = "ulong fptoui(" + Source + " to ulong)";
180 else {
Reid Spencerf459d392006-12-02 16:19:52 +0000181 *O << " %cast_upgrade" << unique << " = fptoui " << Source
182 << " to ulong\n";
183 Source = "ulong %cast_upgrade" + llvm::utostr(unique);
Reid Spencera50d5962006-12-02 04:11:07 +0000184 }
185 // Update the SrcTy for the getCastOpcode call below
186 SrcTy.destroy();
187 SrcTy.newTy = new std::string("ulong");
188 SrcTy.oldTy = ULongTy;
Reid Spencer5fe27e72006-12-09 19:41:25 +0000189 } else if (DstTy.oldTy == BoolTy && SrcTy.oldTy != BoolTy) {
Reid Spencera50d5962006-12-02 04:11:07 +0000190 // cast ptr %x to bool was previously defined as setne ptr %x, null
191 // The ptrtoint semantic is to truncate, not compare so we must retain
192 // the original intent by replace the cast with a setne
193 const char* comparator = SrcTy.isPointer() ? ", null" :
194 (SrcTy.isFloatingPoint() ? ", 0.0" : ", 0");
Reid Spencere4d87aa2006-12-23 06:05:41 +0000195 const char* compareOp = SrcTy.isFloatingPoint() ? "fcmp one " : "icmp ne ";
Reid Spencer187ccf82006-12-09 16:57:22 +0000196 if (isConst) {
197 Result = "(" + Source + comparator + ")";
198 Result = compareOp + Result;
199 } else
200 Result = compareOp + Source + comparator;
Reid Spencera50d5962006-12-02 04:11:07 +0000201 return Result; // skip cast processing below
202 }
203 ResolveType(SrcTy);
204 ResolveType(DstTy);
205 std::string Opcode(getCastOpcode(Source, SrcTy, DstTy));
206 if (isConst)
207 Result += Opcode + "( " + Source + " to " + *DstTy.newTy + ")";
208 else
209 Result += Opcode + " " + Source + " to " + *DstTy.newTy;
210 return Result;
211}
212
Reid Spencer78720742006-12-02 20:21:22 +0000213const char* getDivRemOpcode(const std::string& opcode, const TypeInfo& TI) {
214 const char* op = opcode.c_str();
215 TypeInfo Ty = TI;
216 ResolveType(Ty);
217 if (Ty.isPacked())
218 Ty.oldTy = Ty.getElementType();
219 if (opcode == "div")
220 if (Ty.isFloatingPoint())
221 op = "fdiv";
222 else if (Ty.isUnsigned())
223 op = "udiv";
224 else if (Ty.isSigned())
225 op = "sdiv";
226 else
227 yyerror("Invalid type for div instruction");
228 else if (opcode == "rem")
229 if (Ty.isFloatingPoint())
230 op = "frem";
231 else if (Ty.isUnsigned())
232 op = "urem";
233 else if (Ty.isSigned())
234 op = "srem";
235 else
236 yyerror("Invalid type for rem instruction");
237 return op;
238}
Reid Spencer229e9362006-12-02 22:14:11 +0000239
240std::string
241getCompareOp(const std::string& setcc, const TypeInfo& TI) {
242 assert(setcc.length() == 5);
243 char cc1 = setcc[3];
244 char cc2 = setcc[4];
245 assert(cc1 == 'e' || cc1 == 'n' || cc1 == 'l' || cc1 == 'g');
246 assert(cc2 == 'q' || cc2 == 'e' || cc2 == 'e' || cc2 == 't');
247 std::string result("xcmp xxx");
248 result[6] = cc1;
249 result[7] = cc2;
250 if (TI.isFloatingPoint()) {
251 result[0] = 'f';
Reid Spencere4d87aa2006-12-23 06:05:41 +0000252 result[5] = 'o';
Reid Spencerf0cf1322006-12-07 04:23:03 +0000253 if (cc1 == 'n')
254 result[5] = 'u'; // NE maps to unordered
255 else
256 result[5] = 'o'; // everything else maps to ordered
Reid Spencer229e9362006-12-02 22:14:11 +0000257 } else if (TI.isIntegral() || TI.isPointer()) {
258 result[0] = 'i';
259 if ((cc1 == 'e' && cc2 == 'q') || (cc1 == 'n' && cc2 == 'e'))
260 result.erase(5,1);
261 else if (TI.isSigned())
262 result[5] = 's';
Reid Spencer3e5ab8c2006-12-06 06:25:46 +0000263 else if (TI.isUnsigned() || TI.isPointer() || TI.isBool())
Reid Spencer229e9362006-12-02 22:14:11 +0000264 result[5] = 'u';
265 else
266 yyerror("Invalid integral type for setcc");
267 }
268 return result;
269}
270
Reid Spencere7c3c602006-11-30 06:36:44 +0000271%}
272
Reid Spencerf0cf1322006-12-07 04:23:03 +0000273// %file-prefix="UpgradeParser"
Reid Spencere77e35e2006-12-01 20:26:20 +0000274
275%union {
276 std::string* String;
277 TypeInfo Type;
278 ValueInfo Value;
279 ConstInfo Const;
Reid Spencerf8483652006-12-02 15:16:01 +0000280 ValueList* ValList;
Reid Spencere77e35e2006-12-01 20:26:20 +0000281}
282
Reid Spencerf2d55322006-12-01 21:52:30 +0000283%token <Type> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
Reid Spencera50d5962006-12-02 04:11:07 +0000284%token <Type> FLOAT DOUBLE LABEL
285%token <String> OPAQUE ESINT64VAL EUINT64VAL SINTVAL UINTVAL FPVAL
Reid Spencerf2d55322006-12-01 21:52:30 +0000286%token <String> NULL_TOK UNDEF ZEROINITIALIZER TRUETOK FALSETOK
Reid Spencere77e35e2006-12-01 20:26:20 +0000287%token <String> TYPE VAR_ID LABELSTR STRINGCONSTANT
288%token <String> IMPLEMENTATION BEGINTOK ENDTOK
Reid Spencer6fd36ab2006-12-29 20:35:03 +0000289%token <String> DECLARE DEFINE GLOBAL CONSTANT SECTION VOLATILE
Reid Spencere77e35e2006-12-01 20:26:20 +0000290%token <String> TO DOTDOTDOT CONST INTERNAL LINKONCE WEAK
291%token <String> DLLIMPORT DLLEXPORT EXTERN_WEAK APPENDING
292%token <String> NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG
Reid Spencer78720742006-12-02 20:21:22 +0000293%token <String> ALIGN UNINITIALIZED
Reid Spencere77e35e2006-12-01 20:26:20 +0000294%token <String> DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
295%token <String> CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
296%token <String> X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
297%token <String> DATALAYOUT
Reid Spencer78720742006-12-02 20:21:22 +0000298%token <String> RET BR SWITCH INVOKE EXCEPT UNWIND UNREACHABLE
299%token <String> ADD SUB MUL DIV UDIV SDIV FDIV REM UREM SREM FREM AND OR XOR
Reid Spencere77e35e2006-12-01 20:26:20 +0000300%token <String> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comparators
Reid Spencer229e9362006-12-02 22:14:11 +0000301%token <String> ICMP FCMP EQ NE SLT SGT SLE SGE OEQ ONE OLT OGT OLE OGE
302%token <String> ORD UNO UEQ UNE ULT UGT ULE UGE
Reid Spencere77e35e2006-12-01 20:26:20 +0000303%token <String> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Reid Spencerf7bde222006-12-01 22:26:37 +0000304%token <String> PHI_TOK SELECT SHL SHR ASHR LSHR VAARG
Reid Spencere77e35e2006-12-01 20:26:20 +0000305%token <String> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Reid Spencerfcb5df82006-12-01 22:34:43 +0000306%token <String> CAST TRUNC ZEXT SEXT FPTRUNC FPEXT FPTOUI FPTOSI UITOFP SITOFP
307%token <String> PTRTOINT INTTOPTR BITCAST
Reid Spencere77e35e2006-12-01 20:26:20 +0000308
309%type <String> OptAssign OptLinkage OptCallingConv OptAlign OptCAlign
310%type <String> SectionString OptSection GlobalVarAttributes GlobalVarAttribute
311%type <String> ArgTypeListI ConstExpr DefinitionList
312%type <String> ConstPool TargetDefinition LibrariesDefinition LibList OptName
313%type <String> ArgVal ArgListH ArgList FunctionHeaderH BEGIN FunctionHeader END
314%type <String> Function FunctionProto BasicBlock TypeListI
315%type <String> InstructionList BBTerminatorInst JumpTable Inst PHIList
Reid Spencer78720742006-12-02 20:21:22 +0000316%type <String> OptTailCall InstVal OptVolatile Unwind
Reid Spencere77e35e2006-12-01 20:26:20 +0000317%type <String> MemoryInst SymbolicValueRef OptSideEffect GlobalType
318%type <String> FnDeclareLinkage BasicBlockList BigOrLittle AsmBlock
Reid Spencer78720742006-12-02 20:21:22 +0000319%type <String> Name ConstValueRef ConstVector External
Reid Spencer57f28f92006-12-03 07:10:26 +0000320%type <String> ShiftOps SetCondOps LogicalOps ArithmeticOps CastOps
321%type <String> IPredicates FPredicates
Reid Spencere77e35e2006-12-01 20:26:20 +0000322
Reid Spencerf8483652006-12-02 15:16:01 +0000323%type <ValList> ValueRefList ValueRefListE IndexList
Reid Spencere77e35e2006-12-01 20:26:20 +0000324
325%type <Type> IntType SIntType UIntType FPType TypesV Types
326%type <Type> PrimType UpRTypesV UpRTypes
327
Reid Spencerf2d55322006-12-01 21:52:30 +0000328%type <String> IntVal EInt64Val
329%type <Const> ConstVal
Reid Spencere77e35e2006-12-01 20:26:20 +0000330
Reid Spencerf459d392006-12-02 16:19:52 +0000331%type <Value> ValueRef ResolvedVal
Reid Spencere7c3c602006-11-30 06:36:44 +0000332
333%start Module
334
335%%
336
337// Handle constant integer size restriction and conversion...
Reid Spencerf2d55322006-12-01 21:52:30 +0000338IntVal : SINTVAL | UINTVAL ;
Reid Spencere77e35e2006-12-01 20:26:20 +0000339EInt64Val : ESINT64VAL | EUINT64VAL;
Reid Spencere7c3c602006-11-30 06:36:44 +0000340
341// Operations that are notably excluded from this list include:
342// RET, BR, & SWITCH because they end basic blocks and are treated specially.
Reid Spencer78720742006-12-02 20:21:22 +0000343ArithmeticOps: ADD | SUB | MUL | DIV | UDIV | SDIV | FDIV
344 | REM | UREM | SREM | FREM;
Reid Spencere7c3c602006-11-30 06:36:44 +0000345LogicalOps : AND | OR | XOR;
346SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
Reid Spencer57f28f92006-12-03 07:10:26 +0000347IPredicates : EQ | NE | SLT | SGT | SLE | SGE | ULT | UGT | ULE | UGE;
348FPredicates : OEQ | ONE | OLT | OGT | OLE | OGE | ORD | UNO | UEQ | UNE
349 | ULT | UGT | ULE | UGE | TRUETOK | FALSETOK;
Reid Spencerf7bde222006-12-01 22:26:37 +0000350ShiftOps : SHL | SHR | ASHR | LSHR;
Reid Spencerfcb5df82006-12-01 22:34:43 +0000351CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | FPTOUI | FPTOSI |
352 UITOFP | SITOFP | PTRTOINT | INTTOPTR | BITCAST | CAST
353 ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000354
355// These are some types that allow classification if we only want a particular
356// thing... for example, only a signed, unsigned, or integral type.
357SIntType : LONG | INT | SHORT | SBYTE;
358UIntType : ULONG | UINT | USHORT | UBYTE;
359IntType : SIntType | UIntType;
360FPType : FLOAT | DOUBLE;
361
362// OptAssign - Value producing statements have an optional assignment component
363OptAssign : Name '=' {
Reid Spencere7c3c602006-11-30 06:36:44 +0000364 $$ = $1;
365 }
366 | /*empty*/ {
367 $$ = new std::string("");
368 };
369
370OptLinkage
371 : INTERNAL | LINKONCE | WEAK | APPENDING | DLLIMPORT | DLLEXPORT
372 | EXTERN_WEAK
373 | /*empty*/ { $$ = new std::string(""); } ;
374
375OptCallingConv
376 : CCC_TOK | CSRETCC_TOK | FASTCC_TOK | COLDCC_TOK | X86_STDCALLCC_TOK
Reid Spencer16244f42006-12-01 21:10:07 +0000377 | X86_FASTCALLCC_TOK
378 | CC_TOK EUINT64VAL {
Reid Spencerf2d55322006-12-01 21:52:30 +0000379 *$1 += *$2;
380 delete $2;
Reid Spencer16244f42006-12-01 21:10:07 +0000381 $$ = $1;
382 }
Reid Spencere7c3c602006-11-30 06:36:44 +0000383 | /*empty*/ { $$ = new std::string(""); } ;
384
385// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
386// a comma before it.
387OptAlign
388 : /*empty*/ { $$ = new std::string(); }
Reid Spencerf2d55322006-12-01 21:52:30 +0000389 | ALIGN EUINT64VAL { *$1 += " " + *$2; delete $2; $$ = $1; };
Reid Spencerf0cf1322006-12-07 04:23:03 +0000390
Reid Spencere7c3c602006-11-30 06:36:44 +0000391OptCAlign
392 : /*empty*/ { $$ = new std::string(); }
393 | ',' ALIGN EUINT64VAL {
394 $2->insert(0, ", ");
Reid Spencerf2d55322006-12-01 21:52:30 +0000395 *$2 += " " + *$3;
396 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000397 $$ = $2;
398 };
399
400SectionString
401 : SECTION STRINGCONSTANT {
402 *$1 += " " + *$2;
403 delete $2;
404 $$ = $1;
405 };
406
407OptSection : /*empty*/ { $$ = new std::string(); }
408 | SectionString;
409
410GlobalVarAttributes
411 : /* empty */ { $$ = new std::string(); }
412 | ',' GlobalVarAttribute GlobalVarAttributes {
413 $2->insert(0, ", ");
414 if (!$3->empty())
415 *$2 += " " + *$3;
416 delete $3;
417 $$ = $2;
418 };
419
420GlobalVarAttribute
421 : SectionString
422 | ALIGN EUINT64VAL {
Reid Spencerf2d55322006-12-01 21:52:30 +0000423 *$1 += " " + *$2;
424 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000425 $$ = $1;
426 };
427
428//===----------------------------------------------------------------------===//
429// Types includes all predefined types... except void, because it can only be
430// used in specific contexts (function returning void for example). To have
431// access to it, a user must explicitly use TypesV.
432//
433
434// TypesV includes all of 'Types', but it also includes the void type.
435TypesV : Types | VOID ;
436UpRTypesV : UpRTypes | VOID ;
437Types : UpRTypes ;
438
439// Derived types are added later...
440//
441PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
Reid Spencere77e35e2006-12-01 20:26:20 +0000442PrimType : LONG | ULONG | FLOAT | DOUBLE | LABEL;
Reid Spencera50d5962006-12-02 04:11:07 +0000443UpRTypes
444 : OPAQUE {
445 $$.newTy = $1;
446 $$.oldTy = OpaqueTy;
447 }
448 | SymbolicValueRef {
449 $$.newTy = $1;
450 $$.oldTy = UnresolvedTy;
451 }
Reid Spencer78720742006-12-02 20:21:22 +0000452 | PrimType {
453 $$ = $1;
454 }
455 | '\\' EUINT64VAL { // Type UpReference
Reid Spencerf2d55322006-12-01 21:52:30 +0000456 $2->insert(0, "\\");
457 $$.newTy = $2;
Reid Spencera50d5962006-12-02 04:11:07 +0000458 $$.oldTy = NumericTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000459 }
460 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Reid Spencere77e35e2006-12-01 20:26:20 +0000461 *$1.newTy += "( " + *$3 + " )";
Reid Spencere7c3c602006-11-30 06:36:44 +0000462 delete $3;
Reid Spencere77e35e2006-12-01 20:26:20 +0000463 $$.newTy = $1.newTy;
464 $$.oldTy = FunctionTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000465 }
466 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Reid Spencerf2d55322006-12-01 21:52:30 +0000467 $2->insert(0,"[ ");
468 *$2 += " x " + *$4.newTy + " ]";
Reid Spencere77e35e2006-12-01 20:26:20 +0000469 delete $4.newTy;
Reid Spencerf2d55322006-12-01 21:52:30 +0000470 $$.newTy = $2;
Reid Spencere77e35e2006-12-01 20:26:20 +0000471 $$.oldTy = ArrayTy;
Reid Spencer78720742006-12-02 20:21:22 +0000472 $$.elemTy = $4.oldTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000473 }
474 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
Reid Spencerf2d55322006-12-01 21:52:30 +0000475 $2->insert(0,"< ");
476 *$2 += " x " + *$4.newTy + " >";
Reid Spencere77e35e2006-12-01 20:26:20 +0000477 delete $4.newTy;
Reid Spencerf2d55322006-12-01 21:52:30 +0000478 $$.newTy = $2;
Reid Spencere77e35e2006-12-01 20:26:20 +0000479 $$.oldTy = PackedTy;
Reid Spencer78720742006-12-02 20:21:22 +0000480 $$.elemTy = $4.oldTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000481 }
482 | '{' TypeListI '}' { // Structure type?
483 $2->insert(0, "{ ");
484 *$2 += " }";
Reid Spencere77e35e2006-12-01 20:26:20 +0000485 $$.newTy = $2;
486 $$.oldTy = StructTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000487 }
488 | '{' '}' { // Empty structure type?
Reid Spencer0b7e5072006-12-01 22:42:01 +0000489 $$.newTy = new std::string("{}");
Reid Spencere77e35e2006-12-01 20:26:20 +0000490 $$.oldTy = StructTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000491 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +0000492 | '<' '{' TypeListI '}' '>' { // Packed Structure type?
493 $3->insert(0, "<{ ");
494 *$3 += " }>";
495 $$.newTy = $3;
496 $$.oldTy = StructTy;
497 }
498 | '<' '{' '}' '>' { // Empty packed structure type?
499 $$.newTy = new std::string("<{}>");
500 $$.oldTy = StructTy;
501 }
Reid Spencere7c3c602006-11-30 06:36:44 +0000502 | UpRTypes '*' { // Pointer type?
Reid Spencere77e35e2006-12-01 20:26:20 +0000503 *$1.newTy += '*';
Reid Spencer78720742006-12-02 20:21:22 +0000504 $$.elemTy = $1.oldTy;
Reid Spencere77e35e2006-12-01 20:26:20 +0000505 $1.oldTy = PointerTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000506 $$ = $1;
507 };
508
509// TypeList - Used for struct declarations and as a basis for function type
510// declaration type lists
511//
Reid Spencere77e35e2006-12-01 20:26:20 +0000512TypeListI
513 : UpRTypes {
514 $$ = $1.newTy;
515 }
516 | TypeListI ',' UpRTypes {
517 *$1 += ", " + *$3.newTy;
518 delete $3.newTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000519 $$ = $1;
520 };
521
522// ArgTypeList - List of types for a function type declaration...
Reid Spencere77e35e2006-12-01 20:26:20 +0000523ArgTypeListI
524 : TypeListI
Reid Spencere7c3c602006-11-30 06:36:44 +0000525 | TypeListI ',' DOTDOTDOT {
526 *$1 += ", ...";
527 delete $3;
528 $$ = $1;
529 }
530 | DOTDOTDOT {
531 $$ = $1;
532 }
533 | /*empty*/ {
534 $$ = new std::string();
535 };
536
537// ConstVal - The various declarations that go into the constant pool. This
538// production is used ONLY to represent constants that show up AFTER a 'const',
539// 'constant' or 'global' token at global scope. Constants that can be inlined
540// into other expressions (such as integers and constexprs) are handled by the
541// ResolvedVal, ValueRef and ConstValueRef productions.
542//
543ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencere77e35e2006-12-01 20:26:20 +0000544 $$.type = $1;
545 $$.cnst = new std::string(*$1.newTy);
546 *$$.cnst += " [ " + *$3 + " ]";
Reid Spencere7c3c602006-11-30 06:36:44 +0000547 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000548 }
549 | Types '[' ']' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000550 $$.type = $1;
551 $$.cnst = new std::string(*$1.newTy);
552 *$$.cnst += "[ ]";
Reid Spencere7c3c602006-11-30 06:36:44 +0000553 }
554 | Types 'c' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +0000555 $$.type = $1;
556 $$.cnst = new std::string(*$1.newTy);
557 *$$.cnst += " c" + *$3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000558 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000559 }
560 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencere77e35e2006-12-01 20:26:20 +0000561 $$.type = $1;
562 $$.cnst = new std::string(*$1.newTy);
563 *$$.cnst += " < " + *$3 + " >";
Reid Spencere7c3c602006-11-30 06:36:44 +0000564 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000565 }
566 | Types '{' ConstVector '}' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000567 $$.type = $1;
568 $$.cnst = new std::string(*$1.newTy);
569 *$$.cnst += " { " + *$3 + " }";
Reid Spencere7c3c602006-11-30 06:36:44 +0000570 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000571 }
572 | Types '{' '}' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000573 $$.type = $1;
574 $$.cnst = new std::string(*$1.newTy);
Reid Spencer0b7e5072006-12-01 22:42:01 +0000575 *$$.cnst += " {}";
Reid Spencere7c3c602006-11-30 06:36:44 +0000576 }
577 | Types NULL_TOK {
Reid Spencere77e35e2006-12-01 20:26:20 +0000578 $$.type = $1;
579 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000580 *$$.cnst += " " + *$2;
581 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000582 }
583 | Types UNDEF {
Reid Spencere77e35e2006-12-01 20:26:20 +0000584 $$.type = $1;
585 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000586 *$$.cnst += " " + *$2;
587 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000588 }
589 | Types SymbolicValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000590 $$.type = $1;
591 $$.cnst = new std::string(*$1.newTy);
592 *$$.cnst += " " + *$2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000593 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000594 }
595 | Types ConstExpr {
Reid Spencere77e35e2006-12-01 20:26:20 +0000596 $$.type = $1;
597 $$.cnst = new std::string(*$1.newTy);
598 *$$.cnst += " " + *$2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000599 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000600 }
601 | Types ZEROINITIALIZER {
Reid Spencere77e35e2006-12-01 20:26:20 +0000602 $$.type = $1;
603 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000604 *$$.cnst += " " + *$2;
605 delete $2;
Reid Spencere77e35e2006-12-01 20:26:20 +0000606 }
607 | SIntType EInt64Val { // integral constants
608 $$.type = $1;
609 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000610 *$$.cnst += " " + *$2;
611 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000612 }
613 | UIntType EUINT64VAL { // integral constants
Reid Spencere77e35e2006-12-01 20:26:20 +0000614 $$.type = $1;
615 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000616 *$$.cnst += " " + *$2;
617 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000618 }
619 | BOOL TRUETOK { // Boolean constants
Reid Spencere77e35e2006-12-01 20:26:20 +0000620 $$.type = $1;
621 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000622 *$$.cnst += " " + *$2;
623 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000624 }
625 | BOOL FALSETOK { // Boolean constants
Reid Spencere77e35e2006-12-01 20:26:20 +0000626 $$.type = $1;
627 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000628 *$$.cnst += " " + *$2;
629 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000630 }
631 | FPType FPVAL { // Float & Double constants
Reid Spencere77e35e2006-12-01 20:26:20 +0000632 $$.type = $1;
633 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000634 *$$.cnst += " " + *$2;
635 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000636 };
637
638
Reid Spencerfcb5df82006-12-01 22:34:43 +0000639ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencer280d8012006-12-01 23:40:53 +0000640 std::string source = *$3.cnst;
Reid Spencera50d5962006-12-02 04:11:07 +0000641 TypeInfo DstTy = $5;
642 ResolveType(DstTy);
Reid Spencer280d8012006-12-01 23:40:53 +0000643 if (*$1 == "cast") {
Reid Spencera50d5962006-12-02 04:11:07 +0000644 // Call getCastUpgrade to upgrade the old cast
645 $$ = new std::string(getCastUpgrade(source, $3.type, $5, true));
646 } else {
647 // Nothing to upgrade, just create the cast constant expr
648 $$ = new std::string(*$1);
649 *$$ += "( " + source + " to " + *$5.newTy + ")";
Reid Spencer280d8012006-12-01 23:40:53 +0000650 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000651 delete $1; $3.destroy(); delete $4; $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000652 }
653 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencerf8483652006-12-02 15:16:01 +0000654 *$1 += "(" + *$3.cnst;
655 for (unsigned i = 0; i < $4->size(); ++i) {
656 ValueInfo& VI = (*$4)[i];
657 *$1 += ", " + *VI.val;
658 VI.destroy();
659 }
660 *$1 += ")";
Reid Spencere77e35e2006-12-01 20:26:20 +0000661 $$ = $1;
662 $3.destroy();
663 delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +0000664 }
665 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000666 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
667 $3.destroy(); $5.destroy(); $7.destroy();
668 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000669 }
670 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer78720742006-12-02 20:21:22 +0000671 const char* op = getDivRemOpcode(*$1, $3.type);
672 $$ = new std::string(op);
673 *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
674 delete $1; $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000675 }
676 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000677 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
678 $3.destroy(); $5.destroy();
679 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000680 }
681 | SetCondOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer229e9362006-12-02 22:14:11 +0000682 *$1 = getCompareOp(*$1, $3.type);
Reid Spencere77e35e2006-12-01 20:26:20 +0000683 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
684 $3.destroy(); $5.destroy();
685 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000686 }
Reid Spencer57f28f92006-12-03 07:10:26 +0000687 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
688 *$1 += "(" + *$2 + "," + *$4.cnst + "," + *$6.cnst + ")";
689 delete $2; $4.destroy(); $6.destroy();
690 $$ = $1;
691 }
692 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
Reid Spencer229e9362006-12-02 22:14:11 +0000693 *$1 += "(" + *$2 + "," + *$4.cnst + "," + *$6.cnst + ")";
694 delete $2; $4.destroy(); $6.destroy();
695 $$ = $1;
696 }
Reid Spencere7c3c602006-11-30 06:36:44 +0000697 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Reid Spencerf7bde222006-12-01 22:26:37 +0000698 const char* shiftop = $1->c_str();
699 if (*$1 == "shr")
700 shiftop = ($3.type.isUnsigned()) ? "lshr" : "ashr";
701 $$ = new std::string(shiftop);
702 *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
703 delete $1; $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000704 }
705 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000706 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
707 $3.destroy(); $5.destroy();
708 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000709 }
710 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000711 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
712 $3.destroy(); $5.destroy(); $7.destroy();
713 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000714 }
715 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000716 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
717 $3.destroy(); $5.destroy(); $7.destroy();
718 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000719 };
720
721
722// ConstVector - A list of comma separated constants.
Reid Spencere77e35e2006-12-01 20:26:20 +0000723
724ConstVector
725 : ConstVector ',' ConstVal {
726 *$1 += ", " + *$3.cnst;
727 $3.destroy();
728 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000729 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000730 | ConstVal { $$ = new std::string(*$1.cnst); $1.destroy(); }
731 ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000732
733
734// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Reid Spencere77e35e2006-12-01 20:26:20 +0000735GlobalType : GLOBAL | CONSTANT ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000736
737
738//===----------------------------------------------------------------------===//
739// Rules to match Modules
740//===----------------------------------------------------------------------===//
741
742// Module rule: Capture the result of parsing the whole file into a result
743// variable...
744//
745Module : DefinitionList {
746};
747
748// DefinitionList - Top level definitions
749//
750DefinitionList : DefinitionList Function {
751 $$ = 0;
752 }
753 | DefinitionList FunctionProto {
Reid Spencer6fd36ab2006-12-29 20:35:03 +0000754 *O << *$2 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +0000755 delete $2;
756 $$ = 0;
757 }
758 | DefinitionList MODULE ASM_TOK AsmBlock {
Reid Spencer6fd36ab2006-12-29 20:35:03 +0000759 *O << "module asm " << ' ' << *$4 << '\n';
Reid Spencerd154b572006-12-01 20:36:40 +0000760 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000761 }
762 | DefinitionList IMPLEMENTATION {
763 *O << "implementation\n";
Reid Spencerd154b572006-12-01 20:36:40 +0000764 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000765 }
Reid Spencera50d5962006-12-02 04:11:07 +0000766 | ConstPool { $$ = 0; }
Reid Spencere7c3c602006-11-30 06:36:44 +0000767
Reid Spencer78720742006-12-02 20:21:22 +0000768External : EXTERNAL | UNINITIALIZED { $$ = $1; *$$ = "external"; }
769
Reid Spencere7c3c602006-11-30 06:36:44 +0000770// ConstPool - Constants with optional names assigned to them.
771ConstPool : ConstPool OptAssign TYPE TypesV {
Reid Spencera50d5962006-12-02 04:11:07 +0000772 EnumeratedTypes.push_back($4);
773 if (!$2->empty()) {
774 NamedTypes[*$2].newTy = new std::string(*$4.newTy);
775 NamedTypes[*$2].oldTy = $4.oldTy;
Reid Spencer78720742006-12-02 20:21:22 +0000776 NamedTypes[*$2].elemTy = $4.elemTy;
Reid Spencera50d5962006-12-02 04:11:07 +0000777 *O << *$2 << " = ";
778 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +0000779 *O << "type " << *$4.newTy << '\n';
Reid Spencera50d5962006-12-02 04:11:07 +0000780 delete $2; delete $3; $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000781 $$ = 0;
782 }
783 | ConstPool FunctionProto { // Function prototypes can be in const pool
Reid Spencer6fd36ab2006-12-29 20:35:03 +0000784 *O << *$2 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +0000785 delete $2;
786 $$ = 0;
787 }
788 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
Reid Spencer6fd36ab2006-12-29 20:35:03 +0000789 *O << *$2 << ' ' << *$3 << ' ' << *$4 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +0000790 delete $2; delete $3; delete $4;
791 $$ = 0;
792 }
793 | ConstPool OptAssign OptLinkage GlobalType ConstVal GlobalVarAttributes {
Reid Spencerf12ee422006-12-05 19:21:25 +0000794 if (!$2->empty()) {
Reid Spencera50d5962006-12-02 04:11:07 +0000795 *O << *$2 << " = ";
Reid Spencerf12ee422006-12-05 19:21:25 +0000796 Globals[*$2] = $5.type.clone();
797 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +0000798 *O << *$3 << ' ' << *$4 << ' ' << *$5.cnst << ' ' << *$6 << '\n';
Reid Spencere77e35e2006-12-01 20:26:20 +0000799 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000800 $$ = 0;
801 }
Reid Spencer78720742006-12-02 20:21:22 +0000802 | ConstPool OptAssign External GlobalType Types GlobalVarAttributes {
Reid Spencerf12ee422006-12-05 19:21:25 +0000803 if (!$2->empty()) {
Reid Spencera50d5962006-12-02 04:11:07 +0000804 *O << *$2 << " = ";
Reid Spencerf12ee422006-12-05 19:21:25 +0000805 Globals[*$2] = $5.clone();
806 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +0000807 *O << *$3 << ' ' << *$4 << ' ' << *$5.newTy << ' ' << *$6 << '\n';
Reid Spencere77e35e2006-12-01 20:26:20 +0000808 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000809 $$ = 0;
810 }
811 | ConstPool OptAssign DLLIMPORT GlobalType Types GlobalVarAttributes {
Reid Spencerf12ee422006-12-05 19:21:25 +0000812 if (!$2->empty()) {
Reid Spencera50d5962006-12-02 04:11:07 +0000813 *O << *$2 << " = ";
Reid Spencerf12ee422006-12-05 19:21:25 +0000814 Globals[*$2] = $5.clone();
815 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +0000816 *O << *$3 << ' ' << *$4 << ' ' << *$5.newTy << ' ' << *$6 << '\n';
Reid Spencere77e35e2006-12-01 20:26:20 +0000817 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000818 $$ = 0;
819 }
820 | ConstPool OptAssign EXTERN_WEAK GlobalType Types GlobalVarAttributes {
Reid Spencerf12ee422006-12-05 19:21:25 +0000821 if (!$2->empty()) {
Reid Spencera50d5962006-12-02 04:11:07 +0000822 *O << *$2 << " = ";
Reid Spencerf12ee422006-12-05 19:21:25 +0000823 Globals[*$2] = $5.clone();
824 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +0000825 *O << *$3 << ' ' << *$4 << ' ' << *$5.newTy << ' ' << *$6 << '\n';
Reid Spencere77e35e2006-12-01 20:26:20 +0000826 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000827 $$ = 0;
828 }
829 | ConstPool TARGET TargetDefinition {
Reid Spencer6fd36ab2006-12-29 20:35:03 +0000830 *O << *$2 << ' ' << *$3 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +0000831 delete $2; delete $3;
832 $$ = 0;
833 }
834 | ConstPool DEPLIBS '=' LibrariesDefinition {
Reid Spencer6fd36ab2006-12-29 20:35:03 +0000835 *O << *$2 << " = " << *$4 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +0000836 delete $2; delete $4;
837 $$ = 0;
838 }
839 | /* empty: end of list */ {
840 $$ = 0;
841 };
842
843
844AsmBlock : STRINGCONSTANT ;
845
846BigOrLittle : BIG | LITTLE
847
848TargetDefinition
849 : ENDIAN '=' BigOrLittle {
Reid Spencere77e35e2006-12-01 20:26:20 +0000850 *$1 += " = " + *$3;
851 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000852 $$ = $1;
853 }
854 | POINTERSIZE '=' EUINT64VAL {
Reid Spencerf2d55322006-12-01 21:52:30 +0000855 *$1 += " = " + *$3;
856 if (*$3 == "64")
Reid Spencere77e35e2006-12-01 20:26:20 +0000857 SizeOfPointer = 64;
Reid Spencerf2d55322006-12-01 21:52:30 +0000858 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000859 $$ = $1;
860 }
861 | TRIPLE '=' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +0000862 *$1 += " = " + *$3;
863 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000864 $$ = $1;
865 }
866 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +0000867 *$1 += " = " + *$3;
868 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000869 $$ = $1;
870 };
871
872LibrariesDefinition
873 : '[' LibList ']' {
874 $2->insert(0, "[ ");
875 *$2 += " ]";
876 $$ = $2;
877 };
878
879LibList
880 : LibList ',' STRINGCONSTANT {
881 *$1 += ", " + *$3;
882 delete $3;
883 $$ = $1;
884 }
885 | STRINGCONSTANT
886 | /* empty: end of list */ {
887 $$ = new std::string();
888 };
889
890//===----------------------------------------------------------------------===//
891// Rules to match Function Headers
892//===----------------------------------------------------------------------===//
893
894Name : VAR_ID | STRINGCONSTANT;
895OptName : Name | /*empty*/ { $$ = new std::string(); };
896
897ArgVal : Types OptName {
Reid Spencere77e35e2006-12-01 20:26:20 +0000898 $$ = $1.newTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000899 if (!$2->empty())
900 *$$ += " " + *$2;
Reid Spencere77e35e2006-12-01 20:26:20 +0000901 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000902};
903
904ArgListH : ArgListH ',' ArgVal {
905 *$1 += ", " + *$3;
Reid Spencere77e35e2006-12-01 20:26:20 +0000906 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000907 }
908 | ArgVal {
909 $$ = $1;
910 };
911
912ArgList : ArgListH {
913 $$ = $1;
914 }
915 | ArgListH ',' DOTDOTDOT {
916 *$1 += ", ...";
917 $$ = $1;
Reid Spencere77e35e2006-12-01 20:26:20 +0000918 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000919 }
920 | DOTDOTDOT {
921 $$ = $1;
922 }
Reid Spencerd154b572006-12-01 20:36:40 +0000923 | /* empty */ { $$ = new std::string(); };
Reid Spencere7c3c602006-11-30 06:36:44 +0000924
925FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')'
926 OptSection OptAlign {
927 if (!$1->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000928 *$1 += " ";
Reid Spencere7c3c602006-11-30 06:36:44 +0000929 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000930 *$1 += *$2.newTy + " " + *$3 + "(" + *$5 + ")";
Reid Spencere7c3c602006-11-30 06:36:44 +0000931 if (!$7->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000932 *$1 += " " + *$7;
Reid Spencere7c3c602006-11-30 06:36:44 +0000933 }
934 if (!$8->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000935 *$1 += " " + *$8;
Reid Spencere7c3c602006-11-30 06:36:44 +0000936 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000937 $2.destroy();
938 delete $3;
939 delete $5;
940 delete $7;
941 delete $8;
942 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000943 };
944
Reid Spencer78720742006-12-02 20:21:22 +0000945BEGIN : BEGINTOK { $$ = new std::string("{"); delete $1; }
946 | '{' { $$ = new std::string ("{"); }
Reid Spencere7c3c602006-11-30 06:36:44 +0000947
Reid Spencer6fd36ab2006-12-29 20:35:03 +0000948FunctionHeader
949 : OptLinkage FunctionHeaderH BEGIN {
950 *O << "define ";
951 if (!$1->empty()) {
952 *O << *$1 << ' ';
953 }
954 *O << *$2 << ' ' << *$3 << '\n';
955 delete $1; delete $2; delete $3;
956 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000957 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +0000958 | DEFINE OptLinkage FunctionHeaderH BEGIN {
959 *O << *$1 << ' ';
960 if (!$2->empty()) {
961 *O << *$2 << ' ';
962 }
963 *O << *$3 << ' ' << *$4 << '\n';
964 delete $1; delete $2; delete $3; delete $4;
965 $$ = 0;
966 }
967 ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000968
Reid Spencer78720742006-12-02 20:21:22 +0000969END : ENDTOK { $$ = new std::string("}"); delete $1; }
Reid Spencere7c3c602006-11-30 06:36:44 +0000970 | '}' { $$ = new std::string("}"); };
971
972Function : FunctionHeader BasicBlockList END {
973 if ($2)
974 *O << *$2;
Reid Spencer6fd36ab2006-12-29 20:35:03 +0000975 *O << '\n' << *$3 << '\n';
Reid Spencere77e35e2006-12-01 20:26:20 +0000976 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000977};
978
Reid Spencere77e35e2006-12-01 20:26:20 +0000979FnDeclareLinkage
980 : /*default*/ { $$ = new std::string(); }
Reid Spencere7c3c602006-11-30 06:36:44 +0000981 | DLLIMPORT
982 | EXTERN_WEAK
983 ;
984
985FunctionProto
986 : DECLARE FnDeclareLinkage FunctionHeaderH {
Reid Spencere77e35e2006-12-01 20:26:20 +0000987 if (!$2->empty())
988 *$1 += " " + *$2;
989 *$1 += " " + *$3;
990 delete $2;
991 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000992 $$ = $1;
993 };
994
995//===----------------------------------------------------------------------===//
996// Rules to match Basic Blocks
997//===----------------------------------------------------------------------===//
998
Reid Spencerd154b572006-12-01 20:36:40 +0000999OptSideEffect : /* empty */ { $$ = new std::string(); }
1000 | SIDEEFFECT;
Reid Spencere7c3c602006-11-30 06:36:44 +00001001
Reid Spencere77e35e2006-12-01 20:26:20 +00001002ConstValueRef
Reid Spencerf2d55322006-12-01 21:52:30 +00001003 : ESINT64VAL | EUINT64VAL | FPVAL | TRUETOK | FALSETOK | NULL_TOK | UNDEF
1004 | ZEROINITIALIZER
Reid Spencere7c3c602006-11-30 06:36:44 +00001005 | '<' ConstVector '>' {
1006 $2->insert(0, "<");
1007 *$2 += ">";
1008 $$ = $2;
1009 }
1010 | ConstExpr
1011 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
1012 if (!$2->empty()) {
1013 *$1 += " " + *$2;
1014 }
Reid Spencere77e35e2006-12-01 20:26:20 +00001015 *$1 += " " + *$3 + ", " + *$5;
1016 delete $2; delete $3; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +00001017 $$ = $1;
1018 };
1019
Reid Spencerf2d55322006-12-01 21:52:30 +00001020SymbolicValueRef : IntVal | Name ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001021
1022// ValueRef - A reference to a definition... either constant or symbolic
Reid Spencerf459d392006-12-02 16:19:52 +00001023ValueRef
1024 : SymbolicValueRef {
1025 $$.val = $1;
1026 $$.constant = false;
1027 $$.type.newTy = 0;
1028 $$.type.oldTy = UnresolvedTy;
1029 }
1030 | ConstValueRef {
1031 $$.val = $1;
1032 $$.constant = true;
1033 $$.type.newTy = 0;
1034 $$.type.oldTy = UnresolvedTy;
1035 }
1036 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001037
1038// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1039// type immediately preceeds the value reference, and allows complex constant
1040// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
1041ResolvedVal : Types ValueRef {
Reid Spencerf459d392006-12-02 16:19:52 +00001042 $$ = $2;
Reid Spencere77e35e2006-12-01 20:26:20 +00001043 $$.type = $1;
Reid Spencerf459d392006-12-02 16:19:52 +00001044 $$.val->insert(0, *$1.newTy + " ");
Reid Spencere7c3c602006-11-30 06:36:44 +00001045 };
1046
1047BasicBlockList : BasicBlockList BasicBlock {
Reid Spencerf2d55322006-12-01 21:52:30 +00001048 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001049 }
1050 | BasicBlock { // Do not allow functions with 0 basic blocks
Reid Spencerf2d55322006-12-01 21:52:30 +00001051 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001052 };
1053
1054
1055// Basic blocks are terminated by branching instructions:
1056// br, br/cc, switch, ret
1057//
Reid Spencer16244f42006-12-01 21:10:07 +00001058BasicBlock : InstructionList BBTerminatorInst {
Reid Spencerf2d55322006-12-01 21:52:30 +00001059 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001060 };
1061
1062InstructionList : InstructionList Inst {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001063 *O << " " << *$2 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001064 delete $2;
1065 $$ = 0;
1066 }
1067 | /* empty */ {
1068 $$ = 0;
1069 }
1070 | LABELSTR {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001071 *O << *$1 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001072 delete $1;
1073 $$ = 0;
1074 };
1075
Reid Spencer78720742006-12-02 20:21:22 +00001076Unwind : UNWIND | EXCEPT { $$ = $1; *$$ = "unwind"; }
1077
Reid Spencere7c3c602006-11-30 06:36:44 +00001078BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001079 *O << " " << *$1 << ' ' << *$2.val << '\n';
Reid Spencere77e35e2006-12-01 20:26:20 +00001080 delete $1; $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001081 $$ = 0;
1082 }
1083 | RET VOID { // Return with no result...
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001084 *O << " " << *$1 << ' ' << *$2.newTy << '\n';
Reid Spencere77e35e2006-12-01 20:26:20 +00001085 delete $1; $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001086 $$ = 0;
1087 }
1088 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001089 *O << " " << *$1 << ' ' << *$2.newTy << ' ' << *$3.val << '\n';
Reid Spencerf459d392006-12-02 16:19:52 +00001090 delete $1; $2.destroy(); $3.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001091 $$ = 0;
1092 } // Conditional Branch...
1093 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001094 *O << " " << *$1 << ' ' << *$2.newTy << ' ' << *$3.val << ", "
1095 << *$5.newTy << ' ' << *$6.val << ", " << *$8.newTy << ' '
1096 << *$9.val << '\n';
Reid Spencerf459d392006-12-02 16:19:52 +00001097 delete $1; $2.destroy(); $3.destroy(); $5.destroy(); $6.destroy();
1098 $8.destroy(); $9.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001099 $$ = 0;
1100 }
1101 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001102 *O << " " << *$1 << ' ' << *$2.newTy << ' ' << *$3.val << ", "
1103 << *$5.newTy << ' ' << *$6.val << " [" << *$8 << " ]\n";
Reid Spencerf459d392006-12-02 16:19:52 +00001104 delete $1; $2.destroy(); $3.destroy(); $5.destroy(); $6.destroy();
1105 delete $8;
Reid Spencere7c3c602006-11-30 06:36:44 +00001106 $$ = 0;
1107 }
1108 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001109 *O << " " << *$1 << ' ' << *$2.newTy << ' ' << *$3.val << ", "
1110 << *$5.newTy << ' ' << *$6.val << "[]\n";
Reid Spencerf459d392006-12-02 16:19:52 +00001111 delete $1; $2.destroy(); $3.destroy(); $5.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001112 $$ = 0;
1113 }
Reid Spencer16244f42006-12-01 21:10:07 +00001114 | OptAssign INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
Reid Spencer78720742006-12-02 20:21:22 +00001115 TO LABEL ValueRef Unwind LABEL ValueRef {
Reid Spencer16244f42006-12-01 21:10:07 +00001116 *O << " ";
1117 if (!$1->empty())
Reid Spencera50d5962006-12-02 04:11:07 +00001118 *O << *$1 << " = ";
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001119 *O << *$2 << ' ' << *$3 << ' ' << *$4.newTy << ' ' << *$5.val << " (";
Reid Spencerf8483652006-12-02 15:16:01 +00001120 for (unsigned i = 0; i < $7->size(); ++i) {
1121 ValueInfo& VI = (*$7)[i];
1122 *O << *VI.val;
1123 if (i+1 < $7->size())
1124 *O << ", ";
1125 VI.destroy();
1126 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001127 *O << ") " << *$9 << ' ' << *$10.newTy << ' ' << *$11.val << ' '
1128 << *$12 << ' ' << *$13.newTy << ' ' << *$14.val << '\n';
Reid Spencerf459d392006-12-02 16:19:52 +00001129 delete $1; delete $2; delete $3; $4.destroy(); $5.destroy(); delete $7;
1130 delete $9; $10.destroy(); $11.destroy(); delete $12; $13.destroy();
1131 $14.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001132 $$ = 0;
1133 }
Reid Spencer78720742006-12-02 20:21:22 +00001134 | Unwind {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001135 *O << " " << *$1 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001136 delete $1;
1137 $$ = 0;
1138 }
1139 | UNREACHABLE {
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001140 *O << " " << *$1 << '\n';
Reid Spencere7c3c602006-11-30 06:36:44 +00001141 delete $1;
1142 $$ = 0;
1143 };
1144
1145JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
Reid Spencerf459d392006-12-02 16:19:52 +00001146 *$1 += " " + *$2.newTy + " " + *$3 + ", " + *$5.newTy + " " + *$6.val;
1147 $2.destroy(); delete $3; $5.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001148 $$ = $1;
1149 }
1150 | IntType ConstValueRef ',' LABEL ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +00001151 $2->insert(0, *$1.newTy + " " );
Reid Spencerf459d392006-12-02 16:19:52 +00001152 *$2 += ", " + *$4.newTy + " " + *$5.val;
1153 $1.destroy(); $4.destroy(); $5.destroy();
Reid Spencere77e35e2006-12-01 20:26:20 +00001154 $$ = $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001155 };
1156
1157Inst
1158 : OptAssign InstVal {
Reid Spencera50d5962006-12-02 04:11:07 +00001159 if (!$1->empty())
1160 *$1 += " = ";
Reid Spencere7c3c602006-11-30 06:36:44 +00001161 *$1 += *$2;
1162 delete $2;
1163 $$ = $1;
1164 };
1165
1166PHIList
1167 : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencerf459d392006-12-02 16:19:52 +00001168 $3.val->insert(0, *$1.newTy + "[");
1169 *$3.val += "," + *$5.val + "]";
1170 $1.destroy(); $5.destroy();
1171 $$ = new std::string(*$3.val);
1172 $3.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001173 }
1174 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
Reid Spencerf459d392006-12-02 16:19:52 +00001175 *$1 += ", [" + *$4.val + "," + *$6.val + "]";
1176 $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001177 $$ = $1;
1178 };
1179
1180
1181ValueRefList
Reid Spencerf8483652006-12-02 15:16:01 +00001182 : ResolvedVal {
1183 $$ = new ValueList();
1184 $$->push_back($1);
1185 }
Reid Spencere7c3c602006-11-30 06:36:44 +00001186 | ValueRefList ',' ResolvedVal {
Reid Spencerf8483652006-12-02 15:16:01 +00001187 $1->push_back($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00001188 $$ = $1;
1189 };
1190
1191// ValueRefListE - Just like ValueRefList, except that it may also be empty!
1192ValueRefListE
Reid Spencerf8483652006-12-02 15:16:01 +00001193 : ValueRefList { $$ = $1; }
1194 | /*empty*/ { $$ = new ValueList(); }
Reid Spencere7c3c602006-11-30 06:36:44 +00001195 ;
1196
1197OptTailCall
1198 : TAIL CALL {
1199 *$1 += " " + *$2;
1200 delete $2;
1201 $$ = $1;
1202 }
1203 | CALL
1204 ;
1205
1206InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencer78720742006-12-02 20:21:22 +00001207 const char* op = getDivRemOpcode(*$1, $2);
1208 $$ = new std::string(op);
1209 *$$ += " " + *$2.newTy + " " + *$3.val + ", " + *$5.val;
1210 delete $1; $2.destroy(); $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001211 }
1212 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencerf459d392006-12-02 16:19:52 +00001213 *$1 += " " + *$2.newTy + " " + *$3.val + ", " + *$5.val;
1214 $2.destroy(); $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001215 $$ = $1;
1216 }
1217 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencer229e9362006-12-02 22:14:11 +00001218 *$1 = getCompareOp(*$1, $2);
Reid Spencerf459d392006-12-02 16:19:52 +00001219 *$1 += " " + *$2.newTy + " " + *$3.val + ", " + *$5.val;
1220 $2.destroy(); $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001221 $$ = $1;
1222 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001223 | ICMP IPredicates Types ValueRef ',' ValueRef {
1224 *$1 += " " + *$2 + " " + *$3.newTy + " " + *$4.val + "," + *$6.val;
Reid Spencer57f28f92006-12-03 07:10:26 +00001225 delete $2; $4.destroy(); $6.destroy();
1226 $$ = $1;
1227 }
Reid Spencer6fd36ab2006-12-29 20:35:03 +00001228 | FCMP FPredicates Types ValueRef ',' ValueRef {
1229 *$1 += " " + *$2 + " " + *$3.newTy + " " + *$4.val + "," + *$6.val;
Reid Spencer229e9362006-12-02 22:14:11 +00001230 delete $2; $4.destroy(); $6.destroy();
1231 $$ = $1;
1232 }
Reid Spencere7c3c602006-11-30 06:36:44 +00001233 | NOT ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001234 *$1 += " " + *$2.val;
1235 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001236 $$ = $1;
1237 }
1238 | ShiftOps ResolvedVal ',' ResolvedVal {
Reid Spencerf7bde222006-12-01 22:26:37 +00001239 const char* shiftop = $1->c_str();
1240 if (*$1 == "shr")
1241 shiftop = ($2.type.isUnsigned()) ? "lshr" : "ashr";
1242 $$ = new std::string(shiftop);
1243 *$$ += " " + *$2.val + ", " + *$4.val;
1244 delete $1; $2.destroy(); $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001245 }
Reid Spencerfcb5df82006-12-01 22:34:43 +00001246 | CastOps ResolvedVal TO Types {
Reid Spencer280d8012006-12-01 23:40:53 +00001247 std::string source = *$2.val;
Reid Spencera50d5962006-12-02 04:11:07 +00001248 TypeInfo SrcTy = $2.type;
1249 TypeInfo DstTy = $4;
1250 ResolveType(DstTy);
1251 $$ = new std::string();
Reid Spencer280d8012006-12-01 23:40:53 +00001252 if (*$1 == "cast") {
Reid Spencera50d5962006-12-02 04:11:07 +00001253 *$$ += getCastUpgrade(source, SrcTy, DstTy, false);
1254 } else {
1255 *$$ += *$1 + " " + source + " to " + *DstTy.newTy;
Reid Spencer280d8012006-12-01 23:40:53 +00001256 }
Reid Spencere77e35e2006-12-01 20:26:20 +00001257 delete $1; $2.destroy();
1258 delete $3; $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001259 }
1260 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001261 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1262 $2.destroy(); $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001263 $$ = $1;
1264 }
1265 | VAARG ResolvedVal ',' Types {
Reid Spencere77e35e2006-12-01 20:26:20 +00001266 *$1 += " " + *$2.val + ", " + *$4.newTy;
1267 $2.destroy(); $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001268 $$ = $1;
1269 }
1270 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001271 *$1 += " " + *$2.val + ", " + *$4.val;
1272 $2.destroy(); $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001273 $$ = $1;
1274 }
1275 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001276 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1277 $2.destroy(); $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001278 $$ = $1;
1279 }
1280 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001281 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1282 $2.destroy(); $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001283 $$ = $1;
1284 }
1285 | PHI_TOK PHIList {
1286 *$1 += " " + *$2;
1287 delete $2;
1288 $$ = $1;
1289 }
1290 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
1291 if (!$2->empty())
1292 *$1 += " " + *$2;
1293 if (!$1->empty())
1294 *$1 += " ";
Reid Spencerf459d392006-12-02 16:19:52 +00001295 *$1 += *$3.newTy + " " + *$4.val + "(";
Reid Spencerf8483652006-12-02 15:16:01 +00001296 for (unsigned i = 0; i < $6->size(); ++i) {
1297 ValueInfo& VI = (*$6)[i];
1298 *$1 += *VI.val;
1299 if (i+1 < $6->size())
1300 *$1 += ", ";
1301 VI.destroy();
1302 }
1303 *$1 += ")";
Reid Spencerf459d392006-12-02 16:19:52 +00001304 delete $2; $3.destroy(); $4.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001305 $$ = $1;
1306 }
1307 | MemoryInst ;
1308
1309
1310// IndexList - List of indices for GEP based instructions...
1311IndexList
Reid Spencerf8483652006-12-02 15:16:01 +00001312 : ',' ValueRefList { $$ = $2; }
1313 | /* empty */ { $$ = new ValueList(); }
Reid Spencere7c3c602006-11-30 06:36:44 +00001314 ;
1315
1316OptVolatile
1317 : VOLATILE
1318 | /* empty */ { $$ = new std::string(); }
1319 ;
1320
1321MemoryInst : MALLOC Types OptCAlign {
Reid Spencere77e35e2006-12-01 20:26:20 +00001322 *$1 += " " + *$2.newTy;
Reid Spencere7c3c602006-11-30 06:36:44 +00001323 if (!$3->empty())
1324 *$1 += " " + *$3;
Reid Spencere77e35e2006-12-01 20:26:20 +00001325 $2.destroy(); delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001326 $$ = $1;
1327 }
1328 | MALLOC Types ',' UINT ValueRef OptCAlign {
Reid Spencerf459d392006-12-02 16:19:52 +00001329 *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5.val;
Reid Spencere7c3c602006-11-30 06:36:44 +00001330 if (!$6->empty())
1331 *$1 += " " + *$6;
Reid Spencerf459d392006-12-02 16:19:52 +00001332 $2.destroy(); $4.destroy(); $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001333 $$ = $1;
1334 }
1335 | ALLOCA Types OptCAlign {
Reid Spencere77e35e2006-12-01 20:26:20 +00001336 *$1 += " " + *$2.newTy;
Reid Spencere7c3c602006-11-30 06:36:44 +00001337 if (!$3->empty())
1338 *$1 += " " + *$3;
Reid Spencere77e35e2006-12-01 20:26:20 +00001339 $2.destroy(); delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001340 $$ = $1;
1341 }
1342 | ALLOCA Types ',' UINT ValueRef OptCAlign {
Reid Spencerf459d392006-12-02 16:19:52 +00001343 *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5.val;
Reid Spencere7c3c602006-11-30 06:36:44 +00001344 if (!$6->empty())
1345 *$1 += " " + *$6;
Reid Spencerf459d392006-12-02 16:19:52 +00001346 $2.destroy(); $4.destroy(); $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001347 $$ = $1;
1348 }
1349 | FREE ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001350 *$1 += " " + *$2.val;
1351 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001352 $$ = $1;
1353 }
1354 | OptVolatile LOAD Types ValueRef {
1355 if (!$1->empty())
1356 *$1 += " ";
Reid Spencerf459d392006-12-02 16:19:52 +00001357 *$1 += *$2 + " " + *$3.newTy + " " + *$4.val;
1358 delete $2; $3.destroy(); $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001359 $$ = $1;
1360 }
1361 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
1362 if (!$1->empty())
1363 *$1 += " ";
Reid Spencerf459d392006-12-02 16:19:52 +00001364 *$1 += *$2 + " " + *$3.val + ", " + *$5.newTy + " " + *$6.val;
1365 delete $2; $3.destroy(); $5.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001366 $$ = $1;
1367 }
1368 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencerf459d392006-12-02 16:19:52 +00001369 // Upgrade the indices
1370 for (unsigned i = 0; i < $4->size(); ++i) {
1371 ValueInfo& VI = (*$4)[i];
1372 if (VI.type.isUnsigned() && !VI.isConstant() &&
1373 VI.type.getBitWidth() < 64) {
1374 std::string* old = VI.val;
1375 *O << " %gep_upgrade" << unique << " = zext " << *old
1376 << " to ulong\n";
1377 VI.val = new std::string("ulong %gep_upgrade" + llvm::utostr(unique++));
1378 VI.type.oldTy = ULongTy;
1379 delete old;
1380 }
1381 }
1382 *$1 += " " + *$2.newTy + " " + *$3.val;
Reid Spencerf8483652006-12-02 15:16:01 +00001383 for (unsigned i = 0; i < $4->size(); ++i) {
1384 ValueInfo& VI = (*$4)[i];
1385 *$1 += ", " + *VI.val;
1386 VI.destroy();
1387 }
Reid Spencerf459d392006-12-02 16:19:52 +00001388 $2.destroy(); $3.destroy(); delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00001389 $$ = $1;
1390 };
1391
1392%%
1393
1394int yyerror(const char *ErrorMsg) {
1395 std::string where
1396 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
1397 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
1398 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
1399 if (yychar == YYEMPTY || yychar == 0)
1400 errMsg += "end-of-file.";
1401 else
1402 errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
1403 std::cerr << errMsg << '\n';
1404 exit(1);
1405}