Commit 4f9976aa authored by clemensh's avatar clemensh Committed by Commit bot

[wasm] Decouple function name and exported name in WasmFunctionBuilder

This is needed for the asm.js -> WASM pipeline. A single exported
function is exported as __single_function__, but we still want to see
the correct function name on the stack, so the underlying wasm function
has to carry the original name.

R=ahaas@chromium.org, titzer@chromium.org
BUG=v8:4203

Review-Url: https://codereview.chromium.org/2406133003
Cr-Commit-Position: refs/heads/master@{#40159}
parent 1c113838
...@@ -82,14 +82,8 @@ class AsmWasmBuilderImpl final : public AstVisitor<AsmWasmBuilderImpl> { ...@@ -82,14 +82,8 @@ class AsmWasmBuilderImpl final : public AstVisitor<AsmWasmBuilderImpl> {
++i) { ++i) {
b.AddParam(i->type); b.AddParam(i->type);
} }
foreign_init_function_->SetExported(); foreign_init_function_->ExportAs(
std::string raw_name = "__foreign_init__"; CStrVector(AsmWasmBuilder::foreign_init_name));
foreign_init_function_->SetName(
AsmWasmBuilder::foreign_init_name,
static_cast<int>(strlen(AsmWasmBuilder::foreign_init_name)));
foreign_init_function_->SetName(raw_name.data(),
static_cast<int>(raw_name.size()));
foreign_init_function_->SetSignature(b.Build()); foreign_init_function_->SetSignature(b.Build());
for (size_t pos = 0; pos < foreign_variables_.size(); ++pos) { for (size_t pos = 0; pos < foreign_variables_.size(); ++pos) {
foreign_init_function_->EmitGetLocal(static_cast<uint32_t>(pos)); foreign_init_function_->EmitGetLocal(static_cast<uint32_t>(pos));
...@@ -564,10 +558,7 @@ class AsmWasmBuilderImpl final : public AstVisitor<AsmWasmBuilderImpl> { ...@@ -564,10 +558,7 @@ class AsmWasmBuilderImpl final : public AstVisitor<AsmWasmBuilderImpl> {
Variable* var = expr->var(); Variable* var = expr->var();
DCHECK(var->is_function()); DCHECK(var->is_function());
WasmFunctionBuilder* function = LookupOrInsertFunction(var); WasmFunctionBuilder* function = LookupOrInsertFunction(var);
function->SetExported(); function->ExportAs(CStrVector(AsmWasmBuilder::single_function_name));
function->SetName(
AsmWasmBuilder::single_function_name,
static_cast<int>(strlen(AsmWasmBuilder::single_function_name)));
} }
} }
...@@ -651,9 +642,9 @@ class AsmWasmBuilderImpl final : public AstVisitor<AsmWasmBuilderImpl> { ...@@ -651,9 +642,9 @@ class AsmWasmBuilderImpl final : public AstVisitor<AsmWasmBuilderImpl> {
const AstRawString* raw_name = name->AsRawPropertyName(); const AstRawString* raw_name = name->AsRawPropertyName();
if (var->is_function()) { if (var->is_function()) {
WasmFunctionBuilder* function = LookupOrInsertFunction(var); WasmFunctionBuilder* function = LookupOrInsertFunction(var);
function->SetExported(); function->Export();
function->SetName(reinterpret_cast<const char*>(raw_name->raw_data()), function->SetName({reinterpret_cast<const char*>(raw_name->raw_data()),
raw_name->length()); raw_name->length()});
} }
} }
} }
...@@ -1822,8 +1813,8 @@ class AsmWasmBuilderImpl final : public AstVisitor<AsmWasmBuilderImpl> { ...@@ -1822,8 +1813,8 @@ class AsmWasmBuilderImpl final : public AstVisitor<AsmWasmBuilderImpl> {
entry = functions_.LookupOrInsert(v, ComputePointerHash(v), entry = functions_.LookupOrInsert(v, ComputePointerHash(v),
ZoneAllocationPolicy(zone())); ZoneAllocationPolicy(zone()));
function->SetName( function->SetName(
reinterpret_cast<const char*>(v->raw_name()->raw_data()), {reinterpret_cast<const char*>(v->raw_name()->raw_data()),
v->raw_name()->length()); v->raw_name()->length()});
entry->value = function; entry->value = function;
} }
return (reinterpret_cast<WasmFunctionBuilder*>(entry->value)); return (reinterpret_cast<WasmFunctionBuilder*>(entry->value));
......
...@@ -54,6 +54,7 @@ WasmFunctionBuilder::WasmFunctionBuilder(WasmModuleBuilder* builder) ...@@ -54,6 +54,7 @@ WasmFunctionBuilder::WasmFunctionBuilder(WasmModuleBuilder* builder)
func_index_(static_cast<uint32_t>(builder->functions_.size())), func_index_(static_cast<uint32_t>(builder->functions_.size())),
body_(builder->zone()), body_(builder->zone()),
name_(builder->zone()), name_(builder->zone()),
exported_name_(builder->zone()),
i32_temps_(builder->zone()), i32_temps_(builder->zone()),
i64_temps_(builder->zone()), i64_temps_(builder->zone()),
f32_temps_(builder->zone()), f32_temps_(builder->zone()),
...@@ -139,15 +140,17 @@ void WasmFunctionBuilder::EmitDirectCallIndex(uint32_t index) { ...@@ -139,15 +140,17 @@ void WasmFunctionBuilder::EmitDirectCallIndex(uint32_t index) {
EmitCode(code, sizeof(code)); EmitCode(code, sizeof(code));
} }
void WasmFunctionBuilder::SetExported() { exported_ = true; } void WasmFunctionBuilder::Export() { exported_ = true; }
void WasmFunctionBuilder::SetName(const char* name, int name_length) { void WasmFunctionBuilder::ExportAs(Vector<const char> name) {
name_.clear(); exported_ = true;
if (name_length > 0) { exported_name_.resize(name.length());
for (int i = 0; i < name_length; ++i) { memcpy(exported_name_.data(), name.start(), name.length());
name_.push_back(*(name + i)); }
}
} void WasmFunctionBuilder::SetName(Vector<const char> name) {
name_.resize(name.length());
memcpy(name_.data(), name.start(), name.length());
} }
void WasmFunctionBuilder::WriteSignature(ZoneBuffer& buffer) const { void WasmFunctionBuilder::WriteSignature(ZoneBuffer& buffer) const {
...@@ -156,10 +159,11 @@ void WasmFunctionBuilder::WriteSignature(ZoneBuffer& buffer) const { ...@@ -156,10 +159,11 @@ void WasmFunctionBuilder::WriteSignature(ZoneBuffer& buffer) const {
void WasmFunctionBuilder::WriteExport(ZoneBuffer& buffer) const { void WasmFunctionBuilder::WriteExport(ZoneBuffer& buffer) const {
if (exported_) { if (exported_) {
buffer.write_size(name_.size()); const ZoneVector<char>* exported_name =
if (name_.size() > 0) { exported_name_.size() == 0 ? &name_ : &exported_name_;
buffer.write(reinterpret_cast<const byte*>(&name_[0]), name_.size()); buffer.write_size(exported_name->size());
} buffer.write(reinterpret_cast<const byte*>(exported_name->data()),
exported_name->size());
buffer.write_u8(kExternalFunction); buffer.write_u8(kExternalFunction);
buffer.write_u32v(func_index_ + buffer.write_u32v(func_index_ +
static_cast<uint32_t>(builder_->imports_.size())); static_cast<uint32_t>(builder_->imports_.size()));
......
...@@ -132,8 +132,9 @@ class V8_EXPORT_PRIVATE WasmFunctionBuilder : public ZoneObject { ...@@ -132,8 +132,9 @@ class V8_EXPORT_PRIVATE WasmFunctionBuilder : public ZoneObject {
void EmitWithU8U8(WasmOpcode opcode, const byte imm1, const byte imm2); void EmitWithU8U8(WasmOpcode opcode, const byte imm1, const byte imm2);
void EmitWithVarInt(WasmOpcode opcode, uint32_t immediate); void EmitWithVarInt(WasmOpcode opcode, uint32_t immediate);
void EmitDirectCallIndex(uint32_t index); void EmitDirectCallIndex(uint32_t index);
void SetExported(); void Export();
void SetName(const char* name, int name_length); void ExportAs(Vector<const char> name);
void SetName(Vector<const char> name);
void WriteSignature(ZoneBuffer& buffer) const; void WriteSignature(ZoneBuffer& buffer) const;
void WriteExport(ZoneBuffer& buffer) const; void WriteExport(ZoneBuffer& buffer) const;
...@@ -160,6 +161,7 @@ class V8_EXPORT_PRIVATE WasmFunctionBuilder : public ZoneObject { ...@@ -160,6 +161,7 @@ class V8_EXPORT_PRIVATE WasmFunctionBuilder : public ZoneObject {
uint32_t func_index_; uint32_t func_index_;
ZoneVector<uint8_t> body_; ZoneVector<uint8_t> body_;
ZoneVector<char> name_; ZoneVector<char> name_;
ZoneVector<char> exported_name_;
ZoneVector<uint32_t> i32_temps_; ZoneVector<uint32_t> i32_temps_;
ZoneVector<uint32_t> i64_temps_; ZoneVector<uint32_t> i64_temps_;
ZoneVector<uint32_t> f32_temps_; ZoneVector<uint32_t> f32_temps_;
......
...@@ -48,15 +48,7 @@ void TestModuleException(Zone* zone, WasmModuleBuilder* builder) { ...@@ -48,15 +48,7 @@ void TestModuleException(Zone* zone, WasmModuleBuilder* builder) {
isolate->clear_pending_exception(); isolate->clear_pending_exception();
} }
void ExportAs(WasmFunctionBuilder* f, const char* name) { void ExportAsMain(WasmFunctionBuilder* f) { f->ExportAs(CStrVector("main")); }
f->SetExported();
f->SetName(name, static_cast<int>(strlen(name)));
}
void ExportAsMain(WasmFunctionBuilder* f) {
static const char kMainName[] = "main";
ExportAs(f, kMainName);
}
} // namespace } // namespace
...@@ -190,7 +182,7 @@ TEST(Run_WasmModule_Serialization) { ...@@ -190,7 +182,7 @@ TEST(Run_WasmModule_Serialization) {
WasmFunctionBuilder* f = builder->AddFunction(sigs.i_i()); WasmFunctionBuilder* f = builder->AddFunction(sigs.i_i());
byte code[] = {WASM_GET_LOCAL(0), kExprI32Const, 1, kExprI32Add}; byte code[] = {WASM_GET_LOCAL(0), kExprI32Const, 1, kExprI32Add};
f->EmitCode(code, sizeof(code)); f->EmitCode(code, sizeof(code));
ExportAs(f, kFunctionName); f->ExportAs(CStrVector(kFunctionName));
ZoneBuffer buffer(&zone); ZoneBuffer buffer(&zone);
builder->WriteTo(buffer); builder->WriteTo(buffer);
......
...@@ -45,8 +45,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { ...@@ -45,8 +45,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
v8::internal::wasm::WasmFunctionBuilder* f = v8::internal::wasm::WasmFunctionBuilder* f =
builder.AddFunction(sigs.i_iii()); builder.AddFunction(sigs.i_iii());
f->EmitCode(data, static_cast<uint32_t>(size)); f->EmitCode(data, static_cast<uint32_t>(size));
f->SetExported(); f->ExportAs(v8::internal::CStrVector("main"));
f->SetName("main", 4);
ZoneBuffer buffer(&zone); ZoneBuffer buffer(&zone);
builder.WriteTo(buffer); builder.WriteTo(buffer);
......
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