5 #ifndef V8_WASM_WASM_MODULE_BUILDER_H_ 6 #define V8_WASM_WASM_MODULE_BUILDER_H_ 8 #include "src/signature.h" 9 #include "src/zone/zone-containers.h" 11 #include "src/v8memory.h" 12 #include "src/wasm/leb-helper.h" 13 #include "src/wasm/local-decl-encoder.h" 14 #include "src/wasm/wasm-opcodes.h" 15 #include "src/wasm/wasm-result.h" 23 static constexpr
size_t kInitialSize = 1024;
25 : zone_(zone), buffer_(reinterpret_cast<byte*>(zone->New(initial))) {
27 end_ = buffer_ + initial;
30 void write_u8(uint8_t x) {
35 void write_u16(uint16_t x) {
37 WriteLittleEndianValue<uint16_t>(
reinterpret_cast<Address>(pos_), x);
43 WriteLittleEndianValue<uint32_t>(
reinterpret_cast<Address>(pos_), x);
47 void write_u64(uint64_t x) {
49 WriteLittleEndianValue<uint64_t>(
reinterpret_cast<Address>(pos_), x);
55 LEBHelper::write_u32v(&pos_, val);
58 void write_i32v(int32_t val) {
60 LEBHelper::write_i32v(&pos_, val);
63 void write_u64v(uint64_t val) {
65 LEBHelper::write_u64v(&pos_, val);
70 LEBHelper::write_i64v(&pos_, val);
73 void write_size(
size_t val) {
75 DCHECK_EQ(val, static_cast<uint32_t>(val));
76 LEBHelper::write_u32v(&pos_, static_cast<uint32_t>(val));
79 void write_f32(
float val) { write_u32(bit_cast<uint32_t>(val)); }
81 void write_f64(
double val) { write_u64(bit_cast<uint64_t>(val)); }
83 void write(
const byte* data,
size_t size) {
85 memcpy(pos_, data, size);
90 write_size(name.length());
91 write(reinterpret_cast<const byte*>(name.start()), name.length());
94 size_t reserve_u32v() {
95 size_t off = offset();
97 pos_ += kMaxVarInt32Size;
102 void patch_u32v(
size_t offset,
uint32_t val) {
103 byte* ptr = buffer_ + offset;
104 for (
size_t pos = 0; pos != kPaddedVarInt32Size; ++pos) {
106 byte out =
static_cast<byte
>(val & 0x7f);
107 if (pos != kPaddedVarInt32Size - 1) {
108 *(ptr++) = 0x80 | out;
116 void patch_u8(
size_t offset, byte val) {
117 DCHECK_GE(size(), offset);
118 buffer_[offset] = val;
121 size_t offset()
const {
return static_cast<size_t>(pos_ - buffer_); }
122 size_t size()
const {
return static_cast<size_t>(pos_ - buffer_); }
123 const byte* begin()
const {
return buffer_; }
124 const byte* end()
const {
return pos_; }
127 if ((pos_ + size) > end_) {
128 size_t new_size = size + (end_ - buffer_) * 2;
129 byte* new_buffer =
reinterpret_cast<byte*
>(zone_->New(new_size));
130 memcpy(new_buffer, buffer_, (pos_ - buffer_));
131 pos_ = new_buffer + (pos_ - buffer_);
132 buffer_ = new_buffer;
133 end_ = new_buffer + new_size;
135 DCHECK(pos_ + size <= end_);
138 void Truncate(
size_t size) {
139 DCHECK_GE(offset(), size);
140 pos_ = buffer_ + size;
143 byte** pos_ptr() {
return &pos_; }
159 void EmitI32V(int32_t val);
161 void EmitCode(
const byte* code,
uint32_t code_size);
162 void Emit(WasmOpcode opcode);
166 void EmitI32Const(int32_t val);
167 void EmitI64Const(
int64_t val);
168 void EmitF32Const(
float val);
169 void EmitF64Const(
double val);
170 void EmitWithU8(WasmOpcode opcode,
const byte immediate);
171 void EmitWithU8U8(WasmOpcode opcode,
const byte imm1,
const byte imm2);
172 void EmitWithI32V(WasmOpcode opcode, int32_t immediate);
173 void EmitWithU32V(WasmOpcode opcode,
uint32_t immediate);
174 void EmitDirectCallIndex(
uint32_t index);
176 void AddAsmWasmOffset(
size_t call_position,
size_t to_number_position);
177 void SetAsmFunctionStartPosition(
size_t function_position);
179 size_t GetPosition()
const {
return body_.size(); }
180 void FixupByte(
size_t position, byte value) {
181 body_.patch_u8(position, value);
183 void DeleteCodeAfter(
size_t position);
185 void WriteSignature(
ZoneBuffer& buffer)
const;
187 void WriteAsmWasmOffsetTable(
ZoneBuffer& buffer)
const;
190 uint32_t func_index() {
return func_index_; }
197 struct DirectCallIndex {
217 uint32_t last_asm_source_position_ = 0;
218 uint32_t asm_func_start_source_position_ = 0;
228 uint32_t AddGlobal(ValueType
type,
bool exported,
bool mutability =
true,
237 void SetMinMemorySize(
uint32_t value);
238 void SetMaxMemorySize(
uint32_t value);
239 void SetHasSharedMemory();
243 void WriteAsmJsOffsetTable(
ZoneBuffer& buffer)
const;
245 Zone* zone() {
return zone_; }
250 struct WasmFunctionImport {
255 struct WasmFunctionExport {
260 struct WasmGlobalImport {
262 ValueTypeCode type_code;
272 struct WasmDataSegment {
288 int start_function_index_;
291 bool has_max_memory_size_;
292 bool has_shared_memory_;
295 inline FunctionSig* WasmFunctionBuilder::signature() {
296 return builder_->signatures_[signature_index_];
303 #endif // V8_WASM_WASM_MODULE_BUILDER_H_