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 "UpgradeInternals.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
34
35 // This bool controls whether attributes are ever added to function declarations
36 // definitions and calls.
37 static bool AddAttributes = false;
38
39 static void warning(const std::string& msg);
40
41 void UpgradeAssembly(const std::string &infile, std::istream& in, 
42                      std::ostream &out, bool debug, bool addAttrs)
43 {
44   Upgradelineno = 1; 
45   CurFilename = infile;
46   LexInput = &in;
47   yydebug = debug;
48   AddAttributes = addAttrs;
49   O = &out;
50
51   if (yyparse()) {
52     std::cerr << "llvm-upgrade: parse failed.\n";
53     out << "llvm-upgrade: parse failed.\n";
54     exit(1);
55   }
56 }
57
58 namespace { // Anonymous namespace to keep our implementation local
59
60
61 /// This type is used to keep track of the signedness of values. Instead
62 /// of creating llvm::Value directly, the parser will create ValueInfo which
63 /// associates a Value* with a Signedness indication.
64 struct ValueInfo {
65   std::string* val;
66   const TypeInfo* type;
67   bool constant;
68   bool isConstant() const { return constant; }
69   ~ValueInfo() { delete val; }
70 };
71
72
73 /// This type is used to keep track of the signedness of the obsolete
74 /// integer types. Instead of creating an llvm::Type directly, the Lexer will
75 /// create instances of TypeInfo which retains the signedness indication so
76 /// it can be used by the parser for upgrade decisions.
77 /// For example if "uint" is encountered then the "first" field will be set 
78 /// to "int32" and the "second" field will be set to "isUnsigned".  If the 
79 /// type is not obsolete then "second" will be set to "isSignless".
80 class TypeInfo {
81 public:
82   static const TypeInfo* get(const std::string &newType, Types oldType);
83   static const TypeInfo* get(const std::string& newType, Types oldType, 
84                              const TypeInfo* eTy, const TypeInfo* rTy);
85
86   static const TypeInfo* get(const std::string& newType, Types oldType, 
87                              const TypeInfo *eTy, uint64_t elems);
88
89   static const TypeInfo* get(const std::string& newType, Types oldType, 
90                              TypeList* TL);
91
92   static const TypeInfo* get(const std::string& newType, const TypeInfo* resTy, 
93                              TypeList* TL);
94
95   const TypeInfo* resolve() const;
96   bool operator<(const TypeInfo& that) const;
97
98   bool sameNewTyAs(const TypeInfo* that) const {
99     return this->newTy == that->newTy;
100   }
101
102   bool sameOldTyAs(const TypeInfo* that) const;
103
104   Types getElementTy() const {
105     if (elemTy) {
106       return elemTy->oldTy;
107     }
108     return UnresolvedTy;
109   }
110
111   unsigned getUpRefNum() const {
112     assert(oldTy == UpRefTy && "Can't getUpRefNum on non upreference");
113     return atoi(&((getNewTy().c_str())[1])); // skip the slash
114   }
115
116   typedef std::vector<const TypeInfo*> UpRefStack;
117   void getSignedness(unsigned &sNum, unsigned &uNum, UpRefStack& stk) const;
118   std::string makeUniqueName(const std::string& BaseName) const;
119
120   const std::string& getNewTy() const { return newTy; }
121   const TypeInfo* getResultType() const { return resultTy; }
122   const TypeInfo* getElementType() const { return elemTy; }
123
124   const TypeInfo* getPointerType() const {
125     return get(newTy + "*", PointerTy, this, (TypeInfo*)0);
126   }
127
128   bool isUnresolved() const { return oldTy == UnresolvedTy; }
129   bool isUpReference() const { return oldTy == UpRefTy; }
130   bool isVoid() const { return oldTy == VoidTy; }
131   bool isBool() const { return oldTy == BoolTy; }
132   bool isSigned() const {
133     return oldTy == SByteTy || oldTy == ShortTy || 
134            oldTy == IntTy || oldTy == LongTy;
135   }
136
137   bool isUnsigned() const {
138     return oldTy == UByteTy || oldTy == UShortTy || 
139            oldTy == UIntTy || oldTy == ULongTy;
140   }
141   bool isSignless() const { return !isSigned() && !isUnsigned(); }
142   bool isInteger() const { return isSigned() || isUnsigned(); }
143   bool isIntegral() const { return oldTy == BoolTy || isInteger(); }
144   bool isFloatingPoint() const { return oldTy == DoubleTy || oldTy == FloatTy; }
145   bool isPacked() const { return oldTy == PackedTy; }
146   bool isPointer() const { return oldTy == PointerTy; }
147   bool isStruct() const { return oldTy == StructTy || oldTy == PackedStructTy; }
148   bool isArray() const { return oldTy == ArrayTy; }
149   bool isOther() const { 
150     return !isPacked() && !isPointer() && !isFloatingPoint() && !isIntegral(); }
151   bool isFunction() const { return oldTy == FunctionTy; }
152   bool isComposite() const {
153     return isStruct() || isPointer() || isArray() || isPacked();
154   }
155
156   bool isAttributeCandidate() const {
157     return isIntegral() && getBitWidth() < 32;
158   }
159
160   bool isUnresolvedDeep() const;
161
162   unsigned getBitWidth() const;
163
164   const TypeInfo* getIndexedType(const ValueInfo*  VI) const;
165
166   unsigned getNumStructElements() const { 
167     return (elements ? elements->size() : 0);
168   }
169
170   const TypeInfo* getElement(unsigned idx) const {
171     if (elements)
172       if (idx < elements->size())
173         return (*elements)[idx];
174     return 0;
175   }
176
177 private:
178   TypeInfo() 
179     : newTy(), oldTy(UnresolvedTy), elemTy(0), resultTy(0), elements(0),
180       nelems(0) {
181   }
182
183   TypeInfo(const TypeInfo& that); // do not implement
184   TypeInfo& operator=(const TypeInfo& that); // do not implement
185
186   ~TypeInfo() { delete elements; }
187
188   struct ltfunctor
189   {
190     bool operator()(const TypeInfo* X, const TypeInfo* Y) const {
191       assert(X && "Can't compare null pointer");
192       assert(Y && "Can't compare null pointer");
193       return *X < *Y;
194     }
195   };
196
197   typedef std::set<const TypeInfo*, ltfunctor> TypeRegMap;
198
199   static const TypeInfo* add_new_type(TypeInfo* existing);
200
201   std::string newTy;
202   Types oldTy;
203   TypeInfo *elemTy;
204   TypeInfo *resultTy;
205   TypeList *elements;
206   uint64_t nelems;
207   static TypeRegMap registry;
208 public:
209   typedef std::vector<const TypeInfo*> TypeVector;
210   typedef std::map<std::string,const TypeInfo*> TypeMap;
211   typedef std::map<const TypeInfo*,std::string> TypePlaneMap;
212   typedef std::map<std::string,TypePlaneMap> GlobalsTypeMap;
213   static TypeVector EnumeratedTypes;
214   static TypeMap NamedTypes;
215   static GlobalsTypeMap Globals;
216 };
217
218 TypeInfo::TypeRegMap     TypeInfo::registry;
219 TypeInfo::TypeVector     TypeInfo::EnumeratedTypes;
220 TypeInfo::TypeMap        TypeInfo::NamedTypes;
221 TypeInfo::GlobalsTypeMap TypeInfo::Globals;
222
223 const TypeInfo* TypeInfo::get(const std::string &newType, Types oldType) {
224   TypeInfo* Ty = new TypeInfo();
225   Ty->newTy = newType;
226   Ty->oldTy = oldType;
227   return add_new_type(Ty);
228 }
229
230 const TypeInfo* TypeInfo::get(const std::string& newType, Types oldType, 
231                               const TypeInfo* eTy, const TypeInfo* rTy) {
232   TypeInfo* Ty= new TypeInfo();
233   Ty->newTy = newType;
234   Ty->oldTy = oldType;
235   Ty->elemTy = const_cast<TypeInfo*>(eTy);
236   Ty->resultTy = const_cast<TypeInfo*>(rTy);
237   return add_new_type(Ty);
238 }
239
240 const TypeInfo* TypeInfo::get(const std::string& newType, Types oldType, 
241                               const TypeInfo *eTy, uint64_t elems) {
242   TypeInfo* Ty = new TypeInfo();
243   Ty->newTy = newType;
244   Ty->oldTy = oldType;
245   Ty->elemTy = const_cast<TypeInfo*>(eTy);
246   Ty->nelems = elems;
247   return  add_new_type(Ty);
248 }
249
250 const TypeInfo* TypeInfo::get(const std::string& newType, Types oldType, 
251                               TypeList* TL) {
252   TypeInfo* Ty = new TypeInfo();
253   Ty->newTy = newType;
254   Ty->oldTy = oldType;
255   Ty->elements = TL;
256   return add_new_type(Ty);
257 }
258
259 const TypeInfo* TypeInfo::get(const std::string& newType, const TypeInfo* resTy,
260                               TypeList* TL) {
261   TypeInfo* Ty = new TypeInfo();
262   Ty->newTy = newType;
263   Ty->oldTy = FunctionTy;
264   Ty->resultTy = const_cast<TypeInfo*>(resTy);
265   Ty->elements = TL;
266   return add_new_type(Ty);
267 }
268
269 const TypeInfo* TypeInfo::resolve() const {
270   if (isUnresolved()) {
271     if (getNewTy()[0] == '%' && isdigit(newTy[1])) {
272       unsigned ref = atoi(&((newTy.c_str())[1])); // skip the %
273       if (ref < EnumeratedTypes.size()) {
274         return EnumeratedTypes[ref];
275       } else {
276         std::string msg("Can't resolve numbered type: ");
277         msg += getNewTy();
278         yyerror(msg.c_str());
279       }
280     } else {
281       TypeInfo::TypeMap::iterator I = NamedTypes.find(newTy);
282       if (I != NamedTypes.end()) {
283         return I->second;
284       } else {
285         std::string msg("Cannot resolve type: ");
286         msg += getNewTy();
287         yyerror(msg.c_str());
288       }
289     }
290   }
291   // otherwise its already resolved.
292   return this;
293 }
294
295 bool TypeInfo::operator<(const TypeInfo& that) const {
296   if (this == &that)
297     return false;
298   if (oldTy != that.oldTy)
299     return oldTy < that.oldTy;
300   switch (oldTy) {
301     case UpRefTy: {
302       unsigned thisUp = this->getUpRefNum();
303       unsigned thatUp = that.getUpRefNum();
304       return thisUp < thatUp;
305     }
306     case PackedTy:
307     case ArrayTy:
308       if (this->nelems != that.nelems)
309         return nelems < that.nelems;
310     case PointerTy: {
311       const TypeInfo* thisTy = this->elemTy;
312       const TypeInfo* thatTy = that.elemTy;
313       return *thisTy < *thatTy;
314     }
315     case FunctionTy: {
316       const TypeInfo* thisTy = this->resultTy;
317       const TypeInfo* thatTy = that.resultTy;
318       if (!thisTy->sameOldTyAs(thatTy))
319         return *thisTy < *thatTy;
320       /* FALL THROUGH */
321     }
322     case StructTy:
323     case PackedStructTy: {
324       if (elements->size() != that.elements->size())
325         return elements->size() < that.elements->size();
326       for (unsigned i = 0; i < elements->size(); i++) {
327         const TypeInfo* thisTy = (*this->elements)[i];
328         const TypeInfo* thatTy = (*that.elements)[i];
329         if (!thisTy->sameOldTyAs(thatTy))
330           return *thisTy < *thatTy;
331       }
332       break;
333     }
334     case UnresolvedTy:
335       return this->newTy < that.newTy;
336     default:
337       break;
338   }
339   return false; 
340 }
341
342 bool TypeInfo::sameOldTyAs(const TypeInfo* that) const {
343   if (that == 0)
344     return false;
345   if ( this == that ) 
346     return true;
347   if (oldTy != that->oldTy)
348     return false;
349   switch (oldTy) {
350     case PackedTy:
351     case ArrayTy:
352       if (nelems != that->nelems)
353         return false;
354       /* FALL THROUGH */
355     case PointerTy: {
356       const TypeInfo* thisTy = this->elemTy;
357       const TypeInfo* thatTy = that->elemTy;
358       return thisTy->sameOldTyAs(thatTy);
359     }
360     case FunctionTy: {
361       const TypeInfo* thisTy = this->resultTy;
362       const TypeInfo* thatTy = that->resultTy;
363       if (!thisTy->sameOldTyAs(thatTy))
364         return false;
365       /* FALL THROUGH */
366     }
367     case StructTy:
368     case PackedStructTy: {
369       if (elements->size() != that->elements->size())
370         return false;
371       for (unsigned i = 0; i < elements->size(); i++) {
372         const TypeInfo* thisTy = (*this->elements)[i];
373         const TypeInfo* thatTy = (*that->elements)[i];
374         if (!thisTy->sameOldTyAs(thatTy))
375           return false;
376       }
377       return true;
378     }
379     case UnresolvedTy:
380       return this->newTy == that->newTy;
381     default:
382       return true; // for all others oldTy == that->oldTy is sufficient
383   }
384   return true;
385 }
386
387 bool TypeInfo::isUnresolvedDeep() const {
388   switch (oldTy) {
389     case UnresolvedTy: 
390       return true;
391     case PackedTy:
392     case ArrayTy:
393     case PointerTy:
394       return elemTy->isUnresolvedDeep();
395     case PackedStructTy:
396     case StructTy:
397       for (unsigned i = 0; i < elements->size(); i++)
398         if ((*elements)[i]->isUnresolvedDeep())
399           return true;
400       return false;
401     default:
402       return false;
403   }
404 }
405
406 unsigned TypeInfo::getBitWidth() const {
407   switch (oldTy) {
408     default:
409     case LabelTy:
410     case VoidTy : return 0;
411     case BoolTy : return 1;
412     case SByteTy: case UByteTy : return 8;
413     case ShortTy: case UShortTy : return 16;
414     case IntTy: case UIntTy: case FloatTy: return 32;
415     case LongTy: case ULongTy: case DoubleTy : return 64;
416     case PointerTy: return SizeOfPointer; // global var
417     case PackedTy: 
418     case ArrayTy: 
419       return nelems * elemTy->getBitWidth();
420     case StructTy:
421     case PackedStructTy: {
422       uint64_t size = 0;
423       for (unsigned i = 0; i < elements->size(); i++) {
424         size += (*elements)[i]->getBitWidth();
425       }
426       return size;
427     }
428   }
429 }
430
431 const TypeInfo* TypeInfo::getIndexedType(const ValueInfo*  VI) const {
432   if (isStruct()) {
433     if (VI->isConstant() && VI->type->isInteger()) {
434       size_t pos = VI->val->find(' ') + 1;
435       if (pos < VI->val->size()) {
436         uint64_t idx = atoi(VI->val->substr(pos).c_str());
437         return (*elements)[idx];
438       } else {
439         yyerror("Invalid value for constant integer");
440         return 0;
441       }
442     } else {
443       yyerror("Structure requires constant index");
444       return 0;
445     }
446   }
447   if (isArray() || isPacked() || isPointer())
448     return elemTy;
449   yyerror("Invalid type for getIndexedType");
450   return 0;
451 }
452
453 void TypeInfo::getSignedness(unsigned &sNum, unsigned &uNum, 
454                              UpRefStack& stack) const {
455   switch (oldTy) {
456     default:
457     case OpaqueTy: case LabelTy: case VoidTy: case BoolTy: 
458     case FloatTy : case DoubleTy: case UpRefTy:
459       return;
460     case SByteTy: case ShortTy: case LongTy: case IntTy: 
461       sNum++;
462       return;
463     case UByteTy: case UShortTy: case UIntTy: case ULongTy: 
464       uNum++;
465       return;
466     case PointerTy:
467     case PackedTy: 
468     case ArrayTy:
469       stack.push_back(this);
470       elemTy->getSignedness(sNum, uNum, stack);
471       return;
472     case StructTy:
473     case PackedStructTy: {
474       stack.push_back(this);
475       for (unsigned i = 0; i < elements->size(); i++) {
476         (*elements)[i]->getSignedness(sNum, uNum, stack);
477       }
478       return;
479     }
480     case UnresolvedTy: {
481       const TypeInfo* Ty = this->resolve();
482       // Let's not recurse.
483       UpRefStack::const_iterator I = stack.begin(), E = stack.end();
484       for ( ; I != E && *I != Ty; ++I) 
485         ;
486       if (I == E)
487         Ty->getSignedness(sNum, uNum, stack);
488       return;
489     }
490   }
491 }
492
493 std::string AddSuffix(const std::string& Name, const std::string& Suffix) {
494   if (Name[Name.size()-1] == '"') {
495     std::string Result = Name;
496     Result.insert(Result.size()-1, Suffix);
497     return Result;
498   }
499   return Name + Suffix;
500 }
501
502 std::string TypeInfo::makeUniqueName(const std::string& BaseName) const {
503   if (BaseName == "\"alloca point\"")
504     return BaseName;
505   switch (oldTy) {
506     default:
507       break;
508     case OpaqueTy: case LabelTy: case VoidTy: case BoolTy: case UpRefTy:
509     case FloatTy : case DoubleTy: case UnresolvedTy:
510       return BaseName;
511     case SByteTy: case ShortTy: case LongTy: case IntTy: 
512       return AddSuffix(BaseName, ".s");
513     case UByteTy: case UShortTy: case UIntTy: case ULongTy: 
514       return AddSuffix(BaseName, ".u");
515   }
516
517   unsigned uNum = 0, sNum = 0;
518   std::string Suffix;
519   switch (oldTy) {
520     case PointerTy:
521     case PackedTy: 
522     case ArrayTy: {
523       TypeInfo::UpRefStack stack;
524       elemTy->resolve()->getSignedness(sNum, uNum, stack);
525       break;
526     }
527     case StructTy:
528     case PackedStructTy: {
529       for (unsigned i = 0; i < elements->size(); i++) {
530         TypeInfo::UpRefStack stack;
531         (*elements)[i]->resolve()->getSignedness(sNum, uNum, stack);
532       }
533       break;
534     }
535     default:
536       assert(0 && "Invalid Type");
537       break;
538   }
539
540   if (sNum == 0 && uNum == 0)
541     return BaseName;
542
543   switch (oldTy) {
544     default:             Suffix += ".nada"; break;
545     case PointerTy:      Suffix += ".pntr"; break;
546     case PackedTy:       Suffix += ".pckd"; break;
547     case ArrayTy:        Suffix += ".arry"; break;
548     case StructTy:       Suffix += ".strc"; break;
549     case PackedStructTy: Suffix += ".pstr"; break;
550   }
551
552   Suffix += ".s" + llvm::utostr(sNum);
553   Suffix += ".u" + llvm::utostr(uNum);
554   return AddSuffix(BaseName, Suffix);
555 }
556
557 TypeInfo& TypeInfo::operator=(const TypeInfo& that) {
558   oldTy = that.oldTy;
559   nelems = that.nelems;
560   newTy = that.newTy;
561   elemTy = that.elemTy;
562   resultTy = that.resultTy;
563   if (that.elements) {
564     elements = new TypeList(that.elements->size());
565     *elements = *that.elements;
566   } else {
567     elements = 0;
568   }
569   return *this;
570 }
571
572 const TypeInfo* TypeInfo::add_new_type(TypeInfo* newTy) {
573   TypeRegMap::iterator I = registry.find(newTy);
574   if (I != registry.end()) {
575     delete newTy;
576     return *I;
577   }
578   registry.insert(newTy);
579   return newTy;
580 }
581
582 /// This type is used to keep track of the signedness of constants.
583 struct ConstInfo {
584   std::string *cnst;
585   const TypeInfo *type;
586   ~ConstInfo() { delete cnst; }
587 };
588
589 /// This variable provides a counter for unique names. It is used in various
590 /// productions to ensure a unique name is generated.
591 static uint64_t UniqueNameCounter = 1;
592
593 // This is set when a DECLARE keyword is recognized so that subsequent parsing
594 // of a function prototype can know if its a declaration or definition.
595 static bool isDeclare = false;
596
597 // This bool is used to communicate between the InstVal and Inst rules about
598 // whether or not a cast should be deleted. When the flag is set, InstVal has
599 // determined that the cast is a candidate. However, it can only be deleted if
600 // the value being casted is the same value name as the instruction. The Inst
601 // rule makes that comparison if the flag is set and comments out the
602 // instruction if they match.
603 static bool deleteUselessCastFlag = false;
604 static std::string* deleteUselessCastName = 0;
605
606
607
608 const char* getCastOpcode(std::string& Source, const TypeInfo* SrcTy, 
609                           const TypeInfo* DstTy) {
610   unsigned SrcBits = SrcTy->getBitWidth();
611   unsigned DstBits = DstTy->getBitWidth();
612   const char* opcode = "bitcast";
613   // Run through the possibilities ...
614   if (DstTy->isIntegral()) {                        // Casting to integral
615     if (SrcTy->isIntegral()) {                      // Casting from integral
616       if (DstBits < SrcBits)
617         opcode = "trunc";
618       else if (DstBits > SrcBits) {                // its an extension
619         if (SrcTy->isSigned())
620           opcode ="sext";                          // signed -> SEXT
621         else
622           opcode = "zext";                         // unsigned -> ZEXT
623       } else {
624         opcode = "bitcast";                        // Same size, No-op cast
625       }
626     } else if (SrcTy->isFloatingPoint()) {          // Casting from floating pt
627       if (DstTy->isSigned()) 
628         opcode = "fptosi";                         // FP -> sint
629       else
630         opcode = "fptoui";                         // FP -> uint 
631     } else if (SrcTy->isPacked()) {
632       assert(DstBits == SrcTy->getBitWidth() &&
633                "Casting packed to integer of different width");
634         opcode = "bitcast";                        // same size, no-op cast
635     } else {
636       assert(SrcTy->isPointer() &&
637              "Casting from a value that is not first-class type");
638       opcode = "ptrtoint";                         // ptr -> int
639     }
640   } else if (DstTy->isFloatingPoint()) {           // Casting to floating pt
641     if (SrcTy->isIntegral()) {                     // Casting from integral
642       if (SrcTy->isSigned())
643         opcode = "sitofp";                         // sint -> FP
644       else
645         opcode = "uitofp";                         // uint -> FP
646     } else if (SrcTy->isFloatingPoint()) {         // Casting from floating pt
647       if (DstBits < SrcBits) {
648         opcode = "fptrunc";                        // FP -> smaller FP
649       } else if (DstBits > SrcBits) {
650         opcode = "fpext";                          // FP -> larger FP
651       } else  {
652         opcode ="bitcast";                         // same size, no-op cast
653       }
654     } else if (SrcTy->isPacked()) {
655       assert(DstBits == SrcTy->getBitWidth() &&
656              "Casting packed to floating point of different width");
657         opcode = "bitcast";                        // same size, no-op cast
658     } else {
659       assert(0 && "Casting pointer or non-first class to float");
660     }
661   } else if (DstTy->isPacked()) {
662     if (SrcTy->isPacked()) {
663       assert(DstTy->getBitWidth() == SrcTy->getBitWidth() &&
664              "Casting packed to packed of different widths");
665       opcode = "bitcast";                          // packed -> packed
666     } else if (DstTy->getBitWidth() == SrcBits) {
667       opcode = "bitcast";                          // float/int -> packed
668     } else {
669       assert(!"Illegal cast to packed (wrong type or size)");
670     }
671   } else if (DstTy->isPointer()) {
672     if (SrcTy->isPointer()) {
673       opcode = "bitcast";                          // ptr -> ptr
674     } else if (SrcTy->isIntegral()) {
675       opcode = "inttoptr";                         // int -> ptr
676     } else {
677       assert(!"Casting invalid type to pointer");
678     }
679   } else {
680     assert(!"Casting to type that is not first-class");
681   }
682   return opcode;
683 }
684
685 std::string getCastUpgrade(const std::string& Src, const TypeInfo* SrcTy,
686                            const TypeInfo* DstTy, bool isConst) {
687   std::string Result;
688   std::string Source = Src;
689   if (SrcTy->isFloatingPoint() && DstTy->isPointer()) {
690     // fp -> ptr cast is no longer supported but we must upgrade this
691     // by doing a double cast: fp -> int -> ptr
692     if (isConst)
693       Source = "i64 fptoui(" + Source + " to i64)";
694     else {
695       *O << "    %cast_upgrade" << UniqueNameCounter++ << " = fptoui " 
696          << Source << " to i64\n";
697       Source = "i64 %cast_upgrade" + llvm::utostr(UniqueNameCounter);
698     }
699     // Update the SrcTy for the getCastOpcode call below
700     SrcTy = TypeInfo::get("i64", ULongTy);
701   } else if (DstTy->isBool()) {
702     // cast type %x to bool was previously defined as setne type %x, null
703     // The cast semantic is now to truncate, not compare so we must retain
704     // the original intent by replacing the cast with a setne
705     const char* comparator = SrcTy->isPointer() ? ", null" : 
706       (SrcTy->isFloatingPoint() ? ", 0.0" : 
707        (SrcTy->isBool() ? ", false" : ", 0"));
708     const char* compareOp = SrcTy->isFloatingPoint() ? "fcmp one " : "icmp ne ";
709     if (isConst) { 
710       Result = "(" + Source + comparator + ")";
711       Result = compareOp + Result;
712     } else
713       Result = compareOp + Source + comparator;
714     return Result; // skip cast processing below
715   }
716   SrcTy = SrcTy->resolve();
717   DstTy = DstTy->resolve();
718   std::string Opcode(getCastOpcode(Source, SrcTy, DstTy));
719   if (isConst)
720     Result += Opcode + "( " + Source + " to " + DstTy->getNewTy() + ")";
721   else
722     Result += Opcode + " " + Source + " to " + DstTy->getNewTy();
723   return Result;
724 }
725
726 const char* getDivRemOpcode(const std::string& opcode, const TypeInfo* TI) {
727   const char* op = opcode.c_str();
728   const TypeInfo* Ty = TI->resolve();
729   if (Ty->isPacked())
730     Ty = Ty->getElementType();
731   if (opcode == "div")
732     if (Ty->isFloatingPoint())
733       op = "fdiv";
734     else if (Ty->isUnsigned())
735       op = "udiv";
736     else if (Ty->isSigned())
737       op = "sdiv";
738     else
739       yyerror("Invalid type for div instruction");
740   else if (opcode == "rem")
741     if (Ty->isFloatingPoint())
742       op = "frem";
743     else if (Ty->isUnsigned())
744       op = "urem";
745     else if (Ty->isSigned())
746       op = "srem";
747     else
748       yyerror("Invalid type for rem instruction");
749   return op;
750 }
751
752 std::string getCompareOp(const std::string& setcc, const TypeInfo* TI) {
753   assert(setcc.length() == 5);
754   char cc1 = setcc[3];
755   char cc2 = setcc[4];
756   assert(cc1 == 'e' || cc1 == 'n' || cc1 == 'l' || cc1 == 'g');
757   assert(cc2 == 'q' || cc2 == 'e' || cc2 == 'e' || cc2 == 't');
758   std::string result("xcmp xxx");
759   result[6] = cc1;
760   result[7] = cc2;
761   if (TI->isFloatingPoint()) {
762     result[0] = 'f';
763     result[5] = 'o';
764     if (cc1 == 'n')
765       result[5] = 'u'; // NE maps to unordered
766     else
767       result[5] = 'o'; // everything else maps to ordered
768   } else if (TI->isIntegral() || TI->isPointer()) {
769     result[0] = 'i';
770     if ((cc1 == 'e' && cc2 == 'q') || (cc1 == 'n' && cc2 == 'e'))
771       result.erase(5,1);
772     else if (TI->isSigned())
773       result[5] = 's';
774     else if (TI->isUnsigned() || TI->isPointer() || TI->isBool())
775       result[5] = 'u';
776     else
777       yyerror("Invalid integral type for setcc");
778   }
779   return result;
780 }
781
782 const TypeInfo* getFunctionReturnType(const TypeInfo* PFTy) {
783   PFTy = PFTy->resolve();
784   if (PFTy->isPointer()) {
785     const TypeInfo* ElemTy = PFTy->getElementType();
786     ElemTy = ElemTy->resolve();
787     if (ElemTy->isFunction())
788       return ElemTy->getResultType();
789   } else if (PFTy->isFunction()) {
790     return PFTy->getResultType();
791   }
792   return PFTy;
793 }
794
795 const TypeInfo* ResolveUpReference(const TypeInfo* Ty, 
796                                    TypeInfo::UpRefStack* stack) {
797   assert(Ty->isUpReference() && "Can't resolve a non-upreference");
798   unsigned upref = Ty->getUpRefNum();
799   assert(upref < stack->size() && "Invalid up reference");
800   return (*stack)[upref - stack->size() - 1];
801 }
802
803 const TypeInfo* getGEPIndexedType(const TypeInfo* PTy, ValueList* idxs) {
804   const TypeInfo* Result = PTy = PTy->resolve();
805   assert(PTy->isPointer() && "GEP Operand is not a pointer?");
806   TypeInfo::UpRefStack stack;
807   for (unsigned i = 0; i < idxs->size(); ++i) {
808     if (Result->isComposite()) {
809       Result = Result->getIndexedType((*idxs)[i]);
810       Result = Result->resolve();
811       stack.push_back(Result);
812     } else
813       yyerror("Invalid type for index");
814   }
815   // Resolve upreferences so we can return a more natural type
816   if (Result->isPointer()) {
817     if (Result->getElementType()->isUpReference()) {
818       stack.push_back(Result);
819       Result = ResolveUpReference(Result->getElementType(), &stack);
820     }
821   } else if (Result->isUpReference()) {
822     Result = ResolveUpReference(Result->getElementType(), &stack);
823   }
824   return Result->getPointerType();
825 }
826
827 // This function handles appending .u or .s to integer value names that
828 // were previously unsigned or signed, respectively. This avoids name
829 // collisions since the unsigned and signed type planes have collapsed
830 // into a single signless type plane.
831 std::string getUniqueName(const std::string *Name, const TypeInfo* Ty,
832                           bool isGlobal = false, bool isDef = false) {
833
834   // If its not a symbolic name, don't modify it, probably a constant val.
835   if ((*Name)[0] != '%' && (*Name)[0] != '"')
836     return *Name;
837
838   // If its a numeric reference, just leave it alone.
839   if (isdigit((*Name)[1]))
840     return *Name;
841
842   // Resolve the type
843   Ty = Ty->resolve(); 
844
845   // If its a global name, get its uniquified name, if any
846   TypeInfo::GlobalsTypeMap::iterator GI = TypeInfo::Globals.find(*Name);
847   if (GI != TypeInfo::Globals.end()) {
848     TypeInfo::TypePlaneMap::iterator TPI = GI->second.begin();
849     TypeInfo::TypePlaneMap::iterator TPE = GI->second.end();
850     for ( ; TPI != TPE ; ++TPI) {
851       if (TPI->first->sameNewTyAs(Ty)) 
852         return TPI->second;
853     }
854   }
855
856   if (isGlobal) {
857     // We didn't find a global name, but if its supposed to be global then all 
858     // we can do is return the name. This is probably a forward reference of a 
859     // global value that hasn't been defined yet. Since we have no definition
860     // we don't know its linkage class. Just assume its an external and the name
861     // shouldn't change.
862     return *Name;
863   }
864
865   // Default the result to the current name
866   std::string Result = Ty->makeUniqueName(*Name);
867
868   return Result;
869 }
870
871 std::string getGlobalName(const std::string* Name, const std::string Linkage,
872                           const TypeInfo* Ty, bool isConstant) {
873   // Default to given name
874   std::string Result = *Name; 
875   // Look up the name in the Globals Map
876   TypeInfo::GlobalsTypeMap::iterator GI = TypeInfo::Globals.find(*Name);
877   // Did we see this global name before?
878   if (GI != TypeInfo::Globals.end()) {
879     if (Ty->isUnresolvedDeep()) {
880       // The Gval's type is unresolved. Consequently, we can't disambiguate it
881       // by type. We'll just change its name and emit a warning.
882       warning("Cannot disambiguate global value '" + *Name + 
883               "' because type '" + Ty->getNewTy() + "'is unresolved.\n");
884       Result = *Name + ".unique";
885       UniqueNameCounter++;
886       Result += llvm::utostr(UniqueNameCounter);
887       return Result;
888     } else {
889       TypeInfo::TypePlaneMap::iterator TPI = GI->second.find(Ty);
890       if (TPI != GI->second.end()) {
891         // We found an existing name of the same old type. This isn't allowed 
892         // in LLVM 2.0. Consequently, we must alter the name of the global so it
893         // can at least compile. References to the global will yield the first
894         // definition, which is okay. We also must warn about this.
895         Result = *Name + ".unique";
896         UniqueNameCounter++;
897         Result += llvm::utostr(UniqueNameCounter);
898         warning(std::string("Global variable '") + *Name + "' was renamed to '"+
899                 Result + "'");
900       } else { 
901         // There isn't an existing definition for this name according to the
902         // old types. Now search the TypePlanMap for types with the same new
903         // name. 
904         TypeInfo::TypePlaneMap::iterator TPI = GI->second.begin();
905         TypeInfo::TypePlaneMap::iterator TPE = GI->second.end();
906         for ( ; TPI != TPE; ++TPI) {
907           if (TPI->first->sameNewTyAs(Ty)) {
908             // The new types are the same but the old types are different so 
909             // this is a global name collision resulting from type planes 
910             // collapsing. 
911             if (Linkage == "external" || Linkage == "dllimport" || 
912                 Linkage == "extern_weak" || Linkage == "") {
913               // The linkage of this gval is external so we can't reliably 
914               // rename it because it could potentially create a linking 
915               // problem.  However, we can't leave the name conflict in the 
916               // output either or it won't assemble with LLVM 2.0.  So, all we 
917               // can do is rename this one to something unique and emit a 
918               // warning about the problem.
919               Result = *Name + ".unique";
920               UniqueNameCounter++;
921               Result += llvm::utostr(UniqueNameCounter);
922               warning("Renaming global value '" + *Name + "' to '" + Result + 
923                       "' may cause linkage errors.");
924               return Result;
925             } else {
926               // Its linkage is internal and its type is known so we can 
927               // disambiguate the name collision successfully based on the type.
928               Result = getUniqueName(Name, Ty);
929               TPI->second = Result;
930               return Result;
931             }
932           }
933         }
934         // We didn't find an entry in the type plane with the same new type and
935         // the old types differ so this is a new type plane for this global 
936         // variable. We just fall through to the logic below which inserts
937         // the global.
938       }
939     }
940   }
941
942   // Its a new global name, if it is external we can't change it
943   if (isConstant || Linkage == "external" || Linkage == "dllimport" || 
944       Linkage == "extern_weak" || Linkage == "") {
945     TypeInfo::Globals[Result][Ty] = Result;
946     return Result;
947   }
948
949   // Its a new global name, and it is internal, change the name to make it
950   // unique for its type.
951   // Result = getUniqueName(Name, Ty);
952   TypeInfo::Globals[*Name][Ty] = Result;
953   return Result;
954 }
955
956 } // End anonymous namespace
957
958 // This function is used by the Lexer to create a TypeInfo. It can't be
959 // in the anonymous namespace.
960 const TypeInfo* getTypeInfo(const std::string& newTy, Types oldTy) {
961   return TypeInfo::get(newTy, oldTy);
962 }
963
964 %}
965
966 // %file-prefix="UpgradeParser"
967
968 %union {
969   std::string*    String;
970   const TypeInfo* Type;
971   ValueInfo*      Value;
972   ConstInfo*      Const;
973   ValueList*      ValList;
974   TypeList*       TypeVec;
975 }
976
977 %token <Type>   VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
978 %token <Type>   FLOAT DOUBLE LABEL 
979 %token <String> OPAQUE ESINT64VAL EUINT64VAL SINTVAL UINTVAL FPVAL
980 %token <String> NULL_TOK UNDEF ZEROINITIALIZER TRUETOK FALSETOK
981 %token <String> TYPE VAR_ID LABELSTR STRINGCONSTANT
982 %token <String> IMPLEMENTATION BEGINTOK ENDTOK
983 %token <String> DECLARE GLOBAL CONSTANT SECTION VOLATILE
984 %token <String> TO DOTDOTDOT CONST INTERNAL LINKONCE WEAK 
985 %token <String> DLLIMPORT DLLEXPORT EXTERN_WEAK APPENDING
986 %token <String> EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG
987 %token <String> ALIGN UNINITIALIZED
988 %token <String> DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
989 %token <String> CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
990 %token <String> X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
991 %token <String> DATALAYOUT
992 %token <String> RET BR SWITCH INVOKE EXCEPT UNWIND UNREACHABLE
993 %token <String> ADD SUB MUL DIV UDIV SDIV FDIV REM UREM SREM FREM AND OR XOR
994 %token <String> SETLE SETGE SETLT SETGT SETEQ SETNE  // Binary Comparators
995 %token <String> ICMP FCMP EQ NE SLT SGT SLE SGE OEQ ONE OLT OGT OLE OGE 
996 %token <String> ORD UNO UEQ UNE ULT UGT ULE UGE
997 %token <String> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
998 %token <String> PHI_TOK SELECT SHL SHR ASHR LSHR VAARG
999 %token <String> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
1000 %token <String> CAST TRUNC ZEXT SEXT FPTRUNC FPEXT FPTOUI FPTOSI UITOFP SITOFP 
1001 %token <String> PTRTOINT INTTOPTR BITCAST
1002
1003 %type <String> OptAssign OptLinkage OptCallingConv OptAlign OptCAlign 
1004 %type <String> SectionString OptSection GlobalVarAttributes GlobalVarAttribute
1005 %type <String> ConstExpr DefinitionList
1006 %type <String> ConstPool TargetDefinition LibrariesDefinition LibList OptName
1007 %type <String> ArgVal ArgListH ArgList FunctionHeaderH BEGIN FunctionHeader END
1008 %type <String> Function FunctionProto BasicBlock 
1009 %type <String> InstructionList BBTerminatorInst JumpTable Inst
1010 %type <String> OptTailCall OptVolatile Unwind
1011 %type <String> SymbolicValueRef OptSideEffect GlobalType
1012 %type <String> FnDeclareLinkage BasicBlockList BigOrLittle AsmBlock
1013 %type <String> Name ConstValueRef ConstVector External
1014 %type <String> ShiftOps SetCondOps LogicalOps ArithmeticOps CastOps
1015 %type <String> IPredicates FPredicates
1016
1017 %type <ValList> ValueRefList ValueRefListE IndexList
1018 %type <TypeVec> TypeListI ArgTypeListI
1019
1020 %type <Type> IntType SIntType UIntType FPType TypesV Types 
1021 %type <Type> PrimType UpRTypesV UpRTypes
1022
1023 %type <String> IntVal EInt64Val 
1024 %type <Const>  ConstVal
1025
1026 %type <Value> ValueRef ResolvedVal InstVal PHIList MemoryInst
1027
1028 %start Module
1029
1030 %%
1031
1032 // Handle constant integer size restriction and conversion...
1033 IntVal : SINTVAL | UINTVAL ;
1034 EInt64Val : ESINT64VAL | EUINT64VAL;
1035
1036 // Operations that are notably excluded from this list include:
1037 // RET, BR, & SWITCH because they end basic blocks and are treated specially.
1038 ArithmeticOps: ADD | SUB | MUL | DIV | UDIV | SDIV | FDIV 
1039              | REM | UREM | SREM | FREM;
1040 LogicalOps   : AND | OR | XOR;
1041 SetCondOps   : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
1042 IPredicates  : EQ | NE | SLT | SGT | SLE | SGE | ULT | UGT | ULE | UGE;
1043 FPredicates  : OEQ | ONE | OLT | OGT | OLE | OGE | ORD | UNO | UEQ | UNE
1044              | ULT | UGT | ULE | UGE | TRUETOK | FALSETOK;
1045 ShiftOps     : SHL | SHR | ASHR | LSHR;
1046 CastOps      : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | FPTOUI | FPTOSI | 
1047                UITOFP | SITOFP | PTRTOINT | INTTOPTR | BITCAST | CAST
1048              ;
1049
1050 // These are some types that allow classification if we only want a particular 
1051 // thing... for example, only a signed, unsigned, or integral type.
1052 SIntType :  LONG |  INT |  SHORT | SBYTE;
1053 UIntType : ULONG | UINT | USHORT | UBYTE;
1054 IntType  : SIntType | UIntType;
1055 FPType   : FLOAT | DOUBLE;
1056
1057 // OptAssign - Value producing statements have an optional assignment component
1058 OptAssign : Name '=' {
1059     $$ = $1;
1060   }
1061   | /*empty*/ {
1062     $$ = new std::string(""); 
1063   };
1064
1065 OptLinkage 
1066   : INTERNAL | LINKONCE | WEAK | APPENDING | DLLIMPORT | DLLEXPORT 
1067   | EXTERN_WEAK 
1068   | /*empty*/   { $$ = new std::string(""); } ;
1069
1070 OptCallingConv 
1071   : CCC_TOK | CSRETCC_TOK | FASTCC_TOK | COLDCC_TOK | X86_STDCALLCC_TOK 
1072   | X86_FASTCALLCC_TOK 
1073   | CC_TOK EUINT64VAL { 
1074     *$1 += *$2; 
1075     delete $2;
1076     $$ = $1; 
1077     }
1078   | /*empty*/ { $$ = new std::string(""); } ;
1079
1080 // OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1081 // a comma before it.
1082 OptAlign 
1083   : /*empty*/        { $$ = new std::string(); }
1084   | ALIGN EUINT64VAL { *$1 += " " + *$2; delete $2; $$ = $1; };
1085
1086 OptCAlign 
1087   : /*empty*/            { $$ = new std::string(); } 
1088   | ',' ALIGN EUINT64VAL { 
1089     $2->insert(0, ", "); 
1090     *$2 += " " + *$3;
1091     delete $3;
1092     $$ = $2;
1093   };
1094
1095 SectionString 
1096   : SECTION STRINGCONSTANT { 
1097     *$1 += " " + *$2;
1098     delete $2;
1099     $$ = $1;
1100   };
1101
1102 OptSection : /*empty*/     { $$ = new std::string(); } 
1103            | SectionString;
1104
1105 GlobalVarAttributes 
1106     : /* empty */ { $$ = new std::string(); } 
1107     | ',' GlobalVarAttribute GlobalVarAttributes  {
1108       $2->insert(0, ", ");
1109       if (!$3->empty())
1110         *$2 += " " + *$3;
1111       delete $3;
1112       $$ = $2;
1113     };
1114
1115 GlobalVarAttribute 
1116     : SectionString 
1117     | ALIGN EUINT64VAL {
1118       *$1 += " " + *$2;
1119       delete $2;
1120       $$ = $1;
1121     };
1122
1123 //===----------------------------------------------------------------------===//
1124 // Types includes all predefined types... except void, because it can only be
1125 // used in specific contexts (function returning void for example).  To have
1126 // access to it, a user must explicitly use TypesV.
1127 //
1128
1129 // TypesV includes all of 'Types', but it also includes the void type.
1130 TypesV    : Types    | VOID ;
1131 UpRTypesV : UpRTypes | VOID ; 
1132 Types     : UpRTypes ;
1133
1134 // Derived types are added later...
1135 //
1136 PrimType : BOOL | SBYTE | UBYTE | SHORT  | USHORT | INT   | UINT ;
1137 PrimType : LONG | ULONG | FLOAT | DOUBLE | LABEL;
1138 UpRTypes 
1139   : OPAQUE { 
1140     $$ = TypeInfo::get(*$1, OpaqueTy);
1141   } 
1142   | SymbolicValueRef { 
1143     $$ = TypeInfo::get(*$1, UnresolvedTy);
1144   }
1145   | PrimType { 
1146     $$ = $1; 
1147   }
1148   | '\\' EUINT64VAL {                   // Type UpReference
1149     $2->insert(0, "\\");
1150     $$ = TypeInfo::get(*$2, UpRefTy);
1151   }
1152   | UpRTypesV '(' ArgTypeListI ')' {           // Function derived type?
1153     std::string newTy( $1->getNewTy() + "(");
1154     for (unsigned i = 0; i < $3->size(); ++i) {
1155       if (i != 0)
1156         newTy +=  ", ";
1157       if ((*$3)[i]->isVoid())
1158         newTy += "...";
1159       else
1160         newTy += (*$3)[i]->getNewTy();
1161     }
1162     newTy += ")";
1163     $$ = TypeInfo::get(newTy, $1, $3);
1164   }
1165   | '[' EUINT64VAL 'x' UpRTypes ']' {          // Sized array type?
1166     uint64_t elems = atoi($2->c_str());
1167     $2->insert(0,"[ ");
1168     *$2 += " x " + $4->getNewTy() + " ]";
1169     $$ = TypeInfo::get(*$2, ArrayTy, $4, elems);
1170   }
1171   | '<' EUINT64VAL 'x' UpRTypes '>' {          // Packed array type?
1172     uint64_t elems = atoi($2->c_str());
1173     $2->insert(0,"< ");
1174     *$2 += " x " + $4->getNewTy() + " >";
1175     $$ = TypeInfo::get(*$2, PackedTy, $4, elems);
1176   }
1177   | '{' TypeListI '}' {                        // Structure type?
1178     std::string newTy("{");
1179     for (unsigned i = 0; i < $2->size(); ++i) {
1180       if (i != 0)
1181         newTy +=  ", ";
1182       newTy += (*$2)[i]->getNewTy();
1183     }
1184     newTy += "}";
1185     $$ = TypeInfo::get(newTy, StructTy, $2);
1186   }
1187   | '{' '}' {                                  // Empty structure type?
1188     $$ = TypeInfo::get("{}", StructTy, new TypeList());
1189   }
1190   | '<' '{' TypeListI '}' '>' {                // Packed Structure type?
1191     std::string newTy("<{");
1192     for (unsigned i = 0; i < $3->size(); ++i) {
1193       if (i != 0)
1194         newTy +=  ", ";
1195       newTy += (*$3)[i]->getNewTy();
1196     }
1197     newTy += "}>";
1198     $$ = TypeInfo::get(newTy, PackedStructTy, $3);
1199   }
1200   | '<' '{' '}' '>' {                          // Empty packed structure type?
1201     $$ = TypeInfo::get("<{}>", PackedStructTy, new TypeList());
1202   }
1203   | UpRTypes '*' {                             // Pointer type?
1204     $$ = $1->getPointerType();
1205   };
1206
1207 // TypeList - Used for struct declarations and as a basis for function type 
1208 // declaration type lists
1209 //
1210 TypeListI 
1211   : UpRTypes {
1212     $$ = new TypeList();
1213     $$->push_back($1);
1214   }
1215   | TypeListI ',' UpRTypes {
1216     $$ = $1;
1217     $$->push_back($3);
1218   };
1219
1220 // ArgTypeList - List of types for a function type declaration...
1221 ArgTypeListI 
1222   : TypeListI 
1223   | TypeListI ',' DOTDOTDOT {
1224     $$ = $1;
1225     $$->push_back(TypeInfo::get("void",VoidTy));
1226     delete $3;
1227   }
1228   | DOTDOTDOT {
1229     $$ = new TypeList();
1230     $$->push_back(TypeInfo::get("void",VoidTy));
1231     delete $1;
1232   }
1233   | /*empty*/ {
1234     $$ = new TypeList();
1235   };
1236
1237 // ConstVal - The various declarations that go into the constant pool.  This
1238 // production is used ONLY to represent constants that show up AFTER a 'const',
1239 // 'constant' or 'global' token at global scope.  Constants that can be inlined
1240 // into other expressions (such as integers and constexprs) are handled by the
1241 // ResolvedVal, ValueRef and ConstValueRef productions.
1242 //
1243 ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
1244     $$ = new ConstInfo;
1245     $$->type = $1;
1246     $$->cnst = new std::string($1->getNewTy());
1247     *$$->cnst += " [ " + *$3 + " ]";
1248     delete $3;
1249   }
1250   | Types '[' ']' {
1251     $$ = new ConstInfo;
1252     $$->type = $1;
1253     $$->cnst = new std::string($1->getNewTy());
1254     *$$->cnst += "[ ]";
1255   }
1256   | Types 'c' STRINGCONSTANT {
1257     $$ = new ConstInfo;
1258     $$->type = $1;
1259     $$->cnst = new std::string($1->getNewTy());
1260     *$$->cnst += " c" + *$3;
1261     delete $3;
1262   }
1263   | Types '<' ConstVector '>' { // Nonempty unsized arr
1264     $$ = new ConstInfo;
1265     $$->type = $1;
1266     $$->cnst = new std::string($1->getNewTy());
1267     *$$->cnst += " < " + *$3 + " >";
1268     delete $3;
1269   }
1270   | Types '{' ConstVector '}' {
1271     $$ = new ConstInfo;
1272     $$->type = $1;
1273     $$->cnst = new std::string($1->getNewTy());
1274     *$$->cnst += " { " + *$3 + " }";
1275     delete $3;
1276   }
1277   | Types '{' '}' {
1278     $$ = new ConstInfo;
1279     $$->type = $1;
1280     $$->cnst = new std::string($1->getNewTy());
1281     *$$->cnst += " {}";
1282   }
1283   | Types NULL_TOK {
1284     $$ = new ConstInfo;
1285     $$->type = $1;
1286     $$->cnst = new std::string($1->getNewTy());
1287     *$$->cnst +=  " " + *$2;
1288     delete $2;
1289   }
1290   | Types UNDEF {
1291     $$ = new ConstInfo;
1292     $$->type = $1;
1293     $$->cnst = new std::string($1->getNewTy());
1294     *$$->cnst += " " + *$2;
1295     delete $2;
1296   }
1297   | Types SymbolicValueRef {
1298     $$ = new ConstInfo;
1299     std::string Name = getUniqueName($2, $1->resolve(), true);
1300     $$->type = $1;
1301     $$->cnst = new std::string($1->getNewTy());
1302     *$$->cnst += " " + Name;
1303     delete $2;
1304   }
1305   | Types ConstExpr {
1306     $$ = new ConstInfo;
1307     $$->type = $1;
1308     $$->cnst = new std::string($1->getNewTy());
1309     *$$->cnst += " " + *$2;
1310     delete $2;
1311   }
1312   | Types ZEROINITIALIZER {
1313     $$ = new ConstInfo;
1314     $$->type = $1;
1315     $$->cnst = new std::string($1->getNewTy());
1316     *$$->cnst += " " + *$2;
1317     delete $2;
1318   }
1319   | SIntType EInt64Val {      // integral constants
1320     $$ = new ConstInfo;
1321     $$->type = $1;
1322     $$->cnst = new std::string($1->getNewTy());
1323     *$$->cnst += " " + *$2;
1324     delete $2;
1325   }
1326   | UIntType EInt64Val {            // integral constants
1327     $$ = new ConstInfo;
1328     $$->type = $1;
1329     $$->cnst = new std::string($1->getNewTy());
1330     *$$->cnst += " " + *$2;
1331     delete $2;
1332   }
1333   | BOOL TRUETOK {                      // Boolean constants
1334     $$ = new ConstInfo;
1335     $$->type = $1;
1336     $$->cnst = new std::string($1->getNewTy());
1337     *$$->cnst += " " + *$2;
1338     delete $2;
1339   }
1340   | BOOL FALSETOK {                     // Boolean constants
1341     $$ = new ConstInfo;
1342     $$->type = $1;
1343     $$->cnst = new std::string($1->getNewTy());
1344     *$$->cnst += " " + *$2;
1345     delete $2;
1346   }
1347   | FPType FPVAL {                   // Float & Double constants
1348     $$ = new ConstInfo;
1349     $$->type = $1;
1350     $$->cnst = new std::string($1->getNewTy());
1351     *$$->cnst += " " + *$2;
1352     delete $2;
1353   };
1354
1355 ConstExpr: CastOps '(' ConstVal TO Types ')' {
1356     std::string source = *$3->cnst;
1357     const TypeInfo* SrcTy = $3->type->resolve();
1358     const TypeInfo* DstTy = $5->resolve(); 
1359     if (*$1 == "cast") {
1360       // Call getCastUpgrade to upgrade the old cast
1361       $$ = new std::string(getCastUpgrade(source, SrcTy, DstTy, true));
1362     } else {
1363       // Nothing to upgrade, just create the cast constant expr
1364       $$ = new std::string(*$1);
1365       *$$ += "( " + source + " to " + $5->getNewTy() + ")";
1366     }
1367     delete $1; delete $3; delete $4;
1368   }
1369   | GETELEMENTPTR '(' ConstVal IndexList ')' {
1370     *$1 += "(" + *$3->cnst;
1371     for (unsigned i = 0; i < $4->size(); ++i) {
1372       ValueInfo* VI = (*$4)[i];
1373       *$1 += ", " + *VI->val;
1374       delete VI;
1375     }
1376     *$1 += ")";
1377     $$ = $1;
1378     delete $3;
1379     delete $4;
1380   }
1381   | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1382     *$1 += "(" + *$3->cnst + "," + *$5->cnst + "," + *$7->cnst + ")";
1383     delete $3; delete $5; delete $7;
1384     $$ = $1;
1385   }
1386   | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
1387     const char* op = getDivRemOpcode(*$1, $3->type); 
1388     $$ = new std::string(op);
1389     *$$ += "(" + *$3->cnst + "," + *$5->cnst + ")";
1390     delete $1; delete $3; delete $5;
1391   }
1392   | LogicalOps '(' ConstVal ',' ConstVal ')' {
1393     *$1 += "(" + *$3->cnst + "," + *$5->cnst + ")";
1394     delete $3; delete $5;
1395     $$ = $1;
1396   }
1397   | SetCondOps '(' ConstVal ',' ConstVal ')' {
1398     *$1 = getCompareOp(*$1, $3->type);
1399     *$1 += "(" + *$3->cnst + "," + *$5->cnst + ")";
1400     delete $3; delete $5;
1401     $$ = $1;
1402   }
1403   | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1404     *$1 += " " + *$2 + " (" +  *$4->cnst + "," + *$6->cnst + ")";
1405     delete $2; delete $4; delete $6;
1406     $$ = $1;
1407   }
1408   | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
1409     *$1 += " " + *$2 + " (" + *$4->cnst + "," + *$6->cnst + ")";
1410     delete $2; delete $4; delete $6;
1411     $$ = $1;
1412   }
1413   | ShiftOps '(' ConstVal ',' ConstVal ')' {
1414     const char* shiftop = $1->c_str();
1415     if (*$1 == "shr")
1416       shiftop = ($3->type->isUnsigned()) ? "lshr" : "ashr";
1417     $$ = new std::string(shiftop);
1418     *$$ += "(" + *$3->cnst + "," + *$5->cnst + ")";
1419     delete $1; delete $3; delete $5;
1420   }
1421   | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
1422     *$1 += "(" + *$3->cnst + "," + *$5->cnst + ")";
1423     delete $3; delete $5;
1424     $$ = $1;
1425   }
1426   | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1427     *$1 += "(" + *$3->cnst + "," + *$5->cnst + "," + *$7->cnst + ")";
1428     delete $3; delete $5; delete $7;
1429     $$ = $1;
1430   }
1431   | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1432     *$1 += "(" + *$3->cnst + "," + *$5->cnst + "," + *$7->cnst + ")";
1433     delete $3; delete $5; delete $7;
1434     $$ = $1;
1435   };
1436
1437
1438 // ConstVector - A list of comma separated constants.
1439
1440 ConstVector 
1441   : ConstVector ',' ConstVal {
1442     *$1 += ", " + *$3->cnst;
1443     delete $3;
1444     $$ = $1;
1445   }
1446   | ConstVal { $$ = new std::string(*$1->cnst); delete $1; }
1447   ;
1448
1449
1450 // GlobalType - Match either GLOBAL or CONSTANT for global declarations...
1451 GlobalType : GLOBAL | CONSTANT ;
1452
1453
1454 //===----------------------------------------------------------------------===//
1455 //                             Rules to match Modules
1456 //===----------------------------------------------------------------------===//
1457
1458 // Module rule: Capture the result of parsing the whole file into a result
1459 // variable...
1460 //
1461 Module : DefinitionList {
1462 };
1463
1464 // DefinitionList - Top level definitions
1465 //
1466 DefinitionList : DefinitionList Function {
1467     $$ = 0;
1468   } 
1469   | DefinitionList FunctionProto {
1470     *O << *$2 << '\n';
1471     delete $2;
1472     $$ = 0;
1473   }
1474   | DefinitionList MODULE ASM_TOK AsmBlock {
1475     *O << "module asm " << ' ' << *$4 << '\n';
1476     $$ = 0;
1477   }  
1478   | DefinitionList IMPLEMENTATION {
1479     *O << "implementation\n";
1480     $$ = 0;
1481   }
1482   | ConstPool { $$ = 0; }
1483
1484 External : EXTERNAL | UNINITIALIZED { $$ = $1; *$$ = "external"; }
1485
1486 // ConstPool - Constants with optional names assigned to them.
1487 ConstPool : ConstPool OptAssign TYPE TypesV {
1488     TypeInfo::EnumeratedTypes.push_back($4);
1489     if (!$2->empty()) {
1490       TypeInfo::NamedTypes[*$2] = $4;
1491       *O << *$2 << " = ";
1492     }
1493     *O << "type " << $4->getNewTy() << '\n';
1494     delete $2; delete $3;
1495     $$ = 0;
1496   }
1497   | ConstPool FunctionProto {       // Function prototypes can be in const pool
1498     *O << *$2 << '\n';
1499     delete $2;
1500     $$ = 0;
1501   }
1502   | ConstPool MODULE ASM_TOK AsmBlock {  // Asm blocks can be in the const pool
1503     *O << *$2 << ' ' << *$3 << ' ' << *$4 << '\n';
1504     delete $2; delete $3; delete $4; 
1505     $$ = 0;
1506   }
1507   | ConstPool OptAssign OptLinkage GlobalType ConstVal  GlobalVarAttributes {
1508     if (!$2->empty()) {
1509       std::string Name = getGlobalName($2,*$3, $5->type->getPointerType(),
1510                                        *$4 == "constant");
1511       *O << Name << " = ";
1512     }
1513     *O << *$3 << ' ' << *$4 << ' ' << *$5->cnst << ' ' << *$6 << '\n';
1514     delete $2; delete $3; delete $4; delete $6; 
1515     $$ = 0;
1516   }
1517   | ConstPool OptAssign External GlobalType Types  GlobalVarAttributes {
1518     if (!$2->empty()) {
1519       std::string Name = getGlobalName($2,*$3,$5->getPointerType(),
1520                                        *$4 == "constant");
1521       *O << Name << " = ";
1522     }
1523     *O <<  *$3 << ' ' << *$4 << ' ' << $5->getNewTy() << ' ' << *$6 << '\n';
1524     delete $2; delete $3; delete $4; delete $6;
1525     $$ = 0;
1526   }
1527   | ConstPool OptAssign DLLIMPORT GlobalType Types GlobalVarAttributes {
1528     if (!$2->empty()) {
1529       std::string Name = getGlobalName($2,*$3,$5->getPointerType(),
1530                                        *$4 == "constant");
1531       *O << Name << " = ";
1532     }
1533     *O << *$3 << ' ' << *$4 << ' ' << $5->getNewTy() << ' ' << *$6 << '\n';
1534     delete $2; delete $3; delete $4; delete $6;
1535     $$ = 0;
1536   }
1537   | ConstPool OptAssign EXTERN_WEAK GlobalType Types  GlobalVarAttributes {
1538     if (!$2->empty()) {
1539       std::string Name = getGlobalName($2,*$3,$5->getPointerType(),
1540                                        *$4 == "constant");
1541       *O << Name << " = ";
1542     }
1543     *O << *$3 << ' ' << *$4 << ' ' << $5->getNewTy() << ' ' << *$6 << '\n';
1544     delete $2; delete $3; delete $4; delete $6;
1545     $$ = 0;
1546   }
1547   | ConstPool TARGET TargetDefinition { 
1548     *O << *$2 << ' ' << *$3 << '\n';
1549     delete $2; delete $3;
1550     $$ = 0;
1551   }
1552   | ConstPool DEPLIBS '=' LibrariesDefinition {
1553     *O << *$2 << " = " << *$4 << '\n';
1554     delete $2; delete $4;
1555     $$ = 0;
1556   }
1557   | /* empty: end of list */ { 
1558     $$ = 0;
1559   };
1560
1561
1562 AsmBlock : STRINGCONSTANT ;
1563
1564 BigOrLittle : BIG | LITTLE 
1565
1566 TargetDefinition 
1567   : ENDIAN '=' BigOrLittle {
1568     *$1 += " = " + *$3;
1569     delete $3;
1570     $$ = $1;
1571   }
1572   | POINTERSIZE '=' EUINT64VAL {
1573     *$1 += " = " + *$3;
1574     if (*$3 == "64")
1575       SizeOfPointer = 64;
1576     delete $3;
1577     $$ = $1;
1578   }
1579   | TRIPLE '=' STRINGCONSTANT {
1580     *$1 += " = " + *$3;
1581     delete $3;
1582     $$ = $1;
1583   }
1584   | DATALAYOUT '=' STRINGCONSTANT {
1585     *$1 += " = " + *$3;
1586     delete $3;
1587     $$ = $1;
1588   };
1589
1590 LibrariesDefinition 
1591   : '[' LibList ']' {
1592     $2->insert(0, "[ ");
1593     *$2 += " ]";
1594     $$ = $2;
1595   };
1596
1597 LibList 
1598   : LibList ',' STRINGCONSTANT {
1599     *$1 += ", " + *$3;
1600     delete $3;
1601     $$ = $1;
1602   }
1603   | STRINGCONSTANT 
1604   | /* empty: end of list */ {
1605     $$ = new std::string();
1606   };
1607
1608 //===----------------------------------------------------------------------===//
1609 //                       Rules to match Function Headers
1610 //===----------------------------------------------------------------------===//
1611
1612 Name : VAR_ID | STRINGCONSTANT;
1613 OptName : Name | /*empty*/ { $$ = new std::string(); };
1614
1615 ArgVal : Types OptName {
1616   $$ = new std::string($1->getNewTy());
1617   if (!$2->empty()) {
1618     std::string Name = getUniqueName($2, $1->resolve());
1619     *$$ += " " + Name;
1620   }
1621   delete $2;
1622 };
1623
1624 ArgListH : ArgListH ',' ArgVal {
1625     *$1 += ", " + *$3;
1626     delete $3;
1627   }
1628   | ArgVal {
1629     $$ = $1;
1630   };
1631
1632 ArgList : ArgListH {
1633     $$ = $1;
1634   }
1635   | ArgListH ',' DOTDOTDOT {
1636     *$1 += ", ...";
1637     $$ = $1;
1638     delete $3;
1639   }
1640   | DOTDOTDOT {
1641     $$ = $1;
1642   }
1643   | /* empty */ { $$ = new std::string(); };
1644
1645 FunctionHeaderH 
1646   : OptCallingConv TypesV Name '(' ArgList ')' OptSection OptAlign {
1647     if (*$3 == "%llvm.va_start" || *$3 == "%llvm.va_end") {
1648       *$5 = "i8*";
1649     } else if (*$3 == "%llvm.va_copy") {
1650       *$5 = "i8*, i8*";
1651     }
1652     if (!$1->empty()) {
1653       *$1 += " ";
1654     }
1655     *$1 += $2->getNewTy() + " " + *$3 + "(" + *$5 + ")";
1656     if (!$7->empty()) {
1657       *$1 += " " + *$7;
1658     }
1659     if (!$8->empty()) {
1660       *$1 += " " + *$8;
1661     }
1662     delete $3;
1663     delete $5;
1664     delete $7;
1665     delete $8;
1666     $$ = $1;
1667   };
1668
1669 BEGIN : BEGINTOK { $$ = new std::string("{"); delete $1; }
1670   | '{' { $$ = new std::string ("{"); }
1671
1672 FunctionHeader 
1673   : OptLinkage FunctionHeaderH BEGIN {
1674     *O << "define ";
1675     if (!$1->empty()) {
1676       *O << *$1 << ' ';
1677     }
1678     *O << *$2 << ' ' << *$3 << '\n';
1679     delete $1; delete $2; delete $3;
1680     $$ = 0;
1681   }
1682   ;
1683
1684 END : ENDTOK { $$ = new std::string("}"); delete $1; }
1685     | '}' { $$ = new std::string("}"); };
1686
1687 Function : FunctionHeader BasicBlockList END {
1688   if ($2)
1689     *O << *$2;
1690   *O << *$3 << "\n\n";
1691   delete $1; delete $2; delete $3;
1692   $$ = 0;
1693 };
1694
1695 FnDeclareLinkage
1696   : /*default*/ { $$ = new std::string(); }
1697   | DLLIMPORT    
1698   | EXTERN_WEAK 
1699   ;
1700   
1701 FunctionProto 
1702   : DECLARE { isDeclare = true; } FnDeclareLinkage FunctionHeaderH { 
1703     if (!$3->empty())
1704       *$1 += " " + *$3;
1705     *$1 += " " + *$4;
1706     delete $3;
1707     delete $4;
1708     $$ = $1;
1709     isDeclare = false;
1710   };
1711
1712 //===----------------------------------------------------------------------===//
1713 //                        Rules to match Basic Blocks
1714 //===----------------------------------------------------------------------===//
1715
1716 OptSideEffect : /* empty */ { $$ = new std::string(); }
1717   | SIDEEFFECT;
1718
1719 ConstValueRef 
1720   : ESINT64VAL | EUINT64VAL | FPVAL | TRUETOK | FALSETOK | NULL_TOK | UNDEF
1721   | ZEROINITIALIZER 
1722   | '<' ConstVector '>' { 
1723     $2->insert(0, "<");
1724     *$2 += ">";
1725     $$ = $2;
1726   }
1727   | ConstExpr 
1728   | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
1729     if (!$2->empty()) {
1730       *$1 += " " + *$2;
1731     }
1732     *$1 += " " + *$3 + ", " + *$5;
1733     delete $2; delete $3; delete $5;
1734     $$ = $1;
1735   };
1736
1737 SymbolicValueRef : IntVal | Name ;
1738
1739 // ValueRef - A reference to a definition... either constant or symbolic
1740 ValueRef 
1741   : SymbolicValueRef {
1742     $$ = new ValueInfo;
1743     $$->val = $1;
1744     $$->constant = false;
1745     $$->type = 0;
1746   }
1747   | ConstValueRef {
1748     $$ = new ValueInfo;
1749     $$->val = $1;
1750     $$->constant = true;
1751     $$->type = 0;
1752   }
1753   ;
1754
1755 // ResolvedVal - a <type> <value> pair.  This is used only in cases where the
1756 // type immediately preceeds the value reference, and allows complex constant
1757 // pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
1758 ResolvedVal : Types ValueRef {
1759     $1 = $1->resolve();
1760     std::string Name = getUniqueName($2->val, $1);
1761     $$ = $2;
1762     delete $$->val;
1763     $$->val = new std::string($1->getNewTy() + " " + Name);
1764     $$->type = $1;
1765   };
1766
1767 BasicBlockList : BasicBlockList BasicBlock {
1768     $$ = 0;
1769   }
1770   | BasicBlock { // Do not allow functions with 0 basic blocks   
1771     $$ = 0;
1772   };
1773
1774
1775 // Basic blocks are terminated by branching instructions: 
1776 // br, br/cc, switch, ret
1777 //
1778 BasicBlock : InstructionList BBTerminatorInst  {
1779     $$ = 0;
1780   };
1781
1782 InstructionList : InstructionList Inst {
1783     *O << "    " << *$2 << '\n';
1784     delete $2;
1785     $$ = 0;
1786   }
1787   | /* empty */ {
1788     $$ = 0;
1789   }
1790   | LABELSTR {
1791     *O << *$1 << '\n';
1792     delete $1;
1793     $$ = 0;
1794   };
1795
1796 Unwind : UNWIND | EXCEPT { $$ = $1; *$$ = "unwind"; }
1797
1798 BBTerminatorInst : RET ResolvedVal {              // Return with a result...
1799     *O << "    " << *$1 << ' ' << *$2->val << '\n';
1800     delete $1; delete $2;
1801     $$ = 0;
1802   }
1803   | RET VOID {                                       // Return with no result...
1804     *O << "    " << *$1 << ' ' << $2->getNewTy() << '\n';
1805     delete $1;
1806     $$ = 0;
1807   }
1808   | BR LABEL ValueRef {                         // Unconditional Branch...
1809     *O << "    " << *$1 << ' ' << $2->getNewTy() << ' ' << *$3->val << '\n';
1810     delete $1; delete $3;
1811     $$ = 0;
1812   }                                                  // Conditional Branch...
1813   | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {  
1814     std::string Name = getUniqueName($3->val, $2);
1815     *O << "    " << *$1 << ' ' << $2->getNewTy() << ' ' << Name << ", " 
1816        << $5->getNewTy() << ' ' << *$6->val << ", " << $8->getNewTy() << ' ' 
1817        << *$9->val << '\n';
1818     delete $1; delete $3; delete $6; delete $9;
1819     $$ = 0;
1820   }
1821   | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1822     std::string Name = getUniqueName($3->val, $2);
1823     *O << "    " << *$1 << ' ' << $2->getNewTy() << ' ' << Name << ", " 
1824        << $5->getNewTy() << ' ' << *$6->val << " [" << *$8 << " ]\n";
1825     delete $1; 
1826     delete $3;
1827     delete $6; 
1828     delete $8;
1829     $$ = 0;
1830   }
1831   | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
1832     std::string Name = getUniqueName($3->val, $2);
1833     *O << "    " << *$1 << ' ' << $2->getNewTy() << ' ' << Name << ", " 
1834        << $5->getNewTy() << ' ' << *$6->val << "[]\n";
1835     delete $1; 
1836     delete $3; 
1837     delete $6;
1838     $$ = 0;
1839   }
1840   | OptAssign INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
1841     TO LABEL ValueRef Unwind LABEL ValueRef {
1842     const TypeInfo* ResTy = getFunctionReturnType($4);
1843     *O << "    ";
1844     if (!$1->empty()) {
1845       std::string Name = getUniqueName($1, ResTy);
1846       *O << Name << " = ";
1847     }
1848     *O << *$2 << ' ' << *$3 << ' ' << $4->getNewTy() << ' ' << *$5->val << " (";
1849     for (unsigned i = 0; i < $7->size(); ++i) {
1850       ValueInfo* VI = (*$7)[i];
1851       *O << *VI->val;
1852       if (i+1 < $7->size())
1853         *O << ", ";
1854       delete VI;
1855     }
1856     *O << ") " << *$9 << ' ' << $10->getNewTy() << ' ' << *$11->val << ' ' 
1857        << *$12 << ' ' << $13->getNewTy() << ' ' << *$14->val << '\n';
1858     delete $1; delete $2; delete $3; delete $5; delete $7; 
1859     delete $9; delete $11; delete $12; delete $14; 
1860     $$ = 0;
1861   }
1862   | Unwind {
1863     *O << "    " << *$1 << '\n';
1864     delete $1;
1865     $$ = 0;
1866   }
1867   | UNREACHABLE {
1868     *O << "    " << *$1 << '\n';
1869     delete $1;
1870     $$ = 0;
1871   };
1872
1873 JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1874     *$1 += " " + $2->getNewTy() + " " + *$3 + ", " + $5->getNewTy() + " " + 
1875            *$6->val;
1876     delete $3; delete $6;
1877     $$ = $1;
1878   }
1879   | IntType ConstValueRef ',' LABEL ValueRef {
1880     $2->insert(0, $1->getNewTy() + " " );
1881     *$2 += ", " + $4->getNewTy() + " " + *$5->val;
1882     delete $5;
1883     $$ = $2;
1884   };
1885
1886 Inst 
1887   : OptAssign InstVal {
1888     if (!$1->empty()) {
1889       // Get a unique name for this value, based on its type.
1890       std::string Name = getUniqueName($1, $2->type);
1891       *$1 = Name + " = ";
1892       if (deleteUselessCastFlag && *deleteUselessCastName == Name) {
1893         // don't actually delete it, just comment it out
1894         $1->insert(0, "; USELSS BITCAST: "); 
1895         delete deleteUselessCastName;
1896       }
1897     }
1898     *$1 += *$2->val;
1899     delete $2;
1900     deleteUselessCastFlag = false;
1901     $$ = $1; 
1902   };
1903
1904 PHIList 
1905   : Types '[' ValueRef ',' ValueRef ']' {    // Used for PHI nodes
1906     std::string Name = getUniqueName($3->val, $1);
1907     Name.insert(0, $1->getNewTy() + "[");
1908     Name += "," + *$5->val + "]";
1909     $$ = new ValueInfo;
1910     $$->val = new std::string(Name);
1911     $$->type = $1;
1912     delete $3; delete $5;
1913   }
1914   | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1915     std::string Name = getUniqueName($4->val, $1->type);
1916     *$1->val += ", [" + Name + "," + *$6->val + "]";
1917     delete $4; 
1918     delete $6;
1919     $$ = $1;
1920   };
1921
1922
1923 ValueRefList 
1924   : ResolvedVal {
1925     $$ = new ValueList();
1926     $$->push_back($1);
1927   }
1928   | ValueRefList ',' ResolvedVal {
1929     $$ = $1;
1930     $$->push_back($3);
1931   };
1932
1933 // ValueRefListE - Just like ValueRefList, except that it may also be empty!
1934 ValueRefListE 
1935   : ValueRefList  { $$ = $1; }
1936   | /*empty*/ { $$ = new ValueList(); }
1937   ;
1938
1939 OptTailCall 
1940   : TAIL CALL {
1941     *$1 += " " + *$2;
1942     delete $2;
1943     $$ = $1;
1944   }
1945   | CALL 
1946   ;
1947
1948 InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1949     const char* op = getDivRemOpcode(*$1, $2); 
1950     std::string Name1 = getUniqueName($3->val, $2);
1951     std::string Name2 = getUniqueName($5->val, $2);
1952     $$ = $3;
1953     delete $$->val;
1954     $$->val = new std::string(op);
1955     *$$->val += " " + $2->getNewTy() + " " + Name1 + ", " + Name2;
1956     $$->type = $2;
1957     delete $1; delete $5;
1958   }
1959   | LogicalOps Types ValueRef ',' ValueRef {
1960     std::string Name1 = getUniqueName($3->val, $2);
1961     std::string Name2 = getUniqueName($5->val, $2);
1962     *$1 += " " + $2->getNewTy() + " " + Name1 + ", " + Name2;
1963     $$ = $3;
1964     delete $$->val;
1965     $$->val = $1;
1966     $$->type = $2;
1967     delete $5;
1968   }
1969   | SetCondOps Types ValueRef ',' ValueRef {
1970     std::string Name1 = getUniqueName($3->val, $2);
1971     std::string Name2 = getUniqueName($5->val, $2);
1972     *$1 = getCompareOp(*$1, $2);
1973     *$1 += " " + $2->getNewTy() + " " + Name1 + ", " + Name2;
1974     $$ = $3;
1975     delete $$->val;
1976     $$->val = $1;
1977     $$->type = TypeInfo::get("i1",BoolTy);
1978     delete $5;
1979   }
1980   | ICMP IPredicates Types ValueRef ',' ValueRef {
1981     std::string Name1 = getUniqueName($4->val, $3);
1982     std::string Name2 = getUniqueName($6->val, $3);
1983     *$1 += " " + *$2 + " " + $3->getNewTy() + " " + Name1 + "," + Name2;
1984     $$ = $4;
1985     delete $$->val;
1986     $$->val = $1;
1987     $$->type = TypeInfo::get("i1",BoolTy);
1988     delete $2; delete $6;
1989   }
1990   | FCMP FPredicates Types ValueRef ',' ValueRef {
1991     std::string Name1 = getUniqueName($4->val, $3);
1992     std::string Name2 = getUniqueName($6->val, $3);
1993     *$1 += " " + *$2 + " " + $3->getNewTy() + " " + Name1 + "," + Name2;
1994     $$ = $4;
1995     delete $$->val;
1996     $$->val = $1;
1997     $$->type = TypeInfo::get("i1",BoolTy);
1998     delete $2; delete $6;
1999   }
2000   | ShiftOps ResolvedVal ',' ResolvedVal {
2001     const char* shiftop = $1->c_str();
2002     if (*$1 == "shr")
2003       shiftop = ($2->type->isUnsigned()) ? "lshr" : "ashr";
2004     std::string *val = new std::string(shiftop);
2005     *val += " " + *$2->val + ", " + *$4->val;
2006     $$ = $2;
2007     delete $$->val;
2008     $$->val = val;
2009     delete $1; delete $4;
2010   }
2011   | CastOps ResolvedVal TO Types {
2012     std::string source = *$2->val;
2013     const TypeInfo* SrcTy = $2->type->resolve();
2014     const TypeInfo* DstTy = $4->resolve();
2015     $$ = $2;
2016     delete $$->val;
2017     $$->val = new std::string();
2018     $$->type = DstTy;
2019     if (*$1 == "cast") {
2020       *$$->val += getCastUpgrade(source, SrcTy, DstTy, false);
2021     } else {
2022       *$$->val += *$1 + " " + source + " to " + DstTy->getNewTy();
2023     }
2024     // Check to see if this is a useless cast of a value to the same name
2025     // and the same type. Such casts will probably cause redefinition errors
2026     // when assembled and perform no code gen action so just remove them.
2027     if (*$1 == "cast" || *$1 == "bitcast")
2028       if (SrcTy->isInteger() && DstTy->isInteger() &&
2029           SrcTy->getBitWidth() == DstTy->getBitWidth()) {
2030         deleteUselessCastFlag = true; // Flag the "Inst" rule
2031         deleteUselessCastName = new std::string(*$2->val); // save the name
2032         size_t pos = deleteUselessCastName->find_first_of("%\"",0);
2033         if (pos != std::string::npos) {
2034           // remove the type portion before val
2035           deleteUselessCastName->erase(0, pos);
2036         }
2037       }
2038     delete $1; 
2039     delete $3;
2040   }
2041   | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
2042     *$1 += " " + *$2->val + ", " + *$4->val + ", " + *$6->val;
2043     $$ = $2;
2044     delete $$->val;
2045     $$->val = $1;
2046     $$->type = $4->type;
2047     delete $4;
2048     delete $6;
2049   }
2050   | VAARG ResolvedVal ',' Types {
2051     *$1 += " " + *$2->val + ", " + $4->getNewTy();
2052     $$ = $2;
2053     delete $$->val;
2054     $$->val = $1;
2055     $$->type = $4;
2056   }
2057   | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
2058     *$1 += " " + *$2->val + ", " + *$4->val;
2059     $$ = $2;
2060     delete $$->val;
2061     $$->val = $1;
2062     $$->type = $$->type->resolve();
2063     $$->type = $$->type->getElementType();
2064     delete $4;
2065   }
2066   | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
2067     *$1 += " " + *$2->val + ", " + *$4->val + ", " + *$6->val;
2068     $$ = $2;
2069     delete $$->val;
2070     $$->val = $1;
2071     delete $4; delete $6;
2072   }
2073   | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
2074     *$1 += " " + *$2->val + ", " + *$4->val + ", " + *$6->val;
2075     $$ = $2;
2076     delete $$->val;
2077     $$->val = $1;
2078     delete $4; delete $6;
2079   }
2080   | PHI_TOK PHIList {
2081     *$1 += " " + *$2->val;
2082     $$ = $2;
2083     delete $2->val;
2084     $$->val = $1;
2085   }
2086   | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')'  {
2087     // map llvm.isunordered to "fcmp uno" 
2088     $$ = new ValueInfo;
2089     if (*$4->val == "%llvm.isunordered.f32" ||
2090         *$4->val == "%llvm.isunordered.f64") {
2091       $$->val = new std::string( "fcmp uno " + *(*$6)[0]->val + ", ");
2092       size_t pos = (*$6)[1]->val->find(' ');
2093       assert(pos != std::string::npos && "no space?");
2094       *$$->val += (*$6)[1]->val->substr(pos+1);
2095       $$->type = TypeInfo::get("i1", BoolTy);
2096     } else {
2097       static unsigned upgradeCount = 1;
2098       if (*$4->val == "%llvm.va_start" || *$4->val == "%llvm.va_end") {
2099         if (!$6->empty()) {
2100           std::string name("%va_upgrade");
2101           name += llvm::utostr(upgradeCount++);
2102           $1->insert(0, name + " = bitcast " + *(*$6)[0]->val + " to i8*\n    ");
2103           *(*$6)[0]->val = "i8* " + name;
2104           (*$6)[0]->type = TypeInfo::get("i8", UByteTy)->getPointerType();
2105         }
2106       } else if (*$4->val == "%llvm.va_copy") {
2107         std::string name0("%va_upgrade");
2108         name0 += llvm::utostr(upgradeCount++);
2109         std::string name1("%va_upgrade");
2110         name1 += llvm::utostr(upgradeCount++);
2111         $1->insert(0, name0 + " = bitcast " + *(*$6)[0]->val + " to i8*\n    " +
2112                       name1 + " = bitcast " + *(*$6)[1]->val + " to i8*\n    ");
2113         *(*$6)[0]->val = "i8* " + name0;
2114         (*$6)[0]->type = TypeInfo::get("i8", UByteTy)->getPointerType();
2115         *(*$6)[1]->val = "i8* " + name1;
2116         (*$6)[0]->type = TypeInfo::get("i8", UByteTy)->getPointerType();
2117       }
2118       if (!$2->empty())
2119         *$1 += " " + *$2;
2120       if (!$1->empty())
2121         *$1 += " ";
2122       *$1 += $3->getNewTy() + " " + *$4->val + "(";
2123       for (unsigned i = 0; i < $6->size(); ++i) {
2124         ValueInfo* VI = (*$6)[i];
2125         *$1 += *VI->val;
2126         if (i+1 < $6->size())
2127           *$1 += ", ";
2128         delete VI;
2129       }
2130       *$1 += ")";
2131       $$ = new ValueInfo;
2132       $$->val = $1;
2133       $$->type = getFunctionReturnType($3);
2134     }
2135     delete $2; delete $4; delete $6;
2136   }
2137   | MemoryInst ;
2138
2139
2140 // IndexList - List of indices for GEP based instructions...
2141 IndexList 
2142   : ',' ValueRefList { $$ = $2; }
2143   | /* empty */ {  $$ = new ValueList(); }
2144   ;
2145
2146 OptVolatile 
2147   : VOLATILE 
2148   | /* empty */ { $$ = new std::string(); }
2149   ;
2150
2151 MemoryInst : MALLOC Types OptCAlign {
2152     *$1 += " " + $2->getNewTy();
2153     if (!$3->empty())
2154       *$1 += " " + *$3;
2155     $$ = new ValueInfo;
2156     $$->val = $1;
2157     $$->type = $2->getPointerType();
2158     delete $3;
2159   }
2160   | MALLOC Types ',' UINT ValueRef OptCAlign {
2161     std::string Name = getUniqueName($5->val, $4);
2162     *$1 += " " + $2->getNewTy() + ", " + $4->getNewTy() + " " + Name;
2163     if (!$6->empty())
2164       *$1 += " " + *$6;
2165     $$ = new ValueInfo;
2166     $$->val = $1;
2167     $$->type = $2->getPointerType();
2168     delete $5; delete $6;
2169   }
2170   | ALLOCA Types OptCAlign {
2171     *$1 += " " + $2->getNewTy();
2172     if (!$3->empty())
2173       *$1 += " " + *$3;
2174     $$ = new ValueInfo;
2175     $$->val = $1;
2176     $$->type = $2->getPointerType();
2177     delete $3;
2178   }
2179   | ALLOCA Types ',' UINT ValueRef OptCAlign {
2180     std::string Name = getUniqueName($5->val, $4);
2181     *$1 += " " + $2->getNewTy() + ", " + $4->getNewTy() + " " + Name;
2182     if (!$6->empty())
2183       *$1 += " " + *$6;
2184     $$ = $5;
2185     delete $$->val;
2186     $$->val = $1;
2187     $$->type = $2->getPointerType();
2188     delete $6;
2189   }
2190   | FREE ResolvedVal {
2191     *$1 += " " + *$2->val;
2192     $$ = $2;
2193     delete $2->val;
2194     $$->val = $1;
2195     $$->type = TypeInfo::get("void", VoidTy); 
2196   }
2197   | OptVolatile LOAD Types ValueRef {
2198     std::string Name = getUniqueName($4->val, $3);
2199     if (!$1->empty())
2200       *$1 += " ";
2201     *$1 += *$2 + " " + $3->getNewTy() + " " + Name;
2202     $$ = $4;
2203     delete $$->val;
2204     $$->val = $1;
2205     $$->type = $3->getElementType();
2206     delete $2;
2207   }
2208   | OptVolatile STORE ResolvedVal ',' Types ValueRef {
2209     std::string Name = getUniqueName($6->val, $5);
2210     if (!$1->empty())
2211       *$1 += " ";
2212     *$1 += *$2 + " " + *$3->val + ", " + $5->getNewTy() + " " + Name;
2213     $$ = $3;
2214     delete $$->val;
2215     $$->val = $1;
2216     $$->type = TypeInfo::get("void", VoidTy);
2217     delete $2; delete $6;
2218   }
2219   | GETELEMENTPTR Types ValueRef IndexList {
2220     std::string Name = getUniqueName($3->val, $2);
2221     // Upgrade the indices
2222     for (unsigned i = 0; i < $4->size(); ++i) {
2223       ValueInfo* VI = (*$4)[i];
2224       if (VI->type->isUnsigned() && !VI->isConstant() && 
2225           VI->type->getBitWidth() < 64) {
2226         *O << "    %gep_upgrade" << UniqueNameCounter << " = zext " << *VI->val 
2227            << " to i64\n";
2228         *VI->val = "i64 %gep_upgrade" + llvm::utostr(UniqueNameCounter++);
2229         VI->type = TypeInfo::get("i64",ULongTy);
2230       }
2231     }
2232     *$1 += " " + $2->getNewTy() + " " + Name;
2233     for (unsigned i = 0; i < $4->size(); ++i) {
2234       ValueInfo* VI = (*$4)[i];
2235       *$1 += ", " + *VI->val;
2236     }
2237     $$ = $3;
2238     delete $$->val;
2239     $$->val = $1;
2240     $$->type = getGEPIndexedType($2,$4); 
2241     for (unsigned i = 0; i < $4->size(); ++i)
2242       delete (*$4)[i];
2243     delete $4;
2244   };
2245
2246 %%
2247
2248 int yyerror(const char *ErrorMsg) {
2249   std::string where 
2250     = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
2251                   + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
2252   std::string errMsg = where + "error: " + std::string(ErrorMsg) + 
2253                        " while reading ";
2254   if (yychar == YYEMPTY || yychar == 0)
2255     errMsg += "end-of-file.";
2256   else
2257     errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
2258   std::cerr << "llvm-upgrade: " << errMsg << '\n';
2259   *O << "llvm-upgrade parse failed.\n";
2260   exit(1);
2261 }
2262
2263 void warning(const std::string& ErrorMsg) {
2264   std::string where 
2265     = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
2266                   + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
2267   std::string errMsg = where + "warning: " + std::string(ErrorMsg) + 
2268                        " while reading ";
2269   if (yychar == YYEMPTY || yychar == 0)
2270     errMsg += "end-of-file.";
2271   else
2272     errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
2273   std::cerr << "llvm-upgrade: " << errMsg << '\n';
2274 }