1// Copyright 2016 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/compiler/backend/arm64/unwinding-info-writer-arm64.h"
6#include "src/compiler/backend/instruction.h"
7
8namespace v8 {
9namespace internal {
10namespace compiler {
11
12// TODO(v8:10026): When using CFI, we need to generate unwinding info to tell
13// the unwinder that return addresses are signed.
14
15void UnwindingInfoWriter::BeginInstructionBlock(int pc_offset,
16                                                const InstructionBlock* block) {
17  if (!enabled()) return;
18
19  block_will_exit_ = false;
20
21  DCHECK_LT(block->rpo_number().ToInt(),
22            static_cast<int>(block_initial_states_.size()));
23  const BlockInitialState* initial_state =
24      block_initial_states_[block->rpo_number().ToInt()];
25  if (!initial_state) return;
26  if (initial_state->saved_lr_ != saved_lr_) {
27    eh_frame_writer_.AdvanceLocation(pc_offset);
28    if (initial_state->saved_lr_) {
29      eh_frame_writer_.RecordRegisterSavedToStack(lr, kSystemPointerSize);
30      eh_frame_writer_.RecordRegisterSavedToStack(fp, 0);
31    } else {
32      eh_frame_writer_.RecordRegisterFollowsInitialRule(lr);
33    }
34    saved_lr_ = initial_state->saved_lr_;
35  }
36}
37
38void UnwindingInfoWriter::EndInstructionBlock(const InstructionBlock* block) {
39  if (!enabled() || block_will_exit_) return;
40
41  for (const RpoNumber& successor : block->successors()) {
42    int successor_index = successor.ToInt();
43    DCHECK_LT(successor_index, static_cast<int>(block_initial_states_.size()));
44    const BlockInitialState* existing_state =
45        block_initial_states_[successor_index];
46
47    // If we already had an entry for this BB, check that the values are the
48    // same we are trying to insert.
49    if (existing_state) {
50      DCHECK_EQ(existing_state->saved_lr_, saved_lr_);
51    } else {
52      block_initial_states_[successor_index] =
53          zone_->New<BlockInitialState>(saved_lr_);
54    }
55  }
56}
57
58void UnwindingInfoWriter::MarkFrameConstructed(int at_pc) {
59  if (!enabled()) return;
60
61  // Regardless of the type of frame constructed, the relevant part of the
62  // layout is always the one in the diagram:
63  //
64  // |   ....   |         higher addresses
65  // +----------+               ^
66  // |    LR    |               |            |
67  // +----------+               |            |
68  // | saved FP |               |            |
69  // +----------+ <-- FP                     v
70  // |   ....   |                       stack growth
71  //
72  // The LR is pushed on the stack, and we can record this fact at the end of
73  // the construction, since the LR itself is not modified in the process.
74  eh_frame_writer_.AdvanceLocation(at_pc);
75  eh_frame_writer_.RecordRegisterSavedToStack(lr, kSystemPointerSize);
76  eh_frame_writer_.RecordRegisterSavedToStack(fp, 0);
77  saved_lr_ = true;
78}
79
80void UnwindingInfoWriter::MarkFrameDeconstructed(int at_pc) {
81  if (!enabled()) return;
82
83  // The lr is restored by the last operation in LeaveFrame().
84  eh_frame_writer_.AdvanceLocation(at_pc);
85  eh_frame_writer_.RecordRegisterFollowsInitialRule(lr);
86  saved_lr_ = false;
87}
88
89void UnwindingInfoWriter::MarkLinkRegisterOnTopOfStack(int pc_offset,
90                                                       const Register& sp) {
91  if (!enabled()) return;
92
93  eh_frame_writer_.AdvanceLocation(pc_offset);
94  eh_frame_writer_.SetBaseAddressRegisterAndOffset(sp, 0);
95  eh_frame_writer_.RecordRegisterSavedToStack(lr, 0);
96}
97
98void UnwindingInfoWriter::MarkPopLinkRegisterFromTopOfStack(int pc_offset) {
99  if (!enabled()) return;
100
101  eh_frame_writer_.AdvanceLocation(pc_offset);
102  eh_frame_writer_.SetBaseAddressRegisterAndOffset(fp, 0);
103  eh_frame_writer_.RecordRegisterFollowsInitialRule(lr);
104}
105
106}  // namespace compiler
107}  // namespace internal
108}  // namespace v8
109