Reid Spencer | 90eb4d6 | 2007-01-05 17:18:58 +0000 | [diff] [blame^] | 1 | //===-- UpgradeInternals.h - Internal parser definitionsr -------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Reid Spencer and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This header file defines the variables that are shared between the lexer, |
| 11 | // the parser, and the main program. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef UPGRADE_INTERNALS_H |
| 16 | #define UPGRADE_INTERNALS_H |
| 17 | |
| 18 | #include <llvm/ADT/StringExtras.h> |
| 19 | #include <string> |
| 20 | #include <istream> |
| 21 | #include <vector> |
| 22 | #include <set> |
| 23 | #include <cassert> |
| 24 | |
| 25 | // Global variables exported from the lexer... |
| 26 | |
| 27 | extern std::string CurFileName; |
| 28 | extern std::string Textin; |
| 29 | extern int Upgradelineno; |
| 30 | extern std::istream* LexInput; |
| 31 | |
| 32 | struct TypeInfo; |
| 33 | typedef std::vector<const TypeInfo*> TypeList; |
| 34 | |
| 35 | void UpgradeAssembly( |
| 36 | const std::string & infile, std::istream& in, std::ostream &out, bool debug, |
| 37 | bool addAttrs); |
| 38 | |
| 39 | // Globals exported by the parser... |
| 40 | extern char* Upgradetext; |
| 41 | extern int Upgradeleng; |
| 42 | extern unsigned SizeOfPointer; |
| 43 | |
| 44 | int yyerror(const char *ErrorMsg) ; |
| 45 | |
| 46 | /// This enum is used to keep track of the original (1.9) type used to form |
| 47 | /// a type. These are needed for type upgrades and to determine how to upgrade |
| 48 | /// signed instructions with signless operands. |
| 49 | enum Types { |
| 50 | BoolTy, SByteTy, UByteTy, ShortTy, UShortTy, IntTy, UIntTy, LongTy, ULongTy, |
| 51 | FloatTy, DoubleTy, PointerTy, PackedTy, ArrayTy, StructTy, PackedStructTy, |
| 52 | OpaqueTy, VoidTy, LabelTy, FunctionTy, UnresolvedTy, UpRefTy |
| 53 | }; |
| 54 | |
| 55 | /// This type is used to keep track of the signedness of values. Instead |
| 56 | /// of creating llvm::Value directly, the parser will create ValueInfo which |
| 57 | /// associates a Value* with a Signedness indication. |
| 58 | struct ValueInfo { |
| 59 | std::string* val; |
| 60 | const TypeInfo* type; |
| 61 | bool constant; |
| 62 | bool isConstant() const { return constant; } |
| 63 | inline void destroy(); |
| 64 | }; |
| 65 | |
| 66 | /// This type is used to keep track of the signedness of the obsolete |
| 67 | /// integer types. Instead of creating an llvm::Type directly, the Lexer will |
| 68 | /// create instances of TypeInfo which retains the signedness indication so |
| 69 | /// it can be used by the parser for upgrade decisions. |
| 70 | /// For example if "uint" is encountered then the "first" field will be set |
| 71 | /// to "int32" and the "second" field will be set to "isUnsigned". If the |
| 72 | /// type is not obsolete then "second" will be set to "isSignless". |
| 73 | struct TypeInfo { |
| 74 | |
| 75 | static const TypeInfo* get(const std::string &newType, Types oldType); |
| 76 | static const TypeInfo* get(const std::string& newType, Types oldType, |
| 77 | const TypeInfo* eTy, const TypeInfo* rTy); |
| 78 | |
| 79 | static const TypeInfo* get(const std::string& newType, Types oldType, |
| 80 | const TypeInfo *eTy, uint64_t elems); |
| 81 | |
| 82 | static const TypeInfo* get(const std::string& newType, Types oldType, |
| 83 | TypeList* TL); |
| 84 | |
| 85 | static const TypeInfo* get(const std::string& newType, const TypeInfo* resTy, |
| 86 | TypeList* TL); |
| 87 | |
| 88 | const TypeInfo* resolve() const; |
| 89 | bool operator<(const TypeInfo& that) const; |
| 90 | |
| 91 | bool sameNewTyAs(const TypeInfo* that) const { |
| 92 | return this->newTy == that->newTy; |
| 93 | } |
| 94 | |
| 95 | bool sameOldTyAs(const TypeInfo* that) const; |
| 96 | |
| 97 | Types getElementTy() const { |
| 98 | if (elemTy) { |
| 99 | return elemTy->oldTy; |
| 100 | } |
| 101 | return UnresolvedTy; |
| 102 | } |
| 103 | |
| 104 | unsigned getUpRefNum() const { |
| 105 | assert(oldTy == UpRefTy && "Can't getUpRefNum on non upreference"); |
| 106 | return atoi(&((getNewTy().c_str())[1])); // skip the slash |
| 107 | } |
| 108 | |
| 109 | const std::string& getNewTy() const { return newTy; } |
| 110 | const TypeInfo* getResultType() const { return resultTy; } |
| 111 | const TypeInfo* getElementType() const { return elemTy; } |
| 112 | |
| 113 | const TypeInfo* getPointerType() const { |
| 114 | return get(newTy + "*", PointerTy, this, (TypeInfo*)0); |
| 115 | } |
| 116 | |
| 117 | bool isUnresolved() const { return oldTy == UnresolvedTy; } |
| 118 | bool isUpReference() const { return oldTy == UpRefTy; } |
| 119 | bool isVoid() const { return oldTy == VoidTy; } |
| 120 | bool isBool() const { return oldTy == BoolTy; } |
| 121 | bool isSigned() const { |
| 122 | return oldTy == SByteTy || oldTy == ShortTy || |
| 123 | oldTy == IntTy || oldTy == LongTy; |
| 124 | } |
| 125 | |
| 126 | bool isUnsigned() const { |
| 127 | return oldTy == UByteTy || oldTy == UShortTy || |
| 128 | oldTy == UIntTy || oldTy == ULongTy; |
| 129 | } |
| 130 | bool isSignless() const { return !isSigned() && !isUnsigned(); } |
| 131 | bool isInteger() const { return isSigned() || isUnsigned(); } |
| 132 | bool isIntegral() const { return oldTy == BoolTy || isInteger(); } |
| 133 | bool isFloatingPoint() const { return oldTy == DoubleTy || oldTy == FloatTy; } |
| 134 | bool isPacked() const { return oldTy == PackedTy; } |
| 135 | bool isPointer() const { return oldTy == PointerTy; } |
| 136 | bool isStruct() const { return oldTy == StructTy || oldTy == PackedStructTy; } |
| 137 | bool isArray() const { return oldTy == ArrayTy; } |
| 138 | bool isOther() const { |
| 139 | return !isPacked() && !isPointer() && !isFloatingPoint() && !isIntegral(); } |
| 140 | bool isFunction() const { return oldTy == FunctionTy; } |
| 141 | bool isComposite() const { |
| 142 | return isStruct() || isPointer() || isArray() || isPacked(); |
| 143 | } |
| 144 | |
| 145 | bool isAttributeCandidate() const { |
| 146 | return isIntegral() && getBitWidth() < 32; |
| 147 | } |
| 148 | |
| 149 | bool isUnresolvedDeep() const; |
| 150 | |
| 151 | unsigned getBitWidth() const; |
| 152 | |
| 153 | const TypeInfo* getIndexedType(const ValueInfo& VI) const; |
| 154 | |
| 155 | unsigned getNumStructElements() const { |
| 156 | return (elements ? elements->size() : 0); |
| 157 | } |
| 158 | |
| 159 | const TypeInfo* getElement(unsigned idx) const { |
| 160 | if (elements) |
| 161 | if (idx < elements->size()) |
| 162 | return (*elements)[idx]; |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | |
| 167 | private: |
| 168 | TypeInfo() |
| 169 | : newTy(), oldTy(UnresolvedTy), elemTy(0), resultTy(0), elements(0), |
| 170 | nelems(0) { |
| 171 | } |
| 172 | |
| 173 | TypeInfo(const TypeInfo& that); // do not implement |
| 174 | TypeInfo& operator=(const TypeInfo& that); // do not implement |
| 175 | |
| 176 | ~TypeInfo() { delete elements; } |
| 177 | |
| 178 | struct ltfunctor |
| 179 | { |
| 180 | bool operator()(const TypeInfo* X, const TypeInfo* Y) const { |
| 181 | assert(X && "Can't compare null pointer"); |
| 182 | assert(Y && "Can't compare null pointer"); |
| 183 | return *X < *Y; |
| 184 | } |
| 185 | }; |
| 186 | |
| 187 | typedef std::set<const TypeInfo*, ltfunctor> TypeRegMap; |
| 188 | static const TypeInfo* add_new_type(TypeInfo* existing); |
| 189 | |
| 190 | std::string newTy; |
| 191 | Types oldTy; |
| 192 | TypeInfo *elemTy; |
| 193 | TypeInfo *resultTy; |
| 194 | TypeList *elements; |
| 195 | uint64_t nelems; |
| 196 | static TypeRegMap registry; |
| 197 | }; |
| 198 | |
| 199 | /// This type is used to keep track of the signedness of constants. |
| 200 | struct ConstInfo { |
| 201 | std::string *cnst; |
| 202 | const TypeInfo *type; |
| 203 | void destroy() { delete cnst; } |
| 204 | }; |
| 205 | |
| 206 | typedef std::vector<ValueInfo> ValueList; |
| 207 | |
| 208 | inline void ValueInfo::destroy() { delete val; } |
| 209 | |
| 210 | #endif |