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