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