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