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 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 void EmitWithU8(WasmOpcode opcode, const byte immediate); | 132 void EmitWithU8(WasmOpcode opcode, const byte immediate); |
133 void EmitWithU8U8(WasmOpcode opcode, const byte imm1, const byte imm2); | 133 void EmitWithU8U8(WasmOpcode opcode, const byte imm1, const byte imm2); |
134 void EmitWithVarInt(WasmOpcode opcode, int32_t immediate); | 134 void EmitWithVarInt(WasmOpcode opcode, int32_t immediate); |
135 void EmitWithVarUint(WasmOpcode opcode, uint32_t immediate); | 135 void EmitWithVarUint(WasmOpcode opcode, uint32_t immediate); |
136 void EmitDirectCallIndex(uint32_t index); | 136 void EmitDirectCallIndex(uint32_t index); |
137 void ExportAs(Vector<const char> name); | 137 void ExportAs(Vector<const char> name); |
138 void SetName(Vector<const char> name); | 138 void SetName(Vector<const char> name); |
139 void AddAsmWasmOffset(int call_position, int to_number_position); | 139 void AddAsmWasmOffset(int call_position, int to_number_position); |
140 void SetAsmFunctionStartPosition(int position); | 140 void SetAsmFunctionStartPosition(int position); |
141 | 141 |
| 142 size_t GetPosition() const { return body_.size(); } |
| 143 void FixupByte(size_t position, byte value) { body_[position] = value; } |
| 144 void StashCode(std::vector<byte>* dst, size_t position); |
| 145 |
142 void WriteSignature(ZoneBuffer& buffer) const; | 146 void WriteSignature(ZoneBuffer& buffer) const; |
143 void WriteExports(ZoneBuffer& buffer) const; | 147 void WriteExports(ZoneBuffer& buffer) const; |
144 void WriteBody(ZoneBuffer& buffer) const; | 148 void WriteBody(ZoneBuffer& buffer) const; |
145 void WriteAsmWasmOffsetTable(ZoneBuffer& buffer) const; | 149 void WriteAsmWasmOffsetTable(ZoneBuffer& buffer) const; |
146 | 150 |
147 uint32_t func_index() { return func_index_; } | 151 uint32_t func_index() { return func_index_; } |
148 FunctionSig* signature(); | 152 FunctionSig* signature(); |
149 | 153 |
150 private: | 154 private: |
151 explicit WasmFunctionBuilder(WasmModuleBuilder* builder); | 155 explicit WasmFunctionBuilder(WasmModuleBuilder* builder); |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 uint32_t index_; | 220 uint32_t index_; |
217 }; | 221 }; |
218 | 222 |
219 class V8_EXPORT_PRIVATE WasmModuleBuilder : public ZoneObject { | 223 class V8_EXPORT_PRIVATE WasmModuleBuilder : public ZoneObject { |
220 public: | 224 public: |
221 explicit WasmModuleBuilder(Zone* zone); | 225 explicit WasmModuleBuilder(Zone* zone); |
222 | 226 |
223 // Building methods. | 227 // Building methods. |
224 uint32_t AddImport(const char* name, int name_length, FunctionSig* sig); | 228 uint32_t AddImport(const char* name, int name_length, FunctionSig* sig); |
225 void SetImportName(uint32_t index, const char* name, int name_length) { | 229 void SetImportName(uint32_t index, const char* name, int name_length) { |
226 imports_[index].name = name; | 230 function_imports_[index].name = name; |
227 imports_[index].name_length = name_length; | 231 function_imports_[index].name_length = name_length; |
228 } | 232 } |
229 WasmFunctionBuilder* AddFunction(FunctionSig* sig = nullptr); | 233 WasmFunctionBuilder* AddFunction(FunctionSig* sig = nullptr); |
230 uint32_t AddGlobal(ValueType type, bool exported, bool mutability = true, | 234 uint32_t AddGlobal(ValueType type, bool exported, bool mutability = true, |
231 const WasmInitExpr& init = WasmInitExpr()); | 235 const WasmInitExpr& init = WasmInitExpr()); |
| 236 uint32_t AddGlobalImport(const char* name, int name_length, ValueType type); |
232 void AddDataSegment(const byte* data, uint32_t size, uint32_t dest); | 237 void AddDataSegment(const byte* data, uint32_t size, uint32_t dest); |
233 uint32_t AddSignature(FunctionSig* sig); | 238 uint32_t AddSignature(FunctionSig* sig); |
234 uint32_t AllocateIndirectFunctions(uint32_t count); | 239 uint32_t AllocateIndirectFunctions(uint32_t count); |
235 void SetIndirectFunction(uint32_t indirect, uint32_t direct); | 240 void SetIndirectFunction(uint32_t indirect, uint32_t direct); |
236 void MarkStartFunction(WasmFunctionBuilder* builder); | 241 void MarkStartFunction(WasmFunctionBuilder* builder); |
237 | 242 |
238 // Writing methods. | 243 // Writing methods. |
239 void WriteTo(ZoneBuffer& buffer) const; | 244 void WriteTo(ZoneBuffer& buffer) const; |
240 void WriteAsmJsOffsetTable(ZoneBuffer& buffer) const; | 245 void WriteAsmJsOffsetTable(ZoneBuffer& buffer) const; |
241 | 246 |
242 // TODO(titzer): use SignatureMap from signature-map.h here. | 247 // TODO(titzer): use SignatureMap from signature-map.h here. |
243 // This signature map is zone-allocated, but the other is heap allocated. | 248 // This signature map is zone-allocated, but the other is heap allocated. |
244 struct CompareFunctionSigs { | 249 struct CompareFunctionSigs { |
245 bool operator()(FunctionSig* a, FunctionSig* b) const; | 250 bool operator()(FunctionSig* a, FunctionSig* b) const; |
246 }; | 251 }; |
247 typedef ZoneMap<FunctionSig*, uint32_t, CompareFunctionSigs> SignatureMap; | 252 typedef ZoneMap<FunctionSig*, uint32_t, CompareFunctionSigs> SignatureMap; |
248 | 253 |
249 Zone* zone() { return zone_; } | 254 Zone* zone() { return zone_; } |
250 | 255 |
251 FunctionSig* GetSignature(uint32_t index) { return signatures_[index]; } | 256 FunctionSig* GetSignature(uint32_t index) { return signatures_[index]; } |
252 | 257 |
253 private: | 258 private: |
254 struct WasmFunctionImport { | 259 struct WasmFunctionImport { |
255 uint32_t sig_index; | 260 uint32_t sig_index; |
256 const char* name; | 261 const char* name; |
257 int name_length; | 262 int name_length; |
258 }; | 263 }; |
259 | 264 |
| 265 struct WasmGlobalImport { |
| 266 ValueTypeCode type_code; |
| 267 const char* name; |
| 268 int name_length; |
| 269 }; |
| 270 |
260 struct WasmGlobal { | 271 struct WasmGlobal { |
261 ValueType type; | 272 ValueType type; |
262 bool exported; | 273 bool exported; |
263 bool mutability; | 274 bool mutability; |
264 WasmInitExpr init; | 275 WasmInitExpr init; |
265 }; | 276 }; |
266 | 277 |
267 struct WasmDataSegment { | 278 struct WasmDataSegment { |
268 ZoneVector<byte> data; | 279 ZoneVector<byte> data; |
269 uint32_t dest; | 280 uint32_t dest; |
270 }; | 281 }; |
271 | 282 |
272 friend class WasmFunctionBuilder; | 283 friend class WasmFunctionBuilder; |
273 Zone* zone_; | 284 Zone* zone_; |
274 ZoneVector<FunctionSig*> signatures_; | 285 ZoneVector<FunctionSig*> signatures_; |
275 ZoneVector<WasmFunctionImport> imports_; | 286 ZoneVector<WasmFunctionImport> function_imports_; |
| 287 ZoneVector<WasmGlobalImport> global_imports_; |
276 ZoneVector<WasmFunctionBuilder*> functions_; | 288 ZoneVector<WasmFunctionBuilder*> functions_; |
277 ZoneVector<WasmDataSegment> data_segments_; | 289 ZoneVector<WasmDataSegment> data_segments_; |
278 ZoneVector<uint32_t> indirect_functions_; | 290 ZoneVector<uint32_t> indirect_functions_; |
279 ZoneVector<WasmGlobal> globals_; | 291 ZoneVector<WasmGlobal> globals_; |
280 SignatureMap signature_map_; | 292 SignatureMap signature_map_; |
281 int start_function_index_; | 293 int start_function_index_; |
282 }; | 294 }; |
283 | 295 |
284 inline FunctionSig* WasmFunctionBuilder::signature() { | 296 inline FunctionSig* WasmFunctionBuilder::signature() { |
285 return builder_->signatures_[signature_index_]; | 297 return builder_->signatures_[signature_index_]; |
286 } | 298 } |
287 | 299 |
288 } // namespace wasm | 300 } // namespace wasm |
289 } // namespace internal | 301 } // namespace internal |
290 } // namespace v8 | 302 } // namespace v8 |
291 | 303 |
292 #endif // V8_WASM_WASM_MODULE_BUILDER_H_ | 304 #endif // V8_WASM_WASM_MODULE_BUILDER_H_ |
OLD | NEW |