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 | |
164 // Static representation of a module. | 162 // Static representation of a module. |
165 struct WasmModule { | 163 struct WasmModule { |
166 static const uint32_t kPageSize = 0x10000; // Page size, 64kb. | 164 static const uint32_t kPageSize = 0x10000; // Page size, 64kb. |
167 static const uint32_t kMaxLegalPages = 65536; // Maximum legal pages | 165 static const uint32_t kMaxLegalPages = 65536; // Maximum legal pages |
168 static const uint32_t kMinMemPages = 1; // Minimum memory size = 64kb | 166 static const uint32_t kMinMemPages = 1; // Minimum memory size = 64kb |
169 static const uint32_t kMaxMemPages = 16384; // Maximum memory size = 1gb | 167 static const uint32_t kMaxMemPages = 16384; // Maximum memory size = 1gb |
170 | 168 |
171 const byte* module_start; // starting address for the module bytes. | 169 const byte* module_start; // starting address for the module bytes. |
172 const byte* module_end; // end address for the module bytes. | 170 const byte* module_end; // end address for the module bytes. |
173 uint32_t min_mem_pages; // minimum size of the memory in 64k pages. | 171 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... |
235 bool BoundsCheck(uint32_t start, uint32_t end) const { | 233 bool BoundsCheck(uint32_t start, uint32_t end) const { |
236 size_t size = module_end - module_start; | 234 size_t size = module_end - module_start; |
237 return start <= size && end <= size; | 235 return start <= size && end <= size; |
238 } | 236 } |
239 | 237 |
240 // Creates a new instantiation of the module in the given isolate. | 238 // Creates a new instantiation of the module in the given isolate. |
241 V8_EXPORT_PRIVATE static MaybeHandle<JSObject> Instantiate( | 239 V8_EXPORT_PRIVATE static MaybeHandle<JSObject> Instantiate( |
242 Isolate* isolate, ErrorThrower* thrower, Handle<JSObject> module_object, | 240 Isolate* isolate, ErrorThrower* thrower, Handle<JSObject> module_object, |
243 Handle<JSReceiver> ffi, Handle<JSArrayBuffer> memory); | 241 Handle<JSReceiver> ffi, Handle<JSArrayBuffer> memory); |
244 | 242 |
245 MaybeHandle<WasmCompiledModule> CompileFunctions(Isolate* isolate, | 243 MaybeHandle<FixedArray> CompileFunctions(Isolate* isolate, |
246 ErrorThrower* thrower) const; | 244 ErrorThrower* thrower) const; |
247 | 245 |
248 private: | 246 private: |
249 DISALLOW_COPY_AND_ASSIGN(WasmModule); | 247 DISALLOW_COPY_AND_ASSIGN(WasmModule); |
250 }; | 248 }; |
251 | 249 |
252 // An instantiated WASM module, including memory, function table, etc. | 250 // An instantiated WASM module, including memory, function table, etc. |
253 struct WasmModuleInstance { | 251 struct WasmModuleInstance { |
254 const WasmModule* module; // static representation of the module. | 252 const WasmModule* module; // static representation of the module. |
255 // -- Heap allocated -------------------------------------------------------- | 253 // -- Heap allocated -------------------------------------------------------- |
256 Handle<JSObject> js_object; // JavaScript module object. | 254 Handle<JSObject> js_object; // JavaScript module object. |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 | 332 |
335 std::ostream& operator<<(std::ostream& os, const WasmModule& module); | 333 std::ostream& operator<<(std::ostream& os, const WasmModule& module); |
336 std::ostream& operator<<(std::ostream& os, const WasmFunction& function); | 334 std::ostream& operator<<(std::ostream& os, const WasmFunction& function); |
337 std::ostream& operator<<(std::ostream& os, const WasmFunctionName& name); | 335 std::ostream& operator<<(std::ostream& os, const WasmFunctionName& name); |
338 | 336 |
339 typedef Result<const WasmModule*> ModuleResult; | 337 typedef Result<const WasmModule*> ModuleResult; |
340 typedef Result<WasmFunction*> FunctionResult; | 338 typedef Result<WasmFunction*> FunctionResult; |
341 typedef std::vector<std::pair<int, int>> FunctionOffsets; | 339 typedef std::vector<std::pair<int, int>> FunctionOffsets; |
342 typedef Result<FunctionOffsets> FunctionOffsetsResult; | 340 typedef Result<FunctionOffsets> FunctionOffsetsResult; |
343 | 341 |
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 #define DECLARATION(KIND, TYPE, NAME) WCM_##KIND(TYPE, NAME) | |
446 WCM_PROPERTY_TABLE(DECLARATION) | |
447 #undef DECLARATION | |
448 | |
449 private: | |
450 DISALLOW_IMPLICIT_CONSTRUCTORS(WasmCompiledModule); | |
451 }; | |
452 | |
453 // Extract a function name from the given wasm object. | 342 // Extract a function name from the given wasm object. |
454 // Returns "<WASM UNNAMED>" if the function is unnamed or the name is not a | 343 // Returns "<WASM UNNAMED>" if the function is unnamed or the name is not a |
455 // valid UTF-8 string. | 344 // valid UTF-8 string. |
456 Handle<String> GetWasmFunctionName(Isolate* isolate, Handle<Object> wasm, | 345 Handle<String> GetWasmFunctionName(Isolate* isolate, Handle<Object> wasm, |
457 uint32_t func_index); | 346 uint32_t func_index); |
458 | 347 |
459 // Extract a function name from the given wasm object. | 348 // Extract a function name from the given wasm object. |
460 // Returns a null handle if the function is unnamed or the name is not a valid | 349 // Returns a null handle if the function is unnamed or the name is not a valid |
461 // UTF-8 string. | 350 // UTF-8 string. |
462 Handle<Object> GetWasmFunctionNameOrNull(Isolate* isolate, Handle<Object> wasm, | 351 Handle<Object> GetWasmFunctionNameOrNull(Isolate* isolate, Handle<Object> wasm, |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
534 int instance_count); | 423 int instance_count); |
535 void ValidateModuleState(Isolate* isolate, Handle<JSObject> module_obj); | 424 void ValidateModuleState(Isolate* isolate, Handle<JSObject> module_obj); |
536 void ValidateOrphanedInstance(Isolate* isolate, Handle<JSObject> instance); | 425 void ValidateOrphanedInstance(Isolate* isolate, Handle<JSObject> instance); |
537 | 426 |
538 } // namespace testing | 427 } // namespace testing |
539 } // namespace wasm | 428 } // namespace wasm |
540 } // namespace internal | 429 } // namespace internal |
541 } // namespace v8 | 430 } // namespace v8 |
542 | 431 |
543 #endif // V8_WASM_MODULE_H_ | 432 #endif // V8_WASM_MODULE_H_ |
OLD | NEW |