Commit b577ecfd authored by titzer's avatar titzer Committed by Commit bot

[wasm] Create a wrapper function for WASM.asmCompileRun().

R=bradnelson@chromium.org, ahaas@chromium.org
LOG=Y
BUG=chromium:575372

Review URL: https://codereview.chromium.org/1573213002

Cr-Commit-Position: refs/heads/master@{#33366}
parent 9ed5596a
...@@ -466,13 +466,17 @@ int32_t CompileAndRunWasmModule(Isolate* isolate, WasmModule* module) { ...@@ -466,13 +466,17 @@ int32_t CompileAndRunWasmModule(Isolate* isolate, WasmModule* module) {
// Compile all functions. // Compile all functions.
Handle<Code> main_code = Handle<Code>::null(); // record last code. Handle<Code> main_code = Handle<Code>::null(); // record last code.
int index = 0; int index = 0;
int main_index = 0;
for (const WasmFunction& func : *module->functions) { for (const WasmFunction& func : *module->functions) {
if (!func.external) { if (!func.external) {
// Compile the function and install it in the code table. // Compile the function and install it in the code table.
Handle<Code> code = compiler::CompileWasmFunction( Handle<Code> code = compiler::CompileWasmFunction(
thrower, isolate, &module_env, func, index); thrower, isolate, &module_env, func, index);
if (!code.is_null()) { if (!code.is_null()) {
if (func.exported) main_code = code; if (func.exported) {
main_code = code;
main_index = index;
}
linker.Finish(index, code); linker.Finish(index, code);
} }
if (thrower.error()) return -1; if (thrower.error()) return -1;
...@@ -480,30 +484,37 @@ int32_t CompileAndRunWasmModule(Isolate* isolate, WasmModule* module) { ...@@ -480,30 +484,37 @@ int32_t CompileAndRunWasmModule(Isolate* isolate, WasmModule* module) {
index++; index++;
} }
if (!main_code.is_null()) { if (main_code.is_null()) {
thrower.Error("WASM.compileRun() failed: no main code found");
return -1;
}
linker.Link(module_env.function_table, module->function_table); linker.Link(module_env.function_table, module->function_table);
#if USE_SIMULATOR && V8_TARGET_ARCH_ARM64
// Run the main code on arm64 simulator. // Wrap the main code so it can be called as a JS function.
Simulator* simulator = Simulator::current(isolate); Handle<String> name = isolate->factory()->NewStringFromStaticChars("main");
Simulator::CallArgument args[] = {Simulator::CallArgument(0), Handle<JSObject> module_object = Handle<JSObject>(0, isolate);
Simulator::CallArgument::End()}; Handle<JSFunction> jsfunc = compiler::CompileJSToWasmWrapper(
return static_cast<int32_t>(simulator->CallInt64(main_code->entry(), args)); isolate, &module_env, name, main_code, module_object, main_index);
#elif USE_SIMULATOR
// Run the main code on simulator. // Call the JS function.
Simulator* simulator = Simulator::current(isolate); Handle<Object> undefined(isolate->heap()->undefined_value(), isolate);
return static_cast<int32_t>( MaybeHandle<Object> retval =
simulator->Call(main_code->entry(), 4, 0, 0, 0, 0)); Execution::Call(isolate, jsfunc, undefined, 0, nullptr);
#else
// Run the main code as raw machine code. // The result should be a number.
int32_t (*raw_func)() = reinterpret_cast<int32_t (*)()>( if (retval.is_null()) {
reinterpret_cast<uintptr_t>(main_code->entry())); thrower.Error("WASM.compileRun() failed: Invocation was null");
return raw_func(); return -1;
#endif }
} else { Handle<Object> result = retval.ToHandleChecked();
// No main code was found. if (result->IsSmi()) {
isolate->Throw(*isolate->factory()->NewStringFromStaticChars( return Smi::cast(*result)->value();
"WASM.compileRun() failed: no valid main code produced.")); }
if (result->IsHeapNumber()) {
return static_cast<int32_t>(HeapNumber::cast(*result)->value());
} }
thrower.Error("WASM.compileRun() failed: Return value should be number");
return -1; return -1;
} }
} // namespace wasm } // namespace wasm
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <string.h> #include <string.h>
#include "src/wasm/encoder.h" #include "src/wasm/encoder.h"
#include "src/wasm/wasm-js.h"
#include "src/wasm/wasm-macro-gen.h" #include "src/wasm/wasm-macro-gen.h"
#include "src/wasm/wasm-module.h" #include "src/wasm/wasm-module.h"
#include "src/wasm/wasm-opcodes.h" #include "src/wasm/wasm-opcodes.h"
...@@ -18,9 +19,13 @@ using namespace v8::internal::compiler; ...@@ -18,9 +19,13 @@ using namespace v8::internal::compiler;
using namespace v8::internal::wasm; using namespace v8::internal::wasm;
#if !V8_TARGET_ARCH_ARM64
// TODO(titzer): fix arm64 frame alignment.
namespace { namespace {
void TestModule(WasmModuleIndex* module, int32_t expected_result) { void TestModule(WasmModuleIndex* module, int32_t expected_result) {
Isolate* isolate = CcTest::InitIsolateOnce(); Isolate* isolate = CcTest::InitIsolateOnce();
HandleScope scope(isolate);
WasmJs::InstallWasmFunctionMap(isolate, isolate->native_context());
int32_t result = int32_t result =
CompileAndRunWasmModule(isolate, module->Begin(), module->End()); CompileAndRunWasmModule(isolate, module->Begin(), module->End());
CHECK_EQ(expected_result, result); CHECK_EQ(expected_result, result);
...@@ -50,6 +55,8 @@ TEST(Run_WasmModule_CallAdd_rev) { ...@@ -50,6 +55,8 @@ TEST(Run_WasmModule_CallAdd_rev) {
}; };
Isolate* isolate = CcTest::InitIsolateOnce(); Isolate* isolate = CcTest::InitIsolateOnce();
HandleScope scope(isolate);
WasmJs::InstallWasmFunctionMap(isolate, isolate->native_context());
int32_t result = int32_t result =
CompileAndRunWasmModule(isolate, data, data + arraysize(data)); CompileAndRunWasmModule(isolate, data, data + arraysize(data));
CHECK_EQ(99, result); CHECK_EQ(99, result);
...@@ -197,3 +204,5 @@ TEST(Run_WasmModule_Global) { ...@@ -197,3 +204,5 @@ TEST(Run_WasmModule_Global) {
TestModule(writer->WriteTo(&zone), 97); TestModule(writer->WriteTo(&zone), 97);
} }
#endif #endif
#endif // !V8_TARGET_ARCH_ARM64
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment