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 <memory> | 5 #include <memory> |
6 | 6 |
7 #include "src/base/atomic-utils.h" | 7 #include "src/base/atomic-utils.h" |
8 #include "src/code-stubs.h" | 8 #include "src/code-stubs.h" |
9 | 9 |
10 #include "src/macro-assembler.h" | 10 #include "src/macro-assembler.h" |
(...skipping 1915 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1926 | 1926 |
1927 bool wasm::IsWasmInstance(Object* object) { | 1927 bool wasm::IsWasmInstance(Object* object) { |
1928 return WasmInstanceObject::IsWasmInstanceObject(object); | 1928 return WasmInstanceObject::IsWasmInstanceObject(object); |
1929 } | 1929 } |
1930 | 1930 |
1931 bool wasm::WasmIsAsmJs(Object* instance, Isolate* isolate) { | 1931 bool wasm::WasmIsAsmJs(Object* instance, Isolate* isolate) { |
1932 if (instance->IsUndefined(isolate)) return false; | 1932 if (instance->IsUndefined(isolate)) return false; |
1933 DCHECK(IsWasmInstance(instance)); | 1933 DCHECK(IsWasmInstance(instance)); |
1934 WasmCompiledModule* compiled_module = | 1934 WasmCompiledModule* compiled_module = |
1935 WasmInstanceObject::cast(instance)->get_compiled_module(); | 1935 WasmInstanceObject::cast(instance)->get_compiled_module(); |
1936 DCHECK_EQ(compiled_module->has_asm_js_offset_tables(), | 1936 DCHECK_EQ(compiled_module->has_asm_js_offset_table(), |
1937 compiled_module->script()->type() == Script::TYPE_NORMAL); | 1937 compiled_module->script()->type() == Script::TYPE_NORMAL); |
1938 return compiled_module->has_asm_js_offset_tables(); | 1938 return compiled_module->has_asm_js_offset_table(); |
1939 } | 1939 } |
1940 | 1940 |
1941 Handle<Script> wasm::GetScript(Handle<JSObject> instance) { | 1941 Handle<Script> wasm::GetScript(Handle<JSObject> instance) { |
1942 WasmCompiledModule* compiled_module = | 1942 WasmCompiledModule* compiled_module = |
1943 WasmInstanceObject::cast(*instance)->get_compiled_module(); | 1943 WasmInstanceObject::cast(*instance)->get_compiled_module(); |
1944 DCHECK(compiled_module->has_script()); | 1944 DCHECK(compiled_module->has_script()); |
1945 return compiled_module->script(); | 1945 return compiled_module->script(); |
1946 } | 1946 } |
1947 | 1947 |
1948 std::pair<std::string, std::vector<std::tuple<uint32_t, int, int>>> | 1948 std::pair<std::string, std::vector<std::tuple<uint32_t, int, int>>> |
1949 wasm::DisassembleFunction(Handle<WasmCompiledModule> compiled_module, | 1949 wasm::DisassembleFunction(Handle<WasmCompiledModule> compiled_module, |
1950 int func_index) { | 1950 int func_index) { |
1951 std::ostringstream disassembly_os; | 1951 std::ostringstream disassembly_os; |
1952 std::vector<std::tuple<uint32_t, int, int>> offset_table; | 1952 std::vector<std::tuple<uint32_t, int, int>> offset_table; |
1953 | 1953 |
1954 Vector<const uint8_t> func_bytes = | 1954 Vector<const uint8_t> func_bytes = |
1955 GetFunctionBytes(compiled_module, func_index); | 1955 GetFunctionBytes(compiled_module, func_index); |
1956 DisallowHeapAllocation no_gc; | 1956 DisallowHeapAllocation no_gc; |
1957 if (func_bytes.is_empty()) return {}; | 1957 if (func_bytes.is_empty()) return {}; |
1958 | 1958 |
1959 AccountingAllocator allocator; | 1959 AccountingAllocator allocator; |
1960 bool ok = PrintAst( | 1960 bool ok = PrintAst( |
1961 &allocator, FunctionBodyForTesting(func_bytes.start(), func_bytes.end()), | 1961 &allocator, FunctionBodyForTesting(func_bytes.start(), func_bytes.end()), |
1962 disassembly_os, &offset_table); | 1962 disassembly_os, &offset_table); |
1963 CHECK(ok); | 1963 CHECK(ok); |
1964 | 1964 |
1965 return {disassembly_os.str(), std::move(offset_table)}; | 1965 return {disassembly_os.str(), std::move(offset_table)}; |
1966 } | 1966 } |
1967 | 1967 |
1968 int wasm::GetAsmWasmSourcePosition(Handle<JSObject> instance, int func_index, | |
1969 int byte_offset) { | |
1970 return WasmDebugInfo::GetAsmJsSourcePosition(GetDebugInfo(instance), | |
1971 func_index, byte_offset); | |
1972 } | |
1973 | |
1974 Handle<WasmDebugInfo> wasm::GetDebugInfo(Handle<JSObject> object) { | 1968 Handle<WasmDebugInfo> wasm::GetDebugInfo(Handle<JSObject> object) { |
1975 auto instance = Handle<WasmInstanceObject>::cast(object); | 1969 auto instance = Handle<WasmInstanceObject>::cast(object); |
1976 if (instance->has_debug_info()) { | 1970 if (instance->has_debug_info()) { |
1977 Handle<WasmDebugInfo> info(instance->get_debug_info(), | 1971 Handle<WasmDebugInfo> info(instance->get_debug_info(), |
1978 instance->GetIsolate()); | 1972 instance->GetIsolate()); |
1979 return info; | 1973 return info; |
1980 } | 1974 } |
1981 Handle<WasmDebugInfo> new_info = WasmDebugInfo::New(instance); | 1975 Handle<WasmDebugInfo> new_info = WasmDebugInfo::New(instance); |
1982 instance->set_debug_info(*new_info); | 1976 instance->set_debug_info(*new_info); |
1983 return new_info; | 1977 return new_info; |
1984 } | 1978 } |
1985 | 1979 |
1986 int wasm::GetNumberOfFunctions(Handle<JSObject> object) { | |
1987 return static_cast<int>( | |
1988 Handle<WasmInstanceObject>::cast(object)->module()->functions.size()); | |
1989 } | |
1990 | |
1991 // TODO(clemensh): origin can be inferred from asm_js_script; remove it. | 1980 // TODO(clemensh): origin can be inferred from asm_js_script; remove it. |
1992 MaybeHandle<WasmModuleObject> wasm::CreateModuleObjectFromBytes( | 1981 MaybeHandle<WasmModuleObject> wasm::CreateModuleObjectFromBytes( |
1993 Isolate* isolate, const byte* start, const byte* end, ErrorThrower* thrower, | 1982 Isolate* isolate, const byte* start, const byte* end, ErrorThrower* thrower, |
1994 ModuleOrigin origin, Handle<Script> asm_js_script, | 1983 ModuleOrigin origin, Handle<Script> asm_js_script, |
1995 const byte* asm_js_offset_tables_start, | 1984 const byte* asm_js_offset_tables_start, |
1996 const byte* asm_js_offset_tables_end) { | 1985 const byte* asm_js_offset_tables_end) { |
1997 MaybeHandle<WasmModuleObject> nothing; | 1986 MaybeHandle<WasmModuleObject> nothing; |
1998 ModuleResult result = DecodeWasmModule(isolate, start, end, false, origin); | 1987 ModuleResult result = DecodeWasmModule(isolate, start, end, false, origin); |
1999 if (result.failed()) { | 1988 if (result.failed()) { |
2000 if (result.val) delete result.val; | 1989 if (result.val) delete result.val; |
2001 thrower->CompileFailed("Wasm decoding failed", result); | 1990 thrower->CompileFailed("Wasm decoding failed", result); |
2002 return nothing; | 1991 return nothing; |
2003 } | 1992 } |
2004 // The {module_wrapper} will take ownership of the {WasmModule} object, | 1993 // The {module_wrapper} will take ownership of the {WasmModule} object, |
2005 // and it will be destroyed when the GC reclaims the wrapper object. | 1994 // and it will be destroyed when the GC reclaims the wrapper object. |
2006 Handle<WasmModuleWrapper> module_wrapper = | 1995 Handle<WasmModuleWrapper> module_wrapper = |
2007 WasmModuleWrapper::New(isolate, const_cast<WasmModule*>(result.val)); | 1996 WasmModuleWrapper::New(isolate, const_cast<WasmModule*>(result.val)); |
2008 | 1997 |
2009 // Compile the functions of the module, producing a compiled module. | 1998 // Compile the functions of the module, producing a compiled module. |
2010 MaybeHandle<WasmCompiledModule> maybe_compiled_module = | 1999 MaybeHandle<WasmCompiledModule> maybe_compiled_module = |
2011 result.val->CompileFunctions(isolate, module_wrapper, thrower); | 2000 result.val->CompileFunctions(isolate, module_wrapper, thrower); |
2012 | 2001 |
2013 if (maybe_compiled_module.is_null()) return nothing; | 2002 if (maybe_compiled_module.is_null()) return nothing; |
2014 | 2003 |
2015 Handle<WasmCompiledModule> compiled_module = | 2004 Handle<WasmCompiledModule> compiled_module = |
2016 maybe_compiled_module.ToHandleChecked(); | 2005 maybe_compiled_module.ToHandleChecked(); |
2017 | 2006 |
2018 DCHECK_EQ(origin == kAsmJsOrigin, !asm_js_script.is_null()); | 2007 DCHECK_EQ(origin == kAsmJsOrigin, !asm_js_script.is_null()); |
2019 DCHECK(!compiled_module->has_script()); | 2008 DCHECK(!compiled_module->has_script()); |
2020 DCHECK(!compiled_module->has_asm_js_offset_tables()); | 2009 DCHECK(!compiled_module->has_asm_js_offset_table()); |
2021 if (origin == kAsmJsOrigin) { | 2010 if (origin == kAsmJsOrigin) { |
2022 // Set script for the asm.js source, and the offset table mapping wasm byte | 2011 // Set script for the asm.js source, and the offset table mapping wasm byte |
2023 // offsets to source positions. | 2012 // offsets to source positions. |
2024 compiled_module->set_script(asm_js_script); | 2013 compiled_module->set_script(asm_js_script); |
2025 size_t offset_tables_len = | 2014 size_t offset_table_len = |
2026 asm_js_offset_tables_end - asm_js_offset_tables_start; | 2015 asm_js_offset_tables_end - asm_js_offset_tables_start; |
2027 DCHECK_GE(static_cast<size_t>(kMaxInt), offset_tables_len); | 2016 DCHECK_GE(static_cast<size_t>(kMaxInt), offset_table_len); |
2028 Handle<ByteArray> offset_tables = | 2017 Handle<ByteArray> offset_table = |
2029 isolate->factory()->NewByteArray(static_cast<int>(offset_tables_len)); | 2018 isolate->factory()->NewByteArray(static_cast<int>(offset_table_len)); |
2030 memcpy(offset_tables->GetDataStartAddress(), asm_js_offset_tables_start, | 2019 memcpy(offset_table->GetDataStartAddress(), asm_js_offset_tables_start, |
2031 offset_tables_len); | 2020 offset_table_len); |
2032 compiled_module->set_asm_js_offset_tables(offset_tables); | 2021 compiled_module->set_asm_js_offset_table(offset_table); |
2033 } else { | 2022 } else { |
2034 // Create a new Script object representing this wasm module, store it in the | 2023 // Create a new Script object representing this wasm module, store it in the |
2035 // compiled wasm module, and register it at the debugger. | 2024 // compiled wasm module, and register it at the debugger. |
2036 Handle<Script> script = | 2025 Handle<Script> script = |
2037 isolate->factory()->NewScript(isolate->factory()->empty_string()); | 2026 isolate->factory()->NewScript(isolate->factory()->empty_string()); |
2038 script->set_type(Script::TYPE_WASM); | 2027 script->set_type(Script::TYPE_WASM); |
2039 | 2028 |
2040 DCHECK_GE(kMaxInt, end - start); | 2029 DCHECK_GE(kMaxInt, end - start); |
2041 int hash = StringHasher::HashSequentialString( | 2030 int hash = StringHasher::HashSequentialString( |
2042 reinterpret_cast<const char*>(start), static_cast<int>(end - start), | 2031 reinterpret_cast<const char*>(start), static_cast<int>(end - start), |
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2276 MaybeHandle<String> WasmCompiledModule::GetFunctionName( | 2265 MaybeHandle<String> WasmCompiledModule::GetFunctionName( |
2277 Handle<WasmCompiledModule> compiled_module, uint32_t func_index) { | 2266 Handle<WasmCompiledModule> compiled_module, uint32_t func_index) { |
2278 DCHECK_LT(func_index, compiled_module->module()->functions.size()); | 2267 DCHECK_LT(func_index, compiled_module->module()->functions.size()); |
2279 WasmFunction& function = compiled_module->module()->functions[func_index]; | 2268 WasmFunction& function = compiled_module->module()->functions[func_index]; |
2280 Isolate* isolate = compiled_module->GetIsolate(); | 2269 Isolate* isolate = compiled_module->GetIsolate(); |
2281 MaybeHandle<String> string = ExtractStringFromModuleBytes( | 2270 MaybeHandle<String> string = ExtractStringFromModuleBytes( |
2282 isolate, compiled_module, function.name_offset, function.name_length); | 2271 isolate, compiled_module, function.name_offset, function.name_length); |
2283 if (!string.is_null()) return string.ToHandleChecked(); | 2272 if (!string.is_null()) return string.ToHandleChecked(); |
2284 return {}; | 2273 return {}; |
2285 } | 2274 } |
OLD | NEW |