Commit 8d190053 authored by clemensh's avatar clemensh Committed by Commit bot

[wasm] Clean up wasm module implementation

By defining functions with namespace prefix, the compiler checks that
they were previously declared, and checks that the signature matches.
I stumbled across this several times when changing the interface of a
function in the header.
With this change you get a compile error right away instead of a linker
error in the very end.

This change also revealed two functions which could be placed in an
anonymous namespace, saving 5.5kB program size in Debug build, 2.3kB in
Optdebug and 0.3kB in Release. It's also opening more options for
compiler optimizations, as the functions now have internal linkage.

R=titzer@chromium.org

Review-Url: https://codereview.chromium.org/2415603002
Cr-Commit-Position: refs/heads/master@{#40233}
parent 610fe289
...@@ -24,9 +24,9 @@ ...@@ -24,9 +24,9 @@
#include "src/compiler/wasm-compiler.h" #include "src/compiler/wasm-compiler.h"
namespace v8 { using namespace v8::internal;
namespace internal { using namespace v8::internal::wasm;
namespace wasm { namespace base = v8::base;
#define TRACE(...) \ #define TRACE(...) \
do { \ do { \
...@@ -756,7 +756,7 @@ Handle<FixedArray> SetupIndirectFunctionTable( ...@@ -756,7 +756,7 @@ Handle<FixedArray> SetupIndirectFunctionTable(
} // namespace } // namespace
const char* SectionName(WasmSectionCode code) { const char* wasm::SectionName(WasmSectionCode code) {
switch (code) { switch (code) {
case kUnknownSectionCode: case kUnknownSectionCode:
return "Unknown"; return "Unknown";
...@@ -789,7 +789,7 @@ const char* SectionName(WasmSectionCode code) { ...@@ -789,7 +789,7 @@ const char* SectionName(WasmSectionCode code) {
} }
} }
std::ostream& operator<<(std::ostream& os, const WasmModule& module) { std::ostream& wasm::operator<<(std::ostream& os, const WasmModule& module) {
os << "WASM module with "; os << "WASM module with ";
os << (module.min_mem_pages * module.kPageSize) << " min mem"; os << (module.min_mem_pages * module.kPageSize) << " min mem";
os << (module.max_mem_pages * module.kPageSize) << " max mem"; os << (module.max_mem_pages * module.kPageSize) << " max mem";
...@@ -799,7 +799,7 @@ std::ostream& operator<<(std::ostream& os, const WasmModule& module) { ...@@ -799,7 +799,7 @@ std::ostream& operator<<(std::ostream& os, const WasmModule& module) {
return os; return os;
} }
std::ostream& operator<<(std::ostream& os, const WasmFunction& function) { std::ostream& wasm::operator<<(std::ostream& os, const WasmFunction& function) {
os << "WASM function with signature " << *function.sig; os << "WASM function with signature " << *function.sig;
os << " code bytes: " os << " code bytes: "
...@@ -807,7 +807,7 @@ std::ostream& operator<<(std::ostream& os, const WasmFunction& function) { ...@@ -807,7 +807,7 @@ std::ostream& operator<<(std::ostream& os, const WasmFunction& function) {
return os; return os;
} }
std::ostream& operator<<(std::ostream& os, const WasmFunctionName& pair) { std::ostream& wasm::operator<<(std::ostream& os, const WasmFunctionName& pair) {
os << "#" << pair.function_->func_index << ":"; os << "#" << pair.function_->func_index << ":";
if (pair.function_->name_offset > 0) { if (pair.function_->name_offset > 0) {
if (pair.module_) { if (pair.module_) {
...@@ -823,7 +823,7 @@ std::ostream& operator<<(std::ostream& os, const WasmFunctionName& pair) { ...@@ -823,7 +823,7 @@ std::ostream& operator<<(std::ostream& os, const WasmFunctionName& pair) {
return os; return os;
} }
Handle<JSFunction> WrapExportCodeAsJSFunction( Handle<JSFunction> wasm::WrapExportCodeAsJSFunction(
Isolate* isolate, Handle<Code> export_code, Handle<String> name, int arity, Isolate* isolate, Handle<Code> export_code, Handle<String> name, int arity,
MaybeHandle<ByteArray> maybe_signature, Handle<JSObject> module_instance) { MaybeHandle<ByteArray> maybe_signature, Handle<JSObject> module_instance) {
Handle<SharedFunctionInfo> shared = Handle<SharedFunctionInfo> shared =
...@@ -845,7 +845,7 @@ Handle<JSFunction> WrapExportCodeAsJSFunction( ...@@ -845,7 +845,7 @@ Handle<JSFunction> WrapExportCodeAsJSFunction(
return function; return function;
} }
Object* GetOwningWasmInstance(Code* code) { Object* wasm::GetOwningWasmInstance(Code* code) {
DCHECK(code->kind() == Code::WASM_FUNCTION); DCHECK(code->kind() == Code::WASM_FUNCTION);
DisallowHeapAllocation no_gc; DisallowHeapAllocation no_gc;
FixedArray* deopt_data = code->deoptimization_data(); FixedArray* deopt_data = code->deoptimization_data();
...@@ -857,7 +857,7 @@ Object* GetOwningWasmInstance(Code* code) { ...@@ -857,7 +857,7 @@ Object* GetOwningWasmInstance(Code* code) {
return cell->value(); return cell->value();
} }
int GetNumImportedFunctions(Handle<JSObject> wasm_object) { int wasm::GetNumImportedFunctions(Handle<JSObject> wasm_object) {
// TODO(wasm): Cache this number if it ever becomes a performance problem. // TODO(wasm): Cache this number if it ever becomes a performance problem.
DCHECK(IsWasmObject(*wasm_object)); DCHECK(IsWasmObject(*wasm_object));
WasmCompiledModule* compiled_module = GetCompiledModule(*wasm_object); WasmCompiledModule* compiled_module = GetCompiledModule(*wasm_object);
...@@ -887,6 +887,8 @@ WasmModule::WasmModule(byte* module_start) ...@@ -887,6 +887,8 @@ WasmModule::WasmModule(byte* module_start)
num_exported_functions(0), num_exported_functions(0),
pending_tasks(new base::Semaphore(0)) {} pending_tasks(new base::Semaphore(0)) {}
namespace {
void EncodeInit(const WasmModule* module, Factory* factory, void EncodeInit(const WasmModule* module, Factory* factory,
Handle<FixedArray> entry, int kind_index, int value_index, Handle<FixedArray> entry, int kind_index, int value_index,
const WasmInitExpr& expr) { const WasmInitExpr& expr) {
...@@ -923,6 +925,8 @@ void EncodeInit(const WasmModule* module, Factory* factory, ...@@ -923,6 +925,8 @@ void EncodeInit(const WasmModule* module, Factory* factory,
entry->set(value_index, *value); entry->set(value_index, *value);
} }
} // namespace
MaybeHandle<WasmCompiledModule> WasmModule::CompileFunctions( MaybeHandle<WasmCompiledModule> WasmModule::CompileFunctions(
Isolate* isolate, ErrorThrower* thrower) const { Isolate* isolate, ErrorThrower* thrower) const {
Factory* factory = isolate->factory(); Factory* factory = isolate->factory();
...@@ -1867,7 +1871,7 @@ class WasmInstanceBuilder { ...@@ -1867,7 +1871,7 @@ class WasmInstanceBuilder {
break; break;
} }
Maybe<bool> status = JSReceiver::DefineOwnProperty( v8::Maybe<bool> status = JSReceiver::DefineOwnProperty(
isolate_, exports_object, name, &desc, Object::THROW_ON_ERROR); isolate_, exports_object, name, &desc, Object::THROW_ON_ERROR);
if (!status.IsJust()) { if (!status.IsJust()) {
thrower_->Error("export of %.*s failed.", name->length(), thrower_->Error("export of %.*s failed.", name->length(),
...@@ -1955,8 +1959,9 @@ void WasmCompiledModule::PrintInstancesChain() { ...@@ -1955,8 +1959,9 @@ void WasmCompiledModule::PrintInstancesChain() {
#endif #endif
} }
Handle<Object> GetWasmFunctionNameOrNull(Isolate* isolate, Handle<Object> wasm, Handle<Object> wasm::GetWasmFunctionNameOrNull(Isolate* isolate,
uint32_t func_index) { Handle<Object> wasm,
uint32_t func_index) {
if (!wasm->IsUndefined(isolate)) { if (!wasm->IsUndefined(isolate)) {
DCHECK(IsWasmObject(*wasm)); DCHECK(IsWasmObject(*wasm));
WasmCompiledModule* compiled_module = WasmCompiledModule* compiled_module =
...@@ -1972,8 +1977,8 @@ Handle<Object> GetWasmFunctionNameOrNull(Isolate* isolate, Handle<Object> wasm, ...@@ -1972,8 +1977,8 @@ Handle<Object> GetWasmFunctionNameOrNull(Isolate* isolate, Handle<Object> wasm,
return isolate->factory()->null_value(); return isolate->factory()->null_value();
} }
Handle<String> GetWasmFunctionName(Isolate* isolate, Handle<Object> wasm, Handle<String> wasm::GetWasmFunctionName(Isolate* isolate, Handle<Object> wasm,
uint32_t func_index) { uint32_t func_index) {
Handle<Object> name_or_null = Handle<Object> name_or_null =
GetWasmFunctionNameOrNull(isolate, wasm, func_index); GetWasmFunctionNameOrNull(isolate, wasm, func_index);
if (!name_or_null->IsNull(isolate)) { if (!name_or_null->IsNull(isolate)) {
...@@ -1982,7 +1987,7 @@ Handle<String> GetWasmFunctionName(Isolate* isolate, Handle<Object> wasm, ...@@ -1982,7 +1987,7 @@ Handle<String> GetWasmFunctionName(Isolate* isolate, Handle<Object> wasm,
return isolate->factory()->NewStringFromStaticChars("<WASM UNNAMED>"); return isolate->factory()->NewStringFromStaticChars("<WASM UNNAMED>");
} }
bool IsWasmObject(Object* object) { bool wasm::IsWasmObject(Object* object) {
if (!object->IsJSObject()) return false; if (!object->IsJSObject()) return false;
JSObject* obj = JSObject::cast(object); JSObject* obj = JSObject::cast(object);
...@@ -2003,36 +2008,36 @@ bool IsWasmObject(Object* object) { ...@@ -2003,36 +2008,36 @@ bool IsWasmObject(Object* object) {
return true; return true;
} }
WasmCompiledModule* GetCompiledModule(JSObject* wasm) { WasmCompiledModule* wasm::GetCompiledModule(JSObject* wasm) {
return WasmCompiledModule::cast(wasm->GetInternalField(kWasmCompiledModule)); return WasmCompiledModule::cast(wasm->GetInternalField(kWasmCompiledModule));
} }
bool WasmIsAsmJs(Object* wasm, Isolate* isolate) { bool wasm::WasmIsAsmJs(Object* wasm, Isolate* isolate) {
if (wasm->IsUndefined(isolate)) return false; if (wasm->IsUndefined(isolate)) return false;
DCHECK(IsWasmObject(wasm)); DCHECK(IsWasmObject(wasm));
WasmCompiledModule* compiled_module = GetCompiledModule(JSObject::cast(wasm)); WasmCompiledModule* compiled_module = GetCompiledModule(JSObject::cast(wasm));
return compiled_module->has_asm_js_script(); return compiled_module->has_asm_js_script();
} }
Handle<Script> GetAsmWasmScript(Handle<JSObject> wasm) { Handle<Script> wasm::GetAsmWasmScript(Handle<JSObject> wasm) {
DCHECK(IsWasmObject(*wasm)); DCHECK(IsWasmObject(*wasm));
WasmCompiledModule* compiled_module = GetCompiledModule(*wasm); WasmCompiledModule* compiled_module = GetCompiledModule(*wasm);
return compiled_module->asm_js_script(); return compiled_module->asm_js_script();
} }
int GetAsmWasmSourcePosition(Handle<JSObject> wasm, int func_index, int wasm::GetAsmWasmSourcePosition(Handle<JSObject> wasm, int func_index,
int byte_offset) { int byte_offset) {
return WasmDebugInfo::GetAsmJsSourcePosition(GetDebugInfo(wasm), func_index, return WasmDebugInfo::GetAsmJsSourcePosition(GetDebugInfo(wasm), func_index,
byte_offset); byte_offset);
} }
Handle<SeqOneByteString> GetWasmBytes(Handle<JSObject> wasm) { Handle<SeqOneByteString> wasm::GetWasmBytes(Handle<JSObject> wasm) {
DCHECK(IsWasmObject(*wasm)); DCHECK(IsWasmObject(*wasm));
WasmCompiledModule* compiled_module = GetCompiledModule(*wasm); WasmCompiledModule* compiled_module = GetCompiledModule(*wasm);
return compiled_module->module_bytes(); return compiled_module->module_bytes();
} }
Handle<WasmDebugInfo> GetDebugInfo(Handle<JSObject> wasm) { Handle<WasmDebugInfo> wasm::GetDebugInfo(Handle<JSObject> wasm) {
Handle<Object> info(wasm->GetInternalField(kWasmDebugInfo), Handle<Object> info(wasm->GetInternalField(kWasmDebugInfo),
wasm->GetIsolate()); wasm->GetIsolate());
if (!info->IsUndefined(wasm->GetIsolate())) if (!info->IsUndefined(wasm->GetIsolate()))
...@@ -2042,9 +2047,9 @@ Handle<WasmDebugInfo> GetDebugInfo(Handle<JSObject> wasm) { ...@@ -2042,9 +2047,9 @@ Handle<WasmDebugInfo> GetDebugInfo(Handle<JSObject> wasm) {
return new_info; return new_info;
} }
bool UpdateWasmModuleMemory(Handle<JSObject> object, Address old_start, bool wasm::UpdateWasmModuleMemory(Handle<JSObject> object, Address old_start,
Address new_start, uint32_t old_size, Address new_start, uint32_t old_size,
uint32_t new_size) { uint32_t new_size) {
DisallowHeapAllocation no_allocation; DisallowHeapAllocation no_allocation;
if (!IsWasmObject(*object)) { if (!IsWasmObject(*object)) {
return false; return false;
...@@ -2074,8 +2079,8 @@ bool UpdateWasmModuleMemory(Handle<JSObject> object, Address old_start, ...@@ -2074,8 +2079,8 @@ bool UpdateWasmModuleMemory(Handle<JSObject> object, Address old_start,
return true; return true;
} }
Handle<FixedArray> BuildFunctionTable(Isolate* isolate, uint32_t index, Handle<FixedArray> wasm::BuildFunctionTable(Isolate* isolate, uint32_t index,
const WasmModule* module) { const WasmModule* module) {
const WasmIndirectFunctionTable* table = &module->function_tables[index]; const WasmIndirectFunctionTable* table = &module->function_tables[index];
DCHECK_EQ(table->size, table->values.size()); DCHECK_EQ(table->size, table->values.size());
DCHECK_GE(table->max_size, table->size); DCHECK_GE(table->max_size, table->size);
...@@ -2099,8 +2104,8 @@ Handle<FixedArray> BuildFunctionTable(Isolate* isolate, uint32_t index, ...@@ -2099,8 +2104,8 @@ Handle<FixedArray> BuildFunctionTable(Isolate* isolate, uint32_t index,
return values; return values;
} }
void PopulateFunctionTable(Handle<FixedArray> table, uint32_t table_size, void wasm::PopulateFunctionTable(Handle<FixedArray> table, uint32_t table_size,
const std::vector<Handle<Code>>* code_table) { const std::vector<Handle<Code>>* code_table) {
uint32_t max_size = table->length() / 2; uint32_t max_size = table->length() / 2;
for (uint32_t i = max_size; i < max_size + table_size; ++i) { for (uint32_t i = max_size; i < max_size + table_size; ++i) {
int index = Smi::cast(table->get(static_cast<int>(i)))->value(); int index = Smi::cast(table->get(static_cast<int>(i)))->value();
...@@ -2110,7 +2115,7 @@ void PopulateFunctionTable(Handle<FixedArray> table, uint32_t table_size, ...@@ -2110,7 +2115,7 @@ void PopulateFunctionTable(Handle<FixedArray> table, uint32_t table_size,
} }
} }
int GetNumberOfFunctions(Handle<JSObject> wasm) { int wasm::GetNumberOfFunctions(Handle<JSObject> wasm) {
DCHECK(IsWasmObject(*wasm)); DCHECK(IsWasmObject(*wasm));
WasmCompiledModule* compiled_module = GetCompiledModule(*wasm); WasmCompiledModule* compiled_module = GetCompiledModule(*wasm);
ByteArray* func_names_arr = compiled_module->ptr_to_function_names(); ByteArray* func_names_arr = compiled_module->ptr_to_function_names();
...@@ -2118,7 +2123,7 @@ int GetNumberOfFunctions(Handle<JSObject> wasm) { ...@@ -2118,7 +2123,7 @@ int GetNumberOfFunctions(Handle<JSObject> wasm) {
return func_names_arr->get_int(0); return func_names_arr->get_int(0);
} }
Handle<JSObject> CreateCompiledModuleObject( Handle<JSObject> wasm::CreateCompiledModuleObject(
Isolate* isolate, Handle<WasmCompiledModule> compiled_module, Isolate* isolate, Handle<WasmCompiledModule> compiled_module,
ModuleOrigin origin) { ModuleOrigin origin) {
Handle<JSObject> module_obj; Handle<JSObject> module_obj;
...@@ -2143,7 +2148,7 @@ Handle<JSObject> CreateCompiledModuleObject( ...@@ -2143,7 +2148,7 @@ Handle<JSObject> CreateCompiledModuleObject(
} }
// TODO(clemensh): origin can be inferred from asm_js_script; remove it. // TODO(clemensh): origin can be inferred from asm_js_script; remove it.
MaybeHandle<JSObject> CreateModuleObjectFromBytes( MaybeHandle<JSObject> wasm::CreateModuleObjectFromBytes(
Isolate* isolate, const byte* start, const byte* end, ErrorThrower* thrower, Isolate* isolate, const byte* start, const byte* end, ErrorThrower* thrower,
ModuleOrigin origin, Handle<Script> asm_js_script, ModuleOrigin origin, Handle<Script> asm_js_script,
const byte* asm_js_offset_tables_start, const byte* asm_js_offset_tables_start,
...@@ -2181,8 +2186,9 @@ MaybeHandle<JSObject> CreateModuleObjectFromBytes( ...@@ -2181,8 +2186,9 @@ MaybeHandle<JSObject> CreateModuleObjectFromBytes(
return CreateCompiledModuleObject(isolate, compiled_module, origin); return CreateCompiledModuleObject(isolate, compiled_module, origin);
} }
bool ValidateModuleBytes(Isolate* isolate, const byte* start, const byte* end, bool wasm::ValidateModuleBytes(Isolate* isolate, const byte* start,
ErrorThrower* thrower, ModuleOrigin origin) { const byte* end, ErrorThrower* thrower,
ModuleOrigin origin) {
Zone zone(isolate->allocator()); Zone zone(isolate->allocator());
ModuleResult result = ModuleResult result =
DecodeWasmModule(isolate, &zone, start, end, false, origin); DecodeWasmModule(isolate, &zone, start, end, false, origin);
...@@ -2194,6 +2200,8 @@ bool ValidateModuleBytes(Isolate* isolate, const byte* start, const byte* end, ...@@ -2194,6 +2200,8 @@ bool ValidateModuleBytes(Isolate* isolate, const byte* start, const byte* end,
return false; return false;
} }
namespace {
MaybeHandle<JSArrayBuffer> GetInstanceMemory(Isolate* isolate, MaybeHandle<JSArrayBuffer> GetInstanceMemory(Isolate* isolate,
Handle<JSObject> instance) { Handle<JSObject> instance) {
Object* mem = instance->GetInternalField(kWasmMemArrayBuffer); Object* mem = instance->GetInternalField(kWasmMemArrayBuffer);
...@@ -2210,7 +2218,10 @@ void SetInstanceMemory(Handle<JSObject> instance, JSArrayBuffer* buffer) { ...@@ -2210,7 +2218,10 @@ void SetInstanceMemory(Handle<JSObject> instance, JSArrayBuffer* buffer) {
compiled_module->set_ptr_to_heap(buffer); compiled_module->set_ptr_to_heap(buffer);
} }
int32_t GetInstanceMemorySize(Isolate* isolate, Handle<JSObject> instance) { } // namespace
int32_t wasm::GetInstanceMemorySize(Isolate* isolate,
Handle<JSObject> instance) {
MaybeHandle<JSArrayBuffer> maybe_mem_buffer = MaybeHandle<JSArrayBuffer> maybe_mem_buffer =
GetInstanceMemory(isolate, instance); GetInstanceMemory(isolate, instance);
Handle<JSArrayBuffer> buffer; Handle<JSArrayBuffer> buffer;
...@@ -2221,8 +2232,8 @@ int32_t GetInstanceMemorySize(Isolate* isolate, Handle<JSObject> instance) { ...@@ -2221,8 +2232,8 @@ int32_t GetInstanceMemorySize(Isolate* isolate, Handle<JSObject> instance) {
} }
} }
int32_t GrowInstanceMemory(Isolate* isolate, Handle<JSObject> instance, int32_t wasm::GrowInstanceMemory(Isolate* isolate, Handle<JSObject> instance,
uint32_t pages) { uint32_t pages) {
if (pages == 0) { if (pages == 0) {
return GetInstanceMemorySize(isolate, instance); return GetInstanceMemorySize(isolate, instance);
} }
...@@ -2270,10 +2281,9 @@ int32_t GrowInstanceMemory(Isolate* isolate, Handle<JSObject> instance, ...@@ -2270,10 +2281,9 @@ int32_t GrowInstanceMemory(Isolate* isolate, Handle<JSObject> instance,
return (old_size / WasmModule::kPageSize); return (old_size / WasmModule::kPageSize);
} }
namespace testing { void testing::ValidateInstancesChain(Isolate* isolate,
Handle<JSObject> module_obj,
void ValidateInstancesChain(Isolate* isolate, Handle<JSObject> module_obj, int instance_count) {
int instance_count) {
CHECK_GE(instance_count, 0); CHECK_GE(instance_count, 0);
DisallowHeapAllocation no_gc; DisallowHeapAllocation no_gc;
WasmCompiledModule* compiled_module = WasmCompiledModule* compiled_module =
...@@ -2300,7 +2310,8 @@ void ValidateInstancesChain(Isolate* isolate, Handle<JSObject> module_obj, ...@@ -2300,7 +2310,8 @@ void ValidateInstancesChain(Isolate* isolate, Handle<JSObject> module_obj,
CHECK_EQ(found_instances, instance_count); CHECK_EQ(found_instances, instance_count);
} }
void ValidateModuleState(Isolate* isolate, Handle<JSObject> module_obj) { void testing::ValidateModuleState(Isolate* isolate,
Handle<JSObject> module_obj) {
DisallowHeapAllocation no_gc; DisallowHeapAllocation no_gc;
WasmCompiledModule* compiled_module = WasmCompiledModule* compiled_module =
WasmCompiledModule::cast(module_obj->GetInternalField(0)); WasmCompiledModule::cast(module_obj->GetInternalField(0));
...@@ -2311,15 +2322,11 @@ void ValidateModuleState(Isolate* isolate, Handle<JSObject> module_obj) { ...@@ -2311,15 +2322,11 @@ void ValidateModuleState(Isolate* isolate, Handle<JSObject> module_obj) {
CHECK(!compiled_module->has_weak_owning_instance()); CHECK(!compiled_module->has_weak_owning_instance());
} }
void ValidateOrphanedInstance(Isolate* isolate, Handle<JSObject> instance) { void testing::ValidateOrphanedInstance(Isolate* isolate,
Handle<JSObject> instance) {
DisallowHeapAllocation no_gc; DisallowHeapAllocation no_gc;
CHECK(IsWasmObject(*instance)); CHECK(IsWasmObject(*instance));
WasmCompiledModule* compiled_module = GetCompiledModule(*instance); WasmCompiledModule* compiled_module = GetCompiledModule(*instance);
CHECK(compiled_module->has_weak_module_object()); CHECK(compiled_module->has_weak_module_object());
CHECK(compiled_module->ptr_to_weak_module_object()->cleared()); CHECK(compiled_module->ptr_to_weak_module_object()->cleared());
} }
} // namespace testing
} // namespace wasm
} // namespace internal
} // namespace v8
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