blob: 3458c3a0cce84298677c3ee7791e613f56b5b234 [file] [log] [blame]
Reid Spencer96839be2006-11-30 16:50:26 +00001//===-- UpgradeParser.y - Upgrade parser for llvm assmbly -------*- C++ -*-===//
Reid Spencere7c3c602006-11-30 06:36:44 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Reid Spencer and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Reid Spencer96839be2006-11-30 16:50:26 +000010// This file implements the bison parser for LLVM 1.9 assembly language.
Reid Spencere7c3c602006-11-30 06:36:44 +000011//
12//===----------------------------------------------------------------------===//
13
14%{
Reid Spencere7c3c602006-11-30 06:36:44 +000015#include "ParserInternals.h"
16#include <llvm/ADT/StringExtras.h>
Reid Spencere7c3c602006-11-30 06:36:44 +000017#include <algorithm>
Reid Spencera50d5962006-12-02 04:11:07 +000018#include <map>
Reid Spencere7c3c602006-11-30 06:36:44 +000019#include <utility>
20#include <iostream>
Reid Spencerbec0b592006-12-03 03:16:48 +000021#include <cassert>
Reid Spencere7c3c602006-11-30 06:36:44 +000022
Reid Spencere77e35e2006-12-01 20:26:20 +000023#define YYERROR_VERBOSE 1
Reid Spencer96839be2006-11-30 16:50:26 +000024#define YYINCLUDED_STDLIB_H
Reid Spencere77e35e2006-12-01 20:26:20 +000025#define YYDEBUG 1
Reid Spencere7c3c602006-11-30 06:36:44 +000026
27int yylex(); // declaration" of xxx warnings.
28int yyparse();
Reid Spencere77e35e2006-12-01 20:26:20 +000029extern int yydebug;
Reid Spencere7c3c602006-11-30 06:36:44 +000030
31static std::string CurFilename;
Reid Spencere7c3c602006-11-30 06:36:44 +000032static std::ostream *O = 0;
Reid Spencer96839be2006-11-30 16:50:26 +000033std::istream* LexInput = 0;
Reid Spencere77e35e2006-12-01 20:26:20 +000034unsigned SizeOfPointer = 32;
Reid Spencerf459d392006-12-02 16:19:52 +000035static uint64_t unique = 1;
Reid Spencer96839be2006-11-30 16:50:26 +000036
Reid Spencera50d5962006-12-02 04:11:07 +000037typedef std::vector<TypeInfo> TypeVector;
38static TypeVector EnumeratedTypes;
39typedef std::map<std::string,TypeInfo> TypeMap;
40static TypeMap NamedTypes;
Reid Spencerf12ee422006-12-05 19:21:25 +000041static TypeMap Globals;
Reid Spencera50d5962006-12-02 04:11:07 +000042
Reid Spencerf8483652006-12-02 15:16:01 +000043void destroy(ValueList* VL) {
44 while (!VL->empty()) {
45 ValueInfo& VI = VL->back();
46 VI.destroy();
47 VL->pop_back();
48 }
49 delete VL;
50}
51
Reid Spencer96839be2006-11-30 16:50:26 +000052void UpgradeAssembly(const std::string &infile, std::istream& in,
Reid Spencere77e35e2006-12-01 20:26:20 +000053 std::ostream &out, bool debug)
Reid Spencere7c3c602006-11-30 06:36:44 +000054{
55 Upgradelineno = 1;
56 CurFilename = infile;
Reid Spencer96839be2006-11-30 16:50:26 +000057 LexInput = &in;
Reid Spencere77e35e2006-12-01 20:26:20 +000058 yydebug = debug;
Reid Spencere7c3c602006-11-30 06:36:44 +000059 O = &out;
60
61 if (yyparse()) {
62 std::cerr << "Parse failed.\n";
63 exit(1);
64 }
65}
66
Reid Spencera50d5962006-12-02 04:11:07 +000067static void ResolveType(TypeInfo& Ty) {
68 if (Ty.oldTy == UnresolvedTy) {
69 TypeMap::iterator I = NamedTypes.find(*Ty.newTy);
Reid Spencer78720742006-12-02 20:21:22 +000070 if (I != NamedTypes.end()) {
Reid Spencera50d5962006-12-02 04:11:07 +000071 Ty.oldTy = I->second.oldTy;
Reid Spencer78720742006-12-02 20:21:22 +000072 Ty.elemTy = I->second.elemTy;
73 } else {
Reid Spencera50d5962006-12-02 04:11:07 +000074 std::string msg("Can't resolve type: ");
75 msg += *Ty.newTy;
76 yyerror(msg.c_str());
Reid Spencer280d8012006-12-01 23:40:53 +000077 }
Reid Spencera50d5962006-12-02 04:11:07 +000078 } else if (Ty.oldTy == NumericTy) {
79 unsigned ref = atoi(&((Ty.newTy->c_str())[1])); // Skip the '\\'
80 if (ref < EnumeratedTypes.size()) {
81 Ty.oldTy = EnumeratedTypes[ref].oldTy;
Reid Spencer78720742006-12-02 20:21:22 +000082 Ty.elemTy = EnumeratedTypes[ref].elemTy;
Reid Spencera50d5962006-12-02 04:11:07 +000083 } else {
84 std::string msg("Can't resolve type: ");
85 msg += *Ty.newTy;
86 yyerror(msg.c_str());
87 }
Reid Spencer280d8012006-12-01 23:40:53 +000088 }
Reid Spencera50d5962006-12-02 04:11:07 +000089 // otherwise its already resolved.
Reid Spencer280d8012006-12-01 23:40:53 +000090}
91
Reid Spencera50d5962006-12-02 04:11:07 +000092static const char* getCastOpcode(
93 std::string& Source, const TypeInfo& SrcTy, const TypeInfo& DstTy)
94{
Reid Spencere77e35e2006-12-01 20:26:20 +000095 unsigned SrcBits = SrcTy.getBitWidth();
96 unsigned DstBits = DstTy.getBitWidth();
97 const char* opcode = "bitcast";
98 // Run through the possibilities ...
99 if (DstTy.isIntegral()) { // Casting to integral
100 if (SrcTy.isIntegral()) { // Casting from integral
101 if (DstBits < SrcBits)
102 opcode = "trunc";
103 else if (DstBits > SrcBits) { // its an extension
104 if (SrcTy.isSigned())
105 opcode ="sext"; // signed -> SEXT
106 else
107 opcode = "zext"; // unsigned -> ZEXT
108 } else {
109 opcode = "bitcast"; // Same size, No-op cast
110 }
111 } else if (SrcTy.isFloatingPoint()) { // Casting from floating pt
112 if (DstTy.isSigned())
113 opcode = "fptosi"; // FP -> sint
114 else
115 opcode = "fptoui"; // FP -> uint
116 } else if (SrcTy.isPacked()) {
117 assert(DstBits == SrcTy.getBitWidth() &&
118 "Casting packed to integer of different width");
119 opcode = "bitcast"; // same size, no-op cast
120 } else {
121 assert(SrcTy.isPointer() &&
122 "Casting from a value that is not first-class type");
123 opcode = "ptrtoint"; // ptr -> int
124 }
125 } else if (DstTy.isFloatingPoint()) { // Casting to floating pt
126 if (SrcTy.isIntegral()) { // Casting from integral
127 if (SrcTy.isSigned())
128 opcode = "sitofp"; // sint -> FP
129 else
130 opcode = "uitofp"; // uint -> FP
131 } else if (SrcTy.isFloatingPoint()) { // Casting from floating pt
132 if (DstBits < SrcBits) {
133 opcode = "fptrunc"; // FP -> smaller FP
134 } else if (DstBits > SrcBits) {
135 opcode = "fpext"; // FP -> larger FP
136 } else {
137 opcode ="bitcast"; // same size, no-op cast
138 }
139 } else if (SrcTy.isPacked()) {
140 assert(DstBits == SrcTy.getBitWidth() &&
141 "Casting packed to floating point of different width");
142 opcode = "bitcast"; // same size, no-op cast
143 } else {
144 assert(0 && "Casting pointer or non-first class to float");
145 }
146 } else if (DstTy.isPacked()) {
147 if (SrcTy.isPacked()) {
148 assert(DstTy.getBitWidth() == SrcTy.getBitWidth() &&
149 "Casting packed to packed of different widths");
150 opcode = "bitcast"; // packed -> packed
151 } else if (DstTy.getBitWidth() == SrcBits) {
152 opcode = "bitcast"; // float/int -> packed
153 } else {
154 assert(!"Illegal cast to packed (wrong type or size)");
155 }
156 } else if (DstTy.isPointer()) {
157 if (SrcTy.isPointer()) {
158 opcode = "bitcast"; // ptr -> ptr
159 } else if (SrcTy.isIntegral()) {
160 opcode = "inttoptr"; // int -> ptr
161 } else {
Reid Spencera50d5962006-12-02 04:11:07 +0000162 assert(!"Casting invalid type to pointer");
Reid Spencere77e35e2006-12-01 20:26:20 +0000163 }
164 } else {
165 assert(!"Casting to type that is not first-class");
166 }
167 return opcode;
168}
169
Reid Spencera50d5962006-12-02 04:11:07 +0000170static std::string getCastUpgrade(
171 const std::string& Src, TypeInfo& SrcTy, TypeInfo& DstTy, bool isConst)
172{
173 std::string Result;
174 std::string Source = Src;
175 if (SrcTy.isFloatingPoint() && DstTy.isPointer()) {
176 // fp -> ptr cast is no longer supported but we must upgrade this
177 // by doing a double cast: fp -> int -> ptr
178 if (isConst)
179 Source = "ulong fptoui(" + Source + " to ulong)";
180 else {
Reid Spencerf459d392006-12-02 16:19:52 +0000181 *O << " %cast_upgrade" << unique << " = fptoui " << Source
182 << " to ulong\n";
183 Source = "ulong %cast_upgrade" + llvm::utostr(unique);
Reid Spencera50d5962006-12-02 04:11:07 +0000184 }
185 // Update the SrcTy for the getCastOpcode call below
186 SrcTy.destroy();
187 SrcTy.newTy = new std::string("ulong");
188 SrcTy.oldTy = ULongTy;
Reid Spencer5fe27e72006-12-09 19:41:25 +0000189 } else if (DstTy.oldTy == BoolTy && SrcTy.oldTy != BoolTy) {
Reid Spencera50d5962006-12-02 04:11:07 +0000190 // cast ptr %x to bool was previously defined as setne ptr %x, null
191 // The ptrtoint semantic is to truncate, not compare so we must retain
192 // the original intent by replace the cast with a setne
193 const char* comparator = SrcTy.isPointer() ? ", null" :
194 (SrcTy.isFloatingPoint() ? ", 0.0" : ", 0");
Reid Spencere4d87aa2006-12-23 06:05:41 +0000195 const char* compareOp = SrcTy.isFloatingPoint() ? "fcmp one " : "icmp ne ";
Reid Spencer187ccf82006-12-09 16:57:22 +0000196 if (isConst) {
197 Result = "(" + Source + comparator + ")";
198 Result = compareOp + Result;
199 } else
200 Result = compareOp + Source + comparator;
Reid Spencera50d5962006-12-02 04:11:07 +0000201 return Result; // skip cast processing below
202 }
203 ResolveType(SrcTy);
204 ResolveType(DstTy);
205 std::string Opcode(getCastOpcode(Source, SrcTy, DstTy));
206 if (isConst)
207 Result += Opcode + "( " + Source + " to " + *DstTy.newTy + ")";
208 else
209 Result += Opcode + " " + Source + " to " + *DstTy.newTy;
210 return Result;
211}
212
Reid Spencer78720742006-12-02 20:21:22 +0000213const char* getDivRemOpcode(const std::string& opcode, const TypeInfo& TI) {
214 const char* op = opcode.c_str();
215 TypeInfo Ty = TI;
216 ResolveType(Ty);
217 if (Ty.isPacked())
218 Ty.oldTy = Ty.getElementType();
219 if (opcode == "div")
220 if (Ty.isFloatingPoint())
221 op = "fdiv";
222 else if (Ty.isUnsigned())
223 op = "udiv";
224 else if (Ty.isSigned())
225 op = "sdiv";
226 else
227 yyerror("Invalid type for div instruction");
228 else if (opcode == "rem")
229 if (Ty.isFloatingPoint())
230 op = "frem";
231 else if (Ty.isUnsigned())
232 op = "urem";
233 else if (Ty.isSigned())
234 op = "srem";
235 else
236 yyerror("Invalid type for rem instruction");
237 return op;
238}
Reid Spencer229e9362006-12-02 22:14:11 +0000239
240std::string
241getCompareOp(const std::string& setcc, const TypeInfo& TI) {
242 assert(setcc.length() == 5);
243 char cc1 = setcc[3];
244 char cc2 = setcc[4];
245 assert(cc1 == 'e' || cc1 == 'n' || cc1 == 'l' || cc1 == 'g');
246 assert(cc2 == 'q' || cc2 == 'e' || cc2 == 'e' || cc2 == 't');
247 std::string result("xcmp xxx");
248 result[6] = cc1;
249 result[7] = cc2;
250 if (TI.isFloatingPoint()) {
251 result[0] = 'f';
Reid Spencere4d87aa2006-12-23 06:05:41 +0000252 result[5] = 'o';
Reid Spencerf0cf1322006-12-07 04:23:03 +0000253 if (cc1 == 'n')
254 result[5] = 'u'; // NE maps to unordered
255 else
256 result[5] = 'o'; // everything else maps to ordered
Reid Spencer229e9362006-12-02 22:14:11 +0000257 } else if (TI.isIntegral() || TI.isPointer()) {
258 result[0] = 'i';
259 if ((cc1 == 'e' && cc2 == 'q') || (cc1 == 'n' && cc2 == 'e'))
260 result.erase(5,1);
261 else if (TI.isSigned())
262 result[5] = 's';
Reid Spencer3e5ab8c2006-12-06 06:25:46 +0000263 else if (TI.isUnsigned() || TI.isPointer() || TI.isBool())
Reid Spencer229e9362006-12-02 22:14:11 +0000264 result[5] = 'u';
265 else
266 yyerror("Invalid integral type for setcc");
267 }
268 return result;
269}
270
Reid Spencere7c3c602006-11-30 06:36:44 +0000271%}
272
Reid Spencerf0cf1322006-12-07 04:23:03 +0000273// %file-prefix="UpgradeParser"
Reid Spencere77e35e2006-12-01 20:26:20 +0000274
275%union {
276 std::string* String;
277 TypeInfo Type;
278 ValueInfo Value;
279 ConstInfo Const;
Reid Spencerf8483652006-12-02 15:16:01 +0000280 ValueList* ValList;
Reid Spencere77e35e2006-12-01 20:26:20 +0000281}
282
Reid Spencerf2d55322006-12-01 21:52:30 +0000283%token <Type> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
Reid Spencera50d5962006-12-02 04:11:07 +0000284%token <Type> FLOAT DOUBLE LABEL
285%token <String> OPAQUE ESINT64VAL EUINT64VAL SINTVAL UINTVAL FPVAL
Reid Spencerf2d55322006-12-01 21:52:30 +0000286%token <String> NULL_TOK UNDEF ZEROINITIALIZER TRUETOK FALSETOK
Reid Spencere77e35e2006-12-01 20:26:20 +0000287%token <String> TYPE VAR_ID LABELSTR STRINGCONSTANT
288%token <String> IMPLEMENTATION BEGINTOK ENDTOK
289%token <String> DECLARE GLOBAL CONSTANT SECTION VOLATILE
290%token <String> TO DOTDOTDOT CONST INTERNAL LINKONCE WEAK
291%token <String> DLLIMPORT DLLEXPORT EXTERN_WEAK APPENDING
292%token <String> NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG
Reid Spencer78720742006-12-02 20:21:22 +0000293%token <String> ALIGN UNINITIALIZED
Reid Spencere77e35e2006-12-01 20:26:20 +0000294%token <String> DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
295%token <String> CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
296%token <String> X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
297%token <String> DATALAYOUT
Reid Spencer78720742006-12-02 20:21:22 +0000298%token <String> RET BR SWITCH INVOKE EXCEPT UNWIND UNREACHABLE
299%token <String> ADD SUB MUL DIV UDIV SDIV FDIV REM UREM SREM FREM AND OR XOR
Reid Spencere77e35e2006-12-01 20:26:20 +0000300%token <String> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comparators
Reid Spencer229e9362006-12-02 22:14:11 +0000301%token <String> ICMP FCMP EQ NE SLT SGT SLE SGE OEQ ONE OLT OGT OLE OGE
302%token <String> ORD UNO UEQ UNE ULT UGT ULE UGE
Reid Spencere77e35e2006-12-01 20:26:20 +0000303%token <String> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Reid Spencerf7bde222006-12-01 22:26:37 +0000304%token <String> PHI_TOK SELECT SHL SHR ASHR LSHR VAARG
Reid Spencere77e35e2006-12-01 20:26:20 +0000305%token <String> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Reid Spencerfcb5df82006-12-01 22:34:43 +0000306%token <String> CAST TRUNC ZEXT SEXT FPTRUNC FPEXT FPTOUI FPTOSI UITOFP SITOFP
307%token <String> PTRTOINT INTTOPTR BITCAST
Reid Spencere77e35e2006-12-01 20:26:20 +0000308
309%type <String> OptAssign OptLinkage OptCallingConv OptAlign OptCAlign
310%type <String> SectionString OptSection GlobalVarAttributes GlobalVarAttribute
311%type <String> ArgTypeListI ConstExpr DefinitionList
312%type <String> ConstPool TargetDefinition LibrariesDefinition LibList OptName
313%type <String> ArgVal ArgListH ArgList FunctionHeaderH BEGIN FunctionHeader END
314%type <String> Function FunctionProto BasicBlock TypeListI
315%type <String> InstructionList BBTerminatorInst JumpTable Inst PHIList
Reid Spencer78720742006-12-02 20:21:22 +0000316%type <String> OptTailCall InstVal OptVolatile Unwind
Reid Spencere77e35e2006-12-01 20:26:20 +0000317%type <String> MemoryInst SymbolicValueRef OptSideEffect GlobalType
318%type <String> FnDeclareLinkage BasicBlockList BigOrLittle AsmBlock
Reid Spencer78720742006-12-02 20:21:22 +0000319%type <String> Name ConstValueRef ConstVector External
Reid Spencer57f28f92006-12-03 07:10:26 +0000320%type <String> ShiftOps SetCondOps LogicalOps ArithmeticOps CastOps
321%type <String> IPredicates FPredicates
Reid Spencere77e35e2006-12-01 20:26:20 +0000322
Reid Spencerf8483652006-12-02 15:16:01 +0000323%type <ValList> ValueRefList ValueRefListE IndexList
Reid Spencere77e35e2006-12-01 20:26:20 +0000324
325%type <Type> IntType SIntType UIntType FPType TypesV Types
326%type <Type> PrimType UpRTypesV UpRTypes
327
Reid Spencerf2d55322006-12-01 21:52:30 +0000328%type <String> IntVal EInt64Val
329%type <Const> ConstVal
Reid Spencere77e35e2006-12-01 20:26:20 +0000330
Reid Spencerf459d392006-12-02 16:19:52 +0000331%type <Value> ValueRef ResolvedVal
Reid Spencere7c3c602006-11-30 06:36:44 +0000332
333%start Module
334
335%%
336
337// Handle constant integer size restriction and conversion...
Reid Spencerf2d55322006-12-01 21:52:30 +0000338IntVal : SINTVAL | UINTVAL ;
Reid Spencere77e35e2006-12-01 20:26:20 +0000339EInt64Val : ESINT64VAL | EUINT64VAL;
Reid Spencere7c3c602006-11-30 06:36:44 +0000340
341// Operations that are notably excluded from this list include:
342// RET, BR, & SWITCH because they end basic blocks and are treated specially.
Reid Spencer78720742006-12-02 20:21:22 +0000343ArithmeticOps: ADD | SUB | MUL | DIV | UDIV | SDIV | FDIV
344 | REM | UREM | SREM | FREM;
Reid Spencere7c3c602006-11-30 06:36:44 +0000345LogicalOps : AND | OR | XOR;
346SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
Reid Spencer57f28f92006-12-03 07:10:26 +0000347IPredicates : EQ | NE | SLT | SGT | SLE | SGE | ULT | UGT | ULE | UGE;
348FPredicates : OEQ | ONE | OLT | OGT | OLE | OGE | ORD | UNO | UEQ | UNE
349 | ULT | UGT | ULE | UGE | TRUETOK | FALSETOK;
Reid Spencerf7bde222006-12-01 22:26:37 +0000350ShiftOps : SHL | SHR | ASHR | LSHR;
Reid Spencerfcb5df82006-12-01 22:34:43 +0000351CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | FPTOUI | FPTOSI |
352 UITOFP | SITOFP | PTRTOINT | INTTOPTR | BITCAST | CAST
353 ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000354
355// These are some types that allow classification if we only want a particular
356// thing... for example, only a signed, unsigned, or integral type.
357SIntType : LONG | INT | SHORT | SBYTE;
358UIntType : ULONG | UINT | USHORT | UBYTE;
359IntType : SIntType | UIntType;
360FPType : FLOAT | DOUBLE;
361
362// OptAssign - Value producing statements have an optional assignment component
363OptAssign : Name '=' {
Reid Spencere7c3c602006-11-30 06:36:44 +0000364 $$ = $1;
365 }
366 | /*empty*/ {
367 $$ = new std::string("");
368 };
369
370OptLinkage
371 : INTERNAL | LINKONCE | WEAK | APPENDING | DLLIMPORT | DLLEXPORT
372 | EXTERN_WEAK
373 | /*empty*/ { $$ = new std::string(""); } ;
374
375OptCallingConv
376 : CCC_TOK | CSRETCC_TOK | FASTCC_TOK | COLDCC_TOK | X86_STDCALLCC_TOK
Reid Spencer16244f42006-12-01 21:10:07 +0000377 | X86_FASTCALLCC_TOK
378 | CC_TOK EUINT64VAL {
Reid Spencerf2d55322006-12-01 21:52:30 +0000379 *$1 += *$2;
380 delete $2;
Reid Spencer16244f42006-12-01 21:10:07 +0000381 $$ = $1;
382 }
Reid Spencere7c3c602006-11-30 06:36:44 +0000383 | /*empty*/ { $$ = new std::string(""); } ;
384
385// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
386// a comma before it.
387OptAlign
388 : /*empty*/ { $$ = new std::string(); }
Reid Spencerf2d55322006-12-01 21:52:30 +0000389 | ALIGN EUINT64VAL { *$1 += " " + *$2; delete $2; $$ = $1; };
Reid Spencerf0cf1322006-12-07 04:23:03 +0000390
Reid Spencere7c3c602006-11-30 06:36:44 +0000391OptCAlign
392 : /*empty*/ { $$ = new std::string(); }
393 | ',' ALIGN EUINT64VAL {
394 $2->insert(0, ", ");
Reid Spencerf2d55322006-12-01 21:52:30 +0000395 *$2 += " " + *$3;
396 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000397 $$ = $2;
398 };
399
400SectionString
401 : SECTION STRINGCONSTANT {
402 *$1 += " " + *$2;
403 delete $2;
404 $$ = $1;
405 };
406
407OptSection : /*empty*/ { $$ = new std::string(); }
408 | SectionString;
409
410GlobalVarAttributes
411 : /* empty */ { $$ = new std::string(); }
412 | ',' GlobalVarAttribute GlobalVarAttributes {
413 $2->insert(0, ", ");
414 if (!$3->empty())
415 *$2 += " " + *$3;
416 delete $3;
417 $$ = $2;
418 };
419
420GlobalVarAttribute
421 : SectionString
422 | ALIGN EUINT64VAL {
Reid Spencerf2d55322006-12-01 21:52:30 +0000423 *$1 += " " + *$2;
424 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000425 $$ = $1;
426 };
427
428//===----------------------------------------------------------------------===//
429// Types includes all predefined types... except void, because it can only be
430// used in specific contexts (function returning void for example). To have
431// access to it, a user must explicitly use TypesV.
432//
433
434// TypesV includes all of 'Types', but it also includes the void type.
435TypesV : Types | VOID ;
436UpRTypesV : UpRTypes | VOID ;
437Types : UpRTypes ;
438
439// Derived types are added later...
440//
441PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
Reid Spencere77e35e2006-12-01 20:26:20 +0000442PrimType : LONG | ULONG | FLOAT | DOUBLE | LABEL;
Reid Spencera50d5962006-12-02 04:11:07 +0000443UpRTypes
444 : OPAQUE {
445 $$.newTy = $1;
446 $$.oldTy = OpaqueTy;
447 }
448 | SymbolicValueRef {
449 $$.newTy = $1;
450 $$.oldTy = UnresolvedTy;
451 }
Reid Spencer78720742006-12-02 20:21:22 +0000452 | PrimType {
453 $$ = $1;
454 }
455 | '\\' EUINT64VAL { // Type UpReference
Reid Spencerf2d55322006-12-01 21:52:30 +0000456 $2->insert(0, "\\");
457 $$.newTy = $2;
Reid Spencera50d5962006-12-02 04:11:07 +0000458 $$.oldTy = NumericTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000459 }
460 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Reid Spencere77e35e2006-12-01 20:26:20 +0000461 *$1.newTy += "( " + *$3 + " )";
Reid Spencere7c3c602006-11-30 06:36:44 +0000462 delete $3;
Reid Spencere77e35e2006-12-01 20:26:20 +0000463 $$.newTy = $1.newTy;
464 $$.oldTy = FunctionTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000465 }
466 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Reid Spencerf2d55322006-12-01 21:52:30 +0000467 $2->insert(0,"[ ");
468 *$2 += " x " + *$4.newTy + " ]";
Reid Spencere77e35e2006-12-01 20:26:20 +0000469 delete $4.newTy;
Reid Spencerf2d55322006-12-01 21:52:30 +0000470 $$.newTy = $2;
Reid Spencere77e35e2006-12-01 20:26:20 +0000471 $$.oldTy = ArrayTy;
Reid Spencer78720742006-12-02 20:21:22 +0000472 $$.elemTy = $4.oldTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000473 }
474 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
Reid Spencerf2d55322006-12-01 21:52:30 +0000475 $2->insert(0,"< ");
476 *$2 += " x " + *$4.newTy + " >";
Reid Spencere77e35e2006-12-01 20:26:20 +0000477 delete $4.newTy;
Reid Spencerf2d55322006-12-01 21:52:30 +0000478 $$.newTy = $2;
Reid Spencere77e35e2006-12-01 20:26:20 +0000479 $$.oldTy = PackedTy;
Reid Spencer78720742006-12-02 20:21:22 +0000480 $$.elemTy = $4.oldTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000481 }
482 | '{' TypeListI '}' { // Structure type?
483 $2->insert(0, "{ ");
484 *$2 += " }";
Reid Spencere77e35e2006-12-01 20:26:20 +0000485 $$.newTy = $2;
486 $$.oldTy = StructTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000487 }
488 | '{' '}' { // Empty structure type?
Reid Spencer0b7e5072006-12-01 22:42:01 +0000489 $$.newTy = new std::string("{}");
Reid Spencere77e35e2006-12-01 20:26:20 +0000490 $$.oldTy = StructTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000491 }
492 | UpRTypes '*' { // Pointer type?
Reid Spencere77e35e2006-12-01 20:26:20 +0000493 *$1.newTy += '*';
Reid Spencer78720742006-12-02 20:21:22 +0000494 $$.elemTy = $1.oldTy;
Reid Spencere77e35e2006-12-01 20:26:20 +0000495 $1.oldTy = PointerTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000496 $$ = $1;
497 };
498
499// TypeList - Used for struct declarations and as a basis for function type
500// declaration type lists
501//
Reid Spencere77e35e2006-12-01 20:26:20 +0000502TypeListI
503 : UpRTypes {
504 $$ = $1.newTy;
505 }
506 | TypeListI ',' UpRTypes {
507 *$1 += ", " + *$3.newTy;
508 delete $3.newTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000509 $$ = $1;
510 };
511
512// ArgTypeList - List of types for a function type declaration...
Reid Spencere77e35e2006-12-01 20:26:20 +0000513ArgTypeListI
514 : TypeListI
Reid Spencere7c3c602006-11-30 06:36:44 +0000515 | TypeListI ',' DOTDOTDOT {
516 *$1 += ", ...";
517 delete $3;
518 $$ = $1;
519 }
520 | DOTDOTDOT {
521 $$ = $1;
522 }
523 | /*empty*/ {
524 $$ = new std::string();
525 };
526
527// ConstVal - The various declarations that go into the constant pool. This
528// production is used ONLY to represent constants that show up AFTER a 'const',
529// 'constant' or 'global' token at global scope. Constants that can be inlined
530// into other expressions (such as integers and constexprs) are handled by the
531// ResolvedVal, ValueRef and ConstValueRef productions.
532//
533ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencere77e35e2006-12-01 20:26:20 +0000534 $$.type = $1;
535 $$.cnst = new std::string(*$1.newTy);
536 *$$.cnst += " [ " + *$3 + " ]";
Reid Spencere7c3c602006-11-30 06:36:44 +0000537 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000538 }
539 | Types '[' ']' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000540 $$.type = $1;
541 $$.cnst = new std::string(*$1.newTy);
542 *$$.cnst += "[ ]";
Reid Spencere7c3c602006-11-30 06:36:44 +0000543 }
544 | Types 'c' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +0000545 $$.type = $1;
546 $$.cnst = new std::string(*$1.newTy);
547 *$$.cnst += " c" + *$3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000548 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000549 }
550 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencere77e35e2006-12-01 20:26:20 +0000551 $$.type = $1;
552 $$.cnst = new std::string(*$1.newTy);
553 *$$.cnst += " < " + *$3 + " >";
Reid Spencere7c3c602006-11-30 06:36:44 +0000554 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000555 }
556 | Types '{' ConstVector '}' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000557 $$.type = $1;
558 $$.cnst = new std::string(*$1.newTy);
559 *$$.cnst += " { " + *$3 + " }";
Reid Spencere7c3c602006-11-30 06:36:44 +0000560 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000561 }
562 | Types '{' '}' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000563 $$.type = $1;
564 $$.cnst = new std::string(*$1.newTy);
Reid Spencer0b7e5072006-12-01 22:42:01 +0000565 *$$.cnst += " {}";
Reid Spencere7c3c602006-11-30 06:36:44 +0000566 }
567 | Types NULL_TOK {
Reid Spencere77e35e2006-12-01 20:26:20 +0000568 $$.type = $1;
569 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000570 *$$.cnst += " " + *$2;
571 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000572 }
573 | Types UNDEF {
Reid Spencere77e35e2006-12-01 20:26:20 +0000574 $$.type = $1;
575 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000576 *$$.cnst += " " + *$2;
577 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000578 }
579 | Types SymbolicValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000580 $$.type = $1;
581 $$.cnst = new std::string(*$1.newTy);
582 *$$.cnst += " " + *$2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000583 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000584 }
585 | Types ConstExpr {
Reid Spencere77e35e2006-12-01 20:26:20 +0000586 $$.type = $1;
587 $$.cnst = new std::string(*$1.newTy);
588 *$$.cnst += " " + *$2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000589 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000590 }
591 | Types ZEROINITIALIZER {
Reid Spencere77e35e2006-12-01 20:26:20 +0000592 $$.type = $1;
593 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000594 *$$.cnst += " " + *$2;
595 delete $2;
Reid Spencere77e35e2006-12-01 20:26:20 +0000596 }
597 | SIntType EInt64Val { // integral constants
598 $$.type = $1;
599 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000600 *$$.cnst += " " + *$2;
601 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000602 }
603 | UIntType EUINT64VAL { // integral constants
Reid Spencere77e35e2006-12-01 20:26:20 +0000604 $$.type = $1;
605 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000606 *$$.cnst += " " + *$2;
607 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000608 }
609 | BOOL TRUETOK { // Boolean constants
Reid Spencere77e35e2006-12-01 20:26:20 +0000610 $$.type = $1;
611 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000612 *$$.cnst += " " + *$2;
613 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000614 }
615 | BOOL FALSETOK { // Boolean constants
Reid Spencere77e35e2006-12-01 20:26:20 +0000616 $$.type = $1;
617 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000618 *$$.cnst += " " + *$2;
619 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000620 }
621 | FPType FPVAL { // Float & Double constants
Reid Spencere77e35e2006-12-01 20:26:20 +0000622 $$.type = $1;
623 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000624 *$$.cnst += " " + *$2;
625 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000626 };
627
628
Reid Spencerfcb5df82006-12-01 22:34:43 +0000629ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencer280d8012006-12-01 23:40:53 +0000630 std::string source = *$3.cnst;
Reid Spencera50d5962006-12-02 04:11:07 +0000631 TypeInfo DstTy = $5;
632 ResolveType(DstTy);
Reid Spencer280d8012006-12-01 23:40:53 +0000633 if (*$1 == "cast") {
Reid Spencera50d5962006-12-02 04:11:07 +0000634 // Call getCastUpgrade to upgrade the old cast
635 $$ = new std::string(getCastUpgrade(source, $3.type, $5, true));
636 } else {
637 // Nothing to upgrade, just create the cast constant expr
638 $$ = new std::string(*$1);
639 *$$ += "( " + source + " to " + *$5.newTy + ")";
Reid Spencer280d8012006-12-01 23:40:53 +0000640 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000641 delete $1; $3.destroy(); delete $4; $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000642 }
643 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencerf8483652006-12-02 15:16:01 +0000644 *$1 += "(" + *$3.cnst;
645 for (unsigned i = 0; i < $4->size(); ++i) {
646 ValueInfo& VI = (*$4)[i];
647 *$1 += ", " + *VI.val;
648 VI.destroy();
649 }
650 *$1 += ")";
Reid Spencere77e35e2006-12-01 20:26:20 +0000651 $$ = $1;
652 $3.destroy();
653 delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +0000654 }
655 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000656 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
657 $3.destroy(); $5.destroy(); $7.destroy();
658 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000659 }
660 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer78720742006-12-02 20:21:22 +0000661 const char* op = getDivRemOpcode(*$1, $3.type);
662 $$ = new std::string(op);
663 *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
664 delete $1; $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000665 }
666 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000667 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
668 $3.destroy(); $5.destroy();
669 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000670 }
671 | SetCondOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer229e9362006-12-02 22:14:11 +0000672 *$1 = getCompareOp(*$1, $3.type);
Reid Spencere77e35e2006-12-01 20:26:20 +0000673 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
674 $3.destroy(); $5.destroy();
675 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000676 }
Reid Spencer57f28f92006-12-03 07:10:26 +0000677 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
678 *$1 += "(" + *$2 + "," + *$4.cnst + "," + *$6.cnst + ")";
679 delete $2; $4.destroy(); $6.destroy();
680 $$ = $1;
681 }
682 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
Reid Spencer229e9362006-12-02 22:14:11 +0000683 *$1 += "(" + *$2 + "," + *$4.cnst + "," + *$6.cnst + ")";
684 delete $2; $4.destroy(); $6.destroy();
685 $$ = $1;
686 }
Reid Spencere7c3c602006-11-30 06:36:44 +0000687 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Reid Spencerf7bde222006-12-01 22:26:37 +0000688 const char* shiftop = $1->c_str();
689 if (*$1 == "shr")
690 shiftop = ($3.type.isUnsigned()) ? "lshr" : "ashr";
691 $$ = new std::string(shiftop);
692 *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
693 delete $1; $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000694 }
695 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000696 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
697 $3.destroy(); $5.destroy();
698 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000699 }
700 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000701 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
702 $3.destroy(); $5.destroy(); $7.destroy();
703 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000704 }
705 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000706 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
707 $3.destroy(); $5.destroy(); $7.destroy();
708 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000709 };
710
711
712// ConstVector - A list of comma separated constants.
Reid Spencere77e35e2006-12-01 20:26:20 +0000713
714ConstVector
715 : ConstVector ',' ConstVal {
716 *$1 += ", " + *$3.cnst;
717 $3.destroy();
718 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000719 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000720 | ConstVal { $$ = new std::string(*$1.cnst); $1.destroy(); }
721 ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000722
723
724// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Reid Spencere77e35e2006-12-01 20:26:20 +0000725GlobalType : GLOBAL | CONSTANT ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000726
727
728//===----------------------------------------------------------------------===//
729// Rules to match Modules
730//===----------------------------------------------------------------------===//
731
732// Module rule: Capture the result of parsing the whole file into a result
733// variable...
734//
735Module : DefinitionList {
736};
737
738// DefinitionList - Top level definitions
739//
740DefinitionList : DefinitionList Function {
741 $$ = 0;
742 }
743 | DefinitionList FunctionProto {
744 *O << *$2 << "\n";
745 delete $2;
746 $$ = 0;
747 }
748 | DefinitionList MODULE ASM_TOK AsmBlock {
749 *O << "module asm " << " " << *$4 << "\n";
Reid Spencerd154b572006-12-01 20:36:40 +0000750 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000751 }
752 | DefinitionList IMPLEMENTATION {
753 *O << "implementation\n";
Reid Spencerd154b572006-12-01 20:36:40 +0000754 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000755 }
Reid Spencera50d5962006-12-02 04:11:07 +0000756 | ConstPool { $$ = 0; }
Reid Spencere7c3c602006-11-30 06:36:44 +0000757
Reid Spencer78720742006-12-02 20:21:22 +0000758External : EXTERNAL | UNINITIALIZED { $$ = $1; *$$ = "external"; }
759
Reid Spencere7c3c602006-11-30 06:36:44 +0000760// ConstPool - Constants with optional names assigned to them.
761ConstPool : ConstPool OptAssign TYPE TypesV {
Reid Spencera50d5962006-12-02 04:11:07 +0000762 EnumeratedTypes.push_back($4);
763 if (!$2->empty()) {
764 NamedTypes[*$2].newTy = new std::string(*$4.newTy);
765 NamedTypes[*$2].oldTy = $4.oldTy;
Reid Spencer78720742006-12-02 20:21:22 +0000766 NamedTypes[*$2].elemTy = $4.elemTy;
Reid Spencera50d5962006-12-02 04:11:07 +0000767 *O << *$2 << " = ";
768 }
769 *O << "type " << *$4.newTy << "\n";
770 delete $2; delete $3; $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000771 $$ = 0;
772 }
773 | ConstPool FunctionProto { // Function prototypes can be in const pool
774 *O << *$2 << "\n";
775 delete $2;
776 $$ = 0;
777 }
778 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
779 *O << *$2 << " " << *$3 << " " << *$4 << "\n";
780 delete $2; delete $3; delete $4;
781 $$ = 0;
782 }
783 | ConstPool OptAssign OptLinkage GlobalType ConstVal GlobalVarAttributes {
Reid Spencerf12ee422006-12-05 19:21:25 +0000784 if (!$2->empty()) {
Reid Spencera50d5962006-12-02 04:11:07 +0000785 *O << *$2 << " = ";
Reid Spencerf12ee422006-12-05 19:21:25 +0000786 Globals[*$2] = $5.type.clone();
787 }
Reid Spencera50d5962006-12-02 04:11:07 +0000788 *O << *$3 << " " << *$4 << " " << *$5.cnst << " " << *$6 << "\n";
Reid Spencere77e35e2006-12-01 20:26:20 +0000789 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000790 $$ = 0;
791 }
Reid Spencer78720742006-12-02 20:21:22 +0000792 | ConstPool OptAssign External GlobalType Types GlobalVarAttributes {
Reid Spencerf12ee422006-12-05 19:21:25 +0000793 if (!$2->empty()) {
Reid Spencera50d5962006-12-02 04:11:07 +0000794 *O << *$2 << " = ";
Reid Spencerf12ee422006-12-05 19:21:25 +0000795 Globals[*$2] = $5.clone();
796 }
Reid Spencera50d5962006-12-02 04:11:07 +0000797 *O << *$3 << " " << *$4 << " " << *$5.newTy << " " << *$6 << "\n";
Reid Spencere77e35e2006-12-01 20:26:20 +0000798 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000799 $$ = 0;
800 }
801 | ConstPool OptAssign DLLIMPORT GlobalType Types GlobalVarAttributes {
Reid Spencerf12ee422006-12-05 19:21:25 +0000802 if (!$2->empty()) {
Reid Spencera50d5962006-12-02 04:11:07 +0000803 *O << *$2 << " = ";
Reid Spencerf12ee422006-12-05 19:21:25 +0000804 Globals[*$2] = $5.clone();
805 }
Reid Spencera50d5962006-12-02 04:11:07 +0000806 *O << *$3 << " " << *$4 << " " << *$5.newTy << " " << *$6 << "\n";
Reid Spencere77e35e2006-12-01 20:26:20 +0000807 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000808 $$ = 0;
809 }
810 | ConstPool OptAssign EXTERN_WEAK GlobalType Types GlobalVarAttributes {
Reid Spencerf12ee422006-12-05 19:21:25 +0000811 if (!$2->empty()) {
Reid Spencera50d5962006-12-02 04:11:07 +0000812 *O << *$2 << " = ";
Reid Spencerf12ee422006-12-05 19:21:25 +0000813 Globals[*$2] = $5.clone();
814 }
Reid Spencera50d5962006-12-02 04:11:07 +0000815 *O << *$3 << " " << *$4 << " " << *$5.newTy << " " << *$6 << "\n";
Reid Spencere77e35e2006-12-01 20:26:20 +0000816 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000817 $$ = 0;
818 }
819 | ConstPool TARGET TargetDefinition {
820 *O << *$2 << " " << *$3 << "\n";
821 delete $2; delete $3;
822 $$ = 0;
823 }
824 | ConstPool DEPLIBS '=' LibrariesDefinition {
825 *O << *$2 << " = " << *$4 << "\n";
826 delete $2; delete $4;
827 $$ = 0;
828 }
829 | /* empty: end of list */ {
830 $$ = 0;
831 };
832
833
834AsmBlock : STRINGCONSTANT ;
835
836BigOrLittle : BIG | LITTLE
837
838TargetDefinition
839 : ENDIAN '=' BigOrLittle {
Reid Spencere77e35e2006-12-01 20:26:20 +0000840 *$1 += " = " + *$3;
841 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000842 $$ = $1;
843 }
844 | POINTERSIZE '=' EUINT64VAL {
Reid Spencerf2d55322006-12-01 21:52:30 +0000845 *$1 += " = " + *$3;
846 if (*$3 == "64")
Reid Spencere77e35e2006-12-01 20:26:20 +0000847 SizeOfPointer = 64;
Reid Spencerf2d55322006-12-01 21:52:30 +0000848 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000849 $$ = $1;
850 }
851 | TRIPLE '=' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +0000852 *$1 += " = " + *$3;
853 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000854 $$ = $1;
855 }
856 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +0000857 *$1 += " = " + *$3;
858 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000859 $$ = $1;
860 };
861
862LibrariesDefinition
863 : '[' LibList ']' {
864 $2->insert(0, "[ ");
865 *$2 += " ]";
866 $$ = $2;
867 };
868
869LibList
870 : LibList ',' STRINGCONSTANT {
871 *$1 += ", " + *$3;
872 delete $3;
873 $$ = $1;
874 }
875 | STRINGCONSTANT
876 | /* empty: end of list */ {
877 $$ = new std::string();
878 };
879
880//===----------------------------------------------------------------------===//
881// Rules to match Function Headers
882//===----------------------------------------------------------------------===//
883
884Name : VAR_ID | STRINGCONSTANT;
885OptName : Name | /*empty*/ { $$ = new std::string(); };
886
887ArgVal : Types OptName {
Reid Spencere77e35e2006-12-01 20:26:20 +0000888 $$ = $1.newTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000889 if (!$2->empty())
890 *$$ += " " + *$2;
Reid Spencere77e35e2006-12-01 20:26:20 +0000891 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000892};
893
894ArgListH : ArgListH ',' ArgVal {
895 *$1 += ", " + *$3;
Reid Spencere77e35e2006-12-01 20:26:20 +0000896 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000897 }
898 | ArgVal {
899 $$ = $1;
900 };
901
902ArgList : ArgListH {
903 $$ = $1;
904 }
905 | ArgListH ',' DOTDOTDOT {
906 *$1 += ", ...";
907 $$ = $1;
Reid Spencere77e35e2006-12-01 20:26:20 +0000908 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000909 }
910 | DOTDOTDOT {
911 $$ = $1;
912 }
Reid Spencerd154b572006-12-01 20:36:40 +0000913 | /* empty */ { $$ = new std::string(); };
Reid Spencere7c3c602006-11-30 06:36:44 +0000914
915FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')'
916 OptSection OptAlign {
917 if (!$1->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000918 *$1 += " ";
Reid Spencere7c3c602006-11-30 06:36:44 +0000919 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000920 *$1 += *$2.newTy + " " + *$3 + "(" + *$5 + ")";
Reid Spencere7c3c602006-11-30 06:36:44 +0000921 if (!$7->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000922 *$1 += " " + *$7;
Reid Spencere7c3c602006-11-30 06:36:44 +0000923 }
924 if (!$8->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000925 *$1 += " " + *$8;
Reid Spencere7c3c602006-11-30 06:36:44 +0000926 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000927 $2.destroy();
928 delete $3;
929 delete $5;
930 delete $7;
931 delete $8;
932 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000933 };
934
Reid Spencer78720742006-12-02 20:21:22 +0000935BEGIN : BEGINTOK { $$ = new std::string("{"); delete $1; }
936 | '{' { $$ = new std::string ("{"); }
Reid Spencere7c3c602006-11-30 06:36:44 +0000937
938FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
939 if (!$1->empty()) {
940 *O << *$1 << " ";
941 }
942 *O << *$2 << " " << *$3 << "\n";
943 delete $1; delete $2; delete $3;
944 $$ = 0;
945};
946
Reid Spencer78720742006-12-02 20:21:22 +0000947END : ENDTOK { $$ = new std::string("}"); delete $1; }
Reid Spencere7c3c602006-11-30 06:36:44 +0000948 | '}' { $$ = new std::string("}"); };
949
950Function : FunctionHeader BasicBlockList END {
951 if ($2)
952 *O << *$2;
953 *O << '\n' << *$3 << "\n";
Reid Spencere77e35e2006-12-01 20:26:20 +0000954 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000955};
956
Reid Spencere77e35e2006-12-01 20:26:20 +0000957FnDeclareLinkage
958 : /*default*/ { $$ = new std::string(); }
Reid Spencere7c3c602006-11-30 06:36:44 +0000959 | DLLIMPORT
960 | EXTERN_WEAK
961 ;
962
963FunctionProto
964 : DECLARE FnDeclareLinkage FunctionHeaderH {
Reid Spencere77e35e2006-12-01 20:26:20 +0000965 if (!$2->empty())
966 *$1 += " " + *$2;
967 *$1 += " " + *$3;
968 delete $2;
969 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000970 $$ = $1;
971 };
972
973//===----------------------------------------------------------------------===//
974// Rules to match Basic Blocks
975//===----------------------------------------------------------------------===//
976
Reid Spencerd154b572006-12-01 20:36:40 +0000977OptSideEffect : /* empty */ { $$ = new std::string(); }
978 | SIDEEFFECT;
Reid Spencere7c3c602006-11-30 06:36:44 +0000979
Reid Spencere77e35e2006-12-01 20:26:20 +0000980ConstValueRef
Reid Spencerf2d55322006-12-01 21:52:30 +0000981 : ESINT64VAL | EUINT64VAL | FPVAL | TRUETOK | FALSETOK | NULL_TOK | UNDEF
982 | ZEROINITIALIZER
Reid Spencere7c3c602006-11-30 06:36:44 +0000983 | '<' ConstVector '>' {
984 $2->insert(0, "<");
985 *$2 += ">";
986 $$ = $2;
987 }
988 | ConstExpr
989 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
990 if (!$2->empty()) {
991 *$1 += " " + *$2;
992 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000993 *$1 += " " + *$3 + ", " + *$5;
994 delete $2; delete $3; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +0000995 $$ = $1;
996 };
997
Reid Spencerf2d55322006-12-01 21:52:30 +0000998SymbolicValueRef : IntVal | Name ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000999
1000// ValueRef - A reference to a definition... either constant or symbolic
Reid Spencerf459d392006-12-02 16:19:52 +00001001ValueRef
1002 : SymbolicValueRef {
1003 $$.val = $1;
1004 $$.constant = false;
1005 $$.type.newTy = 0;
1006 $$.type.oldTy = UnresolvedTy;
1007 }
1008 | ConstValueRef {
1009 $$.val = $1;
1010 $$.constant = true;
1011 $$.type.newTy = 0;
1012 $$.type.oldTy = UnresolvedTy;
1013 }
1014 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001015
1016// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1017// type immediately preceeds the value reference, and allows complex constant
1018// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
1019ResolvedVal : Types ValueRef {
Reid Spencerf459d392006-12-02 16:19:52 +00001020 $$ = $2;
Reid Spencere77e35e2006-12-01 20:26:20 +00001021 $$.type = $1;
Reid Spencerf459d392006-12-02 16:19:52 +00001022 $$.val->insert(0, *$1.newTy + " ");
Reid Spencere7c3c602006-11-30 06:36:44 +00001023 };
1024
1025BasicBlockList : BasicBlockList BasicBlock {
Reid Spencerf2d55322006-12-01 21:52:30 +00001026 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001027 }
1028 | BasicBlock { // Do not allow functions with 0 basic blocks
Reid Spencerf2d55322006-12-01 21:52:30 +00001029 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001030 };
1031
1032
1033// Basic blocks are terminated by branching instructions:
1034// br, br/cc, switch, ret
1035//
Reid Spencer16244f42006-12-01 21:10:07 +00001036BasicBlock : InstructionList BBTerminatorInst {
Reid Spencerf2d55322006-12-01 21:52:30 +00001037 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001038 };
1039
1040InstructionList : InstructionList Inst {
1041 *O << " " << *$2 << "\n";
1042 delete $2;
1043 $$ = 0;
1044 }
1045 | /* empty */ {
1046 $$ = 0;
1047 }
1048 | LABELSTR {
1049 *O << *$1 << "\n";
1050 delete $1;
1051 $$ = 0;
1052 };
1053
Reid Spencer78720742006-12-02 20:21:22 +00001054Unwind : UNWIND | EXCEPT { $$ = $1; *$$ = "unwind"; }
1055
Reid Spencere7c3c602006-11-30 06:36:44 +00001056BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencere77e35e2006-12-01 20:26:20 +00001057 *O << " " << *$1 << " " << *$2.val << "\n";
1058 delete $1; $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001059 $$ = 0;
1060 }
1061 | RET VOID { // Return with no result...
Reid Spencere77e35e2006-12-01 20:26:20 +00001062 *O << " " << *$1 << " " << *$2.newTy << "\n";
1063 delete $1; $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001064 $$ = 0;
1065 }
1066 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencerf459d392006-12-02 16:19:52 +00001067 *O << " " << *$1 << " " << *$2.newTy << " " << *$3.val << "\n";
1068 delete $1; $2.destroy(); $3.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001069 $$ = 0;
1070 } // Conditional Branch...
1071 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Reid Spencerf459d392006-12-02 16:19:52 +00001072 *O << " " << *$1 << " " << *$2.newTy << " " << *$3.val << ", "
1073 << *$5.newTy << " " << *$6.val << ", " << *$8.newTy << " "
1074 << *$9.val << "\n";
1075 delete $1; $2.destroy(); $3.destroy(); $5.destroy(); $6.destroy();
1076 $8.destroy(); $9.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001077 $$ = 0;
1078 }
1079 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencerf459d392006-12-02 16:19:52 +00001080 *O << " " << *$1 << " " << *$2.newTy << " " << *$3.val << ", "
1081 << *$5.newTy << " " << *$6.val << " [" << *$8 << " ]\n";
1082 delete $1; $2.destroy(); $3.destroy(); $5.destroy(); $6.destroy();
1083 delete $8;
Reid Spencere7c3c602006-11-30 06:36:44 +00001084 $$ = 0;
1085 }
1086 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencerf459d392006-12-02 16:19:52 +00001087 *O << " " << *$1 << " " << *$2.newTy << " " << *$3.val << ", "
1088 << *$5.newTy << " " << *$6.val << "[]\n";
1089 delete $1; $2.destroy(); $3.destroy(); $5.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001090 $$ = 0;
1091 }
Reid Spencer16244f42006-12-01 21:10:07 +00001092 | OptAssign INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
Reid Spencer78720742006-12-02 20:21:22 +00001093 TO LABEL ValueRef Unwind LABEL ValueRef {
Reid Spencer16244f42006-12-01 21:10:07 +00001094 *O << " ";
1095 if (!$1->empty())
Reid Spencera50d5962006-12-02 04:11:07 +00001096 *O << *$1 << " = ";
Reid Spencerf459d392006-12-02 16:19:52 +00001097 *O << *$2 << " " << *$3 << " " << *$4.newTy << " " << *$5.val << " (";
Reid Spencerf8483652006-12-02 15:16:01 +00001098 for (unsigned i = 0; i < $7->size(); ++i) {
1099 ValueInfo& VI = (*$7)[i];
1100 *O << *VI.val;
1101 if (i+1 < $7->size())
1102 *O << ", ";
1103 VI.destroy();
1104 }
Reid Spencerf459d392006-12-02 16:19:52 +00001105 *O << ") " << *$9 << " " << *$10.newTy << " " << *$11.val << " "
1106 << *$12 << " " << *$13.newTy << " " << *$14.val << "\n";
1107 delete $1; delete $2; delete $3; $4.destroy(); $5.destroy(); delete $7;
1108 delete $9; $10.destroy(); $11.destroy(); delete $12; $13.destroy();
1109 $14.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001110 $$ = 0;
1111 }
Reid Spencer78720742006-12-02 20:21:22 +00001112 | Unwind {
Reid Spencere7c3c602006-11-30 06:36:44 +00001113 *O << " " << *$1 << "\n";
1114 delete $1;
1115 $$ = 0;
1116 }
1117 | UNREACHABLE {
1118 *O << " " << *$1 << "\n";
1119 delete $1;
1120 $$ = 0;
1121 };
1122
1123JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
Reid Spencerf459d392006-12-02 16:19:52 +00001124 *$1 += " " + *$2.newTy + " " + *$3 + ", " + *$5.newTy + " " + *$6.val;
1125 $2.destroy(); delete $3; $5.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001126 $$ = $1;
1127 }
1128 | IntType ConstValueRef ',' LABEL ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +00001129 $2->insert(0, *$1.newTy + " " );
Reid Spencerf459d392006-12-02 16:19:52 +00001130 *$2 += ", " + *$4.newTy + " " + *$5.val;
1131 $1.destroy(); $4.destroy(); $5.destroy();
Reid Spencere77e35e2006-12-01 20:26:20 +00001132 $$ = $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001133 };
1134
1135Inst
1136 : OptAssign InstVal {
Reid Spencera50d5962006-12-02 04:11:07 +00001137 if (!$1->empty())
1138 *$1 += " = ";
Reid Spencere7c3c602006-11-30 06:36:44 +00001139 *$1 += *$2;
1140 delete $2;
1141 $$ = $1;
1142 };
1143
1144PHIList
1145 : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencerf459d392006-12-02 16:19:52 +00001146 $3.val->insert(0, *$1.newTy + "[");
1147 *$3.val += "," + *$5.val + "]";
1148 $1.destroy(); $5.destroy();
1149 $$ = new std::string(*$3.val);
1150 $3.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001151 }
1152 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
Reid Spencerf459d392006-12-02 16:19:52 +00001153 *$1 += ", [" + *$4.val + "," + *$6.val + "]";
1154 $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001155 $$ = $1;
1156 };
1157
1158
1159ValueRefList
Reid Spencerf8483652006-12-02 15:16:01 +00001160 : ResolvedVal {
1161 $$ = new ValueList();
1162 $$->push_back($1);
1163 }
Reid Spencere7c3c602006-11-30 06:36:44 +00001164 | ValueRefList ',' ResolvedVal {
Reid Spencerf8483652006-12-02 15:16:01 +00001165 $1->push_back($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00001166 $$ = $1;
1167 };
1168
1169// ValueRefListE - Just like ValueRefList, except that it may also be empty!
1170ValueRefListE
Reid Spencerf8483652006-12-02 15:16:01 +00001171 : ValueRefList { $$ = $1; }
1172 | /*empty*/ { $$ = new ValueList(); }
Reid Spencere7c3c602006-11-30 06:36:44 +00001173 ;
1174
1175OptTailCall
1176 : TAIL CALL {
1177 *$1 += " " + *$2;
1178 delete $2;
1179 $$ = $1;
1180 }
1181 | CALL
1182 ;
1183
1184InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencer78720742006-12-02 20:21:22 +00001185 const char* op = getDivRemOpcode(*$1, $2);
1186 $$ = new std::string(op);
1187 *$$ += " " + *$2.newTy + " " + *$3.val + ", " + *$5.val;
1188 delete $1; $2.destroy(); $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001189 }
1190 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencerf459d392006-12-02 16:19:52 +00001191 *$1 += " " + *$2.newTy + " " + *$3.val + ", " + *$5.val;
1192 $2.destroy(); $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001193 $$ = $1;
1194 }
1195 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencer229e9362006-12-02 22:14:11 +00001196 *$1 = getCompareOp(*$1, $2);
Reid Spencerf459d392006-12-02 16:19:52 +00001197 *$1 += " " + *$2.newTy + " " + *$3.val + ", " + *$5.val;
1198 $2.destroy(); $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001199 $$ = $1;
1200 }
Reid Spencer57f28f92006-12-03 07:10:26 +00001201 | ICMP IPredicates Types ValueRef ',' ValueRef ')' {
1202 *$1 += " " + *$2 + " " + *$4.val + "," + *$6.val + ")";
1203 delete $2; $4.destroy(); $6.destroy();
1204 $$ = $1;
1205 }
1206 | FCMP FPredicates Types ValueRef ',' ValueRef ')' {
Reid Spencer229e9362006-12-02 22:14:11 +00001207 *$1 += " " + *$2 + " " + *$4.val + "," + *$6.val + ")";
1208 delete $2; $4.destroy(); $6.destroy();
1209 $$ = $1;
1210 }
Reid Spencere7c3c602006-11-30 06:36:44 +00001211 | NOT ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001212 *$1 += " " + *$2.val;
1213 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001214 $$ = $1;
1215 }
1216 | ShiftOps ResolvedVal ',' ResolvedVal {
Reid Spencerf7bde222006-12-01 22:26:37 +00001217 const char* shiftop = $1->c_str();
1218 if (*$1 == "shr")
1219 shiftop = ($2.type.isUnsigned()) ? "lshr" : "ashr";
1220 $$ = new std::string(shiftop);
1221 *$$ += " " + *$2.val + ", " + *$4.val;
1222 delete $1; $2.destroy(); $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001223 }
Reid Spencerfcb5df82006-12-01 22:34:43 +00001224 | CastOps ResolvedVal TO Types {
Reid Spencer280d8012006-12-01 23:40:53 +00001225 std::string source = *$2.val;
Reid Spencera50d5962006-12-02 04:11:07 +00001226 TypeInfo SrcTy = $2.type;
1227 TypeInfo DstTy = $4;
1228 ResolveType(DstTy);
1229 $$ = new std::string();
Reid Spencer280d8012006-12-01 23:40:53 +00001230 if (*$1 == "cast") {
Reid Spencera50d5962006-12-02 04:11:07 +00001231 *$$ += getCastUpgrade(source, SrcTy, DstTy, false);
1232 } else {
1233 *$$ += *$1 + " " + source + " to " + *DstTy.newTy;
Reid Spencer280d8012006-12-01 23:40:53 +00001234 }
Reid Spencere77e35e2006-12-01 20:26:20 +00001235 delete $1; $2.destroy();
1236 delete $3; $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001237 }
1238 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001239 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1240 $2.destroy(); $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001241 $$ = $1;
1242 }
1243 | VAARG ResolvedVal ',' Types {
Reid Spencere77e35e2006-12-01 20:26:20 +00001244 *$1 += " " + *$2.val + ", " + *$4.newTy;
1245 $2.destroy(); $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001246 $$ = $1;
1247 }
1248 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001249 *$1 += " " + *$2.val + ", " + *$4.val;
1250 $2.destroy(); $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001251 $$ = $1;
1252 }
1253 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001254 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1255 $2.destroy(); $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001256 $$ = $1;
1257 }
1258 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001259 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1260 $2.destroy(); $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001261 $$ = $1;
1262 }
1263 | PHI_TOK PHIList {
1264 *$1 += " " + *$2;
1265 delete $2;
1266 $$ = $1;
1267 }
1268 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
1269 if (!$2->empty())
1270 *$1 += " " + *$2;
1271 if (!$1->empty())
1272 *$1 += " ";
Reid Spencerf459d392006-12-02 16:19:52 +00001273 *$1 += *$3.newTy + " " + *$4.val + "(";
Reid Spencerf8483652006-12-02 15:16:01 +00001274 for (unsigned i = 0; i < $6->size(); ++i) {
1275 ValueInfo& VI = (*$6)[i];
1276 *$1 += *VI.val;
1277 if (i+1 < $6->size())
1278 *$1 += ", ";
1279 VI.destroy();
1280 }
1281 *$1 += ")";
Reid Spencerf459d392006-12-02 16:19:52 +00001282 delete $2; $3.destroy(); $4.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001283 $$ = $1;
1284 }
1285 | MemoryInst ;
1286
1287
1288// IndexList - List of indices for GEP based instructions...
1289IndexList
Reid Spencerf8483652006-12-02 15:16:01 +00001290 : ',' ValueRefList { $$ = $2; }
1291 | /* empty */ { $$ = new ValueList(); }
Reid Spencere7c3c602006-11-30 06:36:44 +00001292 ;
1293
1294OptVolatile
1295 : VOLATILE
1296 | /* empty */ { $$ = new std::string(); }
1297 ;
1298
1299MemoryInst : MALLOC Types OptCAlign {
Reid Spencere77e35e2006-12-01 20:26:20 +00001300 *$1 += " " + *$2.newTy;
Reid Spencere7c3c602006-11-30 06:36:44 +00001301 if (!$3->empty())
1302 *$1 += " " + *$3;
Reid Spencere77e35e2006-12-01 20:26:20 +00001303 $2.destroy(); delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001304 $$ = $1;
1305 }
1306 | MALLOC Types ',' UINT ValueRef OptCAlign {
Reid Spencerf459d392006-12-02 16:19:52 +00001307 *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5.val;
Reid Spencere7c3c602006-11-30 06:36:44 +00001308 if (!$6->empty())
1309 *$1 += " " + *$6;
Reid Spencerf459d392006-12-02 16:19:52 +00001310 $2.destroy(); $4.destroy(); $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001311 $$ = $1;
1312 }
1313 | ALLOCA 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 | ALLOCA 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 | FREE ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001328 *$1 += " " + *$2.val;
1329 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001330 $$ = $1;
1331 }
1332 | OptVolatile LOAD Types ValueRef {
1333 if (!$1->empty())
1334 *$1 += " ";
Reid Spencerf459d392006-12-02 16:19:52 +00001335 *$1 += *$2 + " " + *$3.newTy + " " + *$4.val;
1336 delete $2; $3.destroy(); $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001337 $$ = $1;
1338 }
1339 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
1340 if (!$1->empty())
1341 *$1 += " ";
Reid Spencerf459d392006-12-02 16:19:52 +00001342 *$1 += *$2 + " " + *$3.val + ", " + *$5.newTy + " " + *$6.val;
1343 delete $2; $3.destroy(); $5.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001344 $$ = $1;
1345 }
1346 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencerf459d392006-12-02 16:19:52 +00001347 // Upgrade the indices
1348 for (unsigned i = 0; i < $4->size(); ++i) {
1349 ValueInfo& VI = (*$4)[i];
1350 if (VI.type.isUnsigned() && !VI.isConstant() &&
1351 VI.type.getBitWidth() < 64) {
1352 std::string* old = VI.val;
1353 *O << " %gep_upgrade" << unique << " = zext " << *old
1354 << " to ulong\n";
1355 VI.val = new std::string("ulong %gep_upgrade" + llvm::utostr(unique++));
1356 VI.type.oldTy = ULongTy;
1357 delete old;
1358 }
1359 }
1360 *$1 += " " + *$2.newTy + " " + *$3.val;
Reid Spencerf8483652006-12-02 15:16:01 +00001361 for (unsigned i = 0; i < $4->size(); ++i) {
1362 ValueInfo& VI = (*$4)[i];
1363 *$1 += ", " + *VI.val;
1364 VI.destroy();
1365 }
Reid Spencerf459d392006-12-02 16:19:52 +00001366 $2.destroy(); $3.destroy(); delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00001367 $$ = $1;
1368 };
1369
1370%%
1371
1372int yyerror(const char *ErrorMsg) {
1373 std::string where
1374 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
1375 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
1376 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
1377 if (yychar == YYEMPTY || yychar == 0)
1378 errMsg += "end-of-file.";
1379 else
1380 errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
1381 std::cerr << errMsg << '\n';
1382 exit(1);
1383}