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;
33 static uint64_t unique = 1;
35 // This bool controls whether attributes are ever added to function declarations
36 // definitions and calls.
37 static bool AddAttributes = false;
39 // This bool is used to communicate between the InstVal and Inst rules about
40 // whether or not a cast should be deleted. When the flag is set, InstVal has
41 // determined that the cast is a candidate. However, it can only be deleted if
42 // the value being casted is the same value name as the instruction. The Inst
43 // rule makes that comparison if the flag is set and comments out the
44 // instruction if they match.
45 static bool deleteUselessCastFlag = false;
46 static std::string* deleteUselessCastName = 0;
48 typedef std::vector<const TypeInfo*> TypeVector;
49 static TypeVector EnumeratedTypes;
50 typedef std::map<std::string,const TypeInfo*> TypeMap;
51 static TypeMap NamedTypes;
52 typedef std::map<const TypeInfo*,std::string> TypePlaneMap;
53 typedef std::map<std::string,TypePlaneMap> GlobalsTypeMap;
54 static GlobalsTypeMap Globals;
56 static void warning(const std::string& msg);
58 void destroy(ValueList* VL) {
59 while (!VL->empty()) {
60 ValueInfo& VI = VL->back();
67 void UpgradeAssembly(const std::string &infile, std::istream& in,
68 std::ostream &out, bool debug, bool addAttrs)
74 AddAttributes = addAttrs;
78 std::cerr << "Parse failed.\n";
79 out << "llvm-upgrade parse failed.\n";
84 TypeInfo::TypeRegMap TypeInfo::registry;
86 const TypeInfo* TypeInfo::get(const std::string &newType, Types oldType) {
87 TypeInfo* Ty = new TypeInfo();
90 return add_new_type(Ty);
93 const TypeInfo* TypeInfo::get(const std::string& newType, Types oldType,
94 const TypeInfo* eTy, const TypeInfo* rTy) {
95 TypeInfo* Ty= new TypeInfo();
98 Ty->elemTy = const_cast<TypeInfo*>(eTy);
99 Ty->resultTy = const_cast<TypeInfo*>(rTy);
100 return add_new_type(Ty);
103 const TypeInfo* TypeInfo::get(const std::string& newType, Types oldType,
104 const TypeInfo *eTy, uint64_t elems) {
105 TypeInfo* Ty = new TypeInfo();
108 Ty->elemTy = const_cast<TypeInfo*>(eTy);
110 return add_new_type(Ty);
113 const TypeInfo* TypeInfo::get(const std::string& newType, Types oldType,
115 TypeInfo* Ty = new TypeInfo();
119 return add_new_type(Ty);
122 const TypeInfo* TypeInfo::get(const std::string& newType, const TypeInfo* resTy,
124 TypeInfo* Ty = new TypeInfo();
126 Ty->oldTy = FunctionTy;
127 Ty->resultTy = const_cast<TypeInfo*>(resTy);
129 return add_new_type(Ty);
132 const TypeInfo* TypeInfo::resolve() const {
133 if (isUnresolved()) {
134 if (getNewTy()[0] == '%' && isdigit(newTy[1])) {
135 unsigned ref = atoi(&((newTy.c_str())[1])); // skip the %
136 if (ref < EnumeratedTypes.size()) {
137 return EnumeratedTypes[ref];
139 std::string msg("Can't resolve numbered type: ");
141 yyerror(msg.c_str());
144 TypeMap::iterator I = NamedTypes.find(newTy);
145 if (I != NamedTypes.end()) {
148 std::string msg("Cannot resolve type: ");
150 yyerror(msg.c_str());
154 // otherwise its already resolved.
158 bool TypeInfo::operator<(const TypeInfo& that) const {
161 if (oldTy != that.oldTy)
162 return oldTy < that.oldTy;
165 unsigned thisUp = this->getUpRefNum();
166 unsigned thatUp = that.getUpRefNum();
167 return thisUp < thatUp;
171 if (this->nelems != that.nelems)
172 return nelems < that.nelems;
174 const TypeInfo* thisTy = this->elemTy;
175 const TypeInfo* thatTy = that.elemTy;
176 return *thisTy < *thatTy;
179 const TypeInfo* thisTy = this->resultTy;
180 const TypeInfo* thatTy = that.resultTy;
181 if (!thisTy->sameOldTyAs(thatTy))
182 return *thisTy < *thatTy;
186 case PackedStructTy: {
187 if (elements->size() != that.elements->size())
188 return elements->size() < that.elements->size();
189 for (unsigned i = 0; i < elements->size(); i++) {
190 const TypeInfo* thisTy = (*this->elements)[i];
191 const TypeInfo* thatTy = (*that.elements)[i];
192 if (!thisTy->sameOldTyAs(thatTy))
193 return *thisTy < *thatTy;
198 return this->newTy < that.newTy;
205 bool TypeInfo::sameOldTyAs(const TypeInfo* that) const {
210 if (oldTy != that->oldTy)
215 if (nelems != that->nelems)
219 const TypeInfo* thisTy = this->elemTy;
220 const TypeInfo* thatTy = that->elemTy;
221 return thisTy->sameOldTyAs(thatTy);
224 const TypeInfo* thisTy = this->resultTy;
225 const TypeInfo* thatTy = that->resultTy;
226 if (!thisTy->sameOldTyAs(thatTy))
231 case PackedStructTy: {
232 if (elements->size() != that->elements->size())
234 for (unsigned i = 0; i < elements->size(); i++) {
235 const TypeInfo* thisTy = (*this->elements)[i];
236 const TypeInfo* thatTy = (*that->elements)[i];
237 if (!thisTy->sameOldTyAs(thatTy))
243 return this->newTy == that->newTy;
245 return true; // for all others oldTy == that->oldTy is sufficient
250 bool TypeInfo::isUnresolvedDeep() const {
257 return elemTy->isUnresolvedDeep();
260 for (unsigned i = 0; i < elements->size(); i++)
261 if ((*elements)[i]->isUnresolvedDeep())
269 unsigned TypeInfo::getBitWidth() const {
273 case VoidTy : return 0;
274 case BoolTy : return 1;
275 case SByteTy: case UByteTy : return 8;
276 case ShortTy: case UShortTy : return 16;
277 case IntTy: case UIntTy: case FloatTy: return 32;
278 case LongTy: case ULongTy: case DoubleTy : return 64;
279 case PointerTy: return SizeOfPointer; // global var
282 return nelems * elemTy->getBitWidth();
284 case PackedStructTy: {
286 for (unsigned i = 0; i < elements->size(); i++) {
287 size += (*elements)[i]->getBitWidth();
294 const TypeInfo* TypeInfo::getIndexedType(const ValueInfo& VI) const {
296 if (VI.isConstant() && VI.type->isInteger()) {
297 size_t pos = VI.val->find(' ') + 1;
298 if (pos < VI.val->size()) {
299 uint64_t idx = atoi(VI.val->substr(pos).c_str());
300 return (*elements)[idx];
302 yyerror("Invalid value for constant integer");
306 yyerror("Structure requires constant index");
310 if (isArray() || isPacked() || isPointer())
312 yyerror("Invalid type for getIndexedType");
316 void TypeInfo::getSignedness(unsigned &sNum, unsigned &uNum,
317 UpRefStack& stack) const {
320 case OpaqueTy: case LabelTy: case VoidTy: case BoolTy:
321 case FloatTy : case DoubleTy: case UpRefTy:
323 case SByteTy: case ShortTy: case LongTy: case IntTy:
326 case UByteTy: case UShortTy: case UIntTy: case ULongTy:
332 stack.push_back(this);
333 elemTy->getSignedness(sNum, uNum, stack);
336 case PackedStructTy: {
337 stack.push_back(this);
338 for (unsigned i = 0; i < elements->size(); i++) {
339 (*elements)[i]->getSignedness(sNum, uNum, stack);
344 const TypeInfo* Ty = this->resolve();
345 // Let's not recurse.
346 UpRefStack::const_iterator I = stack.begin(), E = stack.end();
347 for ( ; I != E && *I != Ty; ++I)
350 Ty->getSignedness(sNum, uNum, stack);
356 std::string AddSuffix(const std::string& Name, const std::string& Suffix) {
357 if (Name[Name.size()-1] == '"') {
358 std::string Result = Name;
359 Result.insert(Result.size()-1, Suffix);
362 return Name + Suffix;
365 std::string TypeInfo::makeUniqueName(const std::string& BaseName) const {
366 if (BaseName == "\"alloca point\"")
371 case OpaqueTy: case LabelTy: case VoidTy: case BoolTy: case UpRefTy:
372 case FloatTy : case DoubleTy: case UnresolvedTy:
374 case SByteTy: case ShortTy: case LongTy: case IntTy:
375 return AddSuffix(BaseName, ".s");
376 case UByteTy: case UShortTy: case UIntTy: case ULongTy:
377 return AddSuffix(BaseName, ".u");
380 unsigned uNum = 0, sNum = 0;
386 TypeInfo::UpRefStack stack;
387 elemTy->resolve()->getSignedness(sNum, uNum, stack);
391 case PackedStructTy: {
392 for (unsigned i = 0; i < elements->size(); i++) {
393 TypeInfo::UpRefStack stack;
394 (*elements)[i]->resolve()->getSignedness(sNum, uNum, stack);
399 assert(0 && "Invalid Type");
403 if (sNum == 0 && uNum == 0)
407 default: Suffix += ".nada"; break;
408 case PointerTy: Suffix += ".pntr"; break;
409 case PackedTy: Suffix += ".pckd"; break;
410 case ArrayTy: Suffix += ".arry"; break;
411 case StructTy: Suffix += ".strc"; break;
412 case PackedStructTy: Suffix += ".pstr"; break;
415 Suffix += ".s" + llvm::utostr(sNum);
416 Suffix += ".u" + llvm::utostr(uNum);
417 return AddSuffix(BaseName, Suffix);
420 TypeInfo& TypeInfo::operator=(const TypeInfo& that) {
422 nelems = that.nelems;
424 elemTy = that.elemTy;
425 resultTy = that.resultTy;
427 elements = new TypeList(that.elements->size());
428 *elements = *that.elements;
435 const TypeInfo* TypeInfo::add_new_type(TypeInfo* newTy) {
436 TypeRegMap::iterator I = registry.find(newTy);
437 if (I != registry.end()) {
441 registry.insert(newTy);
445 static const char* getCastOpcode(
446 std::string& Source, const TypeInfo* SrcTy, const TypeInfo* DstTy)
448 unsigned SrcBits = SrcTy->getBitWidth();
449 unsigned DstBits = DstTy->getBitWidth();
450 const char* opcode = "bitcast";
451 // Run through the possibilities ...
452 if (DstTy->isIntegral()) { // Casting to integral
453 if (SrcTy->isIntegral()) { // Casting from integral
454 if (DstBits < SrcBits)
456 else if (DstBits > SrcBits) { // its an extension
457 if (SrcTy->isSigned())
458 opcode ="sext"; // signed -> SEXT
460 opcode = "zext"; // unsigned -> ZEXT
462 opcode = "bitcast"; // Same size, No-op cast
464 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
465 if (DstTy->isSigned())
466 opcode = "fptosi"; // FP -> sint
468 opcode = "fptoui"; // FP -> uint
469 } else if (SrcTy->isPacked()) {
470 assert(DstBits == SrcTy->getBitWidth() &&
471 "Casting packed to integer of different width");
472 opcode = "bitcast"; // same size, no-op cast
474 assert(SrcTy->isPointer() &&
475 "Casting from a value that is not first-class type");
476 opcode = "ptrtoint"; // ptr -> int
478 } else if (DstTy->isFloatingPoint()) { // Casting to floating pt
479 if (SrcTy->isIntegral()) { // Casting from integral
480 if (SrcTy->isSigned())
481 opcode = "sitofp"; // sint -> FP
483 opcode = "uitofp"; // uint -> FP
484 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
485 if (DstBits < SrcBits) {
486 opcode = "fptrunc"; // FP -> smaller FP
487 } else if (DstBits > SrcBits) {
488 opcode = "fpext"; // FP -> larger FP
490 opcode ="bitcast"; // same size, no-op cast
492 } else if (SrcTy->isPacked()) {
493 assert(DstBits == SrcTy->getBitWidth() &&
494 "Casting packed to floating point of different width");
495 opcode = "bitcast"; // same size, no-op cast
497 assert(0 && "Casting pointer or non-first class to float");
499 } else if (DstTy->isPacked()) {
500 if (SrcTy->isPacked()) {
501 assert(DstTy->getBitWidth() == SrcTy->getBitWidth() &&
502 "Casting packed to packed of different widths");
503 opcode = "bitcast"; // packed -> packed
504 } else if (DstTy->getBitWidth() == SrcBits) {
505 opcode = "bitcast"; // float/int -> packed
507 assert(!"Illegal cast to packed (wrong type or size)");
509 } else if (DstTy->isPointer()) {
510 if (SrcTy->isPointer()) {
511 opcode = "bitcast"; // ptr -> ptr
512 } else if (SrcTy->isIntegral()) {
513 opcode = "inttoptr"; // int -> ptr
515 assert(!"Casting invalid type to pointer");
518 assert(!"Casting to type that is not first-class");
523 static std::string getCastUpgrade(const std::string& Src, const TypeInfo* SrcTy,
524 const TypeInfo* DstTy, bool isConst)
527 std::string Source = Src;
528 if (SrcTy->isFloatingPoint() && DstTy->isPointer()) {
529 // fp -> ptr cast is no longer supported but we must upgrade this
530 // by doing a double cast: fp -> int -> ptr
532 Source = "i64 fptoui(" + Source + " to i64)";
534 *O << " %cast_upgrade" << unique << " = fptoui " << Source
536 Source = "i64 %cast_upgrade" + llvm::utostr(unique);
538 // Update the SrcTy for the getCastOpcode call below
539 SrcTy = TypeInfo::get("i64", ULongTy);
540 } else if (DstTy->isBool()) {
541 // cast type %x to bool was previously defined as setne type %x, null
542 // The cast semantic is now to truncate, not compare so we must retain
543 // the original intent by replacing the cast with a setne
544 const char* comparator = SrcTy->isPointer() ? ", null" :
545 (SrcTy->isFloatingPoint() ? ", 0.0" :
546 (SrcTy->isBool() ? ", false" : ", 0"));
547 const char* compareOp = SrcTy->isFloatingPoint() ? "fcmp one " : "icmp ne ";
549 Result = "(" + Source + comparator + ")";
550 Result = compareOp + Result;
552 Result = compareOp + Source + comparator;
553 return Result; // skip cast processing below
555 SrcTy = SrcTy->resolve();
556 DstTy = DstTy->resolve();
557 std::string Opcode(getCastOpcode(Source, SrcTy, DstTy));
559 Result += Opcode + "( " + Source + " to " + DstTy->getNewTy() + ")";
561 Result += Opcode + " " + Source + " to " + DstTy->getNewTy();
565 const char* getDivRemOpcode(const std::string& opcode, const TypeInfo* TI) {
566 const char* op = opcode.c_str();
567 const TypeInfo* Ty = TI->resolve();
569 Ty = Ty->getElementType();
571 if (Ty->isFloatingPoint())
573 else if (Ty->isUnsigned())
575 else if (Ty->isSigned())
578 yyerror("Invalid type for div instruction");
579 else if (opcode == "rem")
580 if (Ty->isFloatingPoint())
582 else if (Ty->isUnsigned())
584 else if (Ty->isSigned())
587 yyerror("Invalid type for rem instruction");
592 getCompareOp(const std::string& setcc, const TypeInfo* TI) {
593 assert(setcc.length() == 5);
596 assert(cc1 == 'e' || cc1 == 'n' || cc1 == 'l' || cc1 == 'g');
597 assert(cc2 == 'q' || cc2 == 'e' || cc2 == 'e' || cc2 == 't');
598 std::string result("xcmp xxx");
601 if (TI->isFloatingPoint()) {
605 result[5] = 'u'; // NE maps to unordered
607 result[5] = 'o'; // everything else maps to ordered
608 } else if (TI->isIntegral() || TI->isPointer()) {
610 if ((cc1 == 'e' && cc2 == 'q') || (cc1 == 'n' && cc2 == 'e'))
612 else if (TI->isSigned())
614 else if (TI->isUnsigned() || TI->isPointer() || TI->isBool())
617 yyerror("Invalid integral type for setcc");
622 static const TypeInfo* getFunctionReturnType(const TypeInfo* PFTy) {
623 PFTy = PFTy->resolve();
624 if (PFTy->isPointer()) {
625 const TypeInfo* ElemTy = PFTy->getElementType();
626 ElemTy = ElemTy->resolve();
627 if (ElemTy->isFunction())
628 return ElemTy->getResultType();
629 } else if (PFTy->isFunction()) {
630 return PFTy->getResultType();
635 static const TypeInfo* ResolveUpReference(const TypeInfo* Ty,
636 TypeInfo::UpRefStack* stack) {
637 assert(Ty->isUpReference() && "Can't resolve a non-upreference");
638 unsigned upref = Ty->getUpRefNum();
639 assert(upref < stack->size() && "Invalid up reference");
640 return (*stack)[upref - stack->size() - 1];
643 static const TypeInfo* getGEPIndexedType(const TypeInfo* PTy, ValueList* idxs) {
644 const TypeInfo* Result = PTy = PTy->resolve();
645 assert(PTy->isPointer() && "GEP Operand is not a pointer?");
646 TypeInfo::UpRefStack stack;
647 for (unsigned i = 0; i < idxs->size(); ++i) {
648 if (Result->isComposite()) {
649 Result = Result->getIndexedType((*idxs)[i]);
650 Result = Result->resolve();
651 stack.push_back(Result);
653 yyerror("Invalid type for index");
655 // Resolve upreferences so we can return a more natural type
656 if (Result->isPointer()) {
657 if (Result->getElementType()->isUpReference()) {
658 stack.push_back(Result);
659 Result = ResolveUpReference(Result->getElementType(), &stack);
661 } else if (Result->isUpReference()) {
662 Result = ResolveUpReference(Result->getElementType(), &stack);
664 return Result->getPointerType();
668 // This function handles appending .u or .s to integer value names that
669 // were previously unsigned or signed, respectively. This avoids name
670 // collisions since the unsigned and signed type planes have collapsed
671 // into a single signless type plane.
672 static std::string getUniqueName(const std::string *Name, const TypeInfo* Ty,
673 bool isGlobal = false, bool isDef = false) {
675 // If its not a symbolic name, don't modify it, probably a constant val.
676 if ((*Name)[0] != '%' && (*Name)[0] != '"')
679 // If its a numeric reference, just leave it alone.
680 if (isdigit((*Name)[1]))
686 // If its a global name, get its uniquified name, if any
687 GlobalsTypeMap::iterator GI = Globals.find(*Name);
688 if (GI != Globals.end()) {
689 TypePlaneMap::iterator TPI = GI->second.begin();
690 TypePlaneMap::iterator TPE = GI->second.end();
691 for ( ; TPI != TPE ; ++TPI) {
692 if (TPI->first->sameNewTyAs(Ty))
698 // We didn't find a global name, but if its supposed to be global then all
699 // we can do is return the name. This is probably a forward reference of a
700 // global value that hasn't been defined yet. Since we have no definition
701 // we don't know its linkage class. Just assume its an external and the name
706 // Default the result to the current name
707 std::string Result = Ty->makeUniqueName(*Name);
712 static unsigned UniqueNameCounter = 0;
714 std::string getGlobalName(const std::string* Name, const std::string Linkage,
715 const TypeInfo* Ty, bool isConstant) {
716 // Default to given name
717 std::string Result = *Name;
718 // Look up the name in the Globals Map
719 GlobalsTypeMap::iterator GI = Globals.find(*Name);
720 // Did we see this global name before?
721 if (GI != Globals.end()) {
722 if (Ty->isUnresolvedDeep()) {
723 // The Gval's type is unresolved. Consequently, we can't disambiguate it
724 // by type. We'll just change its name and emit a warning.
725 warning("Cannot disambiguate global value '" + *Name +
726 "' because type '" + Ty->getNewTy() + "'is unresolved.\n");
727 Result = *Name + ".unique";
729 Result += llvm::utostr(UniqueNameCounter);
732 TypePlaneMap::iterator TPI = GI->second.find(Ty);
733 if (TPI != GI->second.end()) {
734 // We found an existing name of the same old type. This isn't allowed
735 // in LLVM 2.0. Consequently, we must alter the name of the global so it
736 // can at least compile. References to the global will yield the first
737 // definition, which is okay. We also must warn about this.
738 Result = *Name + ".unique";
740 Result += llvm::utostr(UniqueNameCounter);
741 warning(std::string("Global variable '") + *Name + "' was renamed to '"+
744 // There isn't an existing definition for this name according to the
745 // old types. Now search the TypePlanMap for types with the same new
747 TypePlaneMap::iterator TPI = GI->second.begin();
748 TypePlaneMap::iterator TPE = GI->second.end();
749 for ( ; TPI != TPE; ++TPI) {
750 if (TPI->first->sameNewTyAs(Ty)) {
751 // The new types are the same but the old types are different so
752 // this is a global name collision resulting from type planes
754 if (Linkage == "external" || Linkage == "dllimport" ||
755 Linkage == "extern_weak" || Linkage == "") {
756 // The linkage of this gval is external so we can't reliably
757 // rename it because it could potentially create a linking
758 // problem. However, we can't leave the name conflict in the
759 // output either or it won't assemble with LLVM 2.0. So, all we
760 // can do is rename this one to something unique and emit a
761 // warning about the problem.
762 Result = *Name + ".unique";
764 Result += llvm::utostr(UniqueNameCounter);
765 warning("Renaming global value '" + *Name + "' to '" + Result +
766 "' may cause linkage errors.");
769 // Its linkage is internal and its type is known so we can
770 // disambiguate the name collision successfully based on the type.
771 Result = getUniqueName(Name, Ty);
772 TPI->second = Result;
777 // We didn't find an entry in the type plane with the same new type and
778 // the old types differ so this is a new type plane for this global
779 // variable. We just fall through to the logic below which inserts
785 // Its a new global name, if it is external we can't change it
786 if (isConstant || Linkage == "external" || Linkage == "dllimport" ||
787 Linkage == "extern_weak" || Linkage == "") {
788 Globals[Result][Ty] = Result;
792 // Its a new global name, and it is internal, change the name to make it
793 // unique for its type.
794 // Result = getUniqueName(Name, Ty);
795 Globals[*Name][Ty] = Result;
800 // %file-prefix="UpgradeParser"
804 const TypeInfo* Type;
811 %token <Type> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
812 %token <Type> FLOAT DOUBLE LABEL
813 %token <String> OPAQUE ESINT64VAL EUINT64VAL SINTVAL UINTVAL FPVAL
814 %token <String> NULL_TOK UNDEF ZEROINITIALIZER TRUETOK FALSETOK
815 %token <String> TYPE VAR_ID LABELSTR STRINGCONSTANT
816 %token <String> IMPLEMENTATION BEGINTOK ENDTOK
817 %token <String> DECLARE GLOBAL CONSTANT SECTION VOLATILE
818 %token <String> TO DOTDOTDOT CONST INTERNAL LINKONCE WEAK
819 %token <String> DLLIMPORT DLLEXPORT EXTERN_WEAK APPENDING
820 %token <String> NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG
821 %token <String> ALIGN UNINITIALIZED
822 %token <String> DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
823 %token <String> CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
824 %token <String> X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
825 %token <String> DATALAYOUT
826 %token <String> RET BR SWITCH INVOKE EXCEPT UNWIND UNREACHABLE
827 %token <String> ADD SUB MUL DIV UDIV SDIV FDIV REM UREM SREM FREM AND OR XOR
828 %token <String> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comparators
829 %token <String> ICMP FCMP EQ NE SLT SGT SLE SGE OEQ ONE OLT OGT OLE OGE
830 %token <String> ORD UNO UEQ UNE ULT UGT ULE UGE
831 %token <String> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
832 %token <String> PHI_TOK SELECT SHL SHR ASHR LSHR VAARG
833 %token <String> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
834 %token <String> CAST TRUNC ZEXT SEXT FPTRUNC FPEXT FPTOUI FPTOSI UITOFP SITOFP
835 %token <String> PTRTOINT INTTOPTR BITCAST
837 %type <String> OptAssign OptLinkage OptCallingConv OptAlign OptCAlign
838 %type <String> SectionString OptSection GlobalVarAttributes GlobalVarAttribute
839 %type <String> ConstExpr DefinitionList
840 %type <String> ConstPool TargetDefinition LibrariesDefinition LibList OptName
841 %type <String> ArgVal ArgListH ArgList FunctionHeaderH BEGIN FunctionHeader END
842 %type <String> Function FunctionProto BasicBlock
843 %type <String> InstructionList BBTerminatorInst JumpTable Inst
844 %type <String> OptTailCall OptVolatile Unwind
845 %type <String> SymbolicValueRef OptSideEffect GlobalType
846 %type <String> FnDeclareLinkage BasicBlockList BigOrLittle AsmBlock
847 %type <String> Name ConstValueRef ConstVector External
848 %type <String> ShiftOps SetCondOps LogicalOps ArithmeticOps CastOps
849 %type <String> IPredicates FPredicates
851 %type <ValList> ValueRefList ValueRefListE IndexList
852 %type <TypeVec> TypeListI ArgTypeListI
854 %type <Type> IntType SIntType UIntType FPType TypesV Types
855 %type <Type> PrimType UpRTypesV UpRTypes
857 %type <String> IntVal EInt64Val
858 %type <Const> ConstVal
860 %type <Value> ValueRef ResolvedVal InstVal PHIList MemoryInst
866 // Handle constant integer size restriction and conversion...
867 IntVal : SINTVAL | UINTVAL ;
868 EInt64Val : ESINT64VAL | EUINT64VAL;
870 // Operations that are notably excluded from this list include:
871 // RET, BR, & SWITCH because they end basic blocks and are treated specially.
872 ArithmeticOps: ADD | SUB | MUL | DIV | UDIV | SDIV | FDIV
873 | REM | UREM | SREM | FREM;
874 LogicalOps : AND | OR | XOR;
875 SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
876 IPredicates : EQ | NE | SLT | SGT | SLE | SGE | ULT | UGT | ULE | UGE;
877 FPredicates : OEQ | ONE | OLT | OGT | OLE | OGE | ORD | UNO | UEQ | UNE
878 | ULT | UGT | ULE | UGE | TRUETOK | FALSETOK;
879 ShiftOps : SHL | SHR | ASHR | LSHR;
880 CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | FPTOUI | FPTOSI |
881 UITOFP | SITOFP | PTRTOINT | INTTOPTR | BITCAST | CAST
884 // These are some types that allow classification if we only want a particular
885 // thing... for example, only a signed, unsigned, or integral type.
886 SIntType : LONG | INT | SHORT | SBYTE;
887 UIntType : ULONG | UINT | USHORT | UBYTE;
888 IntType : SIntType | UIntType;
889 FPType : FLOAT | DOUBLE;
891 // OptAssign - Value producing statements have an optional assignment component
892 OptAssign : Name '=' {
896 $$ = new std::string("");
900 : INTERNAL | LINKONCE | WEAK | APPENDING | DLLIMPORT | DLLEXPORT
902 | /*empty*/ { $$ = new std::string(""); } ;
905 : CCC_TOK | CSRETCC_TOK | FASTCC_TOK | COLDCC_TOK | X86_STDCALLCC_TOK
907 | CC_TOK EUINT64VAL {
912 | /*empty*/ { $$ = new std::string(""); } ;
914 // OptAlign/OptCAlign - An optional alignment, and an optional alignment with
915 // a comma before it.
917 : /*empty*/ { $$ = new std::string(); }
918 | ALIGN EUINT64VAL { *$1 += " " + *$2; delete $2; $$ = $1; };
921 : /*empty*/ { $$ = new std::string(); }
922 | ',' ALIGN EUINT64VAL {
930 : SECTION STRINGCONSTANT {
936 OptSection : /*empty*/ { $$ = new std::string(); }
940 : /* empty */ { $$ = new std::string(); }
941 | ',' GlobalVarAttribute GlobalVarAttributes {
957 //===----------------------------------------------------------------------===//
958 // Types includes all predefined types... except void, because it can only be
959 // used in specific contexts (function returning void for example). To have
960 // access to it, a user must explicitly use TypesV.
963 // TypesV includes all of 'Types', but it also includes the void type.
964 TypesV : Types | VOID ;
965 UpRTypesV : UpRTypes | VOID ;
968 // Derived types are added later...
970 PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
971 PrimType : LONG | ULONG | FLOAT | DOUBLE | LABEL;
974 $$ = TypeInfo::get(*$1, OpaqueTy);
977 $$ = TypeInfo::get(*$1, UnresolvedTy);
982 | '\\' EUINT64VAL { // Type UpReference
984 $$ = TypeInfo::get(*$2, UpRefTy);
986 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
987 std::string newTy( $1->getNewTy() + "(");
988 for (unsigned i = 0; i < $3->size(); ++i) {
991 if ((*$3)[i]->isVoid())
994 newTy += (*$3)[i]->getNewTy();
997 $$ = TypeInfo::get(newTy, $1, $3);
999 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
1000 uint64_t elems = atoi($2->c_str());
1002 *$2 += " x " + $4->getNewTy() + " ]";
1003 $$ = TypeInfo::get(*$2, ArrayTy, $4, elems);
1005 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
1006 uint64_t elems = atoi($2->c_str());
1008 *$2 += " x " + $4->getNewTy() + " >";
1009 $$ = TypeInfo::get(*$2, PackedTy, $4, elems);
1011 | '{' TypeListI '}' { // Structure type?
1012 std::string newTy("{");
1013 for (unsigned i = 0; i < $2->size(); ++i) {
1016 newTy += (*$2)[i]->getNewTy();
1019 $$ = TypeInfo::get(newTy, StructTy, $2);
1021 | '{' '}' { // Empty structure type?
1022 $$ = TypeInfo::get("{}", StructTy, new TypeList());
1024 | '<' '{' TypeListI '}' '>' { // Packed Structure type?
1025 std::string newTy("<{");
1026 for (unsigned i = 0; i < $3->size(); ++i) {
1029 newTy += (*$3)[i]->getNewTy();
1032 $$ = TypeInfo::get(newTy, PackedStructTy, $3);
1034 | '<' '{' '}' '>' { // Empty packed structure type?
1035 $$ = TypeInfo::get("<{}>", PackedStructTy, new TypeList());
1037 | UpRTypes '*' { // Pointer type?
1038 $$ = $1->getPointerType();
1041 // TypeList - Used for struct declarations and as a basis for function type
1042 // declaration type lists
1046 $$ = new TypeList();
1049 | TypeListI ',' UpRTypes {
1054 // ArgTypeList - List of types for a function type declaration...
1057 | TypeListI ',' DOTDOTDOT {
1059 $$->push_back(TypeInfo::get("void",VoidTy));
1063 $$ = new TypeList();
1064 $$->push_back(TypeInfo::get("void",VoidTy));
1068 $$ = new TypeList();
1071 // ConstVal - The various declarations that go into the constant pool. This
1072 // production is used ONLY to represent constants that show up AFTER a 'const',
1073 // 'constant' or 'global' token at global scope. Constants that can be inlined
1074 // into other expressions (such as integers and constexprs) are handled by the
1075 // ResolvedVal, ValueRef and ConstValueRef productions.
1077 ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
1079 $$.cnst = new std::string($1->getNewTy());
1080 *$$.cnst += " [ " + *$3 + " ]";
1085 $$.cnst = new std::string($1->getNewTy());
1088 | Types 'c' STRINGCONSTANT {
1090 $$.cnst = new std::string($1->getNewTy());
1091 *$$.cnst += " c" + *$3;
1094 | Types '<' ConstVector '>' { // Nonempty unsized arr
1096 $$.cnst = new std::string($1->getNewTy());
1097 *$$.cnst += " < " + *$3 + " >";
1100 | Types '{' ConstVector '}' {
1102 $$.cnst = new std::string($1->getNewTy());
1103 *$$.cnst += " { " + *$3 + " }";
1108 $$.cnst = new std::string($1->getNewTy());
1113 $$.cnst = new std::string($1->getNewTy());
1114 *$$.cnst += " " + *$2;
1119 $$.cnst = new std::string($1->getNewTy());
1120 *$$.cnst += " " + *$2;
1123 | Types SymbolicValueRef {
1124 std::string Name = getUniqueName($2, $1->resolve(), true);
1126 $$.cnst = new std::string($1->getNewTy());
1127 *$$.cnst += " " + Name;
1132 $$.cnst = new std::string($1->getNewTy());
1133 *$$.cnst += " " + *$2;
1136 | Types ZEROINITIALIZER {
1138 $$.cnst = new std::string($1->getNewTy());
1139 *$$.cnst += " " + *$2;
1142 | SIntType EInt64Val { // integral constants
1144 $$.cnst = new std::string($1->getNewTy());
1145 *$$.cnst += " " + *$2;
1148 | UIntType EInt64Val { // integral constants
1150 $$.cnst = new std::string($1->getNewTy());
1151 *$$.cnst += " " + *$2;
1154 | BOOL TRUETOK { // Boolean constants
1156 $$.cnst = new std::string($1->getNewTy());
1157 *$$.cnst += " " + *$2;
1160 | BOOL FALSETOK { // Boolean constants
1162 $$.cnst = new std::string($1->getNewTy());
1163 *$$.cnst += " " + *$2;
1166 | FPType FPVAL { // Float & Double constants
1168 $$.cnst = new std::string($1->getNewTy());
1169 *$$.cnst += " " + *$2;
1174 ConstExpr: CastOps '(' ConstVal TO Types ')' {
1175 std::string source = *$3.cnst;
1176 const TypeInfo* SrcTy = $3.type->resolve();
1177 const TypeInfo* DstTy = $5->resolve();
1178 if (*$1 == "cast") {
1179 // Call getCastUpgrade to upgrade the old cast
1180 $$ = new std::string(getCastUpgrade(source, SrcTy, DstTy, true));
1182 // Nothing to upgrade, just create the cast constant expr
1183 $$ = new std::string(*$1);
1184 *$$ += "( " + source + " to " + $5->getNewTy() + ")";
1186 delete $1; $3.destroy(); delete $4;
1188 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1189 *$1 += "(" + *$3.cnst;
1190 for (unsigned i = 0; i < $4->size(); ++i) {
1191 ValueInfo& VI = (*$4)[i];
1192 *$1 += ", " + *VI.val;
1200 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1201 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
1202 $3.destroy(); $5.destroy(); $7.destroy();
1205 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
1206 const char* op = getDivRemOpcode(*$1, $3.type);
1207 $$ = new std::string(op);
1208 *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
1209 delete $1; $3.destroy(); $5.destroy();
1211 | LogicalOps '(' ConstVal ',' ConstVal ')' {
1212 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
1213 $3.destroy(); $5.destroy();
1216 | SetCondOps '(' ConstVal ',' ConstVal ')' {
1217 *$1 = getCompareOp(*$1, $3.type);
1218 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
1219 $3.destroy(); $5.destroy();
1222 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1223 *$1 += " " + *$2 + " (" + *$4.cnst + "," + *$6.cnst + ")";
1224 delete $2; $4.destroy(); $6.destroy();
1227 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
1228 *$1 += " " + *$2 + " (" + *$4.cnst + "," + *$6.cnst + ")";
1229 delete $2; $4.destroy(); $6.destroy();
1232 | ShiftOps '(' ConstVal ',' ConstVal ')' {
1233 const char* shiftop = $1->c_str();
1235 shiftop = ($3.type->isUnsigned()) ? "lshr" : "ashr";
1236 $$ = new std::string(shiftop);
1237 *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
1238 delete $1; $3.destroy(); $5.destroy();
1240 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
1241 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
1242 $3.destroy(); $5.destroy();
1245 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1246 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
1247 $3.destroy(); $5.destroy(); $7.destroy();
1250 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1251 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
1252 $3.destroy(); $5.destroy(); $7.destroy();
1257 // ConstVector - A list of comma separated constants.
1260 : ConstVector ',' ConstVal {
1261 *$1 += ", " + *$3.cnst;
1265 | ConstVal { $$ = new std::string(*$1.cnst); $1.destroy(); }
1269 // GlobalType - Match either GLOBAL or CONSTANT for global declarations...
1270 GlobalType : GLOBAL | CONSTANT ;
1273 //===----------------------------------------------------------------------===//
1274 // Rules to match Modules
1275 //===----------------------------------------------------------------------===//
1277 // Module rule: Capture the result of parsing the whole file into a result
1280 Module : DefinitionList {
1283 // DefinitionList - Top level definitions
1285 DefinitionList : DefinitionList Function {
1288 | DefinitionList FunctionProto {
1293 | DefinitionList MODULE ASM_TOK AsmBlock {
1294 *O << "module asm " << ' ' << *$4 << '\n';
1297 | DefinitionList IMPLEMENTATION {
1298 *O << "implementation\n";
1301 | ConstPool { $$ = 0; }
1303 External : EXTERNAL | UNINITIALIZED { $$ = $1; *$$ = "external"; }
1305 // ConstPool - Constants with optional names assigned to them.
1306 ConstPool : ConstPool OptAssign TYPE TypesV {
1307 EnumeratedTypes.push_back($4);
1309 NamedTypes[*$2] = $4;
1312 *O << "type " << $4->getNewTy() << '\n';
1313 delete $2; delete $3;
1316 | ConstPool FunctionProto { // Function prototypes can be in const pool
1321 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
1322 *O << *$2 << ' ' << *$3 << ' ' << *$4 << '\n';
1323 delete $2; delete $3; delete $4;
1326 | ConstPool OptAssign OptLinkage GlobalType ConstVal GlobalVarAttributes {
1328 std::string Name = getGlobalName($2,*$3, $5.type->getPointerType(),
1330 *O << Name << " = ";
1332 *O << *$3 << ' ' << *$4 << ' ' << *$5.cnst << ' ' << *$6 << '\n';
1333 delete $2; delete $3; delete $4; delete $6;
1336 | ConstPool OptAssign External GlobalType Types GlobalVarAttributes {
1338 std::string Name = getGlobalName($2,*$3,$5->getPointerType(),
1340 *O << Name << " = ";
1342 *O << *$3 << ' ' << *$4 << ' ' << $5->getNewTy() << ' ' << *$6 << '\n';
1343 delete $2; delete $3; delete $4; delete $6;
1346 | ConstPool OptAssign DLLIMPORT GlobalType Types GlobalVarAttributes {
1348 std::string Name = getGlobalName($2,*$3,$5->getPointerType(),
1350 *O << Name << " = ";
1352 *O << *$3 << ' ' << *$4 << ' ' << $5->getNewTy() << ' ' << *$6 << '\n';
1353 delete $2; delete $3; delete $4; delete $6;
1356 | ConstPool OptAssign EXTERN_WEAK GlobalType Types GlobalVarAttributes {
1358 std::string Name = getGlobalName($2,*$3,$5->getPointerType(),
1360 *O << Name << " = ";
1362 *O << *$3 << ' ' << *$4 << ' ' << $5->getNewTy() << ' ' << *$6 << '\n';
1363 delete $2; delete $3; delete $4; delete $6;
1366 | ConstPool TARGET TargetDefinition {
1367 *O << *$2 << ' ' << *$3 << '\n';
1368 delete $2; delete $3;
1371 | ConstPool DEPLIBS '=' LibrariesDefinition {
1372 *O << *$2 << " = " << *$4 << '\n';
1373 delete $2; delete $4;
1376 | /* empty: end of list */ {
1381 AsmBlock : STRINGCONSTANT ;
1383 BigOrLittle : BIG | LITTLE
1386 : ENDIAN '=' BigOrLittle {
1391 | POINTERSIZE '=' EUINT64VAL {
1398 | TRIPLE '=' STRINGCONSTANT {
1403 | DATALAYOUT '=' STRINGCONSTANT {
1411 $2->insert(0, "[ ");
1417 : LibList ',' STRINGCONSTANT {
1423 | /* empty: end of list */ {
1424 $$ = new std::string();
1427 //===----------------------------------------------------------------------===//
1428 // Rules to match Function Headers
1429 //===----------------------------------------------------------------------===//
1431 Name : VAR_ID | STRINGCONSTANT;
1432 OptName : Name | /*empty*/ { $$ = new std::string(); };
1434 ArgVal : Types OptName {
1435 $$ = new std::string($1->getNewTy());
1437 std::string Name = getUniqueName($2, $1->resolve());
1443 ArgListH : ArgListH ',' ArgVal {
1451 ArgList : ArgListH {
1454 | ArgListH ',' DOTDOTDOT {
1462 | /* empty */ { $$ = new std::string(); };
1465 : OptCallingConv TypesV Name '(' ArgList ')' OptSection OptAlign {
1469 *$1 += $2->getNewTy() + " " + *$3 + "(" + *$5 + ")";
1483 BEGIN : BEGINTOK { $$ = new std::string("{"); delete $1; }
1484 | '{' { $$ = new std::string ("{"); }
1487 : OptLinkage FunctionHeaderH BEGIN {
1492 *O << *$2 << ' ' << *$3 << '\n';
1493 delete $1; delete $2; delete $3;
1498 END : ENDTOK { $$ = new std::string("}"); delete $1; }
1499 | '}' { $$ = new std::string("}"); };
1501 Function : FunctionHeader BasicBlockList END {
1504 *O << *$3 << "\n\n";
1505 delete $1; delete $2; delete $3;
1510 : /*default*/ { $$ = new std::string(); }
1516 : DECLARE FnDeclareLinkage FunctionHeaderH {
1525 //===----------------------------------------------------------------------===//
1526 // Rules to match Basic Blocks
1527 //===----------------------------------------------------------------------===//
1529 OptSideEffect : /* empty */ { $$ = new std::string(); }
1533 : ESINT64VAL | EUINT64VAL | FPVAL | TRUETOK | FALSETOK | NULL_TOK | UNDEF
1535 | '<' ConstVector '>' {
1541 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
1545 *$1 += " " + *$3 + ", " + *$5;
1546 delete $2; delete $3; delete $5;
1550 SymbolicValueRef : IntVal | Name ;
1552 // ValueRef - A reference to a definition... either constant or symbolic
1554 : SymbolicValueRef {
1556 $$.constant = false;
1566 // ResolvedVal - a <type> <value> pair. This is used only in cases where the
1567 // type immediately preceeds the value reference, and allows complex constant
1568 // pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
1569 ResolvedVal : Types ValueRef {
1571 std::string Name = getUniqueName($2.val, $1);
1574 $$.val = new std::string($1->getNewTy() + " " + Name);
1578 BasicBlockList : BasicBlockList BasicBlock {
1581 | BasicBlock { // Do not allow functions with 0 basic blocks
1586 // Basic blocks are terminated by branching instructions:
1587 // br, br/cc, switch, ret
1589 BasicBlock : InstructionList BBTerminatorInst {
1593 InstructionList : InstructionList Inst {
1594 *O << " " << *$2 << '\n';
1607 Unwind : UNWIND | EXCEPT { $$ = $1; *$$ = "unwind"; }
1609 BBTerminatorInst : RET ResolvedVal { // Return with a result...
1610 *O << " " << *$1 << ' ' << *$2.val << '\n';
1611 delete $1; $2.destroy();
1614 | RET VOID { // Return with no result...
1615 *O << " " << *$1 << ' ' << $2->getNewTy() << '\n';
1619 | BR LABEL ValueRef { // Unconditional Branch...
1620 *O << " " << *$1 << ' ' << $2->getNewTy() << ' ' << *$3.val << '\n';
1621 delete $1; $3.destroy();
1623 } // Conditional Branch...
1624 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
1625 std::string Name = getUniqueName($3.val, $2);
1626 *O << " " << *$1 << ' ' << $2->getNewTy() << ' ' << Name << ", "
1627 << $5->getNewTy() << ' ' << *$6.val << ", " << $8->getNewTy() << ' '
1629 delete $1; $3.destroy(); $6.destroy(); $9.destroy();
1632 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1633 std::string Name = getUniqueName($3.val, $2);
1634 *O << " " << *$1 << ' ' << $2->getNewTy() << ' ' << Name << ", "
1635 << $5->getNewTy() << ' ' << *$6.val << " [" << *$8 << " ]\n";
1636 delete $1; $3.destroy(); $6.destroy();
1640 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
1641 std::string Name = getUniqueName($3.val, $2);
1642 *O << " " << *$1 << ' ' << $2->getNewTy() << ' ' << Name << ", "
1643 << $5->getNewTy() << ' ' << *$6.val << "[]\n";
1644 delete $1; $3.destroy(); $6.destroy();
1647 | OptAssign INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
1648 TO LABEL ValueRef Unwind LABEL ValueRef {
1649 const TypeInfo* ResTy = getFunctionReturnType($4);
1652 std::string Name = getUniqueName($1, ResTy);
1653 *O << Name << " = ";
1655 *O << *$2 << ' ' << *$3 << ' ' << $4->getNewTy() << ' ' << *$5.val << " (";
1656 for (unsigned i = 0; i < $7->size(); ++i) {
1657 ValueInfo& VI = (*$7)[i];
1659 if (i+1 < $7->size())
1663 *O << ") " << *$9 << ' ' << $10->getNewTy() << ' ' << *$11.val << ' '
1664 << *$12 << ' ' << $13->getNewTy() << ' ' << *$14.val << '\n';
1665 delete $1; delete $2; delete $3; $5.destroy(); delete $7;
1666 delete $9; $11.destroy(); delete $12; $14.destroy();
1670 *O << " " << *$1 << '\n';
1675 *O << " " << *$1 << '\n';
1680 JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1681 *$1 += " " + $2->getNewTy() + " " + *$3 + ", " + $5->getNewTy() + " " +
1683 delete $3; $6.destroy();
1686 | IntType ConstValueRef ',' LABEL ValueRef {
1687 $2->insert(0, $1->getNewTy() + " " );
1688 *$2 += ", " + $4->getNewTy() + " " + *$5.val;
1694 : OptAssign InstVal {
1696 // Get a unique name for this value, based on its type.
1697 std::string Name = getUniqueName($1, $2.type);
1699 if (deleteUselessCastFlag && *deleteUselessCastName == Name) {
1700 // don't actually delete it, just comment it out
1701 $1->insert(0, "; USELSS BITCAST: ");
1702 delete deleteUselessCastName;
1707 deleteUselessCastFlag = false;
1712 : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1713 std::string Name = getUniqueName($3.val, $1);
1714 Name.insert(0, $1->getNewTy() + "[");
1715 Name += "," + *$5.val + "]";
1716 $$.val = new std::string(Name);
1718 $3.destroy(); $5.destroy();
1720 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1721 std::string Name = getUniqueName($4.val, $1.type);
1722 *$1.val += ", [" + Name + "," + *$6.val + "]";
1723 $4.destroy(); $6.destroy();
1730 $$ = new ValueList();
1733 | ValueRefList ',' ResolvedVal {
1738 // ValueRefListE - Just like ValueRefList, except that it may also be empty!
1740 : ValueRefList { $$ = $1; }
1741 | /*empty*/ { $$ = new ValueList(); }
1753 InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1754 const char* op = getDivRemOpcode(*$1, $2);
1755 std::string Name1 = getUniqueName($3.val, $2);
1756 std::string Name2 = getUniqueName($5.val, $2);
1757 $$.val = new std::string(op);
1758 *$$.val += " " + $2->getNewTy() + " " + Name1 + ", " + Name2;
1760 delete $1; $3.destroy(); $5.destroy();
1762 | LogicalOps Types ValueRef ',' ValueRef {
1763 std::string Name1 = getUniqueName($3.val, $2);
1764 std::string Name2 = getUniqueName($5.val, $2);
1765 *$1 += " " + $2->getNewTy() + " " + Name1 + ", " + Name2;
1768 $3.destroy(); $5.destroy();
1770 | SetCondOps Types ValueRef ',' ValueRef {
1771 std::string Name1 = getUniqueName($3.val, $2);
1772 std::string Name2 = getUniqueName($5.val, $2);
1773 *$1 = getCompareOp(*$1, $2);
1774 *$1 += " " + $2->getNewTy() + " " + Name1 + ", " + Name2;
1776 $$.type = TypeInfo::get("bool",BoolTy);
1777 $3.destroy(); $5.destroy();
1779 | ICMP IPredicates Types ValueRef ',' ValueRef {
1780 std::string Name1 = getUniqueName($4.val, $3);
1781 std::string Name2 = getUniqueName($6.val, $3);
1782 *$1 += " " + *$2 + " " + $3->getNewTy() + " " + Name1 + "," + Name2;
1784 $$.type = TypeInfo::get("bool",BoolTy);
1785 delete $2; $4.destroy(); $6.destroy();
1787 | FCMP FPredicates Types ValueRef ',' ValueRef {
1788 std::string Name1 = getUniqueName($4.val, $3);
1789 std::string Name2 = getUniqueName($6.val, $3);
1790 *$1 += " " + *$2 + " " + $3->getNewTy() + " " + Name1 + "," + Name2;
1792 $$.type = TypeInfo::get("bool",BoolTy);
1793 delete $2; $4.destroy(); $6.destroy();
1797 $$.val->insert(0, *$1 + " ");
1800 | ShiftOps ResolvedVal ',' ResolvedVal {
1801 const char* shiftop = $1->c_str();
1803 shiftop = ($2.type->isUnsigned()) ? "lshr" : "ashr";
1804 $$.val = new std::string(shiftop);
1805 *$$.val += " " + *$2.val + ", " + *$4.val;
1807 delete $1; $2.destroy(); $4.destroy();
1809 | CastOps ResolvedVal TO Types {
1810 std::string source = *$2.val;
1811 const TypeInfo* SrcTy = $2.type->resolve();
1812 const TypeInfo* DstTy = $4->resolve();
1813 $$.val = new std::string();
1815 if (*$1 == "cast") {
1816 *$$.val += getCastUpgrade(source, SrcTy, DstTy, false);
1818 *$$.val += *$1 + " " + source + " to " + DstTy->getNewTy();
1820 // Check to see if this is a useless cast of a value to the same name
1821 // and the same type. Such casts will probably cause redefinition errors
1822 // when assembled and perform no code gen action so just remove them.
1823 if (*$1 == "cast" || *$1 == "bitcast")
1824 if (SrcTy->isInteger() && DstTy->isInteger() &&
1825 SrcTy->getBitWidth() == DstTy->getBitWidth()) {
1826 deleteUselessCastFlag = true; // Flag the "Inst" rule
1827 deleteUselessCastName = new std::string(*$2.val); // save the name
1828 size_t pos = deleteUselessCastName->find_first_of("%\"",0);
1829 if (pos != std::string::npos) {
1830 // remove the type portion before val
1831 deleteUselessCastName->erase(0, pos);
1834 delete $1; $2.destroy();
1837 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1838 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1841 $2.destroy(); $4.destroy(); $6.destroy();
1843 | VAARG ResolvedVal ',' Types {
1844 *$1 += " " + *$2.val + ", " + $4->getNewTy();
1849 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
1850 *$1 += " " + *$2.val + ", " + *$4.val;
1852 $2.type = $2.type->resolve();;
1853 $$.type = $2.type->getElementType();
1854 $2.destroy(); $4.destroy();
1856 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1857 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1860 $2.destroy(); $4.destroy(); $6.destroy();
1862 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1863 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1866 $2.destroy(); $4.destroy(); $6.destroy();
1869 *$1 += " " + *$2.val;
1874 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
1875 // map llvm.isunordered to "fcmp uno"
1876 if (*$4.val == "%llvm.isunordered.f32" ||
1877 *$4.val == "%llvm.isunordered.f64") {
1878 $$.val = new std::string( "fcmp uno " + *(*$6)[0].val + ", ");
1879 size_t pos = (*$6)[1].val->find(' ');
1880 assert(pos != std::string::npos && "no space?");
1881 *$$.val += (*$6)[1].val->substr(pos+1);
1882 $$.type = TypeInfo::get("bool", BoolTy);
1888 *$1 += $3->getNewTy() + " " + *$4.val + "(";
1889 for (unsigned i = 0; i < $6->size(); ++i) {
1890 ValueInfo& VI = (*$6)[i];
1892 if (i+1 < $6->size())
1898 $$.type = getFunctionReturnType($3);
1900 delete $2; $4.destroy(); delete $6;
1905 // IndexList - List of indices for GEP based instructions...
1907 : ',' ValueRefList { $$ = $2; }
1908 | /* empty */ { $$ = new ValueList(); }
1913 | /* empty */ { $$ = new std::string(); }
1916 MemoryInst : MALLOC Types OptCAlign {
1917 *$1 += " " + $2->getNewTy();
1921 $$.type = $2->getPointerType();
1924 | MALLOC Types ',' UINT ValueRef OptCAlign {
1925 std::string Name = getUniqueName($5.val, $4);
1926 *$1 += " " + $2->getNewTy() + ", " + $4->getNewTy() + " " + Name;
1930 $$.type = $2->getPointerType();
1931 $5.destroy(); delete $6;
1933 | ALLOCA Types OptCAlign {
1934 *$1 += " " + $2->getNewTy();
1938 $$.type = $2->getPointerType();
1941 | ALLOCA Types ',' UINT ValueRef OptCAlign {
1942 std::string Name = getUniqueName($5.val, $4);
1943 *$1 += " " + $2->getNewTy() + ", " + $4->getNewTy() + " " + Name;
1947 $$.type = $2->getPointerType();
1948 $5.destroy(); delete $6;
1950 | FREE ResolvedVal {
1951 *$1 += " " + *$2.val;
1953 $$.type = TypeInfo::get("void", VoidTy);
1956 | OptVolatile LOAD Types ValueRef {
1957 std::string Name = getUniqueName($4.val, $3);
1960 *$1 += *$2 + " " + $3->getNewTy() + " " + Name;
1962 $$.type = $3->getElementType();
1963 delete $2; $4.destroy();
1965 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
1966 std::string Name = getUniqueName($6.val, $5);
1969 *$1 += *$2 + " " + *$3.val + ", " + $5->getNewTy() + " " + Name;
1971 $$.type = TypeInfo::get("void", VoidTy);
1972 delete $2; $3.destroy(); $6.destroy();
1974 | GETELEMENTPTR Types ValueRef IndexList {
1975 std::string Name = getUniqueName($3.val, $2);
1976 // Upgrade the indices
1977 for (unsigned i = 0; i < $4->size(); ++i) {
1978 ValueInfo& VI = (*$4)[i];
1979 if (VI.type->isUnsigned() && !VI.isConstant() &&
1980 VI.type->getBitWidth() < 64) {
1981 *O << " %gep_upgrade" << unique << " = zext " << *VI.val
1983 *VI.val = "i64 %gep_upgrade" + llvm::utostr(unique++);
1984 VI.type = TypeInfo::get("i64",ULongTy);
1987 *$1 += " " + $2->getNewTy() + " " + Name;
1988 for (unsigned i = 0; i < $4->size(); ++i) {
1989 ValueInfo& VI = (*$4)[i];
1990 *$1 += ", " + *VI.val;
1993 $$.type = getGEPIndexedType($2,$4);
1994 $3.destroy(); delete $4;
1999 int yyerror(const char *ErrorMsg) {
2001 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
2002 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
2003 std::string errMsg = where + "error: " + std::string(ErrorMsg) +
2005 if (yychar == YYEMPTY || yychar == 0)
2006 errMsg += "end-of-file.";
2008 errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
2009 std::cerr << "llvm-upgrade: " << errMsg << '\n';
2010 *O << "llvm-upgrade parse failed.\n";
2014 static void warning(const std::string& ErrorMsg) {
2016 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
2017 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
2018 std::string errMsg = where + "warning: " + std::string(ErrorMsg) +
2020 if (yychar == YYEMPTY || yychar == 0)
2021 errMsg += "end-of-file.";
2023 errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
2024 std::cerr << "llvm-upgrade: " << errMsg << '\n';