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