summaryrefslogtreecommitdiff
path: root/lib/VMCore/Type.cpp
blob: cf16309ccb44820a6224631c955e7a89760db8c4 (plain)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
//===-- Type.cpp - Implement the Type class ----------------------*- C++ -*--=//
//
// This file implements the Type class for the VMCore library.
//
//===----------------------------------------------------------------------===//

#include "llvm/DerivedTypes.h"
#include "llvm/Tools/StringExtras.h"

//===----------------------------------------------------------------------===//
//                         Type Class Implementation
//===----------------------------------------------------------------------===//

static unsigned CurUID = 0;
static vector<const Type *> UIDMappings;

Type::Type(const string &name, PrimitiveID id) 
  : Value(Type::TypeTy, Value::TypeVal, name) {
  ID = id;
  ConstRulesImpl = 0;

  UID = CurUID++;       // Assign types UID's as they are created
  UIDMappings.push_back(this);
}

const Type *Type::getUniqueIDType(unsigned UID) {
  assert(UID < UIDMappings.size() && 
         "Type::getPrimitiveType: UID out of range!");
  return UIDMappings[UID];
}

const Type *Type::getPrimitiveType(PrimitiveID IDNumber) {
  switch (IDNumber) {
  case VoidTyID  : return VoidTy;
  case BoolTyID  : return BoolTy;
  case UByteTyID : return UByteTy;
  case SByteTyID : return SByteTy;
  case UShortTyID: return UShortTy;
  case ShortTyID : return ShortTy;
  case UIntTyID  : return UIntTy;
  case IntTyID   : return IntTy;
  case ULongTyID : return ULongTy;
  case LongTyID  : return LongTy;
  case FloatTyID : return FloatTy;
  case DoubleTyID: return DoubleTy;
  case TypeTyID  : return TypeTy;
  case LabelTyID : return LabelTy;
  case LockTyID  : return LockTy;
  case FillerTyID: return new Type("XXX FILLER XXX", FillerTyID); // TODO:KILLME
  default:
    return 0;
  }
}



//===----------------------------------------------------------------------===//
//                           Auxilliary classes
//===----------------------------------------------------------------------===//
//
// These classes are used to implement specialized behavior for each different
// type.
//
class SignedIntType : public Type {
  int Size;
public:
  SignedIntType(const string &Name, PrimitiveID id, int size) : Type(Name, id) {
    Size = size;
  }

  // isSigned - Return whether a numeric type is signed.
  virtual bool isSigned() const { return 1; }
};

class UnsignedIntType : public Type {
  uint64_t Size;
public:
  UnsignedIntType(const string &N, PrimitiveID id, int size) : Type(N, id) {
    Size = size;
  }

  // isUnsigned - Return whether a numeric type is signed.
  virtual bool isUnsigned() const { return 1; }
};

static struct TypeType : public Type {
  TypeType() : Type("type", TypeTyID) {}
} TheTypeType;   // Implement the type that is global.


//===----------------------------------------------------------------------===//
//                           Static 'Type' data
//===----------------------------------------------------------------------===//

const Type *Type::VoidTy   = new            Type("void"  , VoidTyID),
           *Type::BoolTy   = new            Type("bool"  , BoolTyID),
           *Type::SByteTy  = new   SignedIntType("sbyte" , SByteTyID, 1),
           *Type::UByteTy  = new UnsignedIntType("ubyte" , UByteTyID, 1),
           *Type::ShortTy  = new   SignedIntType("short" ,  ShortTyID, 2),
           *Type::UShortTy = new UnsignedIntType("ushort", UShortTyID, 2),
           *Type::IntTy    = new   SignedIntType("int"   ,  IntTyID, 4), 
           *Type::UIntTy   = new UnsignedIntType("uint"  , UIntTyID, 4),
           *Type::LongTy   = new   SignedIntType("long"  ,  LongTyID, 8),
           *Type::ULongTy  = new UnsignedIntType("ulong" , ULongTyID, 8),
           *Type::FloatTy  = new            Type("float" , FloatTyID),
           *Type::DoubleTy = new            Type("double", DoubleTyID),
           *Type::TypeTy   =        &TheTypeType,
           *Type::LabelTy  = new            Type("label" , LabelTyID),
           *Type::LockTy   = new            Type("lock"  , LockTyID);


//===----------------------------------------------------------------------===//
//                      Derived Type Implementations
//===----------------------------------------------------------------------===//

// Make sure that only one instance of a particular type may be created on any
// given run of the compiler...
//
// TODO: This list should be kept in sorted order so that we can do a binary
// TODO: search instead of linear search!
//
// TODO: This should be templatized so that every derived type can use the same
// TODO: code!
//
#define TEST_MERGE_TYPES 0

#if TEST_MERGE_TYPES
#include "llvm/Assembly/Writer.h"
#endif

//===----------------------------------------------------------------------===//
//                          Derived Type Constructors
//===----------------------------------------------------------------------===//

MethodType::MethodType(const Type *Result, const vector<const Type*> &Params, 
                       const string &Name) 
  : Type(Name, MethodTyID), ResultType(Result), ParamTys(Params) {
}

ArrayType::ArrayType(const Type *ElType, int NumEl, const string &Name) 
  : Type(Name, ArrayTyID), ElementType(ElType) {
  NumElements = NumEl;
}

StructType::StructType(const vector<const Type*> &Types, const string &Name) 
  : Type(Name, StructTyID), ETypes(Types) {
}

