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_ENCODER_H_ | 5 #ifndef V8_WASM_ENCODER_H_ |
6 #define V8_WASM_ENCODER_H_ | 6 #define V8_WASM_ENCODER_H_ |
7 | 7 |
8 #include "src/signature.h" | 8 #include "src/signature.h" |
9 #include "src/zone-containers.h" | 9 #include "src/zone-containers.h" |
10 | 10 |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
113 class WasmFunctionBuilder : public ZoneObject { | 113 class WasmFunctionBuilder : public ZoneObject { |
114 public: | 114 public: |
115 // Building methods. | 115 // Building methods. |
116 void SetSignature(FunctionSig* sig); | 116 void SetSignature(FunctionSig* sig); |
117 uint32_t AddLocal(LocalType type); | 117 uint32_t AddLocal(LocalType type); |
118 void EmitVarInt(uint32_t val); | 118 void EmitVarInt(uint32_t val); |
119 void EmitCode(const byte* code, uint32_t code_size); | 119 void EmitCode(const byte* code, uint32_t code_size); |
120 void Emit(WasmOpcode opcode); | 120 void Emit(WasmOpcode opcode); |
121 void EmitGetLocal(uint32_t index); | 121 void EmitGetLocal(uint32_t index); |
122 void EmitSetLocal(uint32_t index); | 122 void EmitSetLocal(uint32_t index); |
123 void EmitTeeLocal(uint32_t index); | |
123 void EmitI32Const(int32_t val); | 124 void EmitI32Const(int32_t val); |
124 void EmitWithU8(WasmOpcode opcode, const byte immediate); | 125 void EmitWithU8(WasmOpcode opcode, const byte immediate); |
125 void EmitWithU8U8(WasmOpcode opcode, const byte imm1, const byte imm2); | 126 void EmitWithU8U8(WasmOpcode opcode, const byte imm1, const byte imm2); |
126 void EmitWithVarInt(WasmOpcode opcode, uint32_t immediate); | 127 void EmitWithVarInt(WasmOpcode opcode, uint32_t immediate); |
127 void SetExported(); | 128 void SetExported(); |
128 void SetName(const char* name, int name_length); | 129 void SetName(const char* name, int name_length); |
130 | |
131 void WriteSignature(ZoneBuffer& buffer) const; | |
132 void WriteExport(ZoneBuffer& buffer) const; | |
133 void WriteBody(ZoneBuffer& buffer) const; | |
134 | |
129 bool exported() { return exported_; } | 135 bool exported() { return exported_; } |
130 | 136 uint32_t func_index() { return func_index_; } |
Mircea Trofin
2016/09/19 16:16:25
const? also for exported() above.
worth checking
titzer
2016/09/21 08:58:44
I don't think const annotations on builder classes
Mircea Trofin
2016/09/21 16:38:23
See my previous point about the value of const for
titzer
2016/09/21 18:49:28
I prefer a very light and judicial application of
| |
131 // Writing methods. | 137 FunctionSig* signature(); |
132 void WriteSignature(ZoneBuffer& buffer) const; | |
133 void WriteExport(ZoneBuffer& buffer, uint32_t func_index) const; | |
134 void WriteBody(ZoneBuffer& buffer) const; | |
135 | 138 |
136 private: | 139 private: |
137 explicit WasmFunctionBuilder(WasmModuleBuilder* builder); | 140 explicit WasmFunctionBuilder(WasmModuleBuilder* builder); |
138 friend class WasmModuleBuilder; | 141 friend class WasmModuleBuilder; |
139 friend class WasmTemporary; | 142 friend class WasmTemporary; |
140 WasmModuleBuilder* builder_; | 143 WasmModuleBuilder* builder_; |
141 LocalDeclEncoder locals_; | 144 LocalDeclEncoder locals_; |
142 uint32_t signature_index_; | 145 uint32_t signature_index_; |
143 bool exported_; | 146 bool exported_; |
147 uint32_t func_index_; | |
144 ZoneVector<uint8_t> body_; | 148 ZoneVector<uint8_t> body_; |
145 ZoneVector<char> name_; | 149 ZoneVector<char> name_; |
146 ZoneVector<uint32_t> i32_temps_; | 150 ZoneVector<uint32_t> i32_temps_; |
147 ZoneVector<uint32_t> i64_temps_; | 151 ZoneVector<uint32_t> i64_temps_; |
148 ZoneVector<uint32_t> f32_temps_; | 152 ZoneVector<uint32_t> f32_temps_; |
149 ZoneVector<uint32_t> f64_temps_; | 153 ZoneVector<uint32_t> f64_temps_; |
150 }; | 154 }; |
151 | 155 |
152 class WasmTemporary { | 156 class WasmTemporary { |
153 public: | 157 public: |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
204 uint32_t sig_index; | 208 uint32_t sig_index; |
205 const char* name; | 209 const char* name; |
206 int name_length; | 210 int name_length; |
207 }; | 211 }; |
208 | 212 |
209 class WasmModuleBuilder : public ZoneObject { | 213 class WasmModuleBuilder : public ZoneObject { |
210 public: | 214 public: |
211 explicit WasmModuleBuilder(Zone* zone); | 215 explicit WasmModuleBuilder(Zone* zone); |
212 | 216 |
213 // Building methods. | 217 // Building methods. |
214 uint32_t AddFunction(); | 218 uint32_t AddImport(const char* name, int name_length, FunctionSig* sig); |
215 uint32_t AddGlobal(LocalType type, bool exported); | 219 void SetImportName(uint32_t index, const char* name, int name_length) { |
216 WasmFunctionBuilder* FunctionAt(size_t index); | 220 imports_[index].name = name; |
221 imports_[index].name_length = name_length; | |
222 } | |
223 WasmFunctionBuilder* AddFunction(FunctionSig* sig = nullptr); | |
224 uint32_t AddGlobal(LocalType type, bool exported, bool mutability = true); | |
217 void AddDataSegment(WasmDataSegmentEncoder* data); | 225 void AddDataSegment(WasmDataSegmentEncoder* data); |
218 uint32_t AddSignature(FunctionSig* sig); | 226 uint32_t AddSignature(FunctionSig* sig); |
219 void AddIndirectFunction(uint32_t index); | 227 void AddIndirectFunction(uint32_t index); |
220 void MarkStartFunction(uint32_t index); | 228 void MarkStartFunction(WasmFunctionBuilder* builder); |
221 uint32_t AddImport(const char* name, int name_length, FunctionSig* sig); | |
222 | 229 |
223 // Writing methods. | 230 // Writing methods. |
224 void WriteTo(ZoneBuffer& buffer) const; | 231 void WriteTo(ZoneBuffer& buffer) const; |
225 | 232 |
226 struct CompareFunctionSigs { | 233 struct CompareFunctionSigs { |
227 bool operator()(FunctionSig* a, FunctionSig* b) const; | 234 bool operator()(FunctionSig* a, FunctionSig* b) const; |
228 }; | 235 }; |
229 typedef ZoneMap<FunctionSig*, uint32_t, CompareFunctionSigs> SignatureMap; | 236 typedef ZoneMap<FunctionSig*, uint32_t, CompareFunctionSigs> SignatureMap; |
230 | 237 |
231 Zone* zone() { return zone_; } | 238 Zone* zone() { return zone_; } |
232 | 239 |
240 FunctionSig* GetSignature(uint32_t index) { return signatures_[index]; } | |
241 | |
233 private: | 242 private: |
243 friend class WasmFunctionBuilder; | |
234 Zone* zone_; | 244 Zone* zone_; |
235 ZoneVector<FunctionSig*> signatures_; | 245 ZoneVector<FunctionSig*> signatures_; |
236 ZoneVector<WasmFunctionImport> imports_; | 246 ZoneVector<WasmFunctionImport> imports_; |
237 ZoneVector<WasmFunctionBuilder*> functions_; | 247 ZoneVector<WasmFunctionBuilder*> functions_; |
238 ZoneVector<WasmDataSegmentEncoder*> data_segments_; | 248 ZoneVector<WasmDataSegmentEncoder*> data_segments_; |
239 ZoneVector<uint32_t> indirect_functions_; | 249 ZoneVector<uint32_t> indirect_functions_; |
240 ZoneVector<std::pair<LocalType, bool>> globals_; | 250 ZoneVector<std::tuple<LocalType, bool, bool>> globals_; |
Mircea Trofin
2016/09/19 16:16:25
For readability (like, what's this second and thir
| |
241 SignatureMap signature_map_; | 251 SignatureMap signature_map_; |
242 int start_function_index_; | 252 int start_function_index_; |
243 }; | 253 }; |
244 | 254 |
255 inline FunctionSig* WasmFunctionBuilder::signature() { | |
256 return builder_->signatures_[signature_index_]; | |
257 } | |
258 | |
245 } // namespace wasm | 259 } // namespace wasm |
246 } // namespace internal | 260 } // namespace internal |
247 } // namespace v8 | 261 } // namespace v8 |
248 | 262 |
249 #endif // V8_WASM_ENCODER_H_ | 263 #endif // V8_WASM_ENCODER_H_ |
OLD | NEW |