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 "src/signature.h" | 8 #include "src/signature.h" |
9 #include "src/wasm/decoder.h" | 9 #include "src/wasm/decoder.h" |
10 #include "src/wasm/wasm-opcodes.h" | 10 #include "src/wasm/wasm-opcodes.h" |
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 bool DecodeLocalDecls(AstLocalDecls& decls, const byte* start, const byte* end); | 274 bool DecodeLocalDecls(AstLocalDecls& decls, const byte* start, const byte* end); |
275 BitVector* AnalyzeLoopAssignmentForTesting(Zone* zone, size_t num_locals, | 275 BitVector* AnalyzeLoopAssignmentForTesting(Zone* zone, size_t num_locals, |
276 const byte* start, const byte* end); | 276 const byte* start, const byte* end); |
277 | 277 |
278 // Computes the length of the opcode at the given address. | 278 // Computes the length of the opcode at the given address. |
279 unsigned OpcodeLength(const byte* pc, const byte* end); | 279 unsigned OpcodeLength(const byte* pc, const byte* end); |
280 | 280 |
281 // Computes the arity (number of sub-nodes) of the opcode at the given address. | 281 // Computes the arity (number of sub-nodes) of the opcode at the given address. |
282 unsigned OpcodeArity(const byte* pc, const byte* end); | 282 unsigned OpcodeArity(const byte* pc, const byte* end); |
283 | 283 |
| 284 // A simple forward iterator for bytecodes. |
| 285 class BytecodeIterator : public Decoder { |
| 286 public: |
| 287 // If one wants to iterate over the bytecode without looking at {pc_offset()}. |
| 288 class iterator { |
| 289 public: |
| 290 inline iterator& operator++() { |
| 291 DCHECK_LT(ptr_, end_); |
| 292 ptr_ += OpcodeLength(ptr_, end_); |
| 293 return *this; |
| 294 } |
| 295 inline WasmOpcode operator*() { |
| 296 DCHECK_LT(ptr_, end_); |
| 297 return static_cast<WasmOpcode>(*ptr_); |
| 298 } |
| 299 inline bool operator==(const iterator& that) { |
| 300 return this->ptr_ == that.ptr_; |
| 301 } |
| 302 inline bool operator!=(const iterator& that) { |
| 303 return this->ptr_ != that.ptr_; |
| 304 } |
| 305 |
| 306 private: |
| 307 friend class BytecodeIterator; |
| 308 const byte* ptr_; |
| 309 const byte* end_; |
| 310 iterator(const byte* ptr, const byte* end) : ptr_(ptr), end_(end) {} |
| 311 }; |
| 312 |
| 313 // Create a new {BytecodeIterator}. If the {decls} pointer is non-null, |
| 314 // assume the bytecode starts with local declarations and decode them. |
| 315 // Otherwise, do not decode local decls. |
| 316 BytecodeIterator(const byte* start, const byte* end, |
| 317 AstLocalDecls* decls = nullptr); |
| 318 |
| 319 inline iterator begin() const { return iterator(pc_, end_); } |
| 320 inline iterator end() const { return iterator(end_, end_); } |
| 321 |
| 322 WasmOpcode current() { |
| 323 return static_cast<WasmOpcode>( |
| 324 checked_read_u8(pc_, 0, "expected bytecode")); |
| 325 } |
| 326 |
| 327 void next() { |
| 328 if (pc_ < end_) { |
| 329 pc_ += OpcodeLength(pc_, end_); |
| 330 if (pc_ >= end_) pc_ = end_; |
| 331 } |
| 332 } |
| 333 |
| 334 bool has_next() { return pc_ < end_; } |
| 335 }; |
| 336 |
284 } // namespace wasm | 337 } // namespace wasm |
285 } // namespace internal | 338 } // namespace internal |
286 } // namespace v8 | 339 } // namespace v8 |
287 | 340 |
288 #endif // V8_WASM_AST_DECODER_H_ | 341 #endif // V8_WASM_AST_DECODER_H_ |
OLD | NEW |