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 #include "src/macro-assembler.h" | 5 #include "src/macro-assembler.h" |
| 6 #include "src/objects.h" | 6 #include "src/objects.h" |
| 7 #include "src/v8.h" | 7 #include "src/v8.h" |
| 8 | 8 |
| 9 #include "src/simulator.h" | 9 #include "src/simulator.h" |
| 10 | 10 |
| (...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 375 } | 375 } |
| 376 | 376 |
| 377 // Instantiates a wasm module as a JSObject. | 377 // Instantiates a wasm module as a JSObject. |
| 378 // * allocates a backing store of {mem_size} bytes. | 378 // * allocates a backing store of {mem_size} bytes. |
| 379 // * installs a named property "memory" for that buffer if exported | 379 // * installs a named property "memory" for that buffer if exported |
| 380 // * installs named properties on the object for exported functions | 380 // * installs named properties on the object for exported functions |
| 381 // * compiles wasm code to machine code | 381 // * compiles wasm code to machine code |
| 382 MaybeHandle<JSObject> WasmModule::Instantiate(Isolate* isolate, | 382 MaybeHandle<JSObject> WasmModule::Instantiate(Isolate* isolate, |
| 383 Handle<JSObject> ffi, | 383 Handle<JSObject> ffi, |
| 384 Handle<JSArrayBuffer> memory) { | 384 Handle<JSArrayBuffer> memory) { |
| 385 HistogramTimerScope wasm_instantiate_time_scope( | |
| 386 isolate->counters()->wasm_instantiate_time()); | |
| 385 this->shared_isolate = isolate; // TODO(titzer): have a real shared isolate. | 387 this->shared_isolate = isolate; // TODO(titzer): have a real shared isolate. |
| 386 ErrorThrower thrower(isolate, "WasmModule::Instantiate()"); | 388 ErrorThrower thrower(isolate, "WasmModule::Instantiate()"); |
| 387 Factory* factory = isolate->factory(); | 389 Factory* factory = isolate->factory(); |
| 388 | 390 |
| 389 //------------------------------------------------------------------------- | 391 //------------------------------------------------------------------------- |
| 390 // Allocate the instance and its JS counterpart. | 392 // Allocate the instance and its JS counterpart. |
| 391 //------------------------------------------------------------------------- | 393 //------------------------------------------------------------------------- |
| 392 Handle<Map> map = factory->NewMap( | 394 Handle<Map> map = factory->NewMap( |
| 393 JS_OBJECT_TYPE, | 395 JS_OBJECT_TYPE, |
| 394 JSObject::kHeaderSize + kWasmModuleInternalFieldCount * kPointerSize); | 396 JSObject::kHeaderSize + kWasmModuleInternalFieldCount * kPointerSize); |
| 395 WasmModuleInstance instance(this); | 397 WasmModuleInstance instance(this); |
| 396 instance.context = isolate->native_context(); | 398 instance.context = isolate->native_context(); |
| 397 instance.js_object = factory->NewJSObjectFromMap(map, TENURED); | 399 instance.js_object = factory->NewJSObjectFromMap(map, TENURED); |
| 398 Handle<FixedArray> code_table = | 400 Handle<FixedArray> code_table = |
| 399 factory->NewFixedArray(static_cast<int>(functions.size()), TENURED); | 401 factory->NewFixedArray(static_cast<int>(functions.size()), TENURED); |
| 400 instance.js_object->SetInternalField(kWasmModuleCodeTable, *code_table); | 402 instance.js_object->SetInternalField(kWasmModuleCodeTable, *code_table); |
| 401 | 403 |
| 402 //------------------------------------------------------------------------- | 404 //------------------------------------------------------------------------- |
| 403 // Allocate and initialize the linear memory. | 405 // Allocate and initialize the linear memory. |
| 404 //------------------------------------------------------------------------- | 406 //------------------------------------------------------------------------- |
| 407 isolate->counters()->wasm_min_mem_pages_memory()->AddSample( | |
| 408 instance.module->min_mem_pages); | |
| 409 isolate->counters()->wasm_max_mem_pages_memory()->AddSample( | |
| 410 instance.module->max_mem_pages); | |
| 405 if (memory.is_null()) { | 411 if (memory.is_null()) { |
| 406 if (!AllocateMemory(&thrower, isolate, &instance)) { | 412 if (!AllocateMemory(&thrower, isolate, &instance)) { |
| 407 return MaybeHandle<JSObject>(); | 413 return MaybeHandle<JSObject>(); |
| 408 } | 414 } |
| 409 } else { | 415 } else { |
| 410 SetMemory(&instance, memory); | 416 SetMemory(&instance, memory); |
| 411 } | 417 } |
| 412 instance.js_object->SetInternalField(kWasmMemArrayBuffer, | 418 instance.js_object->SetInternalField(kWasmMemArrayBuffer, |
| 413 *instance.mem_buffer); | 419 *instance.mem_buffer); |
| 414 LoadDataSegments(this, instance.mem_start, instance.mem_size); | 420 LoadDataSegments(this, instance.mem_start, instance.mem_size); |
| 415 | 421 |
| 416 //------------------------------------------------------------------------- | 422 //------------------------------------------------------------------------- |
| 417 // Allocate the globals area if necessary. | 423 // Allocate the globals area if necessary. |
| 418 //------------------------------------------------------------------------- | 424 //------------------------------------------------------------------------- |
| 419 if (!AllocateGlobals(&thrower, isolate, &instance)) { | 425 if (!AllocateGlobals(&thrower, isolate, &instance)) { |
| 420 return MaybeHandle<JSObject>(); | 426 return MaybeHandle<JSObject>(); |
| 421 } | 427 } |
| 422 if (!instance.globals_buffer.is_null()) { | 428 if (!instance.globals_buffer.is_null()) { |
| 423 instance.js_object->SetInternalField(kWasmGlobalsArrayBuffer, | 429 instance.js_object->SetInternalField(kWasmGlobalsArrayBuffer, |
| 424 *instance.globals_buffer); | 430 *instance.globals_buffer); |
| 425 } | 431 } |
| 426 | 432 |
| 433 HistogramTimerScope wasm_compile_time_scope( | |
| 434 isolate->counters()->wasm_compile_time()); | |
| 435 | |
| 427 //------------------------------------------------------------------------- | 436 //------------------------------------------------------------------------- |
| 428 // Compile wrappers to imported functions. | 437 // Compile wrappers to imported functions. |
| 429 //------------------------------------------------------------------------- | 438 //------------------------------------------------------------------------- |
| 430 uint32_t index = 0; | 439 uint32_t index = 0; |
| 431 instance.function_table = BuildFunctionTable(isolate, this); | 440 instance.function_table = BuildFunctionTable(isolate, this); |
| 432 WasmLinker linker(isolate, functions.size()); | 441 WasmLinker linker(isolate, functions.size()); |
| 433 ModuleEnv module_env; | 442 ModuleEnv module_env; |
| 434 module_env.module = this; | 443 module_env.module = this; |
| 435 module_env.instance = &instance; | 444 module_env.instance = &instance; |
| 436 module_env.linker = &linker; | 445 module_env.linker = &linker; |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 451 module_name, function_name); | 460 module_name, function_name); |
| 452 instance.import_code.push_back(code); | 461 instance.import_code.push_back(code); |
| 453 index++; | 462 index++; |
| 454 } | 463 } |
| 455 } | 464 } |
| 456 | 465 |
| 457 //------------------------------------------------------------------------- | 466 //------------------------------------------------------------------------- |
| 458 // Compile all functions in the module. | 467 // Compile all functions in the module. |
| 459 //------------------------------------------------------------------------- | 468 //------------------------------------------------------------------------- |
| 460 | 469 |
| 470 isolate->counters()->wasm_functions_per_module()->AddSample( | |
| 471 static_cast<int>(functions.size())); | |
| 472 | |
| 461 // First pass: compile each function and initialize the code table. | 473 // First pass: compile each function and initialize the code table. |
| 462 index = FLAG_skip_compiling_wasm_funcs; | 474 index = FLAG_skip_compiling_wasm_funcs; |
| 463 while (index < functions.size()) { | 475 while (index < functions.size()) { |
| 464 const WasmFunction& func = functions[index]; | 476 const WasmFunction& func = functions[index]; |
| 465 if (thrower.error()) break; | 477 if (thrower.error()) break; |
| 466 DCHECK_EQ(index, func.func_index); | 478 DCHECK_EQ(index, func.func_index); |
| 467 | 479 |
| 468 WasmName str = GetName(func.name_offset, func.name_length); | 480 WasmName str = GetName(func.name_offset, func.name_length); |
| 469 WasmName str_null = {nullptr, 0}; | 481 WasmName str_null = {nullptr, 0}; |
| 470 Handle<String> name = factory->InternalizeUtf8String( | 482 Handle<String> name = factory->InternalizeUtf8String( |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 537 | 549 |
| 538 if (mem_export) { | 550 if (mem_export) { |
| 539 // Export the memory as a named property. | 551 // Export the memory as a named property. |
| 540 Handle<String> name = factory->InternalizeUtf8String("memory"); | 552 Handle<String> name = factory->InternalizeUtf8String("memory"); |
| 541 JSObject::AddProperty(exports_object, name, instance.mem_buffer, | 553 JSObject::AddProperty(exports_object, name, instance.mem_buffer, |
| 542 READ_ONLY); | 554 READ_ONLY); |
| 543 } | 555 } |
| 544 } | 556 } |
| 545 | 557 |
| 546 // Run the start function if one was specified. | 558 // Run the start function if one was specified. |
| 547 if (this->start_function_index >= 0) { | 559 if (this->start_function_index >= 0) { |
|
ahaas
2016/04/11 08:03:08
Is it intended that the execution of the start fun
bradn
2016/04/13 00:22:45
Oops, done.
I've embedded it in a block, as I'd li
| |
| 548 HandleScope scope(isolate); | 560 HandleScope scope(isolate); |
| 549 uint32_t index = static_cast<uint32_t>(this->start_function_index); | 561 uint32_t index = static_cast<uint32_t>(this->start_function_index); |
| 550 Handle<String> name = isolate->factory()->NewStringFromStaticChars("start"); | 562 Handle<String> name = isolate->factory()->NewStringFromStaticChars("start"); |
| 551 Handle<Code> code = linker.GetFunctionCode(index); | 563 Handle<Code> code = linker.GetFunctionCode(index); |
| 552 Handle<JSFunction> jsfunc = compiler::CompileJSToWasmWrapper( | 564 Handle<JSFunction> jsfunc = compiler::CompileJSToWasmWrapper( |
| 553 isolate, &module_env, name, code, instance.js_object, index); | 565 isolate, &module_env, name, code, instance.js_object, index); |
| 554 | 566 |
| 555 // Call the JS function. | 567 // Call the JS function. |
| 556 Handle<Object> undefined(isolate->heap()->undefined_value(), isolate); | 568 Handle<Object> undefined(isolate->heap()->undefined_value(), isolate); |
| 557 MaybeHandle<Object> retval = | 569 MaybeHandle<Object> retval = |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 689 } | 701 } |
| 690 if (result->IsHeapNumber()) { | 702 if (result->IsHeapNumber()) { |
| 691 return static_cast<int32_t>(HeapNumber::cast(*result)->value()); | 703 return static_cast<int32_t>(HeapNumber::cast(*result)->value()); |
| 692 } | 704 } |
| 693 thrower.Error("WASM.compileRun() failed: Return value should be number"); | 705 thrower.Error("WASM.compileRun() failed: Return value should be number"); |
| 694 return -1; | 706 return -1; |
| 695 } | 707 } |
| 696 } // namespace wasm | 708 } // namespace wasm |
| 697 } // namespace internal | 709 } // namespace internal |
| 698 } // namespace v8 | 710 } // namespace v8 |
| OLD | NEW |