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