OLD | NEW |
(Empty) | |
| 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 |
| 13 #include "src/wasm/wasm-module.h" |
| 14 #include "src/wasm/wasm-opcodes.h" |
| 15 #include "src/wasm/wasm-result.h" |
| 16 |
| 17 namespace v8 { |
| 18 namespace internal { |
| 19 namespace wasm { |
| 20 |
| 21 class WasmModuleBuilder; |
| 22 |
| 23 class WasmFunctionEncoder : public ZoneObject { |
| 24 public: |
| 25 uint32_t HeaderSize() const; |
| 26 uint32_t BodySize() const; |
| 27 uint32_t NameSize() const; |
| 28 void Serialize(byte* buffer, byte** header, byte** body) const; |
| 29 |
| 30 private: |
| 31 WasmFunctionEncoder(Zone* zone, LocalType return_type, bool exported, |
| 32 bool external); |
| 33 friend class WasmFunctionBuilder; |
| 34 uint16_t signature_index_; |
| 35 ZoneVector<LocalType> params_; |
| 36 uint16_t local_int32_count_; |
| 37 uint16_t local_int64_count_; |
| 38 uint16_t local_float32_count_; |
| 39 uint16_t local_float64_count_; |
| 40 bool exported_; |
| 41 bool external_; |
| 42 ZoneVector<uint8_t> body_; |
| 43 ZoneVector<char> name_; |
| 44 |
| 45 bool HasLocals() const { |
| 46 return (local_int32_count_ + local_int64_count_ + local_float32_count_ + |
| 47 local_float64_count_) > 0; |
| 48 } |
| 49 |
| 50 bool HasName() const { return exported_ && name_.size() > 0; } |
| 51 }; |
| 52 |
| 53 class WasmFunctionBuilder : public ZoneObject { |
| 54 public: |
| 55 uint16_t AddParam(LocalType type); |
| 56 uint16_t AddLocal(LocalType type); |
| 57 void ReturnType(LocalType type); |
| 58 void EmitCode(const byte* code, uint32_t code_size); |
| 59 void EmitCode(const byte* code, uint32_t code_size, |
| 60 const uint32_t* local_indices, uint32_t indices_size); |
| 61 void Emit(WasmOpcode opcode); |
| 62 void EmitWithU8(WasmOpcode opcode, const byte immediate); |
| 63 void EmitWithLocal(WasmOpcode opcode); |
| 64 uint32_t EmitEditableImmediate(const byte immediate); |
| 65 void EditImmediate(uint32_t offset, const byte immediate); |
| 66 void Exported(uint8_t flag); |
| 67 void External(uint8_t flag); |
| 68 WasmFunctionEncoder* Build(Zone* zone, WasmModuleBuilder* mb) const; |
| 69 |
| 70 private: |
| 71 WasmFunctionBuilder(Zone* zone, const unsigned char* name, int name_length); |
| 72 friend class WasmModuleBuilder; |
| 73 LocalType return_type_; |
| 74 struct Type; |
| 75 ZoneVector<Type> locals_; |
| 76 uint8_t exported_; |
| 77 uint8_t external_; |
| 78 ZoneVector<uint8_t> body_; |
| 79 ZoneVector<uint32_t> local_indices_; |
| 80 ZoneVector<char> name_; |
| 81 uint16_t AddVar(LocalType type, bool param); |
| 82 void IndexVars(WasmFunctionEncoder* e, uint16_t* var_index) const; |
| 83 }; |
| 84 |
| 85 class WasmDataSegmentEncoder : public ZoneObject { |
| 86 public: |
| 87 WasmDataSegmentEncoder(Zone* zone, const byte* data, uint32_t size, |
| 88 uint32_t dest); |
| 89 uint32_t HeaderSize() const; |
| 90 uint32_t BodySize() const; |
| 91 void Serialize(byte* buffer, byte** header, byte** body) const; |
| 92 |
| 93 private: |
| 94 ZoneVector<byte> data_; |
| 95 uint32_t dest_; |
| 96 }; |
| 97 |
| 98 class WasmModuleIndex : public ZoneObject { |
| 99 public: |
| 100 const byte* Begin() const { return begin_; } |
| 101 const byte* End() const { return end_; } |
| 102 |
| 103 private: |
| 104 friend class WasmModuleWriter; |
| 105 WasmModuleIndex(const byte* begin, const byte* end) |
| 106 : begin_(begin), end_(end) {} |
| 107 const byte* begin_; |
| 108 const byte* end_; |
| 109 }; |
| 110 |
| 111 class WasmModuleWriter : public ZoneObject { |
| 112 public: |
| 113 WasmModuleIndex* WriteTo(Zone* zone) const; |
| 114 |
| 115 private: |
| 116 friend class WasmModuleBuilder; |
| 117 explicit WasmModuleWriter(Zone* zone); |
| 118 ZoneVector<WasmFunctionEncoder*> functions_; |
| 119 ZoneVector<WasmDataSegmentEncoder*> data_segments_; |
| 120 ZoneVector<FunctionSig*> signatures_; |
| 121 ZoneVector<uint16_t> indirect_functions_; |
| 122 ZoneVector<std::pair<MachineType, bool>> globals_; |
| 123 }; |
| 124 |
| 125 class WasmModuleBuilder : public ZoneObject { |
| 126 public: |
| 127 explicit WasmModuleBuilder(Zone* zone); |
| 128 uint16_t AddFunction(const unsigned char* name = NULL, int name_length = 0); |
| 129 uint32_t AddGlobal(MachineType type, bool exported); |
| 130 WasmFunctionBuilder* FunctionAt(size_t index); |
| 131 void AddDataSegment(WasmDataSegmentEncoder* data); |
| 132 uint16_t AddSignature(FunctionSig* sig); |
| 133 void AddIndirectFunction(uint16_t index); |
| 134 WasmModuleWriter* Build(Zone* zone); |
| 135 |
| 136 private: |
| 137 struct CompareFunctionSigs { |
| 138 int operator()(FunctionSig* a, FunctionSig* b); |
| 139 }; |
| 140 typedef ZoneMap<FunctionSig*, uint16_t, CompareFunctionSigs> SignatureMap; |
| 141 |
| 142 Zone* zone_; |
| 143 ZoneVector<FunctionSig*> signatures_; |
| 144 ZoneVector<WasmFunctionBuilder*> functions_; |
| 145 ZoneVector<WasmDataSegmentEncoder*> data_segments_; |
| 146 ZoneVector<uint16_t> indirect_functions_; |
| 147 ZoneVector<std::pair<MachineType, bool>> globals_; |
| 148 SignatureMap signature_map_; |
| 149 }; |
| 150 |
| 151 std::vector<uint8_t> UnsignedLEB128From(uint32_t result); |
| 152 } // namespace wasm |
| 153 } // namespace internal |
| 154 } // namespace v8 |
| 155 |
| 156 #endif // V8_WASM_ENCODER_H_ |
OLD | NEW |