Commit f91178e8 authored by leszeks's avatar leszeks Committed by Commit bot

[ignition] Add a reverse bytecode iterator

This pre-calculates and stores a vector of bytecode offsets, and then allows
one to iterate over it backwards. This could probably be adapted to a
bidirectional/random access iterator if we wanted to, but for now reverse
is all we need.

Review-Url: https://codereview.chromium.org/2518003002
Cr-Commit-Position: refs/heads/master@{#41153}
parent ae8a77ea
......@@ -1446,6 +1446,8 @@ v8_source_set("v8_base") {
"src/interpreter/bytecode-array-builder.h",
"src/interpreter/bytecode-array-iterator.cc",
"src/interpreter/bytecode-array-iterator.h",
"src/interpreter/bytecode-array-reverse-iterator.cc",
"src/interpreter/bytecode-array-reverse-iterator.h",
"src/interpreter/bytecode-array-writer.cc",
"src/interpreter/bytecode-array-writer.h",
"src/interpreter/bytecode-dead-code-optimizer.cc",
......
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/interpreter/bytecode-array-reverse-iterator.h"
#include "src/objects-inl.h"
namespace v8 {
namespace internal {
namespace interpreter {
BytecodeArrayReverseIterator::BytecodeArrayReverseIterator(
Handle<BytecodeArray> bytecode_array, Zone* zone)
: BytecodeArrayAccessor(bytecode_array, 0), offsets_(zone) {
// Run forwards through the bytecode array to determine the offset of each
// bytecode.
while (current_offset() < bytecode_array->length()) {
offsets_.push_back(current_offset());
SetOffset(current_offset() + current_bytecode_size());
}
Reset();
}
void BytecodeArrayReverseIterator::Advance() {
it_offsets_++;
UpdateOffsetFromIterator();
}
void BytecodeArrayReverseIterator::Reset() {
it_offsets_ = offsets_.rbegin();
UpdateOffsetFromIterator();
}
bool BytecodeArrayReverseIterator::done() const {
return it_offsets_ == offsets_.rend();
}
void BytecodeArrayReverseIterator::UpdateOffsetFromIterator() {
if (it_offsets_ != offsets_.rend()) {
SetOffset(*it_offsets_);
}
}
} // namespace interpreter
} // namespace internal
} // namespace v8
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_INTERPRETER_BYTECODE_ARRAY_REVERSE_ITERATOR_H_
#define V8_INTERPRETER_BYTECODE_ARRAY_REVERSE_ITERATOR_H_
#include "src/interpreter/bytecode-array-accessor.h"
#include "src/zone/zone-containers.h"
#include "src/zone/zone.h"
namespace v8 {
namespace internal {
namespace interpreter {
class V8_EXPORT_PRIVATE BytecodeArrayReverseIterator final
: public BytecodeArrayAccessor {
public:
explicit BytecodeArrayReverseIterator(Handle<BytecodeArray> bytecode_array,
Zone* zone);
void Advance();
void Reset();
bool done() const;
private:
ZoneVector<int> offsets_;
ZoneVector<int>::const_reverse_iterator it_offsets_;
void UpdateOffsetFromIterator();
DISALLOW_COPY_AND_ASSIGN(BytecodeArrayReverseIterator);
};
} // namespace interpreter
} // namespace internal
} // namespace v8
#endif // V8_INTERPRETER_BYTECODE_ARRAY_REVERSE_ITERATOR_H_
......@@ -982,6 +982,8 @@
'interpreter/bytecode-array-builder.h',
'interpreter/bytecode-array-iterator.cc',
'interpreter/bytecode-array-iterator.h',
'interpreter/bytecode-array-reverse-iterator.cc',
'interpreter/bytecode-array-reverse-iterator.h',
'interpreter/bytecode-array-writer.cc',
'interpreter/bytecode-array-writer.h',
'interpreter/bytecode-dead-code-optimizer.cc',
......
......@@ -98,6 +98,7 @@ v8_executable("unittests") {
"heap/slot-set-unittest.cc",
"interpreter/bytecode-array-builder-unittest.cc",
"interpreter/bytecode-array-iterator-unittest.cc",
"interpreter/bytecode-array-reverse-iterator-unittest.cc",
"interpreter/bytecode-array-writer-unittest.cc",
"interpreter/bytecode-dead-code-optimizer-unittest.cc",
"interpreter/bytecode-decoder-unittest.cc",
......
......@@ -89,6 +89,7 @@
'interpreter/bytecodes-unittest.cc',
'interpreter/bytecode-array-builder-unittest.cc',
'interpreter/bytecode-array-iterator-unittest.cc',
'interpreter/bytecode-array-reverse-iterator-unittest.cc',
'interpreter/bytecode-array-writer-unittest.cc',
'interpreter/bytecode-dead-code-optimizer-unittest.cc',
'interpreter/bytecode-decoder-unittest.cc',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment