2015-12-11 12:26:16 +00:00
|
|
|
// Copyright 2015 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "src/signature.h"
|
|
|
|
|
|
|
|
#include "src/handles.h"
|
|
|
|
#include "src/v8.h"
|
|
|
|
#include "src/zone-containers.h"
|
|
|
|
|
|
|
|
#include "src/wasm/ast-decoder.h"
|
|
|
|
#include "src/wasm/encoder.h"
|
2016-04-21 10:14:34 +00:00
|
|
|
#include "src/wasm/leb-helper.h"
|
2016-03-07 21:04:07 +00:00
|
|
|
#include "src/wasm/wasm-macro-gen.h"
|
2015-12-11 12:26:16 +00:00
|
|
|
#include "src/wasm/wasm-module.h"
|
|
|
|
#include "src/wasm/wasm-opcodes.h"
|
|
|
|
|
|
|
|
#include "src/v8memory.h"
|
|
|
|
|
2016-03-10 12:36:42 +00:00
|
|
|
#if DEBUG
|
|
|
|
#define TRACE(...) \
|
|
|
|
do { \
|
2016-03-10 17:04:44 +00:00
|
|
|
if (FLAG_trace_wasm_encoder) PrintF(__VA_ARGS__); \
|
2016-03-10 12:36:42 +00:00
|
|
|
} while (false)
|
|
|
|
#else
|
|
|
|
#define TRACE(...)
|
|
|
|
#endif
|
|
|
|
|
2015-12-11 12:26:16 +00:00
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace wasm {
|
|
|
|
|
2016-05-25 16:12:09 +00:00
|
|
|
// Emit a section name and the size as a padded varint that can be patched
|
|
|
|
// later.
|
|
|
|
size_t EmitSection(WasmSection::Code code, ZoneBuffer& buffer) {
|
2016-04-21 11:18:41 +00:00
|
|
|
// Emit the section name.
|
|
|
|
const char* name = WasmSection::getName(code);
|
|
|
|
TRACE("emit section: %s\n", name);
|
|
|
|
size_t length = WasmSection::getNameLength(code);
|
2016-05-25 16:12:09 +00:00
|
|
|
buffer.write_size(length); // Section name string size.
|
|
|
|
buffer.write(reinterpret_cast<const byte*>(name), length);
|
2016-04-21 11:18:41 +00:00
|
|
|
|
2016-04-29 09:39:26 +00:00
|
|
|
// Emit a placeholder for the length.
|
2016-05-25 16:12:09 +00:00
|
|
|
return buffer.reserve_u32v();
|
|
|
|
}
|
2016-04-29 09:39:26 +00:00
|
|
|
|
2016-05-25 16:12:09 +00:00
|
|
|
// Patch the size of a section after it's finished.
|
|
|
|
void FixupSection(ZoneBuffer& buffer, size_t start) {
|
|
|
|
buffer.patch_u32v(start, static_cast<uint32_t>(buffer.offset() - start -
|
|
|
|
kPaddedVarInt32Size));
|
2016-03-10 12:36:42 +00:00
|
|
|
}
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-05-27 12:15:23 +00:00
|
|
|
WasmFunctionBuilder::WasmFunctionBuilder(WasmModuleBuilder* builder)
|
|
|
|
: builder_(builder),
|
|
|
|
locals_(builder->zone()),
|
|
|
|
signature_index_(0),
|
|
|
|
exported_(0),
|
|
|
|
body_(builder->zone()),
|
|
|
|
name_(builder->zone()) {}
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-04-21 10:14:34 +00:00
|
|
|
void WasmFunctionBuilder::EmitVarInt(uint32_t val) {
|
|
|
|
byte buffer[8];
|
|
|
|
byte* ptr = buffer;
|
|
|
|
LEBHelper::write_u32v(&ptr, val);
|
|
|
|
for (byte* p = buffer; p < ptr; p++) {
|
|
|
|
body_.push_back(*p);
|
|
|
|
}
|
|
|
|
}
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-05-17 17:53:46 +00:00
|
|
|
void WasmFunctionBuilder::SetSignature(FunctionSig* sig) {
|
|
|
|
DCHECK(!locals_.has_sig());
|
|
|
|
locals_.set_sig(sig);
|
2016-05-27 12:15:23 +00:00
|
|
|
signature_index_ = builder_->AddSignature(sig);
|
2015-12-11 12:26:16 +00:00
|
|
|
}
|
|
|
|
|
2016-05-17 17:53:46 +00:00
|
|
|
uint32_t WasmFunctionBuilder::AddLocal(LocalType type) {
|
|
|
|
DCHECK(locals_.has_sig());
|
|
|
|
return locals_.AddLocals(1, type);
|
2015-12-11 12:26:16 +00:00
|
|
|
}
|
|
|
|
|
2016-04-18 20:57:05 +00:00
|
|
|
void WasmFunctionBuilder::EmitGetLocal(uint32_t local_index) {
|
|
|
|
EmitWithVarInt(kExprGetLocal, local_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WasmFunctionBuilder::EmitSetLocal(uint32_t local_index) {
|
|
|
|
EmitWithVarInt(kExprSetLocal, local_index);
|
|
|
|
}
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-05-17 17:53:46 +00:00
|
|
|
void WasmFunctionBuilder::EmitCode(const byte* code, uint32_t code_size) {
|
2016-06-23 22:26:06 +00:00
|
|
|
for (size_t i = 0; i < code_size; ++i) {
|
2015-12-11 12:26:16 +00:00
|
|
|
body_.push_back(code[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WasmFunctionBuilder::Emit(WasmOpcode opcode) {
|
|
|
|
body_.push_back(static_cast<byte>(opcode));
|
|
|
|
}
|
|
|
|
|
|
|
|
void WasmFunctionBuilder::EmitWithU8(WasmOpcode opcode, const byte immediate) {
|
|
|
|
body_.push_back(static_cast<byte>(opcode));
|
|
|
|
body_.push_back(immediate);
|
|
|
|
}
|
|
|
|
|
2016-03-09 18:51:28 +00:00
|
|
|
void WasmFunctionBuilder::EmitWithU8U8(WasmOpcode opcode, const byte imm1,
|
|
|
|
const byte imm2) {
|
|
|
|
body_.push_back(static_cast<byte>(opcode));
|
|
|
|
body_.push_back(imm1);
|
|
|
|
body_.push_back(imm2);
|
|
|
|
}
|
|
|
|
|
2016-03-08 22:54:43 +00:00
|
|
|
void WasmFunctionBuilder::EmitWithVarInt(WasmOpcode opcode,
|
|
|
|
uint32_t immediate) {
|
|
|
|
body_.push_back(static_cast<byte>(opcode));
|
2016-04-21 10:14:34 +00:00
|
|
|
EmitVarInt(immediate);
|
2016-03-08 22:54:43 +00:00
|
|
|
}
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-04-29 09:15:26 +00:00
|
|
|
void WasmFunctionBuilder::EmitI32Const(int32_t value) {
|
|
|
|
// TODO(titzer): variable-length signed and unsigned i32 constants.
|
|
|
|
if (-128 <= value && value <= 127) {
|
|
|
|
EmitWithU8(kExprI8Const, static_cast<byte>(value));
|
|
|
|
} else {
|
|
|
|
byte code[] = {WASM_I32V_5(value)};
|
|
|
|
EmitCode(code, sizeof(code));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-27 12:15:23 +00:00
|
|
|
void WasmFunctionBuilder::SetExported() { exported_ = true; }
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-05-13 08:44:28 +00:00
|
|
|
void WasmFunctionBuilder::SetName(const char* name, int name_length) {
|
2016-01-13 01:23:43 +00:00
|
|
|
name_.clear();
|
|
|
|
if (name_length > 0) {
|
2016-06-23 22:26:06 +00:00
|
|
|
for (int i = 0; i < name_length; ++i) {
|
2016-01-13 01:23:43 +00:00
|
|
|
name_.push_back(*(name + i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-27 12:15:23 +00:00
|
|
|
void WasmFunctionBuilder::WriteSignature(ZoneBuffer& buffer) const {
|
2016-05-25 16:12:09 +00:00
|
|
|
buffer.write_u32v(signature_index_);
|
2015-12-11 12:26:16 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 12:15:23 +00:00
|
|
|
void WasmFunctionBuilder::WriteExport(ZoneBuffer& buffer,
|
2016-05-25 16:12:09 +00:00
|
|
|
uint32_t func_index) const {
|
|
|
|
if (exported_) {
|
|
|
|
buffer.write_u32v(func_index);
|
|
|
|
buffer.write_size(name_.size());
|
|
|
|
if (name_.size() > 0) {
|
|
|
|
buffer.write(reinterpret_cast<const byte*>(&name_[0]), name_.size());
|
2016-03-09 18:55:27 +00:00
|
|
|
}
|
2015-12-11 12:26:16 +00:00
|
|
|
}
|
2016-05-25 16:12:09 +00:00
|
|
|
}
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-05-27 12:15:23 +00:00
|
|
|
void WasmFunctionBuilder::WriteBody(ZoneBuffer& buffer) const {
|
2016-05-25 16:12:09 +00:00
|
|
|
size_t locals_size = locals_.Size();
|
|
|
|
buffer.write_size(locals_size + body_.size());
|
|
|
|
buffer.EnsureSpace(locals_size);
|
|
|
|
byte** ptr = buffer.pos_ptr();
|
|
|
|
locals_.Emit(*ptr);
|
|
|
|
(*ptr) += locals_size; // UGLY: manual bump of position pointer
|
2016-05-13 08:44:28 +00:00
|
|
|
if (body_.size() > 0) {
|
2016-05-25 16:12:09 +00:00
|
|
|
buffer.write(&body_[0], body_.size());
|
2015-12-11 12:26:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WasmDataSegmentEncoder::WasmDataSegmentEncoder(Zone* zone, const byte* data,
|
|
|
|
uint32_t size, uint32_t dest)
|
|
|
|
: data_(zone), dest_(dest) {
|
2016-06-23 22:26:06 +00:00
|
|
|
for (size_t i = 0; i < size; ++i) {
|
2015-12-11 12:26:16 +00:00
|
|
|
data_.push_back(data[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-25 16:12:09 +00:00
|
|
|
void WasmDataSegmentEncoder::Write(ZoneBuffer& buffer) const {
|
|
|
|
buffer.write_u32v(dest_);
|
|
|
|
buffer.write_u32v(static_cast<uint32_t>(data_.size()));
|
|
|
|
buffer.write(&data_[0], data_.size());
|
2015-12-11 12:26:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WasmModuleBuilder::WasmModuleBuilder(Zone* zone)
|
|
|
|
: zone_(zone),
|
|
|
|
signatures_(zone),
|
2016-05-13 08:44:28 +00:00
|
|
|
imports_(zone),
|
2015-12-11 12:26:16 +00:00
|
|
|
functions_(zone),
|
|
|
|
data_segments_(zone),
|
|
|
|
indirect_functions_(zone),
|
|
|
|
globals_(zone),
|
2016-03-01 05:49:51 +00:00
|
|
|
signature_map_(zone),
|
|
|
|
start_function_index_(-1) {}
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-05-13 08:44:28 +00:00
|
|
|
uint32_t WasmModuleBuilder::AddFunction() {
|
2016-05-27 12:15:23 +00:00
|
|
|
functions_.push_back(new (zone_) WasmFunctionBuilder(this));
|
2016-05-13 08:44:28 +00:00
|
|
|
return static_cast<uint32_t>(functions_.size() - 1);
|
2015-12-11 12:26:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
WasmFunctionBuilder* WasmModuleBuilder::FunctionAt(size_t index) {
|
|
|
|
if (functions_.size() > index) {
|
|
|
|
return functions_.at(index);
|
|
|
|
} else {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WasmModuleBuilder::AddDataSegment(WasmDataSegmentEncoder* data) {
|
|
|
|
data_segments_.push_back(data);
|
|
|
|
}
|
|
|
|
|
2016-01-20 23:36:42 +00:00
|
|
|
bool WasmModuleBuilder::CompareFunctionSigs::operator()(FunctionSig* a,
|
|
|
|
FunctionSig* b) const {
|
|
|
|
if (a->return_count() < b->return_count()) return true;
|
|
|
|
if (a->return_count() > b->return_count()) return false;
|
|
|
|
if (a->parameter_count() < b->parameter_count()) return true;
|
|
|
|
if (a->parameter_count() > b->parameter_count()) return false;
|
2015-12-11 12:26:16 +00:00
|
|
|
for (size_t r = 0; r < a->return_count(); r++) {
|
2016-01-20 23:36:42 +00:00
|
|
|
if (a->GetReturn(r) < b->GetReturn(r)) return true;
|
|
|
|
if (a->GetReturn(r) > b->GetReturn(r)) return false;
|
2015-12-11 12:26:16 +00:00
|
|
|
}
|
|
|
|
for (size_t p = 0; p < a->parameter_count(); p++) {
|
2016-01-20 23:36:42 +00:00
|
|
|
if (a->GetParam(p) < b->GetParam(p)) return true;
|
|
|
|
if (a->GetParam(p) > b->GetParam(p)) return false;
|
2015-12-11 12:26:16 +00:00
|
|
|
}
|
2016-01-20 23:36:42 +00:00
|
|
|
return false;
|
2015-12-11 12:26:16 +00:00
|
|
|
}
|
|
|
|
|
2016-05-13 08:44:28 +00:00
|
|
|
uint32_t WasmModuleBuilder::AddSignature(FunctionSig* sig) {
|
2015-12-11 12:26:16 +00:00
|
|
|
SignatureMap::iterator pos = signature_map_.find(sig);
|
|
|
|
if (pos != signature_map_.end()) {
|
|
|
|
return pos->second;
|
|
|
|
} else {
|
2016-05-17 17:53:46 +00:00
|
|
|
uint32_t index = static_cast<uint32_t>(signatures_.size());
|
2015-12-11 12:26:16 +00:00
|
|
|
signature_map_[sig] = index;
|
|
|
|
signatures_.push_back(sig);
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-17 17:53:46 +00:00
|
|
|
void WasmModuleBuilder::AddIndirectFunction(uint32_t index) {
|
2015-12-11 12:26:16 +00:00
|
|
|
indirect_functions_.push_back(index);
|
|
|
|
}
|
|
|
|
|
2016-05-13 08:44:28 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-05-17 17:53:46 +00:00
|
|
|
void WasmModuleBuilder::MarkStartFunction(uint32_t index) {
|
2016-03-01 05:49:51 +00:00
|
|
|
start_function_index_ = index;
|
|
|
|
}
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-07-21 12:34:28 +00:00
|
|
|
uint32_t WasmModuleBuilder::AddGlobal(LocalType type, bool exported) {
|
2015-12-11 12:26:16 +00:00
|
|
|
globals_.push_back(std::make_pair(type, exported));
|
|
|
|
return static_cast<uint32_t>(globals_.size() - 1);
|
|
|
|
}
|
|
|
|
|
2016-05-27 12:15:23 +00:00
|
|
|
void WasmModuleBuilder::WriteTo(ZoneBuffer& buffer) const {
|
2016-05-25 16:12:09 +00:00
|
|
|
uint32_t exports = 0;
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-05-25 16:12:09 +00:00
|
|
|
// == Emit magic =============================================================
|
2016-03-10 12:36:42 +00:00
|
|
|
TRACE("emit magic\n");
|
2016-05-25 16:12:09 +00:00
|
|
|
buffer.write_u32(kWasmMagic);
|
|
|
|
buffer.write_u32(kWasmVersion);
|
2016-02-28 00:37:11 +00:00
|
|
|
|
2016-05-25 16:12:09 +00:00
|
|
|
// == Emit signatures ========================================================
|
2015-12-11 12:26:16 +00:00
|
|
|
if (signatures_.size() > 0) {
|
2016-05-25 16:12:09 +00:00
|
|
|
size_t start = EmitSection(WasmSection::Code::Signatures, buffer);
|
|
|
|
buffer.write_size(signatures_.size());
|
2015-12-11 12:26:16 +00:00
|
|
|
|
|
|
|
for (FunctionSig* sig : signatures_) {
|
2016-05-25 16:12:09 +00:00
|
|
|
buffer.write_u8(kWasmFunctionTypeForm);
|
|
|
|
buffer.write_size(sig->parameter_count());
|
2015-12-11 12:26:16 +00:00
|
|
|
for (size_t j = 0; j < sig->parameter_count(); j++) {
|
2016-05-25 16:12:09 +00:00
|
|
|
buffer.write_u8(WasmOpcodes::LocalTypeCodeFor(sig->GetParam(j)));
|
2015-12-11 12:26:16 +00:00
|
|
|
}
|
2016-05-25 16:12:09 +00:00
|
|
|
buffer.write_size(sig->return_count());
|
2016-04-29 09:39:26 +00:00
|
|
|
for (size_t j = 0; j < sig->return_count(); j++) {
|
2016-05-25 16:12:09 +00:00
|
|
|
buffer.write_u8(WasmOpcodes::LocalTypeCodeFor(sig->GetReturn(j)));
|
2016-04-29 09:39:26 +00:00
|
|
|
}
|
2015-12-11 12:26:16 +00:00
|
|
|
}
|
2016-05-25 16:12:09 +00:00
|
|
|
FixupSection(buffer, start);
|
|
|
|
}
|
|
|
|
|
|
|
|
// == Emit globals ===========================================================
|
|
|
|
if (globals_.size() > 0) {
|
|
|
|
size_t start = EmitSection(WasmSection::Code::Globals, buffer);
|
|
|
|
buffer.write_size(globals_.size());
|
|
|
|
|
|
|
|
for (auto global : globals_) {
|
|
|
|
buffer.write_u32v(0); // Length of the global name.
|
2016-07-21 12:34:28 +00:00
|
|
|
buffer.write_u8(WasmOpcodes::LocalTypeCodeFor(global.first));
|
2016-05-25 16:12:09 +00:00
|
|
|
buffer.write_u8(global.second);
|
|
|
|
}
|
|
|
|
FixupSection(buffer, start);
|
2015-12-11 12:26:16 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 16:12:09 +00:00
|
|
|
// == Emit imports ===========================================================
|
2016-05-13 08:44:28 +00:00
|
|
|
if (imports_.size() > 0) {
|
2016-05-25 16:12:09 +00:00
|
|
|
size_t start = EmitSection(WasmSection::Code::ImportTable, buffer);
|
|
|
|
buffer.write_size(imports_.size());
|
2016-05-13 08:44:28 +00:00
|
|
|
for (auto import : imports_) {
|
2016-05-25 16:12:09 +00:00
|
|
|
buffer.write_u32v(import.sig_index);
|
|
|
|
buffer.write_u32v(import.name_length);
|
|
|
|
buffer.write(reinterpret_cast<const byte*>(import.name),
|
|
|
|
import.name_length);
|
|
|
|
buffer.write_u32v(0);
|
2016-05-13 08:44:28 +00:00
|
|
|
}
|
2016-05-25 16:12:09 +00:00
|
|
|
FixupSection(buffer, start);
|
2016-05-13 08:44:28 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 16:12:09 +00:00
|
|
|
// == Emit function signatures ===============================================
|
2015-12-11 12:26:16 +00:00
|
|
|
if (functions_.size() > 0) {
|
2016-05-25 16:12:09 +00:00
|
|
|
size_t start = EmitSection(WasmSection::Code::FunctionSignatures, buffer);
|
|
|
|
buffer.write_size(functions_.size());
|
|
|
|
for (auto function : functions_) {
|
|
|
|
function->WriteSignature(buffer);
|
|
|
|
if (function->exported()) exports++;
|
2015-12-11 12:26:16 +00:00
|
|
|
}
|
2016-05-25 16:12:09 +00:00
|
|
|
FixupSection(buffer, start);
|
2015-12-11 12:26:16 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 16:12:09 +00:00
|
|
|
// == emit function table ====================================================
|
2016-04-21 11:18:41 +00:00
|
|
|
if (indirect_functions_.size() > 0) {
|
2016-05-25 16:12:09 +00:00
|
|
|
size_t start = EmitSection(WasmSection::Code::FunctionTable, buffer);
|
|
|
|
buffer.write_size(indirect_functions_.size());
|
2016-04-21 11:18:41 +00:00
|
|
|
|
|
|
|
for (auto index : indirect_functions_) {
|
2016-05-25 16:12:09 +00:00
|
|
|
buffer.write_u32v(index);
|
2016-04-21 11:18:41 +00:00
|
|
|
}
|
2016-05-25 16:12:09 +00:00
|
|
|
FixupSection(buffer, start);
|
2016-04-21 11:18:41 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 16:12:09 +00:00
|
|
|
// == emit memory declaration ================================================
|
2016-04-21 11:18:41 +00:00
|
|
|
{
|
2016-05-25 16:12:09 +00:00
|
|
|
size_t start = EmitSection(WasmSection::Code::Memory, buffer);
|
|
|
|
buffer.write_u32v(16); // min memory size
|
|
|
|
buffer.write_u32v(16); // max memory size
|
|
|
|
buffer.write_u8(0); // memory export
|
2016-04-21 11:18:41 +00:00
|
|
|
static_assert(kDeclMemorySize == 3, "memory size must match emit above");
|
2016-05-25 16:12:09 +00:00
|
|
|
FixupSection(buffer, start);
|
2016-04-21 11:18:41 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 16:12:09 +00:00
|
|
|
// == emit exports ===========================================================
|
|
|
|
if (exports > 0) {
|
|
|
|
size_t start = EmitSection(WasmSection::Code::ExportTable, buffer);
|
|
|
|
buffer.write_u32v(exports);
|
|
|
|
uint32_t index = 0;
|
|
|
|
for (auto function : functions_) {
|
|
|
|
function->WriteExport(buffer, index++);
|
|
|
|
}
|
|
|
|
FixupSection(buffer, start);
|
|
|
|
}
|
|
|
|
|
|
|
|
// == emit start function index ==============================================
|
2016-03-01 05:49:51 +00:00
|
|
|
if (start_function_index_ >= 0) {
|
2016-05-25 16:12:09 +00:00
|
|
|
size_t start = EmitSection(WasmSection::Code::StartFunction, buffer);
|
|
|
|
buffer.write_u32v(start_function_index_);
|
|
|
|
FixupSection(buffer, start);
|
2016-03-01 05:49:51 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 16:12:09 +00:00
|
|
|
// == emit code ==============================================================
|
|
|
|
if (functions_.size() > 0) {
|
|
|
|
size_t start = EmitSection(WasmSection::Code::FunctionBodies, buffer);
|
|
|
|
buffer.write_size(functions_.size());
|
|
|
|
for (auto function : functions_) {
|
|
|
|
function->WriteBody(buffer);
|
|
|
|
}
|
|
|
|
FixupSection(buffer, start);
|
|
|
|
}
|
|
|
|
|
|
|
|
// == emit data segments =====================================================
|
2015-12-11 12:26:16 +00:00
|
|
|
if (data_segments_.size() > 0) {
|
2016-05-25 16:12:09 +00:00
|
|
|
size_t start = EmitSection(WasmSection::Code::DataSegments, buffer);
|
|
|
|
buffer.write_size(data_segments_.size());
|
2015-12-11 12:26:16 +00:00
|
|
|
|
|
|
|
for (auto segment : data_segments_) {
|
2016-05-25 16:12:09 +00:00
|
|
|
segment->Write(buffer);
|
2015-12-11 12:26:16 +00:00
|
|
|
}
|
2016-05-25 16:12:09 +00:00
|
|
|
FixupSection(buffer, start);
|
2015-12-11 12:26:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace wasm
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|