Line data Source code
1 : // Copyright 2015 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #ifndef V8_WASM_WASM_MODULE_BUILDER_H_
6 : #define V8_WASM_WASM_MODULE_BUILDER_H_
7 :
8 : #include "src/signature.h"
9 : #include "src/zone/zone-containers.h"
10 :
11 : #include "src/v8memory.h"
12 : #include "src/vector.h"
13 : #include "src/wasm/leb-helper.h"
14 : #include "src/wasm/local-decl-encoder.h"
15 : #include "src/wasm/wasm-opcodes.h"
16 : #include "src/wasm/wasm-result.h"
17 :
18 : namespace v8 {
19 : namespace internal {
20 : namespace wasm {
21 :
22 : class ZoneBuffer : public ZoneObject {
23 : public:
24 : static constexpr size_t kInitialSize = 1024;
25 : explicit ZoneBuffer(Zone* zone, size_t initial = kInitialSize)
26 36507 : : zone_(zone), buffer_(reinterpret_cast<byte*>(zone->New(initial))) {
27 36507 : pos_ = buffer_;
28 36507 : end_ = buffer_ + initial;
29 : }
30 :
31 : void write_u8(uint8_t x) {
32 14559089 : EnsureSpace(1);
33 14559090 : *(pos_++) = x;
34 : }
35 :
36 : void write_u16(uint16_t x) {
37 : EnsureSpace(2);
38 : WriteLittleEndianValue<uint16_t>(reinterpret_cast<Address>(pos_), x);
39 : pos_ += 2;
40 : }
41 :
42 5718 : void write_u32(uint32_t x) {
43 5718 : EnsureSpace(4);
44 5718 : WriteLittleEndianValue<uint32_t>(reinterpret_cast<Address>(pos_), x);
45 5718 : pos_ += 4;
46 5718 : }
47 :
48 5009786 : void write_u64(uint64_t x) {
49 5009786 : EnsureSpace(8);
50 5009786 : WriteLittleEndianValue<uint64_t>(reinterpret_cast<Address>(pos_), x);
51 5009786 : pos_ += 8;
52 5009786 : }
53 :
54 1408702 : void write_u32v(uint32_t val) {
55 1408702 : EnsureSpace(kMaxVarInt32Size);
56 : LEBHelper::write_u32v(&pos_, val);
57 1408702 : }
58 :
59 : void write_i32v(int32_t val) {
60 1655592 : EnsureSpace(kMaxVarInt32Size);
61 1655592 : LEBHelper::write_i32v(&pos_, val);
62 : }
63 :
64 : void write_u64v(uint64_t val) {
65 : EnsureSpace(kMaxVarInt64Size);
66 : LEBHelper::write_u64v(&pos_, val);
67 : }
68 :
69 : void write_i64v(int64_t val) {
70 0 : EnsureSpace(kMaxVarInt64Size);
71 0 : LEBHelper::write_i64v(&pos_, val);
72 : }
73 :
74 94181 : void write_size(size_t val) {
75 94181 : EnsureSpace(kMaxVarInt32Size);
76 : DCHECK_EQ(val, static_cast<uint32_t>(val));
77 94181 : LEBHelper::write_u32v(&pos_, static_cast<uint32_t>(val));
78 94181 : }
79 :
80 92 : void write_f32(float val) { write_u32(bit_cast<uint32_t>(val)); }
81 :
82 5009786 : void write_f64(double val) { write_u64(bit_cast<uint64_t>(val)); }
83 :
84 83925 : void write(const byte* data, size_t size) {
85 83925 : EnsureSpace(size);
86 83925 : memcpy(pos_, data, size);
87 83925 : pos_ += size;
88 83925 : }
89 :
90 : void write_string(Vector<const char> name) {
91 21027 : write_size(name.length());
92 21027 : write(reinterpret_cast<const byte*>(name.start()), name.length());
93 : }
94 :
95 : size_t reserve_u32v() {
96 : size_t off = offset();
97 22621 : EnsureSpace(kMaxVarInt32Size);
98 22621 : pos_ += kMaxVarInt32Size;
99 : return off;
100 : }
101 :
102 : // Patch a (padded) u32v at the given offset to be the given value.
103 : void patch_u32v(size_t offset, uint32_t val) {
104 55851 : byte* ptr = buffer_ + offset;
105 614361 : for (size_t pos = 0; pos != kPaddedVarInt32Size; ++pos) {
106 279255 : uint32_t next = val >> 7;
107 279255 : byte out = static_cast<byte>(val & 0x7f);
108 279255 : if (pos != kPaddedVarInt32Size - 1) {
109 223404 : *(ptr++) = 0x80 | out;
110 : val = next;
111 : } else {
112 55851 : *(ptr++) = out;
113 : }
114 : }
115 : }
116 :
117 : void patch_u8(size_t offset, byte val) {
118 : DCHECK_GE(size(), offset);
119 7324 : buffer_[offset] = val;
120 : }
121 :
122 60049 : size_t offset() const { return static_cast<size_t>(pos_ - buffer_); }
123 570125 : size_t size() const { return static_cast<size_t>(pos_ - buffer_); }
124 : const byte* begin() const { return buffer_; }
125 : const byte* end() const { return pos_; }
126 :
127 22854398 : void EnsureSpace(size_t size) {
128 22854398 : if ((pos_ + size) > end_) {
129 16385 : size_t new_size = size + (end_ - buffer_) * 2;
130 16385 : byte* new_buffer = reinterpret_cast<byte*>(zone_->New(new_size));
131 16385 : memcpy(new_buffer, buffer_, (pos_ - buffer_));
132 16385 : pos_ = new_buffer + (pos_ - buffer_);
133 16385 : buffer_ = new_buffer;
134 16385 : end_ = new_buffer + new_size;
135 : }
136 : DCHECK(pos_ + size <= end_);
137 22854398 : }
138 :
139 : void Truncate(size_t size) {
140 : DCHECK_GE(offset(), size);
141 392914 : pos_ = buffer_ + size;
142 : }
143 :
144 : byte** pos_ptr() { return &pos_; }
145 :
146 : private:
147 : Zone* zone_;
148 : byte* buffer_;
149 : byte* pos_;
150 : byte* end_;
151 : };
152 :
153 : class WasmModuleBuilder;
154 :
155 : class V8_EXPORT_PRIVATE WasmFunctionBuilder : public ZoneObject {
156 : public:
157 : // Building methods.
158 : void SetSignature(FunctionSig* sig);
159 : uint32_t AddLocal(ValueType type);
160 : void EmitI32V(int32_t val);
161 : void EmitU32V(uint32_t val);
162 : void EmitCode(const byte* code, uint32_t code_size);
163 : void Emit(WasmOpcode opcode);
164 : void EmitGetLocal(uint32_t index);
165 : void EmitSetLocal(uint32_t index);
166 : void EmitTeeLocal(uint32_t index);
167 : void EmitI32Const(int32_t val);
168 : void EmitI64Const(int64_t val);
169 : void EmitF32Const(float val);
170 : void EmitF64Const(double val);
171 : void EmitWithU8(WasmOpcode opcode, const byte immediate);
172 : void EmitWithU8U8(WasmOpcode opcode, const byte imm1, const byte imm2);
173 : void EmitWithI32V(WasmOpcode opcode, int32_t immediate);
174 : void EmitWithU32V(WasmOpcode opcode, uint32_t immediate);
175 : void EmitDirectCallIndex(uint32_t index);
176 : void SetName(Vector<const char> name);
177 : void AddAsmWasmOffset(size_t call_position, size_t to_number_position);
178 : void SetAsmFunctionStartPosition(size_t function_position);
179 :
180 : size_t GetPosition() const { return body_.size(); }
181 : void FixupByte(size_t position, byte value) {
182 : body_.patch_u8(position, value);
183 : }
184 : void DeleteCodeAfter(size_t position);
185 :
186 : void WriteSignature(ZoneBuffer& buffer) const;
187 : void WriteBody(ZoneBuffer& buffer) const;
188 : void WriteAsmWasmOffsetTable(ZoneBuffer& buffer) const;
189 :
190 : WasmModuleBuilder* builder() const { return builder_; }
191 : uint32_t func_index() { return func_index_; }
192 : FunctionSig* signature();
193 :
194 : private:
195 : explicit WasmFunctionBuilder(WasmModuleBuilder* builder);
196 : friend class WasmModuleBuilder;
197 :
198 : struct DirectCallIndex {
199 : size_t offset;
200 : uint32_t direct_index;
201 : };
202 :
203 : WasmModuleBuilder* builder_;
204 : LocalDeclEncoder locals_;
205 : uint32_t signature_index_;
206 : uint32_t func_index_;
207 : ZoneBuffer body_;
208 : Vector<const char> name_;
209 : ZoneVector<uint32_t> i32_temps_;
210 : ZoneVector<uint32_t> i64_temps_;
211 : ZoneVector<uint32_t> f32_temps_;
212 : ZoneVector<uint32_t> f64_temps_;
213 : ZoneVector<DirectCallIndex> direct_calls_;
214 :
215 : // Delta-encoded mapping from wasm bytes to asm.js source positions.
216 : ZoneBuffer asm_offsets_;
217 : uint32_t last_asm_byte_offset_ = 0;
218 : uint32_t last_asm_source_position_ = 0;
219 : uint32_t asm_func_start_source_position_ = 0;
220 : };
221 :
222 54 : class V8_EXPORT_PRIVATE WasmModuleBuilder : public ZoneObject {
223 : public:
224 : explicit WasmModuleBuilder(Zone* zone);
225 :
226 : // Building methods.
227 : uint32_t AddImport(Vector<const char> name, FunctionSig* sig);
228 : WasmFunctionBuilder* AddFunction(FunctionSig* sig = nullptr);
229 : uint32_t AddGlobal(ValueType type, bool exported, bool mutability = true,
230 : const WasmInitExpr& init = WasmInitExpr());
231 : uint32_t AddGlobalImport(Vector<const char> name, ValueType type);
232 : void AddDataSegment(const byte* data, uint32_t size, uint32_t dest);
233 : uint32_t AddSignature(FunctionSig* sig);
234 : uint32_t AllocateIndirectFunctions(uint32_t count);
235 : void SetIndirectFunction(uint32_t indirect, uint32_t direct);
236 : void MarkStartFunction(WasmFunctionBuilder* builder);
237 : void AddExport(Vector<const char> name, WasmFunctionBuilder* builder);
238 : void SetMinMemorySize(uint32_t value);
239 : void SetMaxMemorySize(uint32_t value);
240 : void SetHasSharedMemory();
241 :
242 : // Writing methods.
243 : void WriteTo(ZoneBuffer& buffer) const;
244 : void WriteAsmJsOffsetTable(ZoneBuffer& buffer) const;
245 :
246 : Zone* zone() { return zone_; }
247 :
248 : FunctionSig* GetSignature(uint32_t index) { return signatures_[index]; }
249 :
250 : private:
251 : struct WasmFunctionImport {
252 : Vector<const char> name;
253 : uint32_t sig_index;
254 : };
255 :
256 : struct WasmFunctionExport {
257 : Vector<const char> name;
258 : uint32_t function_index;
259 : };
260 :
261 : struct WasmGlobalImport {
262 : Vector<const char> name;
263 : ValueTypeCode type_code;
264 : };
265 :
266 : struct WasmGlobal {
267 : ValueType type;
268 : bool exported;
269 : bool mutability;
270 : WasmInitExpr init;
271 : };
272 :
273 16 : struct WasmDataSegment {
274 : ZoneVector<byte> data;
275 : uint32_t dest;
276 : };
277 :
278 : friend class WasmFunctionBuilder;
279 : Zone* zone_;
280 : ZoneVector<FunctionSig*> signatures_;
281 : ZoneVector<WasmFunctionImport> function_imports_;
282 : ZoneVector<WasmFunctionExport> function_exports_;
283 : ZoneVector<WasmGlobalImport> global_imports_;
284 : ZoneVector<WasmFunctionBuilder*> functions_;
285 : ZoneVector<WasmDataSegment> data_segments_;
286 : ZoneVector<uint32_t> indirect_functions_;
287 : ZoneVector<WasmGlobal> globals_;
288 : ZoneUnorderedMap<FunctionSig, uint32_t> signature_map_;
289 : int start_function_index_;
290 : uint32_t min_memory_size_;
291 : uint32_t max_memory_size_;
292 : bool has_max_memory_size_;
293 : bool has_shared_memory_;
294 : };
295 :
296 : inline FunctionSig* WasmFunctionBuilder::signature() {
297 2 : return builder_->signatures_[signature_index_];
298 : }
299 :
300 : } // namespace wasm
301 : } // namespace internal
302 : } // namespace v8
303 :
304 : #endif // V8_WASM_WASM_MODULE_BUILDER_H_
|