OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8_WASM_WASM_MODULE_BUILDER_H_ | 5 #ifndef V8_WASM_WASM_MODULE_BUILDER_H_ |
6 #define V8_WASM_WASM_MODULE_BUILDER_H_ | 6 #define V8_WASM_WASM_MODULE_BUILDER_H_ |
7 | 7 |
8 #include "src/signature.h" | 8 #include "src/signature.h" |
9 #include "src/zone/zone-containers.h" | 9 #include "src/zone/zone-containers.h" |
10 | 10 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 byte out = static_cast<byte>(val & 0x7f); | 81 byte out = static_cast<byte>(val & 0x7f); |
82 if (pos != kPaddedVarInt32Size - 1) { | 82 if (pos != kPaddedVarInt32Size - 1) { |
83 *(ptr++) = 0x80 | out; | 83 *(ptr++) = 0x80 | out; |
84 val = next; | 84 val = next; |
85 } else { | 85 } else { |
86 *(ptr++) = out; | 86 *(ptr++) = out; |
87 } | 87 } |
88 } | 88 } |
89 } | 89 } |
90 | 90 |
91 size_t offset() { return static_cast<size_t>(pos_ - buffer_); } | 91 size_t offset() const { return static_cast<size_t>(pos_ - buffer_); } |
92 size_t size() { return static_cast<size_t>(pos_ - buffer_); } | 92 size_t size() const { return static_cast<size_t>(pos_ - buffer_); } |
93 const byte* begin() { return buffer_; } | 93 const byte* begin() const { return buffer_; } |
94 const byte* end() { return pos_; } | 94 const byte* end() const { return pos_; } |
95 | 95 |
96 void EnsureSpace(size_t size) { | 96 void EnsureSpace(size_t size) { |
97 if ((pos_ + size) > end_) { | 97 if ((pos_ + size) > end_) { |
98 size_t new_size = 4096 + size + (end_ - buffer_) * 3; | 98 size_t new_size = 4096 + size + (end_ - buffer_) * 3; |
99 byte* new_buffer = reinterpret_cast<byte*>(zone_->New(new_size)); | 99 byte* new_buffer = reinterpret_cast<byte*>(zone_->New(new_size)); |
100 memcpy(new_buffer, buffer_, (pos_ - buffer_)); | 100 memcpy(new_buffer, buffer_, (pos_ - buffer_)); |
101 pos_ = new_buffer + (pos_ - buffer_); | 101 pos_ = new_buffer + (pos_ - buffer_); |
102 buffer_ = new_buffer; | 102 buffer_ = new_buffer; |
103 end_ = new_buffer + new_size; | 103 end_ = new_buffer + new_size; |
104 } | 104 } |
(...skipping 23 matching lines...) Expand all Loading... |
128 void EmitSetLocal(uint32_t index); | 128 void EmitSetLocal(uint32_t index); |
129 void EmitTeeLocal(uint32_t index); | 129 void EmitTeeLocal(uint32_t index); |
130 void EmitI32Const(int32_t val); | 130 void EmitI32Const(int32_t val); |
131 void EmitWithU8(WasmOpcode opcode, const byte immediate); | 131 void EmitWithU8(WasmOpcode opcode, const byte immediate); |
132 void EmitWithU8U8(WasmOpcode opcode, const byte imm1, const byte imm2); | 132 void EmitWithU8U8(WasmOpcode opcode, const byte imm1, const byte imm2); |
133 void EmitWithVarInt(WasmOpcode opcode, uint32_t immediate); | 133 void EmitWithVarInt(WasmOpcode opcode, uint32_t immediate); |
134 void EmitDirectCallIndex(uint32_t index); | 134 void EmitDirectCallIndex(uint32_t index); |
135 void Export(); | 135 void Export(); |
136 void ExportAs(Vector<const char> name); | 136 void ExportAs(Vector<const char> name); |
137 void SetName(Vector<const char> name); | 137 void SetName(Vector<const char> name); |
| 138 void AddAsmWasmOffset(int asm_position); |
138 | 139 |
139 void WriteSignature(ZoneBuffer& buffer) const; | 140 void WriteSignature(ZoneBuffer& buffer) const; |
140 void WriteExport(ZoneBuffer& buffer) const; | 141 void WriteExport(ZoneBuffer& buffer) const; |
141 void WriteBody(ZoneBuffer& buffer) const; | 142 void WriteBody(ZoneBuffer& buffer) const; |
| 143 void WriteAsmWasmOffsetTable(ZoneBuffer& buffer) const; |
142 | 144 |
143 bool exported() { return exported_; } | 145 bool exported() { return exported_; } |
144 uint32_t func_index() { return func_index_; } | 146 uint32_t func_index() { return func_index_; } |
145 FunctionSig* signature(); | 147 FunctionSig* signature(); |
146 | 148 |
147 private: | 149 private: |
148 explicit WasmFunctionBuilder(WasmModuleBuilder* builder); | 150 explicit WasmFunctionBuilder(WasmModuleBuilder* builder); |
149 friend class WasmModuleBuilder; | 151 friend class WasmModuleBuilder; |
150 friend class WasmTemporary; | 152 friend class WasmTemporary; |
151 | 153 |
152 struct DirectCallIndex { | 154 struct DirectCallIndex { |
153 size_t offset; | 155 size_t offset; |
154 uint32_t direct_index; | 156 uint32_t direct_index; |
155 }; | 157 }; |
156 | 158 |
157 WasmModuleBuilder* builder_; | 159 WasmModuleBuilder* builder_; |
158 LocalDeclEncoder locals_; | 160 LocalDeclEncoder locals_; |
159 uint32_t signature_index_; | 161 uint32_t signature_index_; |
160 bool exported_; | 162 bool exported_; |
161 uint32_t func_index_; | 163 uint32_t func_index_; |
162 ZoneVector<uint8_t> body_; | 164 ZoneVector<uint8_t> body_; |
163 ZoneVector<char> name_; | 165 ZoneVector<char> name_; |
164 ZoneVector<char> exported_name_; | 166 ZoneVector<char> exported_name_; |
165 ZoneVector<uint32_t> i32_temps_; | 167 ZoneVector<uint32_t> i32_temps_; |
166 ZoneVector<uint32_t> i64_temps_; | 168 ZoneVector<uint32_t> i64_temps_; |
167 ZoneVector<uint32_t> f32_temps_; | 169 ZoneVector<uint32_t> f32_temps_; |
168 ZoneVector<uint32_t> f64_temps_; | 170 ZoneVector<uint32_t> f64_temps_; |
169 ZoneVector<DirectCallIndex> direct_calls_; | 171 ZoneVector<DirectCallIndex> direct_calls_; |
| 172 |
| 173 // Delta-encoded mapping from wasm bytes to asm.js source positions. |
| 174 ZoneBuffer asm_offsets_; |
| 175 uint32_t last_asm_byte_offset_ = 0; |
| 176 uint32_t last_asm_source_position_ = 0; |
170 }; | 177 }; |
171 | 178 |
172 class WasmTemporary { | 179 class WasmTemporary { |
173 public: | 180 public: |
174 WasmTemporary(WasmFunctionBuilder* builder, LocalType type) { | 181 WasmTemporary(WasmFunctionBuilder* builder, LocalType type) { |
175 switch (type) { | 182 switch (type) { |
176 case kAstI32: | 183 case kAstI32: |
177 temporary_ = &builder->i32_temps_; | 184 temporary_ = &builder->i32_temps_; |
178 break; | 185 break; |
179 case kAstI64: | 186 case kAstI64: |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 WasmFunctionBuilder* AddFunction(FunctionSig* sig = nullptr); | 228 WasmFunctionBuilder* AddFunction(FunctionSig* sig = nullptr); |
222 uint32_t AddGlobal(LocalType type, bool exported, bool mutability = true, | 229 uint32_t AddGlobal(LocalType type, bool exported, bool mutability = true, |
223 const WasmInitExpr& init = WasmInitExpr()); | 230 const WasmInitExpr& init = WasmInitExpr()); |
224 void AddDataSegment(const byte* data, uint32_t size, uint32_t dest); | 231 void AddDataSegment(const byte* data, uint32_t size, uint32_t dest); |
225 uint32_t AddSignature(FunctionSig* sig); | 232 uint32_t AddSignature(FunctionSig* sig); |
226 void AddIndirectFunction(uint32_t index); | 233 void AddIndirectFunction(uint32_t index); |
227 void MarkStartFunction(WasmFunctionBuilder* builder); | 234 void MarkStartFunction(WasmFunctionBuilder* builder); |
228 | 235 |
229 // Writing methods. | 236 // Writing methods. |
230 void WriteTo(ZoneBuffer& buffer) const; | 237 void WriteTo(ZoneBuffer& buffer) const; |
| 238 void WriteAsmJsOffsetTable(ZoneBuffer& buffer) const; |
231 | 239 |
232 // TODO(titzer): use SignatureMap from signature-map.h here. | 240 // TODO(titzer): use SignatureMap from signature-map.h here. |
233 // This signature map is zone-allocated, but the other is heap allocated. | 241 // This signature map is zone-allocated, but the other is heap allocated. |
234 struct CompareFunctionSigs { | 242 struct CompareFunctionSigs { |
235 bool operator()(FunctionSig* a, FunctionSig* b) const; | 243 bool operator()(FunctionSig* a, FunctionSig* b) const; |
236 }; | 244 }; |
237 typedef ZoneMap<FunctionSig*, uint32_t, CompareFunctionSigs> SignatureMap; | 245 typedef ZoneMap<FunctionSig*, uint32_t, CompareFunctionSigs> SignatureMap; |
238 | 246 |
239 Zone* zone() { return zone_; } | 247 Zone* zone() { return zone_; } |
240 | 248 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 | 281 |
274 inline FunctionSig* WasmFunctionBuilder::signature() { | 282 inline FunctionSig* WasmFunctionBuilder::signature() { |
275 return builder_->signatures_[signature_index_]; | 283 return builder_->signatures_[signature_index_]; |
276 } | 284 } |
277 | 285 |
278 } // namespace wasm | 286 } // namespace wasm |
279 } // namespace internal | 287 } // namespace internal |
280 } // namespace v8 | 288 } // namespace v8 |
281 | 289 |
282 #endif // V8_WASM_WASM_MODULE_BUILDER_H_ | 290 #endif // V8_WASM_WASM_MODULE_BUILDER_H_ |
OLD | NEW |