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