OLD | NEW |
| (Empty) |
1 // Copyright 2014 the V8 project authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef V8_TYPES_INL_H_ | |
6 #define V8_TYPES_INL_H_ | |
7 | |
8 #include "src/types.h" | |
9 | |
10 #include "src/factory.h" | |
11 #include "src/handles-inl.h" | |
12 | |
13 namespace v8 { | |
14 namespace internal { | |
15 | |
16 // ----------------------------------------------------------------------------- | |
17 // TypeImpl | |
18 | |
19 template<class Config> | |
20 typename TypeImpl<Config>::bitset TypeImpl<Config>::BitsetType::SignedSmall() { | |
21 return i::SmiValuesAre31Bits() ? kSigned31 : kSigned32; | |
22 } | |
23 | |
24 | |
25 template<class Config> | |
26 typename TypeImpl<Config>::bitset | |
27 TypeImpl<Config>::BitsetType::UnsignedSmall() { | |
28 return i::SmiValuesAre31Bits() ? kUnsigned30 : kUnsigned31; | |
29 } | |
30 | |
31 | |
32 #define CONSTRUCT_SIMD_TYPE(NAME, Name, name, lane_count, lane_type) \ | |
33 template<class Config> \ | |
34 typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::Name( \ | |
35 Isolate* isolate, Region* region) { \ | |
36 return Class(i::handle(isolate->heap()->name##_map()), region); \ | |
37 } | |
38 SIMD128_TYPES(CONSTRUCT_SIMD_TYPE) | |
39 #undef CONSTRUCT_SIMD_TYPE | |
40 | |
41 | |
42 template<class Config> | |
43 TypeImpl<Config>* TypeImpl<Config>::cast(typename Config::Base* object) { | |
44 TypeImpl* t = static_cast<TypeImpl*>(object); | |
45 DCHECK(t->IsBitset() || t->IsClass() || t->IsConstant() || t->IsRange() || | |
46 t->IsUnion() || t->IsArray() || t->IsFunction() || t->IsContext()); | |
47 return t; | |
48 } | |
49 | |
50 | |
51 // Most precise _current_ type of a value (usually its class). | |
52 template<class Config> | |
53 typename TypeImpl<Config>::TypeHandle TypeImpl<Config>::NowOf( | |
54 i::Object* value, Region* region) { | |
55 if (value->IsSmi() || | |
56 i::HeapObject::cast(value)->map()->instance_type() == HEAP_NUMBER_TYPE) { | |
57 return Of(value, region); | |
58 } | |
59 return Class(i::handle(i::HeapObject::cast(value)->map()), region); | |
60 } | |
61 | |
62 | |
63 template<class Config> | |
64 bool TypeImpl<Config>::NowContains(i::Object* value) { | |
65 DisallowHeapAllocation no_allocation; | |
66 if (this->IsAny()) return true; | |
67 if (value->IsHeapObject()) { | |
68 i::Map* map = i::HeapObject::cast(value)->map(); | |
69 for (Iterator<i::Map> it = this->Classes(); !it.Done(); it.Advance()) { | |
70 if (*it.Current() == map) return true; | |
71 } | |
72 } | |
73 return this->Contains(value); | |
74 } | |
75 | |
76 | |
77 // ----------------------------------------------------------------------------- | |
78 // ZoneTypeConfig | |
79 | |
80 // static | |
81 template<class T> | |
82 T* ZoneTypeConfig::handle(T* type) { | |
83 return type; | |
84 } | |
85 | |
86 | |
87 // static | |
88 template<class T> | |
89 T* ZoneTypeConfig::cast(Type* type) { | |
90 return static_cast<T*>(type); | |
91 } | |
92 | |
93 | |
94 // static | |
95 bool ZoneTypeConfig::is_bitset(Type* type) { | |
96 return reinterpret_cast<uintptr_t>(type) & 1; | |
97 } | |
98 | |
99 | |
100 // static | |
101 bool ZoneTypeConfig::is_struct(Type* type, int tag) { | |
102 DCHECK(tag != kRangeStructTag); | |
103 if (is_bitset(type)) return false; | |
104 int type_tag = struct_tag(as_struct(type)); | |
105 return type_tag == tag; | |
106 } | |
107 | |
108 | |
109 // static | |
110 bool ZoneTypeConfig::is_range(Type* type) { | |
111 if (is_bitset(type)) return false; | |
112 int type_tag = struct_tag(as_struct(type)); | |
113 return type_tag == kRangeStructTag; | |
114 } | |
115 | |
116 | |
117 // static | |
118 bool ZoneTypeConfig::is_class(Type* type) { | |
119 return false; | |
120 } | |
121 | |
122 | |
123 // static | |
124 ZoneTypeConfig::Type::bitset ZoneTypeConfig::as_bitset(Type* type) { | |
125 DCHECK(is_bitset(type)); | |
126 return static_cast<Type::bitset>(reinterpret_cast<uintptr_t>(type) ^ 1u); | |
127 } | |
128 | |
129 | |
130 // static | |
131 ZoneTypeConfig::Struct* ZoneTypeConfig::as_struct(Type* type) { | |
132 DCHECK(!is_bitset(type)); | |
133 return reinterpret_cast<Struct*>(type); | |
134 } | |
135 | |
136 | |
137 // static | |
138 ZoneTypeConfig::Range* ZoneTypeConfig::as_range(Type* type) { | |
139 DCHECK(!is_bitset(type)); | |
140 return reinterpret_cast<Range*>(type); | |
141 } | |
142 | |
143 | |
144 // static | |
145 i::Handle<i::Map> ZoneTypeConfig::as_class(Type* type) { | |
146 UNREACHABLE(); | |
147 return i::Handle<i::Map>(); | |
148 } | |
149 | |
150 | |
151 // static | |
152 ZoneTypeConfig::Type* ZoneTypeConfig::from_bitset(Type::bitset bitset) { | |
153 return reinterpret_cast<Type*>(static_cast<uintptr_t>(bitset | 1u)); | |
154 } | |
155 | |
156 | |
157 // static | |
158 ZoneTypeConfig::Type* ZoneTypeConfig::from_bitset( | |
159 Type::bitset bitset, Zone* Zone) { | |
160 return from_bitset(bitset); | |
161 } | |
162 | |
163 | |
164 // static | |
165 ZoneTypeConfig::Type* ZoneTypeConfig::from_struct(Struct* structure) { | |
166 return reinterpret_cast<Type*>(structure); | |
167 } | |
168 | |
169 | |
170 // static | |
171 ZoneTypeConfig::Type* ZoneTypeConfig::from_range(Range* range) { | |
172 return reinterpret_cast<Type*>(range); | |
173 } | |
174 | |
175 | |
176 // static | |
177 ZoneTypeConfig::Type* ZoneTypeConfig::from_class( | |
178 i::Handle<i::Map> map, Zone* zone) { | |
179 return from_bitset(0); | |
180 } | |
181 | |
182 | |
183 // static | |
184 ZoneTypeConfig::Struct* ZoneTypeConfig::struct_create( | |
185 int tag, int length, Zone* zone) { | |
186 DCHECK(tag != kRangeStructTag); | |
187 Struct* structure = reinterpret_cast<Struct*>( | |
188 zone->New(sizeof(void*) * (length + 2))); // NOLINT | |
189 structure[0] = reinterpret_cast<void*>(tag); | |
190 structure[1] = reinterpret_cast<void*>(length); | |
191 return structure; | |
192 } | |
193 | |
194 | |
195 // static | |
196 void ZoneTypeConfig::struct_shrink(Struct* structure, int length) { | |
197 DCHECK(0 <= length && length <= struct_length(structure)); | |
198 structure[1] = reinterpret_cast<void*>(length); | |
199 } | |
200 | |
201 | |
202 // static | |
203 int ZoneTypeConfig::struct_tag(Struct* structure) { | |
204 return static_cast<int>(reinterpret_cast<intptr_t>(structure[0])); | |
205 } | |
206 | |
207 | |
208 // static | |
209 int ZoneTypeConfig::struct_length(Struct* structure) { | |
210 return static_cast<int>(reinterpret_cast<intptr_t>(structure[1])); | |
211 } | |
212 | |
213 | |
214 // static | |
215 Type* ZoneTypeConfig::struct_get(Struct* structure, int i) { | |
216 DCHECK(0 <= i && i <= struct_length(structure)); | |
217 return static_cast<Type*>(structure[2 + i]); | |
218 } | |
219 | |
220 | |
221 // static | |
222 void ZoneTypeConfig::struct_set(Struct* structure, int i, Type* x) { | |
223 DCHECK(0 <= i && i <= struct_length(structure)); | |
224 structure[2 + i] = x; | |
225 } | |
226 | |
227 | |
228 // static | |
229 template<class V> | |
230 i::Handle<V> ZoneTypeConfig::struct_get_value(Struct* structure, int i) { | |
231 DCHECK(0 <= i && i <= struct_length(structure)); | |
232 return i::Handle<V>(static_cast<V**>(structure[2 + i])); | |
233 } | |
234 | |
235 | |
236 // static | |
237 template<class V> | |
238 void ZoneTypeConfig::struct_set_value( | |
239 Struct* structure, int i, i::Handle<V> x) { | |
240 DCHECK(0 <= i && i <= struct_length(structure)); | |
241 structure[2 + i] = x.location(); | |
242 } | |
243 | |
244 | |
245 // static | |
246 ZoneTypeConfig::Range* ZoneTypeConfig::range_create(Zone* zone) { | |
247 Range* range = reinterpret_cast<Range*>(zone->New(sizeof(Range))); // NOLINT | |
248 range->tag = reinterpret_cast<void*>(kRangeStructTag); | |
249 range->bitset = 0; | |
250 range->limits[0] = 1; | |
251 range->limits[1] = 0; | |
252 return range; | |
253 } | |
254 | |
255 | |
256 // static | |
257 int ZoneTypeConfig::range_get_bitset(ZoneTypeConfig::Range* range) { | |
258 return range->bitset; | |
259 } | |
260 | |
261 | |
262 // static | |
263 void ZoneTypeConfig::range_set_bitset(ZoneTypeConfig::Range* range, int value) { | |
264 range->bitset = value; | |
265 } | |
266 | |
267 | |
268 // static | |
269 double ZoneTypeConfig::range_get_double(ZoneTypeConfig::Range* range, | |
270 int index) { | |
271 DCHECK(index >= 0 && index < 2); | |
272 return range->limits[index]; | |
273 } | |
274 | |
275 | |
276 // static | |
277 void ZoneTypeConfig::range_set_double(ZoneTypeConfig::Range* range, int index, | |
278 double value, Zone*) { | |
279 DCHECK(index >= 0 && index < 2); | |
280 range->limits[index] = value; | |
281 } | |
282 | |
283 } // namespace internal | |
284 } // namespace v8 | |
285 | |
286 #endif // V8_TYPES_INL_H_ | |
OLD | NEW |