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 | |
Jakob Kummerow
2013/06/05 14:53:24
As discussed offline, I think we'll need an Intege
rossberg
2013/06/05 15:37:02
Yes, let's add stuff once we get there.
| |
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 the 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 class Type : public Object { | |
86 public: | |
87 static Type* None() { return from_bitset(kNone); } | |
88 static Type* Any() { return from_bitset(kAny); } | |
89 | |
90 static Type* Oddball() { return from_bitset(kOddball); } | |
91 static Type* Boolean() { return from_bitset(kBoolean); } | |
92 static Type* Null() { return from_bitset(kNull); } | |
93 static Type* Undefined() { return from_bitset(kUndefined); } | |
94 | |
95 static Type* Number() { return from_bitset(kNumber); } | |
96 static Type* Smi() { return from_bitset(kSmi); } | |
97 static Type* Double() { return from_bitset(kDouble); } | |
98 | |
99 static Type* Name() { return from_bitset(kName); } | |
100 static Type* UniqueName() { return from_bitset(kUniqueName); } | |
101 static Type* String() { return from_bitset(kString); } | |
102 static Type* InternalizedString() { return from_bitset(kInternalizedString); } | |
103 static Type* Symbol() { return from_bitset(kSymbol); } | |
104 | |
105 static Type* Receiver() { return from_bitset(kReceiver); } | |
106 static Type* Object() { return from_bitset(kObject); } | |
107 static Type* Array() { return from_bitset(kArray); } | |
108 static Type* Function() { return from_bitset(kFunction); } | |
109 static Type* Proxy() { return from_bitset(kProxy); } | |
110 | |
111 static Type* Class(Handle<Map> map) { return from_handle(map); } | |
112 static Type* Constant(Handle<HeapObject> value) { | |
113 ASSERT(!value->IsMap() && !value->IsFixedArray()); | |
114 return from_handle(value); | |
115 } | |
116 | |
117 static Type* Union(Handle<Type> type1, Handle<Type> type2); | |
118 static Type* Optional(Handle<Type> type); // type \/ Undefined | |
119 | |
120 bool Is(Handle<Type> that); | |
121 bool Maybe(Handle<Type> that); | |
122 | |
123 // TODO(rossberg): method to iterate unions? | |
124 | |
125 private: | |
126 // A union is a fixed array containing types. Invariants: | |
127 // - its length is at least 2 | |
128 // - at most one field is a bitset, and it must go into index 0 | |
129 // - no field is a union | |
130 typedef FixedArray Unioned; | |
131 | |
132 enum { | |
133 kNull = 1<<0, | |
Jakob Kummerow
2013/06/05 14:53:24
nit: any reason not to have spaces around the oper
rossberg
2013/06/05 15:37:02
Done.
| |
134 kUndefined = 1<<1, | |
135 kBoolean = 1<<2, | |
136 kSmi = 1<<3, | |
137 kDouble = 1<<4, | |
138 kSymbol = 1<<5, | |
139 kInternalizedString = 1<<6, | |
140 kOtherString = 1<<7, | |
141 kArray = 1<<8, | |
142 kFunction = 1<<9, | |
143 kOtherObject = 1<<10, | |
144 kProxy = 1<<11, | |
145 | |
146 kOddball = kBoolean | kNull | kUndefined, | |
147 kNumber = kSmi | kDouble, | |
148 kString = kInternalizedString | kOtherString, | |
149 kUniqueName = kSymbol | kInternalizedString, | |
150 kName = kSymbol | kString, | |
151 kObject = kArray | kFunction | kOtherObject, | |
152 kReceiver = kObject | kProxy, | |
153 kAny = kOddball | kNumber | kName | kReceiver, | |
154 kNone = 0 | |
155 }; | |
156 | |
157 bool is_bitset() { return this->IsSmi(); } | |
158 bool is_class() { return this->IsMap(); } | |
159 bool is_constant() { return !(is_bitset() || is_class() || is_union()); } | |
160 bool is_union() { return this->IsFixedArray(); } | |
161 | |
162 int as_bitset() { return Smi::cast(this)->value(); } | |
163 Handle<Map> as_class() { return Handle<Map>::cast(handle()); } | |
164 Handle<HeapObject> as_constant() { | |
165 ASSERT(is_constant()); | |
166 return Handle<HeapObject>::cast(handle()); | |
167 } | |
168 Handle<Unioned> as_union() { return Handle<Unioned>::cast(handle()); } | |
169 | |
170 Handle<Type> handle() { return handle_via(this); } | |
171 Handle<Type> handle_via(Type* type) { | |
Jakob Kummerow
2013/06/05 14:53:24
I find it surprising that |type| is only used to r
rossberg
2013/06/05 15:37:02
Done.
| |
172 ASSERT(type->IsHeapObject()); | |
173 return v8::internal::handle(this, HeapObject::cast(type)->GetIsolate()); | |
174 } | |
175 | |
176 static Type* from_bitset(int bitset) { | |
177 return static_cast<Type*>(Object::cast(Smi::FromInt(bitset))); | |
178 } | |
179 static Type* from_handle(Handle<HeapObject> handle) { | |
180 return static_cast<Type*>(Object::cast(*handle)); | |
181 } | |
182 | |
183 static Handle<Type> union_get(Handle<Unioned> unioned, int i) { | |
184 Type* type = static_cast<Type*>(unioned->get(i)); | |
185 ASSERT(!type->is_union()); | |
186 return type->handle_via(from_handle(unioned)); | |
187 } | |
188 | |
189 int LubBitset(); | |
Jakob Kummerow
2013/06/05 14:53:24
short comment "Least upper bound"? You can even ad
rossberg
2013/06/05 15:37:02
Done.
| |
190 int GlbBitset(); | |
191 bool InUnion(Handle<Unioned> unioned, int current_size); | |
192 int ExtendUnion(Handle<Unioned> unioned, int current_size); | |
193 }; | |
194 | |
195 } } // namespace v8::internal | |
196 | |
197 #endif // V8_TYPES_H_ | |
OLD | NEW |