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