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_MACRO_GEN_H_ | 5 #ifndef V8_WASM_MACRO_GEN_H_ |
6 #define V8_WASM_MACRO_GEN_H_ | 6 #define V8_WASM_MACRO_GEN_H_ |
7 | 7 |
8 #include "src/wasm/wasm-opcodes.h" | 8 #include "src/wasm/wasm-opcodes.h" |
9 | 9 |
| 10 #include "src/zone-containers.h" |
| 11 |
10 #define U32_LE(v) \ | 12 #define U32_LE(v) \ |
11 static_cast<byte>(v), static_cast<byte>((v) >> 8), \ | 13 static_cast<byte>(v), static_cast<byte>((v) >> 8), \ |
12 static_cast<byte>((v) >> 16), static_cast<byte>((v) >> 24) | 14 static_cast<byte>((v) >> 16), static_cast<byte>((v) >> 24) |
13 | 15 |
14 #define U16_LE(v) static_cast<byte>(v), static_cast<byte>((v) >> 8) | 16 #define U16_LE(v) static_cast<byte>(v), static_cast<byte>((v) >> 8) |
15 | 17 |
16 #define WASM_MODULE_HEADER U32_LE(kWasmMagic), U32_LE(kWasmVersion) | 18 #define WASM_MODULE_HEADER U32_LE(kWasmMagic), U32_LE(kWasmVersion) |
17 | 19 |
18 #define SIG_INDEX(v) U16_LE(v) | 20 #define SIG_INDEX(v) U16_LE(v) |
19 // TODO(binji): make SIG_INDEX match this. | 21 // TODO(binji): make SIG_INDEX match this. |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 DCHECK(length == 5 || I32V_IN_RANGE(value, length)); | 125 DCHECK(length == 5 || I32V_IN_RANGE(value, length)); |
124 } | 126 } |
125 | 127 |
126 inline void CheckI64v(int64_t value, int length) { | 128 inline void CheckI64v(int64_t value, int length) { |
127 DCHECK(length >= 1 && length <= 10); | 129 DCHECK(length >= 1 && length <= 10); |
128 DCHECK(length == 10 || I64V_IN_RANGE(value, length)); | 130 DCHECK(length == 10 || I64V_IN_RANGE(value, length)); |
129 } | 131 } |
130 | 132 |
131 // A helper for encoding local declarations prepended to the body of a | 133 // A helper for encoding local declarations prepended to the body of a |
132 // function. | 134 // function. |
| 135 // TODO(titzer): move this to an appropriate header. |
133 class LocalDeclEncoder { | 136 class LocalDeclEncoder { |
134 public: | 137 public: |
| 138 explicit LocalDeclEncoder(Zone* zone, FunctionSig* s = nullptr) |
| 139 : sig(s), local_decls(zone), total(0) {} |
| 140 |
135 // Prepend local declarations by creating a new buffer and copying data | 141 // Prepend local declarations by creating a new buffer and copying data |
136 // over. The new buffer must be delete[]'d by the caller. | 142 // over. The new buffer must be delete[]'d by the caller. |
137 void Prepend(const byte** start, const byte** end) const { | 143 void Prepend(const byte** start, const byte** end) const { |
138 size_t size = (*end - *start); | 144 size_t size = (*end - *start); |
139 byte* buffer = new byte[Size() + size]; | 145 byte* buffer = new byte[Size() + size]; |
140 size_t pos = Emit(buffer); | 146 size_t pos = Emit(buffer); |
141 memcpy(buffer + pos, *start, size); | 147 memcpy(buffer + pos, *start, size); |
142 pos += size; | 148 pos += size; |
143 *start = buffer; | 149 *start = buffer; |
144 *end = buffer + pos; | 150 *end = buffer + pos; |
145 } | 151 } |
146 | 152 |
147 size_t Emit(byte* buffer) const { | 153 size_t Emit(byte* buffer) const { |
148 size_t pos = 0; | 154 size_t pos = 0; |
149 pos = WriteUint32v(buffer, pos, static_cast<uint32_t>(local_decls.size())); | 155 pos = WriteUint32v(buffer, pos, static_cast<uint32_t>(local_decls.size())); |
150 for (size_t i = 0; i < local_decls.size(); i++) { | 156 for (size_t i = 0; i < local_decls.size(); i++) { |
151 pos = WriteUint32v(buffer, pos, local_decls[i].first); | 157 pos = WriteUint32v(buffer, pos, local_decls[i].first); |
152 buffer[pos++] = WasmOpcodes::LocalTypeCodeFor(local_decls[i].second); | 158 buffer[pos++] = WasmOpcodes::LocalTypeCodeFor(local_decls[i].second); |
153 } | 159 } |
154 DCHECK_EQ(Size(), pos); | 160 DCHECK_EQ(Size(), pos); |
155 return pos; | 161 return pos; |
156 } | 162 } |
157 | 163 |
158 // Add locals declarations to this helper. Return the index of the newly added | 164 // Add locals declarations to this helper. Return the index of the newly added |
159 // local(s), with an optional adjustment for the parameters. | 165 // local(s), with an optional adjustment for the parameters. |
160 uint32_t AddLocals(uint32_t count, LocalType type, | 166 uint32_t AddLocals(uint32_t count, LocalType type) { |
161 FunctionSig* sig = nullptr) { | 167 uint32_t result = |
162 if (count == 0) { | 168 static_cast<uint32_t>(total + (sig ? sig->parameter_count() : 0)); |
163 return static_cast<uint32_t>((sig ? sig->parameter_count() : 0) + | 169 total += count; |
164 local_decls.size()); | |
165 } | |
166 size_t pos = local_decls.size(); | |
167 if (local_decls.size() > 0 && local_decls.back().second == type) { | 170 if (local_decls.size() > 0 && local_decls.back().second == type) { |
168 count += local_decls.back().first; | 171 count += local_decls.back().first; |
169 local_decls.pop_back(); | 172 local_decls.pop_back(); |
170 } | 173 } |
171 local_decls.push_back(std::pair<uint32_t, LocalType>(count, type)); | 174 local_decls.push_back(std::pair<uint32_t, LocalType>(count, type)); |
172 return static_cast<uint32_t>(pos + (sig ? sig->parameter_count() : 0)); | 175 return result; |
173 } | 176 } |
174 | 177 |
175 size_t Size() const { | 178 size_t Size() const { |
176 size_t size = SizeofUint32v(static_cast<uint32_t>(local_decls.size())); | 179 size_t size = SizeofUint32v(static_cast<uint32_t>(local_decls.size())); |
177 for (auto p : local_decls) size += 1 + SizeofUint32v(p.first); | 180 for (auto p : local_decls) size += 1 + SizeofUint32v(p.first); |
178 return size; | 181 return size; |
179 } | 182 } |
180 | 183 |
| 184 bool has_sig() const { return sig != nullptr; } |
| 185 FunctionSig* get_sig() const { return sig; } |
| 186 void set_sig(FunctionSig* s) { sig = s; } |
| 187 |
181 private: | 188 private: |
182 std::vector<std::pair<uint32_t, LocalType>> local_decls; | 189 FunctionSig* sig; |
| 190 ZoneVector<std::pair<uint32_t, LocalType>> local_decls; |
| 191 size_t total; |
183 | 192 |
184 size_t SizeofUint32v(uint32_t val) const { | 193 size_t SizeofUint32v(uint32_t val) const { |
185 size_t size = 1; | 194 size_t size = 1; |
186 while (true) { | 195 while (true) { |
187 byte b = val & MASK_7; | 196 byte b = val & MASK_7; |
188 if (b == val) return size; | 197 if (b == val) return size; |
189 size++; | 198 size++; |
190 val = val >> 7; | 199 val = val >> 7; |
191 } | 200 } |
192 } | 201 } |
(...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
574 #define SIG_ENTRY_x(r) kWasmFunctionTypeForm, 0, 1, r | 583 #define SIG_ENTRY_x(r) kWasmFunctionTypeForm, 0, 1, r |
575 #define SIG_ENTRY_x_x(r, a) kWasmFunctionTypeForm, 1, a, 1, r | 584 #define SIG_ENTRY_x_x(r, a) kWasmFunctionTypeForm, 1, a, 1, r |
576 #define SIG_ENTRY_x_xx(r, a, b) kWasmFunctionTypeForm, 2, a, b, 1, r | 585 #define SIG_ENTRY_x_xx(r, a, b) kWasmFunctionTypeForm, 2, a, b, 1, r |
577 #define SIG_ENTRY_x_xxx(r, a, b, c) kWasmFunctionTypeForm, 3, a, b, c, 1, r | 586 #define SIG_ENTRY_x_xxx(r, a, b, c) kWasmFunctionTypeForm, 3, a, b, c, 1, r |
578 #define SIZEOF_SIG_ENTRY_x 4 | 587 #define SIZEOF_SIG_ENTRY_x 4 |
579 #define SIZEOF_SIG_ENTRY_x_x 5 | 588 #define SIZEOF_SIG_ENTRY_x_x 5 |
580 #define SIZEOF_SIG_ENTRY_x_xx 6 | 589 #define SIZEOF_SIG_ENTRY_x_xx 6 |
581 #define SIZEOF_SIG_ENTRY_x_xxx 7 | 590 #define SIZEOF_SIG_ENTRY_x_xxx 7 |
582 | 591 |
583 #endif // V8_WASM_MACRO_GEN_H_ | 592 #endif // V8_WASM_MACRO_GEN_H_ |
OLD | NEW |