Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1 | //===-- Type.cpp - Implement the Type class ----------------------*- C++ -*--=// |
| 2 | // |
| 3 | // This file implements the Type class for the VMCore library. |
| 4 | // |
| 5 | //===----------------------------------------------------------------------===// |
| 6 | |
| 7 | #include "llvm/DerivedTypes.h" |
| 8 | #include "llvm/Tools/StringExtras.h" |
| 9 | |
| 10 | //===----------------------------------------------------------------------===// |
| 11 | // Type Class Implementation |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | static unsigned CurUID = 0; |
| 15 | static vector<const Type *> UIDMappings; |
| 16 | |
| 17 | Type::Type(const string &name, PrimitiveID id) |
| 18 | : Value(Type::TypeTy, Value::TypeVal, name) { |
| 19 | ID = id; |
| 20 | ConstRulesImpl = 0; |
| 21 | |
| 22 | UID = CurUID++; // Assign types UID's as they are created |
| 23 | UIDMappings.push_back(this); |
| 24 | } |
| 25 | |
| 26 | const Type *Type::getUniqueIDType(unsigned UID) { |
| 27 | assert(UID < UIDMappings.size() && |
| 28 | "Type::getPrimitiveType: UID out of range!"); |
| 29 | return UIDMappings[UID]; |
| 30 | } |
| 31 | |
| 32 | const Type *Type::getPrimitiveType(PrimitiveID IDNumber) { |
| 33 | switch (IDNumber) { |
| 34 | case VoidTyID : return VoidTy; |
| 35 | case BoolTyID : return BoolTy; |
| 36 | case UByteTyID : return UByteTy; |
| 37 | case SByteTyID : return SByteTy; |
| 38 | case UShortTyID: return UShortTy; |
| 39 | case ShortTyID : return ShortTy; |
| 40 | case UIntTyID : return UIntTy; |
| 41 | case IntTyID : return IntTy; |
| 42 | case ULongTyID : return ULongTy; |
| 43 | case LongTyID : return LongTy; |
| 44 | case FloatTyID : return FloatTy; |
| 45 | case DoubleTyID: return DoubleTy; |
| 46 | case TypeTyID : return TypeTy; |
| 47 | case LabelTyID : return LabelTy; |
| 48 | case LockTyID : return LockTy; |
| 49 | case FillerTyID: return new Type("XXX FILLER XXX", FillerTyID); // TODO:KILLME |
| 50 | default: |
| 51 | return 0; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | |
| 56 | |
| 57 | //===----------------------------------------------------------------------===// |
| 58 | // Auxilliary classes |
| 59 | //===----------------------------------------------------------------------===// |
| 60 | // |
| 61 | // These classes are used to implement specialized behavior for each different |
| 62 | // type. |
| 63 | // |
| 64 | class SignedIntType : public Type { |
| 65 | int Size; |
| 66 | public: |
| 67 | SignedIntType(const string &Name, PrimitiveID id, int size) : Type(Name, id) { |
| 68 | Size = size; |
| 69 | } |
| 70 | |
| 71 | // isSigned - Return whether a numeric type is signed. |
| 72 | virtual bool isSigned() const { return 1; } |
| 73 | }; |
| 74 | |
| 75 | class UnsignedIntType : public Type { |
| 76 | uint64_t Size; |
| 77 | public: |
| 78 | UnsignedIntType(const string &N, PrimitiveID id, int size) : Type(N, id) { |
| 79 | Size = size; |
| 80 | } |
| 81 | |
| 82 | // isUnsigned - Return whether a numeric type is signed. |
| 83 | virtual bool isUnsigned() const { return 1; } |
| 84 | }; |
| 85 | |
| 86 | static struct TypeType : public Type { |
| 87 | TypeType() : Type("type", TypeTyID) {} |
| 88 | } TheTypeType; // Implement the type that is global. |
| 89 | |
| 90 | |
| 91 | //===----------------------------------------------------------------------===// |
| 92 | // Static 'Type' data |
| 93 | //===----------------------------------------------------------------------===// |
| 94 | |
| 95 | const Type *Type::VoidTy = new Type("void" , VoidTyID), |
| 96 | *Type::BoolTy = new Type("bool" , BoolTyID), |
| 97 | *Type::SByteTy = new SignedIntType("sbyte" , SByteTyID, 1), |
| 98 | *Type::UByteTy = new UnsignedIntType("ubyte" , UByteTyID, 1), |
| 99 | *Type::ShortTy = new SignedIntType("short" , ShortTyID, 2), |
| 100 | *Type::UShortTy = new UnsignedIntType("ushort", UShortTyID, 2), |
| 101 | *Type::IntTy = new SignedIntType("int" , IntTyID, 4), |
| 102 | *Type::UIntTy = new UnsignedIntType("uint" , UIntTyID, 4), |
| 103 | *Type::LongTy = new SignedIntType("long" , LongTyID, 8), |
| 104 | *Type::ULongTy = new UnsignedIntType("ulong" , ULongTyID, 8), |
| 105 | *Type::FloatTy = new Type("float" , FloatTyID), |
| 106 | *Type::DoubleTy = new Type("double", DoubleTyID), |
| 107 | *Type::TypeTy = &TheTypeType, |
| 108 | *Type::LabelTy = new Type("label" , LabelTyID), |
| 109 | *Type::LockTy = new Type("lock" , LockTyID); |
| 110 | |
| 111 | |
| 112 | //===----------------------------------------------------------------------===// |
| 113 | // Derived Type Implementations |
| 114 | //===----------------------------------------------------------------------===// |
| 115 | |
| 116 | // Make sure that only one instance of a particular type may be created on any |
| 117 | // given run of the compiler... |
| 118 | // |
| 119 | // TODO: This list should be kept in sorted order so that we can do a binary |
| 120 | // TODO: search instead of linear search! |
| 121 | // |
| 122 | // TODO: This should be templatized so that every derived type can use the same |
| 123 | // TODO: code! |
| 124 | // |
| 125 | #define TEST_MERGE_TYPES 0 |
| 126 | |
| 127 | #if TEST_MERGE_TYPES |
| 128 | #include "llvm/Assembly/Writer.h" |
| 129 | #endif |
| 130 | |
| 131 | //===----------------------------------------------------------------------===// |
| 132 | // Derived Type Constructors |
| 133 | //===----------------------------------------------------------------------===// |
| 134 | |
| 135 | MethodType::MethodType(const Type *Result, const vector<const Type*> &Params, |
| 136 | const string &Name) |
| 137 | : Type(Name, MethodTyID), ResultType(Result), ParamTys(Params) { |
| 138 | } |
| 139 | |
| 140 | ArrayType::ArrayType(const Type *ElType, int NumEl, const string &Name) |
| 141 | : Type(Name, ArrayTyID), ElementType(ElType) { |
| 142 | NumElements = NumEl; |
| 143 | } |
| 144 | |
| 145 | StructType::StructType(const vector<const Type*> &Types, const string &Name) |
Vikram S. Adve | d01d99b | 2001-07-20 21:09:17 +0000 | [diff] [blame^] | 146 | : Type(Name, StructTyID), |
| 147 | ETypes(Types), |
| 148 | layoutCache(new StructSizeAndOffsetInfo) |
| 149 | { |
| 150 | ResetCachedInfo(); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | PointerType::PointerType(const Type *E) |
| 154 | : Type(E->getName() + " *", PointerTyID), ValueType(E) { |
| 155 | } |
| 156 | |
| 157 | //===----------------------------------------------------------------------===// |
| 158 | // Derived Type Creator Functions |
| 159 | //===----------------------------------------------------------------------===// |
| 160 | |
| 161 | const MethodType *MethodType::getMethodType(const Type *ReturnType, |
| 162 | const vector<const Type*> &Params) { |
| 163 | static vector<const MethodType*> ExistingMethodTypesCache; |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 164 | for (unsigned i = 0; i < ExistingMethodTypesCache.size(); ++i) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 165 | const MethodType *T = ExistingMethodTypesCache[i]; |
| 166 | if (T->getReturnType() == ReturnType) { |
| 167 | const ParamTypes &EParams = T->getParamTypes(); |
| 168 | ParamTypes::const_iterator I = Params.begin(); |
| 169 | ParamTypes::const_iterator J = EParams.begin(); |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 170 | for (; I != Params.end() && J != EParams.end(); ++I, ++J) |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 171 | if (*I != *J) break; // These types aren't equal! |
| 172 | |
| 173 | if (I == Params.end() && J == EParams.end()) { |
| 174 | #if TEST_MERGE_TYPES == 2 |
| 175 | ostream_iterator<const Type*> out(cerr, ", "); |
| 176 | cerr << "Type: \""; |
| 177 | copy(Params.begin(), Params.end(), out); |
| 178 | cerr << "\"\nEquals: \""; |
| 179 | copy(EParams.begin(), EParams.end(), out); |
| 180 | cerr << "\"" << endl; |
| 181 | #endif |
| 182 | return T; |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | #if TEST_MERGE_TYPES == 2 |
| 187 | ostream_iterator<const Type*> out(cerr, ", "); |
| 188 | cerr << "Input Types: "; |
| 189 | copy(Params.begin(), Params.end(), out); |
| 190 | cerr << endl; |
| 191 | #endif |
| 192 | |
| 193 | // Calculate the string name for the new type... |
| 194 | string Name = ReturnType->getName() + " ("; |
| 195 | for (ParamTypes::const_iterator I = Params.begin(); |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 196 | I != Params.end(); ++I) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 197 | if (I != Params.begin()) |
| 198 | Name += ", "; |
| 199 | Name += (*I)->getName(); |
| 200 | } |
| 201 | Name += ")"; |
| 202 | |
| 203 | #if TEST_MERGE_TYPES |
| 204 | cerr << "Derived new type: " << Name << endl; |
| 205 | #endif |
| 206 | |
| 207 | MethodType *Result = new MethodType(ReturnType, Params, Name); |
| 208 | ExistingMethodTypesCache.push_back(Result); |
| 209 | return Result; |
| 210 | } |
| 211 | |
| 212 | |
| 213 | const ArrayType *ArrayType::getArrayType(const Type *ElementType, |
| 214 | int NumElements = -1) { |
Chris Lattner | ab5ac6b | 2001-07-08 23:22:50 +0000 | [diff] [blame] | 215 | assert(ElementType && "Can't get array of null types!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 216 | static vector<const ArrayType*> ExistingTypesCache; |
| 217 | |
| 218 | // Search cache for value... |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 219 | for (unsigned i = 0; i < ExistingTypesCache.size(); ++i) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 220 | const ArrayType *T = ExistingTypesCache[i]; |
| 221 | |
| 222 | if (T->getElementType() == ElementType && |
| 223 | T->getNumElements() == NumElements) |
| 224 | return T; |
| 225 | } |
| 226 | |
| 227 | // Value not found. Derive a new type! |
| 228 | string Name = "["; |
| 229 | if (NumElements != -1) Name += itostr(NumElements) + " x "; |
| 230 | |
| 231 | Name += ElementType->getName(); |
| 232 | |
| 233 | ArrayType *Result = new ArrayType(ElementType, NumElements, Name + "]"); |
| 234 | ExistingTypesCache.push_back(Result); |
| 235 | |
| 236 | #if TEST_MERGE_TYPES |
| 237 | cerr << "Derived new type: " << Result->getName() << endl; |
| 238 | #endif |
| 239 | return Result; |
| 240 | } |
| 241 | |
| 242 | const StructType *StructType::getStructType(const ElementTypes &ETypes) { |
| 243 | static vector<const StructType*> ExistingStructTypesCache; |
| 244 | |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 245 | for (unsigned i = 0; i < ExistingStructTypesCache.size(); ++i) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 246 | const StructType *T = ExistingStructTypesCache[i]; |
| 247 | |
| 248 | const ElementTypes &Elements = T->getElementTypes(); |
| 249 | ElementTypes::const_iterator I = ETypes.begin(); |
| 250 | ElementTypes::const_iterator J = Elements.begin(); |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 251 | for (; I != ETypes.end() && J != Elements.end(); ++I, ++J) |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 252 | if (*I != *J) break; // These types aren't equal! |
| 253 | |
| 254 | if (I == ETypes.end() && J == Elements.end()) { |
| 255 | #if TEST_MERGE_TYPES == 2 |
| 256 | ostream_iterator<const Type*> out(cerr, ", "); |
| 257 | cerr << "Type: \""; |
| 258 | copy(ETypes.begin(), ETypes.end(), out); |
| 259 | cerr << "\"\nEquals: \""; |
| 260 | copy(Elements.begin(), Elements.end(), out); |
| 261 | cerr << "\"" << endl; |
| 262 | #endif |
| 263 | return T; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | #if TEST_MERGE_TYPES == 2 |
| 268 | ostream_iterator<const Type*> out(cerr, ", "); |
| 269 | cerr << "Input Types: "; |
| 270 | copy(ETypes.begin(), ETypes.end(), out); |
| 271 | cerr << endl; |
| 272 | #endif |
| 273 | |
| 274 | // Calculate the string name for the new type... |
| 275 | string Name = "{ "; |
| 276 | for (ElementTypes::const_iterator I = ETypes.begin(); |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 277 | I != ETypes.end(); ++I) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 278 | if (I != ETypes.begin()) |
| 279 | Name += ", "; |
| 280 | Name += (*I)->getName(); |
| 281 | } |
| 282 | Name += " }"; |
| 283 | |
| 284 | #if TEST_MERGE_TYPES |
| 285 | cerr << "Derived new type: " << Name << endl; |
| 286 | #endif |
| 287 | |
| 288 | StructType *Result = new StructType(ETypes, Name); |
| 289 | ExistingStructTypesCache.push_back(Result); |
| 290 | return Result; |
| 291 | } |
| 292 | |
| 293 | |
| 294 | const PointerType *PointerType::getPointerType(const Type *ValueType) { |
Chris Lattner | ab5ac6b | 2001-07-08 23:22:50 +0000 | [diff] [blame] | 295 | assert(ValueType && "Can't get a pointer to <null> type!"); |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 296 | static vector<const PointerType*> ExistingTypesCache; |
| 297 | |
| 298 | // Search cache for value... |
Chris Lattner | 7fc9fe3 | 2001-06-27 23:41:11 +0000 | [diff] [blame] | 299 | for (unsigned i = 0; i < ExistingTypesCache.size(); ++i) { |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 300 | const PointerType *T = ExistingTypesCache[i]; |
| 301 | |
| 302 | if (T->getValueType() == ValueType) |
| 303 | return T; |
| 304 | } |
| 305 | |
| 306 | PointerType *Result = new PointerType(ValueType); |
| 307 | ExistingTypesCache.push_back(Result); |
| 308 | |
| 309 | #if TEST_MERGE_TYPES |
| 310 | cerr << "Derived new type: " << Result->getName() << endl; |
| 311 | #endif |
| 312 | return Result; |
| 313 | } |
| 314 | |