blob: 5a4118d8d74d7693bc450bd42e1e2b1b4b24e0dc [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 Spencer996fb282006-12-06 06:30:15 +000026#define UPGRADE_SETCOND_OPS 0
Reid Spencerf0cf1322006-12-07 04:23:03 +000027#define GENERATE_FCMP_INSTS 0
Reid Spencere7c3c602006-11-30 06:36:44 +000028
29int yylex(); // declaration" of xxx warnings.
30int yyparse();
Reid Spencere77e35e2006-12-01 20:26:20 +000031extern int yydebug;
Reid Spencere7c3c602006-11-30 06:36:44 +000032
33static std::string CurFilename;
Reid Spencere7c3c602006-11-30 06:36:44 +000034static std::ostream *O = 0;
Reid Spencer96839be2006-11-30 16:50:26 +000035std::istream* LexInput = 0;
Reid Spencere77e35e2006-12-01 20:26:20 +000036unsigned SizeOfPointer = 32;
Reid Spencerf459d392006-12-02 16:19:52 +000037static uint64_t unique = 1;
Reid Spencer96839be2006-11-30 16:50:26 +000038
Reid Spencera50d5962006-12-02 04:11:07 +000039typedef std::vector<TypeInfo> TypeVector;
40static TypeVector EnumeratedTypes;
41typedef std::map<std::string,TypeInfo> TypeMap;
42static TypeMap NamedTypes;
Reid Spencerf12ee422006-12-05 19:21:25 +000043static TypeMap Globals;
Reid Spencera50d5962006-12-02 04:11:07 +000044
Reid Spencerf8483652006-12-02 15:16:01 +000045void destroy(ValueList* VL) {
46 while (!VL->empty()) {
47 ValueInfo& VI = VL->back();
48 VI.destroy();
49 VL->pop_back();
50 }
51 delete VL;
52}
53
Reid Spencer96839be2006-11-30 16:50:26 +000054void UpgradeAssembly(const std::string &infile, std::istream& in,
Reid Spencere77e35e2006-12-01 20:26:20 +000055 std::ostream &out, bool debug)
Reid Spencere7c3c602006-11-30 06:36:44 +000056{
57 Upgradelineno = 1;
58 CurFilename = infile;
Reid Spencer96839be2006-11-30 16:50:26 +000059 LexInput = &in;
Reid Spencere77e35e2006-12-01 20:26:20 +000060 yydebug = debug;
Reid Spencere7c3c602006-11-30 06:36:44 +000061 O = &out;
62
63 if (yyparse()) {
64 std::cerr << "Parse failed.\n";
65 exit(1);
66 }
67}
68
Reid Spencera50d5962006-12-02 04:11:07 +000069static void ResolveType(TypeInfo& Ty) {
70 if (Ty.oldTy == UnresolvedTy) {
71 TypeMap::iterator I = NamedTypes.find(*Ty.newTy);
Reid Spencer78720742006-12-02 20:21:22 +000072 if (I != NamedTypes.end()) {
Reid Spencera50d5962006-12-02 04:11:07 +000073 Ty.oldTy = I->second.oldTy;
Reid Spencer78720742006-12-02 20:21:22 +000074 Ty.elemTy = I->second.elemTy;
75 } else {
Reid Spencera50d5962006-12-02 04:11:07 +000076 std::string msg("Can't resolve type: ");
77 msg += *Ty.newTy;
78 yyerror(msg.c_str());
Reid Spencer280d8012006-12-01 23:40:53 +000079 }
Reid Spencera50d5962006-12-02 04:11:07 +000080 } else if (Ty.oldTy == NumericTy) {
81 unsigned ref = atoi(&((Ty.newTy->c_str())[1])); // Skip the '\\'
82 if (ref < EnumeratedTypes.size()) {
83 Ty.oldTy = EnumeratedTypes[ref].oldTy;
Reid Spencer78720742006-12-02 20:21:22 +000084 Ty.elemTy = EnumeratedTypes[ref].elemTy;
Reid Spencera50d5962006-12-02 04:11:07 +000085 } else {
86 std::string msg("Can't resolve type: ");
87 msg += *Ty.newTy;
88 yyerror(msg.c_str());
89 }
Reid Spencer280d8012006-12-01 23:40:53 +000090 }
Reid Spencera50d5962006-12-02 04:11:07 +000091 // otherwise its already resolved.
Reid Spencer280d8012006-12-01 23:40:53 +000092}
93
Reid Spencera50d5962006-12-02 04:11:07 +000094static const char* getCastOpcode(
95 std::string& Source, const TypeInfo& SrcTy, const TypeInfo& DstTy)
96{
Reid Spencere77e35e2006-12-01 20:26:20 +000097 unsigned SrcBits = SrcTy.getBitWidth();
98 unsigned DstBits = DstTy.getBitWidth();
99 const char* opcode = "bitcast";
100 // Run through the possibilities ...
101 if (DstTy.isIntegral()) { // Casting to integral
102 if (SrcTy.isIntegral()) { // Casting from integral
103 if (DstBits < SrcBits)
104 opcode = "trunc";
105 else if (DstBits > SrcBits) { // its an extension
106 if (SrcTy.isSigned())
107 opcode ="sext"; // signed -> SEXT
108 else
109 opcode = "zext"; // unsigned -> ZEXT
110 } else {
111 opcode = "bitcast"; // Same size, No-op cast
112 }
113 } else if (SrcTy.isFloatingPoint()) { // Casting from floating pt
114 if (DstTy.isSigned())
115 opcode = "fptosi"; // FP -> sint
116 else
117 opcode = "fptoui"; // FP -> uint
118 } else if (SrcTy.isPacked()) {
119 assert(DstBits == SrcTy.getBitWidth() &&
120 "Casting packed to integer of different width");
121 opcode = "bitcast"; // same size, no-op cast
122 } else {
123 assert(SrcTy.isPointer() &&
124 "Casting from a value that is not first-class type");
125 opcode = "ptrtoint"; // ptr -> int
126 }
127 } else if (DstTy.isFloatingPoint()) { // Casting to floating pt
128 if (SrcTy.isIntegral()) { // Casting from integral
129 if (SrcTy.isSigned())
130 opcode = "sitofp"; // sint -> FP
131 else
132 opcode = "uitofp"; // uint -> FP
133 } else if (SrcTy.isFloatingPoint()) { // Casting from floating pt
134 if (DstBits < SrcBits) {
135 opcode = "fptrunc"; // FP -> smaller FP
136 } else if (DstBits > SrcBits) {
137 opcode = "fpext"; // FP -> larger FP
138 } else {
139 opcode ="bitcast"; // same size, no-op cast
140 }
141 } else if (SrcTy.isPacked()) {
142 assert(DstBits == SrcTy.getBitWidth() &&
143 "Casting packed to floating point of different width");
144 opcode = "bitcast"; // same size, no-op cast
145 } else {
146 assert(0 && "Casting pointer or non-first class to float");
147 }
148 } else if (DstTy.isPacked()) {
149 if (SrcTy.isPacked()) {
150 assert(DstTy.getBitWidth() == SrcTy.getBitWidth() &&
151 "Casting packed to packed of different widths");
152 opcode = "bitcast"; // packed -> packed
153 } else if (DstTy.getBitWidth() == SrcBits) {
154 opcode = "bitcast"; // float/int -> packed
155 } else {
156 assert(!"Illegal cast to packed (wrong type or size)");
157 }
158 } else if (DstTy.isPointer()) {
159 if (SrcTy.isPointer()) {
160 opcode = "bitcast"; // ptr -> ptr
161 } else if (SrcTy.isIntegral()) {
162 opcode = "inttoptr"; // int -> ptr
163 } else {
Reid Spencera50d5962006-12-02 04:11:07 +0000164 assert(!"Casting invalid type to pointer");
Reid Spencere77e35e2006-12-01 20:26:20 +0000165 }
166 } else {
167 assert(!"Casting to type that is not first-class");
168 }
169 return opcode;
170}
171
Reid Spencera50d5962006-12-02 04:11:07 +0000172static std::string getCastUpgrade(
173 const std::string& Src, TypeInfo& SrcTy, TypeInfo& DstTy, bool isConst)
174{
175 std::string Result;
176 std::string Source = Src;
177 if (SrcTy.isFloatingPoint() && DstTy.isPointer()) {
178 // fp -> ptr cast is no longer supported but we must upgrade this
179 // by doing a double cast: fp -> int -> ptr
180 if (isConst)
181 Source = "ulong fptoui(" + Source + " to ulong)";
182 else {
Reid Spencerf459d392006-12-02 16:19:52 +0000183 *O << " %cast_upgrade" << unique << " = fptoui " << Source
184 << " to ulong\n";
185 Source = "ulong %cast_upgrade" + llvm::utostr(unique);
Reid Spencera50d5962006-12-02 04:11:07 +0000186 }
187 // Update the SrcTy for the getCastOpcode call below
188 SrcTy.destroy();
189 SrcTy.newTy = new std::string("ulong");
190 SrcTy.oldTy = ULongTy;
Reid Spencer5fe27e72006-12-09 19:41:25 +0000191 } else if (DstTy.oldTy == BoolTy && SrcTy.oldTy != BoolTy) {
Reid Spencera50d5962006-12-02 04:11:07 +0000192 // cast ptr %x to bool was previously defined as setne ptr %x, null
193 // The ptrtoint semantic is to truncate, not compare so we must retain
194 // the original intent by replace the cast with a setne
195 const char* comparator = SrcTy.isPointer() ? ", null" :
196 (SrcTy.isFloatingPoint() ? ", 0.0" : ", 0");
Reid Spencer187ccf82006-12-09 16:57:22 +0000197#if UPGRADE_SETCOND_OPS
198 const char* compareOp = SrcTy.isFloatingPoint() ? "setne " : "icmp ne ";
199#else
200 const char* compareOp = "setne";
201#endif
202 if (isConst) {
203 Result = "(" + Source + comparator + ")";
204 Result = compareOp + Result;
205 } else
206 Result = compareOp + Source + comparator;
Reid Spencera50d5962006-12-02 04:11:07 +0000207 return Result; // skip cast processing below
208 }
209 ResolveType(SrcTy);
210 ResolveType(DstTy);
211 std::string Opcode(getCastOpcode(Source, SrcTy, DstTy));
212 if (isConst)
213 Result += Opcode + "( " + Source + " to " + *DstTy.newTy + ")";
214 else
215 Result += Opcode + " " + Source + " to " + *DstTy.newTy;
216 return Result;
217}
218
Reid Spencer78720742006-12-02 20:21:22 +0000219const char* getDivRemOpcode(const std::string& opcode, const TypeInfo& TI) {
220 const char* op = opcode.c_str();
221 TypeInfo Ty = TI;
222 ResolveType(Ty);
223 if (Ty.isPacked())
224 Ty.oldTy = Ty.getElementType();
225 if (opcode == "div")
226 if (Ty.isFloatingPoint())
227 op = "fdiv";
228 else if (Ty.isUnsigned())
229 op = "udiv";
230 else if (Ty.isSigned())
231 op = "sdiv";
232 else
233 yyerror("Invalid type for div instruction");
234 else if (opcode == "rem")
235 if (Ty.isFloatingPoint())
236 op = "frem";
237 else if (Ty.isUnsigned())
238 op = "urem";
239 else if (Ty.isSigned())
240 op = "srem";
241 else
242 yyerror("Invalid type for rem instruction");
243 return op;
244}
Reid Spencer229e9362006-12-02 22:14:11 +0000245
246std::string
247getCompareOp(const std::string& setcc, const TypeInfo& TI) {
248 assert(setcc.length() == 5);
249 char cc1 = setcc[3];
250 char cc2 = setcc[4];
251 assert(cc1 == 'e' || cc1 == 'n' || cc1 == 'l' || cc1 == 'g');
252 assert(cc2 == 'q' || cc2 == 'e' || cc2 == 'e' || cc2 == 't');
253 std::string result("xcmp xxx");
254 result[6] = cc1;
255 result[7] = cc2;
256 if (TI.isFloatingPoint()) {
Reid Spencerf0cf1322006-12-07 04:23:03 +0000257#if GENERATE_FCMP_INSTS
Reid Spencer229e9362006-12-02 22:14:11 +0000258 result[0] = 'f';
259 result[5] = 'o'; // FIXME: Always map to ordered comparison ?
Reid Spencerf0cf1322006-12-07 04:23:03 +0000260 if (cc1 == 'n')
261 result[5] = 'u'; // NE maps to unordered
262 else
263 result[5] = 'o'; // everything else maps to ordered
264#else
265 result = setcc;
266#endif
Reid Spencer229e9362006-12-02 22:14:11 +0000267 } else if (TI.isIntegral() || TI.isPointer()) {
268 result[0] = 'i';
269 if ((cc1 == 'e' && cc2 == 'q') || (cc1 == 'n' && cc2 == 'e'))
270 result.erase(5,1);
271 else if (TI.isSigned())
272 result[5] = 's';
Reid Spencer3e5ab8c2006-12-06 06:25:46 +0000273 else if (TI.isUnsigned() || TI.isPointer() || TI.isBool())
Reid Spencer229e9362006-12-02 22:14:11 +0000274 result[5] = 'u';
275 else
276 yyerror("Invalid integral type for setcc");
277 }
278 return result;
279}
280
Reid Spencere7c3c602006-11-30 06:36:44 +0000281%}
282
Reid Spencerf0cf1322006-12-07 04:23:03 +0000283// %file-prefix="UpgradeParser"
Reid Spencere77e35e2006-12-01 20:26:20 +0000284
285%union {
286 std::string* String;
287 TypeInfo Type;
288 ValueInfo Value;
289 ConstInfo Const;
Reid Spencerf8483652006-12-02 15:16:01 +0000290 ValueList* ValList;
Reid Spencere77e35e2006-12-01 20:26:20 +0000291}
292
Reid Spencerf2d55322006-12-01 21:52:30 +0000293%token <Type> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
Reid Spencera50d5962006-12-02 04:11:07 +0000294%token <Type> FLOAT DOUBLE LABEL
295%token <String> OPAQUE ESINT64VAL EUINT64VAL SINTVAL UINTVAL FPVAL
Reid Spencerf2d55322006-12-01 21:52:30 +0000296%token <String> NULL_TOK UNDEF ZEROINITIALIZER TRUETOK FALSETOK
Reid Spencere77e35e2006-12-01 20:26:20 +0000297%token <String> TYPE VAR_ID LABELSTR STRINGCONSTANT
298%token <String> IMPLEMENTATION BEGINTOK ENDTOK
299%token <String> DECLARE GLOBAL CONSTANT SECTION VOLATILE
300%token <String> TO DOTDOTDOT CONST INTERNAL LINKONCE WEAK
301%token <String> DLLIMPORT DLLEXPORT EXTERN_WEAK APPENDING
302%token <String> NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG
Reid Spencer78720742006-12-02 20:21:22 +0000303%token <String> ALIGN UNINITIALIZED
Reid Spencere77e35e2006-12-01 20:26:20 +0000304%token <String> DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
305%token <String> CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
306%token <String> X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
307%token <String> DATALAYOUT
Reid Spencer78720742006-12-02 20:21:22 +0000308%token <String> RET BR SWITCH INVOKE EXCEPT UNWIND UNREACHABLE
309%token <String> ADD SUB MUL DIV UDIV SDIV FDIV REM UREM SREM FREM AND OR XOR
Reid Spencere77e35e2006-12-01 20:26:20 +0000310%token <String> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comparators
Reid Spencer229e9362006-12-02 22:14:11 +0000311%token <String> ICMP FCMP EQ NE SLT SGT SLE SGE OEQ ONE OLT OGT OLE OGE
312%token <String> ORD UNO UEQ UNE ULT UGT ULE UGE
Reid Spencere77e35e2006-12-01 20:26:20 +0000313%token <String> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Reid Spencerf7bde222006-12-01 22:26:37 +0000314%token <String> PHI_TOK SELECT SHL SHR ASHR LSHR VAARG
Reid Spencere77e35e2006-12-01 20:26:20 +0000315%token <String> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Reid Spencerfcb5df82006-12-01 22:34:43 +0000316%token <String> CAST TRUNC ZEXT SEXT FPTRUNC FPEXT FPTOUI FPTOSI UITOFP SITOFP
317%token <String> PTRTOINT INTTOPTR BITCAST
Reid Spencere77e35e2006-12-01 20:26:20 +0000318
319%type <String> OptAssign OptLinkage OptCallingConv OptAlign OptCAlign
320%type <String> SectionString OptSection GlobalVarAttributes GlobalVarAttribute
321%type <String> ArgTypeListI ConstExpr DefinitionList
322%type <String> ConstPool TargetDefinition LibrariesDefinition LibList OptName
323%type <String> ArgVal ArgListH ArgList FunctionHeaderH BEGIN FunctionHeader END
324%type <String> Function FunctionProto BasicBlock TypeListI
325%type <String> InstructionList BBTerminatorInst JumpTable Inst PHIList
Reid Spencer78720742006-12-02 20:21:22 +0000326%type <String> OptTailCall InstVal OptVolatile Unwind
Reid Spencere77e35e2006-12-01 20:26:20 +0000327%type <String> MemoryInst SymbolicValueRef OptSideEffect GlobalType
328%type <String> FnDeclareLinkage BasicBlockList BigOrLittle AsmBlock
Reid Spencer78720742006-12-02 20:21:22 +0000329%type <String> Name ConstValueRef ConstVector External
Reid Spencer57f28f92006-12-03 07:10:26 +0000330%type <String> ShiftOps SetCondOps LogicalOps ArithmeticOps CastOps
331%type <String> IPredicates FPredicates
Reid Spencere77e35e2006-12-01 20:26:20 +0000332
Reid Spencerf8483652006-12-02 15:16:01 +0000333%type <ValList> ValueRefList ValueRefListE IndexList
Reid Spencere77e35e2006-12-01 20:26:20 +0000334
335%type <Type> IntType SIntType UIntType FPType TypesV Types
336%type <Type> PrimType UpRTypesV UpRTypes
337
Reid Spencerf2d55322006-12-01 21:52:30 +0000338%type <String> IntVal EInt64Val
339%type <Const> ConstVal
Reid Spencere77e35e2006-12-01 20:26:20 +0000340
Reid Spencerf459d392006-12-02 16:19:52 +0000341%type <Value> ValueRef ResolvedVal
Reid Spencere7c3c602006-11-30 06:36:44 +0000342
343%start Module
344
345%%
346
347// Handle constant integer size restriction and conversion...
Reid Spencerf2d55322006-12-01 21:52:30 +0000348IntVal : SINTVAL | UINTVAL ;
Reid Spencere77e35e2006-12-01 20:26:20 +0000349EInt64Val : ESINT64VAL | EUINT64VAL;
Reid Spencere7c3c602006-11-30 06:36:44 +0000350
351// Operations that are notably excluded from this list include:
352// RET, BR, & SWITCH because they end basic blocks and are treated specially.
Reid Spencer78720742006-12-02 20:21:22 +0000353ArithmeticOps: ADD | SUB | MUL | DIV | UDIV | SDIV | FDIV
354 | REM | UREM | SREM | FREM;
Reid Spencere7c3c602006-11-30 06:36:44 +0000355LogicalOps : AND | OR | XOR;
356SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
Reid Spencer57f28f92006-12-03 07:10:26 +0000357IPredicates : EQ | NE | SLT | SGT | SLE | SGE | ULT | UGT | ULE | UGE;
358FPredicates : OEQ | ONE | OLT | OGT | OLE | OGE | ORD | UNO | UEQ | UNE
359 | ULT | UGT | ULE | UGE | TRUETOK | FALSETOK;
Reid Spencerf7bde222006-12-01 22:26:37 +0000360ShiftOps : SHL | SHR | ASHR | LSHR;
Reid Spencerfcb5df82006-12-01 22:34:43 +0000361CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | FPTOUI | FPTOSI |
362 UITOFP | SITOFP | PTRTOINT | INTTOPTR | BITCAST | CAST
363 ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000364
365// These are some types that allow classification if we only want a particular
366// thing... for example, only a signed, unsigned, or integral type.
367SIntType : LONG | INT | SHORT | SBYTE;
368UIntType : ULONG | UINT | USHORT | UBYTE;
369IntType : SIntType | UIntType;
370FPType : FLOAT | DOUBLE;
371
372// OptAssign - Value producing statements have an optional assignment component
373OptAssign : Name '=' {
Reid Spencere7c3c602006-11-30 06:36:44 +0000374 $$ = $1;
375 }
376 | /*empty*/ {
377 $$ = new std::string("");
378 };
379
380OptLinkage
381 : INTERNAL | LINKONCE | WEAK | APPENDING | DLLIMPORT | DLLEXPORT
382 | EXTERN_WEAK
383 | /*empty*/ { $$ = new std::string(""); } ;
384
385OptCallingConv
386 : CCC_TOK | CSRETCC_TOK | FASTCC_TOK | COLDCC_TOK | X86_STDCALLCC_TOK
Reid Spencer16244f42006-12-01 21:10:07 +0000387 | X86_FASTCALLCC_TOK
388 | CC_TOK EUINT64VAL {
Reid Spencerf2d55322006-12-01 21:52:30 +0000389 *$1 += *$2;
390 delete $2;
Reid Spencer16244f42006-12-01 21:10:07 +0000391 $$ = $1;
392 }
Reid Spencere7c3c602006-11-30 06:36:44 +0000393 | /*empty*/ { $$ = new std::string(""); } ;
394
395// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
396// a comma before it.
397OptAlign
398 : /*empty*/ { $$ = new std::string(); }
Reid Spencerf2d55322006-12-01 21:52:30 +0000399 | ALIGN EUINT64VAL { *$1 += " " + *$2; delete $2; $$ = $1; };
Reid Spencerf0cf1322006-12-07 04:23:03 +0000400
Reid Spencere7c3c602006-11-30 06:36:44 +0000401OptCAlign
402 : /*empty*/ { $$ = new std::string(); }
403 | ',' ALIGN EUINT64VAL {
404 $2->insert(0, ", ");
Reid Spencerf2d55322006-12-01 21:52:30 +0000405 *$2 += " " + *$3;
406 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000407 $$ = $2;
408 };
409
410SectionString
411 : SECTION STRINGCONSTANT {
412 *$1 += " " + *$2;
413 delete $2;
414 $$ = $1;
415 };
416
417OptSection : /*empty*/ { $$ = new std::string(); }
418 | SectionString;
419
420GlobalVarAttributes
421 : /* empty */ { $$ = new std::string(); }
422 | ',' GlobalVarAttribute GlobalVarAttributes {
423 $2->insert(0, ", ");
424 if (!$3->empty())
425 *$2 += " " + *$3;
426 delete $3;
427 $$ = $2;
428 };
429
430GlobalVarAttribute
431 : SectionString
432 | ALIGN EUINT64VAL {
Reid Spencerf2d55322006-12-01 21:52:30 +0000433 *$1 += " " + *$2;
434 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000435 $$ = $1;
436 };
437
438//===----------------------------------------------------------------------===//
439// Types includes all predefined types... except void, because it can only be
440// used in specific contexts (function returning void for example). To have
441// access to it, a user must explicitly use TypesV.
442//
443
444// TypesV includes all of 'Types', but it also includes the void type.
445TypesV : Types | VOID ;
446UpRTypesV : UpRTypes | VOID ;
447Types : UpRTypes ;
448
449// Derived types are added later...
450//
451PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
Reid Spencere77e35e2006-12-01 20:26:20 +0000452PrimType : LONG | ULONG | FLOAT | DOUBLE | LABEL;
Reid Spencera50d5962006-12-02 04:11:07 +0000453UpRTypes
454 : OPAQUE {
455 $$.newTy = $1;
456 $$.oldTy = OpaqueTy;
457 }
458 | SymbolicValueRef {
459 $$.newTy = $1;
460 $$.oldTy = UnresolvedTy;
461 }
Reid Spencer78720742006-12-02 20:21:22 +0000462 | PrimType {
463 $$ = $1;
464 }
465 | '\\' EUINT64VAL { // Type UpReference
Reid Spencerf2d55322006-12-01 21:52:30 +0000466 $2->insert(0, "\\");
467 $$.newTy = $2;
Reid Spencera50d5962006-12-02 04:11:07 +0000468 $$.oldTy = NumericTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000469 }
470 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Reid Spencere77e35e2006-12-01 20:26:20 +0000471 *$1.newTy += "( " + *$3 + " )";
Reid Spencere7c3c602006-11-30 06:36:44 +0000472 delete $3;
Reid Spencere77e35e2006-12-01 20:26:20 +0000473 $$.newTy = $1.newTy;
474 $$.oldTy = FunctionTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000475 }
476 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Reid Spencerf2d55322006-12-01 21:52:30 +0000477 $2->insert(0,"[ ");
478 *$2 += " x " + *$4.newTy + " ]";
Reid Spencere77e35e2006-12-01 20:26:20 +0000479 delete $4.newTy;
Reid Spencerf2d55322006-12-01 21:52:30 +0000480 $$.newTy = $2;
Reid Spencere77e35e2006-12-01 20:26:20 +0000481 $$.oldTy = ArrayTy;
Reid Spencer78720742006-12-02 20:21:22 +0000482 $$.elemTy = $4.oldTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000483 }
484 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
Reid Spencerf2d55322006-12-01 21:52:30 +0000485 $2->insert(0,"< ");
486 *$2 += " x " + *$4.newTy + " >";
Reid Spencere77e35e2006-12-01 20:26:20 +0000487 delete $4.newTy;
Reid Spencerf2d55322006-12-01 21:52:30 +0000488 $$.newTy = $2;
Reid Spencere77e35e2006-12-01 20:26:20 +0000489 $$.oldTy = PackedTy;
Reid Spencer78720742006-12-02 20:21:22 +0000490 $$.elemTy = $4.oldTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000491 }
492 | '{' TypeListI '}' { // Structure type?
493 $2->insert(0, "{ ");
494 *$2 += " }";
Reid Spencere77e35e2006-12-01 20:26:20 +0000495 $$.newTy = $2;
496 $$.oldTy = StructTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000497 }
498 | '{' '}' { // Empty structure type?
Reid Spencer0b7e5072006-12-01 22:42:01 +0000499 $$.newTy = new std::string("{}");
Reid Spencere77e35e2006-12-01 20:26:20 +0000500 $$.oldTy = StructTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000501 }
502 | 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#if UPGRADE_SETCOND_OPS
683 *$1 = getCompareOp(*$1, $3.type);
684#endif
Reid Spencere77e35e2006-12-01 20:26:20 +0000685 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
686 $3.destroy(); $5.destroy();
687 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000688 }
Reid Spencer57f28f92006-12-03 07:10:26 +0000689 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
690 *$1 += "(" + *$2 + "," + *$4.cnst + "," + *$6.cnst + ")";
691 delete $2; $4.destroy(); $6.destroy();
692 $$ = $1;
693 }
694 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
Reid Spencer229e9362006-12-02 22:14:11 +0000695 *$1 += "(" + *$2 + "," + *$4.cnst + "," + *$6.cnst + ")";
696 delete $2; $4.destroy(); $6.destroy();
697 $$ = $1;
698 }
Reid Spencere7c3c602006-11-30 06:36:44 +0000699 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Reid Spencerf7bde222006-12-01 22:26:37 +0000700 const char* shiftop = $1->c_str();
701 if (*$1 == "shr")
702 shiftop = ($3.type.isUnsigned()) ? "lshr" : "ashr";
703 $$ = new std::string(shiftop);
704 *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
705 delete $1; $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000706 }
707 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000708 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
709 $3.destroy(); $5.destroy();
710 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000711 }
712 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000713 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
714 $3.destroy(); $5.destroy(); $7.destroy();
715 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000716 }
717 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000718 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
719 $3.destroy(); $5.destroy(); $7.destroy();
720 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000721 };
722
723
724// ConstVector - A list of comma separated constants.
Reid Spencere77e35e2006-12-01 20:26:20 +0000725
726ConstVector
727 : ConstVector ',' ConstVal {
728 *$1 += ", " + *$3.cnst;
729 $3.destroy();
730 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000731 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000732 | ConstVal { $$ = new std::string(*$1.cnst); $1.destroy(); }
733 ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000734
735
736// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Reid Spencere77e35e2006-12-01 20:26:20 +0000737GlobalType : GLOBAL | CONSTANT ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000738
739
740//===----------------------------------------------------------------------===//
741// Rules to match Modules
742//===----------------------------------------------------------------------===//
743
744// Module rule: Capture the result of parsing the whole file into a result
745// variable...
746//
747Module : DefinitionList {
748};
749
750// DefinitionList - Top level definitions
751//
752DefinitionList : DefinitionList Function {
753 $$ = 0;
754 }
755 | DefinitionList FunctionProto {
756 *O << *$2 << "\n";
757 delete $2;
758 $$ = 0;
759 }
760 | DefinitionList MODULE ASM_TOK AsmBlock {
761 *O << "module asm " << " " << *$4 << "\n";
Reid Spencerd154b572006-12-01 20:36:40 +0000762 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000763 }
764 | DefinitionList IMPLEMENTATION {
765 *O << "implementation\n";
Reid Spencerd154b572006-12-01 20:36:40 +0000766 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000767 }
Reid Spencera50d5962006-12-02 04:11:07 +0000768 | ConstPool { $$ = 0; }
Reid Spencere7c3c602006-11-30 06:36:44 +0000769
Reid Spencer78720742006-12-02 20:21:22 +0000770External : EXTERNAL | UNINITIALIZED { $$ = $1; *$$ = "external"; }
771
Reid Spencere7c3c602006-11-30 06:36:44 +0000772// ConstPool - Constants with optional names assigned to them.
773ConstPool : ConstPool OptAssign TYPE TypesV {
Reid Spencera50d5962006-12-02 04:11:07 +0000774 EnumeratedTypes.push_back($4);
775 if (!$2->empty()) {
776 NamedTypes[*$2].newTy = new std::string(*$4.newTy);
777 NamedTypes[*$2].oldTy = $4.oldTy;
Reid Spencer78720742006-12-02 20:21:22 +0000778 NamedTypes[*$2].elemTy = $4.elemTy;
Reid Spencera50d5962006-12-02 04:11:07 +0000779 *O << *$2 << " = ";
780 }
781 *O << "type " << *$4.newTy << "\n";
782 delete $2; delete $3; $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000783 $$ = 0;
784 }
785 | ConstPool FunctionProto { // Function prototypes can be in const pool
786 *O << *$2 << "\n";
787 delete $2;
788 $$ = 0;
789 }
790 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
791 *O << *$2 << " " << *$3 << " " << *$4 << "\n";
792 delete $2; delete $3; delete $4;
793 $$ = 0;
794 }
795 | ConstPool OptAssign OptLinkage GlobalType ConstVal GlobalVarAttributes {
Reid Spencerf12ee422006-12-05 19:21:25 +0000796 if (!$2->empty()) {
Reid Spencera50d5962006-12-02 04:11:07 +0000797 *O << *$2 << " = ";
Reid Spencerf12ee422006-12-05 19:21:25 +0000798 Globals[*$2] = $5.type.clone();
799 }
Reid Spencera50d5962006-12-02 04:11:07 +0000800 *O << *$3 << " " << *$4 << " " << *$5.cnst << " " << *$6 << "\n";
Reid Spencere77e35e2006-12-01 20:26:20 +0000801 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000802 $$ = 0;
803 }
Reid Spencer78720742006-12-02 20:21:22 +0000804 | ConstPool OptAssign External GlobalType Types GlobalVarAttributes {
Reid Spencerf12ee422006-12-05 19:21:25 +0000805 if (!$2->empty()) {
Reid Spencera50d5962006-12-02 04:11:07 +0000806 *O << *$2 << " = ";
Reid Spencerf12ee422006-12-05 19:21:25 +0000807 Globals[*$2] = $5.clone();
808 }
Reid Spencera50d5962006-12-02 04:11:07 +0000809 *O << *$3 << " " << *$4 << " " << *$5.newTy << " " << *$6 << "\n";
Reid Spencere77e35e2006-12-01 20:26:20 +0000810 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000811 $$ = 0;
812 }
813 | ConstPool OptAssign DLLIMPORT GlobalType Types GlobalVarAttributes {
Reid Spencerf12ee422006-12-05 19:21:25 +0000814 if (!$2->empty()) {
Reid Spencera50d5962006-12-02 04:11:07 +0000815 *O << *$2 << " = ";
Reid Spencerf12ee422006-12-05 19:21:25 +0000816 Globals[*$2] = $5.clone();
817 }
Reid Spencera50d5962006-12-02 04:11:07 +0000818 *O << *$3 << " " << *$4 << " " << *$5.newTy << " " << *$6 << "\n";
Reid Spencere77e35e2006-12-01 20:26:20 +0000819 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000820 $$ = 0;
821 }
822 | ConstPool OptAssign EXTERN_WEAK GlobalType Types GlobalVarAttributes {
Reid Spencerf12ee422006-12-05 19:21:25 +0000823 if (!$2->empty()) {
Reid Spencera50d5962006-12-02 04:11:07 +0000824 *O << *$2 << " = ";
Reid Spencerf12ee422006-12-05 19:21:25 +0000825 Globals[*$2] = $5.clone();
826 }
Reid Spencera50d5962006-12-02 04:11:07 +0000827 *O << *$3 << " " << *$4 << " " << *$5.newTy << " " << *$6 << "\n";
Reid Spencere77e35e2006-12-01 20:26:20 +0000828 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000829 $$ = 0;
830 }
831 | ConstPool TARGET TargetDefinition {
832 *O << *$2 << " " << *$3 << "\n";
833 delete $2; delete $3;
834 $$ = 0;
835 }
836 | ConstPool DEPLIBS '=' LibrariesDefinition {
837 *O << *$2 << " = " << *$4 << "\n";
838 delete $2; delete $4;
839 $$ = 0;
840 }
841 | /* empty: end of list */ {
842 $$ = 0;
843 };
844
845
846AsmBlock : STRINGCONSTANT ;
847
848BigOrLittle : BIG | LITTLE
849
850TargetDefinition
851 : ENDIAN '=' BigOrLittle {
Reid Spencere77e35e2006-12-01 20:26:20 +0000852 *$1 += " = " + *$3;
853 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000854 $$ = $1;
855 }
856 | POINTERSIZE '=' EUINT64VAL {
Reid Spencerf2d55322006-12-01 21:52:30 +0000857 *$1 += " = " + *$3;
858 if (*$3 == "64")
Reid Spencere77e35e2006-12-01 20:26:20 +0000859 SizeOfPointer = 64;
Reid Spencerf2d55322006-12-01 21:52:30 +0000860 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000861 $$ = $1;
862 }
863 | TRIPLE '=' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +0000864 *$1 += " = " + *$3;
865 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000866 $$ = $1;
867 }
868 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +0000869 *$1 += " = " + *$3;
870 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000871 $$ = $1;
872 };
873
874LibrariesDefinition
875 : '[' LibList ']' {
876 $2->insert(0, "[ ");
877 *$2 += " ]";
878 $$ = $2;
879 };
880
881LibList
882 : LibList ',' STRINGCONSTANT {
883 *$1 += ", " + *$3;
884 delete $3;
885 $$ = $1;
886 }
887 | STRINGCONSTANT
888 | /* empty: end of list */ {
889 $$ = new std::string();
890 };
891
892//===----------------------------------------------------------------------===//
893// Rules to match Function Headers
894//===----------------------------------------------------------------------===//
895
896Name : VAR_ID | STRINGCONSTANT;
897OptName : Name | /*empty*/ { $$ = new std::string(); };
898
899ArgVal : Types OptName {
Reid Spencere77e35e2006-12-01 20:26:20 +0000900 $$ = $1.newTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000901 if (!$2->empty())
902 *$$ += " " + *$2;
Reid Spencere77e35e2006-12-01 20:26:20 +0000903 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000904};
905
906ArgListH : ArgListH ',' ArgVal {
907 *$1 += ", " + *$3;
Reid Spencere77e35e2006-12-01 20:26:20 +0000908 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000909 }
910 | ArgVal {
911 $$ = $1;
912 };
913
914ArgList : ArgListH {
915 $$ = $1;
916 }
917 | ArgListH ',' DOTDOTDOT {
918 *$1 += ", ...";
919 $$ = $1;
Reid Spencere77e35e2006-12-01 20:26:20 +0000920 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000921 }
922 | DOTDOTDOT {
923 $$ = $1;
924 }
Reid Spencerd154b572006-12-01 20:36:40 +0000925 | /* empty */ { $$ = new std::string(); };
Reid Spencere7c3c602006-11-30 06:36:44 +0000926
927FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')'
928 OptSection OptAlign {
929 if (!$1->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000930 *$1 += " ";
Reid Spencere7c3c602006-11-30 06:36:44 +0000931 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000932 *$1 += *$2.newTy + " " + *$3 + "(" + *$5 + ")";
Reid Spencere7c3c602006-11-30 06:36:44 +0000933 if (!$7->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000934 *$1 += " " + *$7;
Reid Spencere7c3c602006-11-30 06:36:44 +0000935 }
936 if (!$8->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000937 *$1 += " " + *$8;
Reid Spencere7c3c602006-11-30 06:36:44 +0000938 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000939 $2.destroy();
940 delete $3;
941 delete $5;
942 delete $7;
943 delete $8;
944 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000945 };
946
Reid Spencer78720742006-12-02 20:21:22 +0000947BEGIN : BEGINTOK { $$ = new std::string("{"); delete $1; }
948 | '{' { $$ = new std::string ("{"); }
Reid Spencere7c3c602006-11-30 06:36:44 +0000949
950FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
951 if (!$1->empty()) {
952 *O << *$1 << " ";
953 }
954 *O << *$2 << " " << *$3 << "\n";
955 delete $1; delete $2; delete $3;
956 $$ = 0;
957};
958
Reid Spencer78720742006-12-02 20:21:22 +0000959END : ENDTOK { $$ = new std::string("}"); delete $1; }
Reid Spencere7c3c602006-11-30 06:36:44 +0000960 | '}' { $$ = new std::string("}"); };
961
962Function : FunctionHeader BasicBlockList END {
963 if ($2)
964 *O << *$2;
965 *O << '\n' << *$3 << "\n";
Reid Spencere77e35e2006-12-01 20:26:20 +0000966 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000967};
968
Reid Spencere77e35e2006-12-01 20:26:20 +0000969FnDeclareLinkage
970 : /*default*/ { $$ = new std::string(); }
Reid Spencere7c3c602006-11-30 06:36:44 +0000971 | DLLIMPORT
972 | EXTERN_WEAK
973 ;
974
975FunctionProto
976 : DECLARE FnDeclareLinkage FunctionHeaderH {
Reid Spencere77e35e2006-12-01 20:26:20 +0000977 if (!$2->empty())
978 *$1 += " " + *$2;
979 *$1 += " " + *$3;
980 delete $2;
981 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000982 $$ = $1;
983 };
984
985//===----------------------------------------------------------------------===//
986// Rules to match Basic Blocks
987//===----------------------------------------------------------------------===//
988
Reid Spencerd154b572006-12-01 20:36:40 +0000989OptSideEffect : /* empty */ { $$ = new std::string(); }
990 | SIDEEFFECT;
Reid Spencere7c3c602006-11-30 06:36:44 +0000991
Reid Spencere77e35e2006-12-01 20:26:20 +0000992ConstValueRef
Reid Spencerf2d55322006-12-01 21:52:30 +0000993 : ESINT64VAL | EUINT64VAL | FPVAL | TRUETOK | FALSETOK | NULL_TOK | UNDEF
994 | ZEROINITIALIZER
Reid Spencere7c3c602006-11-30 06:36:44 +0000995 | '<' ConstVector '>' {
996 $2->insert(0, "<");
997 *$2 += ">";
998 $$ = $2;
999 }
1000 | ConstExpr
1001 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
1002 if (!$2->empty()) {
1003 *$1 += " " + *$2;
1004 }
Reid Spencere77e35e2006-12-01 20:26:20 +00001005 *$1 += " " + *$3 + ", " + *$5;
1006 delete $2; delete $3; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +00001007 $$ = $1;
1008 };
1009
Reid Spencerf2d55322006-12-01 21:52:30 +00001010SymbolicValueRef : IntVal | Name ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001011
1012// ValueRef - A reference to a definition... either constant or symbolic
Reid Spencerf459d392006-12-02 16:19:52 +00001013ValueRef
1014 : SymbolicValueRef {
1015 $$.val = $1;
1016 $$.constant = false;
1017 $$.type.newTy = 0;
1018 $$.type.oldTy = UnresolvedTy;
1019 }
1020 | ConstValueRef {
1021 $$.val = $1;
1022 $$.constant = true;
1023 $$.type.newTy = 0;
1024 $$.type.oldTy = UnresolvedTy;
1025 }
1026 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001027
1028// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1029// type immediately preceeds the value reference, and allows complex constant
1030// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
1031ResolvedVal : Types ValueRef {
Reid Spencerf459d392006-12-02 16:19:52 +00001032 $$ = $2;
Reid Spencere77e35e2006-12-01 20:26:20 +00001033 $$.type = $1;
Reid Spencerf459d392006-12-02 16:19:52 +00001034 $$.val->insert(0, *$1.newTy + " ");
Reid Spencere7c3c602006-11-30 06:36:44 +00001035 };
1036
1037BasicBlockList : BasicBlockList BasicBlock {
Reid Spencerf2d55322006-12-01 21:52:30 +00001038 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001039 }
1040 | BasicBlock { // Do not allow functions with 0 basic blocks
Reid Spencerf2d55322006-12-01 21:52:30 +00001041 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001042 };
1043
1044
1045// Basic blocks are terminated by branching instructions:
1046// br, br/cc, switch, ret
1047//
Reid Spencer16244f42006-12-01 21:10:07 +00001048BasicBlock : InstructionList BBTerminatorInst {
Reid Spencerf2d55322006-12-01 21:52:30 +00001049 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001050 };
1051
1052InstructionList : InstructionList Inst {
1053 *O << " " << *$2 << "\n";
1054 delete $2;
1055 $$ = 0;
1056 }
1057 | /* empty */ {
1058 $$ = 0;
1059 }
1060 | LABELSTR {
1061 *O << *$1 << "\n";
1062 delete $1;
1063 $$ = 0;
1064 };
1065
Reid Spencer78720742006-12-02 20:21:22 +00001066Unwind : UNWIND | EXCEPT { $$ = $1; *$$ = "unwind"; }
1067
Reid Spencere7c3c602006-11-30 06:36:44 +00001068BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencere77e35e2006-12-01 20:26:20 +00001069 *O << " " << *$1 << " " << *$2.val << "\n";
1070 delete $1; $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001071 $$ = 0;
1072 }
1073 | RET VOID { // Return with no result...
Reid Spencere77e35e2006-12-01 20:26:20 +00001074 *O << " " << *$1 << " " << *$2.newTy << "\n";
1075 delete $1; $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001076 $$ = 0;
1077 }
1078 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencerf459d392006-12-02 16:19:52 +00001079 *O << " " << *$1 << " " << *$2.newTy << " " << *$3.val << "\n";
1080 delete $1; $2.destroy(); $3.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001081 $$ = 0;
1082 } // Conditional Branch...
1083 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Reid Spencerf459d392006-12-02 16:19:52 +00001084 *O << " " << *$1 << " " << *$2.newTy << " " << *$3.val << ", "
1085 << *$5.newTy << " " << *$6.val << ", " << *$8.newTy << " "
1086 << *$9.val << "\n";
1087 delete $1; $2.destroy(); $3.destroy(); $5.destroy(); $6.destroy();
1088 $8.destroy(); $9.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001089 $$ = 0;
1090 }
1091 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencerf459d392006-12-02 16:19:52 +00001092 *O << " " << *$1 << " " << *$2.newTy << " " << *$3.val << ", "
1093 << *$5.newTy << " " << *$6.val << " [" << *$8 << " ]\n";
1094 delete $1; $2.destroy(); $3.destroy(); $5.destroy(); $6.destroy();
1095 delete $8;
Reid Spencere7c3c602006-11-30 06:36:44 +00001096 $$ = 0;
1097 }
1098 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencerf459d392006-12-02 16:19:52 +00001099 *O << " " << *$1 << " " << *$2.newTy << " " << *$3.val << ", "
1100 << *$5.newTy << " " << *$6.val << "[]\n";
1101 delete $1; $2.destroy(); $3.destroy(); $5.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001102 $$ = 0;
1103 }
Reid Spencer16244f42006-12-01 21:10:07 +00001104 | OptAssign INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
Reid Spencer78720742006-12-02 20:21:22 +00001105 TO LABEL ValueRef Unwind LABEL ValueRef {
Reid Spencer16244f42006-12-01 21:10:07 +00001106 *O << " ";
1107 if (!$1->empty())
Reid Spencera50d5962006-12-02 04:11:07 +00001108 *O << *$1 << " = ";
Reid Spencerf459d392006-12-02 16:19:52 +00001109 *O << *$2 << " " << *$3 << " " << *$4.newTy << " " << *$5.val << " (";
Reid Spencerf8483652006-12-02 15:16:01 +00001110 for (unsigned i = 0; i < $7->size(); ++i) {
1111 ValueInfo& VI = (*$7)[i];
1112 *O << *VI.val;
1113 if (i+1 < $7->size())
1114 *O << ", ";
1115 VI.destroy();
1116 }
Reid Spencerf459d392006-12-02 16:19:52 +00001117 *O << ") " << *$9 << " " << *$10.newTy << " " << *$11.val << " "
1118 << *$12 << " " << *$13.newTy << " " << *$14.val << "\n";
1119 delete $1; delete $2; delete $3; $4.destroy(); $5.destroy(); delete $7;
1120 delete $9; $10.destroy(); $11.destroy(); delete $12; $13.destroy();
1121 $14.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001122 $$ = 0;
1123 }
Reid Spencer78720742006-12-02 20:21:22 +00001124 | Unwind {
Reid Spencere7c3c602006-11-30 06:36:44 +00001125 *O << " " << *$1 << "\n";
1126 delete $1;
1127 $$ = 0;
1128 }
1129 | UNREACHABLE {
1130 *O << " " << *$1 << "\n";
1131 delete $1;
1132 $$ = 0;
1133 };
1134
1135JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
Reid Spencerf459d392006-12-02 16:19:52 +00001136 *$1 += " " + *$2.newTy + " " + *$3 + ", " + *$5.newTy + " " + *$6.val;
1137 $2.destroy(); delete $3; $5.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001138 $$ = $1;
1139 }
1140 | IntType ConstValueRef ',' LABEL ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +00001141 $2->insert(0, *$1.newTy + " " );
Reid Spencerf459d392006-12-02 16:19:52 +00001142 *$2 += ", " + *$4.newTy + " " + *$5.val;
1143 $1.destroy(); $4.destroy(); $5.destroy();
Reid Spencere77e35e2006-12-01 20:26:20 +00001144 $$ = $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001145 };
1146
1147Inst
1148 : OptAssign InstVal {
Reid Spencera50d5962006-12-02 04:11:07 +00001149 if (!$1->empty())
1150 *$1 += " = ";
Reid Spencere7c3c602006-11-30 06:36:44 +00001151 *$1 += *$2;
1152 delete $2;
1153 $$ = $1;
1154 };
1155
1156PHIList
1157 : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencerf459d392006-12-02 16:19:52 +00001158 $3.val->insert(0, *$1.newTy + "[");
1159 *$3.val += "," + *$5.val + "]";
1160 $1.destroy(); $5.destroy();
1161 $$ = new std::string(*$3.val);
1162 $3.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001163 }
1164 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
Reid Spencerf459d392006-12-02 16:19:52 +00001165 *$1 += ", [" + *$4.val + "," + *$6.val + "]";
1166 $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001167 $$ = $1;
1168 };
1169
1170
1171ValueRefList
Reid Spencerf8483652006-12-02 15:16:01 +00001172 : ResolvedVal {
1173 $$ = new ValueList();
1174 $$->push_back($1);
1175 }
Reid Spencere7c3c602006-11-30 06:36:44 +00001176 | ValueRefList ',' ResolvedVal {
Reid Spencerf8483652006-12-02 15:16:01 +00001177 $1->push_back($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00001178 $$ = $1;
1179 };
1180
1181// ValueRefListE - Just like ValueRefList, except that it may also be empty!
1182ValueRefListE
Reid Spencerf8483652006-12-02 15:16:01 +00001183 : ValueRefList { $$ = $1; }
1184 | /*empty*/ { $$ = new ValueList(); }
Reid Spencere7c3c602006-11-30 06:36:44 +00001185 ;
1186
1187OptTailCall
1188 : TAIL CALL {
1189 *$1 += " " + *$2;
1190 delete $2;
1191 $$ = $1;
1192 }
1193 | CALL
1194 ;
1195
1196InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencer78720742006-12-02 20:21:22 +00001197 const char* op = getDivRemOpcode(*$1, $2);
1198 $$ = new std::string(op);
1199 *$$ += " " + *$2.newTy + " " + *$3.val + ", " + *$5.val;
1200 delete $1; $2.destroy(); $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001201 }
1202 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencerf459d392006-12-02 16:19:52 +00001203 *$1 += " " + *$2.newTy + " " + *$3.val + ", " + *$5.val;
1204 $2.destroy(); $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001205 $$ = $1;
1206 }
1207 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencer229e9362006-12-02 22:14:11 +00001208#if UPGRADE_SETCOND_OPS
1209 *$1 = getCompareOp(*$1, $2);
1210#endif
Reid Spencerf459d392006-12-02 16:19:52 +00001211 *$1 += " " + *$2.newTy + " " + *$3.val + ", " + *$5.val;
1212 $2.destroy(); $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001213 $$ = $1;
1214 }
Reid Spencer57f28f92006-12-03 07:10:26 +00001215 | ICMP IPredicates Types ValueRef ',' ValueRef ')' {
1216 *$1 += " " + *$2 + " " + *$4.val + "," + *$6.val + ")";
1217 delete $2; $4.destroy(); $6.destroy();
1218 $$ = $1;
1219 }
1220 | FCMP FPredicates Types ValueRef ',' ValueRef ')' {
Reid Spencer229e9362006-12-02 22:14:11 +00001221 *$1 += " " + *$2 + " " + *$4.val + "," + *$6.val + ")";
1222 delete $2; $4.destroy(); $6.destroy();
1223 $$ = $1;
1224 }
Reid Spencere7c3c602006-11-30 06:36:44 +00001225 | NOT ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001226 *$1 += " " + *$2.val;
1227 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001228 $$ = $1;
1229 }
1230 | ShiftOps ResolvedVal ',' ResolvedVal {
Reid Spencerf7bde222006-12-01 22:26:37 +00001231 const char* shiftop = $1->c_str();
1232 if (*$1 == "shr")
1233 shiftop = ($2.type.isUnsigned()) ? "lshr" : "ashr";
1234 $$ = new std::string(shiftop);
1235 *$$ += " " + *$2.val + ", " + *$4.val;
1236 delete $1; $2.destroy(); $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001237 }
Reid Spencerfcb5df82006-12-01 22:34:43 +00001238 | CastOps ResolvedVal TO Types {
Reid Spencer280d8012006-12-01 23:40:53 +00001239 std::string source = *$2.val;
Reid Spencera50d5962006-12-02 04:11:07 +00001240 TypeInfo SrcTy = $2.type;
1241 TypeInfo DstTy = $4;
1242 ResolveType(DstTy);
1243 $$ = new std::string();
Reid Spencer280d8012006-12-01 23:40:53 +00001244 if (*$1 == "cast") {
Reid Spencera50d5962006-12-02 04:11:07 +00001245 *$$ += getCastUpgrade(source, SrcTy, DstTy, false);
1246 } else {
1247 *$$ += *$1 + " " + source + " to " + *DstTy.newTy;
Reid Spencer280d8012006-12-01 23:40:53 +00001248 }
Reid Spencere77e35e2006-12-01 20:26:20 +00001249 delete $1; $2.destroy();
1250 delete $3; $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001251 }
1252 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001253 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1254 $2.destroy(); $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001255 $$ = $1;
1256 }
1257 | VAARG ResolvedVal ',' Types {
Reid Spencere77e35e2006-12-01 20:26:20 +00001258 *$1 += " " + *$2.val + ", " + *$4.newTy;
1259 $2.destroy(); $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001260 $$ = $1;
1261 }
1262 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001263 *$1 += " " + *$2.val + ", " + *$4.val;
1264 $2.destroy(); $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001265 $$ = $1;
1266 }
1267 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001268 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1269 $2.destroy(); $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001270 $$ = $1;
1271 }
1272 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001273 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1274 $2.destroy(); $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001275 $$ = $1;
1276 }
1277 | PHI_TOK PHIList {
1278 *$1 += " " + *$2;
1279 delete $2;
1280 $$ = $1;
1281 }
1282 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
1283 if (!$2->empty())
1284 *$1 += " " + *$2;
1285 if (!$1->empty())
1286 *$1 += " ";
Reid Spencerf459d392006-12-02 16:19:52 +00001287 *$1 += *$3.newTy + " " + *$4.val + "(";
Reid Spencerf8483652006-12-02 15:16:01 +00001288 for (unsigned i = 0; i < $6->size(); ++i) {
1289 ValueInfo& VI = (*$6)[i];
1290 *$1 += *VI.val;
1291 if (i+1 < $6->size())
1292 *$1 += ", ";
1293 VI.destroy();
1294 }
1295 *$1 += ")";
Reid Spencerf459d392006-12-02 16:19:52 +00001296 delete $2; $3.destroy(); $4.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001297 $$ = $1;
1298 }
1299 | MemoryInst ;
1300
1301
1302// IndexList - List of indices for GEP based instructions...
1303IndexList
Reid Spencerf8483652006-12-02 15:16:01 +00001304 : ',' ValueRefList { $$ = $2; }
1305 | /* empty */ { $$ = new ValueList(); }
Reid Spencere7c3c602006-11-30 06:36:44 +00001306 ;
1307
1308OptVolatile
1309 : VOLATILE
1310 | /* empty */ { $$ = new std::string(); }
1311 ;
1312
1313MemoryInst : MALLOC Types OptCAlign {
Reid Spencere77e35e2006-12-01 20:26:20 +00001314 *$1 += " " + *$2.newTy;
Reid Spencere7c3c602006-11-30 06:36:44 +00001315 if (!$3->empty())
1316 *$1 += " " + *$3;
Reid Spencere77e35e2006-12-01 20:26:20 +00001317 $2.destroy(); delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001318 $$ = $1;
1319 }
1320 | MALLOC Types ',' UINT ValueRef OptCAlign {
Reid Spencerf459d392006-12-02 16:19:52 +00001321 *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5.val;
Reid Spencere7c3c602006-11-30 06:36:44 +00001322 if (!$6->empty())
1323 *$1 += " " + *$6;
Reid Spencerf459d392006-12-02 16:19:52 +00001324 $2.destroy(); $4.destroy(); $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001325 $$ = $1;
1326 }
1327 | ALLOCA Types OptCAlign {
Reid Spencere77e35e2006-12-01 20:26:20 +00001328 *$1 += " " + *$2.newTy;
Reid Spencere7c3c602006-11-30 06:36:44 +00001329 if (!$3->empty())
1330 *$1 += " " + *$3;
Reid Spencere77e35e2006-12-01 20:26:20 +00001331 $2.destroy(); delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001332 $$ = $1;
1333 }
1334 | ALLOCA Types ',' UINT ValueRef OptCAlign {
Reid Spencerf459d392006-12-02 16:19:52 +00001335 *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5.val;
Reid Spencere7c3c602006-11-30 06:36:44 +00001336 if (!$6->empty())
1337 *$1 += " " + *$6;
Reid Spencerf459d392006-12-02 16:19:52 +00001338 $2.destroy(); $4.destroy(); $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001339 $$ = $1;
1340 }
1341 | FREE ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001342 *$1 += " " + *$2.val;
1343 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001344 $$ = $1;
1345 }
1346 | OptVolatile LOAD Types ValueRef {
1347 if (!$1->empty())
1348 *$1 += " ";
Reid Spencerf459d392006-12-02 16:19:52 +00001349 *$1 += *$2 + " " + *$3.newTy + " " + *$4.val;
1350 delete $2; $3.destroy(); $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001351 $$ = $1;
1352 }
1353 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
1354 if (!$1->empty())
1355 *$1 += " ";
Reid Spencerf459d392006-12-02 16:19:52 +00001356 *$1 += *$2 + " " + *$3.val + ", " + *$5.newTy + " " + *$6.val;
1357 delete $2; $3.destroy(); $5.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001358 $$ = $1;
1359 }
1360 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencerf459d392006-12-02 16:19:52 +00001361 // Upgrade the indices
1362 for (unsigned i = 0; i < $4->size(); ++i) {
1363 ValueInfo& VI = (*$4)[i];
1364 if (VI.type.isUnsigned() && !VI.isConstant() &&
1365 VI.type.getBitWidth() < 64) {
1366 std::string* old = VI.val;
1367 *O << " %gep_upgrade" << unique << " = zext " << *old
1368 << " to ulong\n";
1369 VI.val = new std::string("ulong %gep_upgrade" + llvm::utostr(unique++));
1370 VI.type.oldTy = ULongTy;
1371 delete old;
1372 }
1373 }
1374 *$1 += " " + *$2.newTy + " " + *$3.val;
Reid Spencerf8483652006-12-02 15:16:01 +00001375 for (unsigned i = 0; i < $4->size(); ++i) {
1376 ValueInfo& VI = (*$4)[i];
1377 *$1 += ", " + *VI.val;
1378 VI.destroy();
1379 }
Reid Spencerf459d392006-12-02 16:19:52 +00001380 $2.destroy(); $3.destroy(); delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00001381 $$ = $1;
1382 };
1383
1384%%
1385
1386int yyerror(const char *ErrorMsg) {
1387 std::string where
1388 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
1389 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
1390 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
1391 if (yychar == YYEMPTY || yychar == 0)
1392 errMsg += "end-of-file.";
1393 else
1394 errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
1395 std::cerr << errMsg << '\n';
1396 exit(1);
1397}