OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_WASM_MODULE_H_ | 5 #ifndef V8_WASM_MODULE_H_ |
6 #define V8_WASM_MODULE_H_ | 6 #define V8_WASM_MODULE_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "src/api.h" | 10 #include "src/api.h" |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 // Static representation of a WASM export. | 152 // Static representation of a WASM export. |
153 struct WasmExport { | 153 struct WasmExport { |
154 uint32_t name_length; // length in bytes of the exported name. | 154 uint32_t name_length; // length in bytes of the exported name. |
155 uint32_t name_offset; // offset in module bytes of the name to export. | 155 uint32_t name_offset; // offset in module bytes of the name to export. |
156 WasmExternalKind kind; // kind of the export. | 156 WasmExternalKind kind; // kind of the export. |
157 uint32_t index; // index into the respective space. | 157 uint32_t index; // index into the respective space. |
158 }; | 158 }; |
159 | 159 |
160 enum ModuleOrigin { kWasmOrigin, kAsmJsOrigin }; | 160 enum ModuleOrigin { kWasmOrigin, kAsmJsOrigin }; |
161 | 161 |
| 162 class WasmCompiledModule; |
| 163 |
162 // Static representation of a module. | 164 // Static representation of a module. |
163 struct WasmModule { | 165 struct WasmModule { |
164 static const uint32_t kPageSize = 0x10000; // Page size, 64kb. | 166 static const uint32_t kPageSize = 0x10000; // Page size, 64kb. |
165 static const uint32_t kMaxLegalPages = 65536; // Maximum legal pages | 167 static const uint32_t kMaxLegalPages = 65536; // Maximum legal pages |
166 static const uint32_t kMinMemPages = 1; // Minimum memory size = 64kb | 168 static const uint32_t kMinMemPages = 1; // Minimum memory size = 64kb |
167 static const uint32_t kMaxMemPages = 16384; // Maximum memory size = 1gb | 169 static const uint32_t kMaxMemPages = 16384; // Maximum memory size = 1gb |
168 | 170 |
169 const byte* module_start; // starting address for the module bytes. | 171 const byte* module_start; // starting address for the module bytes. |
170 const byte* module_end; // end address for the module bytes. | 172 const byte* module_end; // end address for the module bytes. |
171 uint32_t min_mem_pages; // minimum size of the memory in 64k pages. | 173 uint32_t min_mem_pages; // minimum size of the memory in 64k pages. |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 bool BoundsCheck(uint32_t start, uint32_t end) const { | 235 bool BoundsCheck(uint32_t start, uint32_t end) const { |
234 size_t size = module_end - module_start; | 236 size_t size = module_end - module_start; |
235 return start <= size && end <= size; | 237 return start <= size && end <= size; |
236 } | 238 } |
237 | 239 |
238 // Creates a new instantiation of the module in the given isolate. | 240 // Creates a new instantiation of the module in the given isolate. |
239 V8_EXPORT_PRIVATE static MaybeHandle<JSObject> Instantiate( | 241 V8_EXPORT_PRIVATE static MaybeHandle<JSObject> Instantiate( |
240 Isolate* isolate, ErrorThrower* thrower, Handle<JSObject> module_object, | 242 Isolate* isolate, ErrorThrower* thrower, Handle<JSObject> module_object, |
241 Handle<JSReceiver> ffi, Handle<JSArrayBuffer> memory); | 243 Handle<JSReceiver> ffi, Handle<JSArrayBuffer> memory); |
242 | 244 |
243 MaybeHandle<FixedArray> CompileFunctions(Isolate* isolate, | 245 MaybeHandle<WasmCompiledModule> CompileFunctions(Isolate* isolate, |
244 ErrorThrower* thrower) const; | 246 ErrorThrower* thrower) const; |
245 | 247 |
246 private: | 248 private: |
247 DISALLOW_COPY_AND_ASSIGN(WasmModule); | 249 DISALLOW_COPY_AND_ASSIGN(WasmModule); |
248 }; | 250 }; |
249 | 251 |
250 // An instantiated WASM module, including memory, function table, etc. | 252 // An instantiated WASM module, including memory, function table, etc. |
251 struct WasmModuleInstance { | 253 struct WasmModuleInstance { |
252 const WasmModule* module; // static representation of the module. | 254 const WasmModule* module; // static representation of the module. |
253 // -- Heap allocated -------------------------------------------------------- | 255 // -- Heap allocated -------------------------------------------------------- |
254 Handle<JSObject> js_object; // JavaScript module object. | 256 Handle<JSObject> js_object; // JavaScript module object. |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 | 334 |
333 std::ostream& operator<<(std::ostream& os, const WasmModule& module); | 335 std::ostream& operator<<(std::ostream& os, const WasmModule& module); |
334 std::ostream& operator<<(std::ostream& os, const WasmFunction& function); | 336 std::ostream& operator<<(std::ostream& os, const WasmFunction& function); |
335 std::ostream& operator<<(std::ostream& os, const WasmFunctionName& name); | 337 std::ostream& operator<<(std::ostream& os, const WasmFunctionName& name); |
336 | 338 |
337 typedef Result<const WasmModule*> ModuleResult; | 339 typedef Result<const WasmModule*> ModuleResult; |
338 typedef Result<WasmFunction*> FunctionResult; | 340 typedef Result<WasmFunction*> FunctionResult; |
339 typedef std::vector<std::pair<int, int>> FunctionOffsets; | 341 typedef std::vector<std::pair<int, int>> FunctionOffsets; |
340 typedef Result<FunctionOffsets> FunctionOffsetsResult; | 342 typedef Result<FunctionOffsets> FunctionOffsetsResult; |
341 | 343 |
| 344 class WasmCompiledModule : public FixedArray { |
| 345 public: |
| 346 static WasmCompiledModule* cast(Object* fixed_array) { |
| 347 return reinterpret_cast<WasmCompiledModule*>(fixed_array); |
| 348 } |
| 349 |
| 350 #define WCM_OBJECT_OR_WEAK(TYPE, NAME, ID) \ |
| 351 Handle<TYPE> NAME() const { return handle(ptr_to_##NAME()); } \ |
| 352 \ |
| 353 MaybeHandle<TYPE> maybe_##NAME() const { \ |
| 354 if (has_##NAME()) return NAME(); \ |
| 355 return MaybeHandle<TYPE>(); \ |
| 356 } \ |
| 357 \ |
| 358 TYPE* ptr_to_##NAME() const { \ |
| 359 Object* obj = get(ID); \ |
| 360 if (!obj->Is##TYPE()) return nullptr; \ |
| 361 return TYPE::cast(obj); \ |
| 362 } \ |
| 363 \ |
| 364 void set_##NAME(Handle<TYPE> value) { set_ptr_to_##NAME(*value); } \ |
| 365 \ |
| 366 void set_ptr_to_##NAME(TYPE* value) { set(ID, value); } \ |
| 367 \ |
| 368 bool has_##NAME() const { return get(ID)->Is##TYPE(); } \ |
| 369 \ |
| 370 void reset_##NAME() { set_undefined(ID); } |
| 371 |
| 372 #define WCM_OBJECT(TYPE, NAME) WCM_OBJECT_OR_WEAK(TYPE, NAME, kID_##NAME) |
| 373 |
| 374 #define WCM_SMALL_NUMBER(TYPE, NAME) \ |
| 375 TYPE NAME() const { \ |
| 376 return static_cast<TYPE>(Smi::cast(get(kID_##NAME))->value()); \ |
| 377 } \ |
| 378 \ |
| 379 void set_##NAME(TYPE value) { \ |
| 380 set(kID_##NAME, Smi::FromInt(static_cast<int>(value))); \ |
| 381 } |
| 382 |
| 383 #define WCM_LARGE_NUMBER(TYPE, NAME) \ |
| 384 TYPE NAME() const { \ |
| 385 return static_cast<TYPE>(HeapNumber::cast(get(kID_##NAME))->value()); \ |
| 386 } \ |
| 387 \ |
| 388 void set_##NAME(TYPE value) { \ |
| 389 HeapNumber::cast(get(kID_##NAME))->set_value(static_cast<double>(value)); \ |
| 390 } |
| 391 |
| 392 #define WCM_WEAK_LINK(TYPE, NAME) \ |
| 393 WCM_OBJECT_OR_WEAK(WeakCell, weak_##NAME, kID_##NAME); \ |
| 394 \ |
| 395 Handle<TYPE> NAME() const { \ |
| 396 return handle(TYPE::cast(weak_##NAME()->value())); \ |
| 397 } |
| 398 |
| 399 #define WCM_PROPERTY_TABLE(MACRO) \ |
| 400 MACRO(OBJECT, FixedArray, code_table) \ |
| 401 MACRO(OBJECT, FixedArray, import_data) \ |
| 402 MACRO(OBJECT, FixedArray, exports) \ |
| 403 MACRO(OBJECT, FixedArray, startup_function) \ |
| 404 MACRO(OBJECT, FixedArray, indirect_function_tables) \ |
| 405 MACRO(OBJECT, String, module_bytes) \ |
| 406 MACRO(OBJECT, ByteArray, function_names) \ |
| 407 MACRO(LARGE_NUMBER, uint32_t, min_required_memory) \ |
| 408 MACRO(OBJECT, FixedArray, data_segments_info) \ |
| 409 MACRO(OBJECT, ByteArray, data_segments) \ |
| 410 MACRO(LARGE_NUMBER, uint32_t, globals_size) \ |
| 411 MACRO(LARGE_NUMBER, uint32_t, mem_size) \ |
| 412 MACRO(OBJECT, JSArrayBuffer, mem_start) \ |
| 413 MACRO(SMALL_NUMBER, bool, export_memory) \ |
| 414 MACRO(SMALL_NUMBER, ModuleOrigin, origin) \ |
| 415 MACRO(WEAK_LINK, WasmCompiledModule, next_instance) \ |
| 416 MACRO(WEAK_LINK, WasmCompiledModule, prev_instance) \ |
| 417 MACRO(WEAK_LINK, JSObject, owning_instance) \ |
| 418 MACRO(WEAK_LINK, JSObject, module_object) |
| 419 |
| 420 private: |
| 421 enum PropertyIndices { |
| 422 #define INDICES(IGNORE1, IGNORE2, NAME) kID_##NAME, |
| 423 WCM_PROPERTY_TABLE(INDICES) Count |
| 424 #undef INDICES |
| 425 }; |
| 426 |
| 427 public: |
| 428 static Handle<WasmCompiledModule> New(Isolate* isolate) { |
| 429 Handle<FixedArray> ret = |
| 430 isolate->factory()->NewFixedArray(PropertyIndices::Count, TENURED); |
| 431 Handle<HeapNumber> number; |
| 432 #define WCM_INIT_OBJECT(IGNORE1, IGNORE2) |
| 433 #define WCM_INIT_WEAK_LINK(IGNORE1, IGNORE2) |
| 434 #define WCM_INIT_SMALL_NUMBER(IGNORE1, IGNORE2) |
| 435 #define WCM_INIT_LARGE_NUMBER(IGNORE, NAME) \ |
| 436 number = isolate->factory()->NewHeapNumber(0.0, MUTABLE, TENURED); \ |
| 437 ret->set(kID_##NAME, *number); |
| 438 |
| 439 #define INITIALIZER(KIND, TYPE, NAME) WCM_INIT_##KIND(TYPE, NAME) |
| 440 WCM_PROPERTY_TABLE(INITIALIZER) |
| 441 #undef INITIALIZER |
| 442 return handle(WasmCompiledModule::cast(*ret)); |
| 443 } |
| 444 |
| 445 Handle<WasmCompiledModule> Clone(Isolate* isolate) { |
| 446 Handle<WasmCompiledModule> ret = handle(WasmCompiledModule::cast( |
| 447 *isolate->factory()->CopyFixedArray(handle(this)))); |
| 448 Handle<HeapNumber> number = |
| 449 isolate->factory()->NewHeapNumber(0.0, MUTABLE, TENURED); |
| 450 ret->set(kID_mem_size, *number); |
| 451 ret->set_mem_size(mem_size()); |
| 452 return ret; |
| 453 } |
| 454 |
| 455 #define DECLARATION(KIND, TYPE, NAME) WCM_##KIND(TYPE, NAME) |
| 456 WCM_PROPERTY_TABLE(DECLARATION) |
| 457 #undef DECLARATION |
| 458 |
| 459 private: |
| 460 DISALLOW_IMPLICIT_CONSTRUCTORS(WasmCompiledModule); |
| 461 }; |
| 462 |
342 // Extract a function name from the given wasm object. | 463 // Extract a function name from the given wasm object. |
343 // Returns "<WASM UNNAMED>" if the function is unnamed or the name is not a | 464 // Returns "<WASM UNNAMED>" if the function is unnamed or the name is not a |
344 // valid UTF-8 string. | 465 // valid UTF-8 string. |
345 Handle<String> GetWasmFunctionName(Isolate* isolate, Handle<Object> wasm, | 466 Handle<String> GetWasmFunctionName(Isolate* isolate, Handle<Object> wasm, |
346 uint32_t func_index); | 467 uint32_t func_index); |
347 | 468 |
348 // Extract a function name from the given wasm object. | 469 // Extract a function name from the given wasm object. |
349 // Returns a null handle if the function is unnamed or the name is not a valid | 470 // Returns a null handle if the function is unnamed or the name is not a valid |
350 // UTF-8 string. | 471 // UTF-8 string. |
351 Handle<Object> GetWasmFunctionNameOrNull(Isolate* isolate, Handle<Object> wasm, | 472 Handle<Object> GetWasmFunctionNameOrNull(Isolate* isolate, Handle<Object> wasm, |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
423 int instance_count); | 544 int instance_count); |
424 void ValidateModuleState(Isolate* isolate, Handle<JSObject> module_obj); | 545 void ValidateModuleState(Isolate* isolate, Handle<JSObject> module_obj); |
425 void ValidateOrphanedInstance(Isolate* isolate, Handle<JSObject> instance); | 546 void ValidateOrphanedInstance(Isolate* isolate, Handle<JSObject> instance); |
426 | 547 |
427 } // namespace testing | 548 } // namespace testing |
428 } // namespace wasm | 549 } // namespace wasm |
429 } // namespace internal | 550 } // namespace internal |
430 } // namespace v8 | 551 } // namespace v8 |
431 | 552 |
432 #endif // V8_WASM_MODULE_H_ | 553 #endif // V8_WASM_MODULE_H_ |
OLD | NEW |