Chromium Code Reviews| 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 10 matching lines...) Expand all Loading... | |
| 21 namespace internal { | 21 namespace internal { |
| 22 | 22 |
| 23 class WasmCompiledModule; | 23 class WasmCompiledModule; |
| 24 class WasmDebugInfo; | 24 class WasmDebugInfo; |
| 25 class WasmModuleObject; | 25 class WasmModuleObject; |
| 26 class WasmInstanceObject; | 26 class WasmInstanceObject; |
| 27 class WasmMemoryObject; | 27 class WasmMemoryObject; |
| 28 | 28 |
| 29 namespace compiler { | 29 namespace compiler { |
| 30 class CallDescriptor; | 30 class CallDescriptor; |
| 31 class WasmCompilationUnit; | |
| 32 } | 31 } |
| 33 | 32 |
| 34 namespace wasm { | 33 namespace wasm { |
| 35 class ErrorThrower; | 34 class ErrorThrower; |
| 36 | 35 |
| 37 const uint32_t kWasmMagic = 0x6d736100; | 36 const uint32_t kWasmMagic = 0x6d736100; |
| 38 const uint32_t kWasmVersion = 0x01; | 37 const uint32_t kWasmVersion = 0x01; |
| 39 // Legacy version supported for short transitionary period. | 38 // Legacy version supported for short transitionary period. |
| 40 const uint32_t kWasmLegacyVersion = 0x0d; | 39 const uint32_t kWasmLegacyVersion = 0x0d; |
| 41 | 40 |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 210 // invalid-semaphore error in the compilation tasks. | 209 // invalid-semaphore error in the compilation tasks. |
| 211 // TODO(wasm): Move this semaphore back to CompileInParallel when the try bots | 210 // TODO(wasm): Move this semaphore back to CompileInParallel when the try bots |
| 212 // switch to libc-2.21 or higher. | 211 // switch to libc-2.21 or higher. |
| 213 std::unique_ptr<base::Semaphore> pending_tasks; | 212 std::unique_ptr<base::Semaphore> pending_tasks; |
| 214 | 213 |
| 215 WasmModule() : WasmModule(nullptr) {} | 214 WasmModule() : WasmModule(nullptr) {} |
| 216 WasmModule(Zone* owned_zone); | 215 WasmModule(Zone* owned_zone); |
| 217 ~WasmModule() { | 216 ~WasmModule() { |
| 218 if (owned_zone) delete owned_zone; | 217 if (owned_zone) delete owned_zone; |
| 219 } | 218 } |
| 220 | |
| 221 // Creates a new instantiation of the module in the given isolate. | |
| 222 static MaybeHandle<WasmInstanceObject> Instantiate( | |
| 223 Isolate* isolate, ErrorThrower* thrower, | |
| 224 Handle<WasmModuleObject> wasm_module, Handle<JSReceiver> ffi, | |
| 225 Handle<JSArrayBuffer> memory = Handle<JSArrayBuffer>::null()); | |
| 226 | |
| 227 MaybeHandle<WasmCompiledModule> CompileFunctions( | |
| 228 Isolate* isolate, Handle<Managed<WasmModule>> module_wrapper, | |
| 229 ErrorThrower* thrower, const ModuleWireBytes& wire_bytes, | |
| 230 Handle<Script> asm_js_script, | |
| 231 Vector<const byte> asm_js_offset_table_bytes) const; | |
| 232 }; | 219 }; |
| 233 | 220 |
| 234 typedef Managed<WasmModule> WasmModuleWrapper; | 221 typedef Managed<WasmModule> WasmModuleWrapper; |
| 235 | 222 |
| 236 // An instantiated WASM module, including memory, function table, etc. | 223 // An instantiated WASM module, including memory, function table, etc. |
| 237 struct WasmInstance { | 224 struct WasmInstance { |
| 238 const WasmModule* module; // static representation of the module. | 225 const WasmModule* module; // static representation of the module. |
| 239 // -- Heap allocated -------------------------------------------------------- | 226 // -- Heap allocated -------------------------------------------------------- |
| 240 Handle<Context> context; // JavaScript native context. | 227 Handle<Context> context; // JavaScript native context. |
| 241 std::vector<Handle<FixedArray>> function_tables; // indirect function tables. | 228 std::vector<Handle<FixedArray>> function_tables; // indirect function tables. |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 253 function_tables(m->function_tables.size()), | 240 function_tables(m->function_tables.size()), |
| 254 signature_tables(m->function_tables.size()), | 241 signature_tables(m->function_tables.size()), |
| 255 function_code(m->functions.size()) {} | 242 function_code(m->functions.size()) {} |
| 256 }; | 243 }; |
| 257 | 244 |
| 258 // Interface to the storage (wire bytes) of a wasm module. | 245 // Interface to the storage (wire bytes) of a wasm module. |
| 259 // It is illegal for anyone receiving a ModuleWireBytes to store pointers based | 246 // It is illegal for anyone receiving a ModuleWireBytes to store pointers based |
| 260 // on module_bytes, as this storage is only guaranteed to be alive as long as | 247 // on module_bytes, as this storage is only guaranteed to be alive as long as |
| 261 // this struct is alive. | 248 // this struct is alive. |
| 262 struct V8_EXPORT_PRIVATE ModuleWireBytes { | 249 struct V8_EXPORT_PRIVATE ModuleWireBytes { |
| 250 ModuleWireBytes(Handle<Object> handle, const byte* start, const byte* end) | |
| 251 : handle_(handle), module_bytes_(start, static_cast<int>(end - start)) { | |
| 252 DCHECK_GE(kMaxInt, end - start); | |
| 253 } | |
| 263 ModuleWireBytes(Vector<const byte> module_bytes) | 254 ModuleWireBytes(Vector<const byte> module_bytes) |
|
ahaas
2017/02/17 13:02:29
I think this constructor should use the explicit k
| |
| 264 : module_bytes_(module_bytes) {} | 255 : handle_(Handle<Object>::null()), module_bytes_(module_bytes) {} |
|
Clemens Hammacher
2017/02/16 20:36:58
This is default-initialized, you can skip it here.
| |
| 265 ModuleWireBytes(const byte* start, const byte* end) | 256 ModuleWireBytes(const byte* start, const byte* end) |
| 266 : module_bytes_(start, static_cast<int>(end - start)) { | 257 : handle_(Handle<Object>::null()), |
|
Clemens Hammacher
2017/02/16 20:36:58
Same here.
| |
| 258 module_bytes_(start, static_cast<int>(end - start)) { | |
| 267 DCHECK_GE(kMaxInt, end - start); | 259 DCHECK_GE(kMaxInt, end - start); |
| 268 } | 260 } |
| 269 | 261 |
| 270 // Get a string stored in the module bytes representing a name. | 262 // Get a string stored in the module bytes representing a name. |
| 271 WasmName GetName(uint32_t offset, uint32_t length) const { | 263 WasmName GetName(uint32_t offset, uint32_t length) const { |
| 272 if (length == 0) return {"<?>", 3}; // no name. | 264 if (length == 0) return {"<?>", 3}; // no name. |
| 273 CHECK(BoundsCheck(offset, length)); | 265 CHECK(BoundsCheck(offset, length)); |
| 274 DCHECK_GE(length, 0); | 266 DCHECK_GE(length, 0); |
| 275 return Vector<const char>::cast( | 267 return Vector<const char>::cast( |
| 276 module_bytes_.SubVector(offset, offset + length)); | 268 module_bytes_.SubVector(offset, offset + length)); |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 304 Vector<const byte> GetFunctionBytes(const WasmFunction* function) const { | 296 Vector<const byte> GetFunctionBytes(const WasmFunction* function) const { |
| 305 return module_bytes_.SubVector(function->code_start_offset, | 297 return module_bytes_.SubVector(function->code_start_offset, |
| 306 function->code_end_offset); | 298 function->code_end_offset); |
| 307 } | 299 } |
| 308 | 300 |
| 309 const byte* start() const { return module_bytes_.start(); } | 301 const byte* start() const { return module_bytes_.start(); } |
| 310 const byte* end() const { return module_bytes_.end(); } | 302 const byte* end() const { return module_bytes_.end(); } |
| 311 int length() const { return module_bytes_.length(); } | 303 int length() const { return module_bytes_.length(); } |
| 312 | 304 |
| 313 private: | 305 private: |
| 306 Handle<Object> handle_; | |
|
Clemens Hammacher
2017/02/16 20:36:58
This field seems to be unused.
If I am wrong, can
| |
| 314 const Vector<const byte> module_bytes_; | 307 const Vector<const byte> module_bytes_; |
| 315 }; | 308 }; |
| 316 | 309 |
| 317 // Interface provided to the decoder/graph builder which contains only | 310 // Interface provided to the decoder/graph builder which contains only |
| 318 // minimal information about the globals, functions, and function tables. | 311 // minimal information about the globals, functions, and function tables. |
| 319 struct V8_EXPORT_PRIVATE ModuleEnv { | 312 struct V8_EXPORT_PRIVATE ModuleEnv { |
| 320 ModuleEnv(const WasmModule* module, WasmInstance* instance) | 313 ModuleEnv(const WasmModule* module, WasmInstance* instance) |
| 321 : module(module), instance(instance) {} | 314 : module(module), instance(instance) {} |
| 322 | 315 |
| 323 const WasmModule* module; | 316 const WasmModule* module; |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 352 return &module->function_tables[index]; | 345 return &module->function_tables[index]; |
| 353 } | 346 } |
| 354 | 347 |
| 355 bool asm_js() { return module->origin == kAsmJsOrigin; } | 348 bool asm_js() { return module->origin == kAsmJsOrigin; } |
| 356 | 349 |
| 357 Handle<Code> GetFunctionCode(uint32_t index) { | 350 Handle<Code> GetFunctionCode(uint32_t index) { |
| 358 DCHECK_NOT_NULL(instance); | 351 DCHECK_NOT_NULL(instance); |
| 359 return instance->function_code[index]; | 352 return instance->function_code[index]; |
| 360 } | 353 } |
| 361 | 354 |
| 355 // TODO(titzer): move these into src/compiler/wasm-compiler.cc | |
| 362 static compiler::CallDescriptor* GetWasmCallDescriptor(Zone* zone, | 356 static compiler::CallDescriptor* GetWasmCallDescriptor(Zone* zone, |
| 363 FunctionSig* sig); | 357 FunctionSig* sig); |
| 364 static compiler::CallDescriptor* GetI32WasmCallDescriptor( | 358 static compiler::CallDescriptor* GetI32WasmCallDescriptor( |
| 365 Zone* zone, compiler::CallDescriptor* descriptor); | 359 Zone* zone, compiler::CallDescriptor* descriptor); |
| 366 static compiler::CallDescriptor* GetI32WasmCallDescriptorForSimd( | 360 static compiler::CallDescriptor* GetI32WasmCallDescriptorForSimd( |
| 367 Zone* zone, compiler::CallDescriptor* descriptor); | 361 Zone* zone, compiler::CallDescriptor* descriptor); |
| 368 }; | 362 }; |
| 369 | 363 |
| 370 // A ModuleEnv together with ModuleWireBytes. | 364 // A ModuleEnv together with ModuleWireBytes. |
| 371 struct ModuleBytesEnv { | 365 struct ModuleBytesEnv { |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 418 Handle<Context> context); | 412 Handle<Context> context); |
| 419 | 413 |
| 420 V8_EXPORT_PRIVATE Handle<JSArray> GetImports(Isolate* isolate, | 414 V8_EXPORT_PRIVATE Handle<JSArray> GetImports(Isolate* isolate, |
| 421 Handle<WasmModuleObject> module); | 415 Handle<WasmModuleObject> module); |
| 422 V8_EXPORT_PRIVATE Handle<JSArray> GetExports(Isolate* isolate, | 416 V8_EXPORT_PRIVATE Handle<JSArray> GetExports(Isolate* isolate, |
| 423 Handle<WasmModuleObject> module); | 417 Handle<WasmModuleObject> module); |
| 424 V8_EXPORT_PRIVATE Handle<JSArray> GetCustomSections( | 418 V8_EXPORT_PRIVATE Handle<JSArray> GetCustomSections( |
| 425 Isolate* isolate, Handle<WasmModuleObject> module, Handle<String> name, | 419 Isolate* isolate, Handle<WasmModuleObject> module, Handle<String> name, |
| 426 ErrorThrower* thrower); | 420 ErrorThrower* thrower); |
| 427 | 421 |
| 428 V8_EXPORT_PRIVATE bool ValidateModuleBytes(Isolate* isolate, const byte* start, | |
| 429 const byte* end, | |
| 430 ErrorThrower* thrower, | |
| 431 ModuleOrigin origin); | |
| 432 | |
| 433 // Get the offset of the code of a function within a module. | 422 // Get the offset of the code of a function within a module. |
| 434 int GetFunctionCodeOffset(Handle<WasmCompiledModule> compiled_module, | 423 int GetFunctionCodeOffset(Handle<WasmCompiledModule> compiled_module, |
| 435 int func_index); | 424 int func_index); |
| 436 | 425 |
| 437 // Assumed to be called with a code object associated to a wasm module instance. | 426 // Assumed to be called with a code object associated to a wasm module instance. |
| 438 // Intended to be called from runtime functions. | 427 // Intended to be called from runtime functions. |
| 439 // Returns nullptr on failing to get owning instance. | 428 // Returns nullptr on failing to get owning instance. |
| 440 WasmInstanceObject* GetOwningWasmInstance(Code* code); | 429 WasmInstanceObject* GetOwningWasmInstance(Code* code); |
| 441 | 430 |
| 442 MaybeHandle<JSArrayBuffer> GetInstanceMemory( | 431 MaybeHandle<JSArrayBuffer> GetInstanceMemory( |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 457 | 446 |
| 458 int32_t GrowMemory(Isolate* isolate, Handle<WasmInstanceObject> instance, | 447 int32_t GrowMemory(Isolate* isolate, Handle<WasmInstanceObject> instance, |
| 459 uint32_t pages); | 448 uint32_t pages); |
| 460 | 449 |
| 461 void UpdateDispatchTables(Isolate* isolate, Handle<FixedArray> dispatch_tables, | 450 void UpdateDispatchTables(Isolate* isolate, Handle<FixedArray> dispatch_tables, |
| 462 int index, Handle<JSFunction> js_function); | 451 int index, Handle<JSFunction> js_function); |
| 463 | 452 |
| 464 void GrowDispatchTables(Isolate* isolate, Handle<FixedArray> dispatch_tables, | 453 void GrowDispatchTables(Isolate* isolate, Handle<FixedArray> dispatch_tables, |
| 465 uint32_t old_size, uint32_t count); | 454 uint32_t old_size, uint32_t count); |
| 466 | 455 |
| 456 //============================================================================ | |
| 457 //== Compilation and instantiation =========================================== | |
| 458 //============================================================================ | |
| 459 V8_EXPORT_PRIVATE bool SyncValidate(Isolate* isolate, ErrorThrower* thrower, | |
| 460 const ModuleWireBytes& bytes); | |
| 461 | |
| 462 V8_EXPORT_PRIVATE MaybeHandle<WasmModuleObject> SyncCompileTranslatedAsmJs( | |
| 463 Isolate* isolate, ErrorThrower* thrower, const ModuleWireBytes& bytes, | |
| 464 Handle<Script> asm_js_script, Vector<const byte> asm_js_offset_table_bytes); | |
| 465 | |
| 466 V8_EXPORT_PRIVATE MaybeHandle<WasmModuleObject> SyncCompile( | |
| 467 Isolate* isolate, ErrorThrower* thrower, const ModuleWireBytes& bytes); | |
| 468 | |
| 469 V8_EXPORT_PRIVATE MaybeHandle<WasmInstanceObject> SyncInstantiate( | |
| 470 Isolate* isolate, ErrorThrower* thrower, | |
| 471 Handle<WasmModuleObject> module_object, MaybeHandle<JSReceiver> imports, | |
| 472 MaybeHandle<JSArrayBuffer> memory); | |
| 473 | |
| 474 V8_EXPORT_PRIVATE void AsyncCompile(Isolate* isolate, Handle<JSPromise> promise, | |
| 475 const ModuleWireBytes& bytes); | |
| 476 | |
| 477 V8_EXPORT_PRIVATE void AsyncInstantiate(Isolate* isolate, | |
| 478 Handle<JSPromise> promise, | |
| 479 Handle<WasmModuleObject> module_object, | |
| 480 MaybeHandle<JSReceiver> imports); | |
| 481 | |
| 482 V8_EXPORT_PRIVATE void AsyncCompileAndInstantiate( | |
| 483 Isolate* isolate, Handle<JSPromise> promise, const ModuleWireBytes& bytes, | |
| 484 void* unused, MaybeHandle<JSReceiver> imports); | |
|
Clemens Hammacher
2017/02/16 20:36:58
Wut? "void* unused"? Why is this needed? The compi
| |
| 485 | |
| 486 V8_EXPORT_PRIVATE void AsyncCompileAndInstantiate( | |
| 487 Isolate* isolate, Handle<JSPromise> promise, | |
| 488 Handle<WasmModuleObject> module, MaybeHandle<JSReceiver> imports); | |
| 489 | |
| 467 namespace testing { | 490 namespace testing { |
| 468 | |
| 469 void ValidateInstancesChain(Isolate* isolate, | 491 void ValidateInstancesChain(Isolate* isolate, |
| 470 Handle<WasmModuleObject> module_obj, | 492 Handle<WasmModuleObject> module_obj, |
| 471 int instance_count); | 493 int instance_count); |
| 472 void ValidateModuleState(Isolate* isolate, Handle<WasmModuleObject> module_obj); | 494 void ValidateModuleState(Isolate* isolate, Handle<WasmModuleObject> module_obj); |
| 473 void ValidateOrphanedInstance(Isolate* isolate, | 495 void ValidateOrphanedInstance(Isolate* isolate, |
| 474 Handle<WasmInstanceObject> instance); | 496 Handle<WasmInstanceObject> instance); |
| 475 | |
| 476 } // namespace testing | 497 } // namespace testing |
| 477 } // namespace wasm | 498 } // namespace wasm |
| 478 } // namespace internal | 499 } // namespace internal |
| 479 } // namespace v8 | 500 } // namespace v8 |
| 480 | 501 |
| 481 #endif // V8_WASM_MODULE_H_ | 502 #endif // V8_WASM_MODULE_H_ |
| OLD | NEW |