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.
|
|
|
|
|
2016-09-29 11:29:05 +00:00
|
|
|
#ifndef V8_WASM_WASM_MODULE_BUILDER_H_
|
|
|
|
#define V8_WASM_WASM_MODULE_BUILDER_H_
|
2015-12-11 12:26:16 +00:00
|
|
|
|
|
|
|
#include "src/signature.h"
|
2016-09-20 16:07:25 +00:00
|
|
|
#include "src/zone/zone-containers.h"
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-05-25 16:12:09 +00:00
|
|
|
#include "src/wasm/leb-helper.h"
|
2016-05-17 17:53:46 +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/wasm/wasm-result.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace wasm {
|
|
|
|
|
2016-05-25 16:12:09 +00:00
|
|
|
class ZoneBuffer : public ZoneObject {
|
|
|
|
public:
|
|
|
|
static const uint32_t kInitialSize = 4096;
|
|
|
|
explicit ZoneBuffer(Zone* zone, size_t initial = kInitialSize)
|
|
|
|
: zone_(zone), buffer_(reinterpret_cast<byte*>(zone->New(initial))) {
|
|
|
|
pos_ = buffer_;
|
|
|
|
end_ = buffer_ + initial;
|
|
|
|
}
|
|
|
|
|
|
|
|
void write_u8(uint8_t x) {
|
|
|
|
EnsureSpace(1);
|
|
|
|
*(pos_++) = x;
|
|
|
|
}
|
|
|
|
|
|
|
|
void write_u16(uint16_t x) {
|
|
|
|
EnsureSpace(2);
|
2016-06-29 09:25:24 +00:00
|
|
|
WriteLittleEndianValue<uint16_t>(pos_, x);
|
2016-05-25 16:12:09 +00:00
|
|
|
pos_ += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
void write_u32(uint32_t x) {
|
|
|
|
EnsureSpace(4);
|
2016-06-29 09:25:24 +00:00
|
|
|
WriteLittleEndianValue<uint32_t>(pos_, x);
|
2016-05-25 16:12:09 +00:00
|
|
|
pos_ += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
void write_u32v(uint32_t val) {
|
|
|
|
EnsureSpace(kMaxVarInt32Size);
|
|
|
|
LEBHelper::write_u32v(&pos_, val);
|
|
|
|
}
|
|
|
|
|
2016-10-11 09:03:22 +00:00
|
|
|
void write_i32v(int32_t val) {
|
|
|
|
EnsureSpace(kMaxVarInt32Size);
|
|
|
|
LEBHelper::write_i32v(&pos_, val);
|
|
|
|
}
|
|
|
|
|
2016-05-25 16:12:09 +00:00
|
|
|
void write_size(size_t val) {
|
|
|
|
EnsureSpace(kMaxVarInt32Size);
|
|
|
|
DCHECK_EQ(val, static_cast<uint32_t>(val));
|
|
|
|
LEBHelper::write_u32v(&pos_, static_cast<uint32_t>(val));
|
|
|
|
}
|
|
|
|
|
|
|
|
void write(const byte* data, size_t size) {
|
|
|
|
EnsureSpace(size);
|
|
|
|
memcpy(pos_, data, size);
|
|
|
|
pos_ += size;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t reserve_u32v() {
|
|
|
|
size_t off = offset();
|
|
|
|
EnsureSpace(kMaxVarInt32Size);
|
|
|
|
pos_ += kMaxVarInt32Size;
|
|
|
|
return off;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Patch a (padded) u32v at the given offset to be the given value.
|
|
|
|
void patch_u32v(size_t offset, uint32_t val) {
|
|
|
|
byte* ptr = buffer_ + offset;
|
|
|
|
for (size_t pos = 0; pos != kPaddedVarInt32Size; ++pos) {
|
|
|
|
uint32_t next = val >> 7;
|
|
|
|
byte out = static_cast<byte>(val & 0x7f);
|
|
|
|
if (pos != kPaddedVarInt32Size - 1) {
|
|
|
|
*(ptr++) = 0x80 | out;
|
|
|
|
val = next;
|
|
|
|
} else {
|
|
|
|
*(ptr++) = out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-12 09:17:12 +00:00
|
|
|
size_t offset() const { return static_cast<size_t>(pos_ - buffer_); }
|
|
|
|
size_t size() const { return static_cast<size_t>(pos_ - buffer_); }
|
|
|
|
const byte* begin() const { return buffer_; }
|
|
|
|
const byte* end() const { return pos_; }
|
2016-05-25 16:12:09 +00:00
|
|
|
|
|
|
|
void EnsureSpace(size_t size) {
|
|
|
|
if ((pos_ + size) > end_) {
|
2016-09-20 14:03:08 +00:00
|
|
|
size_t new_size = 4096 + size + (end_ - buffer_) * 3;
|
2016-05-25 16:12:09 +00:00
|
|
|
byte* new_buffer = reinterpret_cast<byte*>(zone_->New(new_size));
|
|
|
|
memcpy(new_buffer, buffer_, (pos_ - buffer_));
|
|
|
|
pos_ = new_buffer + (pos_ - buffer_);
|
|
|
|
buffer_ = new_buffer;
|
|
|
|
end_ = new_buffer + new_size;
|
|
|
|
}
|
2016-09-20 14:03:08 +00:00
|
|
|
DCHECK(pos_ + size <= end_);
|
2016-05-25 16:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
byte** pos_ptr() { return &pos_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Zone* zone_;
|
|
|
|
byte* buffer_;
|
|
|
|
byte* pos_;
|
|
|
|
byte* end_;
|
|
|
|
};
|
|
|
|
|
2015-12-11 12:26:16 +00:00
|
|
|
class WasmModuleBuilder;
|
|
|
|
|
2016-09-26 07:40:24 +00:00
|
|
|
class V8_EXPORT_PRIVATE WasmFunctionBuilder : public ZoneObject {
|
2015-12-11 12:26:16 +00:00
|
|
|
public:
|
2016-05-27 12:15:23 +00:00
|
|
|
// Building methods.
|
2016-05-17 17:53:46 +00:00
|
|
|
void SetSignature(FunctionSig* sig);
|
|
|
|
uint32_t AddLocal(LocalType type);
|
2016-04-21 10:14:34 +00:00
|
|
|
void EmitVarInt(uint32_t val);
|
2015-12-11 12:26:16 +00:00
|
|
|
void EmitCode(const byte* code, uint32_t code_size);
|
|
|
|
void Emit(WasmOpcode opcode);
|
2016-04-18 20:57:05 +00:00
|
|
|
void EmitGetLocal(uint32_t index);
|
|
|
|
void EmitSetLocal(uint32_t index);
|
2016-09-27 20:46:10 +00:00
|
|
|
void EmitTeeLocal(uint32_t index);
|
2016-04-29 09:15:26 +00:00
|
|
|
void EmitI32Const(int32_t val);
|
2015-12-11 12:26:16 +00:00
|
|
|
void EmitWithU8(WasmOpcode opcode, const byte immediate);
|
2016-03-09 18:51:28 +00:00
|
|
|
void EmitWithU8U8(WasmOpcode opcode, const byte imm1, const byte imm2);
|
2016-03-08 22:54:43 +00:00
|
|
|
void EmitWithVarInt(WasmOpcode opcode, uint32_t immediate);
|
2016-10-03 21:04:29 +00:00
|
|
|
void EmitDirectCallIndex(uint32_t index);
|
2016-10-11 10:35:49 +00:00
|
|
|
void ExportAs(Vector<const char> name);
|
|
|
|
void SetName(Vector<const char> name);
|
2016-12-09 10:29:53 +00:00
|
|
|
void AddAsmWasmOffset(int call_position, int to_number_position);
|
2016-05-27 12:15:23 +00:00
|
|
|
|
|
|
|
void WriteSignature(ZoneBuffer& buffer) const;
|
2016-12-01 10:12:39 +00:00
|
|
|
void WriteExports(ZoneBuffer& buffer) const;
|
2016-05-27 12:15:23 +00:00
|
|
|
void WriteBody(ZoneBuffer& buffer) const;
|
2016-10-12 09:17:12 +00:00
|
|
|
void WriteAsmWasmOffsetTable(ZoneBuffer& buffer) const;
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-09-27 20:46:10 +00:00
|
|
|
uint32_t func_index() { return func_index_; }
|
|
|
|
FunctionSig* signature();
|
|
|
|
|
2015-12-11 12:26:16 +00:00
|
|
|
private:
|
2016-05-27 12:15:23 +00:00
|
|
|
explicit WasmFunctionBuilder(WasmModuleBuilder* builder);
|
2015-12-11 12:26:16 +00:00
|
|
|
friend class WasmModuleBuilder;
|
2016-08-30 09:42:34 +00:00
|
|
|
friend class WasmTemporary;
|
2016-10-03 21:04:29 +00:00
|
|
|
|
|
|
|
struct DirectCallIndex {
|
|
|
|
size_t offset;
|
|
|
|
uint32_t direct_index;
|
|
|
|
};
|
|
|
|
|
2016-05-27 12:15:23 +00:00
|
|
|
WasmModuleBuilder* builder_;
|
2016-05-17 17:53:46 +00:00
|
|
|
LocalDeclEncoder locals_;
|
2016-05-27 12:15:23 +00:00
|
|
|
uint32_t signature_index_;
|
2016-09-27 20:46:10 +00:00
|
|
|
uint32_t func_index_;
|
2015-12-11 12:26:16 +00:00
|
|
|
ZoneVector<uint8_t> body_;
|
|
|
|
ZoneVector<char> name_;
|
2016-12-01 10:12:39 +00:00
|
|
|
ZoneVector<ZoneVector<char>> exported_names_;
|
2016-08-30 09:42:34 +00:00
|
|
|
ZoneVector<uint32_t> i32_temps_;
|
|
|
|
ZoneVector<uint32_t> i64_temps_;
|
|
|
|
ZoneVector<uint32_t> f32_temps_;
|
|
|
|
ZoneVector<uint32_t> f64_temps_;
|
2016-10-03 21:04:29 +00:00
|
|
|
ZoneVector<DirectCallIndex> direct_calls_;
|
2016-10-12 09:17:12 +00:00
|
|
|
|
|
|
|
// Delta-encoded mapping from wasm bytes to asm.js source positions.
|
|
|
|
ZoneBuffer asm_offsets_;
|
|
|
|
uint32_t last_asm_byte_offset_ = 0;
|
|
|
|
uint32_t last_asm_source_position_ = 0;
|
2016-08-30 09:42:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class WasmTemporary {
|
|
|
|
public:
|
|
|
|
WasmTemporary(WasmFunctionBuilder* builder, LocalType type) {
|
|
|
|
switch (type) {
|
|
|
|
case kAstI32:
|
|
|
|
temporary_ = &builder->i32_temps_;
|
|
|
|
break;
|
|
|
|
case kAstI64:
|
|
|
|
temporary_ = &builder->i64_temps_;
|
|
|
|
break;
|
|
|
|
case kAstF32:
|
|
|
|
temporary_ = &builder->f32_temps_;
|
|
|
|
break;
|
|
|
|
case kAstF64:
|
|
|
|
temporary_ = &builder->f64_temps_;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
temporary_ = nullptr;
|
|
|
|
}
|
|
|
|
if (temporary_->size() == 0) {
|
|
|
|
// Allocate a new temporary.
|
|
|
|
index_ = builder->AddLocal(type);
|
|
|
|
} else {
|
|
|
|
// Reuse a previous temporary.
|
|
|
|
index_ = temporary_->back();
|
|
|
|
temporary_->pop_back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
~WasmTemporary() {
|
|
|
|
temporary_->push_back(index_); // return the temporary to the list.
|
|
|
|
}
|
|
|
|
uint32_t index() { return index_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
ZoneVector<uint32_t>* temporary_;
|
|
|
|
uint32_t index_;
|
2015-12-11 12:26:16 +00:00
|
|
|
};
|
|
|
|
|
2016-09-26 07:40:24 +00:00
|
|
|
class V8_EXPORT_PRIVATE WasmModuleBuilder : public ZoneObject {
|
2015-12-11 12:26:16 +00:00
|
|
|
public:
|
|
|
|
explicit WasmModuleBuilder(Zone* zone);
|
2016-05-27 12:15:23 +00:00
|
|
|
|
|
|
|
// Building methods.
|
2016-09-27 20:46:10 +00:00
|
|
|
uint32_t AddImport(const char* name, int name_length, FunctionSig* sig);
|
|
|
|
void SetImportName(uint32_t index, const char* name, int name_length) {
|
|
|
|
imports_[index].name = name;
|
|
|
|
imports_[index].name_length = name_length;
|
|
|
|
}
|
|
|
|
WasmFunctionBuilder* AddFunction(FunctionSig* sig = nullptr);
|
2016-10-06 15:43:10 +00:00
|
|
|
uint32_t AddGlobal(LocalType type, bool exported, bool mutability = true,
|
|
|
|
const WasmInitExpr& init = WasmInitExpr());
|
2016-09-29 18:12:45 +00:00
|
|
|
void AddDataSegment(const byte* data, uint32_t size, uint32_t dest);
|
2016-05-13 08:44:28 +00:00
|
|
|
uint32_t AddSignature(FunctionSig* sig);
|
2016-11-30 00:25:21 +00:00
|
|
|
uint32_t AllocateIndirectFunctions(uint32_t count);
|
|
|
|
void SetIndirectFunction(uint32_t indirect, uint32_t direct);
|
2016-09-27 20:46:10 +00:00
|
|
|
void MarkStartFunction(WasmFunctionBuilder* builder);
|
2016-05-27 12:15:23 +00:00
|
|
|
|
|
|
|
// Writing methods.
|
|
|
|
void WriteTo(ZoneBuffer& buffer) const;
|
2016-10-12 09:17:12 +00:00
|
|
|
void WriteAsmJsOffsetTable(ZoneBuffer& buffer) const;
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-10-11 12:40:24 +00:00
|
|
|
// TODO(titzer): use SignatureMap from signature-map.h here.
|
|
|
|
// This signature map is zone-allocated, but the other is heap allocated.
|
2015-12-11 12:26:16 +00:00
|
|
|
struct CompareFunctionSigs {
|
2016-01-20 23:36:42 +00:00
|
|
|
bool operator()(FunctionSig* a, FunctionSig* b) const;
|
2015-12-11 12:26:16 +00:00
|
|
|
};
|
2016-05-17 17:53:46 +00:00
|
|
|
typedef ZoneMap<FunctionSig*, uint32_t, CompareFunctionSigs> SignatureMap;
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-05-27 12:15:23 +00:00
|
|
|
Zone* zone() { return zone_; }
|
|
|
|
|
2016-09-27 20:46:10 +00:00
|
|
|
FunctionSig* GetSignature(uint32_t index) { return signatures_[index]; }
|
|
|
|
|
2016-02-05 02:07:23 +00:00
|
|
|
private:
|
2016-09-29 18:12:45 +00:00
|
|
|
struct WasmFunctionImport {
|
|
|
|
uint32_t sig_index;
|
|
|
|
const char* name;
|
|
|
|
int name_length;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct WasmGlobal {
|
|
|
|
LocalType type;
|
|
|
|
bool exported;
|
|
|
|
bool mutability;
|
2016-10-06 15:43:10 +00:00
|
|
|
WasmInitExpr init;
|
2016-09-29 18:12:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct WasmDataSegment {
|
|
|
|
ZoneVector<byte> data;
|
|
|
|
uint32_t dest;
|
|
|
|
};
|
|
|
|
|
2016-09-27 20:46:10 +00:00
|
|
|
friend class WasmFunctionBuilder;
|
2015-12-11 12:26:16 +00:00
|
|
|
Zone* zone_;
|
|
|
|
ZoneVector<FunctionSig*> signatures_;
|
2016-05-13 08:44:28 +00:00
|
|
|
ZoneVector<WasmFunctionImport> imports_;
|
2015-12-11 12:26:16 +00:00
|
|
|
ZoneVector<WasmFunctionBuilder*> functions_;
|
2016-09-29 18:12:45 +00:00
|
|
|
ZoneVector<WasmDataSegment> data_segments_;
|
2016-05-17 17:53:46 +00:00
|
|
|
ZoneVector<uint32_t> indirect_functions_;
|
2016-09-29 18:12:45 +00:00
|
|
|
ZoneVector<WasmGlobal> globals_;
|
2015-12-11 12:26:16 +00:00
|
|
|
SignatureMap signature_map_;
|
2016-03-01 05:49:51 +00:00
|
|
|
int start_function_index_;
|
2015-12-11 12:26:16 +00:00
|
|
|
};
|
|
|
|
|
2016-09-27 20:46:10 +00:00
|
|
|
inline FunctionSig* WasmFunctionBuilder::signature() {
|
|
|
|
return builder_->signatures_[signature_index_];
|
|
|
|
}
|
|
|
|
|
2015-12-11 12:26:16 +00:00
|
|
|
} // namespace wasm
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|
|
|
|
|
2016-09-29 11:29:05 +00:00
|
|
|
#endif // V8_WASM_WASM_MODULE_BUILDER_H_
|