| 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 10 matching lines...) Expand all Loading... |
| 21 class WasmModuleBuilder; | 21 class WasmModuleBuilder; |
| 22 | 22 |
| 23 class WasmFunctionEncoder : public ZoneObject { | 23 class WasmFunctionEncoder : public ZoneObject { |
| 24 public: | 24 public: |
| 25 uint32_t HeaderSize() const; | 25 uint32_t HeaderSize() const; |
| 26 uint32_t BodySize() const; | 26 uint32_t BodySize() const; |
| 27 uint32_t NameSize() const; | 27 uint32_t NameSize() const; |
| 28 void Serialize(byte* buffer, byte** header, byte** body) const; | 28 void Serialize(byte* buffer, byte** header, byte** body) const; |
| 29 | 29 |
| 30 private: | 30 private: |
| 31 WasmFunctionEncoder(Zone* zone, LocalType return_type, bool exported, | 31 WasmFunctionEncoder(Zone* zone, LocalType return_type, bool exported); |
| 32 bool external); | |
| 33 friend class WasmFunctionBuilder; | 32 friend class WasmFunctionBuilder; |
| 34 uint16_t signature_index_; | 33 uint16_t signature_index_; |
| 35 ZoneVector<LocalType> params_; | 34 ZoneVector<LocalType> params_; |
| 36 uint16_t local_i32_count_; | 35 uint16_t local_i32_count_; |
| 37 uint16_t local_i64_count_; | 36 uint16_t local_i64_count_; |
| 38 uint16_t local_f32_count_; | 37 uint16_t local_f32_count_; |
| 39 uint16_t local_f64_count_; | 38 uint16_t local_f64_count_; |
| 40 bool exported_; | 39 bool exported_; |
| 41 bool external_; | |
| 42 ZoneVector<uint8_t> body_; | 40 ZoneVector<uint8_t> body_; |
| 43 ZoneVector<char> name_; | 41 ZoneVector<char> name_; |
| 44 | 42 |
| 45 bool HasName() const { return (exported_ || external_) && name_.size() > 0; } | 43 bool HasName() const { return exported_ && name_.size() > 0; } |
| 46 }; | 44 }; |
| 47 | 45 |
| 48 class WasmFunctionBuilder : public ZoneObject { | 46 class WasmFunctionBuilder : public ZoneObject { |
| 49 public: | 47 public: |
| 50 uint16_t AddParam(LocalType type); | 48 uint16_t AddParam(LocalType type); |
| 51 uint16_t AddLocal(LocalType type); | 49 uint16_t AddLocal(LocalType type); |
| 52 void ReturnType(LocalType type); | 50 void ReturnType(LocalType type); |
| 53 void EmitVarInt(uint32_t val); | 51 void EmitVarInt(uint32_t val); |
| 54 void EmitCode(const byte* code, uint32_t code_size); | 52 void EmitCode(const byte* code, uint32_t code_size); |
| 55 void EmitCode(const byte* code, uint32_t code_size, | 53 void EmitCode(const byte* code, uint32_t code_size, |
| 56 const uint32_t* local_indices, uint32_t indices_size); | 54 const uint32_t* local_indices, uint32_t indices_size); |
| 57 void Emit(WasmOpcode opcode); | 55 void Emit(WasmOpcode opcode); |
| 58 void EmitGetLocal(uint32_t index); | 56 void EmitGetLocal(uint32_t index); |
| 59 void EmitSetLocal(uint32_t index); | 57 void EmitSetLocal(uint32_t index); |
| 60 void EmitI32Const(int32_t val); | 58 void EmitI32Const(int32_t val); |
| 61 void EmitWithU8(WasmOpcode opcode, const byte immediate); | 59 void EmitWithU8(WasmOpcode opcode, const byte immediate); |
| 62 void EmitWithU8U8(WasmOpcode opcode, const byte imm1, const byte imm2); | 60 void EmitWithU8U8(WasmOpcode opcode, const byte imm1, const byte imm2); |
| 63 void EmitWithVarInt(WasmOpcode opcode, uint32_t immediate); | 61 void EmitWithVarInt(WasmOpcode opcode, uint32_t immediate); |
| 64 uint32_t EmitEditableVarIntImmediate(); | 62 uint32_t EmitEditableVarIntImmediate(); |
| 65 void EditVarIntImmediate(uint32_t offset, const uint32_t immediate); | 63 void EditVarIntImmediate(uint32_t offset, const uint32_t immediate); |
| 66 void Exported(uint8_t flag); | 64 void Exported(uint8_t flag); |
| 67 void External(uint8_t flag); | 65 void SetName(const char* name, int name_length); |
| 68 void SetName(const unsigned char* name, int name_length); | |
| 69 WasmFunctionEncoder* Build(Zone* zone, WasmModuleBuilder* mb) const; | 66 WasmFunctionEncoder* Build(Zone* zone, WasmModuleBuilder* mb) const; |
| 70 | 67 |
| 71 private: | 68 private: |
| 72 explicit WasmFunctionBuilder(Zone* zone); | 69 explicit WasmFunctionBuilder(Zone* zone); |
| 73 friend class WasmModuleBuilder; | 70 friend class WasmModuleBuilder; |
| 74 LocalType return_type_; | 71 LocalType return_type_; |
| 75 struct Type; | 72 struct Type; |
| 76 ZoneVector<Type> locals_; | 73 ZoneVector<Type> locals_; |
| 77 uint8_t exported_; | 74 uint8_t exported_; |
| 78 uint8_t external_; | |
| 79 ZoneVector<uint8_t> body_; | 75 ZoneVector<uint8_t> body_; |
| 80 ZoneVector<uint32_t> local_indices_; | 76 ZoneVector<uint32_t> local_indices_; |
| 81 ZoneVector<char> name_; | 77 ZoneVector<char> name_; |
| 82 uint16_t AddVar(LocalType type, bool param); | 78 uint16_t AddVar(LocalType type, bool param); |
| 83 void IndexVars(WasmFunctionEncoder* e, uint16_t* var_index) const; | 79 void IndexVars(WasmFunctionEncoder* e, uint16_t* var_index) const; |
| 84 }; | 80 }; |
| 85 | 81 |
| 86 class WasmDataSegmentEncoder : public ZoneObject { | 82 class WasmDataSegmentEncoder : public ZoneObject { |
| 87 public: | 83 public: |
| 88 WasmDataSegmentEncoder(Zone* zone, const byte* data, uint32_t size, | 84 WasmDataSegmentEncoder(Zone* zone, const byte* data, uint32_t size, |
| (...skipping 13 matching lines...) Expand all Loading... |
| 102 const byte* End() const { return end_; } | 98 const byte* End() const { return end_; } |
| 103 | 99 |
| 104 private: | 100 private: |
| 105 friend class WasmModuleWriter; | 101 friend class WasmModuleWriter; |
| 106 WasmModuleIndex(const byte* begin, const byte* end) | 102 WasmModuleIndex(const byte* begin, const byte* end) |
| 107 : begin_(begin), end_(end) {} | 103 : begin_(begin), end_(end) {} |
| 108 const byte* begin_; | 104 const byte* begin_; |
| 109 const byte* end_; | 105 const byte* end_; |
| 110 }; | 106 }; |
| 111 | 107 |
| 108 struct WasmFunctionImport { |
| 109 uint32_t sig_index; |
| 110 const char* name; |
| 111 int name_length; |
| 112 }; |
| 113 |
| 112 class WasmModuleWriter : public ZoneObject { | 114 class WasmModuleWriter : public ZoneObject { |
| 113 public: | 115 public: |
| 114 WasmModuleIndex* WriteTo(Zone* zone) const; | 116 WasmModuleIndex* WriteTo(Zone* zone) const; |
| 115 | 117 |
| 116 private: | 118 private: |
| 117 friend class WasmModuleBuilder; | 119 friend class WasmModuleBuilder; |
| 118 explicit WasmModuleWriter(Zone* zone); | 120 explicit WasmModuleWriter(Zone* zone); |
| 121 ZoneVector<WasmFunctionImport> imports_; |
| 119 ZoneVector<WasmFunctionEncoder*> functions_; | 122 ZoneVector<WasmFunctionEncoder*> functions_; |
| 120 ZoneVector<WasmDataSegmentEncoder*> data_segments_; | 123 ZoneVector<WasmDataSegmentEncoder*> data_segments_; |
| 121 ZoneVector<FunctionSig*> signatures_; | 124 ZoneVector<FunctionSig*> signatures_; |
| 122 ZoneVector<uint16_t> indirect_functions_; | 125 ZoneVector<uint16_t> indirect_functions_; |
| 123 ZoneVector<std::pair<MachineType, bool>> globals_; | 126 ZoneVector<std::pair<MachineType, bool>> globals_; |
| 124 int start_function_index_; | 127 int start_function_index_; |
| 125 }; | 128 }; |
| 126 | 129 |
| 127 class WasmModuleBuilder : public ZoneObject { | 130 class WasmModuleBuilder : public ZoneObject { |
| 128 public: | 131 public: |
| 129 explicit WasmModuleBuilder(Zone* zone); | 132 explicit WasmModuleBuilder(Zone* zone); |
| 130 uint16_t AddFunction(); | 133 uint32_t AddFunction(); |
| 131 uint32_t AddGlobal(MachineType type, bool exported); | 134 uint32_t AddGlobal(MachineType type, bool exported); |
| 132 WasmFunctionBuilder* FunctionAt(size_t index); | 135 WasmFunctionBuilder* FunctionAt(size_t index); |
| 133 void AddDataSegment(WasmDataSegmentEncoder* data); | 136 void AddDataSegment(WasmDataSegmentEncoder* data); |
| 134 uint16_t AddSignature(FunctionSig* sig); | 137 uint32_t AddSignature(FunctionSig* sig); |
| 135 void AddIndirectFunction(uint16_t index); | 138 void AddIndirectFunction(uint16_t index); |
| 136 void MarkStartFunction(uint16_t index); | 139 void MarkStartFunction(uint16_t index); |
| 140 uint32_t AddImport(const char* name, int name_length, FunctionSig* sig); |
| 137 WasmModuleWriter* Build(Zone* zone); | 141 WasmModuleWriter* Build(Zone* zone); |
| 138 | 142 |
| 139 struct CompareFunctionSigs { | 143 struct CompareFunctionSigs { |
| 140 bool operator()(FunctionSig* a, FunctionSig* b) const; | 144 bool operator()(FunctionSig* a, FunctionSig* b) const; |
| 141 }; | 145 }; |
| 142 typedef ZoneMap<FunctionSig*, uint16_t, CompareFunctionSigs> SignatureMap; | 146 typedef ZoneMap<FunctionSig*, uint16_t, CompareFunctionSigs> SignatureMap; |
| 143 | 147 |
| 144 private: | 148 private: |
| 145 Zone* zone_; | 149 Zone* zone_; |
| 146 ZoneVector<FunctionSig*> signatures_; | 150 ZoneVector<FunctionSig*> signatures_; |
| 151 ZoneVector<WasmFunctionImport> imports_; |
| 147 ZoneVector<WasmFunctionBuilder*> functions_; | 152 ZoneVector<WasmFunctionBuilder*> functions_; |
| 148 ZoneVector<WasmDataSegmentEncoder*> data_segments_; | 153 ZoneVector<WasmDataSegmentEncoder*> data_segments_; |
| 149 ZoneVector<uint16_t> indirect_functions_; | 154 ZoneVector<uint16_t> indirect_functions_; |
| 150 ZoneVector<std::pair<MachineType, bool>> globals_; | 155 ZoneVector<std::pair<MachineType, bool>> globals_; |
| 151 SignatureMap signature_map_; | 156 SignatureMap signature_map_; |
| 152 int start_function_index_; | 157 int start_function_index_; |
| 153 }; | 158 }; |
| 154 | 159 |
| 155 } // namespace wasm | 160 } // namespace wasm |
| 156 } // namespace internal | 161 } // namespace internal |
| 157 } // namespace v8 | 162 } // namespace v8 |
| 158 | 163 |
| 159 #endif // V8_WASM_ENCODER_H_ | 164 #endif // V8_WASM_ENCODER_H_ |
| OLD | NEW |