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_AST_DECODER_H_ | 5 #ifndef V8_WASM_AST_DECODER_H_ |
6 #define V8_WASM_AST_DECODER_H_ | 6 #define V8_WASM_AST_DECODER_H_ |
7 | 7 |
| 8 #include <iterator> |
| 9 |
8 #include "src/base/compiler-specific.h" | 10 #include "src/base/compiler-specific.h" |
| 11 #include "src/base/iterator.h" |
9 #include "src/globals.h" | 12 #include "src/globals.h" |
10 #include "src/signature.h" | 13 #include "src/signature.h" |
11 #include "src/wasm/decoder.h" | 14 #include "src/wasm/decoder.h" |
12 #include "src/wasm/wasm-opcodes.h" | 15 #include "src/wasm/wasm-opcodes.h" |
13 #include "src/wasm/wasm-result.h" | 16 #include "src/wasm/wasm-result.h" |
14 | 17 |
15 namespace v8 { | 18 namespace v8 { |
16 namespace internal { | 19 namespace internal { |
17 | 20 |
18 class BitVector; // forward declaration | 21 class BitVector; // forward declaration |
(...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 V8_EXPORT_PRIVATE BitVector* AnalyzeLoopAssignmentForTesting(Zone* zone, | 378 V8_EXPORT_PRIVATE BitVector* AnalyzeLoopAssignmentForTesting(Zone* zone, |
376 size_t num_locals, | 379 size_t num_locals, |
377 const byte* start, | 380 const byte* start, |
378 const byte* end); | 381 const byte* end); |
379 | 382 |
380 // Computes the length of the opcode at the given address. | 383 // Computes the length of the opcode at the given address. |
381 V8_EXPORT_PRIVATE unsigned OpcodeLength(const byte* pc, const byte* end); | 384 V8_EXPORT_PRIVATE unsigned OpcodeLength(const byte* pc, const byte* end); |
382 | 385 |
383 // A simple forward iterator for bytecodes. | 386 // A simple forward iterator for bytecodes. |
384 class V8_EXPORT_PRIVATE BytecodeIterator : public NON_EXPORTED_BASE(Decoder) { | 387 class V8_EXPORT_PRIVATE BytecodeIterator : public NON_EXPORTED_BASE(Decoder) { |
385 public: | 388 // Base class for both iterators defined below. |
386 // If one wants to iterate over the bytecode without looking at {pc_offset()}. | 389 class iterator_base { |
387 class iterator { | |
388 public: | 390 public: |
389 inline iterator& operator++() { | 391 inline iterator_base& operator++() { |
390 DCHECK_LT(ptr_, end_); | 392 DCHECK_LT(ptr_, end_); |
391 ptr_ += OpcodeLength(ptr_, end_); | 393 ptr_ += OpcodeLength(ptr_, end_); |
392 return *this; | 394 return *this; |
393 } | 395 } |
| 396 inline bool operator==(const iterator_base& that) { |
| 397 return this->ptr_ == that.ptr_; |
| 398 } |
| 399 inline bool operator!=(const iterator_base& that) { |
| 400 return this->ptr_ != that.ptr_; |
| 401 } |
| 402 |
| 403 protected: |
| 404 const byte* ptr_; |
| 405 const byte* end_; |
| 406 iterator_base(const byte* ptr, const byte* end) : ptr_(ptr), end_(end) {} |
| 407 }; |
| 408 |
| 409 public: |
| 410 // If one wants to iterate over the bytecode without looking at {pc_offset()}. |
| 411 class opcode_iterator |
| 412 : public iterator_base, |
| 413 public std::iterator<std::input_iterator_tag, WasmOpcode> { |
| 414 public: |
394 inline WasmOpcode operator*() { | 415 inline WasmOpcode operator*() { |
395 DCHECK_LT(ptr_, end_); | 416 DCHECK_LT(ptr_, end_); |
396 return static_cast<WasmOpcode>(*ptr_); | 417 return static_cast<WasmOpcode>(*ptr_); |
397 } | 418 } |
398 inline bool operator==(const iterator& that) { | 419 |
399 return this->ptr_ == that.ptr_; | 420 private: |
400 } | 421 friend class BytecodeIterator; |
401 inline bool operator!=(const iterator& that) { | 422 opcode_iterator(const byte* ptr, const byte* end) |
402 return this->ptr_ != that.ptr_; | 423 : iterator_base(ptr, end) {} |
| 424 }; |
| 425 // If one wants to iterate over the instruction offsets without looking at |
| 426 // opcodes. |
| 427 class offset_iterator |
| 428 : public iterator_base, |
| 429 public std::iterator<std::input_iterator_tag, uint32_t> { |
| 430 public: |
| 431 inline uint32_t operator*() { |
| 432 DCHECK_LT(ptr_, end_); |
| 433 return static_cast<uint32_t>(ptr_ - start_); |
403 } | 434 } |
404 | 435 |
405 private: | 436 private: |
| 437 const byte* start_; |
406 friend class BytecodeIterator; | 438 friend class BytecodeIterator; |
407 const byte* ptr_; | 439 offset_iterator(const byte* start, const byte* ptr, const byte* end) |
408 const byte* end_; | 440 : iterator_base(ptr, end), start_(start) {} |
409 iterator(const byte* ptr, const byte* end) : ptr_(ptr), end_(end) {} | |
410 }; | 441 }; |
411 | 442 |
412 // Create a new {BytecodeIterator}. If the {decls} pointer is non-null, | 443 // Create a new {BytecodeIterator}. If the {decls} pointer is non-null, |
413 // assume the bytecode starts with local declarations and decode them. | 444 // assume the bytecode starts with local declarations and decode them. |
414 // Otherwise, do not decode local decls. | 445 // Otherwise, do not decode local decls. |
415 BytecodeIterator(const byte* start, const byte* end, | 446 BytecodeIterator(const byte* start, const byte* end, |
416 AstLocalDecls* decls = nullptr); | 447 AstLocalDecls* decls = nullptr); |
417 | 448 |
418 inline iterator begin() const { return iterator(pc_, end_); } | 449 base::iterator_range<opcode_iterator> opcodes() { |
419 inline iterator end() const { return iterator(end_, end_); } | 450 return base::iterator_range<opcode_iterator>(opcode_iterator(pc_, end_), |
| 451 opcode_iterator(end_, end_)); |
| 452 } |
| 453 |
| 454 base::iterator_range<offset_iterator> offsets() { |
| 455 return base::iterator_range<offset_iterator>( |
| 456 offset_iterator(start_, pc_, end_), |
| 457 offset_iterator(start_, end_, end_)); |
| 458 } |
420 | 459 |
421 WasmOpcode current() { | 460 WasmOpcode current() { |
422 return static_cast<WasmOpcode>( | 461 return static_cast<WasmOpcode>( |
423 checked_read_u8(pc_, 0, "expected bytecode")); | 462 checked_read_u8(pc_, 0, "expected bytecode")); |
424 } | 463 } |
425 | 464 |
426 void next() { | 465 void next() { |
427 if (pc_ < end_) { | 466 if (pc_ < end_) { |
428 pc_ += OpcodeLength(pc_, end_); | 467 pc_ += OpcodeLength(pc_, end_); |
429 if (pc_ >= end_) pc_ = end_; | 468 if (pc_ >= end_) pc_ = end_; |
430 } | 469 } |
431 } | 470 } |
432 | 471 |
433 bool has_next() { return pc_ < end_; } | 472 bool has_next() { return pc_ < end_; } |
434 }; | 473 }; |
435 | 474 |
436 } // namespace wasm | 475 } // namespace wasm |
437 } // namespace internal | 476 } // namespace internal |
438 } // namespace v8 | 477 } // namespace v8 |
439 | 478 |
440 #endif // V8_WASM_AST_DECODER_H_ | 479 #endif // V8_WASM_AST_DECODER_H_ |
OLD | NEW |