Regenerate.
[oota-llvm.git] / tools / llvm-upgrade / UpgradeParser.y.cvs
1 //===-- UpgradeParser.y - Upgrade parser for llvm assmbly -------*- C++ -*-===//
2 //
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 //
10 //  This file implements the bison parser for LLVM 1.9 assembly language.
11 //
12 //===----------------------------------------------------------------------===//
13
14 %{
15 #include "ParserInternals.h"
16 #include <llvm/ADT/StringExtras.h>
17 #include <algorithm>
18 #include <map>
19 #include <utility>
20 #include <iostream>
21 #include <cassert>
22
23 #define YYERROR_VERBOSE 1
24 #define YYINCLUDED_STDLIB_H
25 #define YYDEBUG 1
26 #define UPGRADE_SETCOND_OPS 0
27
28 int yylex();                       // declaration" of xxx warnings.
29 int yyparse();
30 extern int yydebug;
31
32 static std::string CurFilename;
33 static std::ostream *O = 0;
34 std::istream* LexInput = 0;
35 unsigned SizeOfPointer = 32;
36 static uint64_t unique = 1;
37
38 typedef std::vector<TypeInfo> TypeVector;
39 static TypeVector EnumeratedTypes;
40 typedef std::map<std::string,TypeInfo> TypeMap;
41 static TypeMap NamedTypes;
42 static TypeMap Globals;
43
44 void destroy(ValueList* VL) {
45   while (!VL->empty()) {
46     ValueInfo& VI = VL->back();
47     VI.destroy();
48     VL->pop_back();
49   }
50   delete VL;
51 }
52
53 void UpgradeAssembly(const std::string &infile, std::istream& in, 
54                      std::ostream &out, bool debug)
55 {
56   Upgradelineno = 1; 
57   CurFilename = infile;
58   LexInput = &in;
59   yydebug = debug;
60   O = &out;
61
62   if (yyparse()) {
63     std::cerr << "Parse failed.\n";
64     exit(1);
65   }
66 }
67
68 static void ResolveType(TypeInfo& Ty) {
69   if (Ty.oldTy == UnresolvedTy) {
70     TypeMap::iterator I = NamedTypes.find(*Ty.newTy);
71     if (I != NamedTypes.end()) {
72       Ty.oldTy = I->second.oldTy;
73       Ty.elemTy = I->second.elemTy;
74     } else {
75       std::string msg("Can't resolve type: ");
76       msg += *Ty.newTy;
77       yyerror(msg.c_str());
78     }
79   } else if (Ty.oldTy == NumericTy) {
80     unsigned ref = atoi(&((Ty.newTy->c_str())[1])); // Skip the '\\'
81     if (ref < EnumeratedTypes.size()) {
82       Ty.oldTy = EnumeratedTypes[ref].oldTy;
83       Ty.elemTy = EnumeratedTypes[ref].elemTy;
84     } else {
85       std::string msg("Can't resolve type: ");
86       msg += *Ty.newTy;
87       yyerror(msg.c_str());
88     }
89   }
90   // otherwise its already resolved.
91 }
92
93 static const char* getCastOpcode(
94   std::string& Source, const TypeInfo& SrcTy, const TypeInfo& DstTy) 
95 {
96   unsigned SrcBits = SrcTy.getBitWidth();
97   unsigned DstBits = DstTy.getBitWidth();
98   const char* opcode = "bitcast";
99   // Run through the possibilities ...
100   if (DstTy.isIntegral()) {                        // Casting to integral
101     if (SrcTy.isIntegral()) {                      // Casting from integral
102       if (DstBits < SrcBits)
103         opcode = "trunc";
104       else if (DstBits > SrcBits) {                // its an extension
105         if (SrcTy.isSigned())
106           opcode ="sext";                          // signed -> SEXT
107         else
108           opcode = "zext";                         // unsigned -> ZEXT
109       } else {
110         opcode = "bitcast";                        // Same size, No-op cast
111       }
112     } else if (SrcTy.isFloatingPoint()) {          // Casting from floating pt
113       if (DstTy.isSigned()) 
114         opcode = "fptosi";                         // FP -> sint
115       else
116         opcode = "fptoui";                         // FP -> uint 
117     } else if (SrcTy.isPacked()) {
118       assert(DstBits == SrcTy.getBitWidth() &&
119                "Casting packed to integer of different width");
120         opcode = "bitcast";                        // same size, no-op cast
121     } else {
122       assert(SrcTy.isPointer() &&
123              "Casting from a value that is not first-class type");
124       opcode = "ptrtoint";                         // ptr -> int
125     }
126   } else if (DstTy.isFloatingPoint()) {           // Casting to floating pt
127     if (SrcTy.isIntegral()) {                     // Casting from integral
128       if (SrcTy.isSigned())
129         opcode = "sitofp";                         // sint -> FP
130       else
131         opcode = "uitofp";                         // uint -> FP
132     } else if (SrcTy.isFloatingPoint()) {         // Casting from floating pt
133       if (DstBits < SrcBits) {
134         opcode = "fptrunc";                        // FP -> smaller FP
135       } else if (DstBits > SrcBits) {
136         opcode = "fpext";                          // FP -> larger FP
137       } else  {
138         opcode ="bitcast";                         // same size, no-op cast
139       }
140     } else if (SrcTy.isPacked()) {
141       assert(DstBits == SrcTy.getBitWidth() &&
142              "Casting packed to floating point of different width");
143         opcode = "bitcast";                        // same size, no-op cast
144     } else {
145       assert(0 && "Casting pointer or non-first class to float");
146     }
147   } else if (DstTy.isPacked()) {
148     if (SrcTy.isPacked()) {
149       assert(DstTy.getBitWidth() == SrcTy.getBitWidth() &&
150              "Casting packed to packed of different widths");
151       opcode = "bitcast";                          // packed -> packed
152     } else if (DstTy.getBitWidth() == SrcBits) {
153       opcode = "bitcast";                          // float/int -> packed
154     } else {
155       assert(!"Illegal cast to packed (wrong type or size)");
156     }
157   } else if (DstTy.isPointer()) {
158     if (SrcTy.isPointer()) {
159       opcode = "bitcast";                          // ptr -> ptr
160     } else if (SrcTy.isIntegral()) {
161       opcode = "inttoptr";                         // int -> ptr
162     } else {
163       assert(!"Casting invalid type to pointer");
164     }
165   } else {
166     assert(!"Casting to type that is not first-class");
167   }
168   return opcode;
169 }
170
171 static std::string getCastUpgrade(
172   const std::string& Src, TypeInfo& SrcTy, TypeInfo& DstTy, bool isConst)
173 {
174   std::string Result;
175   std::string Source = Src;
176   if (SrcTy.isFloatingPoint() && DstTy.isPointer()) {
177     // fp -> ptr cast is no longer supported but we must upgrade this
178     // by doing a double cast: fp -> int -> ptr
179     if (isConst)
180       Source = "ulong fptoui(" + Source + " to ulong)";
181     else {
182       *O << "    %cast_upgrade" << unique << " = fptoui " << Source 
183          << " to ulong\n";
184       Source = "ulong %cast_upgrade" + llvm::utostr(unique);
185     }
186     // Update the SrcTy for the getCastOpcode call below
187     SrcTy.destroy();
188     SrcTy.newTy = new std::string("ulong");
189     SrcTy.oldTy = ULongTy;
190   } else if (DstTy.oldTy == BoolTy) {
191     // cast ptr %x to  bool was previously defined as setne ptr %x, null
192     // The ptrtoint semantic is to truncate, not compare so we must retain
193     // the original intent by replace the cast with a setne
194     const char* comparator = SrcTy.isPointer() ? ", null" : 
195       (SrcTy.isFloatingPoint() ? ", 0.0" : ", 0");
196     if (isConst) 
197       Result = "setne (" + Source + comparator + ")";
198     else
199       Result = "setne " + Source + comparator;
200     return Result; // skip cast processing below
201   }
202   ResolveType(SrcTy);
203   ResolveType(DstTy);
204   std::string Opcode(getCastOpcode(Source, SrcTy, DstTy));
205   if (isConst)
206     Result += Opcode + "( " + Source + " to " + *DstTy.newTy + ")";
207   else
208     Result += Opcode + " " + Source + " to " + *DstTy.newTy;
209   return Result;
210 }
211
212 const char* getDivRemOpcode(const std::string& opcode, const TypeInfo& TI) {
213   const char* op = opcode.c_str();
214   TypeInfo Ty = TI;
215   ResolveType(Ty);
216   if (Ty.isPacked())
217     Ty.oldTy = Ty.getElementType();
218   if (opcode == "div")
219     if (Ty.isFloatingPoint())
220       op = "fdiv";
221     else if (Ty.isUnsigned())
222       op = "udiv";
223     else if (Ty.isSigned())
224       op = "sdiv";
225     else
226       yyerror("Invalid type for div instruction");
227   else if (opcode == "rem")
228     if (Ty.isFloatingPoint())
229       op = "frem";
230     else if (Ty.isUnsigned())
231       op = "urem";
232     else if (Ty.isSigned())
233       op = "srem";
234     else
235       yyerror("Invalid type for rem instruction");
236   return op;
237 }
238
239 std::string 
240 getCompareOp(const std::string& setcc, const TypeInfo& TI) {
241   assert(setcc.length() == 5);
242   char cc1 = setcc[3];
243   char cc2 = setcc[4];
244   assert(cc1 == 'e' || cc1 == 'n' || cc1 == 'l' || cc1 == 'g');
245   assert(cc2 == 'q' || cc2 == 'e' || cc2 == 'e' || cc2 == 't');
246   std::string result("xcmp xxx");
247   result[6] = cc1;
248   result[7] = cc2;
249   if (TI.isFloatingPoint()) {
250     result[0] = 'f';
251     result[5] = 'o'; // FIXME: Always map to ordered comparison ?
252   } else if (TI.isIntegral() || TI.isPointer()) {
253     result[0] = 'i';
254     if ((cc1 == 'e' && cc2 == 'q') || (cc1 == 'n' && cc2 == 'e'))
255       result.erase(5,1);
256     else if (TI.isSigned())
257       result[5] = 's';
258     else if (TI.isUnsigned() || TI.isPointer())
259       result[5] = 'u';
260     else
261       yyerror("Invalid integral type for setcc");
262   }
263   return result;
264 }
265
266 %}
267
268 %file-prefix="UpgradeParser"
269
270 %union {
271   std::string*    String;
272   TypeInfo        Type;
273   ValueInfo       Value;
274   ConstInfo       Const;
275   ValueList*      ValList;
276 }
277
278 %token <Type>   VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
279 %token <Type>   FLOAT DOUBLE LABEL 
280 %token <String> OPAQUE ESINT64VAL EUINT64VAL SINTVAL UINTVAL FPVAL
281 %token <String> NULL_TOK UNDEF ZEROINITIALIZER TRUETOK FALSETOK
282 %token <String> TYPE VAR_ID LABELSTR STRINGCONSTANT
283 %token <String> IMPLEMENTATION BEGINTOK ENDTOK
284 %token <String> DECLARE GLOBAL CONSTANT SECTION VOLATILE
285 %token <String> TO DOTDOTDOT CONST INTERNAL LINKONCE WEAK 
286 %token <String> DLLIMPORT DLLEXPORT EXTERN_WEAK APPENDING
287 %token <String> NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG
288 %token <String> ALIGN UNINITIALIZED
289 %token <String> DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
290 %token <String> CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
291 %token <String> X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
292 %token <String> DATALAYOUT
293 %token <String> RET BR SWITCH INVOKE EXCEPT UNWIND UNREACHABLE
294 %token <String> ADD SUB MUL DIV UDIV SDIV FDIV REM UREM SREM FREM AND OR XOR
295 %token <String> SETLE SETGE SETLT SETGT SETEQ SETNE  // Binary Comparators
296 %token <String> ICMP FCMP EQ NE SLT SGT SLE SGE OEQ ONE OLT OGT OLE OGE 
297 %token <String> ORD UNO UEQ UNE ULT UGT ULE UGE
298 %token <String> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
299 %token <String> PHI_TOK SELECT SHL SHR ASHR LSHR VAARG
300 %token <String> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
301 %token <String> CAST TRUNC ZEXT SEXT FPTRUNC FPEXT FPTOUI FPTOSI UITOFP SITOFP 
302 %token <String> PTRTOINT INTTOPTR BITCAST
303
304 %type <String> OptAssign OptLinkage OptCallingConv OptAlign OptCAlign 
305 %type <String> SectionString OptSection GlobalVarAttributes GlobalVarAttribute
306 %type <String> ArgTypeListI ConstExpr DefinitionList
307 %type <String> ConstPool TargetDefinition LibrariesDefinition LibList OptName
308 %type <String> ArgVal ArgListH ArgList FunctionHeaderH BEGIN FunctionHeader END
309 %type <String> Function FunctionProto BasicBlock TypeListI
310 %type <String> InstructionList BBTerminatorInst JumpTable Inst PHIList
311 %type <String> OptTailCall InstVal OptVolatile Unwind
312 %type <String> MemoryInst SymbolicValueRef OptSideEffect GlobalType
313 %type <String> FnDeclareLinkage BasicBlockList BigOrLittle AsmBlock
314 %type <String> Name ConstValueRef ConstVector External
315 %type <String> ShiftOps SetCondOps LogicalOps ArithmeticOps CastOps
316 %type <String> IPredicates FPredicates
317
318 %type <ValList> ValueRefList ValueRefListE IndexList
319
320 %type <Type> IntType SIntType UIntType FPType TypesV Types 
321 %type <Type> PrimType UpRTypesV UpRTypes
322
323 %type <String> IntVal EInt64Val 
324 %type <Const>  ConstVal
325
326 %type <Value> ValueRef ResolvedVal 
327
328 %start Module
329
330 %%
331
332 // Handle constant integer size restriction and conversion...
333 IntVal : SINTVAL | UINTVAL ;
334 EInt64Val : ESINT64VAL | EUINT64VAL;
335
336 // Operations that are notably excluded from this list include:
337 // RET, BR, & SWITCH because they end basic blocks and are treated specially.
338 ArithmeticOps: ADD | SUB | MUL | DIV | UDIV | SDIV | FDIV 
339              | REM | UREM | SREM | FREM;
340 LogicalOps   : AND | OR | XOR;
341 SetCondOps   : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
342 IPredicates  : EQ | NE | SLT | SGT | SLE | SGE | ULT | UGT | ULE | UGE;
343 FPredicates  : OEQ | ONE | OLT | OGT | OLE | OGE | ORD | UNO | UEQ | UNE
344              | ULT | UGT | ULE | UGE | TRUETOK | FALSETOK;
345 ShiftOps     : SHL | SHR | ASHR | LSHR;
346 CastOps      : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | FPTOUI | FPTOSI | 
347                UITOFP | SITOFP | PTRTOINT | INTTOPTR | BITCAST | CAST
348              ;
349
350 // These are some types that allow classification if we only want a particular 
351 // thing... for example, only a signed, unsigned, or integral type.
352 SIntType :  LONG |  INT |  SHORT | SBYTE;
353 UIntType : ULONG | UINT | USHORT | UBYTE;
354 IntType  : SIntType | UIntType;
355 FPType   : FLOAT | DOUBLE;
356
357 // OptAssign - Value producing statements have an optional assignment component
358 OptAssign : Name '=' {
359     $$ = $1;
360   }
361   | /*empty*/ {
362     $$ = new std::string(""); 
363   };
364
365 OptLinkage 
366   : INTERNAL | LINKONCE | WEAK | APPENDING | DLLIMPORT | DLLEXPORT 
367   | EXTERN_WEAK 
368   | /*empty*/   { $$ = new std::string(""); } ;
369
370 OptCallingConv 
371   : CCC_TOK | CSRETCC_TOK | FASTCC_TOK | COLDCC_TOK | X86_STDCALLCC_TOK 
372   | X86_FASTCALLCC_TOK 
373   | CC_TOK EUINT64VAL { 
374     *$1 += *$2; 
375     delete $2;
376     $$ = $1; 
377     }
378   | /*empty*/ { $$ = new std::string(""); } ;
379
380 // OptAlign/OptCAlign - An optional alignment, and an optional alignment with
381 // a comma before it.
382 OptAlign 
383   : /*empty*/        { $$ = new std::string(); }
384   | ALIGN EUINT64VAL { *$1 += " " + *$2; delete $2; $$ = $1; };
385          ;
386 OptCAlign 
387   : /*empty*/            { $$ = new std::string(); } 
388   | ',' ALIGN EUINT64VAL { 
389     $2->insert(0, ", "); 
390     *$2 += " " + *$3;
391     delete $3;
392     $$ = $2;
393   };
394
395 SectionString 
396   : SECTION STRINGCONSTANT { 
397     *$1 += " " + *$2;
398     delete $2;
399     $$ = $1;
400   };
401
402 OptSection : /*empty*/     { $$ = new std::string(); } 
403            | SectionString;
404
405 GlobalVarAttributes 
406     : /* empty */ { $$ = new std::string(); } 
407     | ',' GlobalVarAttribute GlobalVarAttributes  {
408       $2->insert(0, ", ");
409       if (!$3->empty())
410         *$2 += " " + *$3;
411       delete $3;
412       $$ = $2;
413     };
414
415 GlobalVarAttribute 
416     : SectionString 
417     | ALIGN EUINT64VAL {
418       *$1 += " " + *$2;
419       delete $2;
420       $$ = $1;
421     };
422
423 //===----------------------------------------------------------------------===//
424 // Types includes all predefined types... except void, because it can only be
425 // used in specific contexts (function returning void for example).  To have
426 // access to it, a user must explicitly use TypesV.
427 //
428
429 // TypesV includes all of 'Types', but it also includes the void type.
430 TypesV    : Types    | VOID ;
431 UpRTypesV : UpRTypes | VOID ; 
432 Types     : UpRTypes ;
433
434 // Derived types are added later...
435 //
436 PrimType : BOOL | SBYTE | UBYTE | SHORT  | USHORT | INT   | UINT ;
437 PrimType : LONG | ULONG | FLOAT | DOUBLE | LABEL;
438 UpRTypes 
439   : OPAQUE { 
440     $$.newTy = $1; 
441     $$.oldTy = OpaqueTy; 
442   } 
443   | SymbolicValueRef { 
444     $$.newTy = $1;
445     $$.oldTy = UnresolvedTy;
446   }
447   | PrimType { 
448     $$ = $1; 
449   }
450   | '\\' EUINT64VAL {                   // Type UpReference
451     $2->insert(0, "\\");
452     $$.newTy = $2;
453     $$.oldTy = NumericTy;
454   }
455   | UpRTypesV '(' ArgTypeListI ')' {           // Function derived type?
456     *$1.newTy += "( " + *$3 + " )";
457     delete $3;
458     $$.newTy = $1.newTy;
459     $$.oldTy = FunctionTy;
460   }
461   | '[' EUINT64VAL 'x' UpRTypes ']' {          // Sized array type?
462     $2->insert(0,"[ ");
463     *$2 += " x " + *$4.newTy + " ]";
464     delete $4.newTy;
465     $$.newTy = $2;
466     $$.oldTy = ArrayTy;
467     $$.elemTy = $4.oldTy;
468   }
469   | '<' EUINT64VAL 'x' UpRTypes '>' {          // Packed array type?
470     $2->insert(0,"< ");
471     *$2 += " x " + *$4.newTy + " >";
472     delete $4.newTy;
473     $$.newTy = $2;
474     $$.oldTy = PackedTy;
475     $$.elemTy = $4.oldTy;
476   }
477   | '{' TypeListI '}' {                        // Structure type?
478     $2->insert(0, "{ ");
479     *$2 += " }";
480     $$.newTy = $2;
481     $$.oldTy = StructTy;
482   }
483   | '{' '}' {                                  // Empty structure type?
484     $$.newTy = new std::string("{}");
485     $$.oldTy = StructTy;
486   }
487   | UpRTypes '*' {                             // Pointer type?
488     *$1.newTy += '*';
489     $$.elemTy = $1.oldTy;
490     $1.oldTy = PointerTy;
491     $$ = $1;
492   };
493
494 // TypeList - Used for struct declarations and as a basis for function type 
495 // declaration type lists
496 //
497 TypeListI 
498   : UpRTypes {
499     $$ = $1.newTy;
500   }
501   | TypeListI ',' UpRTypes {
502     *$1 += ", " + *$3.newTy;
503     delete $3.newTy;
504     $$ = $1;
505   };
506
507 // ArgTypeList - List of types for a function type declaration...
508 ArgTypeListI 
509   : TypeListI 
510   | TypeListI ',' DOTDOTDOT {
511     *$1 += ", ...";
512     delete $3;
513     $$ = $1;
514   }
515   | DOTDOTDOT {
516     $$ = $1;
517   }
518   | /*empty*/ {
519     $$ = new std::string();
520   };
521
522 // ConstVal - The various declarations that go into the constant pool.  This
523 // production is used ONLY to represent constants that show up AFTER a 'const',
524 // 'constant' or 'global' token at global scope.  Constants that can be inlined
525 // into other expressions (such as integers and constexprs) are handled by the
526 // ResolvedVal, ValueRef and ConstValueRef productions.
527 //
528 ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
529     $$.type = $1;
530     $$.cnst = new std::string(*$1.newTy);
531     *$$.cnst += " [ " + *$3 + " ]";
532     delete $3;
533   }
534   | Types '[' ']' {
535     $$.type = $1;
536     $$.cnst = new std::string(*$1.newTy);
537     *$$.cnst += "[ ]";
538   }
539   | Types 'c' STRINGCONSTANT {
540     $$.type = $1;
541     $$.cnst = new std::string(*$1.newTy);
542     *$$.cnst += " c" + *$3;
543     delete $3;
544   }
545   | Types '<' ConstVector '>' { // Nonempty unsized arr
546     $$.type = $1;
547     $$.cnst = new std::string(*$1.newTy);
548     *$$.cnst += " < " + *$3 + " >";
549     delete $3;
550   }
551   | Types '{' ConstVector '}' {
552     $$.type = $1;
553     $$.cnst = new std::string(*$1.newTy);
554     *$$.cnst += " { " + *$3 + " }";
555     delete $3;
556   }
557   | Types '{' '}' {
558     $$.type = $1;
559     $$.cnst = new std::string(*$1.newTy);
560     *$$.cnst += " {}";
561   }
562   | Types NULL_TOK {
563     $$.type = $1;
564     $$.cnst = new std::string(*$1.newTy);
565     *$$.cnst +=  " " + *$2;
566     delete $2;
567   }
568   | Types UNDEF {
569     $$.type = $1;
570     $$.cnst = new std::string(*$1.newTy);
571     *$$.cnst += " " + *$2;
572     delete $2;
573   }
574   | Types SymbolicValueRef {
575     $$.type = $1;
576     $$.cnst = new std::string(*$1.newTy);
577     *$$.cnst += " " + *$2;
578     delete $2;
579   }
580   | Types ConstExpr {
581     $$.type = $1;
582     $$.cnst = new std::string(*$1.newTy);
583     *$$.cnst += " " + *$2;
584     delete $2;
585   }
586   | Types ZEROINITIALIZER {
587     $$.type = $1;
588     $$.cnst = new std::string(*$1.newTy);
589     *$$.cnst += " " + *$2;
590     delete $2;
591   }
592   | SIntType EInt64Val {      // integral constants
593     $$.type = $1;
594     $$.cnst = new std::string(*$1.newTy);
595     *$$.cnst += " " + *$2;
596     delete $2;
597   }
598   | UIntType EUINT64VAL {            // integral constants
599     $$.type = $1;
600     $$.cnst = new std::string(*$1.newTy);
601     *$$.cnst += " " + *$2;
602     delete $2;
603   }
604   | BOOL TRUETOK {                      // Boolean constants
605     $$.type = $1;
606     $$.cnst = new std::string(*$1.newTy);
607     *$$.cnst += " " + *$2;
608     delete $2;
609   }
610   | BOOL FALSETOK {                     // Boolean constants
611     $$.type = $1;
612     $$.cnst = new std::string(*$1.newTy);
613     *$$.cnst += " " + *$2;
614     delete $2;
615   }
616   | FPType FPVAL {                   // Float & Double constants
617     $$.type = $1;
618     $$.cnst = new std::string(*$1.newTy);
619     *$$.cnst += " " + *$2;
620     delete $2;
621   };
622
623
624 ConstExpr: CastOps '(' ConstVal TO Types ')' {
625     std::string source = *$3.cnst;
626     TypeInfo DstTy = $5;
627     ResolveType(DstTy);
628     if (*$1 == "cast") {
629       // Call getCastUpgrade to upgrade the old cast
630       $$ = new std::string(getCastUpgrade(source, $3.type, $5, true));
631     } else {
632       // Nothing to upgrade, just create the cast constant expr
633       $$ = new std::string(*$1);
634       *$$ += "( " + source + " to " + *$5.newTy + ")";
635     }
636     delete $1; $3.destroy(); delete $4; $5.destroy();
637   }
638   | GETELEMENTPTR '(' ConstVal IndexList ')' {
639     *$1 += "(" + *$3.cnst;
640     for (unsigned i = 0; i < $4->size(); ++i) {
641       ValueInfo& VI = (*$4)[i];
642       *$1 += ", " + *VI.val;
643       VI.destroy();
644     }
645     *$1 += ")";
646     $$ = $1;
647     $3.destroy();
648     delete $4;
649   }
650   | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
651     *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
652     $3.destroy(); $5.destroy(); $7.destroy();
653     $$ = $1;
654   }
655   | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
656     const char* op = getDivRemOpcode(*$1, $3.type); 
657     $$ = new std::string(op);
658     *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
659     delete $1; $3.destroy(); $5.destroy();
660   }
661   | LogicalOps '(' ConstVal ',' ConstVal ')' {
662     *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
663     $3.destroy(); $5.destroy();
664     $$ = $1;
665   }
666   | SetCondOps '(' ConstVal ',' ConstVal ')' {
667 #if UPGRADE_SETCOND_OPS
668     *$1 = getCompareOp(*$1, $3.type);
669 #endif
670     *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
671     $3.destroy(); $5.destroy();
672     $$ = $1;
673   }
674   | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
675     *$1 += "(" + *$2 + "," + *$4.cnst + "," + *$6.cnst + ")";
676     delete $2; $4.destroy(); $6.destroy();
677     $$ = $1;
678   }
679   | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
680     *$1 += "(" + *$2 + "," + *$4.cnst + "," + *$6.cnst + ")";
681     delete $2; $4.destroy(); $6.destroy();
682     $$ = $1;
683   }
684   | ShiftOps '(' ConstVal ',' ConstVal ')' {
685     const char* shiftop = $1->c_str();
686     if (*$1 == "shr")
687       shiftop = ($3.type.isUnsigned()) ? "lshr" : "ashr";
688     $$ = new std::string(shiftop);
689     *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
690     delete $1; $3.destroy(); $5.destroy();
691   }
692   | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
693     *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
694     $3.destroy(); $5.destroy();
695     $$ = $1;
696   }
697   | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
698     *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
699     $3.destroy(); $5.destroy(); $7.destroy();
700     $$ = $1;
701   }
702   | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
703     *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
704     $3.destroy(); $5.destroy(); $7.destroy();
705     $$ = $1;
706   };
707
708
709 // ConstVector - A list of comma separated constants.
710
711 ConstVector 
712   : ConstVector ',' ConstVal {
713     *$1 += ", " + *$3.cnst;
714     $3.destroy();
715     $$ = $1;
716   }
717   | ConstVal { $$ = new std::string(*$1.cnst); $1.destroy(); }
718   ;
719
720
721 // GlobalType - Match either GLOBAL or CONSTANT for global declarations...
722 GlobalType : GLOBAL | CONSTANT ;
723
724
725 //===----------------------------------------------------------------------===//
726 //                             Rules to match Modules
727 //===----------------------------------------------------------------------===//
728
729 // Module rule: Capture the result of parsing the whole file into a result
730 // variable...
731 //
732 Module : DefinitionList {
733 };
734
735 // DefinitionList - Top level definitions
736 //
737 DefinitionList : DefinitionList Function {
738     $$ = 0;
739   } 
740   | DefinitionList FunctionProto {
741     *O << *$2 << "\n";
742     delete $2;
743     $$ = 0;
744   }
745   | DefinitionList MODULE ASM_TOK AsmBlock {
746     *O << "module asm " << " " << *$4 << "\n";
747     $$ = 0;
748   }  
749   | DefinitionList IMPLEMENTATION {
750     *O << "implementation\n";
751     $$ = 0;
752   }
753   | ConstPool { $$ = 0; }
754
755 External : EXTERNAL | UNINITIALIZED { $$ = $1; *$$ = "external"; }
756
757 // ConstPool - Constants with optional names assigned to them.
758 ConstPool : ConstPool OptAssign TYPE TypesV {
759     EnumeratedTypes.push_back($4);
760     if (!$2->empty()) {
761       NamedTypes[*$2].newTy = new std::string(*$4.newTy);
762       NamedTypes[*$2].oldTy = $4.oldTy;
763       NamedTypes[*$2].elemTy = $4.elemTy;
764       *O << *$2 << " = ";
765     }
766     *O << "type " << *$4.newTy << "\n";
767     delete $2; delete $3; $4.destroy();
768     $$ = 0;
769   }
770   | ConstPool FunctionProto {       // Function prototypes can be in const pool
771     *O << *$2 << "\n";
772     delete $2;
773     $$ = 0;
774   }
775   | ConstPool MODULE ASM_TOK AsmBlock {  // Asm blocks can be in the const pool
776     *O << *$2 << " " << *$3 << " " << *$4 << "\n";
777     delete $2; delete $3; delete $4; 
778     $$ = 0;
779   }
780   | ConstPool OptAssign OptLinkage GlobalType ConstVal  GlobalVarAttributes {
781     if (!$2->empty()) {
782       *O << *$2 << " = ";
783       Globals[*$2] = $5.type.clone();
784     }
785     *O << *$3 << " " << *$4 << " " << *$5.cnst << " " << *$6 << "\n";
786     delete $2; delete $3; delete $4; $5.destroy(); delete $6; 
787     $$ = 0;
788   }
789   | ConstPool OptAssign External GlobalType Types  GlobalVarAttributes {
790     if (!$2->empty()) {
791       *O << *$2 << " = ";
792       Globals[*$2] = $5.clone();
793     }
794     *O <<  *$3 << " " << *$4 << " " << *$5.newTy << " " << *$6 << "\n";
795     delete $2; delete $3; delete $4; $5.destroy(); delete $6;
796     $$ = 0;
797   }
798   | ConstPool OptAssign DLLIMPORT GlobalType Types  GlobalVarAttributes {
799     if (!$2->empty()) {
800       *O << *$2 << " = ";
801       Globals[*$2] = $5.clone();
802     }
803     *O << *$3 << " " << *$4 << " " << *$5.newTy << " " << *$6 << "\n";
804     delete $2; delete $3; delete $4; $5.destroy(); delete $6;
805     $$ = 0;
806   }
807   | ConstPool OptAssign EXTERN_WEAK GlobalType Types  GlobalVarAttributes {
808     if (!$2->empty()) {
809       *O << *$2 << " = ";
810       Globals[*$2] = $5.clone();
811     }
812     *O << *$3 << " " << *$4 << " " << *$5.newTy << " " << *$6 << "\n";
813     delete $2; delete $3; delete $4; $5.destroy(); delete $6;
814     $$ = 0;
815   }
816   | ConstPool TARGET TargetDefinition { 
817     *O << *$2 << " " << *$3 << "\n";
818     delete $2; delete $3;
819     $$ = 0;
820   }
821   | ConstPool DEPLIBS '=' LibrariesDefinition {
822     *O << *$2 << " = " << *$4 << "\n";
823     delete $2; delete $4;
824     $$ = 0;
825   }
826   | /* empty: end of list */ { 
827     $$ = 0;
828   };
829
830
831 AsmBlock : STRINGCONSTANT ;
832
833 BigOrLittle : BIG | LITTLE 
834
835 TargetDefinition 
836   : ENDIAN '=' BigOrLittle {
837     *$1 += " = " + *$3;
838     delete $3;
839     $$ = $1;
840   }
841   | POINTERSIZE '=' EUINT64VAL {
842     *$1 += " = " + *$3;
843     if (*$3 == "64")
844       SizeOfPointer = 64;
845     delete $3;
846     $$ = $1;
847   }
848   | TRIPLE '=' STRINGCONSTANT {
849     *$1 += " = " + *$3;
850     delete $3;
851     $$ = $1;
852   }
853   | DATALAYOUT '=' STRINGCONSTANT {
854     *$1 += " = " + *$3;
855     delete $3;
856     $$ = $1;
857   };
858
859 LibrariesDefinition 
860   : '[' LibList ']' {
861     $2->insert(0, "[ ");
862     *$2 += " ]";
863     $$ = $2;
864   };
865
866 LibList 
867   : LibList ',' STRINGCONSTANT {
868     *$1 += ", " + *$3;
869     delete $3;
870     $$ = $1;
871   }
872   | STRINGCONSTANT 
873   | /* empty: end of list */ {
874     $$ = new std::string();
875   };
876
877 //===----------------------------------------------------------------------===//
878 //                       Rules to match Function Headers
879 //===----------------------------------------------------------------------===//
880
881 Name : VAR_ID | STRINGCONSTANT;
882 OptName : Name | /*empty*/ { $$ = new std::string(); };
883
884 ArgVal : Types OptName {
885   $$ = $1.newTy;
886   if (!$2->empty())
887     *$$ += " " + *$2;
888   delete $2;
889 };
890
891 ArgListH : ArgListH ',' ArgVal {
892     *$1 += ", " + *$3;
893     delete $3;
894   }
895   | ArgVal {
896     $$ = $1;
897   };
898
899 ArgList : ArgListH {
900     $$ = $1;
901   }
902   | ArgListH ',' DOTDOTDOT {
903     *$1 += ", ...";
904     $$ = $1;
905     delete $3;
906   }
907   | DOTDOTDOT {
908     $$ = $1;
909   }
910   | /* empty */ { $$ = new std::string(); };
911
912 FunctionHeaderH : OptCallingConv TypesV Name '(' ArgList ')' 
913                   OptSection OptAlign {
914     if (!$1->empty()) {
915       *$1 += " ";
916     }
917     *$1 += *$2.newTy + " " + *$3 + "(" + *$5 + ")";
918     if (!$7->empty()) {
919       *$1 += " " + *$7;
920     }
921     if (!$8->empty()) {
922       *$1 += " " + *$8;
923     }
924     $2.destroy();
925     delete $3;
926     delete $5;
927     delete $7;
928     delete $8;
929     $$ = $1;
930   };
931
932 BEGIN : BEGINTOK { $$ = new std::string("{"); delete $1; }
933   | '{' { $$ = new std::string ("{"); }
934
935 FunctionHeader : OptLinkage FunctionHeaderH BEGIN {
936   if (!$1->empty()) {
937     *O << *$1 << " ";
938   }
939   *O << *$2 << " " << *$3 << "\n";
940   delete $1; delete $2; delete $3;
941   $$ = 0;
942 };
943
944 END : ENDTOK { $$ = new std::string("}"); delete $1; }
945     | '}' { $$ = new std::string("}"); };
946
947 Function : FunctionHeader BasicBlockList END {
948   if ($2)
949     *O << *$2;
950   *O << '\n' << *$3 << "\n";
951   $$ = 0;
952 };
953
954 FnDeclareLinkage
955   : /*default*/ { $$ = new std::string(); }
956   | DLLIMPORT    
957   | EXTERN_WEAK 
958   ;
959   
960 FunctionProto 
961   : DECLARE FnDeclareLinkage FunctionHeaderH { 
962     if (!$2->empty())
963       *$1 += " " + *$2;
964     *$1 += " " + *$3;
965     delete $2;
966     delete $3;
967     $$ = $1;
968   };
969
970 //===----------------------------------------------------------------------===//
971 //                        Rules to match Basic Blocks
972 //===----------------------------------------------------------------------===//
973
974 OptSideEffect : /* empty */ { $$ = new std::string(); }
975   | SIDEEFFECT;
976
977 ConstValueRef 
978   : ESINT64VAL | EUINT64VAL | FPVAL | TRUETOK | FALSETOK | NULL_TOK | UNDEF
979   | ZEROINITIALIZER 
980   | '<' ConstVector '>' { 
981     $2->insert(0, "<");
982     *$2 += ">";
983     $$ = $2;
984   }
985   | ConstExpr 
986   | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
987     if (!$2->empty()) {
988       *$1 += " " + *$2;
989     }
990     *$1 += " " + *$3 + ", " + *$5;
991     delete $2; delete $3; delete $5;
992     $$ = $1;
993   };
994
995 SymbolicValueRef : IntVal | Name ;
996
997 // ValueRef - A reference to a definition... either constant or symbolic
998 ValueRef 
999   : SymbolicValueRef {
1000     $$.val = $1;
1001     $$.constant = false;
1002     $$.type.newTy = 0;
1003     $$.type.oldTy = UnresolvedTy;
1004   }
1005   | ConstValueRef {
1006     $$.val = $1;
1007     $$.constant = true;
1008     $$.type.newTy = 0;
1009     $$.type.oldTy = UnresolvedTy;
1010   }
1011   ;
1012
1013 // ResolvedVal - a <type> <value> pair.  This is used only in cases where the
1014 // type immediately preceeds the value reference, and allows complex constant
1015 // pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
1016 ResolvedVal : Types ValueRef {
1017     $$ = $2;
1018     $$.type = $1;
1019     $$.val->insert(0, *$1.newTy + " ");
1020   };
1021
1022 BasicBlockList : BasicBlockList BasicBlock {
1023     $$ = 0;
1024   }
1025   | BasicBlock { // Do not allow functions with 0 basic blocks   
1026     $$ = 0;
1027   };
1028
1029
1030 // Basic blocks are terminated by branching instructions: 
1031 // br, br/cc, switch, ret
1032 //
1033 BasicBlock : InstructionList BBTerminatorInst  {
1034     $$ = 0;
1035   };
1036
1037 InstructionList : InstructionList Inst {
1038     *O << "    " << *$2 << "\n";
1039     delete $2;
1040     $$ = 0;
1041   }
1042   | /* empty */ {
1043     $$ = 0;
1044   }
1045   | LABELSTR {
1046     *O << *$1 << "\n";
1047     delete $1;
1048     $$ = 0;
1049   };
1050
1051 Unwind : UNWIND | EXCEPT { $$ = $1; *$$ = "unwind"; }
1052
1053 BBTerminatorInst : RET ResolvedVal {              // Return with a result...
1054     *O << "    " << *$1 << " " << *$2.val << "\n";
1055     delete $1; $2.destroy();
1056     $$ = 0;
1057   }
1058   | RET VOID {                                       // Return with no result...
1059     *O << "    " << *$1 << " " << *$2.newTy << "\n";
1060     delete $1; $2.destroy();
1061     $$ = 0;
1062   }
1063   | BR LABEL ValueRef {                         // Unconditional Branch...
1064     *O << "    " << *$1 << " " << *$2.newTy << " " << *$3.val << "\n";
1065     delete $1; $2.destroy(); $3.destroy();
1066     $$ = 0;
1067   }                                                  // Conditional Branch...
1068   | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {  
1069     *O << "    " << *$1 << " " << *$2.newTy << " " << *$3.val << ", " 
1070        << *$5.newTy << " " << *$6.val << ", " << *$8.newTy << " " 
1071        << *$9.val << "\n";
1072     delete $1; $2.destroy(); $3.destroy(); $5.destroy(); $6.destroy(); 
1073     $8.destroy(); $9.destroy();
1074     $$ = 0;
1075   }
1076   | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1077     *O << "    " << *$1 << " " << *$2.newTy << " " << *$3.val << ", " 
1078        << *$5.newTy << " " << *$6.val << " [" << *$8 << " ]\n";
1079     delete $1; $2.destroy(); $3.destroy(); $5.destroy(); $6.destroy(); 
1080     delete $8;
1081     $$ = 0;
1082   }
1083   | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
1084     *O << "    " << *$1 << " " << *$2.newTy << " " << *$3.val << ", " 
1085        << *$5.newTy << " " << *$6.val << "[]\n";
1086     delete $1; $2.destroy(); $3.destroy(); $5.destroy(); $6.destroy();
1087     $$ = 0;
1088   }
1089   | OptAssign INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
1090     TO LABEL ValueRef Unwind LABEL ValueRef {
1091     *O << "    ";
1092     if (!$1->empty())
1093       *O << *$1 << " = ";
1094     *O << *$2 << " " << *$3 << " " << *$4.newTy << " " << *$5.val << " (";
1095     for (unsigned i = 0; i < $7->size(); ++i) {
1096       ValueInfo& VI = (*$7)[i];
1097       *O << *VI.val;
1098       if (i+1 < $7->size())
1099         *O << ", ";
1100       VI.destroy();
1101     }
1102     *O << ") " << *$9 << " " << *$10.newTy << " " << *$11.val << " " 
1103        << *$12 << " " << *$13.newTy << " " << *$14.val << "\n";
1104     delete $1; delete $2; delete $3; $4.destroy(); $5.destroy(); delete $7; 
1105     delete $9; $10.destroy(); $11.destroy(); delete $12; $13.destroy(); 
1106     $14.destroy(); 
1107     $$ = 0;
1108   }
1109   | Unwind {
1110     *O << "    " << *$1 << "\n";
1111     delete $1;
1112     $$ = 0;
1113   }
1114   | UNREACHABLE {
1115     *O << "    " << *$1 << "\n";
1116     delete $1;
1117     $$ = 0;
1118   };
1119
1120 JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1121     *$1 += " " + *$2.newTy + " " + *$3 + ", " + *$5.newTy + " " + *$6.val;
1122     $2.destroy(); delete $3; $5.destroy(); $6.destroy();
1123     $$ = $1;
1124   }
1125   | IntType ConstValueRef ',' LABEL ValueRef {
1126     $2->insert(0, *$1.newTy + " " );
1127     *$2 += ", " + *$4.newTy + " " + *$5.val;
1128     $1.destroy(); $4.destroy(); $5.destroy();
1129     $$ = $2;
1130   };
1131
1132 Inst 
1133   : OptAssign InstVal {
1134     if (!$1->empty())
1135       *$1 += " = ";
1136     *$1 += *$2;
1137     delete $2;
1138     $$ = $1; 
1139   };
1140
1141 PHIList 
1142   : Types '[' ValueRef ',' ValueRef ']' {    // Used for PHI nodes
1143     $3.val->insert(0, *$1.newTy + "[");
1144     *$3.val += "," + *$5.val + "]";
1145     $1.destroy(); $5.destroy();
1146     $$ = new std::string(*$3.val);
1147     $3.destroy();
1148   }
1149   | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1150     *$1 += ", [" + *$4.val + "," + *$6.val + "]";
1151     $4.destroy(); $6.destroy();
1152     $$ = $1;
1153   };
1154
1155
1156 ValueRefList 
1157   : ResolvedVal { 
1158     $$ = new ValueList();
1159     $$->push_back($1);
1160   }
1161   | ValueRefList ',' ResolvedVal {
1162     $1->push_back($3);
1163     $$ = $1;
1164   };
1165
1166 // ValueRefListE - Just like ValueRefList, except that it may also be empty!
1167 ValueRefListE 
1168   : ValueRefList  { $$ = $1; }
1169   | /*empty*/ { $$ = new ValueList(); }
1170   ;
1171
1172 OptTailCall 
1173   : TAIL CALL {
1174     *$1 += " " + *$2;
1175     delete $2;
1176     $$ = $1;
1177   }
1178   | CALL 
1179   ;
1180
1181 InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1182     const char* op = getDivRemOpcode(*$1, $2); 
1183     $$ = new std::string(op);
1184     *$$ += " " + *$2.newTy + " " + *$3.val + ", " + *$5.val;
1185     delete $1; $2.destroy(); $3.destroy(); $5.destroy();
1186   }
1187   | LogicalOps Types ValueRef ',' ValueRef {
1188     *$1 += " " + *$2.newTy + " " + *$3.val + ", " + *$5.val;
1189     $2.destroy(); $3.destroy(); $5.destroy();
1190     $$ = $1;
1191   }
1192   | SetCondOps Types ValueRef ',' ValueRef {
1193 #if UPGRADE_SETCOND_OPS
1194     *$1 = getCompareOp(*$1, $2);
1195 #endif
1196     *$1 += " " + *$2.newTy + " " + *$3.val + ", " + *$5.val;
1197     $2.destroy(); $3.destroy(); $5.destroy();
1198     $$ = $1;
1199   }
1200   | ICMP IPredicates Types ValueRef ',' ValueRef ')' {
1201     *$1 += " " + *$2 + " " + *$4.val + "," + *$6.val + ")";
1202     delete $2; $4.destroy(); $6.destroy();
1203     $$ = $1;
1204   }
1205   | FCMP FPredicates Types ValueRef ',' ValueRef ')' {
1206     *$1 += " " + *$2 + " " + *$4.val + "," + *$6.val + ")";
1207     delete $2; $4.destroy(); $6.destroy();
1208     $$ = $1;
1209   }
1210   | NOT ResolvedVal {
1211     *$1 += " " + *$2.val;
1212     $2.destroy();
1213     $$ = $1;
1214   }
1215   | ShiftOps ResolvedVal ',' ResolvedVal {
1216     const char* shiftop = $1->c_str();
1217     if (*$1 == "shr")
1218       shiftop = ($2.type.isUnsigned()) ? "lshr" : "ashr";
1219     $$ = new std::string(shiftop);
1220     *$$ += " " + *$2.val + ", " + *$4.val;
1221     delete $1; $2.destroy(); $4.destroy();
1222   }
1223   | CastOps ResolvedVal TO Types {
1224     std::string source = *$2.val;
1225     TypeInfo SrcTy = $2.type;
1226     TypeInfo DstTy = $4;
1227     ResolveType(DstTy);
1228     $$ = new std::string();
1229     if (*$1 == "cast") {
1230       *$$ +=  getCastUpgrade(source, SrcTy, DstTy, false);
1231     } else {
1232       *$$ += *$1 + " " + source + " to " + *DstTy.newTy;
1233     }
1234     delete $1; $2.destroy();
1235     delete $3; $4.destroy();
1236   }
1237   | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1238     *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1239     $2.destroy(); $4.destroy(); $6.destroy();
1240     $$ = $1;
1241   }
1242   | VAARG ResolvedVal ',' Types {
1243     *$1 += " " + *$2.val + ", " + *$4.newTy;
1244     $2.destroy(); $4.destroy();
1245     $$ = $1;
1246   }
1247   | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
1248     *$1 += " " + *$2.val + ", " + *$4.val;
1249     $2.destroy(); $4.destroy();
1250     $$ = $1;
1251   }
1252   | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1253     *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1254     $2.destroy(); $4.destroy(); $6.destroy();
1255     $$ = $1;
1256   }
1257   | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1258     *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1259     $2.destroy(); $4.destroy(); $6.destroy();
1260     $$ = $1;
1261   }
1262   | PHI_TOK PHIList {
1263     *$1 += " " + *$2;
1264     delete $2;
1265     $$ = $1;
1266   }
1267   | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')'  {
1268     if (!$2->empty())
1269       *$1 += " " + *$2;
1270     if (!$1->empty())
1271       *$1 += " ";
1272     *$1 += *$3.newTy + " " + *$4.val + "(";
1273     for (unsigned i = 0; i < $6->size(); ++i) {
1274       ValueInfo& VI = (*$6)[i];
1275       *$1 += *VI.val;
1276       if (i+1 < $6->size())
1277         *$1 += ", ";
1278       VI.destroy();
1279     }
1280     *$1 += ")";
1281     delete $2; $3.destroy(); $4.destroy(); delete $6;
1282     $$ = $1;
1283   }
1284   | MemoryInst ;
1285
1286
1287 // IndexList - List of indices for GEP based instructions...
1288 IndexList 
1289   : ',' ValueRefList { $$ = $2; }
1290   | /* empty */ {  $$ = new ValueList(); }
1291   ;
1292
1293 OptVolatile 
1294   : VOLATILE 
1295   | /* empty */ { $$ = new std::string(); }
1296   ;
1297
1298 MemoryInst : MALLOC Types OptCAlign {
1299     *$1 += " " + *$2.newTy;
1300     if (!$3->empty())
1301       *$1 += " " + *$3;
1302     $2.destroy(); delete $3;
1303     $$ = $1;
1304   }
1305   | MALLOC Types ',' UINT ValueRef OptCAlign {
1306     *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5.val;
1307     if (!$6->empty())
1308       *$1 += " " + *$6;
1309     $2.destroy(); $4.destroy(); $5.destroy(); delete $6;
1310     $$ = $1;
1311   }
1312   | ALLOCA Types OptCAlign {
1313     *$1 += " " + *$2.newTy;
1314     if (!$3->empty())
1315       *$1 += " " + *$3;
1316     $2.destroy(); delete $3;
1317     $$ = $1;
1318   }
1319   | ALLOCA Types ',' UINT ValueRef OptCAlign {
1320     *$1 += " " + *$2.newTy + ", " + *$4.newTy + " " + *$5.val;
1321     if (!$6->empty())
1322       *$1 += " " + *$6;
1323     $2.destroy(); $4.destroy(); $5.destroy(); delete $6;
1324     $$ = $1;
1325   }
1326   | FREE ResolvedVal {
1327     *$1 += " " + *$2.val;
1328     $2.destroy();
1329     $$ = $1;
1330   }
1331   | OptVolatile LOAD Types ValueRef {
1332     if (!$1->empty())
1333       *$1 += " ";
1334     *$1 += *$2 + " " + *$3.newTy + " " + *$4.val;
1335     delete $2; $3.destroy(); $4.destroy();
1336     $$ = $1;
1337   }
1338   | OptVolatile STORE ResolvedVal ',' Types ValueRef {
1339     if (!$1->empty())
1340       *$1 += " ";
1341     *$1 += *$2 + " " + *$3.val + ", " + *$5.newTy + " " + *$6.val;
1342     delete $2; $3.destroy(); $5.destroy(); $6.destroy();
1343     $$ = $1;
1344   }
1345   | GETELEMENTPTR Types ValueRef IndexList {
1346     // Upgrade the indices
1347     for (unsigned i = 0; i < $4->size(); ++i) {
1348       ValueInfo& VI = (*$4)[i];
1349       if (VI.type.isUnsigned() && !VI.isConstant() && 
1350           VI.type.getBitWidth() < 64) {
1351         std::string* old = VI.val;
1352         *O << "    %gep_upgrade" << unique << " = zext " << *old 
1353            << " to ulong\n";
1354         VI.val = new std::string("ulong %gep_upgrade" + llvm::utostr(unique++));
1355         VI.type.oldTy = ULongTy;
1356         delete old;
1357       }
1358     }
1359     *$1 += " " + *$2.newTy + " " + *$3.val;
1360     for (unsigned i = 0; i < $4->size(); ++i) {
1361       ValueInfo& VI = (*$4)[i];
1362       *$1 += ", " + *VI.val;
1363       VI.destroy();
1364     }
1365     $2.destroy(); $3.destroy(); delete $4;
1366     $$ = $1;
1367   };
1368
1369 %%
1370
1371 int yyerror(const char *ErrorMsg) {
1372   std::string where 
1373     = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
1374                   + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
1375   std::string errMsg = std::string(ErrorMsg) + "\n" + where + " while reading ";
1376   if (yychar == YYEMPTY || yychar == 0)
1377     errMsg += "end-of-file.";
1378   else
1379     errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
1380   std::cerr << errMsg << '\n';
1381   exit(1);
1382 }