Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef V8_WASM_ENCODER_H_ |
| 6 | #define V8_WASM_ENCODER_H_ |
| 7 | |
| 8 | #include "src/signature.h" |
| 9 | #include "src/zone-containers.h" |
| 10 | |
| 11 | #include "src/base/smart-pointers.h" |
| 12 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 13 | #include "src/wasm/wasm-macro-gen.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14 | #include "src/wasm/wasm-module.h" |
| 15 | #include "src/wasm/wasm-opcodes.h" |
| 16 | #include "src/wasm/wasm-result.h" |
| 17 | |
| 18 | namespace v8 { |
| 19 | namespace internal { |
| 20 | namespace wasm { |
| 21 | |
| 22 | class WasmModuleBuilder; |
| 23 | |
| 24 | class WasmFunctionEncoder : public ZoneObject { |
| 25 | public: |
| 26 | uint32_t HeaderSize() const; |
| 27 | uint32_t BodySize() const; |
| 28 | uint32_t NameSize() const; |
| 29 | void Serialize(byte* buffer, byte** header, byte** body) const; |
| 30 | |
| 31 | private: |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 32 | WasmFunctionEncoder(Zone* zone, LocalDeclEncoder locals, bool exported); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 33 | friend class WasmFunctionBuilder; |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 34 | uint32_t signature_index_; |
| 35 | LocalDeclEncoder locals_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 36 | bool exported_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 37 | ZoneVector<uint8_t> body_; |
| 38 | ZoneVector<char> name_; |
| 39 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 40 | bool HasName() const { return exported_ && name_.size() > 0; } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | class WasmFunctionBuilder : public ZoneObject { |
| 44 | public: |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 45 | void SetSignature(FunctionSig* sig); |
| 46 | uint32_t AddLocal(LocalType type); |
| 47 | void EmitVarInt(uint32_t val); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 48 | void EmitCode(const byte* code, uint32_t code_size); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 49 | void Emit(WasmOpcode opcode); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 50 | void EmitGetLocal(uint32_t index); |
| 51 | void EmitSetLocal(uint32_t index); |
| 52 | void EmitI32Const(int32_t val); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 53 | void EmitWithU8(WasmOpcode opcode, const byte immediate); |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 54 | void EmitWithU8U8(WasmOpcode opcode, const byte imm1, const byte imm2); |
| 55 | void EmitWithVarInt(WasmOpcode opcode, uint32_t immediate); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 56 | void Exported(uint8_t flag); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 57 | void SetName(const char* name, int name_length); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 58 | WasmFunctionEncoder* Build(Zone* zone, WasmModuleBuilder* mb) const; |
| 59 | |
| 60 | private: |
| 61 | explicit WasmFunctionBuilder(Zone* zone); |
| 62 | friend class WasmModuleBuilder; |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 63 | LocalDeclEncoder locals_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 64 | uint8_t exported_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 65 | ZoneVector<uint8_t> body_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 66 | ZoneVector<char> name_; |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 67 | void IndexVars(WasmFunctionEncoder* e, uint32_t* var_index) const; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | class WasmDataSegmentEncoder : public ZoneObject { |
| 71 | public: |
| 72 | WasmDataSegmentEncoder(Zone* zone, const byte* data, uint32_t size, |
| 73 | uint32_t dest); |
| 74 | uint32_t HeaderSize() const; |
| 75 | uint32_t BodySize() const; |
| 76 | void Serialize(byte* buffer, byte** header, byte** body) const; |
| 77 | |
| 78 | private: |
| 79 | ZoneVector<byte> data_; |
| 80 | uint32_t dest_; |
| 81 | }; |
| 82 | |
| 83 | class WasmModuleIndex : public ZoneObject { |
| 84 | public: |
| 85 | const byte* Begin() const { return begin_; } |
| 86 | const byte* End() const { return end_; } |
| 87 | |
| 88 | private: |
| 89 | friend class WasmModuleWriter; |
| 90 | WasmModuleIndex(const byte* begin, const byte* end) |
| 91 | : begin_(begin), end_(end) {} |
| 92 | const byte* begin_; |
| 93 | const byte* end_; |
| 94 | }; |
| 95 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 96 | struct WasmFunctionImport { |
| 97 | uint32_t sig_index; |
| 98 | const char* name; |
| 99 | int name_length; |
| 100 | }; |
| 101 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 102 | class WasmModuleWriter : public ZoneObject { |
| 103 | public: |
| 104 | WasmModuleIndex* WriteTo(Zone* zone) const; |
| 105 | |
| 106 | private: |
| 107 | friend class WasmModuleBuilder; |
| 108 | explicit WasmModuleWriter(Zone* zone); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 109 | ZoneVector<WasmFunctionImport> imports_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 110 | ZoneVector<WasmFunctionEncoder*> functions_; |
| 111 | ZoneVector<WasmDataSegmentEncoder*> data_segments_; |
| 112 | ZoneVector<FunctionSig*> signatures_; |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 113 | ZoneVector<uint32_t> indirect_functions_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 114 | ZoneVector<std::pair<MachineType, bool>> globals_; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 115 | int start_function_index_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | class WasmModuleBuilder : public ZoneObject { |
| 119 | public: |
| 120 | explicit WasmModuleBuilder(Zone* zone); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 121 | uint32_t AddFunction(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 122 | uint32_t AddGlobal(MachineType type, bool exported); |
| 123 | WasmFunctionBuilder* FunctionAt(size_t index); |
| 124 | void AddDataSegment(WasmDataSegmentEncoder* data); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 125 | uint32_t AddSignature(FunctionSig* sig); |
| 126 | void AddIndirectFunction(uint32_t index); |
| 127 | void MarkStartFunction(uint32_t index); |
| 128 | uint32_t AddImport(const char* name, int name_length, FunctionSig* sig); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 129 | WasmModuleWriter* Build(Zone* zone); |
| 130 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 131 | struct CompareFunctionSigs { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 132 | bool operator()(FunctionSig* a, FunctionSig* b) const; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 133 | }; |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 134 | typedef ZoneMap<FunctionSig*, uint32_t, CompareFunctionSigs> SignatureMap; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 135 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 136 | private: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 137 | Zone* zone_; |
| 138 | ZoneVector<FunctionSig*> signatures_; |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 139 | ZoneVector<WasmFunctionImport> imports_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 140 | ZoneVector<WasmFunctionBuilder*> functions_; |
| 141 | ZoneVector<WasmDataSegmentEncoder*> data_segments_; |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 142 | ZoneVector<uint32_t> indirect_functions_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 143 | ZoneVector<std::pair<MachineType, bool>> globals_; |
| 144 | SignatureMap signature_map_; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 145 | int start_function_index_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 146 | }; |
| 147 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 148 | } // namespace wasm |
| 149 | } // namespace internal |
| 150 | } // namespace v8 |
| 151 | |
| 152 | #endif // V8_WASM_ENCODER_H_ |