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_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 // Convenience macros for building Wasm bytecode directly into a byte array. | 10 // Convenience macros for building Wasm bytecode directly into a byte array. |
| (...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 275 | 275 |
| 276 #define SIG_INDEX(v) U16_LE(v) | 276 #define SIG_INDEX(v) U16_LE(v) |
| 277 #define FUNC_INDEX(v) U16_LE(v) | 277 #define FUNC_INDEX(v) U16_LE(v) |
| 278 #define NAME_OFFSET(v) U32_LE(v) | 278 #define NAME_OFFSET(v) U32_LE(v) |
| 279 | 279 |
| 280 #define MASK_7 ((1 << 7) - 1) | 280 #define MASK_7 ((1 << 7) - 1) |
| 281 #define MASK_14 ((1 << 14) - 1) | 281 #define MASK_14 ((1 << 14) - 1) |
| 282 #define MASK_21 ((1 << 21) - 1) | 282 #define MASK_21 ((1 << 21) - 1) |
| 283 #define MASK_28 ((1 << 28) - 1) | 283 #define MASK_28 ((1 << 28) - 1) |
| 284 | 284 |
| 285 #define U32V_1(x) static_cast<byte>(x & MASK_7) | 285 #define U32V_1(x) static_cast<byte>((x)&MASK_7) |
|
bradnelson
2016/03/02 21:51:15
inconsistent spacing around operator
titzer
2016/03/02 22:43:41
Somehow that's what git cl format has done. Fixed
| |
| 286 #define U32V_2(x) \ | 286 #define U32V_2(x) \ |
| 287 static_cast<byte>((x & MASK_7) | 0x80), static_cast<byte>((x >> 7) & MASK_7) | 287 static_cast<byte>(((x)&MASK_7) | 0x80), static_cast<byte>(((x) >> 7) & MASK_7) |
| 288 #define U32V_3(x) \ | 288 #define U32V_3(x) \ |
| 289 static_cast<byte>((x & MASK_7) | 0x80), \ | 289 static_cast<byte>(((x)&MASK_7) | 0x80), \ |
| 290 static_cast<byte>(((x >> 7) & MASK_7) | 0x80), \ | 290 static_cast<byte>((((x) >> 7) & MASK_7) | 0x80), \ |
|
bradnelson
2016/03/02 21:51:15
yeah not having those parens was muy mal.
titzer
2016/03/02 22:43:41
Acknowledged.
| |
| 291 static_cast<byte>((x >> 14) & MASK_7) | 291 static_cast<byte>(((x) >> 14) & MASK_7) |
| 292 #define U32V_4(x) \ | 292 #define U32V_4(x) \ |
| 293 static_cast<byte>((x & MASK_7) | 0x80), \ | 293 static_cast<byte>(((x)&MASK_7) | 0x80), \ |
| 294 static_cast<byte>(((x >> 7) & MASK_7) | 0x80), \ | 294 static_cast<byte>((((x) >> 7) & MASK_7) | 0x80), \ |
| 295 static_cast<byte>(((x >> 14) & MASK_7) | 0x80), \ | 295 static_cast<byte>((((x) >> 14) & MASK_7) | 0x80), \ |
| 296 static_cast<byte>((x >> 21) & MASK_7) | 296 static_cast<byte>(((x) >> 21) & MASK_7) |
| 297 #define U32V_5(x) \ | 297 #define U32V_5(x) \ |
| 298 static_cast<byte>((x & MASK_7) | 0x80), \ | 298 static_cast<byte>(((x)&MASK_7) | 0x80), \ |
| 299 static_cast<byte>(((x >> 7) & MASK_7) | 0x80), \ | 299 static_cast<byte>((((x) >> 7) & MASK_7) | 0x80), \ |
| 300 static_cast<byte>(((x >> 14) & MASK_7) | 0x80), \ | 300 static_cast<byte>((((x) >> 14) & MASK_7) | 0x80), \ |
| 301 static_cast<byte>(((x >> 21) & MASK_7) | 0x80), \ | 301 static_cast<byte>((((x) >> 21) & MASK_7) | 0x80), \ |
| 302 static_cast<byte>((x >> 28) & 0xF) | 302 static_cast<byte>(((x) >> 28) & 0xF) |
| 303 | 303 |
| 304 #define WASM_NO_LOCALS 0 | |
| 305 | |
| 306 #define WASM_DECL_LOCALS(count, type) \ | |
| 307 static_cast<byte>(count), WasmOpcodes::LocalTypeCodeFor(type) | |
| 308 | |
| 309 namespace v8 { | |
| 310 namespace internal { | |
| 311 namespace wasm { | |
| 312 | |
| 313 // A helper for encoding local declarations prepended to the body of a | |
| 314 // function. | |
| 315 class LocalDeclEncoder { | |
| 316 public: | |
| 317 // Prepend local declarations by creating a new buffer and copying data | |
| 318 // over. The new buffer must be delete[]'d by the caller. | |
| 319 void Prepend(const byte** start, const byte** end) const { | |
| 320 size_t size = (*end - *start); | |
| 321 byte* buffer = new byte[5 + 6 * local_decls.size() + size]; | |
|
binji
2016/03/02 22:51:38
name the magic numbers?
titzer
2016/03/02 23:03:28
Fixed to use Size(), which should be more robust.
| |
| 322 size_t pos = Emit(buffer); | |
| 323 memcpy(buffer + pos, *start, size); | |
| 324 pos += size; | |
| 325 *start = buffer; | |
| 326 *end = buffer + pos; | |
| 327 } | |
| 328 | |
| 329 size_t Emit(byte* buffer) const { | |
| 330 size_t pos = 0; | |
| 331 pos = WriteUint32v(buffer, pos, static_cast<uint32_t>(local_decls.size())); | |
| 332 for (size_t i = 0; i < local_decls.size(); i++) { | |
| 333 pos = WriteUint32v(buffer, pos, local_decls[i].first); | |
| 334 buffer[pos++] = WasmOpcodes::LocalTypeCodeFor(local_decls[i].second); | |
| 335 } | |
| 336 return pos; | |
| 337 } | |
| 338 | |
| 339 // Add locals declarations to this helper. Return the index of the newly added | |
| 340 // local(s), with an optional adjustment for the parameters. | |
| 341 uint32_t AddLocals(uint32_t count, LocalType type, | |
| 342 FunctionSig* sig = nullptr) { | |
| 343 if (count == 0) return static_cast<uint32_t>(local_decls.size()); | |
|
binji
2016/03/02 22:51:38
include sig->parameter_count() if sig is non-null?
titzer
2016/03/02 23:03:28
Done.
| |
| 344 size_t pos = local_decls.size(); | |
| 345 if (local_decls.size() > 0 && local_decls.back().second == type) { | |
| 346 count += local_decls.back().first; | |
| 347 local_decls.pop_back(); | |
| 348 } | |
| 349 local_decls.push_back(std::pair<uint32_t, LocalType>(count, type)); | |
| 350 return static_cast<uint32_t>(pos + (sig ? sig->parameter_count() : 0)); | |
| 351 } | |
| 352 | |
| 353 size_t Size() const { | |
| 354 size_t size = SizeofUint32v(local_decls.size()); | |
| 355 for (auto p : local_decls) size += 1 + SizeofUint32v(p.first); | |
| 356 return size; | |
| 357 } | |
| 358 | |
| 359 private: | |
| 360 std::vector<std::pair<uint32_t, LocalType>> local_decls; | |
| 361 | |
| 362 size_t SizeofUint32v(size_t val) const { | |
|
bradnelson
2016/03/02 21:51:15
This will move out too I assume?
titzer
2016/03/02 22:43:41
Yeah, it needs to find a nice common home.
binji
2016/03/02 22:51:38
should be uint32_t val, right?
titzer
2016/03/02 23:03:28
Done.
| |
| 363 size_t size = 1; | |
| 364 while (true) { | |
| 365 byte b = val & MASK_7; | |
| 366 if (b == val) return size; | |
| 367 size++; | |
| 368 val = val >> 7; | |
| 369 } | |
| 370 } | |
| 371 | |
| 372 // TODO(titzer): lift encoding of u32v to a common place. | |
| 373 size_t WriteUint32v(byte* buffer, size_t pos, uint32_t val) const { | |
| 374 while (true) { | |
| 375 byte b = val & MASK_7; | |
| 376 if (b == val) { | |
| 377 buffer[pos++] = b; | |
| 378 break; | |
| 379 } | |
| 380 buffer[pos++] = 0x80 | b; | |
| 381 val = val >> 7; | |
| 382 } | |
| 383 return pos; | |
| 384 } | |
| 385 }; | |
| 386 } // namespace wasm | |
| 387 } // namespace internal | |
| 388 } // namespace v8 | |
| 304 #endif // V8_WASM_MACRO_GEN_H_ | 389 #endif // V8_WASM_MACRO_GEN_H_ |
| OLD | NEW |