blob: fd1c293ec309f2e1505ec3c8ebd903213274cd47 [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 Spencerbec0b592006-12-03 03:16:48 +000026#define UPGRADE_SETCOND_OPS 0
Reid Spencere7c3c602006-11-30 06:36:44 +000027
28int yylex(); // declaration" of xxx warnings.
29int yyparse();
Reid Spencere77e35e2006-12-01 20:26:20 +000030extern int yydebug;
Reid Spencere7c3c602006-11-30 06:36:44 +000031
32static std::string CurFilename;
Reid Spencere7c3c602006-11-30 06:36:44 +000033static std::ostream *O = 0;
Reid Spencer96839be2006-11-30 16:50:26 +000034std::istream* LexInput = 0;
Reid Spencere77e35e2006-12-01 20:26:20 +000035unsigned SizeOfPointer = 32;
Reid Spencerf459d392006-12-02 16:19:52 +000036static uint64_t unique = 1;
Reid Spencer96839be2006-11-30 16:50:26 +000037
Reid Spencera50d5962006-12-02 04:11:07 +000038typedef std::vector<TypeInfo> TypeVector;
39static TypeVector EnumeratedTypes;
40typedef std::map<std::string,TypeInfo> TypeMap;
41static TypeMap NamedTypes;
Reid Spencerf12ee422006-12-05 19:21:25 +000042static TypeMap Globals;
Reid Spencera50d5962006-12-02 04:11:07 +000043
Reid Spencerf8483652006-12-02 15:16:01 +000044void destroy(ValueList* VL) {
45 while (!VL->empty()) {
46 ValueInfo& VI = VL->back();
47 VI.destroy();
48 VL->pop_back();
49 }
50 delete VL;
51}
52
Reid Spencer96839be2006-11-30 16:50:26 +000053void UpgradeAssembly(const std::string &infile, std::istream& in,
Reid Spencere77e35e2006-12-01 20:26:20 +000054 std::ostream &out, bool debug)
Reid Spencere7c3c602006-11-30 06:36:44 +000055{
56 Upgradelineno = 1;
57 CurFilename = infile;
Reid Spencer96839be2006-11-30 16:50:26 +000058 LexInput = &in;
Reid Spencere77e35e2006-12-01 20:26:20 +000059 yydebug = debug;
Reid Spencere7c3c602006-11-30 06:36:44 +000060 O = &out;
61
62 if (yyparse()) {
63 std::cerr << "Parse failed.\n";
64 exit(1);
65 }
66}
67
Reid Spencera50d5962006-12-02 04:11:07 +000068static void ResolveType(TypeInfo& Ty) {
69 if (Ty.oldTy == UnresolvedTy) {
70 TypeMap::iterator I = NamedTypes.find(*Ty.newTy);
Reid Spencer78720742006-12-02 20:21:22 +000071 if (I != NamedTypes.end()) {
Reid Spencera50d5962006-12-02 04:11:07 +000072 Ty.oldTy = I->second.oldTy;
Reid Spencer78720742006-12-02 20:21:22 +000073 Ty.elemTy = I->second.elemTy;
74 } else {
Reid Spencera50d5962006-12-02 04:11:07 +000075 std::string msg("Can't resolve type: ");
76 msg += *Ty.newTy;
77 yyerror(msg.c_str());
Reid Spencer280d8012006-12-01 23:40:53 +000078 }
Reid Spencera50d5962006-12-02 04:11:07 +000079 } else if (Ty.oldTy == NumericTy) {
80 unsigned ref = atoi(&((Ty.newTy->c_str())[1])); // Skip the '\\'
81 if (ref < EnumeratedTypes.size()) {
82 Ty.oldTy = EnumeratedTypes[ref].oldTy;
Reid Spencer78720742006-12-02 20:21:22 +000083 Ty.elemTy = EnumeratedTypes[ref].elemTy;
Reid Spencera50d5962006-12-02 04:11:07 +000084 } else {
85 std::string msg("Can't resolve type: ");
86 msg += *Ty.newTy;
87 yyerror(msg.c_str());
88 }
Reid Spencer280d8012006-12-01 23:40:53 +000089 }
Reid Spencera50d5962006-12-02 04:11:07 +000090 // otherwise its already resolved.
Reid Spencer280d8012006-12-01 23:40:53 +000091}
92
Reid Spencera50d5962006-12-02 04:11:07 +000093static const char* getCastOpcode(
94 std::string& Source, const TypeInfo& SrcTy, const TypeInfo& DstTy)
95{
Reid Spencere77e35e2006-12-01 20:26:20 +000096 unsigned SrcBits = SrcTy.getBitWidth();
97 unsigned DstBits = DstTy.getBitWidth();
98 const char* opcode = "bitcast";
99 // Run through the possibilities ...
100 if (DstTy.isIntegral()) { // Casting to integral
101 if (SrcTy.isIntegral()) { // Casting from integral
102 if (DstBits < SrcBits)
103 opcode = "trunc";
104 else if (DstBits > SrcBits) { // its an extension
105 if (SrcTy.isSigned())
106 opcode ="sext"; // signed -> SEXT
107 else
108 opcode = "zext"; // unsigned -> ZEXT
109 } else {
110 opcode = "bitcast"; // Same size, No-op cast
111 }
112 } else if (SrcTy.isFloatingPoint()) { // Casting from floating pt
113 if (DstTy.isSigned())
114 opcode = "fptosi"; // FP -> sint
115 else
116 opcode = "fptoui"; // FP -> uint
117 } else if (SrcTy.isPacked()) {
118 assert(DstBits == SrcTy.getBitWidth() &&
119 "Casting packed to integer of different width");
120 opcode = "bitcast"; // same size, no-op cast
121 } else {
122 assert(SrcTy.isPointer() &&
123 "Casting from a value that is not first-class type");
124 opcode = "ptrtoint"; // ptr -> int
125 }
126 } else if (DstTy.isFloatingPoint()) { // Casting to floating pt
127 if (SrcTy.isIntegral()) { // Casting from integral
128 if (SrcTy.isSigned())
129 opcode = "sitofp"; // sint -> FP
130 else
131 opcode = "uitofp"; // uint -> FP
132 } else if (SrcTy.isFloatingPoint()) { // Casting from floating pt
133 if (DstBits < SrcBits) {
134 opcode = "fptrunc"; // FP -> smaller FP
135 } else if (DstBits > SrcBits) {
136 opcode = "fpext"; // FP -> larger FP
137 } else {
138 opcode ="bitcast"; // same size, no-op cast
139 }
140 } else if (SrcTy.isPacked()) {
141 assert(DstBits == SrcTy.getBitWidth() &&
142 "Casting packed to floating point of different width");
143 opcode = "bitcast"; // same size, no-op cast
144 } else {
145 assert(0 && "Casting pointer or non-first class to float");
146 }
147 } else if (DstTy.isPacked()) {
148 if (SrcTy.isPacked()) {
149 assert(DstTy.getBitWidth() == SrcTy.getBitWidth() &&
150 "Casting packed to packed of different widths");
151 opcode = "bitcast"; // packed -> packed
152 } else if (DstTy.getBitWidth() == SrcBits) {
153 opcode = "bitcast"; // float/int -> packed
154 } else {
155 assert(!"Illegal cast to packed (wrong type or size)");
156 }
157 } else if (DstTy.isPointer()) {
158 if (SrcTy.isPointer()) {
159 opcode = "bitcast"; // ptr -> ptr
160 } else if (SrcTy.isIntegral()) {
161 opcode = "inttoptr"; // int -> ptr
162 } else {
Reid Spencera50d5962006-12-02 04:11:07 +0000163 assert(!"Casting invalid type to pointer");
Reid Spencere77e35e2006-12-01 20:26:20 +0000164 }
165 } else {
166 assert(!"Casting to type that is not first-class");
167 }
168 return opcode;
169}
170
Reid Spencera50d5962006-12-02 04:11:07 +0000171static std::string getCastUpgrade(
172 const std::string& Src, TypeInfo& SrcTy, TypeInfo& DstTy, bool isConst)
173{
174 std::string Result;
175 std::string Source = Src;
176 if (SrcTy.isFloatingPoint() && DstTy.isPointer()) {
177 // fp -> ptr cast is no longer supported but we must upgrade this
178 // by doing a double cast: fp -> int -> ptr
179 if (isConst)
180 Source = "ulong fptoui(" + Source + " to ulong)";
181 else {
Reid Spencerf459d392006-12-02 16:19:52 +0000182 *O << " %cast_upgrade" << unique << " = fptoui " << Source
183 << " to ulong\n";
184 Source = "ulong %cast_upgrade" + llvm::utostr(unique);
Reid Spencera50d5962006-12-02 04:11:07 +0000185 }
186 // Update the SrcTy for the getCastOpcode call below
187 SrcTy.destroy();
188 SrcTy.newTy = new std::string("ulong");
189 SrcTy.oldTy = ULongTy;
190 } else if (DstTy.oldTy == BoolTy) {
191 // cast ptr %x to bool was previously defined as setne ptr %x, null
192 // The ptrtoint semantic is to truncate, not compare so we must retain
193 // the original intent by replace the cast with a setne
194 const char* comparator = SrcTy.isPointer() ? ", null" :
195 (SrcTy.isFloatingPoint() ? ", 0.0" : ", 0");
196 if (isConst)
197 Result = "setne (" + Source + comparator + ")";
198 else
199 Result = "setne " + Source + comparator;
200 return Result; // skip cast processing below
201 }
202 ResolveType(SrcTy);
203 ResolveType(DstTy);
204 std::string Opcode(getCastOpcode(Source, SrcTy, DstTy));
205 if (isConst)
206 Result += Opcode + "( " + Source + " to " + *DstTy.newTy + ")";
207 else
208 Result += Opcode + " " + Source + " to " + *DstTy.newTy;
209 return Result;
210}
211
Reid Spencer78720742006-12-02 20:21:22 +0000212const char* getDivRemOpcode(const std::string& opcode, const TypeInfo& TI) {
213 const char* op = opcode.c_str();
214 TypeInfo Ty = TI;
215 ResolveType(Ty);
216 if (Ty.isPacked())
217 Ty.oldTy = Ty.getElementType();
218 if (opcode == "div")
219 if (Ty.isFloatingPoint())
220 op = "fdiv";
221 else if (Ty.isUnsigned())
222 op = "udiv";
223 else if (Ty.isSigned())
224 op = "sdiv";
225 else
226 yyerror("Invalid type for div instruction");
227 else if (opcode == "rem")
228 if (Ty.isFloatingPoint())
229 op = "frem";
230 else if (Ty.isUnsigned())
231 op = "urem";
232 else if (Ty.isSigned())
233 op = "srem";
234 else
235 yyerror("Invalid type for rem instruction");
236 return op;
237}
Reid Spencer229e9362006-12-02 22:14:11 +0000238
239std::string
240getCompareOp(const std::string& setcc, const TypeInfo& TI) {
241 assert(setcc.length() == 5);
242 char cc1 = setcc[3];
243 char cc2 = setcc[4];
244 assert(cc1 == 'e' || cc1 == 'n' || cc1 == 'l' || cc1 == 'g');
245 assert(cc2 == 'q' || cc2 == 'e' || cc2 == 'e' || cc2 == 't');
246 std::string result("xcmp xxx");
247 result[6] = cc1;
248 result[7] = cc2;
249 if (TI.isFloatingPoint()) {
250 result[0] = 'f';
251 result[5] = 'o'; // FIXME: Always map to ordered comparison ?
252 } else if (TI.isIntegral() || TI.isPointer()) {
253 result[0] = 'i';
254 if ((cc1 == 'e' && cc2 == 'q') || (cc1 == 'n' && cc2 == 'e'))
255 result.erase(5,1);
256 else if (TI.isSigned())
257 result[5] = 's';
258 else if (TI.isUnsigned() || TI.isPointer())
259 result[5] = 'u';
260 else
261 yyerror("Invalid integral type for setcc");
262 }
263 return result;
264}
265
Reid Spencere7c3c602006-11-30 06:36:44 +0000266%}
267
Reid Spencere77e35e2006-12-01 20:26:20 +0000268%file-prefix="UpgradeParser"
269
270%union {
271 std::string* String;
272 TypeInfo Type;
273 ValueInfo Value;
274 ConstInfo Const;
Reid Spencerf8483652006-12-02 15:16:01 +0000275 ValueList* ValList;
Reid Spencere77e35e2006-12-01 20:26:20 +0000276}
277
Reid Spencerf2d55322006-12-01 21:52:30 +0000278%token <Type> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
Reid Spencera50d5962006-12-02 04:11:07 +0000279%token <Type> FLOAT DOUBLE LABEL
280%token <String> OPAQUE ESINT64VAL EUINT64VAL SINTVAL UINTVAL FPVAL
Reid Spencerf2d55322006-12-01 21:52:30 +0000281%token <String> NULL_TOK UNDEF ZEROINITIALIZER TRUETOK FALSETOK
Reid Spencere77e35e2006-12-01 20:26:20 +0000282%token <String> TYPE VAR_ID LABELSTR STRINGCONSTANT
283%token <String> IMPLEMENTATION BEGINTOK ENDTOK
284%token <String> DECLARE GLOBAL CONSTANT SECTION VOLATILE
285%token <String> TO DOTDOTDOT CONST INTERNAL LINKONCE WEAK
286%token <String> DLLIMPORT DLLEXPORT EXTERN_WEAK APPENDING
287%token <String> NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG
Reid Spencer78720742006-12-02 20:21:22 +0000288%token <String> ALIGN UNINITIALIZED
Reid Spencere77e35e2006-12-01 20:26:20 +0000289%token <String> DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
290%token <String> CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
291%token <String> X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
292%token <String> DATALAYOUT
Reid Spencer78720742006-12-02 20:21:22 +0000293%token <String> RET BR SWITCH INVOKE EXCEPT UNWIND UNREACHABLE
294%token <String> ADD SUB MUL DIV UDIV SDIV FDIV REM UREM SREM FREM AND OR XOR
Reid Spencere77e35e2006-12-01 20:26:20 +0000295%token <String> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comparators
Reid Spencer229e9362006-12-02 22:14:11 +0000296%token <String> ICMP FCMP EQ NE SLT SGT SLE SGE OEQ ONE OLT OGT OLE OGE
297%token <String> ORD UNO UEQ UNE ULT UGT ULE UGE
Reid Spencere77e35e2006-12-01 20:26:20 +0000298%token <String> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Reid Spencerf7bde222006-12-01 22:26:37 +0000299%token <String> PHI_TOK SELECT SHL SHR ASHR LSHR VAARG
Reid Spencere77e35e2006-12-01 20:26:20 +0000300%token <String> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Reid Spencerfcb5df82006-12-01 22:34:43 +0000301%token <String> CAST TRUNC ZEXT SEXT FPTRUNC FPEXT FPTOUI FPTOSI UITOFP SITOFP
302%token <String> PTRTOINT INTTOPTR BITCAST
Reid Spencere77e35e2006-12-01 20:26:20 +0000303
304%type <String> OptAssign OptLinkage OptCallingConv OptAlign OptCAlign
305%type <String> SectionString OptSection GlobalVarAttributes GlobalVarAttribute
306%type <String> ArgTypeListI ConstExpr DefinitionList
307%type <String> ConstPool TargetDefinition LibrariesDefinition LibList OptName
308%type <String> ArgVal ArgListH ArgList FunctionHeaderH BEGIN FunctionHeader END
309%type <String> Function FunctionProto BasicBlock TypeListI
310%type <String> InstructionList BBTerminatorInst JumpTable Inst PHIList
Reid Spencer78720742006-12-02 20:21:22 +0000311%type <String> OptTailCall InstVal OptVolatile Unwind
Reid Spencere77e35e2006-12-01 20:26:20 +0000312%type <String> MemoryInst SymbolicValueRef OptSideEffect GlobalType
313%type <String> FnDeclareLinkage BasicBlockList BigOrLittle AsmBlock
Reid Spencer78720742006-12-02 20:21:22 +0000314%type <String> Name ConstValueRef ConstVector External
Reid Spencer57f28f92006-12-03 07:10:26 +0000315%type <String> ShiftOps SetCondOps LogicalOps ArithmeticOps CastOps
316%type <String> IPredicates FPredicates
Reid Spencere77e35e2006-12-01 20:26:20 +0000317
Reid Spencerf8483652006-12-02 15:16:01 +0000318%type <ValList> ValueRefList ValueRefListE IndexList
Reid Spencere77e35e2006-12-01 20:26:20 +0000319
320%type <Type> IntType SIntType UIntType FPType TypesV Types
321%type <Type> PrimType UpRTypesV UpRTypes
322
Reid Spencerf2d55322006-12-01 21:52:30 +0000323%type <String> IntVal EInt64Val
324%type <Const> ConstVal
Reid Spencere77e35e2006-12-01 20:26:20 +0000325
Reid Spencerf459d392006-12-02 16:19:52 +0000326%type <Value> ValueRef ResolvedVal
Reid Spencere7c3c602006-11-30 06:36:44 +0000327
328%start Module
329
330%%
331
332// Handle constant integer size restriction and conversion...
Reid Spencerf2d55322006-12-01 21:52:30 +0000333IntVal : SINTVAL | UINTVAL ;
Reid Spencere77e35e2006-12-01 20:26:20 +0000334EInt64Val : ESINT64VAL | EUINT64VAL;
Reid Spencere7c3c602006-11-30 06:36:44 +0000335
336// Operations that are notably excluded from this list include:
337// RET, BR, & SWITCH because they end basic blocks and are treated specially.
Reid Spencer78720742006-12-02 20:21:22 +0000338ArithmeticOps: ADD | SUB | MUL | DIV | UDIV | SDIV | FDIV
339 | REM | UREM | SREM | FREM;
Reid Spencere7c3c602006-11-30 06:36:44 +0000340LogicalOps : AND | OR | XOR;
341SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
Reid Spencer57f28f92006-12-03 07:10:26 +0000342IPredicates : EQ | NE | SLT | SGT | SLE | SGE | ULT | UGT | ULE | UGE;
343FPredicates : OEQ | ONE | OLT | OGT | OLE | OGE | ORD | UNO | UEQ | UNE
344 | ULT | UGT | ULE | UGE | TRUETOK | FALSETOK;
Reid Spencerf7bde222006-12-01 22:26:37 +0000345ShiftOps : SHL | SHR | ASHR | LSHR;
Reid Spencerfcb5df82006-12-01 22:34:43 +0000346CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | FPTOUI | FPTOSI |
347 UITOFP | SITOFP | PTRTOINT | INTTOPTR | BITCAST | CAST
348 ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000349
350// These are some types that allow classification if we only want a particular
351// thing... for example, only a signed, unsigned, or integral type.
352SIntType : LONG | INT | SHORT | SBYTE;
353UIntType : ULONG | UINT | USHORT | UBYTE;
354IntType : SIntType | UIntType;
355FPType : FLOAT | DOUBLE;
356
357// OptAssign - Value producing statements have an optional assignment component
358OptAssign : Name '=' {
Reid Spencere7c3c602006-11-30 06:36:44 +0000359 $$ = $1;
360 }
361 | /*empty*/ {
362 $$ = new std::string("");
363 };
364
365OptLinkage
366 : INTERNAL | LINKONCE | WEAK | APPENDING | DLLIMPORT | DLLEXPORT
367 | EXTERN_WEAK
368 | /*empty*/ { $$ = new std::string(""); } ;
369
370OptCallingConv
371 : CCC_TOK | CSRETCC_TOK | FASTCC_TOK | COLDCC_TOK | X86_STDCALLCC_TOK
Reid Spencer16244f42006-12-01 21:10:07 +0000372 | X86_FASTCALLCC_TOK
373 | CC_TOK EUINT64VAL {
Reid Spencerf2d55322006-12-01 21:52:30 +0000374 *$1 += *$2;
375 delete $2;
Reid Spencer16244f42006-12-01 21:10:07 +0000376 $$ = $1;
377 }
Reid Spencere7c3c602006-11-30 06:36:44 +0000378 | /*empty*/ { $$ = new std::string(""); } ;
379
380// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
381// a comma before it.
382OptAlign
383 : /*empty*/ { $$ = new std::string(); }
Reid Spencerf2d55322006-12-01 21:52:30 +0000384 | ALIGN EUINT64VAL { *$1 += " " + *$2; delete $2; $$ = $1; };
Reid Spencere7c3c602006-11-30 06:36:44 +0000385 ;
386OptCAlign
387 : /*empty*/ { $$ = new std::string(); }
388 | ',' ALIGN EUINT64VAL {
389 $2->insert(0, ", ");
Reid Spencerf2d55322006-12-01 21:52:30 +0000390 *$2 += " " + *$3;
391 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000392 $$ = $2;
393 };
394
395SectionString
396 : SECTION STRINGCONSTANT {
397 *$1 += " " + *$2;
398 delete $2;
399 $$ = $1;
400 };
401
402OptSection : /*empty*/ { $$ = new std::string(); }
403 | SectionString;
404
405GlobalVarAttributes
406 : /* empty */ { $$ = new std::string(); }
407 | ',' GlobalVarAttribute GlobalVarAttributes {
408 $2->insert(0, ", ");
409 if (!$3->empty())
410 *$2 += " " + *$3;
411 delete $3;
412 $$ = $2;
413 };
414
415GlobalVarAttribute
416 : SectionString
417 | ALIGN EUINT64VAL {
Reid Spencerf2d55322006-12-01 21:52:30 +0000418 *$1 += " " + *$2;
419 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000420 $$ = $1;
421 };
422
423//===----------------------------------------------------------------------===//
424// Types includes all predefined types... except void, because it can only be
425// used in specific contexts (function returning void for example). To have
426// access to it, a user must explicitly use TypesV.
427//
428
429// TypesV includes all of 'Types', but it also includes the void type.
430TypesV : Types | VOID ;
431UpRTypesV : UpRTypes | VOID ;
432Types : UpRTypes ;
433
434// Derived types are added later...
435//
436PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
Reid Spencere77e35e2006-12-01 20:26:20 +0000437PrimType : LONG | ULONG | FLOAT | DOUBLE | LABEL;
Reid Spencera50d5962006-12-02 04:11:07 +0000438UpRTypes
439 : OPAQUE {
440 $$.newTy = $1;
441 $$.oldTy = OpaqueTy;
442 }
443 | SymbolicValueRef {
444 $$.newTy = $1;
445 $$.oldTy = UnresolvedTy;
446 }
Reid Spencer78720742006-12-02 20:21:22 +0000447 | PrimType {
448 $$ = $1;
449 }
450 | '\\' EUINT64VAL { // Type UpReference
Reid Spencerf2d55322006-12-01 21:52:30 +0000451 $2->insert(0, "\\");
452 $$.newTy = $2;
Reid Spencera50d5962006-12-02 04:11:07 +0000453 $$.oldTy = NumericTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000454 }
455 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Reid Spencere77e35e2006-12-01 20:26:20 +0000456 *$1.newTy += "( " + *$3 + " )";
Reid Spencere7c3c602006-11-30 06:36:44 +0000457 delete $3;
Reid Spencere77e35e2006-12-01 20:26:20 +0000458 $$.newTy = $1.newTy;
459 $$.oldTy = FunctionTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000460 }
461 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Reid Spencerf2d55322006-12-01 21:52:30 +0000462 $2->insert(0,"[ ");
463 *$2 += " x " + *$4.newTy + " ]";
Reid Spencere77e35e2006-12-01 20:26:20 +0000464 delete $4.newTy;
Reid Spencerf2d55322006-12-01 21:52:30 +0000465 $$.newTy = $2;
Reid Spencere77e35e2006-12-01 20:26:20 +0000466 $$.oldTy = ArrayTy;
Reid Spencer78720742006-12-02 20:21:22 +0000467 $$.elemTy = $4.oldTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000468 }
469 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
Reid Spencerf2d55322006-12-01 21:52:30 +0000470 $2->insert(0,"< ");
471 *$2 += " x " + *$4.newTy + " >";
Reid Spencere77e35e2006-12-01 20:26:20 +0000472 delete $4.newTy;
Reid Spencerf2d55322006-12-01 21:52:30 +0000473 $$.newTy = $2;
Reid Spencere77e35e2006-12-01 20:26:20 +0000474 $$.oldTy = PackedTy;
Reid Spencer78720742006-12-02 20:21:22 +0000475 $$.elemTy = $4.oldTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000476 }
477 | '{' TypeListI '}' { // Structure type?
478 $2->insert(0, "{ ");
479 *$2 += " }";
Reid Spencere77e35e2006-12-01 20:26:20 +0000480 $$.newTy = $2;
481 $$.oldTy = StructTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000482 }
483 | '{' '}' { // Empty structure type?
Reid Spencer0b7e5072006-12-01 22:42:01 +0000484 $$.newTy = new std::string("{}");
Reid Spencere77e35e2006-12-01 20:26:20 +0000485 $$.oldTy = StructTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000486 }
487 | UpRTypes '*' { // Pointer type?
Reid Spencere77e35e2006-12-01 20:26:20 +0000488 *$1.newTy += '*';
Reid Spencer78720742006-12-02 20:21:22 +0000489 $$.elemTy = $1.oldTy;
Reid Spencere77e35e2006-12-01 20:26:20 +0000490 $1.oldTy = PointerTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000491 $$ = $1;
492 };
493
494// TypeList - Used for struct declarations and as a basis for function type
495// declaration type lists
496//
Reid Spencere77e35e2006-12-01 20:26:20 +0000497TypeListI
498 : UpRTypes {
499 $$ = $1.newTy;
500 }
501 | TypeListI ',' UpRTypes {
502 *$1 += ", " + *$3.newTy;
503 delete $3.newTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000504 $$ = $1;
505 };
506
507// ArgTypeList - List of types for a function type declaration...
Reid Spencere77e35e2006-12-01 20:26:20 +0000508ArgTypeListI
509 : TypeListI
Reid Spencere7c3c602006-11-30 06:36:44 +0000510 | TypeListI ',' DOTDOTDOT {
511 *$1 += ", ...";
512 delete $3;
513 $$ = $1;
514 }
515 | DOTDOTDOT {
516 $$ = $1;
517 }
518 | /*empty*/ {
519 $$ = new std::string();
520 };
521
522// ConstVal - The various declarations that go into the constant pool. This
523// production is used ONLY to represent constants that show up AFTER a 'const',
524// 'constant' or 'global' token at global scope. Constants that can be inlined
525// into other expressions (such as integers and constexprs) are handled by the
526// ResolvedVal, ValueRef and ConstValueRef productions.
527//
528ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencere77e35e2006-12-01 20:26:20 +0000529 $$.type = $1;
530 $$.cnst = new std::string(*$1.newTy);
531 *$$.cnst += " [ " + *$3 + " ]";
Reid Spencere7c3c602006-11-30 06:36:44 +0000532 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000533 }
534 | Types '[' ']' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000535 $$.type = $1;
536 $$.cnst = new std::string(*$1.newTy);
537 *$$.cnst += "[ ]";
Reid Spencere7c3c602006-11-30 06:36:44 +0000538 }
539 | Types 'c' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +0000540 $$.type = $1;
541 $$.cnst = new std::string(*$1.newTy);
542 *$$.cnst += " c" + *$3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000543 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000544 }
545 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencere77e35e2006-12-01 20:26:20 +0000546 $$.type = $1;
547 $$.cnst = new std::string(*$1.newTy);
548 *$$.cnst += " < " + *$3 + " >";
Reid Spencere7c3c602006-11-30 06:36:44 +0000549 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000550 }
551 | Types '{' ConstVector '}' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000552 $$.type = $1;
553 $$.cnst = new std::string(*$1.newTy);
554 *$$.cnst += " { " + *$3 + " }";
Reid Spencere7c3c602006-11-30 06:36:44 +0000555 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000556 }
557 | Types '{' '}' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000558 $$.type = $1;
559 $$.cnst = new std::string(*$1.newTy);
Reid Spencer0b7e5072006-12-01 22:42:01 +0000560 *$$.cnst += " {}";
Reid Spencere7c3c602006-11-30 06:36:44 +0000561 }
562 | Types NULL_TOK {
Reid Spencere77e35e2006-12-01 20:26:20 +0000563 $$.type = $1;
564 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000565 *$$.cnst += " " + *$2;
566 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000567 }
568 | Types UNDEF {
Reid Spencere77e35e2006-12-01 20:26:20 +0000569 $$.type = $1;
570 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000571 *$$.cnst += " " + *$2;
572 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000573 }
574 | Types SymbolicValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000575 $$.type = $1;
576 $$.cnst = new std::string(*$1.newTy);
577 *$$.cnst += " " + *$2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000578 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000579 }
580 | Types ConstExpr {
Reid Spencere77e35e2006-12-01 20:26:20 +0000581 $$.type = $1;
582 $$.cnst = new std::string(*$1.newTy);
583 *$$.cnst += " " + *$2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000584 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000585 }
586 | Types ZEROINITIALIZER {
Reid Spencere77e35e2006-12-01 20:26:20 +0000587 $$.type = $1;
588 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000589 *$$.cnst += " " + *$2;
590 delete $2;
Reid Spencere77e35e2006-12-01 20:26:20 +0000591 }
592 | SIntType EInt64Val { // integral constants
593 $$.type = $1;
594 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000595 *$$.cnst += " " + *$2;
596 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000597 }
598 | UIntType EUINT64VAL { // integral constants
Reid Spencere77e35e2006-12-01 20:26:20 +0000599 $$.type = $1;
600 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000601 *$$.cnst += " " + *$2;
602 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000603 }
604 | BOOL TRUETOK { // Boolean constants
Reid Spencere77e35e2006-12-01 20:26:20 +0000605 $$.type = $1;
606 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000607 *$$.cnst += " " + *$2;
608 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000609 }
610 | BOOL FALSETOK { // Boolean constants
Reid Spencere77e35e2006-12-01 20:26:20 +0000611 $$.type = $1;
612 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000613 *$$.cnst += " " + *$2;
614 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000615 }
616 | FPType FPVAL { // Float & Double constants
Reid Spencere77e35e2006-12-01 20:26:20 +0000617 $$.type = $1;
618 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000619 *$$.cnst += " " + *$2;
620 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000621 };
622
623
Reid Spencerfcb5df82006-12-01 22:34:43 +0000624ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencer280d8012006-12-01 23:40:53 +0000625 std::string source = *$3.cnst;
Reid Spencera50d5962006-12-02 04:11:07 +0000626 TypeInfo DstTy = $5;
627 ResolveType(DstTy);
Reid Spencer280d8012006-12-01 23:40:53 +0000628 if (*$1 == "cast") {
Reid Spencera50d5962006-12-02 04:11:07 +0000629 // Call getCastUpgrade to upgrade the old cast
630 $$ = new std::string(getCastUpgrade(source, $3.type, $5, true));
631 } else {
632 // Nothing to upgrade, just create the cast constant expr
633 $$ = new std::string(*$1);
634 *$$ += "( " + source + " to " + *$5.newTy + ")";
Reid Spencer280d8012006-12-01 23:40:53 +0000635 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000636 delete $1; $3.destroy(); delete $4; $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000637 }
638 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencerf8483652006-12-02 15:16:01 +0000639 *$1 += "(" + *$3.cnst;
640 for (unsigned i = 0; i < $4->size(); ++i) {
641 ValueInfo& VI = (*$4)[i];
642 *$1 += ", " + *VI.val;
643 VI.destroy();
644 }
645 *$1 += ")";
Reid Spencere77e35e2006-12-01 20:26:20 +0000646 $$ = $1;
647 $3.destroy();
648 delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +0000649 }
650 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000651 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
652 $3.destroy(); $5.destroy(); $7.destroy();
653 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000654 }
655 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer78720742006-12-02 20:21:22 +0000656 const char* op = getDivRemOpcode(*$1, $3.type);
657 $$ = new std::string(op);
658 *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
659 delete $1; $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000660 }
661 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000662 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
663 $3.destroy(); $5.destroy();
664 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000665 }
666 | SetCondOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer229e9362006-12-02 22:14:11 +0000667#if UPGRADE_SETCOND_OPS
668 *$1 = getCompareOp(*$1, $3.type);
669#endif
Reid Spencere77e35e2006-12-01 20:26:20 +0000670 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
671 $3.destroy(); $5.destroy();
672 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000673 }
Reid Spencer57f28f92006-12-03 07:10:26 +0000674 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
675 *$1 += "(" + *$2 + "," + *$4.cnst + "," + *$6.cnst + ")";
676 delete $2; $4.destroy(); $6.destroy();
677 $$ = $1;
678 }
679 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
Reid Spencer229e9362006-12-02 22:14:11 +0000680 *$1 += "(" + *$2 + "," + *$4.cnst + "," + *$6.cnst + ")";
681 delete $2; $4.destroy(); $6.destroy();
682 $$ = $1;
683 }
Reid Spencere7c3c602006-11-30 06:36:44 +0000684 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Reid Spencerf7bde222006-12-01 22:26:37 +0000685 const char* shiftop = $1->c_str();
686 if (*$1 == "shr")
687 shiftop = ($3.type.isUnsigned()) ? "lshr" : "ashr";
688 $$ = new std::string(shiftop);
689 *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
690 delete $1; $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000691 }
692 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000693 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
694 $3.destroy(); $5.destroy();
695 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000696 }
697 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000698 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
699 $3.destroy(); $5.destroy(); $7.destroy();
700 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000701 }
702 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000703 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
704 $3.destroy(); $5.destroy(); $7.destroy();
705 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000706 };
707
708
709// ConstVector - A list of comma separated constants.
Reid Spencere77e35e2006-12-01 20:26:20 +0000710
711ConstVector
712 : ConstVector ',' ConstVal {
713 *$1 += ", " + *$3.cnst;
714 $3.destroy();
715 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000716 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000717 | ConstVal { $$ = new std::string(*$1.cnst); $1.destroy(); }
718 ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000719
720
721// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Reid Spencere77e35e2006-12-01 20:26:20 +0000722GlobalType : GLOBAL | CONSTANT ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000723
724
725//===----------------------------------------------------------------------===//
726// Rules to match Modules
727//===----------------------------------------------------------------------===//
728
729// Module rule: Capture the result of parsing the whole file into a result
730// variable...
731//
732Module : DefinitionList {
733};
734
735// DefinitionList - Top level definitions
736//
737DefinitionList : DefinitionList Function {
738 $$ = 0;
739 }
740 | DefinitionList FunctionProto {
741 *O << *$2 << "\n";
742 delete $2;
743 $$ = 0;
744 }
745 | DefinitionList MODULE ASM_TOK AsmBlock {
746 *O << "module asm " << " " << *$4 << "\n";
Reid Spencerd154b572006-12-01 20:36:40 +0000747 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000748 }
749 | DefinitionList IMPLEMENTATION {
750 *O << "implementation\n";
Reid Spencerd154b572006-12-01 20:36:40 +0000751 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000752 }
Reid Spencera50d5962006-12-02 04:11:07 +0000753 | ConstPool { $$ = 0; }
Reid Spencere7c3c602006-11-30 06:36:44 +0000754
Reid Spencer78720742006-12-02 20:21:22 +0000755External : EXTERNAL | UNINITIALIZED { $$ = $1; *$$ = "external"; }
756
Reid Spencere7c3c602006-11-30 06:36:44 +0000757// ConstPool - Constants with optional names assigned to them.
758ConstPool : ConstPool OptAssign TYPE TypesV {
Reid Spencera50d5962006-12-02 04:11:07 +0000759 EnumeratedTypes.push_back($4);
760 if (!$2->empty()) {
761 NamedTypes[*$2].newTy = new std::string(*$4.newTy);
762 NamedTypes[*$2].oldTy = $4.oldTy;
Reid Spencer78720742006-12-02 20:21:22 +0000763 NamedTypes[*$2].elemTy = $4.elemTy;
Reid Spencera50d5962006-12-02 04:11:07 +0000764 *O << *$2 << " = ";
765 }
766 *O << "type " << *$4.newTy << "\n";
767 delete $2; delete $3; $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000768 $$ = 0;
769 }
770 | ConstPool FunctionProto { // Function prototypes can be in const pool
771 *O << *$2 << "\n";
772 delete $2;
773 $$ = 0;
774 }
775 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
776 *O << *$2 << " " << *$3 << " " << *$4 << "\n";
777 delete $2; delete $3; delete $4;
778 $$ = 0;
779 }
780 | ConstPool OptAssign OptLinkage GlobalType ConstVal GlobalVarAttributes {
Reid Spencerf12ee422006-12-05 19:21:25 +0000781 if (!$2->empty()) {
Reid Spencera50d5962006-12-02 04:11:07 +0000782 *O << *$2 << " = ";
Reid Spencerf12ee422006-12-05 19:21:25 +0000783 Globals[*$2] = $5.type.clone();
784 }
Reid Spencera50d5962006-12-02 04:11:07 +0000785 *O << *$3 << " " << *$4 << " " << *$5.cnst << " " << *$6 << "\n";
Reid Spencere77e35e2006-12-01 20:26:20 +0000786 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000787 $$ = 0;
788 }
Reid Spencer78720742006-12-02 20:21:22 +0000789 | ConstPool OptAssign External GlobalType Types GlobalVarAttributes {
Reid Spencerf12ee422006-12-05 19:21:25 +0000790 if (!$2->empty()) {
Reid Spencera50d5962006-12-02 04:11:07 +0000791 *O << *$2 << " = ";
Reid Spencerf12ee422006-12-05 19:21:25 +0000792 Globals[*$2] = $5.clone();
793 }
Reid Spencera50d5962006-12-02 04:11:07 +0000794 *O << *$3 << " " << *$4 << " " << *$5.newTy << " " << *$6 << "\n";
Reid Spencere77e35e2006-12-01 20:26:20 +0000795 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000796 $$ = 0;
797 }
798 | ConstPool OptAssign DLLIMPORT GlobalType Types GlobalVarAttributes {
Reid Spencerf12ee422006-12-05 19:21:25 +0000799 if (!$2->empty()) {
Reid Spencera50d5962006-12-02 04:11:07 +0000800 *O << *$2 << " = ";
Reid Spencerf12ee422006-12-05 19:21:25 +0000801 Globals[*$2] = $5.clone();
802 }
Reid Spencera50d5962006-12-02 04:11:07 +0000803 *O << *$3 << " " << *$4 << " " << *$5.newTy << " " << *$6 << "\n";
Reid Spencere77e35e2006-12-01 20:26:20 +0000804 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000805 $$ = 0;
806 }
807 | ConstPool OptAssign EXTERN_WEAK GlobalType Types GlobalVarAttributes {
Reid Spencerf12ee422006-12-05 19:21:25 +0000808 if (!$2->empty()) {
Reid Spencera50d5962006-12-02 04:11:07 +0000809 *O << *$2 << " = ";
Reid Spencerf12ee422006-12-05 19:21:25 +0000810 Globals[*$2] = $5.clone();
811 }
Reid Spencera50d5962006-12-02 04:11:07 +0000812 *O << *$3 << " " << *$4 << " " << *$5.newTy << " " << *$6 << "\n";
Reid Spencere77e35e2006-12-01 20:26:20 +0000813 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000814 $$ = 0;
815 }
816 | ConstPool TARGET TargetDefinition {
817 *O << *$2 << " " << *$3 << "\n";
818 delete $2; delete $3;
819 $$ = 0;
820 }
821 | ConstPool DEPLIBS '=' LibrariesDefinition {
822 *O << *$2 << " = " << *$4 << "\n";
823 delete $2; delete $4;
824 $$ = 0;
825 }
826 | /* empty: end of list */ {
827 $$ = 0;
828 };
829
830
831AsmBlock : STRINGCONSTANT ;
832
833BigOrLittle : BIG | LITTLE
834
835TargetDefinition
836 : ENDIAN '=' BigOrLittle {
Reid Spencere77e35e2006-12-01 20:26:20 +0000837 *$1 += " = " + *$3;
838 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000839 $$ = $1;
840 }
841 | POINTERSIZE '=' EUINT64VAL {
Reid Spencerf2d55322006-12-01 21:52:30 +0000842 *$1 += " = " + *$3;
843 if (*$3 == "64")
Reid Spencere77e35e2006-12-01 20:26:20 +0000844 SizeOfPointer = 64;
Reid Spencerf2d55322006-12-01 21:52:30 +0000845 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000846 $$ = $1;
847 }
848 | TRIPLE '=' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +0000849 *$1 += " = " + *$3;
850 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000851 $$ = $1;
852 }
853 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +0000854 *$1 += " = " + *$3;
855 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000856 $$ = $1;
857 };
858
859LibrariesDefinition
860 : '[' LibList ']' {
861 $2->insert(0, "[ ");
862 *$2 += " ]";
863 $$ = $2;
864 };
865
866LibList
867 : LibList ',' STRINGCONSTANT {
868 *$1 += ", " + *$3;
869 delete $3;
870 $$ = $1;
871 }
872 | STRINGCONSTANT
873 | /* empty: end of list */ {
874 $$ = new std::string();
875 };
876
877//===----------------------------------------------------------------------===//
878// Rules to match Function Headers
879//===----------------------------------------------------------------------===//
880
881Name : VAR_ID | STRINGCONSTANT;
882OptName : Name | /*empty*/ { $$ = new std::string(); };
883
884ArgVal : Types OptName {
Reid Spencere77e35e2006-12-01 20:26:20 +0000885 $$ = $1.newTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000886 if (!$2->empty())
887 *$$ += " " + *$2;
Reid Spencere77e35e2006-12-01 20:26:20 +0000888 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000889};
890
891ArgListH : ArgListH ',' ArgVal {
892 *$1 += ", " + *$3;
Reid Spencere77e35e2006-12-01 20:26:20 +0000893 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000894 }
895 | ArgVal {
896 $$ = $1;
897 };
898
899ArgList : ArgListH {
900 $$ = $1;
901 }
902 | ArgListH ',' DOTDOTDOT {
903 *$1 += ", ...";
904 $$ = $1;
Reid Spencere77e35e2006-12-01 20:26:20 +0000905 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000906 }
907 | DOTDOTDOT {
908 $$ = $1;
909 }
Reid Spencerd154b572006-12-01 20:36:40 +0000910 | /* empty */ { $$ = new std::string(); };
Reid Spencere7c3c602006-11-30 06:36:44 +0000911
912FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')'
913 OptSection OptAlign {
914 if (!$1->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000915 *$1 += " ";
Reid Spencere7c3c602006-11-30 06:36:44 +0000916 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000917 *$1 += *$2.newTy + " " + *$3 + "(" + *$5 + ")";
Reid Spencere7c3c602006-11-30 06:36:44 +0000918 if (!$7->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000919 *$1 += " " + *$7;
Reid Spencere7c3c602006-11-30 06:36:44 +0000920 }
921 if (!$8->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000922 *$1 += " " + *$8;
Reid Spencere7c3c602006-11-30 06:36:44 +0000923 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000924 $2.destroy();
925 delete $3;
926 delete $5;
927 delete $7;
928 delete $8;
929 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000930 };
931
Reid Spencer78720742006-12-02 20:21:22 +0000932BEGIN : BEGINTOK { $$ = new std::string("{"); delete $1; }
933 | '{' { $$ = new std::string ("{"); }
Reid Spencere7c3c602006-11-30 06:36:44 +0000934
935FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
936 if (!$1->empty()) {
937 *O << *$1 << " ";
938 }
939 *O << *$2 << " " << *$3 << "\n";
940 delete $1; delete $2; delete $3;
941 $$ = 0;
942};
943
Reid Spencer78720742006-12-02 20:21:22 +0000944END : ENDTOK { $$ = new std::string("}"); delete $1; }
Reid Spencere7c3c602006-11-30 06:36:44 +0000945 | '}' { $$ = new std::string("}"); };
946
947Function : FunctionHeader BasicBlockList END {
948 if ($2)
949 *O << *$2;
950 *O << '\n' << *$3 << "\n";
Reid Spencere77e35e2006-12-01 20:26:20 +0000951 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000952};
953
Reid Spencere77e35e2006-12-01 20:26:20 +0000954FnDeclareLinkage
955 : /*default*/ { $$ = new std::string(); }
Reid Spencere7c3c602006-11-30 06:36:44 +0000956 | DLLIMPORT
957 | EXTERN_WEAK
958 ;
959
960FunctionProto
961 : DECLARE FnDeclareLinkage FunctionHeaderH {
Reid Spencere77e35e2006-12-01 20:26:20 +0000962 if (!$2->empty())
963 *$1 += " " + *$2;
964 *$1 += " " + *$3;
965 delete $2;
966 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000967 $$ = $1;
968 };
969
970//===----------------------------------------------------------------------===//
971// Rules to match Basic Blocks
972//===----------------------------------------------------------------------===//
973
Reid Spencerd154b572006-12-01 20:36:40 +0000974OptSideEffect : /* empty */ { $$ = new std::string(); }
975 | SIDEEFFECT;
Reid Spencere7c3c602006-11-30 06:36:44 +0000976
Reid Spencere77e35e2006-12-01 20:26:20 +0000977ConstValueRef
Reid Spencerf2d55322006-12-01 21:52:30 +0000978 : ESINT64VAL | EUINT64VAL | FPVAL | TRUETOK | FALSETOK | NULL_TOK | UNDEF
979 | ZEROINITIALIZER
Reid Spencere7c3c602006-11-30 06:36:44 +0000980 | '<' ConstVector '>' {
981 $2->insert(0, "<");
982 *$2 += ">";
983 $$ = $2;
984 }
985 | ConstExpr
986 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
987 if (!$2->empty()) {
988 *$1 += " " + *$2;
989 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000990 *$1 += " " + *$3 + ", " + *$5;
991 delete $2; delete $3; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +0000992 $$ = $1;
993 };
994
Reid Spencerf2d55322006-12-01 21:52:30 +0000995SymbolicValueRef : IntVal | Name ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000996
997// ValueRef - A reference to a definition... either constant or symbolic
Reid Spencerf459d392006-12-02 16:19:52 +0000998ValueRef
999 : SymbolicValueRef {
1000 $$.val = $1;
1001 $$.constant = false;
1002 $$.type.newTy = 0;
1003 $$.type.oldTy = UnresolvedTy;
1004 }
1005 | ConstValueRef {
1006 $$.val = $1;
1007 $$.constant = true;
1008 $$.type.newTy = 0;
1009 $$.type.oldTy = UnresolvedTy;
1010 }
1011 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001012
1013// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1014// type immediately preceeds the value reference, and allows complex constant
1015// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
1016ResolvedVal : Types ValueRef {
Reid Spencerf459d392006-12-02 16:19:52 +00001017 $$ = $2;
Reid Spencere77e35e2006-12-01 20:26:20 +00001018 $$.type = $1;
Reid Spencerf459d392006-12-02 16:19:52 +00001019 $$.val->insert(0, *$1.newTy + " ");
Reid Spencere7c3c602006-11-30 06:36:44 +00001020 };
1021
1022BasicBlockList : BasicBlockList BasicBlock {
Reid Spencerf2d55322006-12-01 21:52:30 +00001023 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001024 }
1025 | BasicBlock { // Do not allow functions with 0 basic blocks
Reid Spencerf2d55322006-12-01 21:52:30 +00001026 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001027 };
1028
1029
1030// Basic blocks are terminated by branching instructions:
1031// br, br/cc, switch, ret
1032//
Reid Spencer16244f42006-12-01 21:10:07 +00001033BasicBlock : InstructionList BBTerminatorInst {
Reid Spencerf2d55322006-12-01 21:52:30 +00001034 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001035 };
1036
1037InstructionList : InstructionList Inst {
1038 *O << " " << *$2 << "\n";
1039 delete $2;
1040 $$ = 0;
1041 }
1042 | /* empty */ {
1043 $$ = 0;
1044 }
1045 | LABELSTR {
1046 *O << *$1 << "\n";
1047 delete $1;
1048 $$ = 0;
1049 };
1050
Reid Spencer78720742006-12-02 20:21:22 +00001051Unwind : UNWIND | EXCEPT { $$ = $1; *$$ = "unwind"; }
1052
Reid Spencere7c3c602006-11-30 06:36:44 +00001053BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencere77e35e2006-12-01 20:26:20 +00001054 *O << " " << *$1 << " " << *$2.val << "\n";
1055 delete $1; $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001056 $$ = 0;
1057 }
1058 | RET VOID { // Return with no result...
Reid Spencere77e35e2006-12-01 20:26:20 +00001059 *O << " " << *$1 << " " << *$2.newTy << "\n";
1060 delete $1; $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001061 $$ = 0;
1062 }
1063 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencerf459d392006-12-02 16:19:52 +00001064 *O << " " << *$1 << " " << *$2.newTy << " " << *$3.val << "\n";
1065 delete $1; $2.destroy(); $3.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001066 $$ = 0;
1067 } // Conditional Branch...
1068 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Reid Spencerf459d392006-12-02 16:19:52 +00001069 *O << " " << *$1 << " " << *$2.newTy << " " << *$3.val << ", "
1070 << *$5.newTy << " " << *$6.val << ", " << *$8.newTy << " "
1071 << *$9.val << "\n";
1072 delete $1; $2.destroy(); $3.destroy(); $5.destroy(); $6.destroy();
1073 $8.destroy(); $9.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001074 $$ = 0;
1075 }
1076 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencerf459d392006-12-02 16:19:52 +00001077 *O << " " << *$1 << " " << *$2.newTy << " " << *$3.val << ", "
1078 << *$5.newTy << " " << *$6.val << " [" << *$8 << " ]\n";
1079 delete $1; $2.destroy(); $3.destroy(); $5.destroy(); $6.destroy();
1080 delete $8;
Reid Spencere7c3c602006-11-30 06:36:44 +00001081 $$ = 0;
1082 }
1083 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencerf459d392006-12-02 16:19:52 +00001084 *O << " " << *$1 << " " << *$2.newTy << " " << *$3.val << ", "
1085 << *$5.newTy << " " << *$6.val << "[]\n";
1086 delete $1; $2.destroy(); $3.destroy(); $5.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001087 $$ = 0;
1088 }
Reid Spencer16244f42006-12-01 21:10:07 +00001089 | OptAssign INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
Reid Spencer78720742006-12-02 20:21:22 +00001090 TO LABEL ValueRef Unwind LABEL ValueRef {
Reid Spencer16244f42006-12-01 21:10:07 +00001091 *O << " ";
1092 if (!$1->empty())
Reid Spencera50d5962006-12-02 04:11:07 +00001093 *O << *$1 << " = ";
Reid Spencerf459d392006-12-02 16:19:52 +00001094 *O << *$2 << " " << *$3 << " " << *$4.newTy << " " << *$5.val << " (";
Reid Spencerf8483652006-12-02 15:16:01 +00001095 for (unsigned i = 0; i < $7->size(); ++i) {
1096 ValueInfo& VI = (*$7)[i];
1097 *O << *VI.val;
1098 if (i+1 < $7->size())
1099 *O << ", ";
1100 VI.destroy();
1101 }
Reid Spencerf459d392006-12-02 16:19:52 +00001102 *O << ") " << *$9 << " " << *$10.newTy << " " << *$11.val << " "
1103 << *$12 << " " << *$13.newTy << " " << *$14.val << "\n";
1104 delete $1; delete $2; delete $3; $4.destroy(); $5.destroy(); delete $7;
1105 delete $9; $10.destroy(); $11.destroy(); delete $12; $13.destroy();
1106 $14.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001107 $$ = 0;
1108 }
Reid Spencer78720742006-12-02 20:21:22 +00001109 | Unwind {
Reid Spencere7c3c602006-11-30 06:36:44 +00001110 *O << " " << *$1 << "\n";
1111 delete $1;
1112 $$ = 0;
1113 }
1114 | UNREACHABLE {
1115 *O << " " << *$1 << "\n";
1116 delete $1;
1117 $$ = 0;
1118 };
1119
1120JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
Reid Spencerf459d392006-12-02 16:19:52 +00001121 *$1 += " " + *$2.newTy + " " + *$3 + ", " + *$5.newTy + " " + *$6.val;
1122 $2.destroy(); delete $3; $5.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001123 $$ = $1;
1124 }
1125 | IntType ConstValueRef ',' LABEL ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +00001126 $2->insert(0, *$1.newTy + " " );
Reid Spencerf459d392006-12-02 16:19:52 +00001127 *$2 += ", " + *$4.newTy + " " + *$5.val;
1128 $1.destroy(); $4.destroy(); $5.destroy();
Reid Spencere77e35e2006-12-01 20:26:20 +00001129 $$ = $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001130 };
1131
1132Inst
1133 : OptAssign InstVal {
Reid Spencera50d5962006-12-02 04:11:07 +00001134 if (!$1->empty())
1135 *$1 += " = ";
Reid Spencere7c3c602006-11-30 06:36:44 +00001136 *$1 += *$2;
1137 delete $2;
1138 $$ = $1;
1139 };
1140
1141PHIList
1142 : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencerf459d392006-12-02 16:19:52 +00001143 $3.val->insert(0, *$1.newTy + "[");
1144 *$3.val += "," + *$5.val + "]";
1145 $1.destroy(); $5.destroy();
1146 $$ = new std::string(*$3.val);
1147 $3.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001148 }
1149 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
Reid Spencerf459d392006-12-02 16:19:52 +00001150 *$1 += ", [" + *$4.val + "," + *$6.val + "]";
1151 $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001152 $$ = $1;
1153 };
1154
1155
1156ValueRefList
Reid Spencerf8483652006-12-02 15:16:01 +00001157 : ResolvedVal {
1158 $$ = new ValueList();
1159 $$->push_back($1);
1160 }
Reid Spencere7c3c602006-11-30 06:36:44 +00001161 | ValueRefList ',' ResolvedVal {
Reid Spencerf8483652006-12-02 15:16:01 +00001162 $1->push_back($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00001163 $$ = $1;
1164 };
1165
1166// ValueRefListE - Just like ValueRefList, except that it may also be empty!
1167ValueRefListE
Reid Spencerf8483652006-12-02 15:16:01 +00001168 : ValueRefList { $$ = $1; }
1169 | /*empty*/ { $$ = new ValueList(); }
Reid Spencere7c3c602006-11-30 06:36:44 +00001170 ;
1171
1172OptTailCall
1173 : TAIL CALL {
1174 *$1 += " " + *$2;
1175 delete $2;
1176 $$ = $1;
1177 }
1178 | CALL
1179 ;
1180
1181InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencer78720742006-12-02 20:21:22 +00001182 const char* op = getDivRemOpcode(*$1, $2);
1183 $$ = new std::string(op);
1184 *$$ += " " + *$2.newTy + " " + *$3.val + ", " + *$5.val;
1185 delete $1; $2.destroy(); $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001186 }
1187 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencerf459d392006-12-02 16:19:52 +00001188 *$1 += " " + *$2.newTy + " " + *$3.val + ", " + *$5.val;
1189 $2.destroy(); $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001190 $$ = $1;
1191 }
1192 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencer229e9362006-12-02 22:14:11 +00001193#if UPGRADE_SETCOND_OPS
1194 *$1 = getCompareOp(*$1, $2);
1195#endif
Reid Spencerf459d392006-12-02 16:19:52 +00001196 *$1 += " " + *$2.newTy + " " + *$3.val + ", " + *$5.val;
1197 $2.destroy(); $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001198 $$ = $1;
1199 }
Reid Spencer57f28f92006-12-03 07:10:26 +00001200 | ICMP IPredicates Types ValueRef ',' ValueRef ')' {
1201 *$1 += " " + *$2 + " " + *$4.val + "," + *$6.val + ")";
1202 delete $2; $4.destroy(); $6.destroy();
1203 $$ = $1;
1204 }
1205 | FCMP FPredicates Types ValueRef ',' ValueRef ')' {
Reid Spencer229e9362006-12-02 22:14:11 +00001206 *$1 += " " + *$2 + " " + *$4.val + "," + *$6.val + ")";
1207 delete $2; $4.destroy(); $6.destroy();
1208 $$ = $1;
1209 }
Reid Spencere7c3c602006-11-30 06:36:44 +00001210 | NOT ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001211 *$1 += " " + *$2.val;
1212 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001213 $$ = $1;
1214 }
1215 | ShiftOps ResolvedVal ',' ResolvedVal {
Reid Spencerf7bde222006-12-01 22:26:37 +00001216 const char* shiftop = $1->c_str();
1217 if (*$1 == "shr")
1218 shiftop = ($2.type.isUnsigned()) ? "lshr" : "ashr";
1219 $$ = new std::string(shiftop);
1220 *$$ += " " + *$2.val + ", " + *$4.val;
1221 delete $1; $2.destroy(); $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001222 }
Reid Spencerfcb5df82006-12-01 22:34:43 +00001223 | CastOps ResolvedVal TO Types {
Reid Spencer280d8012006-12-01 23:40:53 +00001224 std::string source = *$2.val;
Reid Spencera50d5962006-12-02 04:11:07 +00001225 TypeInfo SrcTy = $2.type;
1226 TypeInfo DstTy = $4;
1227 ResolveType(DstTy);
1228 $$ = new std::string();
Reid Spencer280d8012006-12-01 23:40:53 +00001229 if (*$1 == "cast") {
Reid Spencera50d5962006-12-02 04:11:07 +00001230 *$$ += getCastUpgrade(source, SrcTy, DstTy, false);
1231 } else {
1232 *$$ += *$1 + " " + source + " to " + *DstTy.newTy;
Reid Spencer280d8012006-12-01 23:40:53 +00001233 }
Reid Spencere77e35e2006-12-01 20:26:20 +00001234 delete $1; $2.destroy();
1235 delete $3; $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001236 }
1237 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001238 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1239 $2.destroy(); $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001240 $$ = $1;
1241 }
1242 | VAARG ResolvedVal ',' Types {
Reid Spencere77e35e2006-12-01 20:26:20 +00001243 *$1 += " " + *$2.val + ", " + *$4.newTy;
1244 $2.destroy(); $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001245 $$ = $1;
1246 }
1247 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001248 *$1 += " " + *$2.val + ", " + *$4.val;
1249 $2.destroy(); $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001250 $$ = $1;
1251 }
1252 | INSERTELEMENT 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 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001258 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1259 $2.destroy(); $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001260 $$ = $1;
1261 }
1262 | PHI_TOK PHIList {
1263 *$1 += " " + *$2;
1264 delete $2;
1265 $$ = $1;
1266 }
1267 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
1268 if (!$2->empty())
1269 *$1 += " " + *$2;
1270 if (!$1->empty())
1271 *$1 += " ";
Reid Spencerf459d392006-12-02 16:19:52 +00001272 *$1 += *$3.newTy + " " + *$4.val + "(";
Reid Spencerf8483652006-12-02 15:16:01 +00001273 for (unsigned i = 0; i < $6->size(); ++i) {
1274 ValueInfo& VI = (*$6)[i];
1275 *$1 += *VI.val;
1276 if (i+1 < $6->size())
1277 *$1 += ", ";
1278 VI.destroy();
1279 }
1280 *$1 += ")";
Reid Spencerf459d392006-12-02 16:19:52 +00001281 delete $2; $3.destroy(); $4.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001282 $$ = $1;
1283 }
1284 | MemoryInst ;
1285
1286
1287// IndexList - List of indices for GEP based instructions...
1288IndexList
Reid Spencerf8483652006-12-02 15:16:01 +00001289 : ',' ValueRefList { $$ = $2; }
1290 | /* empty */ { $$ = new ValueList(); }
Reid Spencere7c3c602006-11-30 06:36:44 +00001291 ;
1292
1293OptVolatile
1294 : VOLATILE
1295 | /* empty */ { $$ = new std::string(); }
1296 ;
1297
1298MemoryInst : MALLOC Types OptCAlign {
Reid Spencere77e35e2006-12-01 20:26:20 +00001299 *$1 += " " + *$2.newTy;
Reid Spencere7c3c602006-11-30 06:36:44 +00001300 if (!$3->empty())
1301 *$1 += " " + *$3;
Reid Spencere77e35e2006-12-01 20:26:20 +00001302 $2.destroy(); delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001303 $$ = $1;
1304 }
1305 | MALLOC Types ',' UINT ValueRef OptCAlign {
Reid Spencerf459d392006-12-02 16:19:52 +00001306 *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5.val;
Reid Spencere7c3c602006-11-30 06:36:44 +00001307 if (!$6->empty())
1308 *$1 += " " + *$6;
Reid Spencerf459d392006-12-02 16:19:52 +00001309 $2.destroy(); $4.destroy(); $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001310 $$ = $1;
1311 }
1312 | ALLOCA Types OptCAlign {
Reid Spencere77e35e2006-12-01 20:26:20 +00001313 *$1 += " " + *$2.newTy;
Reid Spencere7c3c602006-11-30 06:36:44 +00001314 if (!$3->empty())
1315 *$1 += " " + *$3;
Reid Spencere77e35e2006-12-01 20:26:20 +00001316 $2.destroy(); delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001317 $$ = $1;
1318 }
1319 | ALLOCA Types ',' UINT ValueRef OptCAlign {
Reid Spencerf459d392006-12-02 16:19:52 +00001320 *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5.val;
Reid Spencere7c3c602006-11-30 06:36:44 +00001321 if (!$6->empty())
1322 *$1 += " " + *$6;
Reid Spencerf459d392006-12-02 16:19:52 +00001323 $2.destroy(); $4.destroy(); $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001324 $$ = $1;
1325 }
1326 | FREE ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001327 *$1 += " " + *$2.val;
1328 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001329 $$ = $1;
1330 }
1331 | OptVolatile LOAD Types ValueRef {
1332 if (!$1->empty())
1333 *$1 += " ";
Reid Spencerf459d392006-12-02 16:19:52 +00001334 *$1 += *$2 + " " + *$3.newTy + " " + *$4.val;
1335 delete $2; $3.destroy(); $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001336 $$ = $1;
1337 }
1338 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
1339 if (!$1->empty())
1340 *$1 += " ";
Reid Spencerf459d392006-12-02 16:19:52 +00001341 *$1 += *$2 + " " + *$3.val + ", " + *$5.newTy + " " + *$6.val;
1342 delete $2; $3.destroy(); $5.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001343 $$ = $1;
1344 }
1345 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencerf459d392006-12-02 16:19:52 +00001346 // Upgrade the indices
1347 for (unsigned i = 0; i < $4->size(); ++i) {
1348 ValueInfo& VI = (*$4)[i];
1349 if (VI.type.isUnsigned() && !VI.isConstant() &&
1350 VI.type.getBitWidth() < 64) {
1351 std::string* old = VI.val;
1352 *O << " %gep_upgrade" << unique << " = zext " << *old
1353 << " to ulong\n";
1354 VI.val = new std::string("ulong %gep_upgrade" + llvm::utostr(unique++));
1355 VI.type.oldTy = ULongTy;
1356 delete old;
1357 }
1358 }
1359 *$1 += " " + *$2.newTy + " " + *$3.val;
Reid Spencerf8483652006-12-02 15:16:01 +00001360 for (unsigned i = 0; i < $4->size(); ++i) {
1361 ValueInfo& VI = (*$4)[i];
1362 *$1 += ", " + *VI.val;
1363 VI.destroy();
1364 }
Reid Spencerf459d392006-12-02 16:19:52 +00001365 $2.destroy(); $3.destroy(); delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00001366 $$ = $1;
1367 };
1368
1369%%
1370
1371int yyerror(const char *ErrorMsg) {
1372 std::string where
1373 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
1374 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
1375 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
1376 if (yychar == YYEMPTY || yychar == 0)
1377 errMsg += "end-of-file.";
1378 else
1379 errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
1380 std::cerr << errMsg << '\n';
1381 exit(1);
1382}