LLVM 22.0.0git
DWARFCFIState.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
12#include "llvm/MC/MCDwarf.h"
13#include "llvm/Support/Error.h"
16#include <cassert>
17#include <optional>
18
19using namespace llvm;
20
21std::optional<dwarf::UnwindRow> DWARFCFIState::getCurrentUnwindRow() const {
22 if (!IsInitiated)
23 return std::nullopt;
24 return Row;
25}
26
28 auto CFIP = convert(Directive);
29
30 // This is a copy of the current row, its value will be updated by
31 // `parseRows`.
32 dwarf::UnwindRow NewRow = Row;
33
34 // `parseRows` updates the current row by applying the `CFIProgram` to it.
35 // During this process, it may create multiple rows preceding the newly
36 // updated row and following the previous rows. These middle rows are stored
37 // in `PrecedingRows`. For now, there is no need to store these rows in the
38 // state, so they are ignored in the end.
40
41 // TODO: `.cfi_remember_state` and `.cfi_restore_state` directives are not
42 // supported yet. The reason is that `parseRows` expects the stack of states
43 // to be produced and used in a single `CFIProgram`. However, in this use
44 // case, each instruction creates its own `CFIProgram`, which means the stack
45 // of states is forgotten between instructions. To fix it, `parseRows` should
46 // be refactored to read the current stack of states from the argument and
47 // update it based on the `CFIProgram.`
48 if (Error Err = parseRows(CFIP, NewRow, nullptr).takeError()) {
49 Context->reportError(
50 Directive.getLoc(),
51 formatv("could not parse this CFI directive due to: {0}",
52 toString(std::move(Err))));
53
54 // Proceed the analysis by ignoring this CFI directive.
55 return;
56 }
57
58 Row = NewRow;
59 IsInitiated = true;
60}
61
62dwarf::CFIProgram DWARFCFIState::convert(MCCFIInstruction Directive) {
63 auto CFIP = dwarf::CFIProgram(
64 /* CodeAlignmentFactor */ 1, /* DataAlignmentFactor */ 1,
65 Context->getTargetTriple().getArch());
66
67 auto MaybeCurrentRow = getCurrentUnwindRow();
68 switch (Directive.getOperation()) {
70 CFIP.addInstruction(dwarf::DW_CFA_same_value, Directive.getRegister());
71 break;
73 // TODO: remember state is not supported yet, the following line does not
74 // work:
75 // CFIP.addInstruction(dwarf::DW_CFA_remember_state);
76 // The reason is explained in the `DWARFCFIState::update` method where
77 // `dwarf::parseRows` is used.
78 Context->reportWarning(Directive.getLoc(),
79 "this directive is not supported, ignoring it");
80 break;
82 // TODO: restore state is not supported yet, the following line does not
83 // work:
84 // CFIP.addInstruction(dwarf::DW_CFA_restore_state);
85 // The reason is explained in the `DWARFCFIState::update` method where
86 // `dwarf::parseRows` is used.
87 Context->reportWarning(Directive.getLoc(),
88 "this directive is not supported, ignoring it");
89 break;
91 CFIP.addInstruction(dwarf::DW_CFA_offset, Directive.getRegister(),
92 Directive.getOffset());
93 break;
95 CFIP.addInstruction(dwarf::DW_CFA_LLVM_def_aspace_cfa,
96 Directive.getRegister());
97 break;
99 CFIP.addInstruction(dwarf::DW_CFA_def_cfa_register,
100 Directive.getRegister());
101 break;
103 CFIP.addInstruction(dwarf::DW_CFA_def_cfa_offset, Directive.getOffset());
104 break;
106 CFIP.addInstruction(dwarf::DW_CFA_def_cfa, Directive.getRegister(),
107 Directive.getOffset());
108 break;
110 assert(
111 IsInitiated &&
112 "cannot define relative offset to a non-existing CFA unwinding rule");
113
114 CFIP.addInstruction(dwarf::DW_CFA_offset, Directive.getRegister(),
115 Directive.getOffset() - Row.getCFAValue().getOffset());
116 break;
118 assert(IsInitiated &&
119 "cannot adjust CFA offset of a non-existing CFA unwinding rule");
120
121 CFIP.addInstruction(dwarf::DW_CFA_def_cfa_offset,
122 Directive.getOffset() + Row.getCFAValue().getOffset());
123 break;
125 // TODO: DWARFExpressions are not supported yet, ignoring expression here.
126 Context->reportWarning(Directive.getLoc(),
127 "this directive is not supported, ignoring it");
128 break;
130 // The `.cfi_restore register` directive restores the register's unwinding
131 // information to its CIE value. However, assemblers decide where CIE ends
132 // and the FDE starts, so the functionality of this directive depends on the
133 // assembler's decision and cannot be validated.
134 Context->reportWarning(
135 Directive.getLoc(),
136 "this directive behavior depends on the assembler, ignoring it");
137 break;
139 CFIP.addInstruction(dwarf::DW_CFA_undefined, Directive.getRegister());
140 break;
142 CFIP.addInstruction(dwarf::DW_CFA_register, Directive.getRegister(),
143 Directive.getRegister2());
144 break;
146 CFIP.addInstruction(dwarf::DW_CFA_GNU_window_save);
147 break;
149 CFIP.addInstruction(dwarf::DW_CFA_AARCH64_negate_ra_state);
150 break;
152 CFIP.addInstruction(dwarf::DW_CFA_AARCH64_negate_ra_state_with_pc);
153 break;
155 CFIP.addInstruction(dwarf::DW_CFA_GNU_args_size);
156 break;
158 // `.cfi_label` does not have any functional effect on unwinding process.
159 break;
161 CFIP.addInstruction(dwarf::DW_CFA_val_offset, Directive.getRegister(),
162 Directive.getOffset());
163 break;
164 }
165
166 return CFIP;
167}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file declares DWARFCFIState class.
This file contains constants used for implementing Dwarf debug support.
LLVM_ABI std::optional< dwarf::UnwindRow > getCurrentUnwindRow() const
LLVM_ABI void update(const MCCFIInstruction &Directive)
This method updates the state by applying Directive to the current state.
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Represent a sequence of Call Frame Information instructions that, when read in order,...
A class that represents a single row in the unwind table that is decoded by parsing the DWARF Call Fr...
std::vector< UnwindRow > RowContainer
This is an optimization pass for GlobalISel generic memory operations.
auto formatv(bool Validate, const char *Fmt, Ts &&...Vals)
std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)