| Index: src/wasm/encoder.cc
|
| diff --git a/src/wasm/encoder.cc b/src/wasm/encoder.cc
|
| index 24ca158e7f0e77ba2d4d2e964fa216e5fc13e254..3ae2213ce636f37d6fcfb672eeb78ce5aa17efac 100644
|
| --- a/src/wasm/encoder.cc
|
| +++ b/src/wasm/encoder.cc
|
| @@ -104,7 +104,6 @@ WasmFunctionBuilder::WasmFunctionBuilder(Zone* zone)
|
| : return_type_(kAstI32),
|
| locals_(zone),
|
| exported_(0),
|
| - external_(0),
|
| body_(zone),
|
| local_indices_(zone),
|
| name_(zone) {}
|
| @@ -222,9 +221,7 @@ void WasmFunctionBuilder::EditVarIntImmediate(uint32_t offset,
|
|
|
| void WasmFunctionBuilder::Exported(uint8_t flag) { exported_ = flag; }
|
|
|
| -void WasmFunctionBuilder::External(uint8_t flag) { external_ = flag; }
|
| -
|
| -void WasmFunctionBuilder::SetName(const unsigned char* name, int name_length) {
|
| +void WasmFunctionBuilder::SetName(const char* name, int name_length) {
|
| name_.clear();
|
| if (name_length > 0) {
|
| for (int i = 0; i < name_length; i++) {
|
| @@ -236,7 +233,7 @@ void WasmFunctionBuilder::SetName(const unsigned char* name, int name_length) {
|
| WasmFunctionEncoder* WasmFunctionBuilder::Build(Zone* zone,
|
| WasmModuleBuilder* mb) const {
|
| WasmFunctionEncoder* e =
|
| - new (zone) WasmFunctionEncoder(zone, return_type_, exported_, external_);
|
| + new (zone) WasmFunctionEncoder(zone, return_type_, exported_);
|
| uint16_t* var_index = zone->NewArray<uint16_t>(locals_.size());
|
| IndexVars(e, var_index);
|
| if (body_.size() > 0) {
|
| @@ -330,16 +327,12 @@ void WasmFunctionBuilder::IndexVars(WasmFunctionEncoder* e,
|
| }
|
|
|
| WasmFunctionEncoder::WasmFunctionEncoder(Zone* zone, LocalType return_type,
|
| - bool exported, bool external)
|
| - : params_(zone),
|
| - exported_(exported),
|
| - external_(external),
|
| - body_(zone),
|
| - name_(zone) {}
|
| + bool exported)
|
| + : params_(zone), exported_(exported), body_(zone), name_(zone) {}
|
|
|
| uint32_t WasmFunctionEncoder::HeaderSize() const {
|
| uint32_t size = 3;
|
| - if (!external_) size += 2;
|
| + size += 2;
|
| if (HasName()) {
|
| uint32_t name_size = NameSize();
|
| size +=
|
| @@ -356,8 +349,7 @@ uint32_t WasmFunctionEncoder::BodySize(void) const {
|
| local_decl.AddLocals(local_f32_count_, kAstF32);
|
| local_decl.AddLocals(local_f64_count_, kAstF64);
|
|
|
| - return external_ ? 0
|
| - : static_cast<uint32_t>(body_.size() + local_decl.Size());
|
| + return static_cast<uint32_t>(body_.size() + local_decl.Size());
|
| }
|
|
|
| uint32_t WasmFunctionEncoder::NameSize() const {
|
| @@ -367,7 +359,6 @@ uint32_t WasmFunctionEncoder::NameSize() const {
|
| void WasmFunctionEncoder::Serialize(byte* buffer, byte** header,
|
| byte** body) const {
|
| uint8_t decl_bits = (exported_ ? kDeclFunctionExport : 0) |
|
| - (external_ ? kDeclFunctionImport : 0) |
|
| (HasName() ? kDeclFunctionName : 0);
|
|
|
| EmitUint8(header, decl_bits);
|
| @@ -380,20 +371,18 @@ void WasmFunctionEncoder::Serialize(byte* buffer, byte** header,
|
| }
|
| }
|
|
|
| - if (!external_) {
|
| - // TODO(titzer): embed a LocalDeclEncoder in the WasmFunctionEncoder
|
| - LocalDeclEncoder local_decl;
|
| - local_decl.AddLocals(local_i32_count_, kAstI32);
|
| - local_decl.AddLocals(local_i64_count_, kAstI64);
|
| - local_decl.AddLocals(local_f32_count_, kAstF32);
|
| - local_decl.AddLocals(local_f64_count_, kAstF64);
|
| + // TODO(titzer): embed a LocalDeclEncoder in the WasmFunctionEncoder
|
| + LocalDeclEncoder local_decl;
|
| + local_decl.AddLocals(local_i32_count_, kAstI32);
|
| + local_decl.AddLocals(local_i64_count_, kAstI64);
|
| + local_decl.AddLocals(local_f32_count_, kAstF32);
|
| + local_decl.AddLocals(local_f64_count_, kAstF64);
|
|
|
| - EmitUint16(header, static_cast<uint16_t>(body_.size() + local_decl.Size()));
|
| - (*header) += local_decl.Emit(*header);
|
| - if (body_.size() > 0) {
|
| - std::memcpy(*header, &body_[0], body_.size());
|
| - (*header) += body_.size();
|
| - }
|
| + EmitUint16(header, static_cast<uint16_t>(body_.size() + local_decl.Size()));
|
| + (*header) += local_decl.Emit(*header);
|
| + if (body_.size() > 0) {
|
| + std::memcpy(*header, &body_[0], body_.size());
|
| + (*header) += body_.size();
|
| }
|
| }
|
|
|
| @@ -426,6 +415,7 @@ void WasmDataSegmentEncoder::Serialize(byte* buffer, byte** header,
|
| WasmModuleBuilder::WasmModuleBuilder(Zone* zone)
|
| : zone_(zone),
|
| signatures_(zone),
|
| + imports_(zone),
|
| functions_(zone),
|
| data_segments_(zone),
|
| indirect_functions_(zone),
|
| @@ -433,9 +423,9 @@ WasmModuleBuilder::WasmModuleBuilder(Zone* zone)
|
| signature_map_(zone),
|
| start_function_index_(-1) {}
|
|
|
| -uint16_t WasmModuleBuilder::AddFunction() {
|
| +uint32_t WasmModuleBuilder::AddFunction() {
|
| functions_.push_back(new (zone_) WasmFunctionBuilder(zone_));
|
| - return static_cast<uint16_t>(functions_.size() - 1);
|
| + return static_cast<uint32_t>(functions_.size() - 1);
|
| }
|
|
|
| WasmFunctionBuilder* WasmModuleBuilder::FunctionAt(size_t index) {
|
| @@ -467,7 +457,7 @@ bool WasmModuleBuilder::CompareFunctionSigs::operator()(FunctionSig* a,
|
| return false;
|
| }
|
|
|
| -uint16_t WasmModuleBuilder::AddSignature(FunctionSig* sig) {
|
| +uint32_t WasmModuleBuilder::AddSignature(FunctionSig* sig) {
|
| SignatureMap::iterator pos = signature_map_.find(sig);
|
| if (pos != signature_map_.end()) {
|
| return pos->second;
|
| @@ -483,12 +473,21 @@ void WasmModuleBuilder::AddIndirectFunction(uint16_t index) {
|
| indirect_functions_.push_back(index);
|
| }
|
|
|
| +uint32_t WasmModuleBuilder::AddImport(const char* name, int name_length,
|
| + FunctionSig* sig) {
|
| + imports_.push_back({AddSignature(sig), name, name_length});
|
| + return static_cast<uint32_t>(imports_.size() - 1);
|
| +}
|
| +
|
| void WasmModuleBuilder::MarkStartFunction(uint16_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));
|
| }
|
| @@ -514,7 +513,8 @@ uint32_t WasmModuleBuilder::AddGlobal(MachineType type, bool exported) {
|
| }
|
|
|
| WasmModuleWriter::WasmModuleWriter(Zone* zone)
|
| - : functions_(zone),
|
| + : imports_(zone),
|
| + functions_(zone),
|
| data_segments_(zone),
|
| signatures_(zone),
|
| indirect_functions_(zone),
|
| @@ -576,6 +576,18 @@ WasmModuleIndex* WasmModuleWriter::WriteTo(Zone* zone) const {
|
| (unsigned)sizes.body_size);
|
| }
|
|
|
| + if (imports_.size() > 0) {
|
| + sizes.AddSection(WasmSection::Code::ImportTable, imports_.size());
|
| + for (auto import : imports_) {
|
| + sizes.Add(LEBHelper::sizeof_u32v(import.sig_index), 0);
|
| + sizes.Add(LEBHelper::sizeof_u32v(import.name_length), 0);
|
| + sizes.Add(import.name_length, 0);
|
| + sizes.Add(1, 0);
|
| + }
|
| + TRACE("Size after imports: %u, %u\n", (unsigned)sizes.header_size,
|
| + (unsigned)sizes.body_size);
|
| + }
|
| +
|
| if (indirect_functions_.size() > 0) {
|
| sizes.AddSection(WasmSection::Code::FunctionTable,
|
| indirect_functions_.size());
|
| @@ -655,6 +667,20 @@ WasmModuleIndex* WasmModuleWriter::WriteTo(Zone* zone) const {
|
| FixupSection(section, header);
|
| }
|
|
|
| + // -- emit imports -----------------------------------------------------------
|
| + if (imports_.size() > 0) {
|
| + byte* section = EmitSection(WasmSection::Code::ImportTable, &header);
|
| + EmitVarInt(&header, imports_.size());
|
| + for (auto import : imports_) {
|
| + EmitVarInt(&header, import.sig_index);
|
| + EmitVarInt(&header, import.name_length);
|
| + std::memcpy(header, import.name, import.name_length);
|
| + header += import.name_length;
|
| + EmitVarInt(&header, 0);
|
| + }
|
| + FixupSection(section, header);
|
| + }
|
| +
|
| // -- emit functions ---------------------------------------------------------
|
| if (functions_.size() > 0) {
|
| byte* section = EmitSection(WasmSection::Code::OldFunctions, &header);
|
|
|