Chromium Code Reviews| 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/leb-helper.h" | |
| 13 #include "src/wasm/wasm-macro-gen.h" | 14 #include "src/wasm/wasm-macro-gen.h" |
| 14 #include "src/wasm/wasm-module.h" | 15 #include "src/wasm/wasm-module.h" |
| 15 #include "src/wasm/wasm-opcodes.h" | 16 #include "src/wasm/wasm-opcodes.h" |
| 16 #include "src/wasm/wasm-result.h" | 17 #include "src/wasm/wasm-result.h" |
| 17 | 18 |
| 18 namespace v8 { | 19 namespace v8 { |
| 19 namespace internal { | 20 namespace internal { |
| 20 namespace wasm { | 21 namespace wasm { |
| 21 | 22 |
| 23 class ZoneBuffer : public ZoneObject { | |
|
bradnelson
2016/05/25 14:51:23
How about in a separate file? Or you leaning on th
titzer
2016/05/25 15:40:21
I think we can pull this out to its own header in
| |
| 24 public: | |
| 25 static const uint32_t kInitialSize = 4096; | |
| 26 explicit ZoneBuffer(Zone* zone, size_t initial = kInitialSize) | |
| 27 : zone_(zone), buffer_(reinterpret_cast<byte*>(zone->New(initial))) { | |
| 28 pos_ = buffer_; | |
| 29 end_ = buffer_ + initial; | |
| 30 } | |
| 31 | |
| 32 void write_u8(uint8_t x) { | |
| 33 EnsureSpace(1); | |
| 34 *(pos_++) = x; | |
| 35 } | |
| 36 | |
| 37 void write_u16(uint16_t x) { | |
| 38 EnsureSpace(2); | |
| 39 WriteUnalignedUInt16(pos_, x); | |
| 40 pos_ += 2; | |
| 41 } | |
| 42 | |
| 43 void write_u32(uint32_t x) { | |
| 44 EnsureSpace(4); | |
| 45 WriteUnalignedUInt32(pos_, x); | |
| 46 pos_ += 4; | |
| 47 } | |
| 48 | |
| 49 void write_u32v(uint32_t val) { | |
| 50 EnsureSpace(kMaxVarInt32Size); | |
| 51 LEBHelper::write_u32v(&pos_, val); | |
| 52 } | |
| 53 | |
| 54 void write_size(size_t val) { | |
| 55 EnsureSpace(kMaxVarInt32Size); | |
| 56 DCHECK_EQ(val, static_cast<uint32_t>(val)); | |
| 57 LEBHelper::write_u32v(&pos_, static_cast<uint32_t>(val)); | |
| 58 } | |
| 59 | |
| 60 void write(const byte* data, size_t size) { | |
| 61 EnsureSpace(size); | |
| 62 memcpy(pos_, data, size); | |
| 63 pos_ += size; | |
| 64 } | |
| 65 | |
| 66 size_t reserve_u32v() { | |
| 67 size_t off = offset(); | |
| 68 EnsureSpace(kMaxVarInt32Size); | |
| 69 pos_ += kMaxVarInt32Size; | |
| 70 return off; | |
| 71 } | |
| 72 | |
| 73 // Patch a (padded) u32v at the given offset to be the given value. | |
| 74 void patch_u32v(size_t offset, uint32_t val) { | |
| 75 byte* ptr = buffer_ + offset; | |
| 76 for (size_t pos = 0; pos != kPaddedVarInt32Size; ++pos) { | |
| 77 uint32_t next = val >> 7; | |
| 78 byte out = static_cast<byte>(val & 0x7f); | |
| 79 if (pos != kPaddedVarInt32Size - 1) { | |
| 80 *(ptr++) = 0x80 | out; | |
| 81 val = next; | |
| 82 } else { | |
| 83 *(ptr++) = out; | |
| 84 } | |
| 85 } | |
| 86 } | |
| 87 | |
| 88 size_t offset() { return static_cast<size_t>(pos_ - buffer_); } | |
| 89 size_t size() { return static_cast<size_t>(pos_ - buffer_); } | |
| 90 const byte* begin() { return buffer_; } | |
| 91 const byte* end() { return pos_; } | |
| 92 | |
| 93 void EnsureSpace(size_t size) { | |
| 94 if ((pos_ + size) > end_) { | |
| 95 size_t new_size = 4096 + (end_ - buffer_) * 3; | |
|
bradnelson
2016/05/25 14:51:23
Are all the cool kids doing 3 instead of 2 these d
titzer
2016/05/25 15:40:21
Growth factors are hard. I always felt like 2x was
| |
| 96 byte* new_buffer = reinterpret_cast<byte*>(zone_->New(new_size)); | |
| 97 memcpy(new_buffer, buffer_, (pos_ - buffer_)); | |
| 98 pos_ = new_buffer + (pos_ - buffer_); | |
| 99 buffer_ = new_buffer; | |
| 100 end_ = new_buffer + new_size; | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 byte** pos_ptr() { return &pos_; } | |
|
bradnelson
2016/05/25 14:51:23
That seems a rather leaky abstraction... reserve j
titzer
2016/05/25 15:40:21
Yeah, it's because of a special case with the loca
| |
| 105 | |
| 106 private: | |
| 107 Zone* zone_; | |
| 108 byte* buffer_; | |
| 109 byte* pos_; | |
| 110 byte* end_; | |
| 111 }; | |
| 112 | |
| 22 class WasmModuleBuilder; | 113 class WasmModuleBuilder; |
| 23 | 114 |
| 24 class WasmFunctionEncoder : public ZoneObject { | 115 class WasmFunctionEncoder : public ZoneObject { |
| 25 public: | 116 public: |
| 26 uint32_t HeaderSize() const; | 117 void WriteSignature(ZoneBuffer& buffer) const; |
| 27 uint32_t BodySize() const; | 118 void WriteExport(ZoneBuffer& buffer, uint32_t func_index) const; |
| 28 uint32_t NameSize() const; | 119 void WriteBody(ZoneBuffer& buffer) const; |
| 29 void Serialize(byte* buffer, byte** header, byte** body) const; | 120 bool exported() const { return exported_; } |
| 30 | 121 |
| 31 private: | 122 private: |
| 32 WasmFunctionEncoder(Zone* zone, LocalDeclEncoder locals, bool exported); | 123 WasmFunctionEncoder(Zone* zone, LocalDeclEncoder locals, bool exported); |
| 33 friend class WasmFunctionBuilder; | 124 friend class WasmFunctionBuilder; |
| 34 uint32_t signature_index_; | 125 uint32_t signature_index_; |
| 35 LocalDeclEncoder locals_; | 126 LocalDeclEncoder locals_; |
| 36 bool exported_; | 127 bool exported_; |
| 37 ZoneVector<uint8_t> body_; | 128 ZoneVector<uint8_t> body_; |
| 38 ZoneVector<char> name_; | 129 ZoneVector<char> name_; |
| 39 | 130 |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 64 uint8_t exported_; | 155 uint8_t exported_; |
| 65 ZoneVector<uint8_t> body_; | 156 ZoneVector<uint8_t> body_; |
| 66 ZoneVector<char> name_; | 157 ZoneVector<char> name_; |
| 67 void IndexVars(WasmFunctionEncoder* e, uint32_t* var_index) const; | 158 void IndexVars(WasmFunctionEncoder* e, uint32_t* var_index) const; |
| 68 }; | 159 }; |
| 69 | 160 |
| 70 class WasmDataSegmentEncoder : public ZoneObject { | 161 class WasmDataSegmentEncoder : public ZoneObject { |
| 71 public: | 162 public: |
| 72 WasmDataSegmentEncoder(Zone* zone, const byte* data, uint32_t size, | 163 WasmDataSegmentEncoder(Zone* zone, const byte* data, uint32_t size, |
| 73 uint32_t dest); | 164 uint32_t dest); |
| 74 uint32_t HeaderSize() const; | 165 void Write(ZoneBuffer& buffer) const; |
| 75 uint32_t BodySize() const; | |
| 76 void Serialize(byte* buffer, byte** header, byte** body) const; | |
| 77 | 166 |
| 78 private: | 167 private: |
| 79 ZoneVector<byte> data_; | 168 ZoneVector<byte> data_; |
| 80 uint32_t dest_; | 169 uint32_t dest_; |
| 81 }; | 170 }; |
| 82 | 171 |
| 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 | |
| 96 struct WasmFunctionImport { | 172 struct WasmFunctionImport { |
| 97 uint32_t sig_index; | 173 uint32_t sig_index; |
| 98 const char* name; | 174 const char* name; |
| 99 int name_length; | 175 int name_length; |
| 100 }; | 176 }; |
| 101 | 177 |
| 102 class WasmModuleWriter : public ZoneObject { | 178 class WasmModuleWriter : public ZoneObject { |
| 103 public: | 179 public: |
| 104 WasmModuleIndex* WriteTo(Zone* zone) const; | 180 void WriteTo(ZoneBuffer& buffer) const; |
| 105 | 181 |
| 106 private: | 182 private: |
| 107 friend class WasmModuleBuilder; | 183 friend class WasmModuleBuilder; |
| 108 explicit WasmModuleWriter(Zone* zone); | 184 explicit WasmModuleWriter(Zone* zone); |
| 109 ZoneVector<WasmFunctionImport> imports_; | 185 ZoneVector<WasmFunctionImport> imports_; |
| 110 ZoneVector<WasmFunctionEncoder*> functions_; | 186 ZoneVector<WasmFunctionEncoder*> functions_; |
| 111 ZoneVector<WasmDataSegmentEncoder*> data_segments_; | 187 ZoneVector<WasmDataSegmentEncoder*> data_segments_; |
| 112 ZoneVector<FunctionSig*> signatures_; | 188 ZoneVector<FunctionSig*> signatures_; |
| 113 ZoneVector<uint32_t> indirect_functions_; | 189 ZoneVector<uint32_t> indirect_functions_; |
| 114 ZoneVector<std::pair<MachineType, bool>> globals_; | 190 ZoneVector<std::pair<MachineType, bool>> globals_; |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 143 ZoneVector<std::pair<MachineType, bool>> globals_; | 219 ZoneVector<std::pair<MachineType, bool>> globals_; |
| 144 SignatureMap signature_map_; | 220 SignatureMap signature_map_; |
| 145 int start_function_index_; | 221 int start_function_index_; |
| 146 }; | 222 }; |
| 147 | 223 |
| 148 } // namespace wasm | 224 } // namespace wasm |
| 149 } // namespace internal | 225 } // namespace internal |
| 150 } // namespace v8 | 226 } // namespace v8 |
| 151 | 227 |
| 152 #endif // V8_WASM_ENCODER_H_ | 228 #endif // V8_WASM_ENCODER_H_ |
| OLD | NEW |