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 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 | 105 |
106 private: | 106 private: |
107 Zone* zone_; | 107 Zone* zone_; |
108 byte* buffer_; | 108 byte* buffer_; |
109 byte* pos_; | 109 byte* pos_; |
110 byte* end_; | 110 byte* end_; |
111 }; | 111 }; |
112 | 112 |
113 class WasmModuleBuilder; | 113 class WasmModuleBuilder; |
114 | 114 |
115 class WasmFunctionEncoder : public ZoneObject { | |
116 public: | |
117 void WriteSignature(ZoneBuffer& buffer) const; | |
118 void WriteExport(ZoneBuffer& buffer, uint32_t func_index) const; | |
119 void WriteBody(ZoneBuffer& buffer) const; | |
120 bool exported() const { return exported_; } | |
121 | |
122 private: | |
123 WasmFunctionEncoder(Zone* zone, LocalDeclEncoder locals, bool exported); | |
124 friend class WasmFunctionBuilder; | |
125 uint32_t signature_index_; | |
126 LocalDeclEncoder locals_; | |
127 bool exported_; | |
128 ZoneVector<uint8_t> body_; | |
129 ZoneVector<char> name_; | |
130 | |
131 bool HasName() const { return exported_ && name_.size() > 0; } | |
132 }; | |
133 | |
134 class WasmFunctionBuilder : public ZoneObject { | 115 class WasmFunctionBuilder : public ZoneObject { |
135 public: | 116 public: |
| 117 // Building methods. |
136 void SetSignature(FunctionSig* sig); | 118 void SetSignature(FunctionSig* sig); |
137 uint32_t AddLocal(LocalType type); | 119 uint32_t AddLocal(LocalType type); |
138 void EmitVarInt(uint32_t val); | 120 void EmitVarInt(uint32_t val); |
139 void EmitCode(const byte* code, uint32_t code_size); | 121 void EmitCode(const byte* code, uint32_t code_size); |
140 void Emit(WasmOpcode opcode); | 122 void Emit(WasmOpcode opcode); |
141 void EmitGetLocal(uint32_t index); | 123 void EmitGetLocal(uint32_t index); |
142 void EmitSetLocal(uint32_t index); | 124 void EmitSetLocal(uint32_t index); |
143 void EmitI32Const(int32_t val); | 125 void EmitI32Const(int32_t val); |
144 void EmitWithU8(WasmOpcode opcode, const byte immediate); | 126 void EmitWithU8(WasmOpcode opcode, const byte immediate); |
145 void EmitWithU8U8(WasmOpcode opcode, const byte imm1, const byte imm2); | 127 void EmitWithU8U8(WasmOpcode opcode, const byte imm1, const byte imm2); |
146 void EmitWithVarInt(WasmOpcode opcode, uint32_t immediate); | 128 void EmitWithVarInt(WasmOpcode opcode, uint32_t immediate); |
147 void Exported(uint8_t flag); | 129 void SetExported(); |
148 void SetName(const char* name, int name_length); | 130 void SetName(const char* name, int name_length); |
149 WasmFunctionEncoder* Build(Zone* zone, WasmModuleBuilder* mb) const; | 131 bool exported() { return exported_; } |
| 132 |
| 133 // Writing methods. |
| 134 void WriteSignature(ZoneBuffer& buffer) const; |
| 135 void WriteExport(ZoneBuffer& buffer, uint32_t func_index) const; |
| 136 void WriteBody(ZoneBuffer& buffer) const; |
150 | 137 |
151 private: | 138 private: |
152 explicit WasmFunctionBuilder(Zone* zone); | 139 explicit WasmFunctionBuilder(WasmModuleBuilder* builder); |
153 friend class WasmModuleBuilder; | 140 friend class WasmModuleBuilder; |
| 141 WasmModuleBuilder* builder_; |
154 LocalDeclEncoder locals_; | 142 LocalDeclEncoder locals_; |
155 uint8_t exported_; | 143 uint32_t signature_index_; |
| 144 bool exported_; |
156 ZoneVector<uint8_t> body_; | 145 ZoneVector<uint8_t> body_; |
157 ZoneVector<char> name_; | 146 ZoneVector<char> name_; |
158 void IndexVars(WasmFunctionEncoder* e, uint32_t* var_index) const; | |
159 }; | 147 }; |
160 | 148 |
| 149 // TODO(titzer): kill! |
161 class WasmDataSegmentEncoder : public ZoneObject { | 150 class WasmDataSegmentEncoder : public ZoneObject { |
162 public: | 151 public: |
163 WasmDataSegmentEncoder(Zone* zone, const byte* data, uint32_t size, | 152 WasmDataSegmentEncoder(Zone* zone, const byte* data, uint32_t size, |
164 uint32_t dest); | 153 uint32_t dest); |
165 void Write(ZoneBuffer& buffer) const; | 154 void Write(ZoneBuffer& buffer) const; |
166 | 155 |
167 private: | 156 private: |
168 ZoneVector<byte> data_; | 157 ZoneVector<byte> data_; |
169 uint32_t dest_; | 158 uint32_t dest_; |
170 }; | 159 }; |
171 | 160 |
172 struct WasmFunctionImport { | 161 struct WasmFunctionImport { |
173 uint32_t sig_index; | 162 uint32_t sig_index; |
174 const char* name; | 163 const char* name; |
175 int name_length; | 164 int name_length; |
176 }; | 165 }; |
177 | 166 |
178 class WasmModuleWriter : public ZoneObject { | |
179 public: | |
180 void WriteTo(ZoneBuffer& buffer) const; | |
181 | |
182 private: | |
183 friend class WasmModuleBuilder; | |
184 explicit WasmModuleWriter(Zone* zone); | |
185 ZoneVector<WasmFunctionImport> imports_; | |
186 ZoneVector<WasmFunctionEncoder*> functions_; | |
187 ZoneVector<WasmDataSegmentEncoder*> data_segments_; | |
188 ZoneVector<FunctionSig*> signatures_; | |
189 ZoneVector<uint32_t> indirect_functions_; | |
190 ZoneVector<std::pair<MachineType, bool>> globals_; | |
191 int start_function_index_; | |
192 }; | |
193 | |
194 class WasmModuleBuilder : public ZoneObject { | 167 class WasmModuleBuilder : public ZoneObject { |
195 public: | 168 public: |
196 explicit WasmModuleBuilder(Zone* zone); | 169 explicit WasmModuleBuilder(Zone* zone); |
| 170 |
| 171 // Building methods. |
197 uint32_t AddFunction(); | 172 uint32_t AddFunction(); |
198 uint32_t AddGlobal(MachineType type, bool exported); | 173 uint32_t AddGlobal(MachineType type, bool exported); |
199 WasmFunctionBuilder* FunctionAt(size_t index); | 174 WasmFunctionBuilder* FunctionAt(size_t index); |
200 void AddDataSegment(WasmDataSegmentEncoder* data); | 175 void AddDataSegment(WasmDataSegmentEncoder* data); |
201 uint32_t AddSignature(FunctionSig* sig); | 176 uint32_t AddSignature(FunctionSig* sig); |
202 void AddIndirectFunction(uint32_t index); | 177 void AddIndirectFunction(uint32_t index); |
203 void MarkStartFunction(uint32_t index); | 178 void MarkStartFunction(uint32_t index); |
204 uint32_t AddImport(const char* name, int name_length, FunctionSig* sig); | 179 uint32_t AddImport(const char* name, int name_length, FunctionSig* sig); |
205 WasmModuleWriter* Build(Zone* zone); | 180 |
| 181 // Writing methods. |
| 182 void WriteTo(ZoneBuffer& buffer) const; |
206 | 183 |
207 struct CompareFunctionSigs { | 184 struct CompareFunctionSigs { |
208 bool operator()(FunctionSig* a, FunctionSig* b) const; | 185 bool operator()(FunctionSig* a, FunctionSig* b) const; |
209 }; | 186 }; |
210 typedef ZoneMap<FunctionSig*, uint32_t, CompareFunctionSigs> SignatureMap; | 187 typedef ZoneMap<FunctionSig*, uint32_t, CompareFunctionSigs> SignatureMap; |
211 | 188 |
| 189 Zone* zone() { return zone_; } |
| 190 |
212 private: | 191 private: |
213 Zone* zone_; | 192 Zone* zone_; |
214 ZoneVector<FunctionSig*> signatures_; | 193 ZoneVector<FunctionSig*> signatures_; |
215 ZoneVector<WasmFunctionImport> imports_; | 194 ZoneVector<WasmFunctionImport> imports_; |
216 ZoneVector<WasmFunctionBuilder*> functions_; | 195 ZoneVector<WasmFunctionBuilder*> functions_; |
217 ZoneVector<WasmDataSegmentEncoder*> data_segments_; | 196 ZoneVector<WasmDataSegmentEncoder*> data_segments_; |
218 ZoneVector<uint32_t> indirect_functions_; | 197 ZoneVector<uint32_t> indirect_functions_; |
219 ZoneVector<std::pair<MachineType, bool>> globals_; | 198 ZoneVector<std::pair<MachineType, bool>> globals_; |
220 SignatureMap signature_map_; | 199 SignatureMap signature_map_; |
221 int start_function_index_; | 200 int start_function_index_; |
222 }; | 201 }; |
223 | 202 |
224 } // namespace wasm | 203 } // namespace wasm |
225 } // namespace internal | 204 } // namespace internal |
226 } // namespace v8 | 205 } // namespace v8 |
227 | 206 |
228 #endif // V8_WASM_ENCODER_H_ | 207 #endif // V8_WASM_ENCODER_H_ |
OLD | NEW |