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