PointerType::PointerType(const Type *E) 
  : Type(E->getName() + " *", PointerTyID), ValueType(E) {
}

//===----------------------------------------------------------------------===//
//                         Derived Type Creator Functions
//===----------------------------------------------------------------------===//

const MethodType *MethodType::getMethodType(const Type *ReturnType, 
                                            const vector<const Type*> &Params) {
  static vector<const MethodType*> ExistingMethodTypesCache;
  for (unsigned i = 0; i < ExistingMethodTypesCache.size(); i++) {
    const MethodType *T = ExistingMethodTypesCache[i];
    if (T->getReturnType() == ReturnType) {
      const ParamTypes &EParams = T->getParamTypes();
      ParamTypes::const_iterator I = Params.begin(); 
      ParamTypes::const_iterator J = EParams.begin(); 
      for (; I != Params.end() && J != EParams.end(); I++, J++)
        if (*I != *J) break;  // These types aren't equal!

      if (I == Params.end() && J == EParams.end()) {
#if TEST_MERGE_TYPES == 2
        ostream_iterator<const Type*> out(cerr, ", ");
        cerr << "Type: \"";
        copy(Params.begin(), Params.end(), out);
        cerr << "\"\nEquals: \"";
        copy(EParams.begin(), EParams.end(), out);
        cerr << "\"" << endl;
#endif
        return T;
      }
    }
  }  
#if TEST_MERGE_TYPES == 2
  ostream_iterator<const Type*> out(cerr, ", ");
  cerr << "Input Types: ";
  copy(Params.begin(), Params.end(), out);
  cerr << endl;
#endif

  // Calculate the string name for the new type...
  string Name = ReturnType->getName() + " (";
  for (ParamTypes::const_iterator I = Params.begin();  
       I != Params.end(); I++) {
    if (I != Params.begin())
      Name += ", ";
    Name += (*I)->getName();
  }
  Name += ")";

#if TEST_MERGE_TYPES
  cerr << "Derived new type: " << Name << endl;
#endif

  MethodType *Result = new MethodType(ReturnType, Params, Name);
  ExistingMethodTypesCache.push_back(Result);
  return Result;
}


const ArrayType *ArrayType::getArrayType(const Type *ElementType, 
					 int NumElements = -1) {
  static vector<const ArrayType*> ExistingTypesCache;

  // Search cache for value...
  for (unsigned i = 0; i < ExistingTypesCache.size(); i++) {
    const ArrayType *T = ExistingTypesCache[i];

    if (T->getElementType() == ElementType && 
	T->getNumElements() == NumElements)
      return T;
  }

  // Value not found.  Derive a new type!
  string Name = "[";
  if (NumElements != -1) Name += itostr(NumElements) + " x ";

  Name += ElementType->getName();
  
  ArrayType *Result = new ArrayType(ElementType, NumElements, Name + "]");
  ExistingTypesCache.push_back(Result);

#if TEST_MERGE_TYPES
  cerr << "Derived new type: " << Result->getName() << endl;
#endif
  return Result;
}

const StructType *StructType::getStructType(const ElementTypes &ETypes) {
  static vector<const StructType*> ExistingStructTypesCache;

  for (unsigned i = 0; i < ExistingStructTypesCache.size(); i++) {
    const StructType *T = ExistingStructTypesCache[i];

    const ElementTypes &Elements = T->getElementTypes();
    ElementTypes::const_iterator I = ETypes.begin(); 
    ElementTypes::const_iterator J = Elements.begin(); 
    for (; I != ETypes.end() && J != Elements.end(); I++, J++)
      if (*I != *J) break;  // These types aren't equal!
    
    if (I == ETypes.end() && J == Elements.end()) {
#if TEST_MERGE_TYPES == 2
      ostream_iterator<const Type*> out(cerr, ", ");
      cerr << "Type: \"";
      copy(ETypes.begin(), ETypes.end(), out);
      cerr << "\"\nEquals: \"";
      copy(Elements.begin(), Elements.end(), out);
      cerr << "\"" << endl;
#endif
      return T;
    }
  }

#if TEST_MERGE_TYPES == 2
  ostream_iterator<const Type*> out(cerr, ", ");
  cerr << "Input Types: ";
  copy(ETypes.begin(), ETypes.end(), out);
  cerr << endl;
#endif

  // Calculate the string name for the new type...
  string Name = "{ ";
  for (ElementTypes::const_iterator I = ETypes.begin();  
       I != ETypes.end(); I++) {
    if (I != ETypes.begin())
      Name += ", ";
    Name += (*I)->getName();
  }
  Name += " }";

#if TEST_MERGE_TYPES
  cerr << "Derived new type: " << Name << endl;
#endif

  StructType *Result = new StructType(ETypes, Name);
  ExistingStructTypesCache.push_back(Result);
  return Result;
}


const PointerType *PointerType::getPointerType(const Type *ValueType) {
  static vector<const PointerType*> ExistingTypesCache;

  // Search cache for value...
  for (unsigned i = 0; i < ExistingTypesCache.size(); i++) {
    const PointerType *T = ExistingTypesCache[i];

    if (T->getValueType() == ValueType)
      return T;
  }

  PointerType *Result = new PointerType(ValueType);
  ExistingTypesCache.push_back(Result);

#if TEST_MERGE_TYPES
  cerr << "Derived new type: " << Result->getName() << endl;
#endif
  return Result;
}