1 //===-- UpgradeParser.y - Upgrade parser for llvm assmbly -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the bison parser for LLVM 1.9 assembly language.
12 //===----------------------------------------------------------------------===//
15 #include "UpgradeInternals.h"
21 #define YYERROR_VERBOSE 1
22 #define YYINCLUDED_STDLIB_H
25 int yylex(); // declaration" of xxx warnings.
29 static std::string CurFilename;
30 static std::ostream *O = 0;
31 std::istream* LexInput = 0;
32 unsigned SizeOfPointer = 32;
35 // This bool controls whether attributes are ever added to function declarations
36 // definitions and calls.
37 static bool AddAttributes = false;
39 static void warning(const std::string& msg);
41 void UpgradeAssembly(const std::string &infile, std::istream& in,
42 std::ostream &out, bool debug, bool addAttrs)
48 AddAttributes = addAttrs;
52 std::cerr << "llvm-upgrade: parse failed.\n";
53 out << "llvm-upgrade: parse failed.\n";
58 namespace { // Anonymous namespace to keep our implementation local
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.
68 bool isConstant() const { return constant; }
69 ~ValueInfo() { delete val; }
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".
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);
86 static const TypeInfo* get(const std::string& newType, Types oldType,
87 const TypeInfo *eTy, uint64_t elems);
89 static const TypeInfo* get(const std::string& newType, Types oldType,
92 static const TypeInfo* get(const std::string& newType, const TypeInfo* resTy,
95 const TypeInfo* resolve() const;
96 bool operator<(const TypeInfo& that) const;
98 bool sameNewTyAs(const TypeInfo* that) const {
99 return this->newTy == that->newTy;
102 bool sameOldTyAs(const TypeInfo* that) const;
104 Types getElementTy() const {
106 return elemTy->oldTy;
111 unsigned getUpRefNum() const {
112 assert(oldTy == UpRefTy && "Can't getUpRefNum on non upreference");
113 return atoi(&((getNewTy().c_str())[1])); // skip the slash
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;
120 const std::string& getNewTy() const { return newTy; }
121 const TypeInfo* getResultType() const { return resultTy; }
122 const TypeInfo* getElementType() const { return elemTy; }
124 const TypeInfo* getPointerType() const {
125 return get(newTy + "*", PointerTy, this, (TypeInfo*)0);
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;
137 bool isUnsigned() const {
138 return oldTy == UByteTy || oldTy == UShortTy ||
139 oldTy == UIntTy || oldTy == ULongTy;
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();
156 bool isAttributeCandidate() const {
157 return isIntegral() && getBitWidth() < 32;
160 bool isUnresolvedDeep() const;
162 unsigned getBitWidth() const;
164 const TypeInfo* getIndexedType(const ValueInfo* VI) const;
166 unsigned getNumStructElements() const {
167 return (elements ? elements->size() : 0);
170 const TypeInfo* getElement(unsigned idx) const {
172 if (idx < elements->size())
173 return (*elements)[idx];
179 : newTy(), oldTy(UnresolvedTy), elemTy(0), resultTy(0), elements(0),
183 TypeInfo(const TypeInfo& that); // do not implement
184 TypeInfo& operator=(const TypeInfo& that); // do not implement
186 ~TypeInfo() { delete elements; }
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");
197 typedef std::set<const TypeInfo*, ltfunctor> TypeRegMap;
199 static const TypeInfo* add_new_type(TypeInfo* existing);
207 static TypeRegMap registry;
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;
218 TypeInfo::TypeRegMap TypeInfo::registry;
219 TypeInfo::TypeVector TypeInfo::EnumeratedTypes;
220 TypeInfo::TypeMap TypeInfo::NamedTypes;
221 TypeInfo::GlobalsTypeMap TypeInfo::Globals;
223 const TypeInfo* TypeInfo::get(const std::string &newType, Types oldType) {
224 TypeInfo* Ty = new TypeInfo();
227 return add_new_type(Ty);
230 const TypeInfo* TypeInfo::get(const std::string& newType, Types oldType,
231 const TypeInfo* eTy, const TypeInfo* rTy) {
232 TypeInfo* Ty= new TypeInfo();
235 Ty->elemTy = const_cast<TypeInfo*>(eTy);
236 Ty->resultTy = const_cast<TypeInfo*>(rTy);
237 return add_new_type(Ty);
240 const TypeInfo* TypeInfo::get(const std::string& newType, Types oldType,
241 const TypeInfo *eTy, uint64_t elems) {
242 TypeInfo* Ty = new TypeInfo();
245 Ty->elemTy = const_cast<TypeInfo*>(eTy);
247 return add_new_type(Ty);
250 const TypeInfo* TypeInfo::get(const std::string& newType, Types oldType,
252 TypeInfo* Ty = new TypeInfo();
256 return add_new_type(Ty);
259 const TypeInfo* TypeInfo::get(const std::string& newType, const TypeInfo* resTy,
261 TypeInfo* Ty = new TypeInfo();
263 Ty->oldTy = FunctionTy;
264 Ty->resultTy = const_cast<TypeInfo*>(resTy);
266 return add_new_type(Ty);
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];
276 std::string msg("Can't resolve numbered type: ");
278 yyerror(msg.c_str());
281 TypeInfo::TypeMap::iterator I = NamedTypes.find(newTy);
282 if (I != NamedTypes.end()) {
285 std::string msg("Cannot resolve type: ");
287 yyerror(msg.c_str());
291 // otherwise its already resolved.
295 bool TypeInfo::operator<(const TypeInfo& that) const {
298 if (oldTy != that.oldTy)
299 return oldTy < that.oldTy;
302 unsigned thisUp = this->getUpRefNum();
303 unsigned thatUp = that.getUpRefNum();
304 return thisUp < thatUp;
308 if (this->nelems != that.nelems)
309 return nelems < that.nelems;
311 const TypeInfo* thisTy = this->elemTy;
312 const TypeInfo* thatTy = that.elemTy;
313 return *thisTy < *thatTy;
316 const TypeInfo* thisTy = this->resultTy;
317 const TypeInfo* thatTy = that.resultTy;
318 if (!thisTy->sameOldTyAs(thatTy))
319 return *thisTy < *thatTy;
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;
335 return this->newTy < that.newTy;
342 bool TypeInfo::sameOldTyAs(const TypeInfo* that) const {
347 if (oldTy != that->oldTy)
352 if (nelems != that->nelems)
356 const TypeInfo* thisTy = this->elemTy;
357 const TypeInfo* thatTy = that->elemTy;
358 return thisTy->sameOldTyAs(thatTy);
361 const TypeInfo* thisTy = this->resultTy;
362 const TypeInfo* thatTy = that->resultTy;
363 if (!thisTy->sameOldTyAs(thatTy))
368 case PackedStructTy: {
369 if (elements->size() != that->elements->size())
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))
380 return this->newTy == that->newTy;
382 return true; // for all others oldTy == that->oldTy is sufficient
387 bool TypeInfo::isUnresolvedDeep() const {
394 return elemTy->isUnresolvedDeep();
397 for (unsigned i = 0; i < elements->size(); i++)
398 if ((*elements)[i]->isUnresolvedDeep())
406 unsigned TypeInfo::getBitWidth() const {
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
419 return nelems * elemTy->getBitWidth();
421 case PackedStructTy: {
423 for (unsigned i = 0; i < elements->size(); i++) {
424 size += (*elements)[i]->getBitWidth();
431 const TypeInfo* TypeInfo::getIndexedType(const ValueInfo* VI) const {
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];
439 yyerror("Invalid value for constant integer");
443 yyerror("Structure requires constant index");
447 if (isArray() || isPacked() || isPointer())
449 yyerror("Invalid type for getIndexedType");
453 void TypeInfo::getSignedness(unsigned &sNum, unsigned &uNum,
454 UpRefStack& stack) const {
457 case OpaqueTy: case LabelTy: case VoidTy: case BoolTy:
458 case FloatTy : case DoubleTy: case UpRefTy:
460 case SByteTy: case ShortTy: case LongTy: case IntTy:
463 case UByteTy: case UShortTy: case UIntTy: case ULongTy:
469 stack.push_back(this);
470 elemTy->getSignedness(sNum, uNum, stack);
473 case PackedStructTy: {
474 stack.push_back(this);
475 for (unsigned i = 0; i < elements->size(); i++) {
476 (*elements)[i]->getSignedness(sNum, uNum, stack);
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)
487 Ty->getSignedness(sNum, uNum, stack);
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);
499 return Name + Suffix;
502 std::string TypeInfo::makeUniqueName(const std::string& BaseName) const {
503 if (BaseName == "\"alloca point\"")
508 case OpaqueTy: case LabelTy: case VoidTy: case BoolTy: case UpRefTy:
509 case FloatTy : case DoubleTy: case UnresolvedTy:
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");
517 unsigned uNum = 0, sNum = 0;
523 TypeInfo::UpRefStack stack;
524 elemTy->resolve()->getSignedness(sNum, uNum, stack);
528 case PackedStructTy: {
529 for (unsigned i = 0; i < elements->size(); i++) {
530 TypeInfo::UpRefStack stack;
531 (*elements)[i]->resolve()->getSignedness(sNum, uNum, stack);
536 assert(0 && "Invalid Type");
540 if (sNum == 0 && uNum == 0)
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;
552 Suffix += ".s" + llvm::utostr(sNum);
553 Suffix += ".u" + llvm::utostr(uNum);
554 return AddSuffix(BaseName, Suffix);
557 TypeInfo& TypeInfo::operator=(const TypeInfo& that) {
559 nelems = that.nelems;
561 elemTy = that.elemTy;
562 resultTy = that.resultTy;
564 elements = new TypeList(that.elements->size());
565 *elements = *that.elements;
572 const TypeInfo* TypeInfo::add_new_type(TypeInfo* newTy) {
573 TypeRegMap::iterator I = registry.find(newTy);
574 if (I != registry.end()) {
578 registry.insert(newTy);
582 /// This type is used to keep track of the signedness of constants.
585 const TypeInfo *type;
586 ~ConstInfo() { delete cnst; }
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;
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;
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;
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)
618 else if (DstBits > SrcBits) { // its an extension
619 if (SrcTy->isSigned())
620 opcode ="sext"; // signed -> SEXT
622 opcode = "zext"; // unsigned -> ZEXT
624 opcode = "bitcast"; // Same size, No-op cast
626 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
627 if (DstTy->isSigned())
628 opcode = "fptosi"; // FP -> sint
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
636 assert(SrcTy->isPointer() &&
637 "Casting from a value that is not first-class type");
638 opcode = "ptrtoint"; // ptr -> int
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
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
652 opcode ="bitcast"; // same size, no-op cast
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
659 assert(0 && "Casting pointer or non-first class to float");
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
669 assert(!"Illegal cast to packed (wrong type or size)");
671 } else if (DstTy->isPointer()) {
672 if (SrcTy->isPointer()) {
673 opcode = "bitcast"; // ptr -> ptr
674 } else if (SrcTy->isIntegral()) {
675 opcode = "inttoptr"; // int -> ptr
677 assert(!"Casting invalid type to pointer");
680 assert(!"Casting to type that is not first-class");
685 std::string getCastUpgrade(const std::string& Src, const TypeInfo* SrcTy,
686 const TypeInfo* DstTy, bool isConst) {
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
693 Source = "i64 fptoui(" + Source + " to i64)";
695 *O << " %cast_upgrade" << UniqueNameCounter++ << " = fptoui "
696 << Source << " to i64\n";
697 Source = "i64 %cast_upgrade" + llvm::utostr(UniqueNameCounter);
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 ";
710 Result = "(" + Source + comparator + ")";
711 Result = compareOp + Result;
713 Result = compareOp + Source + comparator;
714 return Result; // skip cast processing below
716 SrcTy = SrcTy->resolve();
717 DstTy = DstTy->resolve();
718 std::string Opcode(getCastOpcode(Source, SrcTy, DstTy));
720 Result += Opcode + "( " + Source + " to " + DstTy->getNewTy() + ")";
722 Result += Opcode + " " + Source + " to " + DstTy->getNewTy();
726 const char* getDivRemOpcode(const std::string& opcode, const TypeInfo* TI) {
727 const char* op = opcode.c_str();
728 const TypeInfo* Ty = TI->resolve();
730 Ty = Ty->getElementType();
732 if (Ty->isFloatingPoint())
734 else if (Ty->isUnsigned())
736 else if (Ty->isSigned())
739 yyerror("Invalid type for div instruction");
740 else if (opcode == "rem")
741 if (Ty->isFloatingPoint())
743 else if (Ty->isUnsigned())
745 else if (Ty->isSigned())
748 yyerror("Invalid type for rem instruction");
752 std::string getCompareOp(const std::string& setcc, const TypeInfo* TI) {
753 assert(setcc.length() == 5);
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");
761 if (TI->isFloatingPoint()) {
765 result[5] = 'u'; // NE maps to unordered
767 result[5] = 'o'; // everything else maps to ordered
768 } else if (TI->isIntegral() || TI->isPointer()) {
770 if ((cc1 == 'e' && cc2 == 'q') || (cc1 == 'n' && cc2 == 'e'))
772 else if (TI->isSigned())
774 else if (TI->isUnsigned() || TI->isPointer() || TI->isBool())
777 yyerror("Invalid integral type for setcc");
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();
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];
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);
813 yyerror("Invalid type for index");
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);
821 } else if (Result->isUpReference()) {
822 Result = ResolveUpReference(Result->getElementType(), &stack);
824 return Result->getPointerType();
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) {
834 // If its not a symbolic name, don't modify it, probably a constant val.
835 if ((*Name)[0] != '%' && (*Name)[0] != '"')
838 // If its a numeric reference, just leave it alone.
839 if (isdigit((*Name)[1]))
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))
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
865 // Default the result to the current name
866 std::string Result = Ty->makeUniqueName(*Name);
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";
886 Result += llvm::utostr(UniqueNameCounter);
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";
897 Result += llvm::utostr(UniqueNameCounter);
898 warning(std::string("Global variable '") + *Name + "' was renamed to '"+
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
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
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";
921 Result += llvm::utostr(UniqueNameCounter);
922 warning("Renaming global value '" + *Name + "' to '" + Result +
923 "' may cause linkage errors.");
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;
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
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;
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;
956 } // End anonymous namespace
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);
966 // %file-prefix="UpgradeParser"
970 const TypeInfo* Type;
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
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
1017 %type <ValList> ValueRefList ValueRefListE IndexList
1018 %type <TypeVec> TypeListI ArgTypeListI
1020 %type <Type> IntType SIntType UIntType FPType TypesV Types
1021 %type <Type> PrimType UpRTypesV UpRTypes
1023 %type <String> IntVal EInt64Val
1024 %type <Const> ConstVal
1026 %type <Value> ValueRef ResolvedVal InstVal PHIList MemoryInst
1032 // Handle constant integer size restriction and conversion...
1033 IntVal : SINTVAL | UINTVAL ;
1034 EInt64Val : ESINT64VAL | EUINT64VAL;
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
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;
1057 // OptAssign - Value producing statements have an optional assignment component
1058 OptAssign : Name '=' {
1062 $$ = new std::string("");
1066 : INTERNAL | LINKONCE | WEAK | APPENDING | DLLIMPORT | DLLEXPORT
1068 | /*empty*/ { $$ = new std::string(""); } ;
1071 : CCC_TOK | CSRETCC_TOK | FASTCC_TOK | COLDCC_TOK | X86_STDCALLCC_TOK
1072 | X86_FASTCALLCC_TOK
1073 | CC_TOK EUINT64VAL {
1078 | /*empty*/ { $$ = new std::string(""); } ;
1080 // OptAlign/OptCAlign - An optional alignment, and an optional alignment with
1081 // a comma before it.
1083 : /*empty*/ { $$ = new std::string(); }
1084 | ALIGN EUINT64VAL { *$1 += " " + *$2; delete $2; $$ = $1; };
1087 : /*empty*/ { $$ = new std::string(); }
1088 | ',' ALIGN EUINT64VAL {
1089 $2->insert(0, ", ");
1096 : SECTION STRINGCONSTANT {
1102 OptSection : /*empty*/ { $$ = new std::string(); }
1106 : /* empty */ { $$ = new std::string(); }
1107 | ',' GlobalVarAttribute GlobalVarAttributes {
1108 $2->insert(0, ", ");
1117 | ALIGN EUINT64VAL {
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.
1129 // TypesV includes all of 'Types', but it also includes the void type.
1130 TypesV : Types | VOID ;
1131 UpRTypesV : UpRTypes | VOID ;
1134 // Derived types are added later...
1136 PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
1137 PrimType : LONG | ULONG | FLOAT | DOUBLE | LABEL;
1140 $$ = TypeInfo::get(*$1, OpaqueTy);
1142 | SymbolicValueRef {
1143 $$ = TypeInfo::get(*$1, UnresolvedTy);
1148 | '\\' EUINT64VAL { // Type UpReference
1149 $2->insert(0, "\\");
1150 $$ = TypeInfo::get(*$2, UpRefTy);
1152 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
1153 std::string newTy( $1->getNewTy() + "(");
1154 for (unsigned i = 0; i < $3->size(); ++i) {
1157 if ((*$3)[i]->isVoid())
1160 newTy += (*$3)[i]->getNewTy();
1163 $$ = TypeInfo::get(newTy, $1, $3);
1165 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
1166 uint64_t elems = atoi($2->c_str());
1168 *$2 += " x " + $4->getNewTy() + " ]";
1169 $$ = TypeInfo::get(*$2, ArrayTy, $4, elems);
1171 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
1172 uint64_t elems = atoi($2->c_str());
1174 *$2 += " x " + $4->getNewTy() + " >";
1175 $$ = TypeInfo::get(*$2, PackedTy, $4, elems);
1177 | '{' TypeListI '}' { // Structure type?
1178 std::string newTy("{");
1179 for (unsigned i = 0; i < $2->size(); ++i) {
1182 newTy += (*$2)[i]->getNewTy();
1185 $$ = TypeInfo::get(newTy, StructTy, $2);
1187 | '{' '}' { // Empty structure type?
1188 $$ = TypeInfo::get("{}", StructTy, new TypeList());
1190 | '<' '{' TypeListI '}' '>' { // Packed Structure type?
1191 std::string newTy("<{");
1192 for (unsigned i = 0; i < $3->size(); ++i) {
1195 newTy += (*$3)[i]->getNewTy();
1198 $$ = TypeInfo::get(newTy, PackedStructTy, $3);
1200 | '<' '{' '}' '>' { // Empty packed structure type?
1201 $$ = TypeInfo::get("<{}>", PackedStructTy, new TypeList());
1203 | UpRTypes '*' { // Pointer type?
1204 $$ = $1->getPointerType();
1207 // TypeList - Used for struct declarations and as a basis for function type
1208 // declaration type lists
1212 $$ = new TypeList();
1215 | TypeListI ',' UpRTypes {
1220 // ArgTypeList - List of types for a function type declaration...
1223 | TypeListI ',' DOTDOTDOT {
1225 $$->push_back(TypeInfo::get("void",VoidTy));
1229 $$ = new TypeList();
1230 $$->push_back(TypeInfo::get("void",VoidTy));
1234 $$ = new TypeList();
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.
1243 ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
1246 $$->cnst = new std::string($1->getNewTy());
1247 *$$->cnst += " [ " + *$3 + " ]";
1253 $$->cnst = new std::string($1->getNewTy());
1256 | Types 'c' STRINGCONSTANT {
1259 $$->cnst = new std::string($1->getNewTy());
1260 *$$->cnst += " c" + *$3;
1263 | Types '<' ConstVector '>' { // Nonempty unsized arr
1266 $$->cnst = new std::string($1->getNewTy());
1267 *$$->cnst += " < " + *$3 + " >";
1270 | Types '{' ConstVector '}' {
1273 $$->cnst = new std::string($1->getNewTy());
1274 *$$->cnst += " { " + *$3 + " }";
1280 $$->cnst = new std::string($1->getNewTy());
1286 $$->cnst = new std::string($1->getNewTy());
1287 *$$->cnst += " " + *$2;
1293 $$->cnst = new std::string($1->getNewTy());
1294 *$$->cnst += " " + *$2;
1297 | Types SymbolicValueRef {
1299 std::string Name = getUniqueName($2, $1->resolve(), true);
1301 $$->cnst = new std::string($1->getNewTy());
1302 *$$->cnst += " " + Name;
1308 $$->cnst = new std::string($1->getNewTy());
1309 *$$->cnst += " " + *$2;
1312 | Types ZEROINITIALIZER {
1315 $$->cnst = new std::string($1->getNewTy());
1316 *$$->cnst += " " + *$2;
1319 | SIntType EInt64Val { // integral constants
1322 $$->cnst = new std::string($1->getNewTy());
1323 *$$->cnst += " " + *$2;
1326 | UIntType EInt64Val { // integral constants
1329 $$->cnst = new std::string($1->getNewTy());
1330 *$$->cnst += " " + *$2;
1333 | BOOL TRUETOK { // Boolean constants
1336 $$->cnst = new std::string($1->getNewTy());
1337 *$$->cnst += " " + *$2;
1340 | BOOL FALSETOK { // Boolean constants
1343 $$->cnst = new std::string($1->getNewTy());
1344 *$$->cnst += " " + *$2;
1347 | FPType FPVAL { // Float & Double constants
1350 $$->cnst = new std::string($1->getNewTy());
1351 *$$->cnst += " " + *$2;
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));
1363 // Nothing to upgrade, just create the cast constant expr
1364 $$ = new std::string(*$1);
1365 *$$ += "( " + source + " to " + $5->getNewTy() + ")";
1367 delete $1; delete $3; delete $4;
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;
1381 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1382 *$1 += "(" + *$3->cnst + "," + *$5->cnst + "," + *$7->cnst + ")";
1383 delete $3; delete $5; delete $7;
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;
1392 | LogicalOps '(' ConstVal ',' ConstVal ')' {
1393 *$1 += "(" + *$3->cnst + "," + *$5->cnst + ")";
1394 delete $3; delete $5;
1397 | SetCondOps '(' ConstVal ',' ConstVal ')' {
1398 *$1 = getCompareOp(*$1, $3->type);
1399 *$1 += "(" + *$3->cnst + "," + *$5->cnst + ")";
1400 delete $3; delete $5;
1403 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1404 *$1 += " " + *$2 + " (" + *$4->cnst + "," + *$6->cnst + ")";
1405 delete $2; delete $4; delete $6;
1408 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
1409 *$1 += " " + *$2 + " (" + *$4->cnst + "," + *$6->cnst + ")";
1410 delete $2; delete $4; delete $6;
1413 | ShiftOps '(' ConstVal ',' ConstVal ')' {
1414 const char* shiftop = $1->c_str();
1416 shiftop = ($3->type->isUnsigned()) ? "lshr" : "ashr";
1417 $$ = new std::string(shiftop);
1418 *$$ += "(" + *$3->cnst + "," + *$5->cnst + ")";
1419 delete $1; delete $3; delete $5;
1421 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
1422 *$1 += "(" + *$3->cnst + "," + *$5->cnst + ")";
1423 delete $3; delete $5;
1426 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1427 *$1 += "(" + *$3->cnst + "," + *$5->cnst + "," + *$7->cnst + ")";
1428 delete $3; delete $5; delete $7;
1431 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1432 *$1 += "(" + *$3->cnst + "," + *$5->cnst + "," + *$7->cnst + ")";
1433 delete $3; delete $5; delete $7;
1438 // ConstVector - A list of comma separated constants.
1441 : ConstVector ',' ConstVal {
1442 *$1 += ", " + *$3->cnst;
1446 | ConstVal { $$ = new std::string(*$1->cnst); delete $1; }
1450 // GlobalType - Match either GLOBAL or CONSTANT for global declarations...
1451 GlobalType : GLOBAL | CONSTANT ;
1454 //===----------------------------------------------------------------------===//
1455 // Rules to match Modules
1456 //===----------------------------------------------------------------------===//
1458 // Module rule: Capture the result of parsing the whole file into a result
1461 Module : DefinitionList {
1464 // DefinitionList - Top level definitions
1466 DefinitionList : DefinitionList Function {
1469 | DefinitionList FunctionProto {
1474 | DefinitionList MODULE ASM_TOK AsmBlock {
1475 *O << "module asm " << ' ' << *$4 << '\n';
1478 | DefinitionList IMPLEMENTATION {
1479 *O << "implementation\n";
1482 | ConstPool { $$ = 0; }
1484 External : EXTERNAL | UNINITIALIZED { $$ = $1; *$$ = "external"; }
1486 // ConstPool - Constants with optional names assigned to them.
1487 ConstPool : ConstPool OptAssign TYPE TypesV {
1488 TypeInfo::EnumeratedTypes.push_back($4);
1490 TypeInfo::NamedTypes[*$2] = $4;
1493 *O << "type " << $4->getNewTy() << '\n';
1494 delete $2; delete $3;
1497 | ConstPool FunctionProto { // Function prototypes can be in const pool
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;
1507 | ConstPool OptAssign OptLinkage GlobalType ConstVal GlobalVarAttributes {
1509 std::string Name = getGlobalName($2,*$3, $5->type->getPointerType(),
1511 *O << Name << " = ";
1513 *O << *$3 << ' ' << *$4 << ' ' << *$5->cnst << ' ' << *$6 << '\n';
1514 delete $2; delete $3; delete $4; delete $6;
1517 | ConstPool OptAssign External GlobalType Types GlobalVarAttributes {
1519 std::string Name = getGlobalName($2,*$3,$5->getPointerType(),
1521 *O << Name << " = ";
1523 *O << *$3 << ' ' << *$4 << ' ' << $5->getNewTy() << ' ' << *$6 << '\n';
1524 delete $2; delete $3; delete $4; delete $6;
1527 | ConstPool OptAssign DLLIMPORT GlobalType Types GlobalVarAttributes {
1529 std::string Name = getGlobalName($2,*$3,$5->getPointerType(),
1531 *O << Name << " = ";
1533 *O << *$3 << ' ' << *$4 << ' ' << $5->getNewTy() << ' ' << *$6 << '\n';
1534 delete $2; delete $3; delete $4; delete $6;
1537 | ConstPool OptAssign EXTERN_WEAK GlobalType Types GlobalVarAttributes {
1539 std::string Name = getGlobalName($2,*$3,$5->getPointerType(),
1541 *O << Name << " = ";
1543 *O << *$3 << ' ' << *$4 << ' ' << $5->getNewTy() << ' ' << *$6 << '\n';
1544 delete $2; delete $3; delete $4; delete $6;
1547 | ConstPool TARGET TargetDefinition {
1548 *O << *$2 << ' ' << *$3 << '\n';
1549 delete $2; delete $3;
1552 | ConstPool DEPLIBS '=' LibrariesDefinition {
1553 *O << *$2 << " = " << *$4 << '\n';
1554 delete $2; delete $4;
1557 | /* empty: end of list */ {
1562 AsmBlock : STRINGCONSTANT ;
1564 BigOrLittle : BIG | LITTLE
1567 : ENDIAN '=' BigOrLittle {
1572 | POINTERSIZE '=' EUINT64VAL {
1579 | TRIPLE '=' STRINGCONSTANT {
1584 | DATALAYOUT '=' STRINGCONSTANT {
1592 $2->insert(0, "[ ");
1598 : LibList ',' STRINGCONSTANT {
1604 | /* empty: end of list */ {
1605 $$ = new std::string();
1608 //===----------------------------------------------------------------------===//
1609 // Rules to match Function Headers
1610 //===----------------------------------------------------------------------===//
1612 Name : VAR_ID | STRINGCONSTANT;
1613 OptName : Name | /*empty*/ { $$ = new std::string(); };
1615 ArgVal : Types OptName {
1616 $$ = new std::string($1->getNewTy());
1618 std::string Name = getUniqueName($2, $1->resolve());
1624 ArgListH : ArgListH ',' ArgVal {
1632 ArgList : ArgListH {
1635 | ArgListH ',' DOTDOTDOT {
1643 | /* empty */ { $$ = new std::string(); };
1646 : OptCallingConv TypesV Name '(' ArgList ')' OptSection OptAlign {
1647 if (*$3 == "%llvm.va_start" || *$3 == "%llvm.va_end") {
1649 } else if (*$3 == "%llvm.va_copy") {
1655 *$1 += $2->getNewTy() + " " + *$3 + "(" + *$5 + ")";
1669 BEGIN : BEGINTOK { $$ = new std::string("{"); delete $1; }
1670 | '{' { $$ = new std::string ("{"); }
1673 : OptLinkage FunctionHeaderH BEGIN {
1678 *O << *$2 << ' ' << *$3 << '\n';
1679 delete $1; delete $2; delete $3;
1684 END : ENDTOK { $$ = new std::string("}"); delete $1; }
1685 | '}' { $$ = new std::string("}"); };
1687 Function : FunctionHeader BasicBlockList END {
1690 *O << *$3 << "\n\n";
1691 delete $1; delete $2; delete $3;
1696 : /*default*/ { $$ = new std::string(); }
1702 : DECLARE { isDeclare = true; } FnDeclareLinkage FunctionHeaderH {
1712 //===----------------------------------------------------------------------===//
1713 // Rules to match Basic Blocks
1714 //===----------------------------------------------------------------------===//
1716 OptSideEffect : /* empty */ { $$ = new std::string(); }
1720 : ESINT64VAL | EUINT64VAL | FPVAL | TRUETOK | FALSETOK | NULL_TOK | UNDEF
1722 | '<' ConstVector '>' {
1728 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
1732 *$1 += " " + *$3 + ", " + *$5;
1733 delete $2; delete $3; delete $5;
1737 SymbolicValueRef : IntVal | Name ;
1739 // ValueRef - A reference to a definition... either constant or symbolic
1741 : SymbolicValueRef {
1744 $$->constant = false;
1750 $$->constant = true;
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 {
1760 std::string Name = getUniqueName($2->val, $1);
1763 $$->val = new std::string($1->getNewTy() + " " + Name);
1767 BasicBlockList : BasicBlockList BasicBlock {
1770 | BasicBlock { // Do not allow functions with 0 basic blocks
1775 // Basic blocks are terminated by branching instructions:
1776 // br, br/cc, switch, ret
1778 BasicBlock : InstructionList BBTerminatorInst {
1782 InstructionList : InstructionList Inst {
1783 *O << " " << *$2 << '\n';
1796 Unwind : UNWIND | EXCEPT { $$ = $1; *$$ = "unwind"; }
1798 BBTerminatorInst : RET ResolvedVal { // Return with a result...
1799 *O << " " << *$1 << ' ' << *$2->val << '\n';
1800 delete $1; delete $2;
1803 | RET VOID { // Return with no result...
1804 *O << " " << *$1 << ' ' << $2->getNewTy() << '\n';
1808 | BR LABEL ValueRef { // Unconditional Branch...
1809 *O << " " << *$1 << ' ' << $2->getNewTy() << ' ' << *$3->val << '\n';
1810 delete $1; delete $3;
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;
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";
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";
1840 | OptAssign INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
1841 TO LABEL ValueRef Unwind LABEL ValueRef {
1842 const TypeInfo* ResTy = getFunctionReturnType($4);
1845 std::string Name = getUniqueName($1, ResTy);
1846 *O << Name << " = ";
1848 *O << *$2 << ' ' << *$3 << ' ' << $4->getNewTy() << ' ' << *$5->val << " (";
1849 for (unsigned i = 0; i < $7->size(); ++i) {
1850 ValueInfo* VI = (*$7)[i];
1852 if (i+1 < $7->size())
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;
1863 *O << " " << *$1 << '\n';
1868 *O << " " << *$1 << '\n';
1873 JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1874 *$1 += " " + $2->getNewTy() + " " + *$3 + ", " + $5->getNewTy() + " " +
1876 delete $3; delete $6;
1879 | IntType ConstValueRef ',' LABEL ValueRef {
1880 $2->insert(0, $1->getNewTy() + " " );
1881 *$2 += ", " + $4->getNewTy() + " " + *$5->val;
1887 : OptAssign InstVal {
1889 // Get a unique name for this value, based on its type.
1890 std::string Name = getUniqueName($1, $2->type);
1892 if (deleteUselessCastFlag && *deleteUselessCastName == Name) {
1893 // don't actually delete it, just comment it out
1894 $1->insert(0, "; USELSS BITCAST: ");
1895 delete deleteUselessCastName;
1900 deleteUselessCastFlag = false;
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 + "]";
1910 $$->val = new std::string(Name);
1912 delete $3; delete $5;
1914 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1915 std::string Name = getUniqueName($4->val, $1->type);
1916 *$1->val += ", [" + Name + "," + *$6->val + "]";
1925 $$ = new ValueList();
1928 | ValueRefList ',' ResolvedVal {
1933 // ValueRefListE - Just like ValueRefList, except that it may also be empty!
1935 : ValueRefList { $$ = $1; }
1936 | /*empty*/ { $$ = new ValueList(); }
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);
1954 $$->val = new std::string(op);
1955 *$$->val += " " + $2->getNewTy() + " " + Name1 + ", " + Name2;
1957 delete $1; delete $5;
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;
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;
1977 $$->type = TypeInfo::get("i1",BoolTy);
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;
1987 $$->type = TypeInfo::get("i1",BoolTy);
1988 delete $2; delete $6;
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;
1997 $$->type = TypeInfo::get("i1",BoolTy);
1998 delete $2; delete $6;
2000 | ShiftOps ResolvedVal ',' ResolvedVal {
2001 const char* shiftop = $1->c_str();
2003 shiftop = ($2->type->isUnsigned()) ? "lshr" : "ashr";
2004 std::string *val = new std::string(shiftop);
2005 *val += " " + *$2->val + ", " + *$4->val;
2009 delete $1; delete $4;
2011 | CastOps ResolvedVal TO Types {
2012 std::string source = *$2->val;
2013 const TypeInfo* SrcTy = $2->type->resolve();
2014 const TypeInfo* DstTy = $4->resolve();
2017 $$->val = new std::string();
2019 if (*$1 == "cast") {
2020 *$$->val += getCastUpgrade(source, SrcTy, DstTy, false);
2022 *$$->val += *$1 + " " + source + " to " + DstTy->getNewTy();
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);
2041 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
2042 *$1 += " " + *$2->val + ", " + *$4->val + ", " + *$6->val;
2046 $$->type = $4->type;
2050 | VAARG ResolvedVal ',' Types {
2051 *$1 += " " + *$2->val + ", " + $4->getNewTy();
2057 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
2058 *$1 += " " + *$2->val + ", " + *$4->val;
2062 $$->type = $$->type->resolve();
2063 $$->type = $$->type->getElementType();
2066 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
2067 *$1 += " " + *$2->val + ", " + *$4->val + ", " + *$6->val;
2071 delete $4; delete $6;
2073 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
2074 *$1 += " " + *$2->val + ", " + *$4->val + ", " + *$6->val;
2078 delete $4; delete $6;
2081 *$1 += " " + *$2->val;
2086 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
2087 // map llvm.isunordered to "fcmp uno"
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);
2097 static unsigned upgradeCount = 1;
2098 if (*$4->val == "%llvm.va_start" || *$4->val == "%llvm.va_end") {
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();
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();
2122 *$1 += $3->getNewTy() + " " + *$4->val + "(";
2123 for (unsigned i = 0; i < $6->size(); ++i) {
2124 ValueInfo* VI = (*$6)[i];
2126 if (i+1 < $6->size())
2133 $$->type = getFunctionReturnType($3);
2135 delete $2; delete $4; delete $6;
2140 // IndexList - List of indices for GEP based instructions...
2142 : ',' ValueRefList { $$ = $2; }
2143 | /* empty */ { $$ = new ValueList(); }
2148 | /* empty */ { $$ = new std::string(); }
2151 MemoryInst : MALLOC Types OptCAlign {
2152 *$1 += " " + $2->getNewTy();
2157 $$->type = $2->getPointerType();
2160 | MALLOC Types ',' UINT ValueRef OptCAlign {
2161 std::string Name = getUniqueName($5->val, $4);
2162 *$1 += " " + $2->getNewTy() + ", " + $4->getNewTy() + " " + Name;
2167 $$->type = $2->getPointerType();
2168 delete $5; delete $6;
2170 | ALLOCA Types OptCAlign {
2171 *$1 += " " + $2->getNewTy();
2176 $$->type = $2->getPointerType();
2179 | ALLOCA Types ',' UINT ValueRef OptCAlign {
2180 std::string Name = getUniqueName($5->val, $4);
2181 *$1 += " " + $2->getNewTy() + ", " + $4->getNewTy() + " " + Name;
2187 $$->type = $2->getPointerType();
2190 | FREE ResolvedVal {
2191 *$1 += " " + *$2->val;
2195 $$->type = TypeInfo::get("void", VoidTy);
2197 | OptVolatile LOAD Types ValueRef {
2198 std::string Name = getUniqueName($4->val, $3);
2201 *$1 += *$2 + " " + $3->getNewTy() + " " + Name;
2205 $$->type = $3->getElementType();
2208 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
2209 std::string Name = getUniqueName($6->val, $5);
2212 *$1 += *$2 + " " + *$3->val + ", " + $5->getNewTy() + " " + Name;
2216 $$->type = TypeInfo::get("void", VoidTy);
2217 delete $2; delete $6;
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
2228 *VI->val = "i64 %gep_upgrade" + llvm::utostr(UniqueNameCounter++);
2229 VI->type = TypeInfo::get("i64",ULongTy);
2232 *$1 += " " + $2->getNewTy() + " " + Name;
2233 for (unsigned i = 0; i < $4->size(); ++i) {
2234 ValueInfo* VI = (*$4)[i];
2235 *$1 += ", " + *VI->val;
2240 $$->type = getGEPIndexedType($2,$4);
2241 for (unsigned i = 0; i < $4->size(); ++i)
2248 int yyerror(const char *ErrorMsg) {
2250 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
2251 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
2252 std::string errMsg = where + "error: " + std::string(ErrorMsg) +
2254 if (yychar == YYEMPTY || yychar == 0)
2255 errMsg += "end-of-file.";
2257 errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
2258 std::cerr << "llvm-upgrade: " << errMsg << '\n';
2259 *O << "llvm-upgrade parse failed.\n";
2263 void warning(const std::string& ErrorMsg) {
2265 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
2266 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
2267 std::string errMsg = where + "warning: " + std::string(ErrorMsg) +
2269 if (yychar == YYEMPTY || yychar == 0)
2270 errMsg += "end-of-file.";
2272 errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
2273 std::cerr << "llvm-upgrade: " << errMsg << '\n';