aboutsummaryrefslogtreecommitdiff
path: root/deps/v8/src/types.h
blob: 2810ffc8a17cb4fe64e5fb05793e0f5b57ee5eab (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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// Copyright 2013 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifndef V8_TYPES_H_
#define V8_TYPES_H_

#include "v8.h"

#include "objects.h"

namespace v8 {
namespace internal {


// A simple type system for compiler-internal use. It is based entirely on
// union types, and all subtyping hence amounts to set inclusion. Besides the
// obvious primitive types and some predefined unions, the type language also
// can express class types (a.k.a. specific maps) and singleton types (i.e.,
// concrete constants).
//
// The following equations and inequations hold:
//
//   None <= T
//   T <= Any
//
//   Oddball = Boolean \/ Null \/ Undefined
//   Number = Signed32 \/ Unsigned32 \/ Double
//   Smi <= Signed32
//   Name = String \/ Symbol
//   UniqueName = InternalizedString \/ Symbol
//   InternalizedString < String
//
//   Allocated = Receiver \/ Number \/ Name
//   Detectable = Allocated - Undetectable
//   Undetectable < Object
//   Receiver = Object \/ Proxy
//   Array < Object
//   Function < Object
//   RegExp < Object
//
//   Class(map) < T   iff instance_type(map) < T
//   Constant(x) < T  iff instance_type(map(x)) < T
//
// Note that Constant(x) < Class(map(x)) does _not_ hold, since x's map can
// change! (Its instance type cannot, however.)
// TODO(rossberg): the latter is not currently true for proxies, because of fix,
// but will hold once we implement direct proxies.
//
// There are two main functions for testing types:
//
//   T1->Is(T2)     -- tests whether T1 is included in T2 (i.e., T1 <= T2)
//   T1->Maybe(T2)  -- tests whether T1 and T2 overlap (i.e., T1 /\ T2 =/= 0)
//
// Typically, the former is to be used to select representations (e.g., via
// T->Is(Integer31())), and the to check whether a specific case needs handling
// (e.g., via T->Maybe(Number())).
//
// There is no functionality to discover whether a type is a leaf in the
// lattice. That is intentional. It should always be possible to refine the
// lattice (e.g., splitting up number types further) without invalidating any
// existing assumptions or tests.
//
// Consequently, do not use pointer equality for type tests, always use Is!
//
// Internally, all 'primitive' types, and their unions, are represented as
// bitsets via smis. Class is a heap pointer to the respective map. Only
// Constant's, or unions containing Class'es or Constant's, require allocation.
// Note that the bitset representation is closed under both Union and Intersect.
//
// The type representation is heap-allocated, so cannot (currently) be used in
// a concurrent compilation context.


#define PRIMITIVE_TYPE_LIST(V)           \
  V(None,                0)              \
  V(Null,                1 << 0)         \
  V(Undefined,           1 << 1)         \
  V(Boolean,             1 << 2)         \
  V(Smi,                 1 << 3)         \
  V(OtherSigned32,       1 << 4)         \
  V(Unsigned32,          1 << 5)         \
  V(Double,              1 << 6)         \
  V(Symbol,              1 << 7)         \
  V(InternalizedString,  1 << 8)         \
  V(OtherString,         1 << 9)         \
  V(Undetectable,        1 << 10)        \
  V(Array,               1 << 11)        \
  V(Function,            1 << 12)        \
  V(RegExp,              1 << 13)        \
  V(OtherObject,         1 << 14)        \
  V(Proxy,               1 << 15)        \
  V(Internal,            1 << 16)

#define COMPOSED_TYPE_LIST(V)                                       \
  V(Oddball,         kBoolean | kNull | kUndefined)                 \
  V(Signed32,        kSmi | kOtherSigned32)                         \
  V(Number,          kSigned32 | kUnsigned32 | kDouble)             \
  V(String,          kInternalizedString | kOtherString)            \
  V(UniqueName,      kSymbol | kInternalizedString)                 \
  V(Name,            kSymbol | kString)                             \
  V(NumberOrString,  kNumber | kString)                             \
  V(Object,          kUndetectable | kArray | kFunction |           \
                     kRegExp | kOtherObject)                        \
  V(Receiver,        kObject | kProxy)                              \
  V(Allocated,       kDouble | kName | kReceiver)                   \
  V(Any,             kOddball | kNumber | kAllocated | kInternal)   \
  V(Detectable,      kAllocated - kUndetectable)

#define TYPE_LIST(V)     \
  PRIMITIVE_TYPE_LIST(V) \
  COMPOSED_TYPE_LIST(V)



class Type : public Object {
 public:
  #define DEFINE_TYPE_CONSTRUCTOR(type, value)           \
    static Type* type() { return from_bitset(k##type); }
  TYPE_LIST(DEFINE_TYPE_CONSTRUCTOR)
  #undef DEFINE_TYPE_CONSTRUCTOR

  static Type* Class(Handle<Map> map) { return from_handle(map); }
  static Type* Constant(Handle<HeapObject> value) {
    return Constant(value, value->GetIsolate());
  }
  static Type* Constant(Handle<v8::internal::Object> value, Isolate* isolate) {
    return from_handle(isolate->factory()->NewBox(value));
  }

  static Type* Union(Handle<Type> type1, Handle<Type> type2);
  static Type* Intersect(Handle<Type> type1, Handle<Type> type2);
  static Type* Optional(Handle<Type> type);  // type \/ Undefined

  bool Is(Type* that) { return (this == that) ? true : IsSlowCase(that); }
  bool Is(Handle<Type> that) { return this->Is(*that); }
  bool Maybe(Type* that);
  bool Maybe(Handle<Type> that) { return this->Maybe(*that); }

  bool IsClass() { return is_class(); }
  bool IsConstant() { return is_constant(); }
  Handle<Map> AsClass() { return as_class(); }
  Handle<v8::internal::Object> AsConstant() { return as_constant(); }

  int NumClasses();
  int NumConstants();

  template<class T>
  class Iterator {
   public:
    bool Done() const { return index_ < 0; }
    Handle<T> Current();
    void Advance();

   private:
    friend class Type;

    Iterator() : index_(-1) {}
    explicit Iterator(Handle<Type> type) : type_(type), index_(-1) {
      Advance();
    }

    inline bool matches(Handle<Type> type);
    inline Handle<Type> get_type();

    Handle<Type> type_;
    int index_;
  };

  Iterator<Map> Classes() {
    if (this->is_bitset()) return Iterator<Map>();
    return Iterator<Map>(this->handle());
  }
  Iterator<v8::internal::Object> Constants() {
    if (this->is_bitset()) return Iterator<v8::internal::Object>();
    return Iterator<v8::internal::Object>(this->handle());
  }

  static Type* cast(v8::internal::Object* object) {
    Type* t = static_cast<Type*>(object);
    ASSERT(t->is_bitset() || t->is_class() ||
           t->is_constant() || t->is_union());
    return t;
  }

#ifdef OBJECT_PRINT
  void TypePrint();
  void TypePrint(FILE* out);
#endif

 private:
  // A union is a fixed array containing types. Invariants:
  // - its length is at least 2
  // - at most one field is a bitset, and it must go into index 0
  // - no field is a union
  typedef FixedArray Unioned;

  enum {
    #define DECLARE_TYPE(type, value) k##type = (value),
    TYPE_LIST(DECLARE_TYPE)
    #undef DECLARE_TYPE
    kUnusedEOL = 0
  };

  bool is_bitset() { return this->IsSmi(); }
  bool is_class() { return this->IsMap(); }
  bool is_constant() { return this->IsBox(); }
  bool is_union() { return this->IsFixedArray(); }

  bool IsSlowCase(Type* that);

  int as_bitset() { return Smi::cast(this)->value(); }
  Handle<Map> as_class() { return Handle<Map>::cast(handle()); }
  Handle<v8::internal::Object> as_constant() {
    Handle<Box> box = Handle<Box>::cast(handle());
    return v8::internal::handle(box->value(), box->GetIsolate());
  }
  Handle<Unioned> as_union() { return Handle<Unioned>::cast(handle()); }

  Handle<Type> handle() { return handle_via_isolate_of(this); }
  Handle<Type> handle_via_isolate_of(Type* type) {
    ASSERT(type->IsHeapObject());
    return v8::internal::handle(this, HeapObject::cast(type)->GetIsolate());
  }

  static Type* from_bitset(int bitset) {
    return static_cast<Type*>(Object::cast(Smi::FromInt(bitset)));
  }
  static Type* from_handle(Handle<HeapObject> handle) {
    return static_cast<Type*>(Object::cast(*handle));
  }

  static Handle<Type> union_get(Handle<Unioned> unioned, int i) {
    Type* type = static_cast<Type*>(unioned->get(i));
    ASSERT(!type->is_union());
    return type->handle_via_isolate_of(from_handle(unioned));
  }

  int LubBitset();  // least upper bound that's a bitset
  int GlbBitset();  // greatest lower bound that's a bitset
  bool InUnion(Handle<Unioned> unioned, int current_size);
  int ExtendUnion(Handle<Unioned> unioned, int current_size);
  int ExtendIntersection(
      Handle<Unioned> unioned, Handle<Type> type, int current_size);

  static const char* GetComposedName(int type) {
    switch (type) {
      #define PRINT_COMPOSED_TYPE(type, value)  \
      case k##type:                             \
        return # type;
      COMPOSED_TYPE_LIST(PRINT_COMPOSED_TYPE)
      #undef PRINT_COMPOSED_TYPE
    }
    return NULL;
  }

  static const char* GetPrimitiveName(int type) {
    switch (type) {
      #define PRINT_PRIMITIVE_TYPE(type, value)  \
      case k##type:                              \
        return # type;
      PRIMITIVE_TYPE_LIST(PRINT_PRIMITIVE_TYPE)
      #undef PRINT_PRIMITIVE_TYPE
      default:
        UNREACHABLE();
        return "InvalidType";
    }
  }
};


// A simple struct to represent a pair of lower/upper type bounds.
struct Bounds {
  Handle<Type> lower;
  Handle<Type> upper;

  Bounds() {}
  Bounds(Handle<Type> l, Handle<Type> u) : lower(l), upper(u) {}
  Bounds(Type* l, Type* u, Isolate* isl) : lower(l, isl), upper(u, isl) {}
  explicit Bounds(Handle<Type> t) : lower(t), upper(t) {}
  Bounds(Type* t, Isolate* isl) : lower(t, isl), upper(t, isl) {}

  // Unrestricted bounds.
  static Bounds Unbounded(Isolate* isl) {
    return Bounds(Type::None(), Type::Any(), isl);
  }

  // Meet: both b1 and b2 are known to hold.
  static Bounds Both(Bounds b1, Bounds b2, Isolate* isl) {
    return Bounds(
        handle(Type::Union(b1.lower, b2.lower), isl),
        handle(Type::Intersect(b1.upper, b2.upper), isl));
  }

  // Join: either b1 or b2 is known to hold.
  static Bounds Either(Bounds b1, Bounds b2, Isolate* isl) {
    return Bounds(
        handle(Type::Intersect(b1.lower, b2.lower), isl),
        handle(Type::Union(b1.upper, b2.upper), isl));
  }

  static Bounds NarrowLower(Bounds b, Handle<Type> t, Isolate* isl) {
    return Bounds(handle(Type::Union(b.lower, t), isl), b.upper);
  }
  static Bounds NarrowUpper(Bounds b, Handle<Type> t, Isolate* isl) {
    return Bounds(b.lower, handle(Type::Intersect(b.upper, t), isl));
  }
};

} }  // namespace v8::internal

#endif  // V8_TYPES_H_