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