OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are |
| 4 // met: |
| 5 // |
| 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. |
| 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
| 28 #ifndef V8_TYPES_H_ |
| 29 #define V8_TYPES_H_ |
| 30 |
| 31 #include "v8.h" |
| 32 |
| 33 #include "objects.h" |
| 34 |
| 35 namespace v8 { |
| 36 namespace internal { |
| 37 |
| 38 |
| 39 // A simple type system for compiler-internal use. It is based entirely on |
| 40 // union types, and all subtyping hence amounts to set inclusion. Besides the |
| 41 // obvious primitive types and some predefined unions, the type language also |
| 42 // can express class types (a.k.a. specific maps) and singleton types (i.e., |
| 43 // concrete constants). |
| 44 // |
| 45 // The following equations and inequations hold: |
| 46 // |
| 47 // None <= T |
| 48 // T <= Any |
| 49 // |
| 50 // Oddball = Boolean \/ Null \/ Undefined |
| 51 // Number = Smi \/ Double |
| 52 // Name = String \/ Symbol |
| 53 // UniqueName = InternalizedString \/ Symbol |
| 54 // InternalizedString < String |
| 55 // |
| 56 // Receiver = Object \/ Proxy |
| 57 // Array < Object |
| 58 // Function < Object |
| 59 // |
| 60 // Class(map) < T iff instance_type(map) < T |
| 61 // Constant(x) < T iff instance_type(map(x)) < T |
| 62 // |
| 63 // Note that Constant(x) < Class(map(x)) does _not_ hold, since x's map can |
| 64 // change! (Its instance type cannot, however.) |
| 65 // TODO(rossberg): the latter is not currently true for proxies, because of fix, |
| 66 // but will hold once we implement direct proxies. |
| 67 // |
| 68 // There are two main functions for testing types: |
| 69 // |
| 70 // T1->Is(T2) -- tests whether T1 is included in T2 (i.e., T1 <= T2) |
| 71 // T1->Maybe(T2) -- tests whether T1 and T2 overlap (i.e., T1 /\ T2 =/= 0) |
| 72 // |
| 73 // Typically, the latter should be used to check whether a specific case needs |
| 74 // handling (e.g., via T->Maybe(Number)). |
| 75 // |
| 76 // There is no functionality to discover whether a type is a leaf in the |
| 77 // lattice. That is intentional. It should always be possible to refine the |
| 78 // lattice (e.g., splitting up number types further) without invalidating any |
| 79 // existing assumptions or tests. |
| 80 // |
| 81 // Internally, all 'primitive' types, and their unions, are represented as |
| 82 // bitsets via smis. Class and Constant are heap pointers to the respective |
| 83 // argument. Only unions containing Class'es or Constant's require allocation. |
| 84 // |
| 85 // The type representation is heap-allocated, so cannot (currently) be used in |
| 86 // a parallel compilation context. |
| 87 |
| 88 class Type : public Object { |
| 89 public: |
| 90 static Type* None() { return from_bitset(kNone); } |
| 91 static Type* Any() { return from_bitset(kAny); } |
| 92 |
| 93 static Type* Oddball() { return from_bitset(kOddball); } |
| 94 static Type* Boolean() { return from_bitset(kBoolean); } |
| 95 static Type* Null() { return from_bitset(kNull); } |
| 96 static Type* Undefined() { return from_bitset(kUndefined); } |
| 97 |
| 98 static Type* Number() { return from_bitset(kNumber); } |
| 99 static Type* Smi() { return from_bitset(kSmi); } |
| 100 static Type* Double() { return from_bitset(kDouble); } |
| 101 |
| 102 static Type* Name() { return from_bitset(kName); } |
| 103 static Type* UniqueName() { return from_bitset(kUniqueName); } |
| 104 static Type* String() { return from_bitset(kString); } |
| 105 static Type* InternalizedString() { return from_bitset(kInternalizedString); } |
| 106 static Type* Symbol() { return from_bitset(kSymbol); } |
| 107 |
| 108 static Type* Receiver() { return from_bitset(kReceiver); } |
| 109 static Type* Object() { return from_bitset(kObject); } |
| 110 static Type* Array() { return from_bitset(kArray); } |
| 111 static Type* Function() { return from_bitset(kFunction); } |
| 112 static Type* Proxy() { return from_bitset(kProxy); } |
| 113 |
| 114 static Type* Class(Handle<Map> map) { return from_handle(map); } |
| 115 static Type* Constant(Handle<HeapObject> value) { |
| 116 ASSERT(!value->IsMap() && !value->IsFixedArray()); |
| 117 return from_handle(value); |
| 118 } |
| 119 |
| 120 static Type* Union(Handle<Type> type1, Handle<Type> type2); |
| 121 static Type* Optional(Handle<Type> type); // type \/ Undefined |
| 122 |
| 123 bool Is(Handle<Type> that); |
| 124 bool Maybe(Handle<Type> that); |
| 125 |
| 126 // TODO(rossberg): method to iterate unions? |
| 127 |
| 128 private: |
| 129 // A union is a fixed array containing types. Invariants: |
| 130 // - its length is at least 2 |
| 131 // - at most one field is a bitset, and it must go into index 0 |
| 132 // - no field is a union |
| 133 typedef FixedArray Unioned; |
| 134 |
| 135 enum { |
| 136 kNull = 1 << 0, |
| 137 kUndefined = 1 << 1, |
| 138 kBoolean = 1 << 2, |
| 139 kSmi = 1 << 3, |
| 140 kDouble = 1 << 4, |
| 141 kSymbol = 1 << 5, |
| 142 kInternalizedString = 1 << 6, |
| 143 kOtherString = 1 << 7, |
| 144 kArray = 1 << 8, |
| 145 kFunction = 1 << 9, |
| 146 kOtherObject = 1 << 10, |
| 147 kProxy = 1 << 11, |
| 148 |
| 149 kOddball = kBoolean | kNull | kUndefined, |
| 150 kNumber = kSmi | kDouble, |
| 151 kString = kInternalizedString | kOtherString, |
| 152 kUniqueName = kSymbol | kInternalizedString, |
| 153 kName = kSymbol | kString, |
| 154 kObject = kArray | kFunction | kOtherObject, |
| 155 kReceiver = kObject | kProxy, |
| 156 kAny = kOddball | kNumber | kName | kReceiver, |
| 157 kNone = 0 |
| 158 }; |
| 159 |
| 160 bool is_bitset() { return this->IsSmi(); } |
| 161 bool is_class() { return this->IsMap(); } |
| 162 bool is_constant() { return !(is_bitset() || is_class() || is_union()); } |
| 163 bool is_union() { return this->IsFixedArray(); } |
| 164 |
| 165 int as_bitset() { return Smi::cast(this)->value(); } |
| 166 Handle<Map> as_class() { return Handle<Map>::cast(handle()); } |
| 167 Handle<HeapObject> as_constant() { |
| 168 ASSERT(is_constant()); |
| 169 return Handle<HeapObject>::cast(handle()); |
| 170 } |
| 171 Handle<Unioned> as_union() { return Handle<Unioned>::cast(handle()); } |
| 172 |
| 173 Handle<Type> handle() { return handle_via_isolate_of(this); } |
| 174 Handle<Type> handle_via_isolate_of(Type* type) { |
| 175 ASSERT(type->IsHeapObject()); |
| 176 return v8::internal::handle(this, HeapObject::cast(type)->GetIsolate()); |
| 177 } |
| 178 |
| 179 static Type* from_bitset(int bitset) { |
| 180 return static_cast<Type*>(Object::cast(Smi::FromInt(bitset))); |
| 181 } |
| 182 static Type* from_handle(Handle<HeapObject> handle) { |
| 183 return static_cast<Type*>(Object::cast(*handle)); |
| 184 } |
| 185 |
| 186 static Handle<Type> union_get(Handle<Unioned> unioned, int i) { |
| 187 Type* type = static_cast<Type*>(unioned->get(i)); |
| 188 ASSERT(!type->is_union()); |
| 189 return type->handle_via_isolate_of(from_handle(unioned)); |
| 190 } |
| 191 |
| 192 int LubBitset(); // least upper bound that's a bitset |
| 193 int GlbBitset(); // greatest lower bound that's a bitset |
| 194 bool InUnion(Handle<Unioned> unioned, int current_size); |
| 195 int ExtendUnion(Handle<Unioned> unioned, int current_size); |
| 196 }; |
| 197 |
| 198 } } // namespace v8::internal |
| 199 |
| 200 #endif // V8_TYPES_H_ |
OLD | NEW |