diff options
Diffstat (limited to 'tools/llvm-upgrade/UpgradeParser.cpp')
-rw-r--r-- | tools/llvm-upgrade/UpgradeParser.cpp | 4802 |
1 files changed, 0 insertions, 4802 deletions
diff --git a/tools/llvm-upgrade/UpgradeParser.cpp b/tools/llvm-upgrade/UpgradeParser.cpp deleted file mode 100644 index 50d98d9610..0000000000 --- a/tools/llvm-upgrade/UpgradeParser.cpp +++ /dev/null @@ -1,4802 +0,0 @@ -/* 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 { - VOID = 258, - BOOL = 259, - SBYTE = 260, - UBYTE = 261, - SHORT = 262, - USHORT = 263, - INT = 264, - UINT = 265, - LONG = 266, - ULONG = 267, - FLOAT = 268, - DOUBLE = 269, - LABEL = 270, - OPAQUE = 271, - ESINT64VAL = 272, - EUINT64VAL = 273, - SINTVAL = 274, - UINTVAL = 275, - FPVAL = 276, - NULL_TOK = 277, - UNDEF = 278, - ZEROINITIALIZER = 279, - TRUETOK = 280, - FALSETOK = 281, - TYPE = 282, - VAR_ID = 283, - LABELSTR = 284, - STRINGCONSTANT = 285, - IMPLEMENTATION = 286, - BEGINTOK = 287, - ENDTOK = 288, - DECLARE = 289, - GLOBAL = 290, - CONSTANT = 291, - SECTION = 292, - VOLATILE = 293, - TO = 294, - DOTDOTDOT = 295, - CONST = 296, - INTERNAL = 297, - LINKONCE = 298, - WEAK = 299, - DLLIMPORT = 300, - DLLEXPORT = 301, - EXTERN_WEAK = 302, - APPENDING = 303, - NOT = 304, - EXTERNAL = 305, - TARGET = 306, - TRIPLE = 307, - ENDIAN = 308, - POINTERSIZE = 309, - LITTLE = 310, - BIG = 311, - ALIGN = 312, - UNINITIALIZED = 313, - DEPLIBS = 314, - CALL = 315, - TAIL = 316, - ASM_TOK = 317, - MODULE = 318, - SIDEEFFECT = 319, - CC_TOK = 320, - CCC_TOK = 321, - CSRETCC_TOK = 322, - FASTCC_TOK = 323, - COLDCC_TOK = 324, - X86_STDCALLCC_TOK = 325, - X86_FASTCALLCC_TOK = 326, - DATALAYOUT = 327, - RET = 328, - BR = 329, - SWITCH = 330, - INVOKE = 331, - EXCEPT = 332, - UNWIND = 333, - UNREACHABLE = 334, - ADD = 335, - SUB = 336, - MUL = 337, - DIV = 338, - UDIV = 339, - SDIV = 340, - FDIV = 341, - REM = 342, - UREM = 343, - SREM = 344, - FREM = 345, - AND = 346, - OR = 347, - XOR = 348, - SETLE = 349, - SETGE = 350, - SETLT = 351, - SETGT = 352, - SETEQ = 353, - SETNE = 354, - ICMP = 355, - FCMP = 356, - EQ = 357, - NE = 358, - SLT = 359, - SGT = 360, - SLE = 361, - SGE = 362, - OEQ = 363, - ONE = 364, - OLT = 365, - OGT = 366, - OLE = 367, - OGE = 368, - ORD = 369, - UNO = 370, - UEQ = 371, - UNE = 372, - ULT = 373, - UGT = 374, - ULE = 375, - UGE = 376, - MALLOC = 377, - ALLOCA = 378, - FREE = 379, - LOAD = 380, - STORE = 381, - GETELEMENTPTR = 382, - PHI_TOK = 383, - SELECT = 384, - SHL = 385, - SHR = 386, - ASHR = 387, - LSHR = 388, - VAARG = 389, - EXTRACTELEMENT = 390, - INSERTELEMENT = 391, - SHUFFLEVECTOR = 392, - CAST = 393, - TRUNC = 394, - ZEXT = 395, - SEXT = 396, - FPTRUNC = 397, - FPEXT = 398, - FPTOUI = 399, - FPTOSI = 400, - UITOFP = 401, - SITOFP = 402, - PTRTOINT = 403, - INTTOPTR = 404, - BITCAST = 405 - }; -#endif -/* Tokens. */ -#define VOID 258 -#define BOOL 259 -#define SBYTE 260 -#define UBYTE 261 -#define SHORT 262 -#define USHORT 263 -#define INT 264 -#define UINT 265 -#define LONG 266 -#define ULONG 267 -#define FLOAT 268 -#define DOUBLE 269 -#define LABEL 270 -#define OPAQUE 271 -#define ESINT64VAL 272 -#define EUINT64VAL 273 -#define SINTVAL 274 -#define UINTVAL 275 -#define FPVAL 276 -#define NULL_TOK 277 -#define UNDEF 278 -#define ZEROINITIALIZER 279 -#define TRUETOK 280 -#define FALSETOK 281 -#define TYPE 282 -#define VAR_ID 283 -#define LABELSTR 284 -#define STRINGCONSTANT 285 -#define IMPLEMENTATION 286 -#define BEGINTOK 287 -#define ENDTOK 288 -#define DECLARE 289 -#define GLOBAL 290 -#define CONSTANT 291 -#define SECTION 292 -#define VOLATILE 293 -#define TO 294 -#define DOTDOTDOT 295 -#define CONST 296 -#define INTERNAL 297 -#define LINKONCE 298 -#define WEAK 299 -#define DLLIMPORT 300 -#define DLLEXPORT 301 -#define EXTERN_WEAK 302 -#define APPENDING 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 UNINITIALIZED 313 -#define DEPLIBS 314 -#define CALL 315 -#define TAIL 316 -#define ASM_TOK 317 -#define MODULE 318 -#define SIDEEFFECT 319 -#define CC_TOK 320 -#define CCC_TOK 321 -#define CSRETCC_TOK 322 -#define FASTCC_TOK 323 -#define COLDCC_TOK 324 -#define X86_STDCALLCC_TOK 325 -#define X86_FASTCALLCC_TOK 326 -#define DATALAYOUT 327 -#define RET 328 -#define BR 329 -#define SWITCH 330 -#define INVOKE 331 -#define EXCEPT 332 -#define UNWIND 333 -#define UNREACHABLE 334 -#define ADD 335 -#define SUB 336 -#define MUL 337 -#define DIV 338 -#define UDIV 339 -#define SDIV 340 -#define FDIV 341 -#define REM 342 -#define UREM 343 -#define SREM 344 -#define FREM 345 -#define AND 346 -#define OR 347 -#define XOR 348 -#define SETLE 349 -#define SETGE 350 -#define SETLT 351 -#define SETGT 352 -#define SETEQ 353 -#define SETNE 354 -#define ICMP 355 -#define FCMP 356 -#define EQ 357 -#define NE 358 -#define SLT 359 -#define SGT 360 -#define SLE 361 -#define SGE 362 -#define OEQ 363 -#define ONE 364 -#define OLT 365 -#define OGT 366 -#define OLE 367 -#define OGE 368 -#define ORD 369 -#define UNO 370 -#define UEQ 371 -#define UNE 372 -#define ULT 373 -#define UGT 374 -#define ULE 375 -#define UGE 376 -#define MALLOC 377 -#define ALLOCA 378 -#define FREE 379 -#define LOAD 380 -#define STORE 381 -#define GETELEMENTPTR 382 -#define PHI_TOK 383 -#define SELECT 384 -#define SHL 385 -#define SHR 386 -#define ASHR 387 -#define LSHR 388 -#define VAARG 389 -#define EXTRACTELEMENT 390 -#define INSERTELEMENT 391 -#define SHUFFLEVECTOR 392 -#define CAST 393 -#define TRUNC 394 -#define ZEXT 395 -#define SEXT 396 -#define FPTRUNC 397 -#define FPEXT 398 -#define FPTOUI 399 -#define FPTOSI 400 -#define UITOFP 401 -#define SITOFP 402 -#define PTRTOINT 403 -#define INTTOPTR 404 -#define BITCAST 405 - - - - -/* Copy the first part of user declarations. */ -#line 14 "/developer/zsth/llvm-gcc-dev/HEAD/llvm/llvm/tools/llvm-upgrade/UpgradeParser.y" - -#include "UpgradeInternals.h" -#include <algorithm> -#include <map> -#include <utility> -#include <iostream> - -#define YYERROR_VERBOSE 1 -#define YYINCLUDED_STDLIB_H -#define YYDEBUG 1 - -int yylex(); // declaration" of xxx warnings. -int yyparse(); -extern int yydebug; - -static std::string CurFilename; -static std::ostream *O = 0; -std::istream* LexInput = 0; -unsigned SizeOfPointer = 32; -static uint64_t unique = 1; - -// This bool controls whether attributes are ever added to function declarations -// definitions and calls. -static bool AddAttributes = false; - -// This bool is used to communicate between the InstVal and Inst rules about -// whether or not a cast should be deleted. When the flag is set, InstVal has -// determined that the cast is a candidate. However, it can only be deleted if -// the value being casted is the same value name as the instruction. The Inst -// rule makes that comparison if the flag is set and comments out the -// instruction if they match. -static bool deleteUselessCastFlag = false; -static std::string* deleteUselessCastName = 0; - -typedef std::vector<const TypeInfo*> TypeVector; -static TypeVector EnumeratedTypes; -typedef std::map<std::string,const TypeInfo*> TypeMap; -static TypeMap NamedTypes; -typedef std::map<const TypeInfo*,std::string> TypePlaneMap; -typedef std::map<std::string,TypePlaneMap> GlobalsTypeMap; -static GlobalsTypeMap Globals; - -static void warning(const std::string& msg); - -void destroy(ValueList* VL) { - while (!VL->empty()) { - ValueInfo& VI = VL->back(); - VI.destroy(); - VL->pop_back(); - } - delete VL; -} - -void UpgradeAssembly(const std::string &infile, std::istream& in, - std::ostream &out, bool debug, bool addAttrs) -{ - Upgradelineno = 1; - CurFilename = infile; - LexInput = ∈ - yydebug = debug; - AddAttributes = addAttrs; - O = &out; - - if (yyparse()) { - std::cerr << "Parse failed.\n"; - out << "llvm-upgrade parse failed.\n"; - exit(1); - } -} - -TypeInfo::TypeRegMap TypeInfo::registry; - -const TypeInfo* TypeInfo::get(const std::string &newType, Types oldType) { - TypeInfo* Ty = new TypeInfo(); - Ty->newTy = newType; - Ty->oldTy = oldType; - return add_new_type(Ty); -} - -const TypeInfo* TypeInfo::get(const std::string& newType, Types oldType, - const TypeInfo* eTy, const TypeInfo* rTy) { - TypeInfo* Ty= new TypeInfo(); - Ty->newTy = newType; - Ty->oldTy = oldType; - Ty->elemTy = const_cast<TypeInfo*>(eTy); - Ty->resultTy = const_cast<TypeInfo*>(rTy); - return add_new_type(Ty); -} - -const TypeInfo* TypeInfo::get(const std::string& newType, Types oldType, - const TypeInfo *eTy, uint64_t elems) { - TypeInfo* Ty = new TypeInfo(); - Ty->newTy = newType; - Ty->oldTy = oldType; - Ty->elemTy = const_cast<TypeInfo*>(eTy); - Ty->nelems = elems; - return add_new_type(Ty); -} - -const TypeInfo* TypeInfo::get(const std::string& newType, Types oldType, - TypeList* TL) { - TypeInfo* Ty = new TypeInfo(); - Ty->newTy = newType; - Ty->oldTy = oldType; - Ty->elements = TL; - return add_new_type(Ty); -} - -const TypeInfo* TypeInfo::get(const std::string& newType, const TypeInfo* resTy, - TypeList* TL) { - TypeInfo* Ty = new TypeInfo(); - Ty->newTy = newType; - Ty->oldTy = FunctionTy; - Ty->resultTy = const_cast<TypeInfo*>(resTy); - Ty->elements = TL; - return add_new_type(Ty); -} - -const TypeInfo* TypeInfo::resolve() const { - if (isUnresolved()) { - if (getNewTy()[0] == '%' && isdigit(newTy[1])) { - unsigned ref = atoi(&((newTy.c_str())[1])); // skip the % - if (ref < EnumeratedTypes.size()) { - return EnumeratedTypes[ref]; - } else { - std::string msg("Can't resolve numbered type: "); - msg += getNewTy(); - yyerror(msg.c_str()); - } - } else { - TypeMap::iterator I = NamedTypes.find(newTy); - if (I != NamedTypes.end()) { - return I->second; - } else { - std::string msg("Cannot resolve type: "); - msg += getNewTy(); - yyerror(msg.c_str()); - } - } - } - // otherwise its already resolved. - return this; -} - -bool TypeInfo::operator<(const TypeInfo& that) const { - if (this == &that) - return false; - if (oldTy != that.oldTy) - return oldTy < that.oldTy; - switch (oldTy) { - case UpRefTy: { - unsigned thisUp = this->getUpRefNum(); - unsigned thatUp = that.getUpRefNum(); - return thisUp < thatUp; - } - case PackedTy: - case ArrayTy: - if (this->nelems != that.nelems) - return nelems < that.nelems; - case PointerTy: { - const TypeInfo* thisTy = this->elemTy; - const TypeInfo* thatTy = that.elemTy; - return *thisTy < *thatTy; - } - case FunctionTy: { - const TypeInfo* thisTy = this->resultTy; - const TypeInfo* thatTy = that.resultTy; - if (!thisTy->sameOldTyAs(thatTy)) - return *thisTy < *thatTy; - /* FALL THROUGH */ - } - case StructTy: - case PackedStructTy: { - if (elements->size() != that.elements->size()) - return elements->size() < that.elements->size(); - for (unsigned i = 0; i < elements->size(); i++) { - const TypeInfo* thisTy = (*this->elements)[i]; - const TypeInfo* thatTy = (*that.elements)[i]; - if (!thisTy->sameOldTyAs(thatTy)) - return *thisTy < *thatTy; - } - break; - } - case UnresolvedTy: - return this->newTy < that.newTy; - default: - break; - } - return false; -} - -bool TypeInfo::sameOldTyAs(const TypeInfo* that) const { - if (that == 0) - return false; - if ( this == that ) - return true; - if (oldTy != that->oldTy) - return false; - switch (oldTy) { - case PackedTy: - case ArrayTy: - if (nelems != that->nelems) - return false; - /* FALL THROUGH */ - case PointerTy: { - const TypeInfo* thisTy = this->elemTy; - const TypeInfo* thatTy = that->elemTy; - return thisTy->sameOldTyAs(thatTy); - } - case FunctionTy: { - const TypeInfo* thisTy = this->resultTy; - const TypeInfo* thatTy = that->resultTy; - if (!thisTy->sameOldTyAs(thatTy)) - return false; - /* FALL THROUGH */ - } - case StructTy: - case PackedStructTy: { - if (elements->size() != that->elements->size()) - return false; - for (unsigned i = 0; i < elements->size(); i++) { - const TypeInfo* thisTy = (*this->elements)[i]; - const TypeInfo* thatTy = (*that->elements)[i]; - if (!thisTy->sameOldTyAs(thatTy)) - return false; - } - return true; - } - case UnresolvedTy: - return this->newTy == that->newTy; - default: - return true; // for all others oldTy == that->oldTy is sufficient - } - return true; -} - -bool TypeInfo::isUnresolvedDeep() const { - switch (oldTy) { - case UnresolvedTy: - return true; - case PackedTy: - case ArrayTy: - case PointerTy: - return elemTy->isUnresolvedDeep(); - case PackedStructTy: - case StructTy: - for (unsigned i = 0; i < elements->size(); i++) - if ((*elements)[i]->isUnresolvedDeep()) - return true; - return false; - default: - return false; - } -} - -unsigned TypeInfo::getBitWidth() const { - switch (oldTy) { - default: - case LabelTy: - case VoidTy : return 0; - case BoolTy : return 1; - case SByteTy: case UByteTy : return 8; - case ShortTy: case UShortTy : return 16; - case IntTy: case UIntTy: case FloatTy: return 32; - case LongTy: case ULongTy: case DoubleTy : return 64; - case PointerTy: return SizeOfPointer; // global var - case PackedTy: - case ArrayTy: - return nelems * elemTy->getBitWidth(); - case StructTy: - case PackedStructTy: { - uint64_t size = 0; - for (unsigned i = 0; i < elements->size(); i++) { - size += (*elements)[i]->getBitWidth(); - } - return size; - } - } -} - -const TypeInfo* TypeInfo::getIndexedType(const ValueInfo& VI) const { - if (isStruct()) { - if (VI.isConstant() && VI.type->isInteger()) { - size_t pos = VI.val->find(' ') + 1; - if (pos < VI.val->size()) { - uint64_t idx = atoi(VI.val->substr(pos).c_str()); - return (*elements)[idx]; - } else { - yyerror("Invalid value for constant integer"); - return 0; - } - } else { - yyerror("Structure requires constant index"); - return 0; - } - } - if (isArray() || isPacked() || isPointer()) - return elemTy; - yyerror("Invalid type for getIndexedType"); - return 0; -} - -void TypeInfo::getSignedness(unsigned &sNum, unsigned &uNum, - UpRefStack& stack) const { - switch (oldTy) { - default: - case OpaqueTy: case LabelTy: case VoidTy: case BoolTy: - case FloatTy : case DoubleTy: case UpRefTy: - return; - case SByteTy: case ShortTy: case LongTy: case IntTy: - sNum++; - return; - case UByteTy: case UShortTy: case UIntTy: case ULongTy: - uNum++; - return; - case PointerTy: - case PackedTy: - case ArrayTy: - stack.push_back(this); - elemTy->getSignedness(sNum, uNum, stack); - return; - case StructTy: - case PackedStructTy: { - stack.push_back(this); - for (unsigned i = 0; i < elements->size(); i++) { - (*elements)[i]->getSignedness(sNum, uNum, stack); - } - return; - } - case UnresolvedTy: { - const TypeInfo* Ty = this->resolve(); - // Let's not recurse. - UpRefStack::const_iterator I = stack.begin(), E = stack.end(); - for ( ; I != E && *I != Ty; ++I) - ; - if (I == E) - Ty->getSignedness(sNum, uNum, stack); - return; - } - } -} - -std::string AddSuffix(const std::string& Name, const std::string& Suffix) { - if (Name[Name.size()-1] == '"') { - std::string Result = Name; - Result.insert(Result.size()-1, Suffix); - return Result; - } - return Name + Suffix; -} - -std::string TypeInfo::makeUniqueName(const std::string& BaseName) const { - if (BaseName == "\"alloca point\"") - return BaseName; - switch (oldTy) { - default: - break; - case OpaqueTy: case LabelTy: case VoidTy: case BoolTy: case UpRefTy: - case FloatTy : case DoubleTy: case UnresolvedTy: - return BaseName; - case SByteTy: case ShortTy: case LongTy: case IntTy: - return AddSuffix(BaseName, ".s"); - case UByteTy: case UShortTy: case UIntTy: case ULongTy: - return AddSuffix(BaseName, ".u"); - } - - unsigned uNum = 0, sNum = 0; - std::string Suffix; - switch (oldTy) { - case PointerTy: - case PackedTy: - case ArrayTy: { - TypeInfo::UpRefStack stack; - elemTy->resolve()->getSignedness(sNum, uNum, stack); - break; - } - case StructTy: - case PackedStructTy: { - for (unsigned i = 0; i < elements->size(); i++) { - TypeInfo::UpRefStack stack; - (*elements)[i]->resolve()->getSignedness(sNum, uNum, stack); - } - break; - } - default: - assert(0 && "Invalid Type"); - break; - } - - if (sNum == 0 && uNum == 0) - return BaseName; - - switch (oldTy) { - default: Suffix += ".nada"; break; - case PointerTy: Suffix += ".pntr"; break; - case PackedTy: Suffix += ".pckd"; break; - case ArrayTy: Suffix += ".arry"; break; - case StructTy: Suffix += ".strc"; break; - case PackedStructTy: Suffix += ".pstr"; break; - } - - Suffix += ".s" + llvm::utostr(sNum); - Suffix += ".u" + llvm::utostr(uNum); - return AddSuffix(BaseName, Suffix); -} - -TypeInfo& TypeInfo::operator=(const TypeInfo& that) { - oldTy = that.oldTy; - nelems = that.nelems; - newTy = that.newTy; - elemTy = that.elemTy; - resultTy = that.resultTy; - if (that.elements) { - elements = new TypeList(that.elements->size()); - *elements = *that.elements; - } else { - elements = 0; - } - return *this; -} - -const TypeInfo* TypeInfo::add_new_type(TypeInfo* newTy) { - TypeRegMap::iterator I = registry.find(newTy); - if (I != registry.end()) { - delete newTy; - return *I; - } - registry.insert(newTy); - return newTy; -} - -static const char* getCastOpcode( - std::string& Source, const TypeInfo* SrcTy, const TypeInfo* DstTy) -{ - unsigned SrcBits = SrcTy->getBitWidth(); - unsigned DstBits = DstTy->getBitWidth(); - const char* opcode = "bitcast"; - // Run through the possibilities ... - if (DstTy->isIntegral()) { // Casting to integral - if (SrcTy->isIntegral()) { // Casting from integral - if (DstBits < SrcBits) - opcode = "trunc"; - else if (DstBits > SrcBits) { // its an extension - if (SrcTy->isSigned()) - opcode ="sext"; // signed -> SEXT - else - opcode = "zext"; // unsigned -> ZEXT - } else { - opcode = "bitcast"; // Same size, No-op cast - } - } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt - if (DstTy->isSigned()) - opcode = "fptosi"; // FP -> sint - else - opcode = "fptoui"; // FP -> uint - } else if (SrcTy->isPacked()) { - assert(DstBits == SrcTy->getBitWidth() && - "Casting packed to integer of different width"); - opcode = "bitcast"; // same size, no-op cast - } else { - assert(SrcTy->isPointer() && - "Casting from a value that is not first-class type"); - opcode = "ptrtoint"; // ptr -> int - } - } else if (DstTy->isFloatingPoint()) { // Casting to floating pt - if (SrcTy->isIntegral()) { // Casting from integral - if (SrcTy->isSigned()) - opcode = "sitofp"; // sint -> FP - else - opcode = "uitofp"; // uint -> FP - } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt - if (DstBits < SrcBits) { - opcode = "fptrunc"; // FP -> smaller FP - } else if (DstBits > SrcBits) { - opcode = "fpext"; // FP -> larger FP - } else { - opcode ="bitcast"; // same size, no-op cast - } - } else if (SrcTy->isPacked()) { - assert(DstBits == SrcTy->getBitWidth() && - "Casting packed to floating point of different width"); - opcode = "bitcast"; // same size, no-op cast - } else { - assert(0 && "Casting pointer or non-first class to float"); - } - } else if (DstTy->isPacked()) { - if (SrcTy->isPacked()) { - assert(DstTy->getBitWidth() == SrcTy->getBitWidth() && - "Casting packed to packed of different widths"); - opcode = "bitcast"; // packed -> packed - } else if (DstTy->getBitWidth() == SrcBits) { - opcode = "bitcast"; // float/int -> packed - } else { - assert(!"Illegal cast to packed (wrong type or size)"); - } - } else if (DstTy->isPointer()) { - if (SrcTy->isPointer()) { - opcode = "bitcast"; // ptr -> ptr - } else if (SrcTy->isIntegral()) { - opcode = "inttoptr"; // int -> ptr - } else { - assert(!"Casting invalid type to pointer"); - } - } else { - assert(!"Casting to type that is not first-class"); - } - return opcode; -} - -static std::string getCastUpgrade(const std::string& Src, const TypeInfo* SrcTy, - const TypeInfo* DstTy, bool isConst) -{ - std::string Result; - std::string Source = Src; - if (SrcTy->isFloatingPoint() && DstTy->isPointer()) { - // fp -> ptr cast is no longer supported but we must upgrade this - // by doing a double cast: fp -> int -> ptr - if (isConst) - Source = "i64 fptoui(" + Source + " to i64)"; - else { - *O << " %cast_upgrade" << unique << " = fptoui " << Source - << " to i64\n"; - Source = "i64 %cast_upgrade" + llvm::utostr(unique); - } - // Update the SrcTy for the getCastOpcode call below - SrcTy = TypeInfo::get("i64", ULongTy); - } else if (DstTy->isBool()) { - // 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 - const char* comparator = SrcTy->isPointer() ? ", null" : - (SrcTy->isFloatingPoint() ? ", 0.0" : - (SrcTy->isBool() ? ", false" : ", 0")); - const char* compareOp = SrcTy->isFloatingPoint() ? "fcmp one " : "icmp ne "; - if (isConst) { - Result = "(" + Source + comparator + ")"; - Result = compareOp + Result; - } else - Result = compareOp + Source + comparator; - return Result; // skip cast processing below - } - SrcTy = SrcTy->resolve(); - DstTy = DstTy->resolve(); - std::string Opcode(getCastOpcode(Source, SrcTy, DstTy)); - if (isConst) - Result += Opcode + "( " + Source + " to " + DstTy->getNewTy() + ")"; - else - Result += Opcode + " " + Source + " to " + DstTy->getNewTy(); - return Result; -} - -const char* getDivRemOpcode(const std::string& opcode, const TypeInfo* TI) { - const char* op = opcode.c_str(); - const TypeInfo* Ty = TI->resolve(); - if (Ty->isPacked()) - Ty = Ty->getElementType(); - if (opcode == "div") - if (Ty->isFloatingPoint()) - op = "fdiv"; - else if (Ty->isUnsigned()) - op = "udiv"; - else if (Ty->isSigned()) - op = "sdiv"; - else - yyerror("Invalid type for div instruction"); - else if (opcode == "rem") - if (Ty->isFloatingPoint()) - op = "frem"; - else if (Ty->isUnsigned()) - op = "urem"; - else if (Ty->isSigned()) - op = "srem"; - else - yyerror("Invalid type for rem instruction"); - return op; -} - -std::string -getCompareOp(const std::string& setcc, const TypeInfo* TI) { - assert(setcc.length() == 5); - char cc1 = setcc[3]; - char cc2 = setcc[4]; - assert(cc1 == 'e' || cc1 == 'n' || cc1 == 'l' || cc1 == 'g'); - assert(cc2 == 'q' || cc2 == 'e' || cc2 == 'e' || cc2 == 't'); - std::string result("xcmp xxx"); - result[6] = cc1; - result[7] = cc2; - if (TI->isFloatingPoint()) { - result[0] = 'f'; - result[5] = 'o'; - if (cc1 == 'n') - result[5] = 'u'; // NE maps to unordered - else - result[5] = 'o'; // everything else maps to ordered - } else if (TI->isIntegral() || TI->isPointer()) { - result[0] = 'i'; - if ((cc1 == 'e' && cc2 == 'q') || (cc1 == 'n' && cc2 == 'e')) - result.erase(5,1); - else if (TI->isSigned()) - result[5] = 's'; - else if (TI->isUnsigned() || TI->isPointer() || TI->isBool()) - result[5] = 'u'; - else - yyerror("Invalid integral type for setcc"); - } - return result; -} - -static const TypeInfo* getFunctionReturnType(const TypeInfo* PFTy) { - PFTy = PFTy->resolve(); - if (PFTy->isPointer()) { - const TypeInfo* ElemTy = PFTy->getElementType(); - ElemTy = ElemTy->resolve(); - if (ElemTy->isFunction()) - return ElemTy->getResultType(); - } else if (PFTy->isFunction()) { - return PFTy->getResultType(); - } - return PFTy; -} - -static const TypeInfo* ResolveUpReference(const TypeInfo* Ty, - TypeInfo::UpRefStack* stack) { - assert(Ty->isUpReference() && "Can't resolve a non-upreference"); - unsigned upref = Ty->getUpRefNum(); - assert(upref < stack->size() && "Invalid up reference"); - return (*stack)[upref - stack->size() - 1]; -} - -static const TypeInfo* getGEPIndexedType(const TypeInfo* PTy, ValueList* idxs) { - const TypeInfo* Result = PTy = PTy->resolve(); - assert(PTy->isPointer() && "GEP Operand is not a pointer?"); - TypeInfo::UpRefStack stack; - for (unsigned i = 0; i < idxs->size(); ++i) { - if (Result->isComposite()) { - Result = Result->getIndexedType((*idxs)[i]); - Result = Result->resolve(); - stack.push_back(Result); - } else - yyerror("Invalid type for index"); - } - // Resolve upreferences so we can return a more natural type - if (Result->isPointer()) { - if (Result->getElementType()->isUpReference()) { - stack.push_back(Result); - Result = ResolveUpReference(Result->getElementType(), &stack); - } - } else if (Result->isUpReference()) { - Result = ResolveUpReference(Result->getElementType(), &stack); - } - return Result->getPointerType(); -} - - -// This function handles appending .u or .s to integer value names that -// were previously unsigned or signed, respectively. This avoids name -// collisions since the unsigned and signed type planes have collapsed -// into a single signless type plane. -static std::string getUniqueName(const std::string *Name, const TypeInfo* Ty, - bool isGlobal = false, bool isDef = false) { - - // If its not a symbolic name, don't modify it, probably a constant val. - if ((*Name)[0] != '%' && (*Name)[0] != '"') - return *Name; - - // If its a numeric reference, just leave it alone. - if (isdigit((*Name)[1])) - return *Name; - - // Resolve the type - Ty = Ty->resolve(); - - // If its a global name, get its uniquified name, if any - GlobalsTypeMap::iterator GI = Globals.find(*Name); - if (GI != Globals.end()) { - TypePlaneMap::iterator TPI = GI->second.begin(); - TypePlaneMap::iterator TPE = GI->second.end(); - for ( ; TPI != TPE ; ++TPI) { - if (TPI->first->sameNewTyAs(Ty)) - return TPI->second; - } - } - - if (isGlobal) { - // We didn't find a global name, but if its supposed to be global then all - // we can do is return the name. This is probably a forward reference of a - // global value that hasn't been defined yet. Since we have no definition - // we don't know its linkage class. Just assume its an external and the name - // shouldn't change. - return *Name; - } - - // Default the result to the current name - std::string Result = Ty->makeUniqueName(*Name); - - return Result; -} - -static unsigned UniqueNameCounter = 0; - -std::string getGlobalName(const std::string* Name, const std::string Linkage, - const TypeInfo* Ty, bool isConstant) { - // Default to given name - std::string Result = *Name; - // Look up the name in the Globals Map - GlobalsTypeMap::iterator GI = Globals.find(*Name); - // Did we see this global name before? - if (GI != Globals.end()) { - if (Ty->isUnresolvedDeep()) { - // The Gval's type is unresolved. Consequently, we can't disambiguate it - // by type. We'll just change its name and emit a warning. - warning("Cannot disambiguate global value '" + *Name + - "' because type '" + Ty->getNewTy() + "'is unresolved.\n"); - Result = *Name + ".unique"; - UniqueNameCounter++; - Result += llvm::utostr(UniqueNameCounter); - return Result; - } else { - TypePlaneMap::iterator TPI = GI->second.find(Ty); - if (TPI != GI->second.end()) { - // We found an existing name of the same old type. This isn't allowed - // in LLVM 2.0. Consequently, we must alter the name of the global so it - // can at least compile. References to the global will yield the first - // definition, which is okay. We also must warn about this. - Result = *Name + ".unique"; - UniqueNameCounter++; - Result += llvm::utostr(UniqueNameCounter); - warning(std::string("Global variable '") + *Name + "' was renamed to '"+ - Result + "'"); - } else { - // There isn't an existing definition for this name according to the - // old types. Now search the TypePlanMap for types with the same new - // name. - TypePlaneMap::iterator TPI = GI->second.begin(); - TypePlaneMap::iterator TPE = GI->second.end(); - for ( ; TPI != TPE; ++TPI) { - if (TPI->first->sameNewTyAs(Ty)) |