1// Copyright 2021 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "src/baseline/bytecode-offset-iterator.h"
6
7#include "src/objects/code-inl.h"
8
9namespace v8 {
10namespace internal {
11namespace baseline {
12
13BytecodeOffsetIterator::BytecodeOffsetIterator(Handle<ByteArray> mapping_table,
14                                               Handle<BytecodeArray> bytecodes)
15    : mapping_table_(mapping_table),
16      data_start_address_(mapping_table_->GetDataStartAddress()),
17      data_length_(mapping_table_->length()),
18      current_index_(0),
19      bytecode_iterator_(bytecodes),
20      local_heap_(LocalHeap::Current()
21                      ? LocalHeap::Current()
22                      : Isolate::Current()->main_thread_local_heap()) {
23  local_heap_->AddGCEpilogueCallback(UpdatePointersCallback, this);
24  Initialize();
25}
26
27BytecodeOffsetIterator::BytecodeOffsetIterator(ByteArray mapping_table,
28                                               BytecodeArray bytecodes)
29    : data_start_address_(mapping_table.GetDataStartAddress()),
30      data_length_(mapping_table.length()),
31      current_index_(0),
32      bytecode_handle_storage_(bytecodes),
33      // In the non-handlified version, no GC is allowed. We use a "dummy"
34      // handle to pass the BytecodeArray to the BytecodeArrayIterator, which
35      // is fine since no objects will be moved.
36      bytecode_iterator_(Handle<BytecodeArray>(
37          reinterpret_cast<Address*>(&bytecode_handle_storage_))),
38      local_heap_(nullptr) {
39  no_gc_.emplace();
40  Initialize();
41}
42
43BytecodeOffsetIterator::~BytecodeOffsetIterator() {
44  if (local_heap_ != nullptr) {
45    local_heap_->RemoveGCEpilogueCallback(UpdatePointersCallback, this);
46  }
47}
48
49void BytecodeOffsetIterator::Initialize() {
50  // Initialize values for the prologue.
51  // The first recorded position is at the start of the first bytecode.
52  current_pc_start_offset_ = 0;
53  current_pc_end_offset_ = ReadPosition();
54  current_bytecode_offset_ = kFunctionEntryBytecodeOffset;
55}
56
57void BytecodeOffsetIterator::UpdatePointers() {
58  DisallowGarbageCollection no_gc;
59  DCHECK(!mapping_table_.is_null());
60  data_start_address_ = mapping_table_->GetDataStartAddress();
61}
62
63}  // namespace baseline
64}  // namespace internal
65}  // namespace v8
66