| 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 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 ~WasmTemporary() { | 186 ~WasmTemporary() { |
| 187 temporary_->push_back(index_); // return the temporary to the list. | 187 temporary_->push_back(index_); // return the temporary to the list. |
| 188 } | 188 } |
| 189 uint32_t index() { return index_; } | 189 uint32_t index() { return index_; } |
| 190 | 190 |
| 191 private: | 191 private: |
| 192 ZoneVector<uint32_t>* temporary_; | 192 ZoneVector<uint32_t>* temporary_; |
| 193 uint32_t index_; | 193 uint32_t index_; |
| 194 }; | 194 }; |
| 195 | 195 |
| 196 // TODO(titzer): kill! | |
| 197 class WasmDataSegmentEncoder : public ZoneObject { | |
| 198 public: | |
| 199 WasmDataSegmentEncoder(Zone* zone, const byte* data, uint32_t size, | |
| 200 uint32_t dest); | |
| 201 void Write(ZoneBuffer& buffer) const; | |
| 202 | |
| 203 private: | |
| 204 ZoneVector<byte> data_; | |
| 205 uint32_t dest_; | |
| 206 }; | |
| 207 | |
| 208 struct WasmFunctionImport { | |
| 209 uint32_t sig_index; | |
| 210 const char* name; | |
| 211 int name_length; | |
| 212 }; | |
| 213 | |
| 214 class V8_EXPORT_PRIVATE WasmModuleBuilder : public ZoneObject { | 196 class V8_EXPORT_PRIVATE WasmModuleBuilder : public ZoneObject { |
| 215 public: | 197 public: |
| 216 explicit WasmModuleBuilder(Zone* zone); | 198 explicit WasmModuleBuilder(Zone* zone); |
| 217 | 199 |
| 218 // Building methods. | 200 // Building methods. |
| 219 uint32_t AddImport(const char* name, int name_length, FunctionSig* sig); | 201 uint32_t AddImport(const char* name, int name_length, FunctionSig* sig); |
| 220 void SetImportName(uint32_t index, const char* name, int name_length) { | 202 void SetImportName(uint32_t index, const char* name, int name_length) { |
| 221 imports_[index].name = name; | 203 imports_[index].name = name; |
| 222 imports_[index].name_length = name_length; | 204 imports_[index].name_length = name_length; |
| 223 } | 205 } |
| 224 WasmFunctionBuilder* AddFunction(FunctionSig* sig = nullptr); | 206 WasmFunctionBuilder* AddFunction(FunctionSig* sig = nullptr); |
| 225 uint32_t AddGlobal(LocalType type, bool exported, bool mutability = true); | 207 uint32_t AddGlobal(LocalType type, bool exported, bool mutability = true); |
| 226 void AddDataSegment(WasmDataSegmentEncoder* data); | 208 void AddDataSegment(const byte* data, uint32_t size, uint32_t dest); |
| 227 uint32_t AddSignature(FunctionSig* sig); | 209 uint32_t AddSignature(FunctionSig* sig); |
| 228 void AddIndirectFunction(uint32_t index); | 210 void AddIndirectFunction(uint32_t index); |
| 229 void MarkStartFunction(WasmFunctionBuilder* builder); | 211 void MarkStartFunction(WasmFunctionBuilder* builder); |
| 230 | 212 |
| 231 // Writing methods. | 213 // Writing methods. |
| 232 void WriteTo(ZoneBuffer& buffer) const; | 214 void WriteTo(ZoneBuffer& buffer) const; |
| 233 | 215 |
| 234 struct CompareFunctionSigs { | 216 struct CompareFunctionSigs { |
| 235 bool operator()(FunctionSig* a, FunctionSig* b) const; | 217 bool operator()(FunctionSig* a, FunctionSig* b) const; |
| 236 }; | 218 }; |
| 237 typedef ZoneMap<FunctionSig*, uint32_t, CompareFunctionSigs> SignatureMap; | 219 typedef ZoneMap<FunctionSig*, uint32_t, CompareFunctionSigs> SignatureMap; |
| 238 | 220 |
| 239 Zone* zone() { return zone_; } | 221 Zone* zone() { return zone_; } |
| 240 | 222 |
| 241 FunctionSig* GetSignature(uint32_t index) { return signatures_[index]; } | 223 FunctionSig* GetSignature(uint32_t index) { return signatures_[index]; } |
| 242 | 224 |
| 243 private: | 225 private: |
| 226 struct WasmFunctionImport { |
| 227 uint32_t sig_index; |
| 228 const char* name; |
| 229 int name_length; |
| 230 }; |
| 231 |
| 232 struct WasmGlobal { |
| 233 LocalType type; |
| 234 bool exported; |
| 235 bool mutability; |
| 236 }; |
| 237 |
| 238 struct WasmDataSegment { |
| 239 ZoneVector<byte> data; |
| 240 uint32_t dest; |
| 241 }; |
| 242 |
| 244 friend class WasmFunctionBuilder; | 243 friend class WasmFunctionBuilder; |
| 245 Zone* zone_; | 244 Zone* zone_; |
| 246 ZoneVector<FunctionSig*> signatures_; | 245 ZoneVector<FunctionSig*> signatures_; |
| 247 ZoneVector<WasmFunctionImport> imports_; | 246 ZoneVector<WasmFunctionImport> imports_; |
| 248 ZoneVector<WasmFunctionBuilder*> functions_; | 247 ZoneVector<WasmFunctionBuilder*> functions_; |
| 249 ZoneVector<WasmDataSegmentEncoder*> data_segments_; | 248 ZoneVector<WasmDataSegment> data_segments_; |
| 250 ZoneVector<uint32_t> indirect_functions_; | 249 ZoneVector<uint32_t> indirect_functions_; |
| 251 ZoneVector<std::tuple<LocalType, bool, bool>> globals_; | 250 ZoneVector<WasmGlobal> globals_; |
| 252 SignatureMap signature_map_; | 251 SignatureMap signature_map_; |
| 253 int start_function_index_; | 252 int start_function_index_; |
| 254 }; | 253 }; |
| 255 | 254 |
| 256 inline FunctionSig* WasmFunctionBuilder::signature() { | 255 inline FunctionSig* WasmFunctionBuilder::signature() { |
| 257 return builder_->signatures_[signature_index_]; | 256 return builder_->signatures_[signature_index_]; |
| 258 } | 257 } |
| 259 | 258 |
| 260 } // namespace wasm | 259 } // namespace wasm |
| 261 } // namespace internal | 260 } // namespace internal |
| 262 } // namespace v8 | 261 } // namespace v8 |
| 263 | 262 |
| 264 #endif // V8_WASM_WASM_MODULE_BUILDER_H_ | 263 #endif // V8_WASM_WASM_MODULE_BUILDER_H_ |
| OLD | NEW |