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-module.h"
16 : #include "src/wasm/wasm-opcodes.h"
17 : #include "src/wasm/wasm-result.h"
18 :
19 : namespace v8 {
20 : namespace internal {
21 : namespace wasm {
22 :
23 : class ZoneBuffer : public ZoneObject {
24 : public:
25 : static constexpr size_t kInitialSize = 1024;
26 : explicit ZoneBuffer(Zone* zone, size_t initial = kInitialSize)
27 36492 : : zone_(zone), buffer_(reinterpret_cast<byte*>(zone->New(initial))) {
28 36493 : pos_ = buffer_;
29 36493 : end_ = buffer_ + initial;
30 : }
31 :
32 : void write_u8(uint8_t x) {
33 14559004 : EnsureSpace(1);
34 14559000 : *(pos_++) = x;
35 : }
36 :
37 : void write_u16(uint16_t x) {
38 : EnsureSpace(2);
39 : WriteLittleEndianValue<uint16_t>(reinterpret_cast<Address>(pos_), x);
40 : pos_ += 2;
41 : }
42 :
43 5726 : void write_u32(uint32_t x) {
44 5726 : EnsureSpace(4);
45 5726 : WriteLittleEndianValue<uint32_t>(reinterpret_cast<Address>(pos_), x);
46 5726 : pos_ += 4;
47 5726 : }
48 :
49 5009787 : void write_u64(uint64_t x) {
50 5009787 : EnsureSpace(8);
51 5009787 : WriteLittleEndianValue<uint64_t>(reinterpret_cast<Address>(pos_), x);
52 5009787 : pos_ += 8;
53 5009787 : }
54 :
55 1408661 : void write_u32v(uint32_t val) {
56 1408661 : EnsureSpace(kMaxVarInt32Size);
57 : LEBHelper::write_u32v(&pos_, val);
58 1408661 : }
59 :
60 : void write_i32v(int32_t val) {
61 1655529 : EnsureSpace(kMaxVarInt32Size);
62 1655528 : LEBHelper::write_i32v(&pos_, val);
63 : }
64 :
65 : void write_u64v(uint64_t val) {
66 : EnsureSpace(kMaxVarInt64Size);
67 : LEBHelper::write_u64v(&pos_, val);
68 : }
69 :
70 : void write_i64v(int64_t val) {
71 0 : EnsureSpace(kMaxVarInt64Size);
72 0 : LEBHelper::write_i64v(&pos_, val);
73 : }
74 :
75 94162 : void write_size(size_t val) {
76 94162 : EnsureSpace(kMaxVarInt32Size);
77 : DCHECK_EQ(val, static_cast<uint32_t>(val));
78 94162 : LEBHelper::write_u32v(&pos_, static_cast<uint32_t>(val));
79 94162 : }
80 :
81 92 : void write_f32(float val) { write_u32(bit_cast<uint32_t>(val)); }
82 :
83 5009787 : void write_f64(double val) { write_u64(bit_cast<uint64_t>(val)); }
84 :
85 83915 : void write(const byte* data, size_t size) {
86 83915 : EnsureSpace(size);
87 83915 : memcpy(pos_, data, size);
88 83915 : pos_ += size;
89 83915 : }
90 :
91 : void write_string(Vector<const char> name) {
92 23528 : write_size(name.length());
93 23528 : write(reinterpret_cast<const byte*>(name.start()), name.length());
94 : }
95 :
96 : size_t reserve_u32v() {
97 : size_t off = offset();
98 22636 : EnsureSpace(kMaxVarInt32Size);
99 22636 : pos_ += kMaxVarInt32Size;
100 : return off;
101 : }
102 :
103 : // Patch a (padded) u32v at the given offset to be the given value.
104 : void patch_u32v(size_t offset, uint32_t val) {
105 55864 : byte* ptr = buffer_ + offset;
106 614504 : for (size_t pos = 0; pos != kPaddedVarInt32Size; ++pos) {
107 279320 : uint32_t next = val >> 7;
108 279320 : byte out = static_cast<byte>(val & 0x7f);
109 279320 : if (pos != kPaddedVarInt32Size - 1) {
110 223456 : *(ptr++) = 0x80 | out;
111 : val = next;
112 : } else {
113 55864 : *(ptr++) = out;
114 : }
115 : }
116 : }
117 :
118 : void patch_u8(size_t offset, byte val) {
119 : DCHECK_GE(size(), offset);
120 7324 : buffer_[offset] = val;
121 : }
122 :
123 60070 : size_t offset() const { return static_cast<size_t>(pos_ - buffer_); }
124 570054 : size_t size() const { return static_cast<size_t>(pos_ - buffer_); }
125 : const byte* begin() const { return buffer_; }
126 : const byte* end() const { return pos_; }
127 :
128 22854202 : void EnsureSpace(size_t size) {
129 22854202 : if ((pos_ + size) > end_) {
130 16385 : size_t new_size = size + (end_ - buffer_) * 2;
131 16385 : byte* new_buffer = reinterpret_cast<byte*>(zone_->New(new_size));
132 16385 : memcpy(new_buffer, buffer_, (pos_ - buffer_));
133 16385 : pos_ = new_buffer + (pos_ - buffer_);
134 16385 : buffer_ = new_buffer;
135 16385 : end_ = new_buffer + new_size;
136 : }
137 : DCHECK(pos_ + size <= end_);
138 22854202 : }
139 :
140 : void Truncate(size_t size) {
141 : DCHECK_GE(offset(), size);
142 392906 : pos_ = buffer_ + size;
143 : }
144 :
145 : byte** pos_ptr() { return &pos_; }
146 :
147 : private:
148 : Zone* zone_;
149 : byte* buffer_;
150 : byte* pos_;
151 : byte* end_;
152 : };
153 :
154 : class WasmModuleBuilder;
155 :
156 : class V8_EXPORT_PRIVATE WasmFunctionBuilder : public ZoneObject {
157 : public:
158 : // Building methods.
159 : void SetSignature(FunctionSig* sig);
160 : uint32_t AddLocal(ValueType type);
161 : void EmitI32V(int32_t val);
162 : void EmitU32V(uint32_t val);
163 : void EmitCode(const byte* code, uint32_t code_size);
164 : void Emit(WasmOpcode opcode);
165 : void EmitGetLocal(uint32_t index);
166 : void EmitSetLocal(uint32_t index);
167 : void EmitTeeLocal(uint32_t index);
168 : void EmitI32Const(int32_t val);
169 : void EmitI64Const(int64_t val);
170 : void EmitF32Const(float val);
171 : void EmitF64Const(double val);
172 : void EmitWithU8(WasmOpcode opcode, const byte immediate);
173 : void EmitWithU8U8(WasmOpcode opcode, const byte imm1, const byte imm2);
174 : void EmitWithI32V(WasmOpcode opcode, int32_t immediate);
175 : void EmitWithU32V(WasmOpcode opcode, uint32_t immediate);
176 : void EmitDirectCallIndex(uint32_t index);
177 : void SetName(Vector<const char> name);
178 : void AddAsmWasmOffset(size_t call_position, size_t to_number_position);
179 : void SetAsmFunctionStartPosition(size_t function_position);
180 : void SetCompilationHint(WasmCompilationHintStrategy strategy,
181 : WasmCompilationHintTier baseline,
182 : WasmCompilationHintTier top_tier);
183 :
184 : size_t GetPosition() const { return body_.size(); }
185 : void FixupByte(size_t position, byte value) {
186 : body_.patch_u8(position, value);
187 : }
188 : void DeleteCodeAfter(size_t position);
189 :
190 : void WriteSignature(ZoneBuffer& buffer) const;
191 : void WriteBody(ZoneBuffer& buffer) const;
192 : void WriteAsmWasmOffsetTable(ZoneBuffer& buffer) const;
193 :
194 : WasmModuleBuilder* builder() const { return builder_; }
195 : uint32_t func_index() { return func_index_; }
196 : FunctionSig* signature();
197 :
198 : private:
199 : explicit WasmFunctionBuilder(WasmModuleBuilder* builder);
200 : friend class WasmModuleBuilder;
201 :
202 : struct DirectCallIndex {
203 : size_t offset;
204 : uint32_t direct_index;
205 : };
206 :
207 : WasmModuleBuilder* builder_;
208 : LocalDeclEncoder locals_;
209 : uint32_t signature_index_;
210 : uint32_t func_index_;
211 : ZoneBuffer body_;
212 : Vector<const char> name_;
213 : ZoneVector<uint32_t> i32_temps_;
214 : ZoneVector<uint32_t> i64_temps_;
215 : ZoneVector<uint32_t> f32_temps_;
216 : ZoneVector<uint32_t> f64_temps_;
217 : ZoneVector<DirectCallIndex> direct_calls_;
218 :
219 : // Delta-encoded mapping from wasm bytes to asm.js source positions.
220 : ZoneBuffer asm_offsets_;
221 : uint32_t last_asm_byte_offset_ = 0;
222 : uint32_t last_asm_source_position_ = 0;
223 : uint32_t asm_func_start_source_position_ = 0;
224 : uint8_t hint_ = kNoCompilationHint;
225 : };
226 :
227 54 : class V8_EXPORT_PRIVATE WasmModuleBuilder : public ZoneObject {
228 : public:
229 : explicit WasmModuleBuilder(Zone* zone);
230 :
231 : // Building methods.
232 : uint32_t AddImport(Vector<const char> name, FunctionSig* sig);
233 : WasmFunctionBuilder* AddFunction(FunctionSig* sig = nullptr);
234 : uint32_t AddGlobal(ValueType type, bool exported, bool mutability = true,
235 : const WasmInitExpr& init = WasmInitExpr());
236 : uint32_t AddGlobalImport(Vector<const char> name, ValueType type);
237 : void AddDataSegment(const byte* data, uint32_t size, uint32_t dest);
238 : uint32_t AddSignature(FunctionSig* sig);
239 : uint32_t AllocateIndirectFunctions(uint32_t count);
240 : void SetIndirectFunction(uint32_t indirect, uint32_t direct);
241 : void MarkStartFunction(WasmFunctionBuilder* builder);
242 : void AddExport(Vector<const char> name, WasmFunctionBuilder* builder);
243 : void SetMinMemorySize(uint32_t value);
244 : void SetMaxMemorySize(uint32_t value);
245 : void SetHasSharedMemory();
246 :
247 : // Writing methods.
248 : void WriteTo(ZoneBuffer& buffer) const;
249 : void WriteAsmJsOffsetTable(ZoneBuffer& buffer) const;
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 16 : 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 : ZoneUnorderedMap<FunctionSig, uint32_t> 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 2 : 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_
|