| /* A Bison parser, made by GNU Bison 2.1. */ |
| |
| /* Skeleton parser for Yacc-like parsing with Bison, |
| Copyright (C) 1984, 1989, 1990, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc. |
| |
| This program is free software; you can redistribute it and/or modify |
| it under the terms of the GNU General Public License as published by |
| the Free Software Foundation; either version 2, or (at your option) |
| any later version. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
| |
| You should have received a copy of the GNU General Public License |
| along with this program; if not, write to the Free Software |
| Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| Boston, MA 02110-1301, USA. */ |
| |
| /* As a special exception, when this file is copied by Bison into a |
| Bison output file, you may use that output file without restriction. |
| This special exception was added by the Free Software Foundation |
| in version 1.24 of Bison. */ |
| |
| /* Written by Richard Stallman by simplifying the original so called |
| ``semantic'' parser. */ |
| |
| /* All symbols defined below should begin with yy or YY, to avoid |
| infringing on user name space. This should be done even for local |
| variables, as they might otherwise be expanded by user macros. |
| There are some unavoidable exceptions within include files to |
| define necessary library symbols; they are noted "INFRINGES ON |
| USER NAME SPACE" below. */ |
| |
| /* Identify Bison output. */ |
| #define YYBISON 1 |
| |
| /* Bison version. */ |
| #define YYBISON_VERSION "2.1" |
| |
| /* Skeleton name. */ |
| #define YYSKELETON_NAME "yacc.c" |
| |
| /* Pure parsers. */ |
| #define YYPURE 0 |
| |
| /* Using locations. */ |
| #define YYLSP_NEEDED 0 |
| |
| /* Substitute the variable and function names. */ |
| #define yyparse Upgradeparse |
| #define yylex Upgradelex |
| #define yyerror Upgradeerror |
| #define yylval Upgradelval |
| #define yychar Upgradechar |
| #define yydebug Upgradedebug |
| #define yynerrs Upgradenerrs |
| |
| |
| /* Tokens. */ |
| #ifndef YYTOKENTYPE |
| # define YYTOKENTYPE |
| /* Put the tokens into the symbol table, so that GDB and other debuggers |
| know about them. */ |
| enum yytokentype { |
| ESINT64VAL = 258, |
| EUINT64VAL = 259, |
| SINTVAL = 260, |
| UINTVAL = 261, |
| FPVAL = 262, |
| VOID = 263, |
| BOOL = 264, |
| SBYTE = 265, |
| UBYTE = 266, |
| SHORT = 267, |
| USHORT = 268, |
| INT = 269, |
| UINT = 270, |
| LONG = 271, |
| ULONG = 272, |
| FLOAT = 273, |
| DOUBLE = 274, |
| TYPE = 275, |
| LABEL = 276, |
| VAR_ID = 277, |
| LABELSTR = 278, |
| STRINGCONSTANT = 279, |
| IMPLEMENTATION = 280, |
| ZEROINITIALIZER = 281, |
| TRUETOK = 282, |
| FALSETOK = 283, |
| BEGINTOK = 284, |
| ENDTOK = 285, |
| DECLARE = 286, |
| GLOBAL = 287, |
| CONSTANT = 288, |
| SECTION = 289, |
| VOLATILE = 290, |
| TO = 291, |
| DOTDOTDOT = 292, |
| NULL_TOK = 293, |
| UNDEF = 294, |
| CONST = 295, |
| INTERNAL = 296, |
| LINKONCE = 297, |
| WEAK = 298, |
| APPENDING = 299, |
| DLLIMPORT = 300, |
| DLLEXPORT = 301, |
| EXTERN_WEAK = 302, |
| OPAQUE = 303, |
| NOT = 304, |
| EXTERNAL = 305, |
| TARGET = 306, |
| TRIPLE = 307, |
| ENDIAN = 308, |
| POINTERSIZE = 309, |
| LITTLE = 310, |
| BIG = 311, |
| ALIGN = 312, |
| DEPLIBS = 313, |
| CALL = 314, |
| TAIL = 315, |
| ASM_TOK = 316, |
| MODULE = 317, |
| SIDEEFFECT = 318, |
| CC_TOK = 319, |
| CCC_TOK = 320, |
| CSRETCC_TOK = 321, |
| FASTCC_TOK = 322, |
| COLDCC_TOK = 323, |
| X86_STDCALLCC_TOK = 324, |
| X86_FASTCALLCC_TOK = 325, |
| DATALAYOUT = 326, |
| RET = 327, |
| BR = 328, |
| SWITCH = 329, |
| INVOKE = 330, |
| UNREACHABLE = 331, |
| UNWIND = 332, |
| EXCEPT = 333, |
| ADD = 334, |
| SUB = 335, |
| MUL = 336, |
| DIV = 337, |
| UDIV = 338, |
| SDIV = 339, |
| FDIV = 340, |
| REM = 341, |
| UREM = 342, |
| SREM = 343, |
| FREM = 344, |
| AND = 345, |
| OR = 346, |
| XOR = 347, |
| SHL = 348, |
| SHR = 349, |
| ASHR = 350, |
| LSHR = 351, |
| SETLE = 352, |
| SETGE = 353, |
| SETLT = 354, |
| SETGT = 355, |
| SETEQ = 356, |
| SETNE = 357, |
| ICMP = 358, |
| FCMP = 359, |
| MALLOC = 360, |
| ALLOCA = 361, |
| FREE = 362, |
| LOAD = 363, |
| STORE = 364, |
| GETELEMENTPTR = 365, |
| PHI_TOK = 366, |
| SELECT = 367, |
| VAARG = 368, |
| EXTRACTELEMENT = 369, |
| INSERTELEMENT = 370, |
| SHUFFLEVECTOR = 371, |
| VAARG_old = 372, |
| VANEXT_old = 373, |
| EQ = 374, |
| NE = 375, |
| SLT = 376, |
| SGT = 377, |
| SLE = 378, |
| SGE = 379, |
| ULT = 380, |
| UGT = 381, |
| ULE = 382, |
| UGE = 383, |
| OEQ = 384, |
| ONE = 385, |
| OLT = 386, |
| OGT = 387, |
| OLE = 388, |
| OGE = 389, |
| ORD = 390, |
| UNO = 391, |
| UEQ = 392, |
| UNE = 393, |
| CAST = 394, |
| TRUNC = 395, |
| ZEXT = 396, |
| SEXT = 397, |
| FPTRUNC = 398, |
| FPEXT = 399, |
| FPTOUI = 400, |
| FPTOSI = 401, |
| UITOFP = 402, |
| SITOFP = 403, |
| PTRTOINT = 404, |
| INTTOPTR = 405, |
| BITCAST = 406 |
| }; |
| #endif |
| /* Tokens. */ |
| #define ESINT64VAL 258 |
| #define EUINT64VAL 259 |
| #define SINTVAL 260 |
| #define UINTVAL 261 |
| #define FPVAL 262 |
| #define VOID 263 |
| #define BOOL 264 |
| #define SBYTE 265 |
| #define UBYTE 266 |
| #define SHORT 267 |
| #define USHORT 268 |
| #define INT 269 |
| #define UINT 270 |
| #define LONG 271 |
| #define ULONG 272 |
| #define FLOAT 273 |
| #define DOUBLE 274 |
| #define TYPE 275 |
| #define LABEL 276 |
| #define VAR_ID 277 |
| #define LABELSTR 278 |
| #define STRINGCONSTANT 279 |
| #define IMPLEMENTATION 280 |
| #define ZEROINITIALIZER 281 |
| #define TRUETOK 282 |
| #define FALSETOK 283 |
| #define BEGINTOK 284 |
| #define ENDTOK 285 |
| #define DECLARE 286 |
| #define GLOBAL 287 |
| #define CONSTANT 288 |
| #define SECTION 289 |
| #define VOLATILE 290 |
| #define TO 291 |
| #define DOTDOTDOT 292 |
| #define NULL_TOK 293 |
| #define UNDEF 294 |
| #define CONST 295 |
| #define INTERNAL 296 |
| #define LINKONCE 297 |
| #define WEAK 298 |
| #define APPENDING 299 |
| #define DLLIMPORT 300 |
| #define DLLEXPORT 301 |
| #define EXTERN_WEAK 302 |
| #define OPAQUE 303 |
| #define NOT 304 |
| #define EXTERNAL 305 |
| #define TARGET 306 |
| #define TRIPLE 307 |
| #define ENDIAN 308 |
| #define POINTERSIZE 309 |
| #define LITTLE 310 |
| #define BIG 311 |
| #define ALIGN 312 |
| #define DEPLIBS 313 |
| #define CALL 314 |
| #define TAIL 315 |
| #define ASM_TOK 316 |
| #define MODULE 317 |
| #define SIDEEFFECT 318 |
| #define CC_TOK 319 |
| #define CCC_TOK 320 |
| #define CSRETCC_TOK 321 |
| #define FASTCC_TOK 322 |
| #define COLDCC_TOK 323 |
| #define X86_STDCALLCC_TOK 324 |
| #define X86_FASTCALLCC_TOK 325 |
| #define DATALAYOUT 326 |
| #define RET 327 |
| #define BR 328 |
| #define SWITCH 329 |
| #define INVOKE 330 |
| #define UNREACHABLE 331 |
| #define UNWIND 332 |
| #define EXCEPT 333 |
| #define ADD 334 |
| #define SUB 335 |
| #define MUL 336 |
| #define DIV 337 |
| #define UDIV 338 |
| #define SDIV 339 |
| #define FDIV 340 |
| #define REM 341 |
| #define UREM 342 |
| #define SREM 343 |
| #define FREM 344 |
| #define AND 345 |
| #define OR 346 |
| #define XOR 347 |
| #define SHL 348 |
| #define SHR 349 |
| #define ASHR 350 |
| #define LSHR 351 |
| #define SETLE 352 |
| #define SETGE 353 |
| #define SETLT 354 |
| #define SETGT 355 |
| #define SETEQ 356 |
| #define SETNE 357 |
| #define ICMP 358 |
| #define FCMP 359 |
| #define MALLOC 360 |
| #define ALLOCA 361 |
| #define FREE 362 |
| #define LOAD 363 |
| #define STORE 364 |
| #define GETELEMENTPTR 365 |
| #define PHI_TOK 366 |
| #define SELECT 367 |
| #define VAARG 368 |
| #define EXTRACTELEMENT 369 |
| #define INSERTELEMENT 370 |
| #define SHUFFLEVECTOR 371 |
| #define VAARG_old 372 |
| #define VANEXT_old 373 |
| #define EQ 374 |
| #define NE 375 |
| #define SLT 376 |
| #define SGT 377 |
| #define SLE 378 |
| #define SGE 379 |
| #define ULT 380 |
| #define UGT 381 |
| #define ULE 382 |
| #define UGE 383 |
| #define OEQ 384 |
| #define ONE 385 |
| #define OLT 386 |
| #define OGT 387 |
| #define OLE 388 |
| #define OGE 389 |
| #define ORD 390 |
| #define UNO 391 |
| #define UEQ 392 |
| #define UNE 393 |
| #define CAST 394 |
| #define TRUNC 395 |
| #define ZEXT 396 |
| #define SEXT 397 |
| #define FPTRUNC 398 |
| #define FPEXT 399 |
| #define FPTOUI 400 |
| #define FPTOSI 401 |
| #define UITOFP 402 |
| #define SITOFP 403 |
| #define PTRTOINT 404 |
| #define INTTOPTR 405 |
| #define BITCAST 406 |
| |
| |
| |
| |
| /* Copy the first part of user declarations. */ |
| #line 14 "/proj/llvm/llvm-20/tools/llvm-upgrade/UpgradeParser.y" |
| |
| #include "UpgradeInternals.h" |
| #include "llvm/CallingConv.h" |
| #include "llvm/InlineAsm.h" |
| #include "llvm/Instructions.h" |
| #include "llvm/Module.h" |
| #include "llvm/ParameterAttributes.h" |
| #include "llvm/ValueSymbolTable.h" |
| #include "llvm/Support/GetElementPtrTypeIterator.h" |
| #include "llvm/ADT/STLExtras.h" |
| #include "llvm/Support/MathExtras.h" |
| #include <algorithm> |
| #include <iostream> |
| #include <map> |
| #include <list> |
| #include <utility> |
| |
| // DEBUG_UPREFS - Define this symbol if you want to enable debugging output |
| // relating to upreferences in the input stream. |
| // |
| //#define DEBUG_UPREFS 1 |
| #ifdef DEBUG_UPREFS |
| #define UR_OUT(X) std::cerr << X |
| #else |
| #define UR_OUT(X) |
| #endif |
| |
| #define YYERROR_VERBOSE 1 |
| #define YYINCLUDED_STDLIB_H |
| #define YYDEBUG 1 |
| |
| int yylex(); |
| int yyparse(); |
| |
| int yyerror(const char*); |
| static void warning(const std::string& WarningMsg); |
| |
| namespace llvm { |
| |
| std::istream* LexInput; |
| static std::string CurFilename; |
| |
| // This bool controls whether attributes are ever added to function declarations |
| // definitions and calls. |
| static bool AddAttributes = false; |
| |
| static Module *ParserResult; |
| static bool ObsoleteVarArgs; |
| static bool NewVarArgs; |
| static BasicBlock *CurBB; |
| static GlobalVariable *CurGV; |
| static unsigned lastCallingConv; |
| |
| // This contains info used when building the body of a function. It is |
| // destroyed when the function is completed. |
| // |
| typedef std::vector<Value *> ValueList; // Numbered defs |
| |
| typedef std::pair<std::string,TypeInfo> RenameMapKey; |
| typedef std::map<RenameMapKey,std::string> RenameMapType; |
| |
| static void |
| ResolveDefinitions(std::map<const Type *,ValueList> &LateResolvers, |
| std::map<const Type *,ValueList> *FutureLateResolvers = 0); |
| |
| static struct PerModuleInfo { |
| Module *CurrentModule; |
| std::map<const Type *, ValueList> Values; // Module level numbered definitions |
| std::map<const Type *,ValueList> LateResolveValues; |
| std::vector<PATypeHolder> Types; |
| std::vector<Signedness> TypeSigns; |
| std::map<std::string,Signedness> NamedTypeSigns; |
| std::map<std::string,Signedness> NamedValueSigns; |
| std::map<ValID, PATypeHolder> LateResolveTypes; |
| static Module::Endianness Endian; |
| static Module::PointerSize PointerSize; |
| RenameMapType RenameMap; |
| |
| /// PlaceHolderInfo - When temporary placeholder objects are created, remember |
| /// how they were referenced and on which line of the input they came from so |
| /// that we can resolve them later and print error messages as appropriate. |
| std::map<Value*, std::pair<ValID, int> > PlaceHolderInfo; |
| |
| // GlobalRefs - This maintains a mapping between <Type, ValID>'s and forward |
| // references to global values. Global values may be referenced before they |
| // are defined, and if so, the temporary object that they represent is held |
| // here. This is used for forward references of GlobalValues. |
| // |
| typedef std::map<std::pair<const PointerType *, ValID>, GlobalValue*> |
| GlobalRefsType; |
| GlobalRefsType GlobalRefs; |
| |
| void ModuleDone() { |
| // If we could not resolve some functions at function compilation time |
| // (calls to functions before they are defined), resolve them now... Types |
| // are resolved when the constant pool has been completely parsed. |
| // |
| ResolveDefinitions(LateResolveValues); |
| |
| // Check to make sure that all global value forward references have been |
| // resolved! |
| // |
| if (!GlobalRefs.empty()) { |
| std::string UndefinedReferences = "Unresolved global references exist:\n"; |
| |
| for (GlobalRefsType::iterator I = GlobalRefs.begin(), E =GlobalRefs.end(); |
| I != E; ++I) { |
| UndefinedReferences += " " + I->first.first->getDescription() + " " + |
| I->first.second.getName() + "\n"; |
| } |
| error(UndefinedReferences); |
| return; |
| } |
| |
| if (CurrentModule->getDataLayout().empty()) { |
| std::string dataLayout; |
| if (Endian != Module::AnyEndianness) |
| dataLayout.append(Endian == Module::BigEndian ? "E" : "e"); |
| if (PointerSize != Module::AnyPointerSize) { |
| if (!dataLayout.empty()) |
| dataLayout += "-"; |
| dataLayout.append(PointerSize == Module::Pointer64 ? |
| "p:64:64" : "p:32:32"); |
| } |
| CurrentModule->setDataLayout(dataLayout); |
| } |
| |
| Values.clear(); // Clear out function local definitions |
| Types.clear(); |
| TypeSigns.clear(); |
| NamedTypeSigns.clear(); |
| NamedValueSigns.clear(); |
| CurrentModule = 0; |
| } |
| |
| // GetForwardRefForGlobal - Check to see if there is a forward reference |
| // for this global. If so, remove it from the GlobalRefs map and return it. |
| // If not, just return null. |
| GlobalValue *GetForwardRefForGlobal(const PointerType *PTy, ValID ID) { |
| // Check to see if there is a forward reference to this global variable... |
| // if there is, eliminate it and patch the reference to use the new def'n. |
| GlobalRefsType::iterator I = GlobalRefs.find(std::make_pair(PTy, ID)); |
| GlobalValue *Ret = 0; |
| if (I != GlobalRefs.end()) { |
| Ret = I->second; |
| GlobalRefs.erase(I); |
| } |
| return Ret; |
| } |
| void setEndianness(Module::Endianness E) { Endian = E; } |
| void setPointerSize(Module::PointerSize sz) { PointerSize = sz; } |
| } CurModule; |
| |
| Module::Endianness PerModuleInfo::Endian = Module::AnyEndianness; |
| Module::PointerSize PerModuleInfo::PointerSize = Module::AnyPointerSize; |
| |
| static struct PerFunctionInfo { |
| Function *CurrentFunction; // Pointer to current function being created |
| |
| std::map<const Type*, ValueList> Values; // Keep track of #'d definitions |
| std::map<const Type*, ValueList> LateResolveValues; |
| bool isDeclare; // Is this function a forward declararation? |
| GlobalValue::LinkageTypes Linkage;// Linkage for forward declaration. |
| |
| /// BBForwardRefs - When we see forward references to basic blocks, keep |
| /// track of them here. |
| std::map<BasicBlock*, std::pair<ValID, int> > BBForwardRefs; |
| std::vector<BasicBlock*> NumberedBlocks; |
| RenameMapType RenameMap; |
| unsigned NextBBNum; |
| |
| inline PerFunctionInfo() { |
| CurrentFunction = 0; |
| isDeclare = false; |
| Linkage = GlobalValue::ExternalLinkage; |
| } |
| |
| inline void FunctionStart(Function *M) { |
| CurrentFunction = M; |
| NextBBNum = 0; |
| } |
| |
| void FunctionDone() { |
| NumberedBlocks.clear(); |
| |
| // Any forward referenced blocks left? |
| if (!BBForwardRefs.empty()) { |
| error("Undefined reference to label " + |
| BBForwardRefs.begin()->first->getName()); |
| return; |
| } |
| |
| // Resolve all forward references now. |
| ResolveDefinitions(LateResolveValues, &CurModule.LateResolveValues); |
| |
| Values.clear(); // Clear out function local definitions |
| RenameMap.clear(); |
| CurrentFunction = 0; |
| isDeclare = false; |
| Linkage = GlobalValue::ExternalLinkage; |
| } |
| } CurFun; // Info for the current function... |
| |
| static bool inFunctionScope() { return CurFun.CurrentFunction != 0; } |
| |
| /// This function is just a utility to make a Key value for the rename map. |
| /// The Key is a combination of the name, type, Signedness of the original |
| /// value (global/function). This just constructs the key and ensures that |
| /// named Signedness values are resolved to the actual Signedness. |
| /// @brief Make a key for the RenameMaps |
| static RenameMapKey makeRenameMapKey(const std::string &Name, const Type* Ty, |
| const Signedness &Sign) { |
| TypeInfo TI; |
| TI.T = Ty; |
| if (Sign.isNamed()) |
| // Don't allow Named Signedness nodes because they won't match. The actual |
| // Signedness must be looked up in the NamedTypeSigns map. |
| TI.S.copy(CurModule.NamedTypeSigns[Sign.getName()]); |
| else |
| TI.S.copy(Sign); |
| return std::make_pair(Name, TI); |
| } |
| |
| |
| //===----------------------------------------------------------------------===// |
| // Code to handle definitions of all the types |
| //===----------------------------------------------------------------------===// |
| |
| static int InsertValue(Value *V, |
| std::map<const Type*,ValueList> &ValueTab = CurFun.Values) { |
| if (V->hasName()) return -1; // Is this a numbered definition? |
| |
| // Yes, insert the value into the value table... |
| ValueList &List = ValueTab[V->getType()]; |
| List.push_back(V); |
| return List.size()-1; |
| } |
| |
| static const Type *getType(const ValID &D, bool DoNotImprovise = false) { |
| switch (D.Type) { |
| case ValID::NumberVal: // Is it a numbered definition? |
| // Module constants occupy the lowest numbered slots... |
| if ((unsigned)D.Num < CurModule.Types.size()) { |
| return CurModule.Types[(unsigned)D.Num]; |
| } |
| break; |
| case ValID::NameVal: // Is it a named definition? |
| if (const Type *N = CurModule.CurrentModule->getTypeByName(D.Name)) { |
| return N; |
| } |
| break; |
| default: |
| error("Internal parser error: Invalid symbol type reference"); |
| return 0; |
| } |
| |
| // If we reached here, we referenced either a symbol that we don't know about |
| // or an id number that hasn't been read yet. We may be referencing something |
| // forward, so just create an entry to be resolved later and get to it... |
| // |
| if (DoNotImprovise) return 0; // Do we just want a null to be returned? |
| |
| if (inFunctionScope()) { |
| if (D.Type == ValID::NameVal) { |
| error("Reference to an undefined type: '" + D.getName() + "'"); |
| return 0; |
| } else { |
| error("Reference to an undefined type: #" + itostr(D.Num)); |
| return 0; |
| } |
| } |
| |
| std::map<ValID, PATypeHolder>::iterator I =CurModule.LateResolveTypes.find(D); |
| if (I != CurModule.LateResolveTypes.end()) |
| return I->second; |
| |
| Type *Typ = OpaqueType::get(); |
| CurModule.LateResolveTypes.insert(std::make_pair(D, Typ)); |
| return Typ; |
| } |
| |
| /// This is like the getType method except that instead of looking up the type |
| /// for a given ID, it looks up that type's sign. |
| /// @brief Get the signedness of a referenced type |
| static Signedness getTypeSign(const ValID &D) { |
| switch (D.Type) { |
| case ValID::NumberVal: // Is it a numbered definition? |
| // Module constants occupy the lowest numbered slots... |
| if ((unsigned)D.Num < CurModule.TypeSigns.size()) { |
| return CurModule.TypeSigns[(unsigned)D.Num]; |
| } |
| break; |
| case ValID::NameVal: { // Is it a named definition? |
| std::map<std::string,Signedness>::const_iterator I = |
| CurModule.NamedTypeSigns.find(D.Name); |
| if (I != CurModule.NamedTypeSigns.end()) |
| return I->second; |
| // Perhaps its a named forward .. just cache the name |
| Signedness S; |
| S.makeNamed(D.Name); |
| return S; |
| } |
| default: |
| break; |
| } |
| // If we don't find it, its signless |
| Signedness S; |
| S.makeSignless(); |
| return S; |
| } |
| |
| /// This function is analagous to getElementType in LLVM. It provides the same |
| /// function except that it looks up the Signedness instead of the type. This is |
| /// used when processing GEP instructions that need to extract the type of an |
| /// indexed struct/array/ptr member. |
| /// @brief Look up an element's sign. |
| static Signedness getElementSign(const ValueInfo& VI, |
| const std::vector<Value*> &Indices) { |
| const Type *Ptr = VI.V->getType(); |
| assert(isa<PointerType>(Ptr) && "Need pointer type"); |
| |
| unsigned CurIdx = 0; |
| Signedness S(VI.S); |
| while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) { |
| if (CurIdx == Indices.size()) |
| break; |
| |
| Value *Index = Indices[CurIdx++]; |
| assert(!isa<PointerType>(CT) || CurIdx == 1 && "Invalid type"); |
| Ptr = CT->getTypeAtIndex(Index); |
| if (const Type* Ty = Ptr->getForwardedType()) |
| Ptr = Ty; |
| assert(S.isComposite() && "Bad Signedness type"); |
| if (isa<StructType>(CT)) { |
| S = S.get(cast<ConstantInt>(Index)->getZExtValue()); |
| } else { |
| S = S.get(0UL); |
| } |
| if (S.isNamed()) |
| S = CurModule.NamedTypeSigns[S.getName()]; |
| } |
| Signedness Result; |
| Result.makeComposite(S); |
| return Result; |
| } |
| |
| /// This function just translates a ConstantInfo into a ValueInfo and calls |
| /// getElementSign(ValueInfo,...). Its just a convenience. |
| /// @brief ConstantInfo version of getElementSign. |
| static Signedness getElementSign(const ConstInfo& CI, |
| const std::vector<Constant*> &Indices) { |
| ValueInfo VI; |
| VI.V = CI.C; |
| VI.S.copy(CI.S); |
| std::vector<Value*> Idx; |
| for (unsigned i = 0; i < Indices.size(); ++i) |
| Idx.push_back(Indices[i]); |
| Signedness result = getElementSign(VI, Idx); |
| VI.destroy(); |
| return result; |
| } |
| |
| /// This function determines if two function types differ only in their use of |
| /// the sret parameter attribute in the first argument. If they are identical |
| /// in all other respects, it returns true. Otherwise, it returns false. |
| static bool FuncTysDifferOnlyBySRet(const FunctionType *F1, |
| const FunctionType *F2) { |
| if (F1->getReturnType() != F2->getReturnType() || |
| F1->getNumParams() != F2->getNumParams()) |
| return false; |
| const ParamAttrsList *PAL1 = F1->getParamAttrs(); |
| const ParamAttrsList *PAL2 = F2->getParamAttrs(); |
| if (PAL1 && !PAL2 || PAL2 && !PAL1) |
| return false; |
| if (PAL1 && PAL2 && ((PAL1->size() != PAL2->size()) || |
| (PAL1->getParamAttrs(0) != PAL2->getParamAttrs(0)))) |
| return false; |
| unsigned SRetMask = ~unsigned(ParamAttr::StructRet); |
| for (unsigned i = 0; i < F1->getNumParams(); ++i) { |
| if (F1->getParamType(i) != F2->getParamType(i) || (PAL1 && PAL2 && |
| (unsigned(PAL1->getParamAttrs(i+1)) & SRetMask != |
| unsigned(PAL2->getParamAttrs(i+1)) & SRetMask))) |
| return false; |
| } |
| return true; |
| } |
| |
| /// This function determines if the type of V and Ty differ only by the SRet |
| /// parameter attribute. This is a more generalized case of |
| /// FuncTysDIfferOnlyBySRet since it doesn't require FunctionType arguments. |
| static bool TypesDifferOnlyBySRet(Value *V, const Type* Ty) { |
| if (V->getType() == Ty) |
| return true; |
| const PointerType *PF1 = dyn_cast<PointerType>(Ty); |
| const PointerType *PF2 = dyn_cast<PointerType>(V->getType()); |
| if (PF1 && PF2) { |
| const FunctionType* FT1 = dyn_cast<FunctionType>(PF1->getElementType()); |
| const FunctionType* FT2 = dyn_cast<FunctionType>(PF2->getElementType()); |
| if (FT1 && FT2) |
| return FuncTysDifferOnlyBySRet(FT1, FT2); |
| } |
| return false; |
| } |
| |
| // The upgrade of csretcc to sret param attribute may have caused a function |
| // to not be found because the param attribute changed the type of the called |
| // function. This helper function, used in getExistingValue, detects that |
| // situation and bitcasts the function to the correct type. |
| static Value* handleSRetFuncTypeMerge(Value *V, const Type* Ty) { |
| // Handle degenerate cases |
| if (!V) |
| return 0; |
| if (V->getType() == Ty) |
| return V; |
| |
| const PointerType *PF1 = dyn_cast<PointerType>(Ty); |
| const PointerType *PF2 = dyn_cast<PointerType>(V->getType()); |
| if (PF1 && PF2) { |
| const FunctionType *FT1 = dyn_cast<FunctionType>(PF1->getElementType()); |
| const FunctionType *FT2 = dyn_cast<FunctionType>(PF2->getElementType()); |
| if (FT1 && FT2 && FuncTysDifferOnlyBySRet(FT1, FT2)) { |
| const ParamAttrsList *PAL2 = FT2->getParamAttrs(); |
| if (PAL2 && PAL2->paramHasAttr(1, ParamAttr::StructRet)) |
| return V; |
| else if (Constant *C = dyn_cast<Constant>(V)) |
| return ConstantExpr::getBitCast(C, PF1); |
| else |
| return new BitCastInst(V, PF1, "upgrd.cast", CurBB); |
| } |
| |
| } |
| return 0; |
| } |
| |
| // getExistingValue - Look up the value specified by the provided type and |
| // the provided ValID. If the value exists and has already been defined, return |
| // it. Otherwise return null. |
| // |
| static Value *getExistingValue(const Type *Ty, const ValID &D) { |
| if (isa<FunctionType>(Ty)) { |
| error("Functions are not values and must be referenced as pointers"); |
| } |
| |
| switch (D.Type) { |
| case ValID::NumberVal: { // Is it a numbered definition? |
| unsigned Num = (unsigned)D.Num; |
| |
| // Module constants occupy the lowest numbered slots... |
| std::map<const Type*,ValueList>::iterator VI = CurModule.Values.find(Ty); |
| if (VI != CurModule.Values.end()) { |
| if (Num < VI->second.size()) |
| return VI->second[Num]; |
| Num -= VI->second.size(); |
| } |
| |
| // Make sure that our type is within bounds |
| VI = CurFun.Values.find(Ty); |
| if (VI == CurFun.Values.end()) return 0; |
| |
| // Check that the number is within bounds... |
| if (VI->second.size() <= Num) return 0; |
| |
| return VI->second[Num]; |
| } |
| |
| case ValID::NameVal: { // Is it a named definition? |
| // Get the name out of the ID |
| RenameMapKey Key = makeRenameMapKey(D.Name, Ty, D.S); |
| Value *V = 0; |
| if (inFunctionScope()) { |
| // See if the name was renamed |
| RenameMapType::const_iterator I = CurFun.RenameMap.find(Key); |
| std::string LookupName; |
| if (I != CurFun.RenameMap.end()) |
| LookupName = I->second; |
| else |
| LookupName = D.Name; |
| ValueSymbolTable &SymTab = CurFun.CurrentFunction->getValueSymbolTable(); |
| V = SymTab.lookup(LookupName); |
| if (V && V->getType() != Ty) |
| V = handleSRetFuncTypeMerge(V, Ty); |
| assert((!V || TypesDifferOnlyBySRet(V, Ty)) && "Found wrong type"); |
| } |
| if (!V) { |
| RenameMapType::const_iterator I = CurModule.RenameMap.find(Key); |
| std::string LookupName; |
| if (I != CurModule.RenameMap.end()) |
| LookupName = I->second; |
| else |
| LookupName = D.Name; |
| V = CurModule.CurrentModule->getValueSymbolTable().lookup(LookupName); |
| if (V && V->getType() != Ty) |
| V = handleSRetFuncTypeMerge(V, Ty); |
| assert((!V || TypesDifferOnlyBySRet(V, Ty)) && "Found wrong type"); |
| } |
| if (!V) |
| return 0; |
| |
| D.destroy(); // Free old strdup'd memory... |
| return V; |
| } |
| |
| // Check to make sure that "Ty" is an integral type, and that our |
| // value will fit into the specified type... |
| case ValID::ConstSIntVal: // Is it a constant pool reference?? |
| if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) { |
| error("Signed integral constant '" + itostr(D.ConstPool64) + |
| "' is invalid for type '" + Ty->getDescription() + "'"); |
| } |
| return ConstantInt::get(Ty, D.ConstPool64); |
| |
| case ValID::ConstUIntVal: // Is it an unsigned const pool reference? |
| if (!ConstantInt::isValueValidForType(Ty, D.UConstPool64)) { |
| if (!ConstantInt::isValueValidForType(Ty, D.ConstPool64)) |
| error("Integral constant '" + utostr(D.UConstPool64) + |
| "' is invalid or out of range"); |
| else // This is really a signed reference. Transmogrify. |
| return ConstantInt::get(Ty, D.ConstPool64); |
| } else |
| return ConstantInt::get(Ty, D.UConstPool64); |
| |
| case ValID::ConstFPVal: // Is it a floating point const pool reference? |
| if (!ConstantFP::isValueValidForType(Ty, D.ConstPoolFP)) |
| error("FP constant invalid for type"); |
| return ConstantFP::get(Ty, D.ConstPoolFP); |
| |
| case ValID::ConstNullVal: // Is it a null value? |
| if (!isa<PointerType>(Ty)) |
| error("Cannot create a a non pointer null"); |
| return ConstantPointerNull::get(cast<PointerType>(Ty)); |
| |
| case ValID::ConstUndefVal: // Is it an undef value? |
| return UndefValue::get(Ty); |
| |
| case ValID::ConstZeroVal: // Is it a zero value? |
| return Constant::getNullValue(Ty); |
| |
| case ValID::ConstantVal: // Fully resolved constant? |
| if (D.ConstantValue->getType() != Ty) |
| error("Constant expression type different from required type"); |
| return D.ConstantValue; |
| |
| case ValID::InlineAsmVal: { // Inline asm expression |
| const PointerType *PTy = dyn_cast<PointerType>(Ty); |
| const FunctionType *FTy = |
| PTy ? dyn_cast<FunctionType>(PTy->getElementType()) : 0; |
| if (!FTy || !InlineAsm::Verify(FTy, D.IAD->Constraints)) |
| error("Invalid type for asm constraint string"); |
| InlineAsm *IA = InlineAsm::get(FTy, D.IAD->AsmString, D.IAD->Constraints, |
| D.IAD->HasSideEffects); |
| D.destroy(); // Free InlineAsmDescriptor. |
| return IA; |
| } |
| default: |
| assert(0 && "Unhandled case"); |
| return 0; |
| } // End of switch |
| |
| assert(0 && "Unhandled case"); |
| return 0; |
| } |
| |
| // getVal - This function is identical to getExistingValue, except that if a |
| // value is not already defined, it "improvises" by creating a placeholder var |
| // that looks and acts just like the requested variable. When the value is |
| // defined later, all uses of the placeholder variable are replaced with the |
| // real thing. |
| // |
| static Value *getVal(const Type *Ty, const ValID &ID) { |
| if (Ty == Type::LabelTy) |
| error("Cannot use a basic block here"); |
| |
| // See if the value has already been defined. |
| Value *V = getExistingValue(Ty, ID); |
| if (V) return V; |
| |
| if (!Ty->isFirstClassType() && !isa<OpaqueType>(Ty)) |
| error("Invalid use of a composite type"); |
| |
| // If we reached here, we referenced either a symbol that we don't know about |
| // or an id number that hasn't been read yet. We may be referencing something |
| // forward, so just create an entry to be resolved later and get to it... |
| V = new Argument(Ty); |
| |
| // Remember where this forward reference came from. FIXME, shouldn't we try |
| // to recycle these things?? |
| CurModule.PlaceHolderInfo.insert( |
| std::make_pair(V, std::make_pair(ID, Upgradelineno))); |
| |
| if (inFunctionScope()) |
| InsertValue(V, CurFun.LateResolveValues); |
| else |
| InsertValue(V, CurModule.LateResolveValues); |
| return V; |
| } |
| |
| /// @brief This just makes any name given to it unique, up to MAX_UINT times. |
| static std::string makeNameUnique(const std::string& Name) { |
| static unsigned UniqueNameCounter = 1; |
| std::string Result(Name); |
| Result += ".upgrd." + llvm::utostr(UniqueNameCounter++); |
| return Result; |
| } |
| |
| /// getBBVal - This is used for two purposes: |
| /// * If isDefinition is true, a new basic block with the specified ID is being |
| /// defined. |
| /// * If isDefinition is true, this is a reference to a basic block, which may |
| /// or may not be a forward reference. |
| /// |
| static BasicBlock *getBBVal(const ValID &ID, bool isDefinition = false) { |
| assert(inFunctionScope() && "Can't get basic block at global scope"); |
| |
| std::string Name; |
| BasicBlock *BB = 0; |
| switch (ID.Type) { |
| default: |
| error("Illegal label reference " + ID.getName()); |
| break; |
| case ValID::NumberVal: // Is it a numbered definition? |
| if (unsigned(ID.Num) >= CurFun.NumberedBlocks.size()) |
| CurFun.NumberedBlocks.resize(ID.Num+1); |
| BB = CurFun.NumberedBlocks[ID.Num]; |
| break; |
| case ValID::NameVal: // Is it a named definition? |
| Name = ID.Name; |
| if (Value *N = CurFun.CurrentFunction->getValueSymbolTable().lookup(Name)) { |
| if (N->getType() != Type::LabelTy) { |
| // Register names didn't use to conflict with basic block names |
| // because of type planes. Now they all have to be unique. So, we just |
| // rename the register and treat this name as if no basic block |
| // had been found. |
| RenameMapKey Key = makeRenameMapKey(ID.Name, N->getType(), ID.S); |
| N->setName(makeNameUnique(N->getName())); |
| CurModule.RenameMap[Key] = N->getName(); |
| BB = 0; |
| } else { |
| BB = cast<BasicBlock>(N); |
| } |
| } |
| break; |
| } |
| |
| // See if the block has already been defined. |
| if (BB) { |
| // If this is the definition of the block, make sure the existing value was |
| // just a forward reference. If it was a forward reference, there will be |
| // an entry for it in the PlaceHolderInfo map. |
| if (isDefinition && !CurFun.BBForwardRefs.erase(BB)) |
| // The existing value was a definition, not a forward reference. |
| error("Redefinition of label " + ID.getName()); |
| |
| ID.destroy(); // Free strdup'd memory. |
| return BB; |
| } |
| |
| // Otherwise this block has not been seen before. |
| BB = new BasicBlock("", CurFun.CurrentFunction); |
| if (ID.Type == ValID::NameVal) { |
| BB->setName(ID.Name); |
| } else { |
| CurFun.NumberedBlocks[ID.Num] = BB; |
| } |
| |
| // If this is not a definition, keep track of it so we can use it as a forward |
| // reference. |
| if (!isDefinition) { |
| // Remember where this forward reference came from. |
| CurFun.BBForwardRefs[BB] = std::make_pair(ID, Upgradelineno); |
| } else { |
| // The forward declaration could have been inserted anywhere in the |
| // function: insert it into the correct place now. |
| CurFun.CurrentFunction->getBasicBlockList().remove(BB); |
| CurFun.CurrentFunction->getBasicBlockList().push_back(BB); |
| } |
| ID.destroy(); |
| return BB; |
| } |
| |
| |
| //===----------------------------------------------------------------------===// |
| // Code to handle forward references in instructions |
| //===----------------------------------------------------------------------===// |
| // |
| // This code handles the late binding needed with statements that reference |
| // values not defined yet... for example, a forward branch, or the PHI node for |
| // a loop body. |
| // |
| // This keeps a table (CurFun.LateResolveValues) of all such forward references |
| // and back patchs after we are done. |
| // |
| |
| // ResolveDefinitions - If we could not resolve some defs at parsing |
| // time (forward branches, phi functions for loops, etc...) resolve the |
| // defs now... |
| // |
| static void |
| ResolveDefinitions(std::map<const Type*,ValueList> &LateResolvers, |
| std::map<const Type*,ValueList> *FutureLateResolvers) { |
| |
| // Loop over LateResolveDefs fixing up stuff that couldn't be resolved |
| for (std::map<const Type*,ValueList>::iterator LRI = LateResolvers.begin(), |
| E = LateResolvers.end(); LRI != E; ++LRI) { |
| const Type* Ty = LRI->first; |
| ValueList &List = LRI->second; |
| while (!List.empty()) { |
| Value *V = List.back(); |
| List.pop_back(); |
| |
| std::map<Value*, std::pair<ValID, int> >::iterator PHI = |
| CurModule.PlaceHolderInfo.find(V); |
| assert(PHI != CurModule.PlaceHolderInfo.end() && "Placeholder error"); |
| |
| ValID &DID = PHI->second.first; |
| |
| Value *TheRealValue = getExistingValue(Ty, DID); |
| if (TheRealValue) { |
| V->replaceAllUsesWith(TheRealValue); |
| delete V; |
| CurModule.PlaceHolderInfo.erase(PHI); |
| } else if (FutureLateResolvers) { |
| // Functions have their unresolved items forwarded to the module late |
| // resolver table |
| InsertValue(V, *FutureLateResolvers); |
| } else { |
| if (DID.Type == ValID::NameVal) { |
| error("Reference to an invalid definition: '" + DID.getName() + |
| "' of type '" + V->getType()->getDescription() + "'", |
| PHI->second.second); |
| return; |
| } else { |
| error("Reference to an invalid definition: #" + |
| itostr(DID.Num) + " of type '" + |
| V->getType()->getDescription() + "'", PHI->second.second); |
| return; |
| } |
| } |
| } |
| } |
| |
| LateResolvers.clear(); |
| } |
| |
| /// This function is used for type resolution and upref handling. When a type |
| /// becomes concrete, this function is called to adjust the signedness for the |
| /// concrete type. |
| static void ResolveTypeSign(const Type* oldTy, const Signedness &Sign) { |
| std::string TyName = CurModule.CurrentModule->getTypeName(oldTy); |
| if (!TyName.empty()) |
| CurModule.NamedTypeSigns[TyName] = Sign; |
| } |
| |
| /// ResolveTypeTo - A brand new type was just declared. This means that (if |
| /// name is not null) things referencing Name can be resolved. Otherwise, |
| /// things refering to the number can be resolved. Do this now. |
| static void ResolveTypeTo(char *Name, const Type *ToTy, const Signedness& Sign){ |
| ValID D; |
| if (Name) |
| D = ValID::create(Name); |
| else |
| D = ValID::create((int)CurModule.Types.size()); |
| D.S.copy(Sign); |
| |
| if (Name) |
| CurModule.NamedTypeSigns[Name] = Sign; |
| |
| std::map<ValID, PATypeHolder>::iterator I = |
| CurModule.LateResolveTypes.find(D); |
| if (I != CurModule.LateResolveTypes.end()) { |
| const Type *OldTy = I->second.get(); |
| ((DerivedType*)OldTy)->refineAbstractTypeTo(ToTy); |
| CurModule.LateResolveTypes.erase(I); |
| } |
| } |
| |
| /// This is the implementation portion of TypeHasInteger. It traverses the |
| /// type given, avoiding recursive types, and returns true as soon as it finds |
| /// an integer type. If no integer type is found, it returns false. |
| static bool TypeHasIntegerI(const Type *Ty, std::vector<const Type*> Stack) { |
| // Handle some easy cases |
| if (Ty->isPrimitiveType() || (Ty->getTypeID() == Type::OpaqueTyID)) |
| return false; |
| if (Ty->isInteger()) |
| return true; |
| if (const SequentialType *STy = dyn_cast<SequentialType>(Ty)) |
| return STy->getElementType()->isInteger(); |
| |
| // Avoid type structure recursion |
| for (std::vector<const Type*>::iterator I = Stack.begin(), E = Stack.end(); |
| I != E; ++I) |
| if (Ty == *I) |
| return false; |
| |
| // Push us on the type stack |
| Stack.push_back(Ty); |
| |
| if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty)) { |
| if (TypeHasIntegerI(FTy->getReturnType(), Stack)) |
| return true; |
| FunctionType::param_iterator I = FTy->param_begin(); |
| FunctionType::param_iterator E = FTy->param_end(); |
| for (; I != E; ++I) |
| if (TypeHasIntegerI(*I, Stack)) |
| return true; |
| return false; |
| } else if (const StructType *STy = dyn_cast<StructType>(Ty)) { |
| StructType::element_iterator I = STy->element_begin(); |
| StructType::element_iterator E = STy->element_end(); |
| for (; I != E; ++I) { |
| if (TypeHasIntegerI(*I, Stack)) |
| return true; |
| } |
| return false; |
| } |
| // There shouldn't be anything else, but its definitely not integer |
| assert(0 && "What type is this?"); |
| return false; |
| } |
| |
| /// This is the interface to TypeHasIntegerI. It just provides the type stack, |
| /// to avoid recursion, and then calls TypeHasIntegerI. |
| static inline bool TypeHasInteger(const Type *Ty) { |
| std::vector<const Type*> TyStack; |
| return TypeHasIntegerI(Ty, TyStack); |
| } |
| |
| // setValueName - Set the specified value to the name given. The name may be |
| // null potentially, in which case this is a noop. The string passed in is |
| // assumed to be a malloc'd string buffer, and is free'd by this function. |
| // |
| static void setValueName(const ValueInfo &V, char *NameStr) { |
| if (NameStr) { |
| std::string Name(NameStr); // Copy string |
| free(NameStr); // Free old string |
| |
| if (V.V->getType() == Type::VoidTy) { |
| error("Can't assign name '" + Name + "' to value with void type"); |
| return; |
| } |
| |
| assert(inFunctionScope() && "Must be in function scope"); |
| |
| // Search the function's symbol table for an existing value of this name |
| ValueSymbolTable &ST = CurFun.CurrentFunction->getValueSymbolTable(); |
| Value* Existing = ST.lookup(Name); |
| if (Existing) { |
| // An existing value of the same name was found. This might have happened |
| // because of the integer type planes collapsing in LLVM 2.0. |
| if (Existing->getType() == V.V->getType() && |
| !TypeHasInteger(Existing->getType())) { |
| // If the type does not contain any integers in them then this can't be |
| // a type plane collapsing issue. It truly is a redefinition and we |
| // should error out as the assembly is invalid. |
| error("Redefinition of value named '" + Name + "' of type '" + |
| V.V->getType()->getDescription() + "'"); |
| return; |
| } |
| // In LLVM 2.0 we don't allow names to be re-used for any values in a |
| // function, regardless of Type. Previously re-use of names was okay as |
| // long as they were distinct types. With type planes collapsing because |
| // of the signedness change and because of PR411, this can no longer be |
| // supported. We must search the entire symbol table for a conflicting |
| // name and make the name unique. No warning is needed as this can't |
| // cause a problem. |
| std::string NewName = makeNameUnique(Name); |
| // We're changing the name but it will probably be used by other |
| // instructions as operands later on. Consequently we have to retain |
| // a mapping of the renaming that we're doing. |
| RenameMapKey Key = makeRenameMapKey(Name, V.V->getType(), V.S); |
| CurFun.RenameMap[Key] = NewName; |
| Name = NewName; |
| } |
| |
| // Set the name. |
| V.V->setName(Name); |
| } |
| } |
| |
| /// ParseGlobalVariable - Handle parsing of a global. If Initializer is null, |
| /// this is a declaration, otherwise it is a definition. |
| static GlobalVariable * |
| ParseGlobalVariable(char *NameStr,GlobalValue::LinkageTypes Linkage, |
| bool isConstantGlobal, const Type *Ty, |
| Constant *Initializer, |
| const Signedness &Sign) { |
| if (isa<FunctionType>(Ty)) |
| error("Cannot declare global vars of function type"); |
| |
| const PointerType *PTy = PointerType::get(Ty); |
| |
| std::string Name; |
| if (NameStr) { |
| Name = NameStr; // Copy string |
| free(NameStr); // Free old string |
| } |
| |
| // See if this global value was forward referenced. If so, recycle the |
| // object. |
| ValID ID; |
| if (!Name.empty()) { |
| ID = ValID::create((char*)Name.c_str()); |
| } else { |
| ID = ValID::create((int)CurModule.Values[PTy].size()); |
| } |
| ID.S.makeComposite(Sign); |
| |
| if (GlobalValue *FWGV = CurModule.GetForwardRefForGlobal(PTy, ID)) { |
| // Move the global to the end of the list, from whereever it was |
| // previously inserted. |
| GlobalVariable *GV = cast<GlobalVariable>(FWGV); |
| CurModule.CurrentModule->getGlobalList().remove(GV); |
| CurModule.CurrentModule->getGlobalList().push_back(GV); |
| GV->setInitializer(Initializer); |
| GV->setLinkage(Linkage); |
| GV->setConstant(isConstantGlobal); |
| InsertValue(GV, CurModule.Values); |
| return GV; |
| } |
| |
| // If this global has a name, check to see if there is already a definition |
| // of this global in the module and emit warnings if there are conflicts. |
| if (!Name.empty()) { |
| // The global has a name. See if there's an existing one of the same name. |
| if (CurModule.CurrentModule->getNamedGlobal(Name) || |
| CurModule.CurrentModule->getFunction(Name)) { |
| // We found an existing global of the same name. This isn't allowed |
| // in LLVM 2.0. Consequently, we must alter the name of the global so it |
| // can at least compile. This can happen because of type planes |
| // There is alread a global of the same name which means there is a |
| // conflict. Let's see what we can do about it. |
| std::string NewName(makeNameUnique(Name)); |
| if (Linkage != GlobalValue::InternalLinkage) { |
| // The linkage of this gval is external so we can't reliably rename |
| // it because it could potentially create a linking problem. |
| // However, we can't leave the name conflict in the output either or |
| // it won't assemble with LLVM 2.0. So, all we can do is rename |
| // this one to something unique and emit a warning about the problem. |
| warning("Renaming global variable '" + Name + "' to '" + NewName + |
| "' may cause linkage errors"); |
| } |
| |
| // Put the renaming in the global rename map |
| RenameMapKey Key = makeRenameMapKey(Name, PointerType::get(Ty), ID.S); |
| CurModule.RenameMap[Key] = NewName; |
| |
| // Rename it |
| Name = NewName; |
| } |
| } |
| |
| // Otherwise there is no existing GV to use, create one now. |
| GlobalVariable *GV = |
| new GlobalVariable(Ty, isConstantGlobal, Linkage, Initializer, Name, |
| CurModule.CurrentModule); |
| InsertValue(GV, CurModule.Values); |
| // Remember the sign of this global. |
| CurModule.NamedValueSigns[Name] = ID.S; |
| return GV; |
| } |
| |
| // setTypeName - Set the specified type to the name given. The name may be |
| // null potentially, in which case this is a noop. The string passed in is |
| // assumed to be a malloc'd string buffer, and is freed by this function. |
| // |
| // This function returns true if the type has already been defined, but is |
| // allowed to be redefined in the specified context. If the name is a new name |
| // for the type plane, it is inserted and false is returned. |
| static bool setTypeName(const PATypeInfo& TI, char *NameStr) { |
| assert(!inFunctionScope() && "Can't give types function-local names"); |
| if (NameStr == 0) return false; |
| |
| std::string Name(NameStr); // Copy string |
| free(NameStr); // Free old string |
| |
| const Type* Ty = TI.PAT->get(); |
| |
| // We don't allow assigning names to void type |
| if (Ty == Type::VoidTy) { |
| error("Can't assign name '" + Name + "' to the void type"); |
| return false; |
| } |
| |
| // Set the type name, checking for conflicts as we do so. |
| bool AlreadyExists = CurModule.CurrentModule->addTypeName(Name, Ty); |
| |
| // Save the sign information for later use |
| CurModule.NamedTypeSigns[Name] = TI.S; |
| |
| if (AlreadyExists) { // Inserting a name that is already defined??? |
| const Type *Existing = CurModule.CurrentModule->getTypeByName(Name); |
| assert(Existing && "Conflict but no matching type?"); |
| |
| // There is only one case where this is allowed: when we are refining an |
| // opaque type. In this case, Existing will be an opaque type. |
| if (const OpaqueType *OpTy = dyn_cast<OpaqueType>(Existing)) { |
| // We ARE replacing an opaque type! |
| const_cast<OpaqueType*>(OpTy)->refineAbstractTypeTo(Ty); |
| return true; |
| } |
| |
| // Otherwise, this is an attempt to redefine a type. That's okay if |
| // the redefinition is identical to the original. This will be so if |
| // Existing and T point to the same Type object. In this one case we |
| // allow the equivalent redefinition. |
| if (Existing == Ty) return true; // Yes, it's equal. |
| |
| // Any other kind of (non-equivalent) redefinition is an error. |
| error("Redefinition of type named '" + Name + "' in the '" + |
| Ty->getDescription() + "' type plane"); |
| } |
| |
| return false; |
| } |
| |
| //===----------------------------------------------------------------------===// |
| // Code for handling upreferences in type names... |
| // |
| |
| // TypeContains - Returns true if Ty directly contains E in it. |
| // |
| static bool TypeContains(const Type *Ty, const Type *E) { |
| return std::find(Ty->subtype_begin(), Ty->subtype_end(), |
| E) != Ty->subtype_end(); |
| } |
| |
| namespace { |
| struct UpRefRecord { |
| // NestingLevel - The number of nesting levels that need to be popped before |
| // this type is resolved. |
| unsigned NestingLevel; |
| |
| // LastContainedTy - This is the type at the current binding level for the |
| // type. Every time we reduce the nesting level, this gets updated. |
| const Type *LastContainedTy; |
| |
| // UpRefTy - This is the actual opaque type that the upreference is |
| // represented with. |
| OpaqueType *UpRefTy; |
| |
| UpRefRecord(unsigned NL, OpaqueType *URTy) |
| : NestingLevel(NL), LastContainedTy(URTy), UpRefTy(URTy) { } |
| }; |
| } |
| |
| // UpRefs - A list of the outstanding upreferences that need to be resolved. |
| static std::vector<UpRefRecord> UpRefs; |
| |
| /// HandleUpRefs - Every time we finish a new layer of types, this function is |
| /// called. It loops through the UpRefs vector, which is a list of the |
| /// currently active types. For each type, if the up reference is contained in |
| /// the newly completed type, we decrement the level count. When the level |
| /// count reaches zero, the upreferenced type is the type that is passed in: |
| /// thus we can complete the cycle. |
| /// |
| static PATypeHolder HandleUpRefs(const Type *ty, const Signedness& Sign) { |
| // If Ty isn't abstract, or if there are no up-references in it, then there is |
| // nothing to resolve here. |
| if (!ty->isAbstract() || UpRefs.empty()) return ty; |
| |
| PATypeHolder Ty(ty); |
| UR_OUT("Type '" << Ty->getDescription() << |
| "' newly formed. Resolving upreferences.\n" << |
| UpRefs.size() << " upreferences active!\n"); |
| |
| // If we find any resolvable upreferences (i.e., those whose NestingLevel goes |
| // to zero), we resolve them all together before we resolve them to Ty. At |
| // the end of the loop, if there is anything to resolve to Ty, it will be in |
| // this variable. |
| OpaqueType *TypeToResolve = 0; |
| |
| unsigned i = 0; |
| for (; i != UpRefs.size(); ++i) { |
| UR_OUT(" UR#" << i << " - TypeContains(" << Ty->getDescription() << ", " |
| << UpRefs[i].UpRefTy->getDescription() << ") = " |
| << (TypeContains(Ty, UpRefs[i].UpRefTy) ? "true" : "false") << "\n"); |
| if (TypeContains(Ty, UpRefs[i].LastContainedTy)) { |
| // Decrement level of upreference |
| unsigned Level = --UpRefs[i].NestingLevel; |
| UpRefs[i].LastContainedTy = Ty; |
| UR_OUT(" Uplevel Ref Level = " << Level << "\n"); |
| if (Level == 0) { // Upreference should be resolved! |
| if (!TypeToResolve) { |
| TypeToResolve = UpRefs[i].UpRefTy; |
| } else { |
| UR_OUT(" * Resolving upreference for " |
| << UpRefs[i].UpRefTy->getDescription() << "\n"; |
| std::string OldName = UpRefs[i].UpRefTy->getDescription()); |
| ResolveTypeSign(UpRefs[i].UpRefTy, Sign); |
| UpRefs[i].UpRefTy->refineAbstractTypeTo(TypeToResolve); |
| UR_OUT(" * Type '" << OldName << "' refined upreference to: " |
| << (const void*)Ty << ", " << Ty->getDescription() << "\n"); |
| } |
| UpRefs.erase(UpRefs.begin()+i); // Remove from upreference list... |
| --i; // Do not skip the next element... |
| } |
| } |
| } |
| |
| if (TypeToResolve) { |
| UR_OUT(" * Resolving upreference for " |
| << UpRefs[i].UpRefTy->getDescription() << "\n"; |
| std::string OldName = TypeToResolve->getDescription()); |
| ResolveTypeSign(TypeToResolve, Sign); |
| TypeToResolve->refineAbstractTypeTo(Ty); |
| } |
| |
| return Ty; |
| } |
| |
| bool Signedness::operator<(const Signedness &that) const { |
| if (isNamed()) { |
| if (that.isNamed()) |
| return *(this->name) < *(that.name); |
| else |
| return CurModule.NamedTypeSigns[*name] < that; |
| } else if (that.isNamed()) { |
| return *this < CurModule.NamedTypeSigns[*that.name]; |
| } |
| |
| if (isComposite() && that.isComposite()) { |
| if (sv->size() == that.sv->size()) { |
| SignVector::const_iterator thisI = sv->begin(), thisE = sv->end(); |
| SignVector::const_iterator thatI = that.sv->begin(), |
| thatE = that.sv->end(); |
| for (; thisI != thisE; ++thisI, ++thatI) { |
| if (*thisI < *thatI) |
| return true; |
| else if (!(*thisI == *thatI)) |
| return false; |
| } |
| return false; |
| } |
| return sv->size() < that.sv->size(); |
| } |
| return kind < that.kind; |
| } |
| |
| bool Signedness::operator==(const Signedness &that) const { |
| if (isNamed()) |
| if (that.isNamed()) |
| return *(this->name) == *(that.name); |
| else |
| return CurModule.NamedTypeSigns[*(this->name)] == that; |
| else if (that.isNamed()) |
| return *this == CurModule.NamedTypeSigns[*(that.name)]; |
| if (isComposite() && that.isComposite()) { |
| if (sv->size() == that.sv->size()) { |
| SignVector::const_iterator thisI = sv->begin(), thisE = sv->end(); |
| SignVector::const_iterator thatI = that.sv->begin(), |
| thatE = that.sv->end(); |
| for (; thisI != thisE; ++thisI, ++thatI) { |
| if (!(*thisI == *thatI)) |
| return false; |
| } |
| return true; |
| } |
| return false; |
| } |
| return kind == that.kind; |
| } |
| |
| void Signedness::copy(const Signedness &that) { |
| if (that.isNamed()) { |
| kind = Named; |
| name = new std::string(*that.name); |
| } else if (that.isComposite()) { |
| kind = Composite; |
| sv = new SignVector(); |
| *sv = *that.sv; |
| } else { |
| kind = that.kind; |
| sv = 0; |
| } |
| } |
| |
| void Signedness::destroy() { |
| if (isNamed()) { |
| delete name; |
| } else if (isComposite()) { |
| delete sv; |
| } |
| } |
| |
| #ifndef NDEBUG |
| void Signedness::dump() const { |
| if (isComposite()) { |
| if (sv->size() == 1) { |
| (*sv)[0].dump(); |
| std::cerr << "*"; |
| } else { |
| std::cerr << "{ " ; |
| for (unsigned i = 0; i < sv->size(); ++i) { |
| if (i != 0) |
| std::cerr << ", "; |
| (*sv)[i].dump(); |
| } |
| std::cerr << "} " ; |
| } |
| } else if (isNamed()) { |
| std::cerr << *name; |
| } else if (isSigned()) { |
| std::cerr << "S"; |
| } else if (isUnsigned()) { |
| std::cerr << "U"; |
| } else |
| std::cerr << "."; |
| } |
| #endif |
| |
| static inline Instruction::TermOps |
| getTermOp(TermOps op) { |
| switch (op) { |
| default : assert(0 && "Invalid OldTermOp"); |
| case RetOp : return Instruction::Ret; |
| case BrOp : return Instruction::Br; |
| case SwitchOp : return Instruction::Switch; |
| case InvokeOp : return Instruction::Invoke; |
| case UnwindOp : return Instruction::Unwind; |
| case UnreachableOp: return Instruction::Unreachable; |
| } |
| } |
| |
| static inline Instruction::BinaryOps |
| getBinaryOp(BinaryOps op, const Type *Ty, const Signedness& Sign) { |
| switch (op) { |
| default : assert(0 && "Invalid OldBinaryOps"); |
| case SetEQ : |
| case SetNE : |
| case SetLE : |
| case SetGE : |
| case SetLT : |
| case SetGT : assert(0 && "Should use getCompareOp"); |
| case AddOp : return Instruction::Add; |
| case SubOp : return Instruction::Sub; |
| case MulOp : return Instruction::Mul; |
| case DivOp : { |
| // This is an obsolete instruction so we must upgrade it based on the |
| // types of its operands. |
| bool isFP = Ty->isFloatingPoint(); |
| if (const VectorType* PTy = dyn_cast<VectorType>(Ty)) |
| // If its a vector type we want to use the element type |
| isFP = PTy->getElementType()->isFloatingPoint(); |
| if (isFP) |
| return Instruction::FDiv; |
| else if (Sign.isSigned()) |
| return Instruction::SDiv; |
| return Instruction::UDiv; |
| } |
| case UDivOp : return Instruction::UDiv; |
| case SDivOp : return Instruction::SDiv; |
| case FDivOp : return Instruction::FDiv; |
| case RemOp : { |
| // This is an obsolete instruction so we must upgrade it based on the |
| // types of its operands. |
| bool isFP = Ty->isFloatingPoint(); |
| if (const VectorType* PTy = dyn_cast<VectorType>(Ty)) |
| // If its a vector type we want to use the element type |
| isFP = PTy->getElementType()->isFloatingPoint(); |
| // Select correct opcode |
| if (isFP) |
| return Instruction::FRem; |
| else if (Sign.isSigned()) |
| return Instruction::SRem; |
| return Instruction::URem; |
| } |
| case URemOp : return Instruction::URem; |
| case SRemOp : return Instruction::SRem; |
| case FRemOp : return Instruction::FRem; |
| case LShrOp : return Instruction::LShr; |
| case AShrOp : return Instruction::AShr; |
| case ShlOp : return Instruction::Shl; |
| case ShrOp : |
| if (Sign.isSigned()) |
| return Instruction::AShr; |
| return Instruction::LShr; |
| case AndOp : return Instruction::And; |
| case OrOp : return Instruction::Or; |
| case XorOp : return Instruction::Xor; |
| } |
| } |
| |
| static inline Instruction::OtherOps |
| getCompareOp(BinaryOps op, unsigned short &predicate, const Type* &Ty, |
| const Signedness &Sign) { |
| bool isSigned = Sign.isSigned(); |
| bool isFP = Ty->isFloatingPoint(); |
| switch (op) { |
| default : assert(0 && "Invalid OldSetCC"); |
| case SetEQ : |
| if (isFP) { |
| predicate = FCmpInst::FCMP_OEQ; |
| return Instruction::FCmp; |
| } else { |
| predicate = ICmpInst::ICMP_EQ; |
| return Instruction::ICmp; |
| } |
| case SetNE : |
| if (isFP) { |
| predicate = FCmpInst::FCMP_UNE; |
| return Instruction::FCmp; |
| } else { |
| predicate = ICmpInst::ICMP_NE; |
| return Instruction::ICmp; |
| } |
| case SetLE : |
| if (isFP) { |
| predicate = FCmpInst::FCMP_OLE; |
| return Instruction::FCmp; |
| } else { |
| if (isSigned) |
| predicate = ICmpInst::ICMP_SLE; |
| else |
| predicate = ICmpInst::ICMP_ULE; |
| return Instruction::ICmp; |
| } |
| case SetGE : |
| if (isFP) { |
| predicate = FCmpInst::FCMP_OGE; |
| return Instruction::FCmp; |
| } else { |
| if (isSigned) |
| predicate = ICmpInst::ICMP_SGE; |
| else |
| predicate = ICmpInst::ICMP_UGE; |
| return Instruction::ICmp; |
| } |
| case SetLT : |
| if (isFP) { |
| predicate = FCmpInst::FCMP_OLT; |
| return Instruction::FCmp; |
| } else { |
| if (isSigned) |
| predicate = ICmpInst::ICMP_SLT; |
| else |
| predicate = ICmpInst::ICMP_ULT; |
| return Instruction::ICmp; |
| } |
| case SetGT : |
| if (isFP) { |
| predicate = FCmpInst::FCMP_OGT; |
| return Instruction::FCmp; |
| } else { |
| if (isSigned) |
| predicate = ICmpInst::ICMP_SGT; |
| else |
| predicate = ICmpInst::ICMP_UGT; |
| return Instruction::ICmp; |
| } |
| } |
| } |
| |
| static inline Instruction::MemoryOps getMemoryOp(MemoryOps op) { |
| switch (op) { |
| default : assert(0 && "Invalid OldMemoryOps"); |
| case MallocOp : return Instruction::Malloc; |
| case FreeOp : return Instruction::Free; |
| case AllocaOp : return Instruction::Alloca; |
| case LoadOp : return Instruction::Load; |
| case StoreOp : return Instruction::Store; |
| case GetElementPtrOp : return Instruction::GetElementPtr; |
| } |
| } |
| |
| static inline Instruction::OtherOps |
| getOtherOp(OtherOps op, const Signedness &Sign) { |
| switch (op) { |
| default : assert(0 && "Invalid OldOtherOps"); |
| case PHIOp : return Instruction::PHI; |
| case CallOp : return Instruction::Call; |
| case SelectOp : return Instruction::Select; |
| case UserOp1 : return Instruction::UserOp1; |
| case UserOp2 : return Instruction::UserOp2; |
| case VAArg : return Instruction::VAArg; |
| case ExtractElementOp : return Instruction::ExtractElement; |
| case InsertElementOp : return Instruction::InsertElement; |
| case ShuffleVectorOp : return Instruction::ShuffleVector; |
| case ICmpOp : return Instruction::ICmp; |
| case FCmpOp : return Instruction::FCmp; |
| }; |
| } |
| |
| static inline Value* |
| getCast(CastOps op, Value *Src, const Signedness &SrcSign, const Type *DstTy, |
| const Signedness &DstSign, bool ForceInstruction = false) { |
| Instruction::CastOps Opcode; |
| const Type* SrcTy = Src->getType(); |
| if (op == CastOp) { |
| if (SrcTy->isFloatingPoint() && isa<PointerType>(DstTy)) { |
| // fp -> ptr cast is no longer supported but we must upgrade this |
| // by doing a double cast: fp -> int -> ptr |
| SrcTy = Type::Int64Ty; |
| Opcode = Instruction::IntToPtr; |
| if (isa<Constant>(Src)) { |
| Src = ConstantExpr::getCast(Instruction::FPToUI, |
| cast<Constant>(Src), SrcTy); |
| } else { |
| std::string NewName(makeNameUnique(Src->getName())); |
| Src = new FPToUIInst(Src, SrcTy, NewName, CurBB); |
| } |
| } else if (isa<IntegerType>(DstTy) && |
| cast<IntegerType>(DstTy)->getBitWidth() == 1) { |
| // cast type %x to bool was previously defined as setne type %x, null |
| // The cast semantic is now to truncate, not compare so we must retain |
| // the original intent by replacing the cast with a setne |
| Constant* Null = Constant::getNullValue(SrcTy); |
| Instruction::OtherOps Opcode = Instruction::ICmp; |
| unsigned short predicate = ICmpInst::ICMP_NE; |
| if (SrcTy->isFloatingPoint()) { |
| Opcode = Instruction::FCmp; |
| predicate = FCmpInst::FCMP_ONE; |
| } else if (!SrcTy->isInteger() && !isa<PointerType>(SrcTy)) { |
| error("Invalid cast to bool"); |
| } |
| if (isa<Constant>(Src) && !ForceInstruction) |
| return ConstantExpr::getCompare(predicate, cast<Constant>(Src), Null); |
| else |
| return CmpInst::create(Opcode, predicate, Src, Null); |
| } |
| // Determine the opcode to use by calling CastInst::getCastOpcode |
| Opcode = |
| CastInst::getCastOpcode(Src, SrcSign.isSigned(), DstTy, |
| DstSign.isSigned()); |
| |
| } else switch (op) { |
| default: assert(0 && "Invalid cast token"); |
| case TruncOp: Opcode = Instruction::Trunc; break; |
| case ZExtOp: Opcode = Instruction::ZExt; break; |
| case SExtOp: Opcode = Instruction::SExt; break; |
| case FPTruncOp: Opcode = Instruction::FPTrunc; break; |
| case FPExtOp: Opcode = Instruction::FPExt; break; |
| case FPToUIOp: Opcode = Instruction::FPToUI; break; |
| case FPToSIOp: Opcode = Instruction::FPToSI; break; |
| case UIToFPOp: Opcode = Instruction::UIToFP; break; |
| case SIToFPOp: Opcode = Instruction::SIToFP; break; |
| case PtrToIntOp: Opcode = Instruction::PtrToInt; break; |
| case IntToPtrOp: Opcode = Instruction::IntToPtr; break; |
| case BitCastOp: Opcode = Instruction::BitCast; break; |
| } |
| |
| if (isa<Constant>(Src) && !ForceInstruction) |
| return ConstantExpr::getCast(Opcode, cast<Constant>(Src), DstTy); |
| return CastInst::create(Opcode, Src, DstTy); |
| } |
| |
| static Instruction * |
| upgradeIntrinsicCall(const Type* RetTy, const ValID &ID, |
| std::vector<Value*>& Args) { |
| |
| std::string Name = ID.Type == ValID::NameVal ? ID.Name : ""; |
| if (Name.length() <= 5 || Name[0] != 'l' || Name[1] != 'l' || |
| Name[2] != 'v' || Name[3] != 'm' || Name[4] != '.') |
| return 0; |
| |
| switch (Name[5]) { |
| case 'i': |
| if (Name == "llvm.isunordered.f32" || Name == "llvm.isunordered.f64") { |
| if (Args.size() != 2) |
| error("Invalid prototype for " + Name); |
| return new FCmpInst(FCmpInst::FCMP_UNO, Args[0], Args[1]); |
| } |
| break; |
| case 'b': |
| if (Name.length() == 14 && !memcmp(&Name[5], "bswap.i", 7)) { |
| const Type* ArgTy = Args[0]->getType(); |
| Name += ".i" + utostr(cast<IntegerType>(ArgTy)->getBitWidth()); |
| Function *F = cast<Function>( |
| CurModule.CurrentModule->getOrInsertFunction(Name, RetTy, ArgTy, |
| (void*)0)); |
| return new CallInst(F, Args[0]); |
| } |
| break; |
| case 'c': |
| if ((Name.length() <= 14 && !memcmp(&Name[5], "ctpop.i", 7)) || |
| (Name.length() <= 13 && !memcmp(&Name[5], "ctlz.i", 6)) || |
| (Name.length() <= 13 && !memcmp(&Name[5], "cttz.i", 6))) { |
| // These intrinsics changed their result type. |
| const Type* ArgTy = Args[0]->getType(); |
| Function *OldF = CurModule.CurrentModule->getFunction(Name); |
| if (OldF) |
| OldF->setName("upgrd.rm." + Name); |
| |
| Function *NewF = cast<Function>( |
| CurModule.CurrentModule->getOrInsertFunction(Name, Type::Int32Ty, |
| ArgTy, (void*)0)); |
| |
| Instruction *Call = new CallInst(NewF, Args[0], "", CurBB); |
| return CastInst::createIntegerCast(Call, RetTy, false); |
| } |
| break; |
| |
| case 'v' : { |
| const Type* PtrTy = PointerType::get(Type::Int8Ty); |
| std::vector<const Type*> Params; |
| if (Name == "llvm.va_start" || Name == "llvm.va_end") { |
| if (Args.size() != 1) |
| error("Invalid prototype for " + Name + " prototype"); |
| Params.push_back(PtrTy); |
| const FunctionType *FTy = |
| FunctionType::get(Type::VoidTy, Params, false); |
| const PointerType *PFTy = PointerType::get(FTy); |
| Value* Func = getVal(PFTy, ID); |
| Args[0] = new BitCastInst(Args[0], PtrTy, makeNameUnique("va"), CurBB); |
| return new CallInst(Func, &Args[0], Args.size()); |
| } else if (Name == "llvm.va_copy") { |
| if (Args.size() != 2) |
| error("Invalid prototype for " + Name + " prototype"); |
| Params.push_back(PtrTy); |
| Params.push_back(PtrTy); |
| const FunctionType *FTy = |
| FunctionType::get(Type::VoidTy, Params, false); |
| const PointerType *PFTy = PointerType::get(FTy); |
| Value* Func = getVal(PFTy, ID); |
| std::string InstName0(makeNameUnique("va0")); |
| std::string InstName1(makeNameUnique("va1")); |
| Args[0] = new BitCastInst(Args[0], PtrTy, InstName0, CurBB); |
| Args[1] = new BitCastInst(Args[1], PtrTy, InstName1, CurBB); |
| return new CallInst(Func, &Args[0], Args.size()); |
| } |
| } |
| } |
| return 0; |
| } |
| |
| const Type* upgradeGEPCEIndices(const Type* PTy, |
| std::vector<ValueInfo> *Indices, |
| std::vector<Constant*> &Result) { |
| const Type *Ty = PTy; |
| Result.clear(); |
| for (unsigned i = 0, e = Indices->size(); i != e ; ++i) { |
| Constant *Index = cast<Constant>((*Indices)[i].V); |
| |
| if (ConstantInt *CI = dyn_cast<ConstantInt>(Index)) { |
| // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte |
| // struct indices to i32 struct indices with ZExt for compatibility. |
| if (CI->getBitWidth() < 32) |
| Index = ConstantExpr::getCast(Instruction::ZExt, CI, Type::Int32Ty); |
| } |
| |
| if (isa<SequentialType>(Ty)) { |
| // Make sure that unsigned SequentialType indices are zext'd to |
| // 64-bits if they were smaller than that because LLVM 2.0 will sext |
| // all indices for SequentialType elements. We must retain the same |
| // semantic (zext) for unsigned types. |
| if (const IntegerType *Ity = dyn_cast<IntegerType>(Index->getType())) { |
| if (Ity->getBitWidth() < 64 && (*Indices)[i].S.isUnsigned()) { |
| Index = ConstantExpr::getCast(Instruction::ZExt, Index,Type::Int64Ty); |
| } |
| } |
| } |
| Result.push_back(Index); |
| Ty = GetElementPtrInst::getIndexedType(PTy, (Value**)&Result[0], |
| Result.size(),true); |
| if (!Ty) |
| error("Index list invalid for constant getelementptr"); |
| } |
| return Ty; |
| } |
| |
| const Type* upgradeGEPInstIndices(const Type* PTy, |
| std::vector<ValueInfo> *Indices, |
| std::vector<Value*> &Result) { |
| const Type *Ty = PTy; |
| Result.clear(); |
| for (unsigned i = 0, e = Indices->size(); i != e ; ++i) { |
| Value *Index = (*Indices)[i].V; |
| |
| if (ConstantInt *CI = dyn_cast<ConstantInt>(Index)) { |
| // LLVM 1.2 and earlier used ubyte struct indices. Convert any ubyte |
| // struct indices to i32 struct indices with ZExt for compatibility. |
| if (CI->getBitWidth() < 32) |
| Index = ConstantExpr::getCast(Instruction::ZExt, CI, Type::Int32Ty); |
| } |
| |
| |
| if (isa<StructType>(Ty)) { // Only change struct indices |
| if (!isa<Constant>(Index)) { |
| error("Invalid non-constant structure index"); |
| return 0; |
| } |
| } else { |
| // Make sure that unsigned SequentialType indices are zext'd to |
| // 64-bits if they were smaller than that because LLVM 2.0 will sext |
| // all indices for SequentialType elements. We must retain the same |
| // semantic (zext) for unsigned types. |
| if (const IntegerType *Ity = dyn_cast<IntegerType>(Index->getType())) { |
| if (Ity->getBitWidth() < 64 && (*Indices)[i].S.isUnsigned()) { |
| if (isa<Constant>(Index)) |
| Index = ConstantExpr::getCast(Instruction::ZExt, |
| cast<Constant>(Index), Type::Int64Ty); |
| else |
| Index = CastInst::create(Instruction::ZExt, Index, Type::Int64Ty, |
| makeNameUnique("gep"), CurBB); |
| } |
| } |
| } |
| Result.push_back(Index); |
| Ty = GetElementPtrInst::getIndexedType(PTy, &Result[0], Result.size(),true); |
| if (!Ty) |
| error("Index list invalid for constant getelementptr"); |
| } |
| return Ty; |
| } |
| |
| unsigned upgradeCallingConv(unsigned CC) { |
| switch (CC) { |
| case OldCallingConv::C : return CallingConv::C; |
| case OldCallingConv::CSRet : return CallingConv::C; |
| case OldCallingConv::Fast : return CallingConv::Fast; |
| case OldCallingConv::Cold : return CallingConv::Cold; |
| case OldCallingConv::X86_StdCall : return CallingConv::X86_StdCall; |
| case OldCallingConv::X86_FastCall: return CallingConv::X86_FastCall; |
| default: |
| return CC; |
| } |
| } |
| |
| Module* UpgradeAssembly(const std::string &infile, std::istream& in, |
| bool debug, bool addAttrs) |
| { |
| Upgradelineno = 1; |
| CurFilename = infile; |
| LexInput = ∈ |
| yydebug = debug; |
| AddAttributes = addAttrs; |
| ObsoleteVarArgs = false; |
| NewVarArgs = false; |
| |
| CurModule.CurrentModule = new Module(CurFilename); |
| |
| // Check to make sure the parser succeeded |
| if (yyparse()) { |
| if (ParserResult) |
| delete ParserResult; |
| std::cerr << "llvm-upgrade: parse failed.\n"; |
| return 0; |
| } |
| |
| // Check to make sure that parsing produced a result |
| if (!ParserResult) { |
| std::cerr << "llvm-upgrade: no parse result.\n"; |
| return 0; |
| } |
| |
| // Reset ParserResult variable while saving its value for the result. |
| Module *Result = ParserResult; |
| ParserResult = 0; |
| |
| //Not all functions use vaarg, so make a second check for ObsoleteVarArgs |
| { |
| Function* F; |
| if ((F = Result->getFunction("llvm.va_start")) |
| && F->getFunctionType()->getNumParams() == 0) |
| ObsoleteVarArgs = true; |
| if((F = Result->getFunction("llvm.va_copy")) |
| && F->getFunctionType()->getNumParams() == 1) |
| ObsoleteVarArgs = true; |
| } |
| |
| if (ObsoleteVarArgs && NewVarArgs) { |
| error("This file is corrupt: it uses both new and old style varargs"); |
| return 0; |
| } |
| |
| if(ObsoleteVarArgs) { |
| if(Function* F = Result->getFunction("llvm.va_start")) { |
| if (F->arg_size() != 0) { |
| error("Obsolete va_start takes 0 argument"); |
| return 0; |
| } |
| |
| //foo = va_start() |
| // -> |
| //bar = alloca typeof(foo) |
| //va_start(bar) |
| //foo = load bar |
| |
| const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID); |
| const Type* ArgTy = F->getFunctionType()->getReturnType(); |
| const Type* ArgTyPtr = PointerType::get(ArgTy); |
| Function* NF = cast<Function>(Result->getOrInsertFunction( |
| "llvm.va_start", RetTy, ArgTyPtr, (Type *)0)); |
| |
| while (!F->use_empty()) { |
| CallInst* CI = cast<CallInst>(F->use_back()); |
| AllocaInst* bar = new AllocaInst(ArgTy, 0, "vastart.fix.1", CI); |
| new CallInst(NF, bar, "", CI); |
| Value* foo = new LoadInst(bar, "vastart.fix.2", CI); |
| CI->replaceAllUsesWith(foo); |
| CI->getParent()->getInstList().erase(CI); |
| } |
| Result->getFunctionList().erase(F); |
| } |
| |
| if(Function* F = Result->getFunction("llvm.va_end")) { |
| if(F->arg_size() != 1) { |
| error("Obsolete va_end takes 1 argument"); |
| return 0; |
| } |
| |
| //vaend foo |
| // -> |
| //bar = alloca 1 of typeof(foo) |
| //vaend bar |
| const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID); |
| const Type* ArgTy = F->getFunctionType()->getParamType(0); |
| const Type* ArgTyPtr = PointerType::get(ArgTy); |
| Function* NF = cast<Function>(Result->getOrInsertFunction( |
| "llvm.va_end", RetTy, ArgTyPtr, (Type *)0)); |
| |
| while (!F->use_empty()) { |
| CallInst* CI = cast<CallInst>(F->use_back()); |
| AllocaInst* bar = new AllocaInst(ArgTy, 0, "vaend.fix.1", CI); |
| new StoreInst(CI->getOperand(1), bar, CI); |
| new CallInst(NF, bar, "", CI); |
| CI->getParent()->getInstList().erase(CI); |
| } |
| Result->getFunctionList().erase(F); |
| } |
| |
| if(Function* F = Result->getFunction("llvm.va_copy")) { |
| if(F->arg_size() != 1) { |
| error("Obsolete va_copy takes 1 argument"); |
| return 0; |
| } |
| //foo = vacopy(bar) |
| // -> |
| //a = alloca 1 of typeof(foo) |
| //b = alloca 1 of typeof(foo) |
| //store bar -> b |
| //vacopy(a, b) |
| //foo = load a |
| |
| const Type* RetTy = Type::getPrimitiveType(Type::VoidTyID); |
| const Type* ArgTy = F->getFunctionType()->getReturnType(); |
| const Type* ArgTyPtr = PointerType::get(ArgTy); |
| Function* NF = cast<Function>(Result->getOrInsertFunction( |
| "llvm.va_copy", RetTy, ArgTyPtr, ArgTyPtr, (Type *)0)); |
| |
| while (!F->use_empty()) { |
| CallInst* CI = cast<CallInst>(F->use_back()); |
| AllocaInst* a = new AllocaInst(ArgTy, 0, "vacopy.fix.1", CI); |
| AllocaInst* b = new AllocaInst(ArgTy, 0, "vacopy.fix.2", CI); |
| new StoreInst(CI->getOperand(1), b, CI); |
| new CallInst(NF, a, b, "", CI); |
| Value* foo = new LoadInst(a, "vacopy.fix.3", CI); |
| CI->replaceAllUsesWith(foo); |
| CI->getParent()->getInstList().erase(CI); |
| } |
| Result->getFunctionList().erase(F); |
| } |
| } |
| |
| return Result; |
| } |
| |
| } // end llvm namespace |
| |
| using namespace llvm; |
| |
| |
| |
| /* Enabling traces. */ |
| #ifndef YYDEBUG |
| # define YYDEBUG 0 |
| #endif |
| |
| /* Enabling verbose error messages. */ |
| #ifdef YYERROR_VERBOSE |
| # undef YYERROR_VERBOSE |
| # define YYERROR_VERBOSE 1 |
| #else |
| # define YYERROR_VERBOSE 0 |
| #endif |
| |
| /* Enabling the token table. */ |
| #ifndef YYTOKEN_TABLE |
| # define YYTOKEN_TABLE 0 |
| #endif |
| |
| #if ! defined (YYSTYPE) && ! defined (YYSTYPE_IS_DECLARED) |
| #line 1775 "/proj/llvm/llvm-20/tools/llvm-upgrade/UpgradeParser.y" |
| typedef union YYSTYPE { |
| llvm::Module *ModuleVal; |
| llvm::Function *FunctionVal; |
| std::pair<llvm::PATypeInfo, char*> *ArgVal; |
| llvm::BasicBlock *BasicBlockVal; |
| llvm::TermInstInfo TermInstVal; |
| llvm::InstrInfo InstVal; |
| llvm::ConstInfo ConstVal; |
| llvm::ValueInfo ValueVal; |
| llvm::PATypeInfo TypeVal; |
| llvm::TypeInfo PrimType; |
| llvm::PHIListInfo PHIList; |
| std::list<llvm::PATypeInfo> *TypeList; |
| std::vector<llvm::ValueInfo> *ValueList; |
| std::vector<llvm::ConstInfo> *ConstVector; |
| |
| |
| std::vector<std::pair<llvm::PATypeInfo,char*> > *ArgList; |
| // Represent the RHS of PHI node |
| std::vector<std::pair<llvm::Constant*, llvm::BasicBlock*> > *JumpTable; |
| |
| llvm::GlobalValue::LinkageTypes Linkage; |
| int64_t SInt64Val; |
| uint64_t UInt64Val; |
| int SIntVal; |
| unsigned UIntVal; |
| double FPVal; |
| bool BoolVal; |
| |
| char *StrVal; // This memory is strdup'd! |
| llvm::ValID ValIDVal; // strdup'd memory maybe! |
| |
| llvm::BinaryOps BinaryOpVal; |
| llvm::TermOps TermOpVal; |
| llvm::MemoryOps MemOpVal; |
| llvm::OtherOps OtherOpVal; |
| llvm::CastOps CastOpVal; |
| llvm::ICmpInst::Predicate IPred; |
| llvm::FCmpInst::Predicate FPred; |
| llvm::Module::Endianness Endianness; |
| } YYSTYPE; |
| /* Line 196 of yacc.c. */ |
| #line 2198 "UpgradeParser.tab.c" |
| # define yystype YYSTYPE /* obsolescent; will be withdrawn */ |
| # define YYSTYPE_IS_DECLARED 1 |
| # define YYSTYPE_IS_TRIVIAL 1 |
| #endif |
| |
| |
| |
| /* Copy the second part of user declarations. */ |
| |
| |
| /* Line 219 of yacc.c. */ |
| #line 2210 "UpgradeParser.tab.c" |
| |
| #if ! defined (YYSIZE_T) && defined (__SIZE_TYPE__) |
| # define YYSIZE_T __SIZE_TYPE__ |
| #endif |
| #if ! defined (YYSIZE_T) && defined (size_t) |
| # define YYSIZE_T size_t |
| #endif |
| #if ! defined (YYSIZE_T) && (defined (__STDC__) || defined (__cplusplus)) |
| # include <stddef.h> /* INFRINGES ON USER NAME SPACE */ |
| # define YYSIZE_T size_t |
| #endif |
| #if ! defined (YYSIZE_T) |
| # define YYSIZE_T unsigned int |
| #endif |
| |
| #ifndef YY_ |
| # if YYENABLE_NLS |
| # if ENABLE_NLS |
| # include <libintl.h> /* INFRINGES ON USER NAME SPACE */ |
| # define YY_(msgid) dgettext ("bison-runtime", msgid) |
| # endif |
| # endif |
| # ifndef YY_ |
| # define YY_(msgid) msgid |
| # endif |
| #endif |
| |
| #if ! defined (yyoverflow) || YYERROR_VERBOSE |
| |
| /* The parser invokes alloca or malloc; define the necessary symbols. */ |
| |
| # ifdef YYSTACK_USE_ALLOCA |
| # if YYSTACK_USE_ALLOCA |
| # ifdef __GNUC__ |
| # define YYSTACK_ALLOC __builtin_alloca |
| # else |
| # define YYSTACK_ALLOC alloca |
| # if defined (__STDC__) || defined (__cplusplus) |
| # include <stdlib.h> /* INFRINGES ON USER NAME SPACE */ |
| # define YYINCLUDED_STDLIB_H |
| # endif |
| # endif |
| # endif |
| # endif |
| |
| # ifdef YYSTACK_ALLOC |
| /* Pacify GCC's `empty if-body' warning. */ |
| # define YYSTACK_FREE(Ptr) do { /* empty */; } while (0) |
| # ifndef YYSTACK_ALLOC_MAXIMUM |
| /* The OS might guarantee only one guard page at the bottom of the stack, |
| and a page size can be as small as 4096 bytes. So we cannot safely |
| invoke alloca (N) if N exceeds 4096. Use a slightly smaller number |
| to allow for a few compiler-allocated temporary stack slots. */ |
| # define YYSTACK_ALLOC_MAXIMUM 4032 /* reasonable circa 2005 */ |
| # endif |
| # else |
| # define YYSTACK_ALLOC YYMALLOC |
| # define YYSTACK_FREE YYFREE |
| # ifndef YYSTACK_ALLOC_MAXIMUM |
| # define YYSTACK_ALLOC_MAXIMUM ((YYSIZE_T) -1) |
| # endif |
| # ifdef __cplusplus |
| extern "C" { |
| # endif |
| # ifndef YYMALLOC |
| # define YYMALLOC malloc |
| # if (! defined (malloc) && ! defined (YYINCLUDED_STDLIB_H) \ |
| && (defined (__STDC__) || defined (__cplusplus))) |
| void *malloc (YYSIZE_T); /* INFRINGES ON USER NAME SPACE */ |
| # endif |
| # endif |
| # ifndef YYFREE |
| # define YYFREE free |
| # if (! defined (free) && ! defined (YYINCLUDED_STDLIB_H) \ |
| && (defined (__STDC__) || defined (__cplusplus))) |
| void free (void *); /* INFRINGES ON USER NAME SPACE */ |
| # endif |
| # endif |
| # ifdef __cplusplus |
| } |
| # endif |
| # endif |
| #endif /* ! defined (yyoverflow) || YYERROR_VERBOSE */ |
| |
| |
| #if (! defined (yyoverflow) \ |
| && (! defined (__cplusplus) \ |
| || (defined (YYSTYPE_IS_TRIVIAL) && YYSTYPE_IS_TRIVIAL))) |
| |
| /* A type that is properly aligned for any stack member. */ |
| union yyalloc |
| { |
| short int yyss; |
| YYSTYPE yyvs; |
| }; |
| |
| /* The size of the maximum gap between one aligned stack and the next. */ |
| # define YYSTACK_GAP_MAXIMUM (sizeof (union yyalloc) - 1) |
| |
| /* The size of an array large to enough to hold all stacks, each with |
| N elements. */ |
| # define YYSTACK_BYTES(N) \ |
| ((N) * (sizeof (short int) + sizeof (YYSTYPE)) \ |
| + YYSTACK_GAP_MAXIMUM) |
| |
| /* Copy COUNT objects from FROM to TO. The source and destination do |
| not overlap. */ |
| # ifndef YYCOPY |
| # if defined (__GNUC__) && 1 < __GNUC__ |
| # define YYCOPY(To, From, Count) \ |
| __builtin_memcpy (To, From, (Count) * sizeof (*(From))) |
| # else |
| # define YYCOPY(To, From, Count) \ |
| do \ |
| { \ |
| YYSIZE_T yyi; \ |
| for (yyi = 0; yyi < (Count); yyi++) \ |
| (To)[yyi] = (From)[yyi]; \ |
| } \ |
| while (0) |
| # endif |
| # endif |
| |
| /* Relocate STACK from its old location to the new one. The |
| local variables YYSIZE and YYSTACKSIZE give the old and new number of |
| elements in the stack, and YYPTR gives the new location of the |
| stack. Advance YYPTR to a properly aligned location for the next |
| stack. */ |
| # define YYSTACK_RELOCATE(Stack) \ |
| do \ |
| { \ |
| YYSIZE_T yynewbytes; \ |
| YYCOPY (&yyptr->Stack, Stack, yysize); \ |
| Stack = &yyptr->Stack; \ |
| yynewbytes = yystacksize * sizeof (*Stack) + YYSTACK_GAP_MAXIMUM; \ |
| yyptr += yynewbytes / sizeof (*yyptr); \ |
| } \ |
| while (0) |
| |
| #endif |
| |
| #if defined (__STDC__) || defined (__cplusplus) |
| typedef signed char yysigned_char; |
| #else |
| typedef short int yysigned_char; |
| #endif |
| |
| /* YYFINAL -- State number of the termination state. */ |
| #define YYFINAL 4 |
| /* YYLAST -- Last index in YYTABLE. */ |
| #define YYLAST 1630 |
| |
| /* YYNTOKENS -- Number of terminals. */ |
| #define YYNTOKENS 166 |
| /* YYNNTS -- Number of nonterminals. */ |
| #define YYNNTS 81 |
| /* YYNRULES -- Number of rules. */ |
| #define YYNRULES 310 |
| /* YYNRULES -- Number of states. */ |
| #define YYNSTATES 606 |
| |
| /* YYTRANSLATE(YYLEX) -- Bison symbol number corresponding to YYLEX. */ |
| #define YYUNDEFTOK 2 |
| #define YYMAXUTOK 406 |
| |
| #define YYTRANSLATE(YYX) \ |
| ((unsigned int) (YYX) <= YYMAXUTOK ? yytranslate[YYX] : YYUNDEFTOK) |
| |
| /* YYTRANSLATE[YYLEX] -- Bison symbol number corresponding to YYLEX. */ |
| static const unsigned char yytranslate[] = |
| { |
| 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 155, 156, 164, 2, 153, 2, 2, 2, 2, 2, |
| 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 160, 152, 161, 2, 2, 2, 2, 2, 2, 2, |
| 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 2, 157, 154, 159, 2, 2, 2, 2, 2, 165, |
| 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 158, 2, 2, 162, 2, 163, 2, 2, 2, 2, |
| 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, |
| 2, 2, 2, 2, 2, 2, 1, 2, 3, 4, |
| 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
| 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, |
| 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, |
| 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, |
| 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, |
| 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, |
| 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, |
| 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, |
| 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, |
| 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, |
| 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, |
| 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, |
| 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, |
| 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, |
| 145, 146, 147, 148, 149, 150, 151 |
| }; |
| |
| #if YYDEBUG |
| /* YYPRHS[YYN] -- Index of the first RHS symbol of rule number YYN in |
| YYRHS. */ |
| static const unsigned short int yyprhs[] = |
| { |
| 0, 0, 3, 5, 7, 9, 11, 13, 15, 17, |
| 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, |
| 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, |
| 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, |
| 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, |
| 99, 101, 103, 105, 107, 109, 111, 113, 115, 117, |
| 119, 121, 123, 125, 127, 129, 131, 133, 135, 137, |
| 139, 141, 143, 145, 147, 149, 151, 153, 155, 157, |
| 159, 161, 164, 165, 167, 169, 171, 173, 175, 177<
|