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.
|
|
|
|
|
|
|
|
#ifndef V8_WASM_ENCODER_H_
|
|
|
|
#define V8_WASM_ENCODER_H_
|
|
|
|
|
|
|
|
#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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t offset() { return static_cast<size_t>(pos_ - buffer_); }
|
|
|
|
size_t size() { return static_cast<size_t>(pos_ - buffer_); }
|
|
|
|
const byte* begin() { return buffer_; }
|
|
|
|
const byte* end() { return pos_; }
|
|
|
|
|
|
|
|
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-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-05-27 12:15:23 +00:00
|
|
|
void SetExported();
|
2016-05-13 08:44:28 +00:00
|
|
|
void SetName(const char* name, int name_length);
|
2016-09-23 17:58:07 +00:00
|
|
|
bool exported() { return exported_; }
|
2016-05-27 12:15:23 +00:00
|
|
|
|
2016-09-23 17:58:07 +00:00
|
|
|
// Writing methods.
|
2016-05-27 12:15:23 +00:00
|
|
|
void WriteSignature(ZoneBuffer& buffer) const;
|
2016-09-23 17:58:07 +00:00
|
|
|
void WriteExport(ZoneBuffer& buffer, uint32_t func_index) const;
|
2016-05-27 12:15:23 +00:00
|
|
|
void WriteBody(ZoneBuffer& buffer) const;
|
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-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_;
|
|
|
|
bool exported_;
|
2015-12-11 12:26:16 +00:00
|
|
|
ZoneVector<uint8_t> body_;
|
|
|
|
ZoneVector<char> name_;
|
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_;
|
|
|
|
};
|
|
|
|
|
|
|
|
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-05-27 12:15:23 +00:00
|
|
|
// TODO(titzer): kill!
|
2015-12-11 12:26:16 +00:00
|
|
|
class WasmDataSegmentEncoder : public ZoneObject {
|
|
|
|
public:
|
|
|
|
WasmDataSegmentEncoder(Zone* zone, const byte* data, uint32_t size,
|
|
|
|
uint32_t dest);
|
2016-05-25 16:12:09 +00:00
|
|
|
void Write(ZoneBuffer& buffer) const;
|
2015-12-11 12:26:16 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
ZoneVector<byte> data_;
|
|
|
|
uint32_t dest_;
|
|
|
|
};
|
|
|
|
|
2016-05-13 08:44:28 +00:00
|
|
|
struct WasmFunctionImport {
|
|
|
|
uint32_t sig_index;
|
|
|
|
const char* name;
|
|
|
|
int name_length;
|
|
|
|
};
|
|
|
|
|
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-23 17:58:07 +00:00
|
|
|
uint32_t AddFunction();
|
|
|
|
uint32_t AddGlobal(LocalType type, bool exported);
|
|
|
|
WasmFunctionBuilder* FunctionAt(size_t index);
|
2015-12-11 12:26:16 +00:00
|
|
|
void AddDataSegment(WasmDataSegmentEncoder* data);
|
2016-05-13 08:44:28 +00:00
|
|
|
uint32_t AddSignature(FunctionSig* sig);
|
2016-05-17 17:53:46 +00:00
|
|
|
void AddIndirectFunction(uint32_t index);
|
2016-09-23 17:58:07 +00:00
|
|
|
void MarkStartFunction(uint32_t index);
|
|
|
|
uint32_t AddImport(const char* name, int name_length, FunctionSig* sig);
|
2016-05-27 12:15:23 +00:00
|
|
|
|
|
|
|
// Writing methods.
|
|
|
|
void WriteTo(ZoneBuffer& buffer) const;
|
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-02-05 02:07:23 +00:00
|
|
|
private:
|
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_;
|
|
|
|
ZoneVector<WasmDataSegmentEncoder*> data_segments_;
|
2016-05-17 17:53:46 +00:00
|
|
|
ZoneVector<uint32_t> indirect_functions_;
|
2016-09-23 17:58:07 +00:00
|
|
|
ZoneVector<std::pair<LocalType, bool>> 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
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace wasm
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|
|
|
|
|
|
|
|
#endif // V8_WASM_ENCODER_H_
|