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(getNewTy()[1])) {
135 unsigned ref = atoi(&((getNewTy().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(getNewTy());
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 TypeInfo& TypeInfo::operator=(const TypeInfo& that) {
318 nelems = that.nelems;
320 elemTy = that.elemTy;
321 resultTy = that.resultTy;
323 elements = new TypeList(that.elements->size());
324 *elements = *that.elements;
331 const TypeInfo* TypeInfo::add_new_type(TypeInfo* newTy) {
332 TypeRegMap::iterator I = registry.find(newTy);
333 if (I != registry.end()) {
337 registry.insert(newTy);
341 static const char* getCastOpcode(
342 std::string& Source, const TypeInfo* SrcTy, const TypeInfo* DstTy)
344 unsigned SrcBits = SrcTy->getBitWidth();
345 unsigned DstBits = DstTy->getBitWidth();
346 const char* opcode = "bitcast";
347 // Run through the possibilities ...
348 if (DstTy->isIntegral()) { // Casting to integral
349 if (SrcTy->isIntegral()) { // Casting from integral
350 if (DstBits < SrcBits)
352 else if (DstBits > SrcBits) { // its an extension
353 if (SrcTy->isSigned())
354 opcode ="sext"; // signed -> SEXT
356 opcode = "zext"; // unsigned -> ZEXT
358 opcode = "bitcast"; // Same size, No-op cast
360 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
361 if (DstTy->isSigned())
362 opcode = "fptosi"; // FP -> sint
364 opcode = "fptoui"; // FP -> uint
365 } else if (SrcTy->isPacked()) {
366 assert(DstBits == SrcTy->getBitWidth() &&
367 "Casting packed to integer of different width");
368 opcode = "bitcast"; // same size, no-op cast
370 assert(SrcTy->isPointer() &&
371 "Casting from a value that is not first-class type");
372 opcode = "ptrtoint"; // ptr -> int
374 } else if (DstTy->isFloatingPoint()) { // Casting to floating pt
375 if (SrcTy->isIntegral()) { // Casting from integral
376 if (SrcTy->isSigned())
377 opcode = "sitofp"; // sint -> FP
379 opcode = "uitofp"; // uint -> FP
380 } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt
381 if (DstBits < SrcBits) {
382 opcode = "fptrunc"; // FP -> smaller FP
383 } else if (DstBits > SrcBits) {
384 opcode = "fpext"; // FP -> larger FP
386 opcode ="bitcast"; // same size, no-op cast
388 } else if (SrcTy->isPacked()) {
389 assert(DstBits == SrcTy->getBitWidth() &&
390 "Casting packed to floating point of different width");
391 opcode = "bitcast"; // same size, no-op cast
393 assert(0 && "Casting pointer or non-first class to float");
395 } else if (DstTy->isPacked()) {
396 if (SrcTy->isPacked()) {
397 assert(DstTy->getBitWidth() == SrcTy->getBitWidth() &&
398 "Casting packed to packed of different widths");
399 opcode = "bitcast"; // packed -> packed
400 } else if (DstTy->getBitWidth() == SrcBits) {
401 opcode = "bitcast"; // float/int -> packed
403 assert(!"Illegal cast to packed (wrong type or size)");
405 } else if (DstTy->isPointer()) {
406 if (SrcTy->isPointer()) {
407 opcode = "bitcast"; // ptr -> ptr
408 } else if (SrcTy->isIntegral()) {
409 opcode = "inttoptr"; // int -> ptr
411 assert(!"Casting invalid type to pointer");
414 assert(!"Casting to type that is not first-class");
419 static std::string getCastUpgrade(const std::string& Src, const TypeInfo* SrcTy,
420 const TypeInfo* DstTy, bool isConst)
423 std::string Source = Src;
424 if (SrcTy->isFloatingPoint() && DstTy->isPointer()) {
425 // fp -> ptr cast is no longer supported but we must upgrade this
426 // by doing a double cast: fp -> int -> ptr
428 Source = "i64 fptoui(" + Source + " to i64)";
430 *O << " %cast_upgrade" << unique << " = fptoui " << Source
432 Source = "i64 %cast_upgrade" + llvm::utostr(unique);
434 // Update the SrcTy for the getCastOpcode call below
435 SrcTy = TypeInfo::get("i64", ULongTy);
436 } else if (DstTy->isBool()) {
437 // cast type %x to bool was previously defined as setne type %x, null
438 // The cast semantic is now to truncate, not compare so we must retain
439 // the original intent by replacing the cast with a setne
440 const char* comparator = SrcTy->isPointer() ? ", null" :
441 (SrcTy->isFloatingPoint() ? ", 0.0" :
442 (SrcTy->isBool() ? ", false" : ", 0"));
443 const char* compareOp = SrcTy->isFloatingPoint() ? "fcmp one " : "icmp ne ";
445 Result = "(" + Source + comparator + ")";
446 Result = compareOp + Result;
448 Result = compareOp + Source + comparator;
449 return Result; // skip cast processing below
451 SrcTy = SrcTy->resolve();
452 DstTy = DstTy->resolve();
453 std::string Opcode(getCastOpcode(Source, SrcTy, DstTy));
455 Result += Opcode + "( " + Source + " to " + DstTy->getNewTy() + ")";
457 Result += Opcode + " " + Source + " to " + DstTy->getNewTy();
461 const char* getDivRemOpcode(const std::string& opcode, const TypeInfo* TI) {
462 const char* op = opcode.c_str();
463 const TypeInfo* Ty = TI->resolve();
465 Ty = Ty->getElementType();
467 if (Ty->isFloatingPoint())
469 else if (Ty->isUnsigned())
471 else if (Ty->isSigned())
474 yyerror("Invalid type for div instruction");
475 else if (opcode == "rem")
476 if (Ty->isFloatingPoint())
478 else if (Ty->isUnsigned())
480 else if (Ty->isSigned())
483 yyerror("Invalid type for rem instruction");
488 getCompareOp(const std::string& setcc, const TypeInfo* TI) {
489 assert(setcc.length() == 5);
492 assert(cc1 == 'e' || cc1 == 'n' || cc1 == 'l' || cc1 == 'g');
493 assert(cc2 == 'q' || cc2 == 'e' || cc2 == 'e' || cc2 == 't');
494 std::string result("xcmp xxx");
497 if (TI->isFloatingPoint()) {
501 result[5] = 'u'; // NE maps to unordered
503 result[5] = 'o'; // everything else maps to ordered
504 } else if (TI->isIntegral() || TI->isPointer()) {
506 if ((cc1 == 'e' && cc2 == 'q') || (cc1 == 'n' && cc2 == 'e'))
508 else if (TI->isSigned())
510 else if (TI->isUnsigned() || TI->isPointer() || TI->isBool())
513 yyerror("Invalid integral type for setcc");
518 static const TypeInfo* getFunctionReturnType(const TypeInfo* PFTy) {
519 PFTy = PFTy->resolve();
520 if (PFTy->isPointer()) {
521 const TypeInfo* ElemTy = PFTy->getElementType();
522 ElemTy = ElemTy->resolve();
523 if (ElemTy->isFunction())
524 return ElemTy->getResultType();
525 } else if (PFTy->isFunction()) {
526 return PFTy->getResultType();
531 typedef std::vector<const TypeInfo*> UpRefStack;
532 static const TypeInfo* ResolveUpReference(const TypeInfo* Ty,
534 assert(Ty->isUpReference() && "Can't resolve a non-upreference");
535 unsigned upref = Ty->getUpRefNum();
536 assert(upref < stack->size() && "Invalid up reference");
537 return (*stack)[upref - stack->size() - 1];
540 static const TypeInfo* getGEPIndexedType(const TypeInfo* PTy, ValueList* idxs) {
541 const TypeInfo* Result = PTy = PTy->resolve();
542 assert(PTy->isPointer() && "GEP Operand is not a pointer?");
544 for (unsigned i = 0; i < idxs->size(); ++i) {
545 if (Result->isComposite()) {
546 Result = Result->getIndexedType((*idxs)[i]);
547 Result = Result->resolve();
548 stack.push_back(Result);
550 yyerror("Invalid type for index");
552 // Resolve upreferences so we can return a more natural type
553 if (Result->isPointer()) {
554 if (Result->getElementType()->isUpReference()) {
555 stack.push_back(Result);
556 Result = ResolveUpReference(Result->getElementType(), &stack);
558 } else if (Result->isUpReference()) {
559 Result = ResolveUpReference(Result->getElementType(), &stack);
561 return Result->getPointerType();
564 static std::string makeUniqueName(const std::string *Name, bool isSigned) {
565 const char *suffix = ".u";
568 if ((*Name)[Name->size()-1] == '"') {
569 std::string Result(*Name);
570 Result.insert(Name->size()-1, suffix);
573 return *Name + suffix;
576 // This function handles appending .u or .s to integer value names that
577 // were previously unsigned or signed, respectively. This avoids name
578 // collisions since the unsigned and signed type planes have collapsed
579 // into a single signless type plane.
580 static std::string getUniqueName(const std::string *Name, const TypeInfo* Ty,
581 bool isGlobal = false) {
583 // If its not a symbolic name, don't modify it, probably a constant val.
584 if ((*Name)[0] != '%' && (*Name)[0] != '"')
587 // If its a numeric reference, just leave it alone.
588 if (isdigit((*Name)[1]))
594 // If its a global name, get its uniquified name, if any
595 GlobalsTypeMap::iterator GI = Globals.find(*Name);
596 if (GI != Globals.end()) {
597 TypePlaneMap::iterator TPI = GI->second.begin();
598 TypePlaneMap::iterator TPE = GI->second.end();
599 for ( ; TPI != TPE ; ++TPI) {
600 if (TPI->first->sameNewTyAs(Ty))
606 // We didn't find a global name, but if its supposed to be global then all
607 // we can do is return the name. This is probably a forward reference of a
608 // global value that hasn't been defined yet. Since we have no definition
609 // we don't know its linkage class. Just assume its an external and the name
614 // Remove as many levels of pointer nesting that we have.
615 if (Ty->isPointer()) {
616 // Avoid infinite loops in recursive types
617 const TypeInfo* Last = 0;
618 while (Ty->isPointer() && !Ty->sameOldTyAs(Last)) {
620 Ty = Ty->getElementType()->resolve();
624 // Default the result to the current name
625 std::string Result = *Name;
627 // Now deal with the underlying type
628 if (Ty->isInteger()) {
629 // If its an integer type, make the name unique
630 Result = makeUniqueName(Name, Ty->isSigned());
631 } else if (Ty->isArray() || Ty->isPacked()) {
632 Ty = Ty->getElementType();
634 Result = makeUniqueName(Name, Ty->isSigned());
635 } else if (Ty->isStruct()) {
636 // Scan the fields and count the signed and unsigned fields
638 for (unsigned i = 0; i < Ty->getNumStructElements(); ++i) {
639 const TypeInfo* Tmp = Ty->getElement(i);
640 if (Tmp->isInteger())
647 Result = makeUniqueName(Name, isSigned > 0);
652 static unsigned UniqueNameCounter = 0;
654 std::string getGlobalName(const std::string* Name, const std::string Linkage,
655 const TypeInfo* Ty, bool isConstant) {
656 // Default to given name
657 std::string Result = *Name;
658 // Look up the name in the Globals Map
659 GlobalsTypeMap::iterator GI = Globals.find(*Name);
660 // Did we see this global name before?
661 if (GI != Globals.end()) {
662 if (Ty->isUnresolvedDeep()) {
663 // The Gval's type is unresolved. Consequently, we can't disambiguate it
664 // by type. We'll just change its name and emit a warning.
665 warning("Cannot disambiguate global value '" + *Name +
666 "' because type '" + Ty->getNewTy() + "'is unresolved.\n");
667 Result = *Name + ".unique";
669 Result += llvm::utostr(UniqueNameCounter);
672 TypePlaneMap::iterator TPI = GI->second.find(Ty);
673 if (TPI != GI->second.end()) {
674 // We found an existing name of the same old type. This isn't allowed
675 // in LLVM 2.0. Consequently, we must alter the name of the global so it
676 // can at least compile. References to the global will yield the first
677 // definition, which is okay. We also must warn about this.
678 Result = *Name + ".unique";
680 Result += llvm::utostr(UniqueNameCounter);
681 warning(std::string("Global variable '") + *Name + "' was renamed to '"+
684 // There isn't an existing definition for this name according to the
685 // old types. Now search the TypePlanMap for types with the same new
687 TypePlaneMap::iterator TPI = GI->second.begin();
688 TypePlaneMap::iterator TPE = GI->second.end();
689 for ( ; TPI != TPE; ++TPI) {
690 if (TPI->first->sameNewTyAs(Ty)) {
691 // The new types are the same but the old types are different so
692 // this is a global name collision resulting from type planes
694 if (Linkage == "external" || Linkage == "dllimport" ||
695 Linkage == "extern_weak" || Linkage == "") {
696 // The linkage of this gval is external so we can't reliably
697 // rename it because it could potentially create a linking
698 // problem. However, we can't leave the name conflict in the
699 // output either or it won't assemble with LLVM 2.0. So, all we
700 // can do is rename this one to something unique and emit a
701 // warning about the problem.
702 Result = *Name + ".unique";
704 Result += llvm::utostr(UniqueNameCounter);
705 warning("Renaming global value '" + *Name + "' to '" + Result +
706 "' may cause linkage errors.");
709 // Its linkage is internal and its type is known so we can
710 // disambiguate the name collision successfully based on the type.
711 Result = getUniqueName(Name, Ty);
712 TPI->second = Result;
717 // We didn't find an entry in the type plane with the same new type and
718 // the old types differ so this is a new type plane for this global
719 // variable. We just fall through to the logic below which inserts
725 // Its a new global name, if it is external we can't change it
726 if (isConstant || Linkage == "external" || Linkage == "dllimport" ||
727 Linkage == "extern_weak" || Linkage == "") {
728 Globals[Result][Ty] = Result;
732 // Its a new global name, and it is internal, change the name to make it
733 // unique for its type.
734 // Result = getUniqueName(Name, Ty);
735 Globals[*Name][Ty] = Result;
740 // %file-prefix="UpgradeParser"
744 const TypeInfo* Type;
751 %token <Type> VOID BOOL SBYTE UBYTE SHORT USHORT INT UINT LONG ULONG
752 %token <Type> FLOAT DOUBLE LABEL
753 %token <String> OPAQUE ESINT64VAL EUINT64VAL SINTVAL UINTVAL FPVAL
754 %token <String> NULL_TOK UNDEF ZEROINITIALIZER TRUETOK FALSETOK
755 %token <String> TYPE VAR_ID LABELSTR STRINGCONSTANT
756 %token <String> IMPLEMENTATION BEGINTOK ENDTOK
757 %token <String> DECLARE GLOBAL CONSTANT SECTION VOLATILE
758 %token <String> TO DOTDOTDOT CONST INTERNAL LINKONCE WEAK
759 %token <String> DLLIMPORT DLLEXPORT EXTERN_WEAK APPENDING
760 %token <String> NOT EXTERNAL TARGET TRIPLE ENDIAN POINTERSIZE LITTLE BIG
761 %token <String> ALIGN UNINITIALIZED
762 %token <String> DEPLIBS CALL TAIL ASM_TOK MODULE SIDEEFFECT
763 %token <String> CC_TOK CCC_TOK CSRETCC_TOK FASTCC_TOK COLDCC_TOK
764 %token <String> X86_STDCALLCC_TOK X86_FASTCALLCC_TOK
765 %token <String> DATALAYOUT
766 %token <String> RET BR SWITCH INVOKE EXCEPT UNWIND UNREACHABLE
767 %token <String> ADD SUB MUL DIV UDIV SDIV FDIV REM UREM SREM FREM AND OR XOR
768 %token <String> SETLE SETGE SETLT SETGT SETEQ SETNE // Binary Comparators
769 %token <String> ICMP FCMP EQ NE SLT SGT SLE SGE OEQ ONE OLT OGT OLE OGE
770 %token <String> ORD UNO UEQ UNE ULT UGT ULE UGE
771 %token <String> MALLOC ALLOCA FREE LOAD STORE GETELEMENTPTR
772 %token <String> PHI_TOK SELECT SHL SHR ASHR LSHR VAARG
773 %token <String> EXTRACTELEMENT INSERTELEMENT SHUFFLEVECTOR
774 %token <String> CAST TRUNC ZEXT SEXT FPTRUNC FPEXT FPTOUI FPTOSI UITOFP SITOFP
775 %token <String> PTRTOINT INTTOPTR BITCAST
777 %type <String> OptAssign OptLinkage OptCallingConv OptAlign OptCAlign
778 %type <String> SectionString OptSection GlobalVarAttributes GlobalVarAttribute
779 %type <String> ConstExpr DefinitionList
780 %type <String> ConstPool TargetDefinition LibrariesDefinition LibList OptName
781 %type <String> ArgVal ArgListH ArgList FunctionHeaderH BEGIN FunctionHeader END
782 %type <String> Function FunctionProto BasicBlock
783 %type <String> InstructionList BBTerminatorInst JumpTable Inst
784 %type <String> OptTailCall OptVolatile Unwind
785 %type <String> SymbolicValueRef OptSideEffect GlobalType
786 %type <String> FnDeclareLinkage BasicBlockList BigOrLittle AsmBlock
787 %type <String> Name ConstValueRef ConstVector External
788 %type <String> ShiftOps SetCondOps LogicalOps ArithmeticOps CastOps
789 %type <String> IPredicates FPredicates
791 %type <ValList> ValueRefList ValueRefListE IndexList
792 %type <TypeVec> TypeListI ArgTypeListI
794 %type <Type> IntType SIntType UIntType FPType TypesV Types
795 %type <Type> PrimType UpRTypesV UpRTypes
797 %type <String> IntVal EInt64Val
798 %type <Const> ConstVal
800 %type <Value> ValueRef ResolvedVal InstVal PHIList MemoryInst
806 // Handle constant integer size restriction and conversion...
807 IntVal : SINTVAL | UINTVAL ;
808 EInt64Val : ESINT64VAL | EUINT64VAL;
810 // Operations that are notably excluded from this list include:
811 // RET, BR, & SWITCH because they end basic blocks and are treated specially.
812 ArithmeticOps: ADD | SUB | MUL | DIV | UDIV | SDIV | FDIV
813 | REM | UREM | SREM | FREM;
814 LogicalOps : AND | OR | XOR;
815 SetCondOps : SETLE | SETGE | SETLT | SETGT | SETEQ | SETNE;
816 IPredicates : EQ | NE | SLT | SGT | SLE | SGE | ULT | UGT | ULE | UGE;
817 FPredicates : OEQ | ONE | OLT | OGT | OLE | OGE | ORD | UNO | UEQ | UNE
818 | ULT | UGT | ULE | UGE | TRUETOK | FALSETOK;
819 ShiftOps : SHL | SHR | ASHR | LSHR;
820 CastOps : TRUNC | ZEXT | SEXT | FPTRUNC | FPEXT | FPTOUI | FPTOSI |
821 UITOFP | SITOFP | PTRTOINT | INTTOPTR | BITCAST | CAST
824 // These are some types that allow classification if we only want a particular
825 // thing... for example, only a signed, unsigned, or integral type.
826 SIntType : LONG | INT | SHORT | SBYTE;
827 UIntType : ULONG | UINT | USHORT | UBYTE;
828 IntType : SIntType | UIntType;
829 FPType : FLOAT | DOUBLE;
831 // OptAssign - Value producing statements have an optional assignment component
832 OptAssign : Name '=' {
836 $$ = new std::string("");
840 : INTERNAL | LINKONCE | WEAK | APPENDING | DLLIMPORT | DLLEXPORT
842 | /*empty*/ { $$ = new std::string(""); } ;
845 : CCC_TOK | CSRETCC_TOK | FASTCC_TOK | COLDCC_TOK | X86_STDCALLCC_TOK
847 | CC_TOK EUINT64VAL {
852 | /*empty*/ { $$ = new std::string(""); } ;
854 // OptAlign/OptCAlign - An optional alignment, and an optional alignment with
855 // a comma before it.
857 : /*empty*/ { $$ = new std::string(); }
858 | ALIGN EUINT64VAL { *$1 += " " + *$2; delete $2; $$ = $1; };
861 : /*empty*/ { $$ = new std::string(); }
862 | ',' ALIGN EUINT64VAL {
870 : SECTION STRINGCONSTANT {
876 OptSection : /*empty*/ { $$ = new std::string(); }
880 : /* empty */ { $$ = new std::string(); }
881 | ',' GlobalVarAttribute GlobalVarAttributes {
897 //===----------------------------------------------------------------------===//
898 // Types includes all predefined types... except void, because it can only be
899 // used in specific contexts (function returning void for example). To have
900 // access to it, a user must explicitly use TypesV.
903 // TypesV includes all of 'Types', but it also includes the void type.
904 TypesV : Types | VOID ;
905 UpRTypesV : UpRTypes | VOID ;
908 // Derived types are added later...
910 PrimType : BOOL | SBYTE | UBYTE | SHORT | USHORT | INT | UINT ;
911 PrimType : LONG | ULONG | FLOAT | DOUBLE | LABEL;
914 $$ = TypeInfo::get(*$1, OpaqueTy);
917 $$ = TypeInfo::get(*$1, UnresolvedTy);
922 | '\\' EUINT64VAL { // Type UpReference
924 $$ = TypeInfo::get(*$2, UpRefTy);
926 | UpRTypesV '(' ArgTypeListI ')' { // Function derived type?
927 std::string newTy( $1->getNewTy() + "(");
928 for (unsigned i = 0; i < $3->size(); ++i) {
931 if ((*$3)[i]->isVoid())
934 newTy += (*$3)[i]->getNewTy();
937 $$ = TypeInfo::get(newTy, $1, $3);
939 | '[' EUINT64VAL 'x' UpRTypes ']' { // Sized array type?
940 uint64_t elems = atoi($2->c_str());
942 *$2 += " x " + $4->getNewTy() + " ]";
943 $$ = TypeInfo::get(*$2, ArrayTy, $4, elems);
945 | '<' EUINT64VAL 'x' UpRTypes '>' { // Packed array type?
946 uint64_t elems = atoi($2->c_str());
948 *$2 += " x " + $4->getNewTy() + " >";
949 $$ = TypeInfo::get(*$2, PackedTy, $4, elems);
951 | '{' TypeListI '}' { // Structure type?
952 std::string newTy("{");
953 for (unsigned i = 0; i < $2->size(); ++i) {
956 newTy += (*$2)[i]->getNewTy();
959 $$ = TypeInfo::get(newTy, StructTy, $2);
961 | '{' '}' { // Empty structure type?
962 $$ = TypeInfo::get("{}", StructTy, new TypeList());
964 | '<' '{' TypeListI '}' '>' { // Packed Structure type?
965 std::string newTy("<{");
966 for (unsigned i = 0; i < $3->size(); ++i) {
969 newTy += (*$3)[i]->getNewTy();
972 $$ = TypeInfo::get(newTy, PackedStructTy, $3);
974 | '<' '{' '}' '>' { // Empty packed structure type?
975 $$ = TypeInfo::get("<{}>", PackedStructTy, new TypeList());
977 | UpRTypes '*' { // Pointer type?
978 $$ = $1->getPointerType();
981 // TypeList - Used for struct declarations and as a basis for function type
982 // declaration type lists
989 | TypeListI ',' UpRTypes {
994 // ArgTypeList - List of types for a function type declaration...
997 | TypeListI ',' DOTDOTDOT {
999 $$->push_back(TypeInfo::get("void",VoidTy));
1003 $$ = new TypeList();
1004 $$->push_back(TypeInfo::get("void",VoidTy));
1008 $$ = new TypeList();
1011 // ConstVal - The various declarations that go into the constant pool. This
1012 // production is used ONLY to represent constants that show up AFTER a 'const',
1013 // 'constant' or 'global' token at global scope. Constants that can be inlined
1014 // into other expressions (such as integers and constexprs) are handled by the
1015 // ResolvedVal, ValueRef and ConstValueRef productions.
1017 ConstVal: Types '[' ConstVector ']' { // Nonempty unsized arr
1019 $$.cnst = new std::string($1->getNewTy());
1020 *$$.cnst += " [ " + *$3 + " ]";
1025 $$.cnst = new std::string($1->getNewTy());
1028 | Types 'c' STRINGCONSTANT {
1030 $$.cnst = new std::string($1->getNewTy());
1031 *$$.cnst += " c" + *$3;
1034 | Types '<' ConstVector '>' { // Nonempty unsized arr
1036 $$.cnst = new std::string($1->getNewTy());
1037 *$$.cnst += " < " + *$3 + " >";
1040 | Types '{' ConstVector '}' {
1042 $$.cnst = new std::string($1->getNewTy());
1043 *$$.cnst += " { " + *$3 + " }";
1048 $$.cnst = new std::string($1->getNewTy());
1053 $$.cnst = new std::string($1->getNewTy());
1054 *$$.cnst += " " + *$2;
1059 $$.cnst = new std::string($1->getNewTy());
1060 *$$.cnst += " " + *$2;
1063 | Types SymbolicValueRef {
1064 std::string Name = getUniqueName($2, $1->resolve(), true);
1066 $$.cnst = new std::string($1->getNewTy());
1067 *$$.cnst += " " + Name;
1072 $$.cnst = new std::string($1->getNewTy());
1073 *$$.cnst += " " + *$2;
1076 | Types ZEROINITIALIZER {
1078 $$.cnst = new std::string($1->getNewTy());
1079 *$$.cnst += " " + *$2;
1082 | SIntType EInt64Val { // integral constants
1084 $$.cnst = new std::string($1->getNewTy());
1085 *$$.cnst += " " + *$2;
1088 | UIntType EInt64Val { // integral constants
1090 $$.cnst = new std::string($1->getNewTy());
1091 *$$.cnst += " " + *$2;
1094 | BOOL TRUETOK { // Boolean constants
1096 $$.cnst = new std::string($1->getNewTy());
1097 *$$.cnst += " " + *$2;
1100 | BOOL FALSETOK { // Boolean constants
1102 $$.cnst = new std::string($1->getNewTy());
1103 *$$.cnst += " " + *$2;
1106 | FPType FPVAL { // Float & Double constants
1108 $$.cnst = new std::string($1->getNewTy());
1109 *$$.cnst += " " + *$2;
1114 ConstExpr: CastOps '(' ConstVal TO Types ')' {
1115 std::string source = *$3.cnst;
1116 const TypeInfo* SrcTy = $3.type->resolve();
1117 const TypeInfo* DstTy = $5->resolve();
1118 if (*$1 == "cast") {
1119 // Call getCastUpgrade to upgrade the old cast
1120 $$ = new std::string(getCastUpgrade(source, SrcTy, DstTy, true));
1122 // Nothing to upgrade, just create the cast constant expr
1123 $$ = new std::string(*$1);
1124 *$$ += "( " + source + " to " + $5->getNewTy() + ")";
1126 delete $1; $3.destroy(); delete $4;
1128 | GETELEMENTPTR '(' ConstVal IndexList ')' {
1129 *$1 += "(" + *$3.cnst;
1130 for (unsigned i = 0; i < $4->size(); ++i) {
1131 ValueInfo& VI = (*$4)[i];
1132 *$1 += ", " + *VI.val;
1140 | SELECT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1141 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
1142 $3.destroy(); $5.destroy(); $7.destroy();
1145 | ArithmeticOps '(' ConstVal ',' ConstVal ')' {
1146 const char* op = getDivRemOpcode(*$1, $3.type);
1147 $$ = new std::string(op);
1148 *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
1149 delete $1; $3.destroy(); $5.destroy();
1151 | LogicalOps '(' ConstVal ',' ConstVal ')' {
1152 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
1153 $3.destroy(); $5.destroy();
1156 | SetCondOps '(' ConstVal ',' ConstVal ')' {
1157 *$1 = getCompareOp(*$1, $3.type);
1158 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
1159 $3.destroy(); $5.destroy();
1162 | ICMP IPredicates '(' ConstVal ',' ConstVal ')' {
1163 *$1 += " " + *$2 + " (" + *$4.cnst + "," + *$6.cnst + ")";
1164 delete $2; $4.destroy(); $6.destroy();
1167 | FCMP FPredicates '(' ConstVal ',' ConstVal ')' {
1168 *$1 += " " + *$2 + " (" + *$4.cnst + "," + *$6.cnst + ")";
1169 delete $2; $4.destroy(); $6.destroy();
1172 | ShiftOps '(' ConstVal ',' ConstVal ')' {
1173 const char* shiftop = $1->c_str();
1175 shiftop = ($3.type->isUnsigned()) ? "lshr" : "ashr";
1176 $$ = new std::string(shiftop);
1177 *$$ += "(" + *$3.cnst + "," + *$5.cnst + ")";
1178 delete $1; $3.destroy(); $5.destroy();
1180 | EXTRACTELEMENT '(' ConstVal ',' ConstVal ')' {
1181 *$1 += "(" + *$3.cnst + "," + *$5.cnst + ")";
1182 $3.destroy(); $5.destroy();
1185 | INSERTELEMENT '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1186 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
1187 $3.destroy(); $5.destroy(); $7.destroy();
1190 | SHUFFLEVECTOR '(' ConstVal ',' ConstVal ',' ConstVal ')' {
1191 *$1 += "(" + *$3.cnst + "," + *$5.cnst + "," + *$7.cnst + ")";
1192 $3.destroy(); $5.destroy(); $7.destroy();
1197 // ConstVector - A list of comma separated constants.
1200 : ConstVector ',' ConstVal {
1201 *$1 += ", " + *$3.cnst;
1205 | ConstVal { $$ = new std::string(*$1.cnst); $1.destroy(); }
1209 // GlobalType - Match either GLOBAL or CONSTANT for global declarations...
1210 GlobalType : GLOBAL | CONSTANT ;
1213 //===----------------------------------------------------------------------===//
1214 // Rules to match Modules
1215 //===----------------------------------------------------------------------===//
1217 // Module rule: Capture the result of parsing the whole file into a result
1220 Module : DefinitionList {
1223 // DefinitionList - Top level definitions
1225 DefinitionList : DefinitionList Function {
1228 | DefinitionList FunctionProto {
1233 | DefinitionList MODULE ASM_TOK AsmBlock {
1234 *O << "module asm " << ' ' << *$4 << '\n';
1237 | DefinitionList IMPLEMENTATION {
1238 *O << "implementation\n";
1241 | ConstPool { $$ = 0; }
1243 External : EXTERNAL | UNINITIALIZED { $$ = $1; *$$ = "external"; }
1245 // ConstPool - Constants with optional names assigned to them.
1246 ConstPool : ConstPool OptAssign TYPE TypesV {
1247 EnumeratedTypes.push_back($4);
1249 NamedTypes[*$2] = $4;
1252 *O << "type " << $4->getNewTy() << '\n';
1253 delete $2; delete $3;
1256 | ConstPool FunctionProto { // Function prototypes can be in const pool
1261 | ConstPool MODULE ASM_TOK AsmBlock { // Asm blocks can be in the const pool
1262 *O << *$2 << ' ' << *$3 << ' ' << *$4 << '\n';
1263 delete $2; delete $3; delete $4;
1266 | ConstPool OptAssign OptLinkage GlobalType ConstVal GlobalVarAttributes {
1268 std::string Name = getGlobalName($2,*$3, $5.type->getPointerType(),
1270 *O << Name << " = ";
1272 *O << *$3 << ' ' << *$4 << ' ' << *$5.cnst << ' ' << *$6 << '\n';
1273 delete $2; delete $3; delete $4; delete $6;
1276 | ConstPool OptAssign External GlobalType Types GlobalVarAttributes {
1278 std::string Name = getGlobalName($2,*$3,$5->getPointerType(),
1280 *O << Name << " = ";
1282 *O << *$3 << ' ' << *$4 << ' ' << $5->getNewTy() << ' ' << *$6 << '\n';
1283 delete $2; delete $3; delete $4; delete $6;
1286 | ConstPool OptAssign DLLIMPORT GlobalType Types GlobalVarAttributes {
1288 std::string Name = getGlobalName($2,*$3,$5->getPointerType(),
1290 *O << Name << " = ";
1292 *O << *$3 << ' ' << *$4 << ' ' << $5->getNewTy() << ' ' << *$6 << '\n';
1293 delete $2; delete $3; delete $4; delete $6;
1296 | ConstPool OptAssign EXTERN_WEAK GlobalType Types GlobalVarAttributes {
1298 std::string Name = getGlobalName($2,*$3,$5->getPointerType(),
1300 *O << Name << " = ";
1302 *O << *$3 << ' ' << *$4 << ' ' << $5->getNewTy() << ' ' << *$6 << '\n';
1303 delete $2; delete $3; delete $4; delete $6;
1306 | ConstPool TARGET TargetDefinition {
1307 *O << *$2 << ' ' << *$3 << '\n';
1308 delete $2; delete $3;
1311 | ConstPool DEPLIBS '=' LibrariesDefinition {
1312 *O << *$2 << " = " << *$4 << '\n';
1313 delete $2; delete $4;
1316 | /* empty: end of list */ {
1321 AsmBlock : STRINGCONSTANT ;
1323 BigOrLittle : BIG | LITTLE
1326 : ENDIAN '=' BigOrLittle {
1331 | POINTERSIZE '=' EUINT64VAL {
1338 | TRIPLE '=' STRINGCONSTANT {
1343 | DATALAYOUT '=' STRINGCONSTANT {
1351 $2->insert(0, "[ ");
1357 : LibList ',' STRINGCONSTANT {
1363 | /* empty: end of list */ {
1364 $$ = new std::string();
1367 //===----------------------------------------------------------------------===//
1368 // Rules to match Function Headers
1369 //===----------------------------------------------------------------------===//
1371 Name : VAR_ID | STRINGCONSTANT;
1372 OptName : Name | /*empty*/ { $$ = new std::string(); };
1374 ArgVal : Types OptName {
1375 $$ = new std::string($1->getNewTy());
1377 std::string Name = getUniqueName($2, $1->resolve());
1383 ArgListH : ArgListH ',' ArgVal {
1391 ArgList : ArgListH {
1394 | ArgListH ',' DOTDOTDOT {
1402 | /* empty */ { $$ = new std::string(); };
1405 : OptCallingConv TypesV Name '(' ArgList ')' OptSection OptAlign {
1409 *$1 += $2->getNewTy() + " " + *$3 + "(" + *$5 + ")";
1423 BEGIN : BEGINTOK { $$ = new std::string("{"); delete $1; }
1424 | '{' { $$ = new std::string ("{"); }
1427 : OptLinkage FunctionHeaderH BEGIN {
1432 *O << *$2 << ' ' << *$3 << '\n';
1433 delete $1; delete $2; delete $3;
1438 END : ENDTOK { $$ = new std::string("}"); delete $1; }
1439 | '}' { $$ = new std::string("}"); };
1441 Function : FunctionHeader BasicBlockList END {
1444 *O << *$3 << "\n\n";
1445 delete $1; delete $2; delete $3;
1450 : /*default*/ { $$ = new std::string(); }
1456 : DECLARE FnDeclareLinkage FunctionHeaderH {
1465 //===----------------------------------------------------------------------===//
1466 // Rules to match Basic Blocks
1467 //===----------------------------------------------------------------------===//
1469 OptSideEffect : /* empty */ { $$ = new std::string(); }
1473 : ESINT64VAL | EUINT64VAL | FPVAL | TRUETOK | FALSETOK | NULL_TOK | UNDEF
1475 | '<' ConstVector '>' {
1481 | ASM_TOK OptSideEffect STRINGCONSTANT ',' STRINGCONSTANT {
1485 *$1 += " " + *$3 + ", " + *$5;
1486 delete $2; delete $3; delete $5;
1490 SymbolicValueRef : IntVal | Name ;
1492 // ValueRef - A reference to a definition... either constant or symbolic
1494 : SymbolicValueRef {
1496 $$.constant = false;
1506 // ResolvedVal - a <type> <value> pair. This is used only in cases where the
1507 // type immediately preceeds the value reference, and allows complex constant
1508 // pool references (for things like: 'ret [2 x int] [ int 12, int 42]')
1509 ResolvedVal : Types ValueRef {
1511 std::string Name = getUniqueName($2.val, $1);
1514 $$.val = new std::string($1->getNewTy() + " " + Name);
1518 BasicBlockList : BasicBlockList BasicBlock {
1521 | BasicBlock { // Do not allow functions with 0 basic blocks
1526 // Basic blocks are terminated by branching instructions:
1527 // br, br/cc, switch, ret
1529 BasicBlock : InstructionList BBTerminatorInst {
1533 InstructionList : InstructionList Inst {
1534 *O << " " << *$2 << '\n';
1547 Unwind : UNWIND | EXCEPT { $$ = $1; *$$ = "unwind"; }
1549 BBTerminatorInst : RET ResolvedVal { // Return with a result...
1550 *O << " " << *$1 << ' ' << *$2.val << '\n';
1551 delete $1; $2.destroy();
1554 | RET VOID { // Return with no result...
1555 *O << " " << *$1 << ' ' << $2->getNewTy() << '\n';
1559 | BR LABEL ValueRef { // Unconditional Branch...
1560 *O << " " << *$1 << ' ' << $2->getNewTy() << ' ' << *$3.val << '\n';
1561 delete $1; $3.destroy();
1563 } // Conditional Branch...
1564 | BR BOOL ValueRef ',' LABEL ValueRef ',' LABEL ValueRef {
1565 std::string Name = getUniqueName($3.val, $2);
1566 *O << " " << *$1 << ' ' << $2->getNewTy() << ' ' << Name << ", "
1567 << $5->getNewTy() << ' ' << *$6.val << ", " << $8->getNewTy() << ' '
1569 delete $1; $3.destroy(); $6.destroy(); $9.destroy();
1572 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' JumpTable ']' {
1573 std::string Name = getUniqueName($3.val, $2);
1574 *O << " " << *$1 << ' ' << $2->getNewTy() << ' ' << Name << ", "
1575 << $5->getNewTy() << ' ' << *$6.val << " [" << *$8 << " ]\n";
1576 delete $1; $3.destroy(); $6.destroy();
1580 | SWITCH IntType ValueRef ',' LABEL ValueRef '[' ']' {
1581 std::string Name = getUniqueName($3.val, $2);
1582 *O << " " << *$1 << ' ' << $2->getNewTy() << ' ' << Name << ", "
1583 << $5->getNewTy() << ' ' << *$6.val << "[]\n";
1584 delete $1; $3.destroy(); $6.destroy();
1587 | OptAssign INVOKE OptCallingConv TypesV ValueRef '(' ValueRefListE ')'
1588 TO LABEL ValueRef Unwind LABEL ValueRef {
1589 const TypeInfo* ResTy = getFunctionReturnType($4);
1592 std::string Name = getUniqueName($1, ResTy);
1593 *O << Name << " = ";
1595 *O << *$2 << ' ' << *$3 << ' ' << $4->getNewTy() << ' ' << *$5.val << " (";
1596 for (unsigned i = 0; i < $7->size(); ++i) {
1597 ValueInfo& VI = (*$7)[i];
1599 if (i+1 < $7->size())
1603 *O << ") " << *$9 << ' ' << $10->getNewTy() << ' ' << *$11.val << ' '
1604 << *$12 << ' ' << $13->getNewTy() << ' ' << *$14.val << '\n';
1605 delete $1; delete $2; delete $3; $5.destroy(); delete $7;
1606 delete $9; $11.destroy(); delete $12; $14.destroy();
1610 *O << " " << *$1 << '\n';
1615 *O << " " << *$1 << '\n';
1620 JumpTable : JumpTable IntType ConstValueRef ',' LABEL ValueRef {
1621 *$1 += " " + $2->getNewTy() + " " + *$3 + ", " + $5->getNewTy() + " " +
1623 delete $3; $6.destroy();
1626 | IntType ConstValueRef ',' LABEL ValueRef {
1627 $2->insert(0, $1->getNewTy() + " " );
1628 *$2 += ", " + $4->getNewTy() + " " + *$5.val;
1634 : OptAssign InstVal {
1636 // Get a unique name for this value, based on its type.
1637 std::string Name = getUniqueName($1, $2.type);
1639 if (deleteUselessCastFlag && *deleteUselessCastName == Name) {
1640 // don't actually delete it, just comment it out
1641 $1->insert(0, "; USELSS BITCAST: ");
1642 delete deleteUselessCastName;
1647 deleteUselessCastFlag = false;
1652 : Types '[' ValueRef ',' ValueRef ']' { // Used for PHI nodes
1653 std::string Name = getUniqueName($3.val, $1);
1654 Name.insert(0, $1->getNewTy() + "[");
1655 Name += "," + *$5.val + "]";
1656 $$.val = new std::string(Name);
1658 $3.destroy(); $5.destroy();
1660 | PHIList ',' '[' ValueRef ',' ValueRef ']' {
1661 std::string Name = getUniqueName($4.val, $1.type);
1662 *$1.val += ", [" + Name + "," + *$6.val + "]";
1663 $4.destroy(); $6.destroy();
1670 $$ = new ValueList();
1673 | ValueRefList ',' ResolvedVal {
1678 // ValueRefListE - Just like ValueRefList, except that it may also be empty!
1680 : ValueRefList { $$ = $1; }
1681 | /*empty*/ { $$ = new ValueList(); }
1693 InstVal : ArithmeticOps Types ValueRef ',' ValueRef {
1694 const char* op = getDivRemOpcode(*$1, $2);
1695 std::string Name1 = getUniqueName($3.val, $2);
1696 std::string Name2 = getUniqueName($5.val, $2);
1697 $$.val = new std::string(op);
1698 *$$.val += " " + $2->getNewTy() + " " + Name1 + ", " + Name2;
1700 delete $1; $3.destroy(); $5.destroy();
1702 | LogicalOps Types ValueRef ',' ValueRef {
1703 std::string Name1 = getUniqueName($3.val, $2);
1704 std::string Name2 = getUniqueName($5.val, $2);
1705 *$1 += " " + $2->getNewTy() + " " + Name1 + ", " + Name2;
1708 $3.destroy(); $5.destroy();
1710 | SetCondOps Types ValueRef ',' ValueRef {
1711 std::string Name1 = getUniqueName($3.val, $2);
1712 std::string Name2 = getUniqueName($5.val, $2);
1713 *$1 = getCompareOp(*$1, $2);
1714 *$1 += " " + $2->getNewTy() + " " + Name1 + ", " + Name2;
1716 $$.type = TypeInfo::get("bool",BoolTy);
1717 $3.destroy(); $5.destroy();
1719 | ICMP IPredicates Types ValueRef ',' ValueRef {
1720 std::string Name1 = getUniqueName($4.val, $3);
1721 std::string Name2 = getUniqueName($6.val, $3);
1722 *$1 += " " + *$2 + " " + $3->getNewTy() + " " + Name1 + "," + Name2;
1724 $$.type = TypeInfo::get("bool",BoolTy);
1725 delete $2; $4.destroy(); $6.destroy();
1727 | FCMP FPredicates Types ValueRef ',' ValueRef {
1728 std::string Name1 = getUniqueName($4.val, $3);
1729 std::string Name2 = getUniqueName($6.val, $3);
1730 *$1 += " " + *$2 + " " + $3->getNewTy() + " " + Name1 + "," + Name2;
1732 $$.type = TypeInfo::get("bool",BoolTy);
1733 delete $2; $4.destroy(); $6.destroy();
1737 $$.val->insert(0, *$1 + " ");
1740 | ShiftOps ResolvedVal ',' ResolvedVal {
1741 const char* shiftop = $1->c_str();
1743 shiftop = ($2.type->isUnsigned()) ? "lshr" : "ashr";
1744 $$.val = new std::string(shiftop);
1745 *$$.val += " " + *$2.val + ", " + *$4.val;
1747 delete $1; delete $2.val; $4.destroy();
1749 | CastOps ResolvedVal TO Types {
1750 std::string source = *$2.val;
1751 const TypeInfo* SrcTy = $2.type->resolve();
1752 const TypeInfo* DstTy = $4->resolve();
1753 $$.val = new std::string();
1755 if (*$1 == "cast") {
1756 *$$.val += getCastUpgrade(source, SrcTy, DstTy, false);
1758 *$$.val += *$1 + " " + source + " to " + DstTy->getNewTy();
1760 // Check to see if this is a useless cast of a value to the same name
1761 // and the same type. Such casts will probably cause redefinition errors
1762 // when assembled and perform no code gen action so just remove them.
1763 if (*$1 == "cast" || *$1 == "bitcast")
1764 if (SrcTy->isInteger() && DstTy->isInteger() &&
1765 SrcTy->getBitWidth() == DstTy->getBitWidth()) {
1766 deleteUselessCastFlag = true; // Flag the "Inst" rule
1767 deleteUselessCastName = new std::string(*$2.val); // save the name
1768 size_t pos = deleteUselessCastName->find_first_of("%\"",0);
1769 if (pos != std::string::npos) {
1770 // remove the type portion before val
1771 deleteUselessCastName->erase(0, pos);
1774 delete $1; $2.destroy();
1777 | SELECT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1778 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1781 $2.destroy(); delete $4.val; $6.destroy();
1783 | VAARG ResolvedVal ',' Types {
1784 *$1 += " " + *$2.val + ", " + $4->getNewTy();
1789 | EXTRACTELEMENT ResolvedVal ',' ResolvedVal {
1790 *$1 += " " + *$2.val + ", " + *$4.val;
1792 $2.type = $2.type->resolve();;
1793 $$.type = $2.type->getElementType();
1794 delete $2.val; $4.destroy();
1796 | INSERTELEMENT ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1797 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1800 delete $2.val; $4.destroy(); $6.destroy();
1802 | SHUFFLEVECTOR ResolvedVal ',' ResolvedVal ',' ResolvedVal {
1803 *$1 += " " + *$2.val + ", " + *$4.val + ", " + *$6.val;
1806 delete $2.val; $4.destroy(); $6.destroy();
1809 *$1 += " " + *$2.val;
1814 | OptTailCall OptCallingConv TypesV ValueRef '(' ValueRefListE ')' {
1819 *$1 += $3->getNewTy() + " " + *$4.val + "(";
1820 for (unsigned i = 0; i < $6->size(); ++i) {
1821 ValueInfo& VI = (*$6)[i];
1823 if (i+1 < $6->size())
1829 $$.type = getFunctionReturnType($3);
1830 delete $2; $4.destroy(); delete $6;
1835 // IndexList - List of indices for GEP based instructions...
1837 : ',' ValueRefList { $$ = $2; }
1838 | /* empty */ { $$ = new ValueList(); }
1843 | /* empty */ { $$ = new std::string(); }
1846 MemoryInst : MALLOC Types OptCAlign {
1847 *$1 += " " + $2->getNewTy();
1851 $$.type = $2->getPointerType();
1854 | MALLOC Types ',' UINT ValueRef OptCAlign {
1855 std::string Name = getUniqueName($5.val, $4);
1856 *$1 += " " + $2->getNewTy() + ", " + $4->getNewTy() + " " + Name;
1860 $$.type = $2->getPointerType();
1861 $5.destroy(); delete $6;
1863 | ALLOCA Types OptCAlign {
1864 *$1 += " " + $2->getNewTy();
1868 $$.type = $2->getPointerType();
1871 | ALLOCA Types ',' UINT ValueRef OptCAlign {
1872 std::string Name = getUniqueName($5.val, $4);
1873 *$1 += " " + $2->getNewTy() + ", " + $4->getNewTy() + " " + Name;
1877 $$.type = $2->getPointerType();
1878 $5.destroy(); delete $6;
1880 | FREE ResolvedVal {
1881 *$1 += " " + *$2.val;
1883 $$.type = TypeInfo::get("void", VoidTy);
1886 | OptVolatile LOAD Types ValueRef {
1887 std::string Name = getUniqueName($4.val, $3);
1890 *$1 += *$2 + " " + $3->getNewTy() + " " + Name;
1892 $$.type = $3->getElementType();
1893 delete $2; $4.destroy();
1895 | OptVolatile STORE ResolvedVal ',' Types ValueRef {
1896 std::string Name = getUniqueName($6.val, $5);
1899 *$1 += *$2 + " " + *$3.val + ", " + $5->getNewTy() + " " + Name;
1901 $$.type = TypeInfo::get("void", VoidTy);
1902 delete $2; $3.destroy(); $6.destroy();
1904 | GETELEMENTPTR Types ValueRef IndexList {
1905 std::string Name = getUniqueName($3.val, $2);
1906 // Upgrade the indices
1907 for (unsigned i = 0; i < $4->size(); ++i) {
1908 ValueInfo& VI = (*$4)[i];
1909 if (VI.type->isUnsigned() && !VI.isConstant() &&
1910 VI.type->getBitWidth() < 64) {
1911 std::string* old = VI.val;
1912 *O << " %gep_upgrade" << unique << " = zext " << *old
1914 VI.val = new std::string("i64 %gep_upgrade" + llvm::utostr(unique++));
1915 VI.type = TypeInfo::get("i64",ULongTy);
1918 *$1 += " " + $2->getNewTy() + " " + Name;
1919 for (unsigned i = 0; i < $4->size(); ++i) {
1920 ValueInfo& VI = (*$4)[i];
1921 *$1 += ", " + *VI.val;
1924 $$.type = getGEPIndexedType($2,$4);
1925 $3.destroy(); delete $4;
1930 int yyerror(const char *ErrorMsg) {
1932 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
1933 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
1934 std::string errMsg = where + "error: " + std::string(ErrorMsg) +
1936 if (yychar == YYEMPTY || yychar == 0)
1937 errMsg += "end-of-file.";
1939 errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
1940 std::cerr << "llvm-upgrade: " << errMsg << '\n';
1941 *O << "llvm-upgrade parse failed.\n";
1945 static void warning(const std::string& ErrorMsg) {
1947 = std::string((CurFilename == "-") ? std::string("<stdin>") : CurFilename)
1948 + ":" + llvm::utostr((unsigned) Upgradelineno) + ": ";
1949 std::string errMsg = where + "warning: " + std::string(ErrorMsg) +
1951 if (yychar == YYEMPTY || yychar == 0)
1952 errMsg += "end-of-file.";
1954 errMsg += "token: '" + std::string(Upgradetext, Upgradeleng) + "'";
1955 std::cerr << "llvm-upgrade: " << errMsg << '\n';