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()) {
linker.Link(module_env.function_table, module->function_table); thrower.Error("WASM.compileRun() failed: no main code found");
#if USE_SIMULATOR && V8_TARGET_ARCH_ARM64 return -1;
// Run the main code on arm64 simulator. }
Simulator* simulator = Simulator::current(isolate);
Simulator::CallArgument args[] = {Simulator::CallArgument(0), linker.Link(module_env.function_table, module->function_table);
Simulator::CallArgument::End()};
return static_cast<int32_t>(simulator->CallInt64(main_code->entry(), args)); // Wrap the main code so it can be called as a JS function.
#elif USE_SIMULATOR Handle<String> name = isolate->factory()->NewStringFromStaticChars("main");
// Run the main code on simulator. Handle<JSObject> module_object = Handle<JSObject>(0, isolate);
Simulator* simulator = Simulator::current(isolate); Handle<JSFunction> jsfunc = compiler::CompileJSToWasmWrapper(
return static_cast<int32_t>( isolate, &module_env, name, main_code, module_object, main_index);
simulator->Call(main_code->entry(), 4, 0, 0, 0, 0));
#else // Call the JS function.
// Run the main code as raw machine code. Handle<Object> undefined(isolate->heap()->undefined_value(), isolate);
int32_t (*raw_func)() = reinterpret_cast<int32_t (*)()>( MaybeHandle<Object> retval =
reinterpret_cast<uintptr_t>(main_code->entry())); Execution::Call(isolate, jsfunc, undefined, 0, nullptr);
return raw_func();
#endif // The result should be a number.
} else { if (retval.is_null()) {
// No main code was found. thrower.Error("WASM.compileRun() failed: Invocation was null");
isolate->Throw(*isolate->factory()->NewStringFromStaticChars( return -1;
"WASM.compileRun() failed: no valid main code produced.")); }
Handle<Object> result = retval.ToHandleChecked();
if (result->IsSmi()) {
return Smi::cast(*result)->value();
}
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