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 448 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
459 module_env.function_table = BuildFunctionTable(isolate, module); | 459 module_env.function_table = BuildFunctionTable(isolate, module); |
460 module_env.asm_js = false; | 460 module_env.asm_js = false; |
461 | 461 |
462 // Load data segments. | 462 // Load data segments. |
463 // TODO(titzer): throw instead of crashing if segments don't fit in memory? | 463 // TODO(titzer): throw instead of crashing if segments don't fit in memory? |
464 LoadDataSegments(module, mem_addr.get(), mem_size); | 464 LoadDataSegments(module, mem_addr.get(), mem_size); |
465 | 465 |
466 // Compile all functions. | 466 // Compile all functions. |
467 Handle<Code> main_code = Handle<Code>::null(); // record last code. | 467 Handle<Code> main_code = Handle<Code>::null(); // record last code. |
468 int index = 0; | 468 int index = 0; |
| 469 int main_index = 0; |
469 for (const WasmFunction& func : *module->functions) { | 470 for (const WasmFunction& func : *module->functions) { |
470 if (!func.external) { | 471 if (!func.external) { |
471 // Compile the function and install it in the code table. | 472 // Compile the function and install it in the code table. |
472 Handle<Code> code = compiler::CompileWasmFunction( | 473 Handle<Code> code = compiler::CompileWasmFunction( |
473 thrower, isolate, &module_env, func, index); | 474 thrower, isolate, &module_env, func, index); |
474 if (!code.is_null()) { | 475 if (!code.is_null()) { |
475 if (func.exported) main_code = code; | 476 if (func.exported) { |
| 477 main_code = code; |
| 478 main_index = index; |
| 479 } |
476 linker.Finish(index, code); | 480 linker.Finish(index, code); |
477 } | 481 } |
478 if (thrower.error()) return -1; | 482 if (thrower.error()) return -1; |
479 } | 483 } |
480 index++; | 484 index++; |
481 } | 485 } |
482 | 486 |
483 if (!main_code.is_null()) { | 487 if (main_code.is_null()) { |
484 linker.Link(module_env.function_table, module->function_table); | 488 thrower.Error("WASM.compileRun() failed: no main code found"); |
485 #if USE_SIMULATOR && V8_TARGET_ARCH_ARM64 | 489 return -1; |
486 // Run the main code on arm64 simulator. | |
487 Simulator* simulator = Simulator::current(isolate); | |
488 Simulator::CallArgument args[] = {Simulator::CallArgument(0), | |
489 Simulator::CallArgument::End()}; | |
490 return static_cast<int32_t>(simulator->CallInt64(main_code->entry(), args)); | |
491 #elif USE_SIMULATOR | |
492 // Run the main code on simulator. | |
493 Simulator* simulator = Simulator::current(isolate); | |
494 return static_cast<int32_t>( | |
495 simulator->Call(main_code->entry(), 4, 0, 0, 0, 0)); | |
496 #else | |
497 // Run the main code as raw machine code. | |
498 int32_t (*raw_func)() = reinterpret_cast<int32_t (*)()>( | |
499 reinterpret_cast<uintptr_t>(main_code->entry())); | |
500 return raw_func(); | |
501 #endif | |
502 } else { | |
503 // No main code was found. | |
504 isolate->Throw(*isolate->factory()->NewStringFromStaticChars( | |
505 "WASM.compileRun() failed: no valid main code produced.")); | |
506 } | 490 } |
| 491 |
| 492 linker.Link(module_env.function_table, module->function_table); |
| 493 |
| 494 // Wrap the main code so it can be called as a JS function. |
| 495 Handle<String> name = isolate->factory()->NewStringFromStaticChars("main"); |
| 496 Handle<JSObject> module_object = Handle<JSObject>(0, isolate); |
| 497 Handle<JSFunction> jsfunc = compiler::CompileJSToWasmWrapper( |
| 498 isolate, &module_env, name, main_code, module_object, main_index); |
| 499 |
| 500 // Call the JS function. |
| 501 Handle<Object> undefined(isolate->heap()->undefined_value(), isolate); |
| 502 MaybeHandle<Object> retval = |
| 503 Execution::Call(isolate, jsfunc, undefined, 0, nullptr); |
| 504 |
| 505 // The result should be a number. |
| 506 if (retval.is_null()) { |
| 507 thrower.Error("WASM.compileRun() failed: Invocation was null"); |
| 508 return -1; |
| 509 } |
| 510 Handle<Object> result = retval.ToHandleChecked(); |
| 511 if (result->IsSmi()) { |
| 512 return Smi::cast(*result)->value(); |
| 513 } |
| 514 if (result->IsHeapNumber()) { |
| 515 return static_cast<int32_t>(HeapNumber::cast(*result)->value()); |
| 516 } |
| 517 thrower.Error("WASM.compileRun() failed: Return value should be number"); |
507 return -1; | 518 return -1; |
508 } | 519 } |
509 } // namespace wasm | 520 } // namespace wasm |
510 } // namespace internal | 521 } // namespace internal |
511 } // namespace v8 | 522 } // namespace v8 |
OLD | NEW |