blob: 8aa2beb1a74a2a3cd0fd64448093743aee50002a [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;
42
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;
189 } else if (DstTy.oldTy == BoolTy) {
190 // 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");
195 if (isConst)
196 Result = "setne (" + Source + comparator + ")";
197 else
198 Result = "setne " + Source + comparator;
199 return Result; // skip cast processing below
200 }
201 ResolveType(SrcTy);
202 ResolveType(DstTy);
203 std::string Opcode(getCastOpcode(Source, SrcTy, DstTy));
204 if (isConst)
205 Result += Opcode + "( " + Source + " to " + *DstTy.newTy + ")";
206 else
207 Result += Opcode + " " + Source + " to " + *DstTy.newTy;
208 return Result;
209}
210
Reid Spencer78720742006-12-02 20:21:22 +0000211const char* getDivRemOpcode(const std::string& opcode, const TypeInfo& TI) {
212 const char* op = opcode.c_str();
213 TypeInfo Ty = TI;
214 ResolveType(Ty);
215 if (Ty.isPacked())
216 Ty.oldTy = Ty.getElementType();
217 if (opcode == "div")
218 if (Ty.isFloatingPoint())
219 op = "fdiv";
220 else if (Ty.isUnsigned())
221 op = "udiv";
222 else if (Ty.isSigned())
223 op = "sdiv";
224 else
225 yyerror("Invalid type for div instruction");
226 else if (opcode == "rem")
227 if (Ty.isFloatingPoint())
228 op = "frem";
229 else if (Ty.isUnsigned())
230 op = "urem";
231 else if (Ty.isSigned())
232 op = "srem";
233 else
234 yyerror("Invalid type for rem instruction");
235 return op;
236}
Reid Spencer229e9362006-12-02 22:14:11 +0000237
238std::string
239getCompareOp(const std::string& setcc, const TypeInfo& TI) {
240 assert(setcc.length() == 5);
241 char cc1 = setcc[3];
242 char cc2 = setcc[4];
243 assert(cc1 == 'e' || cc1 == 'n' || cc1 == 'l' || cc1 == 'g');
244 assert(cc2 == 'q' || cc2 == 'e' || cc2 == 'e' || cc2 == 't');
245 std::string result("xcmp xxx");
246 result[6] = cc1;
247 result[7] = cc2;
248 if (TI.isFloatingPoint()) {
249 result[0] = 'f';
250 result[5] = 'o'; // FIXME: Always map to ordered comparison ?
251 } else if (TI.isIntegral() || TI.isPointer()) {
252 result[0] = 'i';
253 if ((cc1 == 'e' && cc2 == 'q') || (cc1 == 'n' && cc2 == 'e'))
254 result.erase(5,1);
255 else if (TI.isSigned())
256 result[5] = 's';
257 else if (TI.isUnsigned() || TI.isPointer())
258 result[5] = 'u';
259 else
260 yyerror("Invalid integral type for setcc");
261 }
262 return result;
263}
264
Reid Spencere7c3c602006-11-30 06:36:44 +0000265%}
266
Reid Spencere77e35e2006-12-01 20:26:20 +0000267%file-prefix="UpgradeParser"
268
269%union {
270 std::string* String;
271 TypeInfo Type;
272 ValueInfo Value;
273 ConstInfo Const;
Reid Spencerf8483652006-12-02 15:16:01 +0000274 ValueList* ValList;
Reid Spencere77e35e2006-12-01 20:26:20 +0000275}
276
Reid Spencerf2d55322006-12-01 21:52:30 +0000277%token <Type> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
Reid Spencera50d5962006-12-02 04:11:07 +0000278%token <Type> FLOAT DOUBLE LABEL
279%token <String> OPAQUE ESINT64VAL EUINT64VAL SINTVAL UINTVAL FPVAL
Reid Spencerf2d55322006-12-01 21:52:30 +0000280%token <String> NULL_TOK UNDEF ZEROINITIALIZER TRUETOK FALSETOK
Reid Spencere77e35e2006-12-01 20:26:20 +0000281%token <String> TYPE VAR_ID LABELSTR STRINGCONSTANT
282%token <String> IMPLEMENTATION BEGINTOK ENDTOK
283%token <String> DECLARE GLOBAL CONSTANT SECTION VOLATILE
284%token <String> TO DOTDOTDOT CONST INTERNAL LINKONCE WEAK
285%token <String> DLLIMPORT DLLEXPORT EXTERN_WEAK APPENDING
286%token <String> NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG
Reid Spencer78720742006-12-02 20:21:22 +0000287%token <String> ALIGN UNINITIALIZED
Reid Spencere77e35e2006-12-01 20:26:20 +0000288%token <String> DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
289%token <String> CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
290%token <String> X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
291%token <String> DATALAYOUT
Reid Spencer78720742006-12-02 20:21:22 +0000292%token <String> RET BR SWITCH INVOKE EXCEPT UNWIND UNREACHABLE
293%token <String> ADD SUB MUL DIV UDIV SDIV FDIV REM UREM SREM FREM AND OR XOR
Reid Spencere77e35e2006-12-01 20:26:20 +0000294%token <String> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comparators
Reid Spencer229e9362006-12-02 22:14:11 +0000295%token <String> ICMP FCMP EQ NE SLT SGT SLE SGE OEQ ONE OLT OGT OLE OGE
296%token <String> ORD UNO UEQ UNE ULT UGT ULE UGE
Reid Spencere77e35e2006-12-01 20:26:20 +0000297%token <String> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
Reid Spencerf7bde222006-12-01 22:26:37 +0000298%token <String> PHI_TOK SELECT SHL SHR ASHR LSHR VAARG
Reid Spencere77e35e2006-12-01 20:26:20 +0000299%token <String> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
Reid Spencerfcb5df82006-12-01 22:34:43 +0000300%token <String> CAST TRUNC ZEXT SEXT FPTRUNC FPEXT FPTOUI FPTOSI UITOFP SITOFP
301%token <String> PTRTOINT INTTOPTR BITCAST
Reid Spencere77e35e2006-12-01 20:26:20 +0000302
303%type <String> OptAssign OptLinkage OptCallingConv OptAlign OptCAlign
304%type <String> SectionString OptSection GlobalVarAttributes GlobalVarAttribute
305%type <String> ArgTypeListI ConstExpr DefinitionList
306%type <String> ConstPool TargetDefinition LibrariesDefinition LibList OptName
307%type <String> ArgVal ArgListH ArgList FunctionHeaderH BEGIN FunctionHeader END
308%type <String> Function FunctionProto BasicBlock TypeListI
309%type <String> InstructionList BBTerminatorInst JumpTable Inst PHIList
Reid Spencer78720742006-12-02 20:21:22 +0000310%type <String> OptTailCall InstVal OptVolatile Unwind
Reid Spencere77e35e2006-12-01 20:26:20 +0000311%type <String> MemoryInst SymbolicValueRef OptSideEffect GlobalType
312%type <String> FnDeclareLinkage BasicBlockList BigOrLittle AsmBlock
Reid Spencer78720742006-12-02 20:21:22 +0000313%type <String> Name ConstValueRef ConstVector External
Reid Spencer57f28f92006-12-03 07:10:26 +0000314%type <String> ShiftOps SetCondOps LogicalOps ArithmeticOps CastOps
315%type <String> IPredicates FPredicates
Reid Spencere77e35e2006-12-01 20:26:20 +0000316
Reid Spencerf8483652006-12-02 15:16:01 +0000317%type <ValList> ValueRefList ValueRefListE IndexList
Reid Spencere77e35e2006-12-01 20:26:20 +0000318
319%type <Type> IntType SIntType UIntType FPType TypesV Types
320%type <Type> PrimType UpRTypesV UpRTypes
321
Reid Spencerf2d55322006-12-01 21:52:30 +0000322%type <String> IntVal EInt64Val
323%type <Const> ConstVal
Reid Spencere77e35e2006-12-01 20:26:20 +0000324
Reid Spencerf459d392006-12-02 16:19:52 +0000325%type <Value> ValueRef ResolvedVal
Reid Spencere7c3c602006-11-30 06:36:44 +0000326
327%start Module
328
329%%
330
331// Handle constant integer size restriction and conversion...
Reid Spencerf2d55322006-12-01 21:52:30 +0000332IntVal : SINTVAL | UINTVAL ;
Reid Spencere77e35e2006-12-01 20:26:20 +0000333EInt64Val : ESINT64VAL | EUINT64VAL;
Reid Spencere7c3c602006-11-30 06:36:44 +0000334
335// Operations that are notably excluded from this list include:
336// RET, BR, & SWITCH because they end basic blocks and are treated specially.
Reid Spencer78720742006-12-02 20:21:22 +0000337ArithmeticOps: ADD | SUB | MUL | DIV | UDIV | SDIV | FDIV
338 | REM | UREM | SREM | FREM;
Reid Spencere7c3c602006-11-30 06:36:44 +0000339LogicalOps : AND | OR | XOR;
340SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
Reid Spencer57f28f92006-12-03 07:10:26 +0000341IPredicates : EQ | NE | SLT | SGT | SLE | SGE | ULT | UGT | ULE | UGE;
342FPredicates : OEQ | ONE | OLT | OGT | OLE | OGE | ORD | UNO | UEQ | UNE
343 | ULT | UGT | ULE | UGE | TRUETOK | FALSETOK;
Reid Spencerf7bde222006-12-01 22:26:37 +0000344ShiftOps : SHL | SHR | ASHR | LSHR;
Reid Spencerfcb5df82006-12-01 22:34:43 +0000345CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | FPTOUI | FPTOSI |
346 UITOFP | SITOFP | PTRTOINT | INTTOPTR | BITCAST | CAST
347 ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000348
349// These are some types that allow classification if we only want a particular
350// thing... for example, only a signed, unsigned, or integral type.
351SIntType : LONG | INT | SHORT | SBYTE;
352UIntType : ULONG | UINT | USHORT | UBYTE;
353IntType : SIntType | UIntType;
354FPType : FLOAT | DOUBLE;
355
356// OptAssign - Value producing statements have an optional assignment component
357OptAssign : Name '=' {
Reid Spencere7c3c602006-11-30 06:36:44 +0000358 $$ = $1;
359 }
360 | /*empty*/ {
361 $$ = new std::string("");
362 };
363
364OptLinkage
365 : INTERNAL | LINKONCE | WEAK | APPENDING | DLLIMPORT | DLLEXPORT
366 | EXTERN_WEAK
367 | /*empty*/ { $$ = new std::string(""); } ;
368
369OptCallingConv
370 : CCC_TOK | CSRETCC_TOK | FASTCC_TOK | COLDCC_TOK | X86_STDCALLCC_TOK
Reid Spencer16244f42006-12-01 21:10:07 +0000371 | X86_FASTCALLCC_TOK
372 | CC_TOK EUINT64VAL {
Reid Spencerf2d55322006-12-01 21:52:30 +0000373 *$1 += *$2;
374 delete $2;
Reid Spencer16244f42006-12-01 21:10:07 +0000375 $$ = $1;
376 }
Reid Spencere7c3c602006-11-30 06:36:44 +0000377 | /*empty*/ { $$ = new std::string(""); } ;
378
379// OptAlign/OptCAlign - An optional alignment, and an optional alignment with
380// a comma before it.
381OptAlign
382 : /*empty*/ { $$ = new std::string(); }
Reid Spencerf2d55322006-12-01 21:52:30 +0000383 | ALIGN EUINT64VAL { *$1 += " " + *$2; delete $2; $$ = $1; };
Reid Spencere7c3c602006-11-30 06:36:44 +0000384 ;
385OptCAlign
386 : /*empty*/ { $$ = new std::string(); }
387 | ',' ALIGN EUINT64VAL {
388 $2->insert(0, ", ");
Reid Spencerf2d55322006-12-01 21:52:30 +0000389 *$2 += " " + *$3;
390 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000391 $$ = $2;
392 };
393
394SectionString
395 : SECTION STRINGCONSTANT {
396 *$1 += " " + *$2;
397 delete $2;
398 $$ = $1;
399 };
400
401OptSection : /*empty*/ { $$ = new std::string(); }
402 | SectionString;
403
404GlobalVarAttributes
405 : /* empty */ { $$ = new std::string(); }
406 | ',' GlobalVarAttribute GlobalVarAttributes {
407 $2->insert(0, ", ");
408 if (!$3->empty())
409 *$2 += " " + *$3;
410 delete $3;
411 $$ = $2;
412 };
413
414GlobalVarAttribute
415 : SectionString
416 | ALIGN EUINT64VAL {
Reid Spencerf2d55322006-12-01 21:52:30 +0000417 *$1 += " " + *$2;
418 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000419 $$ = $1;
420 };
421
422//===----------------------------------------------------------------------===//
423// Types includes all predefined types... except void, because it can only be
424// used in specific contexts (function returning void for example). To have
425// access to it, a user must explicitly use TypesV.
426//
427
428// TypesV includes all of 'Types', but it also includes the void type.
429TypesV : Types | VOID ;
430UpRTypesV : UpRTypes | VOID ;
431Types : UpRTypes ;
432
433// Derived types are added later...
434//
435PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
Reid Spencere77e35e2006-12-01 20:26:20 +0000436PrimType : LONG | ULONG | FLOAT | DOUBLE | LABEL;
Reid Spencera50d5962006-12-02 04:11:07 +0000437UpRTypes
438 : OPAQUE {
439 $$.newTy = $1;
440 $$.oldTy = OpaqueTy;
441 }
442 | SymbolicValueRef {
443 $$.newTy = $1;
444 $$.oldTy = UnresolvedTy;
445 }
Reid Spencer78720742006-12-02 20:21:22 +0000446 | PrimType {
447 $$ = $1;
448 }
449 | '\\' EUINT64VAL { // Type UpReference
Reid Spencerf2d55322006-12-01 21:52:30 +0000450 $2->insert(0, "\\");
451 $$.newTy = $2;
Reid Spencera50d5962006-12-02 04:11:07 +0000452 $$.oldTy = NumericTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000453 }
454 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
Reid Spencere77e35e2006-12-01 20:26:20 +0000455 *$1.newTy += "( " + *$3 + " )";
Reid Spencere7c3c602006-11-30 06:36:44 +0000456 delete $3;
Reid Spencere77e35e2006-12-01 20:26:20 +0000457 $$.newTy = $1.newTy;
458 $$.oldTy = FunctionTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000459 }
460 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
Reid Spencerf2d55322006-12-01 21:52:30 +0000461 $2->insert(0,"[ ");
462 *$2 += " x " + *$4.newTy + " ]";
Reid Spencere77e35e2006-12-01 20:26:20 +0000463 delete $4.newTy;
Reid Spencerf2d55322006-12-01 21:52:30 +0000464 $$.newTy = $2;
Reid Spencere77e35e2006-12-01 20:26:20 +0000465 $$.oldTy = ArrayTy;
Reid Spencer78720742006-12-02 20:21:22 +0000466 $$.elemTy = $4.oldTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000467 }
468 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
Reid Spencerf2d55322006-12-01 21:52:30 +0000469 $2->insert(0,"< ");
470 *$2 += " x " + *$4.newTy + " >";
Reid Spencere77e35e2006-12-01 20:26:20 +0000471 delete $4.newTy;
Reid Spencerf2d55322006-12-01 21:52:30 +0000472 $$.newTy = $2;
Reid Spencere77e35e2006-12-01 20:26:20 +0000473 $$.oldTy = PackedTy;
Reid Spencer78720742006-12-02 20:21:22 +0000474 $$.elemTy = $4.oldTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000475 }
476 | '{' TypeListI '}' { // Structure type?
477 $2->insert(0, "{ ");
478 *$2 += " }";
Reid Spencere77e35e2006-12-01 20:26:20 +0000479 $$.newTy = $2;
480 $$.oldTy = StructTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000481 }
482 | '{' '}' { // Empty structure type?
Reid Spencer0b7e5072006-12-01 22:42:01 +0000483 $$.newTy = new std::string("{}");
Reid Spencere77e35e2006-12-01 20:26:20 +0000484 $$.oldTy = StructTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000485 }
486 | UpRTypes '*' { // Pointer type?
Reid Spencere77e35e2006-12-01 20:26:20 +0000487 *$1.newTy += '*';
Reid Spencer78720742006-12-02 20:21:22 +0000488 $$.elemTy = $1.oldTy;
Reid Spencere77e35e2006-12-01 20:26:20 +0000489 $1.oldTy = PointerTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000490 $$ = $1;
491 };
492
493// TypeList - Used for struct declarations and as a basis for function type
494// declaration type lists
495//
Reid Spencere77e35e2006-12-01 20:26:20 +0000496TypeListI
497 : UpRTypes {
498 $$ = $1.newTy;
499 }
500 | TypeListI ',' UpRTypes {
501 *$1 += ", " + *$3.newTy;
502 delete $3.newTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000503 $$ = $1;
504 };
505
506// ArgTypeList - List of types for a function type declaration...
Reid Spencere77e35e2006-12-01 20:26:20 +0000507ArgTypeListI
508 : TypeListI
Reid Spencere7c3c602006-11-30 06:36:44 +0000509 | TypeListI ',' DOTDOTDOT {
510 *$1 += ", ...";
511 delete $3;
512 $$ = $1;
513 }
514 | DOTDOTDOT {
515 $$ = $1;
516 }
517 | /*empty*/ {
518 $$ = new std::string();
519 };
520
521// ConstVal - The various declarations that go into the constant pool. This
522// production is used ONLY to represent constants that show up AFTER a 'const',
523// 'constant' or 'global' token at global scope. Constants that can be inlined
524// into other expressions (such as integers and constexprs) are handled by the
525// ResolvedVal, ValueRef and ConstValueRef productions.
526//
527ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
Reid Spencere77e35e2006-12-01 20:26:20 +0000528 $$.type = $1;
529 $$.cnst = new std::string(*$1.newTy);
530 *$$.cnst += " [ " + *$3 + " ]";
Reid Spencere7c3c602006-11-30 06:36:44 +0000531 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000532 }
533 | Types '[' ']' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000534 $$.type = $1;
535 $$.cnst = new std::string(*$1.newTy);
536 *$$.cnst += "[ ]";
Reid Spencere7c3c602006-11-30 06:36:44 +0000537 }
538 | Types 'c' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +0000539 $$.type = $1;
540 $$.cnst = new std::string(*$1.newTy);
541 *$$.cnst += " c" + *$3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000542 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000543 }
544 | Types '<' ConstVector '>' { // Nonempty unsized arr
Reid Spencere77e35e2006-12-01 20:26:20 +0000545 $$.type = $1;
546 $$.cnst = new std::string(*$1.newTy);
547 *$$.cnst += " < " + *$3 + " >";
Reid Spencere7c3c602006-11-30 06:36:44 +0000548 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000549 }
550 | Types '{' ConstVector '}' {
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 '{' '}' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000557 $$.type = $1;
558 $$.cnst = new std::string(*$1.newTy);
Reid Spencer0b7e5072006-12-01 22:42:01 +0000559 *$$.cnst += " {}";
Reid Spencere7c3c602006-11-30 06:36:44 +0000560 }
561 | Types NULL_TOK {
Reid Spencere77e35e2006-12-01 20:26:20 +0000562 $$.type = $1;
563 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000564 *$$.cnst += " " + *$2;
565 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000566 }
567 | Types UNDEF {
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 SymbolicValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +0000574 $$.type = $1;
575 $$.cnst = new std::string(*$1.newTy);
576 *$$.cnst += " " + *$2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000577 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000578 }
579 | Types ConstExpr {
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 ZEROINITIALIZER {
Reid Spencere77e35e2006-12-01 20:26:20 +0000586 $$.type = $1;
587 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000588 *$$.cnst += " " + *$2;
589 delete $2;
Reid Spencere77e35e2006-12-01 20:26:20 +0000590 }
591 | SIntType EInt64Val { // integral constants
592 $$.type = $1;
593 $$.cnst = new std::string(*$1.newTy);
Reid Spencerf2d55322006-12-01 21:52:30 +0000594 *$$.cnst += " " + *$2;
595 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000596 }
597 | UIntType EUINT64VAL { // integral constants
Reid Spencere77e35e2006-12-01 20:26:20 +0000598 $$.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 | BOOL TRUETOK { // Boolean 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 FALSETOK { // 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 | FPType FPVAL { // Float & Double 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
622
Reid Spencerfcb5df82006-12-01 22:34:43 +0000623ConstExpr: CastOps '(' ConstVal TO Types ')' {
Reid Spencer280d8012006-12-01 23:40:53 +0000624 std::string source = *$3.cnst;
Reid Spencera50d5962006-12-02 04:11:07 +0000625 TypeInfo DstTy = $5;
626 ResolveType(DstTy);
Reid Spencer280d8012006-12-01 23:40:53 +0000627 if (*$1 == "cast") {
Reid Spencera50d5962006-12-02 04:11:07 +0000628 // Call getCastUpgrade to upgrade the old cast
629 $$ = new std::string(getCastUpgrade(source, $3.type, $5, true));
630 } else {
631 // Nothing to upgrade, just create the cast constant expr
632 $$ = new std::string(*$1);
633 *$$ += "( " + source + " to " + *$5.newTy + ")";
Reid Spencer280d8012006-12-01 23:40:53 +0000634 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000635 delete $1; $3.destroy(); delete $4; $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000636 }
637 | GETELEMENTPTR '(' ConstVal IndexList ')' {
Reid Spencerf8483652006-12-02 15:16:01 +0000638 *$1 += "(" + *$3.cnst;
639 for (unsigned i = 0; i < $4->size(); ++i) {
640 ValueInfo& VI = (*$4)[i];
641 *$1 += ", " + *VI.val;
642 VI.destroy();
643 }
644 *$1 += ")";
Reid Spencere77e35e2006-12-01 20:26:20 +0000645 $$ = $1;
646 $3.destroy();
647 delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +0000648 }
649 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000650 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
651 $3.destroy(); $5.destroy(); $7.destroy();
652 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000653 }
654 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer78720742006-12-02 20:21:22 +0000655 const char* op = getDivRemOpcode(*$1, $3.type);
656 $$ = new std::string(op);
657 *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
658 delete $1; $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000659 }
660 | LogicalOps '(' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000661 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
662 $3.destroy(); $5.destroy();
663 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000664 }
665 | SetCondOps '(' ConstVal ',' ConstVal ')' {
Reid Spencer229e9362006-12-02 22:14:11 +0000666#if UPGRADE_SETCOND_OPS
667 *$1 = getCompareOp(*$1, $3.type);
668#endif
Reid Spencere77e35e2006-12-01 20:26:20 +0000669 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
670 $3.destroy(); $5.destroy();
671 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000672 }
Reid Spencer57f28f92006-12-03 07:10:26 +0000673 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
674 *$1 += "(" + *$2 + "," + *$4.cnst + "," + *$6.cnst + ")";
675 delete $2; $4.destroy(); $6.destroy();
676 $$ = $1;
677 }
678 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
Reid Spencer229e9362006-12-02 22:14:11 +0000679 *$1 += "(" + *$2 + "," + *$4.cnst + "," + *$6.cnst + ")";
680 delete $2; $4.destroy(); $6.destroy();
681 $$ = $1;
682 }
Reid Spencere7c3c602006-11-30 06:36:44 +0000683 | ShiftOps '(' ConstVal ',' ConstVal ')' {
Reid Spencerf7bde222006-12-01 22:26:37 +0000684 const char* shiftop = $1->c_str();
685 if (*$1 == "shr")
686 shiftop = ($3.type.isUnsigned()) ? "lshr" : "ashr";
687 $$ = new std::string(shiftop);
688 *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
689 delete $1; $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000690 }
691 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000692 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
693 $3.destroy(); $5.destroy();
694 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000695 }
696 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000697 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
698 $3.destroy(); $5.destroy(); $7.destroy();
699 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000700 }
701 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
Reid Spencere77e35e2006-12-01 20:26:20 +0000702 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
703 $3.destroy(); $5.destroy(); $7.destroy();
704 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000705 };
706
707
708// ConstVector - A list of comma separated constants.
Reid Spencere77e35e2006-12-01 20:26:20 +0000709
710ConstVector
711 : ConstVector ',' ConstVal {
712 *$1 += ", " + *$3.cnst;
713 $3.destroy();
714 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000715 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000716 | ConstVal { $$ = new std::string(*$1.cnst); $1.destroy(); }
717 ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000718
719
720// GlobalType - Match either GLOBAL or CONSTANT for global declarations...
Reid Spencere77e35e2006-12-01 20:26:20 +0000721GlobalType : GLOBAL | CONSTANT ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000722
723
724//===----------------------------------------------------------------------===//
725// Rules to match Modules
726//===----------------------------------------------------------------------===//
727
728// Module rule: Capture the result of parsing the whole file into a result
729// variable...
730//
731Module : DefinitionList {
732};
733
734// DefinitionList - Top level definitions
735//
736DefinitionList : DefinitionList Function {
737 $$ = 0;
738 }
739 | DefinitionList FunctionProto {
740 *O << *$2 << "\n";
741 delete $2;
742 $$ = 0;
743 }
744 | DefinitionList MODULE ASM_TOK AsmBlock {
745 *O << "module asm " << " " << *$4 << "\n";
Reid Spencerd154b572006-12-01 20:36:40 +0000746 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000747 }
748 | DefinitionList IMPLEMENTATION {
749 *O << "implementation\n";
Reid Spencerd154b572006-12-01 20:36:40 +0000750 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000751 }
Reid Spencera50d5962006-12-02 04:11:07 +0000752 | ConstPool { $$ = 0; }
Reid Spencere7c3c602006-11-30 06:36:44 +0000753
Reid Spencer78720742006-12-02 20:21:22 +0000754External : EXTERNAL | UNINITIALIZED { $$ = $1; *$$ = "external"; }
755
Reid Spencere7c3c602006-11-30 06:36:44 +0000756// ConstPool - Constants with optional names assigned to them.
757ConstPool : ConstPool OptAssign TYPE TypesV {
Reid Spencera50d5962006-12-02 04:11:07 +0000758 EnumeratedTypes.push_back($4);
759 if (!$2->empty()) {
760 NamedTypes[*$2].newTy = new std::string(*$4.newTy);
761 NamedTypes[*$2].oldTy = $4.oldTy;
Reid Spencer78720742006-12-02 20:21:22 +0000762 NamedTypes[*$2].elemTy = $4.elemTy;
Reid Spencera50d5962006-12-02 04:11:07 +0000763 *O << *$2 << " = ";
764 }
765 *O << "type " << *$4.newTy << "\n";
766 delete $2; delete $3; $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +0000767 $$ = 0;
768 }
769 | ConstPool FunctionProto { // Function prototypes can be in const pool
770 *O << *$2 << "\n";
771 delete $2;
772 $$ = 0;
773 }
774 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
775 *O << *$2 << " " << *$3 << " " << *$4 << "\n";
776 delete $2; delete $3; delete $4;
777 $$ = 0;
778 }
779 | ConstPool OptAssign OptLinkage GlobalType ConstVal GlobalVarAttributes {
Reid Spencera50d5962006-12-02 04:11:07 +0000780 if (!$2->empty())
781 *O << *$2 << " = ";
782 *O << *$3 << " " << *$4 << " " << *$5.cnst << " " << *$6 << "\n";
Reid Spencere77e35e2006-12-01 20:26:20 +0000783 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000784 $$ = 0;
785 }
Reid Spencer78720742006-12-02 20:21:22 +0000786 | ConstPool OptAssign External GlobalType Types GlobalVarAttributes {
Reid Spencera50d5962006-12-02 04:11:07 +0000787 if (!$2->empty())
788 *O << *$2 << " = ";
789 *O << *$3 << " " << *$4 << " " << *$5.newTy << " " << *$6 << "\n";
Reid Spencere77e35e2006-12-01 20:26:20 +0000790 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000791 $$ = 0;
792 }
793 | ConstPool OptAssign DLLIMPORT GlobalType Types GlobalVarAttributes {
Reid Spencera50d5962006-12-02 04:11:07 +0000794 if (!$2->empty())
795 *O << *$2 << " = ";
796 *O << *$3 << " " << *$4 << " " << *$5.newTy << " " << *$6 << "\n";
Reid Spencere77e35e2006-12-01 20:26:20 +0000797 delete $2; delete $3; delete $4; $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +0000798 $$ = 0;
799 }
800 | ConstPool OptAssign EXTERN_WEAK GlobalType Types GlobalVarAttributes {
Reid Spencera50d5962006-12-02 04:11:07 +0000801 if (!$2->empty())
802 *O << *$2 << " = ";
803 *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 TARGET TargetDefinition {
808 *O << *$2 << " " << *$3 << "\n";
809 delete $2; delete $3;
810 $$ = 0;
811 }
812 | ConstPool DEPLIBS '=' LibrariesDefinition {
813 *O << *$2 << " = " << *$4 << "\n";
814 delete $2; delete $4;
815 $$ = 0;
816 }
817 | /* empty: end of list */ {
818 $$ = 0;
819 };
820
821
822AsmBlock : STRINGCONSTANT ;
823
824BigOrLittle : BIG | LITTLE
825
826TargetDefinition
827 : ENDIAN '=' BigOrLittle {
Reid Spencere77e35e2006-12-01 20:26:20 +0000828 *$1 += " = " + *$3;
829 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000830 $$ = $1;
831 }
832 | POINTERSIZE '=' EUINT64VAL {
Reid Spencerf2d55322006-12-01 21:52:30 +0000833 *$1 += " = " + *$3;
834 if (*$3 == "64")
Reid Spencere77e35e2006-12-01 20:26:20 +0000835 SizeOfPointer = 64;
Reid Spencerf2d55322006-12-01 21:52:30 +0000836 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000837 $$ = $1;
838 }
839 | TRIPLE '=' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +0000840 *$1 += " = " + *$3;
841 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000842 $$ = $1;
843 }
844 | DATALAYOUT '=' STRINGCONSTANT {
Reid Spencere77e35e2006-12-01 20:26:20 +0000845 *$1 += " = " + *$3;
846 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000847 $$ = $1;
848 };
849
850LibrariesDefinition
851 : '[' LibList ']' {
852 $2->insert(0, "[ ");
853 *$2 += " ]";
854 $$ = $2;
855 };
856
857LibList
858 : LibList ',' STRINGCONSTANT {
859 *$1 += ", " + *$3;
860 delete $3;
861 $$ = $1;
862 }
863 | STRINGCONSTANT
864 | /* empty: end of list */ {
865 $$ = new std::string();
866 };
867
868//===----------------------------------------------------------------------===//
869// Rules to match Function Headers
870//===----------------------------------------------------------------------===//
871
872Name : VAR_ID | STRINGCONSTANT;
873OptName : Name | /*empty*/ { $$ = new std::string(); };
874
875ArgVal : Types OptName {
Reid Spencere77e35e2006-12-01 20:26:20 +0000876 $$ = $1.newTy;
Reid Spencere7c3c602006-11-30 06:36:44 +0000877 if (!$2->empty())
878 *$$ += " " + *$2;
Reid Spencere77e35e2006-12-01 20:26:20 +0000879 delete $2;
Reid Spencere7c3c602006-11-30 06:36:44 +0000880};
881
882ArgListH : ArgListH ',' ArgVal {
883 *$1 += ", " + *$3;
Reid Spencere77e35e2006-12-01 20:26:20 +0000884 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000885 }
886 | ArgVal {
887 $$ = $1;
888 };
889
890ArgList : ArgListH {
891 $$ = $1;
892 }
893 | ArgListH ',' DOTDOTDOT {
894 *$1 += ", ...";
895 $$ = $1;
Reid Spencere77e35e2006-12-01 20:26:20 +0000896 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000897 }
898 | DOTDOTDOT {
899 $$ = $1;
900 }
Reid Spencerd154b572006-12-01 20:36:40 +0000901 | /* empty */ { $$ = new std::string(); };
Reid Spencere7c3c602006-11-30 06:36:44 +0000902
903FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')'
904 OptSection OptAlign {
905 if (!$1->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000906 *$1 += " ";
Reid Spencere7c3c602006-11-30 06:36:44 +0000907 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000908 *$1 += *$2.newTy + " " + *$3 + "(" + *$5 + ")";
Reid Spencere7c3c602006-11-30 06:36:44 +0000909 if (!$7->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000910 *$1 += " " + *$7;
Reid Spencere7c3c602006-11-30 06:36:44 +0000911 }
912 if (!$8->empty()) {
Reid Spencere77e35e2006-12-01 20:26:20 +0000913 *$1 += " " + *$8;
Reid Spencere7c3c602006-11-30 06:36:44 +0000914 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000915 $2.destroy();
916 delete $3;
917 delete $5;
918 delete $7;
919 delete $8;
920 $$ = $1;
Reid Spencere7c3c602006-11-30 06:36:44 +0000921 };
922
Reid Spencer78720742006-12-02 20:21:22 +0000923BEGIN : BEGINTOK { $$ = new std::string("{"); delete $1; }
924 | '{' { $$ = new std::string ("{"); }
Reid Spencere7c3c602006-11-30 06:36:44 +0000925
926FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
927 if (!$1->empty()) {
928 *O << *$1 << " ";
929 }
930 *O << *$2 << " " << *$3 << "\n";
931 delete $1; delete $2; delete $3;
932 $$ = 0;
933};
934
Reid Spencer78720742006-12-02 20:21:22 +0000935END : ENDTOK { $$ = new std::string("}"); delete $1; }
Reid Spencere7c3c602006-11-30 06:36:44 +0000936 | '}' { $$ = new std::string("}"); };
937
938Function : FunctionHeader BasicBlockList END {
939 if ($2)
940 *O << *$2;
941 *O << '\n' << *$3 << "\n";
Reid Spencere77e35e2006-12-01 20:26:20 +0000942 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +0000943};
944
Reid Spencere77e35e2006-12-01 20:26:20 +0000945FnDeclareLinkage
946 : /*default*/ { $$ = new std::string(); }
Reid Spencere7c3c602006-11-30 06:36:44 +0000947 | DLLIMPORT
948 | EXTERN_WEAK
949 ;
950
951FunctionProto
952 : DECLARE FnDeclareLinkage FunctionHeaderH {
Reid Spencere77e35e2006-12-01 20:26:20 +0000953 if (!$2->empty())
954 *$1 += " " + *$2;
955 *$1 += " " + *$3;
956 delete $2;
957 delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +0000958 $$ = $1;
959 };
960
961//===----------------------------------------------------------------------===//
962// Rules to match Basic Blocks
963//===----------------------------------------------------------------------===//
964
Reid Spencerd154b572006-12-01 20:36:40 +0000965OptSideEffect : /* empty */ { $$ = new std::string(); }
966 | SIDEEFFECT;
Reid Spencere7c3c602006-11-30 06:36:44 +0000967
Reid Spencere77e35e2006-12-01 20:26:20 +0000968ConstValueRef
Reid Spencerf2d55322006-12-01 21:52:30 +0000969 : ESINT64VAL | EUINT64VAL | FPVAL | TRUETOK | FALSETOK | NULL_TOK | UNDEF
970 | ZEROINITIALIZER
Reid Spencere7c3c602006-11-30 06:36:44 +0000971 | '<' ConstVector '>' {
972 $2->insert(0, "<");
973 *$2 += ">";
974 $$ = $2;
975 }
976 | ConstExpr
977 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
978 if (!$2->empty()) {
979 *$1 += " " + *$2;
980 }
Reid Spencere77e35e2006-12-01 20:26:20 +0000981 *$1 += " " + *$3 + ", " + *$5;
982 delete $2; delete $3; delete $5;
Reid Spencere7c3c602006-11-30 06:36:44 +0000983 $$ = $1;
984 };
985
Reid Spencerf2d55322006-12-01 21:52:30 +0000986SymbolicValueRef : IntVal | Name ;
Reid Spencere7c3c602006-11-30 06:36:44 +0000987
988// ValueRef - A reference to a definition... either constant or symbolic
Reid Spencerf459d392006-12-02 16:19:52 +0000989ValueRef
990 : SymbolicValueRef {
991 $$.val = $1;
992 $$.constant = false;
993 $$.type.newTy = 0;
994 $$.type.oldTy = UnresolvedTy;
995 }
996 | ConstValueRef {
997 $$.val = $1;
998 $$.constant = true;
999 $$.type.newTy = 0;
1000 $$.type.oldTy = UnresolvedTy;
1001 }
1002 ;
Reid Spencere7c3c602006-11-30 06:36:44 +00001003
1004// ResolvedVal - a <type> <value> pair. This is used only in cases where the
1005// type immediately preceeds the value reference, and allows complex constant
1006// pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
1007ResolvedVal : Types ValueRef {
Reid Spencerf459d392006-12-02 16:19:52 +00001008 $$ = $2;
Reid Spencere77e35e2006-12-01 20:26:20 +00001009 $$.type = $1;
Reid Spencerf459d392006-12-02 16:19:52 +00001010 $$.val->insert(0, *$1.newTy + " ");
Reid Spencere7c3c602006-11-30 06:36:44 +00001011 };
1012
1013BasicBlockList : BasicBlockList BasicBlock {
Reid Spencerf2d55322006-12-01 21:52:30 +00001014 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001015 }
1016 | BasicBlock { // Do not allow functions with 0 basic blocks
Reid Spencerf2d55322006-12-01 21:52:30 +00001017 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001018 };
1019
1020
1021// Basic blocks are terminated by branching instructions:
1022// br, br/cc, switch, ret
1023//
Reid Spencer16244f42006-12-01 21:10:07 +00001024BasicBlock : InstructionList BBTerminatorInst {
Reid Spencerf2d55322006-12-01 21:52:30 +00001025 $$ = 0;
Reid Spencere7c3c602006-11-30 06:36:44 +00001026 };
1027
1028InstructionList : InstructionList Inst {
1029 *O << " " << *$2 << "\n";
1030 delete $2;
1031 $$ = 0;
1032 }
1033 | /* empty */ {
1034 $$ = 0;
1035 }
1036 | LABELSTR {
1037 *O << *$1 << "\n";
1038 delete $1;
1039 $$ = 0;
1040 };
1041
Reid Spencer78720742006-12-02 20:21:22 +00001042Unwind : UNWIND | EXCEPT { $$ = $1; *$$ = "unwind"; }
1043
Reid Spencere7c3c602006-11-30 06:36:44 +00001044BBTerminatorInst : RET ResolvedVal { // Return with a result...
Reid Spencere77e35e2006-12-01 20:26:20 +00001045 *O << " " << *$1 << " " << *$2.val << "\n";
1046 delete $1; $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001047 $$ = 0;
1048 }
1049 | RET VOID { // Return with no result...
Reid Spencere77e35e2006-12-01 20:26:20 +00001050 *O << " " << *$1 << " " << *$2.newTy << "\n";
1051 delete $1; $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001052 $$ = 0;
1053 }
1054 | BR LABEL ValueRef { // Unconditional Branch...
Reid Spencerf459d392006-12-02 16:19:52 +00001055 *O << " " << *$1 << " " << *$2.newTy << " " << *$3.val << "\n";
1056 delete $1; $2.destroy(); $3.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001057 $$ = 0;
1058 } // Conditional Branch...
1059 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
Reid Spencerf459d392006-12-02 16:19:52 +00001060 *O << " " << *$1 << " " << *$2.newTy << " " << *$3.val << ", "
1061 << *$5.newTy << " " << *$6.val << ", " << *$8.newTy << " "
1062 << *$9.val << "\n";
1063 delete $1; $2.destroy(); $3.destroy(); $5.destroy(); $6.destroy();
1064 $8.destroy(); $9.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001065 $$ = 0;
1066 }
1067 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
Reid Spencerf459d392006-12-02 16:19:52 +00001068 *O << " " << *$1 << " " << *$2.newTy << " " << *$3.val << ", "
1069 << *$5.newTy << " " << *$6.val << " [" << *$8 << " ]\n";
1070 delete $1; $2.destroy(); $3.destroy(); $5.destroy(); $6.destroy();
1071 delete $8;
Reid Spencere7c3c602006-11-30 06:36:44 +00001072 $$ = 0;
1073 }
1074 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
Reid Spencerf459d392006-12-02 16:19:52 +00001075 *O << " " << *$1 << " " << *$2.newTy << " " << *$3.val << ", "
1076 << *$5.newTy << " " << *$6.val << "[]\n";
1077 delete $1; $2.destroy(); $3.destroy(); $5.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001078 $$ = 0;
1079 }
Reid Spencer16244f42006-12-01 21:10:07 +00001080 | OptAssign INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
Reid Spencer78720742006-12-02 20:21:22 +00001081 TO LABEL ValueRef Unwind LABEL ValueRef {
Reid Spencer16244f42006-12-01 21:10:07 +00001082 *O << " ";
1083 if (!$1->empty())
Reid Spencera50d5962006-12-02 04:11:07 +00001084 *O << *$1 << " = ";
Reid Spencerf459d392006-12-02 16:19:52 +00001085 *O << *$2 << " " << *$3 << " " << *$4.newTy << " " << *$5.val << " (";
Reid Spencerf8483652006-12-02 15:16:01 +00001086 for (unsigned i = 0; i < $7->size(); ++i) {
1087 ValueInfo& VI = (*$7)[i];
1088 *O << *VI.val;
1089 if (i+1 < $7->size())
1090 *O << ", ";
1091 VI.destroy();
1092 }
Reid Spencerf459d392006-12-02 16:19:52 +00001093 *O << ") " << *$9 << " " << *$10.newTy << " " << *$11.val << " "
1094 << *$12 << " " << *$13.newTy << " " << *$14.val << "\n";
1095 delete $1; delete $2; delete $3; $4.destroy(); $5.destroy(); delete $7;
1096 delete $9; $10.destroy(); $11.destroy(); delete $12; $13.destroy();
1097 $14.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001098 $$ = 0;
1099 }
Reid Spencer78720742006-12-02 20:21:22 +00001100 | Unwind {
Reid Spencere7c3c602006-11-30 06:36:44 +00001101 *O << " " << *$1 << "\n";
1102 delete $1;
1103 $$ = 0;
1104 }
1105 | UNREACHABLE {
1106 *O << " " << *$1 << "\n";
1107 delete $1;
1108 $$ = 0;
1109 };
1110
1111JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
Reid Spencerf459d392006-12-02 16:19:52 +00001112 *$1 += " " + *$2.newTy + " " + *$3 + ", " + *$5.newTy + " " + *$6.val;
1113 $2.destroy(); delete $3; $5.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001114 $$ = $1;
1115 }
1116 | IntType ConstValueRef ',' LABEL ValueRef {
Reid Spencere77e35e2006-12-01 20:26:20 +00001117 $2->insert(0, *$1.newTy + " " );
Reid Spencerf459d392006-12-02 16:19:52 +00001118 *$2 += ", " + *$4.newTy + " " + *$5.val;
1119 $1.destroy(); $4.destroy(); $5.destroy();
Reid Spencere77e35e2006-12-01 20:26:20 +00001120 $$ = $2;
Reid Spencere7c3c602006-11-30 06:36:44 +00001121 };
1122
1123Inst
1124 : OptAssign InstVal {
Reid Spencera50d5962006-12-02 04:11:07 +00001125 if (!$1->empty())
1126 *$1 += " = ";
Reid Spencere7c3c602006-11-30 06:36:44 +00001127 *$1 += *$2;
1128 delete $2;
1129 $$ = $1;
1130 };
1131
1132PHIList
1133 : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
Reid Spencerf459d392006-12-02 16:19:52 +00001134 $3.val->insert(0, *$1.newTy + "[");
1135 *$3.val += "," + *$5.val + "]";
1136 $1.destroy(); $5.destroy();
1137 $$ = new std::string(*$3.val);
1138 $3.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001139 }
1140 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
Reid Spencerf459d392006-12-02 16:19:52 +00001141 *$1 += ", [" + *$4.val + "," + *$6.val + "]";
1142 $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001143 $$ = $1;
1144 };
1145
1146
1147ValueRefList
Reid Spencerf8483652006-12-02 15:16:01 +00001148 : ResolvedVal {
1149 $$ = new ValueList();
1150 $$->push_back($1);
1151 }
Reid Spencere7c3c602006-11-30 06:36:44 +00001152 | ValueRefList ',' ResolvedVal {
Reid Spencerf8483652006-12-02 15:16:01 +00001153 $1->push_back($3);
Reid Spencere7c3c602006-11-30 06:36:44 +00001154 $$ = $1;
1155 };
1156
1157// ValueRefListE - Just like ValueRefList, except that it may also be empty!
1158ValueRefListE
Reid Spencerf8483652006-12-02 15:16:01 +00001159 : ValueRefList { $$ = $1; }
1160 | /*empty*/ { $$ = new ValueList(); }
Reid Spencere7c3c602006-11-30 06:36:44 +00001161 ;
1162
1163OptTailCall
1164 : TAIL CALL {
1165 *$1 += " " + *$2;
1166 delete $2;
1167 $$ = $1;
1168 }
1169 | CALL
1170 ;
1171
1172InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
Reid Spencer78720742006-12-02 20:21:22 +00001173 const char* op = getDivRemOpcode(*$1, $2);
1174 $$ = new std::string(op);
1175 *$$ += " " + *$2.newTy + " " + *$3.val + ", " + *$5.val;
1176 delete $1; $2.destroy(); $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001177 }
1178 | LogicalOps Types ValueRef ',' ValueRef {
Reid Spencerf459d392006-12-02 16:19:52 +00001179 *$1 += " " + *$2.newTy + " " + *$3.val + ", " + *$5.val;
1180 $2.destroy(); $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001181 $$ = $1;
1182 }
1183 | SetCondOps Types ValueRef ',' ValueRef {
Reid Spencer229e9362006-12-02 22:14:11 +00001184#if UPGRADE_SETCOND_OPS
1185 *$1 = getCompareOp(*$1, $2);
1186#endif
Reid Spencerf459d392006-12-02 16:19:52 +00001187 *$1 += " " + *$2.newTy + " " + *$3.val + ", " + *$5.val;
1188 $2.destroy(); $3.destroy(); $5.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001189 $$ = $1;
1190 }
Reid Spencer57f28f92006-12-03 07:10:26 +00001191 | ICMP IPredicates Types ValueRef ',' ValueRef ')' {
1192 *$1 += " " + *$2 + " " + *$4.val + "," + *$6.val + ")";
1193 delete $2; $4.destroy(); $6.destroy();
1194 $$ = $1;
1195 }
1196 | FCMP FPredicates Types ValueRef ',' ValueRef ')' {
Reid Spencer229e9362006-12-02 22:14:11 +00001197 *$1 += " " + *$2 + " " + *$4.val + "," + *$6.val + ")";
1198 delete $2; $4.destroy(); $6.destroy();
1199 $$ = $1;
1200 }
Reid Spencere7c3c602006-11-30 06:36:44 +00001201 | NOT ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001202 *$1 += " " + *$2.val;
1203 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001204 $$ = $1;
1205 }
1206 | ShiftOps ResolvedVal ',' ResolvedVal {
Reid Spencerf7bde222006-12-01 22:26:37 +00001207 const char* shiftop = $1->c_str();
1208 if (*$1 == "shr")
1209 shiftop = ($2.type.isUnsigned()) ? "lshr" : "ashr";
1210 $$ = new std::string(shiftop);
1211 *$$ += " " + *$2.val + ", " + *$4.val;
1212 delete $1; $2.destroy(); $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001213 }
Reid Spencerfcb5df82006-12-01 22:34:43 +00001214 | CastOps ResolvedVal TO Types {
Reid Spencer280d8012006-12-01 23:40:53 +00001215 std::string source = *$2.val;
Reid Spencera50d5962006-12-02 04:11:07 +00001216 TypeInfo SrcTy = $2.type;
1217 TypeInfo DstTy = $4;
1218 ResolveType(DstTy);
1219 $$ = new std::string();
Reid Spencer280d8012006-12-01 23:40:53 +00001220 if (*$1 == "cast") {
Reid Spencera50d5962006-12-02 04:11:07 +00001221 *$$ += getCastUpgrade(source, SrcTy, DstTy, false);
1222 } else {
1223 *$$ += *$1 + " " + source + " to " + *DstTy.newTy;
Reid Spencer280d8012006-12-01 23:40:53 +00001224 }
Reid Spencere77e35e2006-12-01 20:26:20 +00001225 delete $1; $2.destroy();
1226 delete $3; $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001227 }
1228 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001229 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1230 $2.destroy(); $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001231 $$ = $1;
1232 }
1233 | VAARG ResolvedVal ',' Types {
Reid Spencere77e35e2006-12-01 20:26:20 +00001234 *$1 += " " + *$2.val + ", " + *$4.newTy;
1235 $2.destroy(); $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001236 $$ = $1;
1237 }
1238 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001239 *$1 += " " + *$2.val + ", " + *$4.val;
1240 $2.destroy(); $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001241 $$ = $1;
1242 }
1243 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001244 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1245 $2.destroy(); $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001246 $$ = $1;
1247 }
1248 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001249 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1250 $2.destroy(); $4.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001251 $$ = $1;
1252 }
1253 | PHI_TOK PHIList {
1254 *$1 += " " + *$2;
1255 delete $2;
1256 $$ = $1;
1257 }
1258 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
1259 if (!$2->empty())
1260 *$1 += " " + *$2;
1261 if (!$1->empty())
1262 *$1 += " ";
Reid Spencerf459d392006-12-02 16:19:52 +00001263 *$1 += *$3.newTy + " " + *$4.val + "(";
Reid Spencerf8483652006-12-02 15:16:01 +00001264 for (unsigned i = 0; i < $6->size(); ++i) {
1265 ValueInfo& VI = (*$6)[i];
1266 *$1 += *VI.val;
1267 if (i+1 < $6->size())
1268 *$1 += ", ";
1269 VI.destroy();
1270 }
1271 *$1 += ")";
Reid Spencerf459d392006-12-02 16:19:52 +00001272 delete $2; $3.destroy(); $4.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001273 $$ = $1;
1274 }
1275 | MemoryInst ;
1276
1277
1278// IndexList - List of indices for GEP based instructions...
1279IndexList
Reid Spencerf8483652006-12-02 15:16:01 +00001280 : ',' ValueRefList { $$ = $2; }
1281 | /* empty */ { $$ = new ValueList(); }
Reid Spencere7c3c602006-11-30 06:36:44 +00001282 ;
1283
1284OptVolatile
1285 : VOLATILE
1286 | /* empty */ { $$ = new std::string(); }
1287 ;
1288
1289MemoryInst : MALLOC Types OptCAlign {
Reid Spencere77e35e2006-12-01 20:26:20 +00001290 *$1 += " " + *$2.newTy;
Reid Spencere7c3c602006-11-30 06:36:44 +00001291 if (!$3->empty())
1292 *$1 += " " + *$3;
Reid Spencere77e35e2006-12-01 20:26:20 +00001293 $2.destroy(); delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001294 $$ = $1;
1295 }
1296 | MALLOC Types ',' UINT ValueRef OptCAlign {
Reid Spencerf459d392006-12-02 16:19:52 +00001297 *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5.val;
Reid Spencere7c3c602006-11-30 06:36:44 +00001298 if (!$6->empty())
1299 *$1 += " " + *$6;
Reid Spencerf459d392006-12-02 16:19:52 +00001300 $2.destroy(); $4.destroy(); $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001301 $$ = $1;
1302 }
1303 | ALLOCA Types OptCAlign {
Reid Spencere77e35e2006-12-01 20:26:20 +00001304 *$1 += " " + *$2.newTy;
Reid Spencere7c3c602006-11-30 06:36:44 +00001305 if (!$3->empty())
1306 *$1 += " " + *$3;
Reid Spencere77e35e2006-12-01 20:26:20 +00001307 $2.destroy(); delete $3;
Reid Spencere7c3c602006-11-30 06:36:44 +00001308 $$ = $1;
1309 }
1310 | ALLOCA Types ',' UINT ValueRef OptCAlign {
Reid Spencerf459d392006-12-02 16:19:52 +00001311 *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5.val;
Reid Spencere7c3c602006-11-30 06:36:44 +00001312 if (!$6->empty())
1313 *$1 += " " + *$6;
Reid Spencerf459d392006-12-02 16:19:52 +00001314 $2.destroy(); $4.destroy(); $5.destroy(); delete $6;
Reid Spencere7c3c602006-11-30 06:36:44 +00001315 $$ = $1;
1316 }
1317 | FREE ResolvedVal {
Reid Spencere77e35e2006-12-01 20:26:20 +00001318 *$1 += " " + *$2.val;
1319 $2.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001320 $$ = $1;
1321 }
1322 | OptVolatile LOAD Types ValueRef {
1323 if (!$1->empty())
1324 *$1 += " ";
Reid Spencerf459d392006-12-02 16:19:52 +00001325 *$1 += *$2 + " " + *$3.newTy + " " + *$4.val;
1326 delete $2; $3.destroy(); $4.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001327 $$ = $1;
1328 }
1329 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
1330 if (!$1->empty())
1331 *$1 += " ";
Reid Spencerf459d392006-12-02 16:19:52 +00001332 *$1 += *$2 + " " + *$3.val + ", " + *$5.newTy + " " + *$6.val;
1333 delete $2; $3.destroy(); $5.destroy(); $6.destroy();
Reid Spencere7c3c602006-11-30 06:36:44 +00001334 $$ = $1;
1335 }
1336 | GETELEMENTPTR Types ValueRef IndexList {
Reid Spencerf459d392006-12-02 16:19:52 +00001337 // Upgrade the indices
1338 for (unsigned i = 0; i < $4->size(); ++i) {
1339 ValueInfo& VI = (*$4)[i];
1340 if (VI.type.isUnsigned() && !VI.isConstant() &&
1341 VI.type.getBitWidth() < 64) {
1342 std::string* old = VI.val;
1343 *O << " %gep_upgrade" << unique << " = zext " << *old
1344 << " to ulong\n";
1345 VI.val = new std::string("ulong %gep_upgrade" + llvm::utostr(unique++));
1346 VI.type.oldTy = ULongTy;
1347 delete old;
1348 }
1349 }
1350 *$1 += " " + *$2.newTy + " " + *$3.val;
Reid Spencerf8483652006-12-02 15:16:01 +00001351 for (unsigned i = 0; i < $4->size(); ++i) {
1352 ValueInfo& VI = (*$4)[i];
1353 *$1 += ", " + *VI.val;
1354 VI.destroy();
1355 }
Reid Spencerf459d392006-12-02 16:19:52 +00001356 $2.destroy(); $3.destroy(); delete $4;
Reid Spencere7c3c602006-11-30 06:36:44 +00001357 $$ = $1;
1358 };
1359
1360%%
1361
1362int yyerror(const char *ErrorMsg) {
1363 std::string where
1364 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
1365 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
1366 std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
1367 if (yychar == YYEMPTY || yychar == 0)
1368 errMsg += "end-of-file.";
1369 else
1370 errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
1371 std::cerr << errMsg << '\n';
1372 exit(1);
1373}