Commit fd2ccd74 authored by titzer's avatar titzer Committed by Commit bot

[wasm] Remove redundant intermediate data structures in encoder.cc.

R=ahaas@chromium.org,bradnelson@chromium.org,aseemgarg@chromium.org
BUG=

Review-Url: https://codereview.chromium.org/2017853003
Cr-Commit-Position: refs/heads/master@{#36546}
parent 4b235ade
...@@ -90,7 +90,7 @@ class AsmWasmBuilderImpl : public AstVisitor { ...@@ -90,7 +90,7 @@ class AsmWasmBuilderImpl : public AstVisitor {
} }
current_function_builder_ = current_function_builder_ =
builder_->FunctionAt(foreign_init_function_index_); builder_->FunctionAt(foreign_init_function_index_);
current_function_builder_->Exported(1); current_function_builder_->SetExported();
std::string raw_name = "__foreign_init__"; std::string raw_name = "__foreign_init__";
current_function_builder_->SetName(raw_name.data(), current_function_builder_->SetName(raw_name.data(),
static_cast<int>(raw_name.size())); static_cast<int>(raw_name.size()));
...@@ -114,7 +114,7 @@ class AsmWasmBuilderImpl : public AstVisitor { ...@@ -114,7 +114,7 @@ class AsmWasmBuilderImpl : public AstVisitor {
return ret; return ret;
} }
void Compile() { void Build() {
InitializeInitFunction(); InitializeInitFunction();
RECURSE(VisitFunctionLiteral(literal_)); RECURSE(VisitFunctionLiteral(literal_));
BuildForeignInitFunction(); BuildForeignInitFunction();
...@@ -607,7 +607,7 @@ class AsmWasmBuilderImpl : public AstVisitor { ...@@ -607,7 +607,7 @@ class AsmWasmBuilderImpl : public AstVisitor {
const AstRawString* raw_name = name->AsRawPropertyName(); const AstRawString* raw_name = name->AsRawPropertyName();
if (var->is_function()) { if (var->is_function()) {
uint32_t index = LookupOrInsertFunction(var); uint32_t index = LookupOrInsertFunction(var);
builder_->FunctionAt(index)->Exported(1); builder_->FunctionAt(index)->SetExported();
builder_->FunctionAt(index)->SetName( builder_->FunctionAt(index)->SetName(
reinterpret_cast<const char*>(raw_name->raw_data()), reinterpret_cast<const char*>(raw_name->raw_data()),
raw_name->length()); raw_name->length());
...@@ -1761,11 +1761,10 @@ AsmWasmBuilder::AsmWasmBuilder(Isolate* isolate, Zone* zone, ...@@ -1761,11 +1761,10 @@ AsmWasmBuilder::AsmWasmBuilder(Isolate* isolate, Zone* zone,
// that zone in constructor may be thrown away once wasm module is written. // that zone in constructor may be thrown away once wasm module is written.
ZoneBuffer* AsmWasmBuilder::Run(i::Handle<i::FixedArray>* foreign_args) { ZoneBuffer* AsmWasmBuilder::Run(i::Handle<i::FixedArray>* foreign_args) {
AsmWasmBuilderImpl impl(isolate_, zone_, literal_, typer_); AsmWasmBuilderImpl impl(isolate_, zone_, literal_, typer_);
impl.Compile(); impl.Build();
*foreign_args = impl.GetForeignArgs(); *foreign_args = impl.GetForeignArgs();
ZoneBuffer* buffer = new (zone_) ZoneBuffer(zone_); ZoneBuffer* buffer = new (zone_) ZoneBuffer(zone_);
WasmModuleWriter* writer = impl.builder_->Build(zone_); impl.builder_->WriteTo(*buffer);
writer->WriteTo(*buffer);
return buffer; return buffer;
} }
} // namespace wasm } // namespace wasm
......
...@@ -50,8 +50,13 @@ void FixupSection(ZoneBuffer& buffer, size_t start) { ...@@ -50,8 +50,13 @@ void FixupSection(ZoneBuffer& buffer, size_t start) {
kPaddedVarInt32Size)); kPaddedVarInt32Size));
} }
WasmFunctionBuilder::WasmFunctionBuilder(Zone* zone) WasmFunctionBuilder::WasmFunctionBuilder(WasmModuleBuilder* builder)
: locals_(zone), exported_(0), body_(zone), name_(zone) {} : builder_(builder),
locals_(builder->zone()),
signature_index_(0),
exported_(0),
body_(builder->zone()),
name_(builder->zone()) {}
void WasmFunctionBuilder::EmitVarInt(uint32_t val) { void WasmFunctionBuilder::EmitVarInt(uint32_t val) {
byte buffer[8]; byte buffer[8];
...@@ -65,6 +70,7 @@ void WasmFunctionBuilder::EmitVarInt(uint32_t val) { ...@@ -65,6 +70,7 @@ void WasmFunctionBuilder::EmitVarInt(uint32_t val) {
void WasmFunctionBuilder::SetSignature(FunctionSig* sig) { void WasmFunctionBuilder::SetSignature(FunctionSig* sig) {
DCHECK(!locals_.has_sig()); DCHECK(!locals_.has_sig());
locals_.set_sig(sig); locals_.set_sig(sig);
signature_index_ = builder_->AddSignature(sig);
} }
uint32_t WasmFunctionBuilder::AddLocal(LocalType type) { uint32_t WasmFunctionBuilder::AddLocal(LocalType type) {
...@@ -118,7 +124,7 @@ void WasmFunctionBuilder::EmitI32Const(int32_t value) { ...@@ -118,7 +124,7 @@ void WasmFunctionBuilder::EmitI32Const(int32_t value) {
} }
} }
void WasmFunctionBuilder::Exported(uint8_t flag) { exported_ = flag; } void WasmFunctionBuilder::SetExported() { exported_ = true; }
void WasmFunctionBuilder::SetName(const char* name, int name_length) { void WasmFunctionBuilder::SetName(const char* name, int name_length) {
name_.clear(); name_.clear();
...@@ -129,26 +135,11 @@ void WasmFunctionBuilder::SetName(const char* name, int name_length) { ...@@ -129,26 +135,11 @@ void WasmFunctionBuilder::SetName(const char* name, int name_length) {
} }
} }
WasmFunctionEncoder* WasmFunctionBuilder::Build(Zone* zone, void WasmFunctionBuilder::WriteSignature(ZoneBuffer& buffer) const {
WasmModuleBuilder* mb) const {
WasmFunctionEncoder* e =
new (zone) WasmFunctionEncoder(zone, locals_, exported_);
// TODO(titzer): lame memcpy here.
e->body_.insert(e->body_.begin(), body_.begin(), body_.end());
e->signature_index_ = mb->AddSignature(locals_.get_sig());
e->name_.insert(e->name_.begin(), name_.begin(), name_.end());
return e;
}
WasmFunctionEncoder::WasmFunctionEncoder(Zone* zone, LocalDeclEncoder locals,
bool exported)
: locals_(locals), exported_(exported), body_(zone), name_(zone) {}
void WasmFunctionEncoder::WriteSignature(ZoneBuffer& buffer) const {
buffer.write_u32v(signature_index_); buffer.write_u32v(signature_index_);
} }
void WasmFunctionEncoder::WriteExport(ZoneBuffer& buffer, void WasmFunctionBuilder::WriteExport(ZoneBuffer& buffer,
uint32_t func_index) const { uint32_t func_index) const {
if (exported_) { if (exported_) {
buffer.write_u32v(func_index); buffer.write_u32v(func_index);
...@@ -159,7 +150,7 @@ void WasmFunctionEncoder::WriteExport(ZoneBuffer& buffer, ...@@ -159,7 +150,7 @@ void WasmFunctionEncoder::WriteExport(ZoneBuffer& buffer,
} }
} }
void WasmFunctionEncoder::WriteBody(ZoneBuffer& buffer) const { void WasmFunctionBuilder::WriteBody(ZoneBuffer& buffer) const {
size_t locals_size = locals_.Size(); size_t locals_size = locals_.Size();
buffer.write_size(locals_size + body_.size()); buffer.write_size(locals_size + body_.size());
buffer.EnsureSpace(locals_size); buffer.EnsureSpace(locals_size);
...@@ -197,7 +188,7 @@ WasmModuleBuilder::WasmModuleBuilder(Zone* zone) ...@@ -197,7 +188,7 @@ WasmModuleBuilder::WasmModuleBuilder(Zone* zone)
start_function_index_(-1) {} start_function_index_(-1) {}
uint32_t WasmModuleBuilder::AddFunction() { uint32_t WasmModuleBuilder::AddFunction() {
functions_.push_back(new (zone_) WasmFunctionBuilder(zone_)); functions_.push_back(new (zone_) WasmFunctionBuilder(this));
return static_cast<uint32_t>(functions_.size() - 1); return static_cast<uint32_t>(functions_.size() - 1);
} }
...@@ -256,44 +247,12 @@ void WasmModuleBuilder::MarkStartFunction(uint32_t index) { ...@@ -256,44 +247,12 @@ void WasmModuleBuilder::MarkStartFunction(uint32_t index) {
start_function_index_ = index; start_function_index_ = index;
} }
WasmModuleWriter* WasmModuleBuilder::Build(Zone* zone) {
WasmModuleWriter* writer = new (zone) WasmModuleWriter(zone);
for (auto import : imports_) {
writer->imports_.push_back(import);
}
for (auto function : functions_) {
writer->functions_.push_back(function->Build(zone, this));
}
for (auto segment : data_segments_) {
writer->data_segments_.push_back(segment);
}
for (auto sig : signatures_) {
writer->signatures_.push_back(sig);
}
for (auto index : indirect_functions_) {
writer->indirect_functions_.push_back(index);
}
for (auto global : globals_) {
writer->globals_.push_back(global);
}
writer->start_function_index_ = start_function_index_;
return writer;
}
uint32_t WasmModuleBuilder::AddGlobal(MachineType type, bool exported) { uint32_t WasmModuleBuilder::AddGlobal(MachineType type, bool exported) {
globals_.push_back(std::make_pair(type, exported)); globals_.push_back(std::make_pair(type, exported));
return static_cast<uint32_t>(globals_.size() - 1); return static_cast<uint32_t>(globals_.size() - 1);
} }
WasmModuleWriter::WasmModuleWriter(Zone* zone) void WasmModuleBuilder::WriteTo(ZoneBuffer& buffer) const {
: imports_(zone),
functions_(zone),
data_segments_(zone),
signatures_(zone),
indirect_functions_(zone),
globals_(zone) {}
void WasmModuleWriter::WriteTo(ZoneBuffer& buffer) const {
uint32_t exports = 0; uint32_t exports = 0;
// == Emit magic ============================================================= // == Emit magic =============================================================
......
...@@ -112,27 +112,9 @@ class ZoneBuffer : public ZoneObject { ...@@ -112,27 +112,9 @@ class ZoneBuffer : public ZoneObject {
class WasmModuleBuilder; class WasmModuleBuilder;
class WasmFunctionEncoder : public ZoneObject {
public:
void WriteSignature(ZoneBuffer& buffer) const;
void WriteExport(ZoneBuffer& buffer, uint32_t func_index) const;
void WriteBody(ZoneBuffer& buffer) const;
bool exported() const { return exported_; }
private:
WasmFunctionEncoder(Zone* zone, LocalDeclEncoder locals, bool exported);
friend class WasmFunctionBuilder;
uint32_t signature_index_;
LocalDeclEncoder locals_;
bool exported_;
ZoneVector<uint8_t> body_;
ZoneVector<char> name_;
bool HasName() const { return exported_ && name_.size() > 0; }
};
class WasmFunctionBuilder : public ZoneObject { class WasmFunctionBuilder : public ZoneObject {
public: public:
// Building methods.
void SetSignature(FunctionSig* sig); void SetSignature(FunctionSig* sig);
uint32_t AddLocal(LocalType type); uint32_t AddLocal(LocalType type);
void EmitVarInt(uint32_t val); void EmitVarInt(uint32_t val);
...@@ -144,20 +126,27 @@ class WasmFunctionBuilder : public ZoneObject { ...@@ -144,20 +126,27 @@ class WasmFunctionBuilder : public ZoneObject {
void EmitWithU8(WasmOpcode opcode, const byte immediate); void EmitWithU8(WasmOpcode opcode, const byte immediate);
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 Exported(uint8_t flag); void SetExported();
void SetName(const char* name, int name_length); void SetName(const char* name, int name_length);
WasmFunctionEncoder* Build(Zone* zone, WasmModuleBuilder* mb) const; bool exported() { return exported_; }
// Writing methods.
void WriteSignature(ZoneBuffer& buffer) const;
void WriteExport(ZoneBuffer& buffer, uint32_t func_index) const;
void WriteBody(ZoneBuffer& buffer) const;
private: private:
explicit WasmFunctionBuilder(Zone* zone); explicit WasmFunctionBuilder(WasmModuleBuilder* builder);
friend class WasmModuleBuilder; friend class WasmModuleBuilder;
WasmModuleBuilder* builder_;
LocalDeclEncoder locals_; LocalDeclEncoder locals_;
uint8_t exported_; uint32_t signature_index_;
bool exported_;
ZoneVector<uint8_t> body_; ZoneVector<uint8_t> body_;
ZoneVector<char> name_; ZoneVector<char> name_;
void IndexVars(WasmFunctionEncoder* e, uint32_t* var_index) const;
}; };
// TODO(titzer): kill!
class WasmDataSegmentEncoder : public ZoneObject { class WasmDataSegmentEncoder : public ZoneObject {
public: public:
WasmDataSegmentEncoder(Zone* zone, const byte* data, uint32_t size, WasmDataSegmentEncoder(Zone* zone, const byte* data, uint32_t size,
...@@ -175,25 +164,11 @@ struct WasmFunctionImport { ...@@ -175,25 +164,11 @@ struct WasmFunctionImport {
int name_length; int name_length;
}; };
class WasmModuleWriter : public ZoneObject {
public:
void WriteTo(ZoneBuffer& buffer) const;
private:
friend class WasmModuleBuilder;
explicit WasmModuleWriter(Zone* zone);
ZoneVector<WasmFunctionImport> imports_;
ZoneVector<WasmFunctionEncoder*> functions_;
ZoneVector<WasmDataSegmentEncoder*> data_segments_;
ZoneVector<FunctionSig*> signatures_;
ZoneVector<uint32_t> indirect_functions_;
ZoneVector<std::pair<MachineType, bool>> globals_;
int start_function_index_;
};
class WasmModuleBuilder : public ZoneObject { class WasmModuleBuilder : public ZoneObject {
public: public:
explicit WasmModuleBuilder(Zone* zone); explicit WasmModuleBuilder(Zone* zone);
// Building methods.
uint32_t AddFunction(); uint32_t AddFunction();
uint32_t AddGlobal(MachineType type, bool exported); uint32_t AddGlobal(MachineType type, bool exported);
WasmFunctionBuilder* FunctionAt(size_t index); WasmFunctionBuilder* FunctionAt(size_t index);
...@@ -202,13 +177,17 @@ class WasmModuleBuilder : public ZoneObject { ...@@ -202,13 +177,17 @@ class WasmModuleBuilder : public ZoneObject {
void AddIndirectFunction(uint32_t index); void AddIndirectFunction(uint32_t index);
void MarkStartFunction(uint32_t index); void MarkStartFunction(uint32_t index);
uint32_t AddImport(const char* name, int name_length, FunctionSig* sig); uint32_t AddImport(const char* name, int name_length, FunctionSig* sig);
WasmModuleWriter* Build(Zone* zone);
// Writing methods.
void WriteTo(ZoneBuffer& buffer) const;
struct CompareFunctionSigs { struct CompareFunctionSigs {
bool operator()(FunctionSig* a, FunctionSig* b) const; bool operator()(FunctionSig* a, FunctionSig* b) const;
}; };
typedef ZoneMap<FunctionSig*, uint32_t, CompareFunctionSigs> SignatureMap; typedef ZoneMap<FunctionSig*, uint32_t, CompareFunctionSigs> SignatureMap;
Zone* zone() { return zone_; }
private: private:
Zone* zone_; Zone* zone_;
ZoneVector<FunctionSig*> signatures_; ZoneVector<FunctionSig*> signatures_;
......
...@@ -23,8 +23,7 @@ namespace { ...@@ -23,8 +23,7 @@ namespace {
void TestModule(Zone* zone, WasmModuleBuilder* builder, void TestModule(Zone* zone, WasmModuleBuilder* builder,
int32_t expected_result) { int32_t expected_result) {
ZoneBuffer buffer(zone); ZoneBuffer buffer(zone);
WasmModuleWriter* writer = builder->Build(zone); builder->WriteTo(buffer);
writer->WriteTo(buffer);
Isolate* isolate = CcTest::InitIsolateOnce(); Isolate* isolate = CcTest::InitIsolateOnce();
HandleScope scope(isolate); HandleScope scope(isolate);
...@@ -45,7 +44,7 @@ TEST(Run_WasmModule_Return114) { ...@@ -45,7 +44,7 @@ TEST(Run_WasmModule_Return114) {
uint16_t f_index = builder->AddFunction(); uint16_t f_index = builder->AddFunction();
WasmFunctionBuilder* f = builder->FunctionAt(f_index); WasmFunctionBuilder* f = builder->FunctionAt(f_index);
f->SetSignature(sigs.i_v()); f->SetSignature(sigs.i_v());
f->Exported(1); f->SetExported();
byte code[] = {WASM_I8(kReturnValue)}; byte code[] = {WASM_I8(kReturnValue)};
f->EmitCode(code, sizeof(code)); f->EmitCode(code, sizeof(code));
TestModule(&zone, builder, kReturnValue); TestModule(&zone, builder, kReturnValue);
...@@ -70,7 +69,7 @@ TEST(Run_WasmModule_CallAdd) { ...@@ -70,7 +69,7 @@ TEST(Run_WasmModule_CallAdd) {
f = builder->FunctionAt(f2_index); f = builder->FunctionAt(f2_index);
f->SetSignature(sigs.i_v()); f->SetSignature(sigs.i_v());
f->Exported(1); f->SetExported();
byte code2[] = {WASM_CALL_FUNCTION2(f1_index, WASM_I8(77), WASM_I8(22))}; byte code2[] = {WASM_CALL_FUNCTION2(f1_index, WASM_I8(77), WASM_I8(22))};
f->EmitCode(code2, sizeof(code2)); f->EmitCode(code2, sizeof(code2));
TestModule(&zone, builder, 99); TestModule(&zone, builder, 99);
...@@ -87,7 +86,7 @@ TEST(Run_WasmModule_ReadLoadedDataSegment) { ...@@ -87,7 +86,7 @@ TEST(Run_WasmModule_ReadLoadedDataSegment) {
WasmFunctionBuilder* f = builder->FunctionAt(f_index); WasmFunctionBuilder* f = builder->FunctionAt(f_index);
f->SetSignature(sigs.i_v()); f->SetSignature(sigs.i_v());
f->Exported(1); f->SetExported();
byte code[] = { byte code[] = {
WASM_LOAD_MEM(MachineType::Int32(), WASM_I8(kDataSegmentDest0))}; WASM_LOAD_MEM(MachineType::Int32(), WASM_I8(kDataSegmentDest0))};
f->EmitCode(code, sizeof(code)); f->EmitCode(code, sizeof(code));
...@@ -109,7 +108,7 @@ TEST(Run_WasmModule_CheckMemoryIsZero) { ...@@ -109,7 +108,7 @@ TEST(Run_WasmModule_CheckMemoryIsZero) {
f->SetSignature(sigs.i_v()); f->SetSignature(sigs.i_v());
uint16_t localIndex = f->AddLocal(kAstI32); uint16_t localIndex = f->AddLocal(kAstI32);
f->Exported(1); f->SetExported();
byte code[] = {WASM_BLOCK( byte code[] = {WASM_BLOCK(
2, 2,
WASM_WHILE( WASM_WHILE(
...@@ -133,7 +132,7 @@ TEST(Run_WasmModule_CallMain_recursive) { ...@@ -133,7 +132,7 @@ TEST(Run_WasmModule_CallMain_recursive) {
f->SetSignature(sigs.i_v()); f->SetSignature(sigs.i_v());
uint16_t localIndex = f->AddLocal(kAstI32); uint16_t localIndex = f->AddLocal(kAstI32);
f->Exported(1); f->SetExported();
byte code[] = {WASM_BLOCK( byte code[] = {WASM_BLOCK(
2, WASM_SET_LOCAL(localIndex, 2, WASM_SET_LOCAL(localIndex,
WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO)), WASM_LOAD_MEM(MachineType::Int32(), WASM_ZERO)),
...@@ -163,7 +162,7 @@ TEST(Run_WasmModule_Global) { ...@@ -163,7 +162,7 @@ TEST(Run_WasmModule_Global) {
uint16_t f2_index = builder->AddFunction(); uint16_t f2_index = builder->AddFunction();
f = builder->FunctionAt(f2_index); f = builder->FunctionAt(f2_index);
f->SetSignature(sigs.i_v()); f->SetSignature(sigs.i_v());
f->Exported(1); f->SetExported();
byte code2[] = {WASM_STORE_GLOBAL(global1, WASM_I32V_1(56)), byte code2[] = {WASM_STORE_GLOBAL(global1, WASM_I32V_1(56)),
WASM_STORE_GLOBAL(global2, WASM_I32V_1(41)), WASM_STORE_GLOBAL(global2, WASM_I32V_1(41)),
WASM_RETURN1(WASM_CALL_FUNCTION0(f1_index))}; WASM_RETURN1(WASM_CALL_FUNCTION0(f1_index))};
......
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