1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "cfi_generator.h"
16 #if TARGAARCH64
17 #include "aarch64_cfi_generator.h"
18 #include "aarch64_cgfunc.h"
19 #endif
20
21 namespace maplebe {
FindStackDefInsn(BB & bb) const22 Insn *GenCfi::FindStackDefInsn(BB &bb) const
23 {
24 FOR_BB_INSNS(insn, &bb)
25 {
26 if (insn->IsStackDef()) {
27 return insn;
28 }
29 }
30 return nullptr;
31 }
32
FinsStackRevertInsn(BB & bb) const33 Insn *GenCfi::FinsStackRevertInsn(BB &bb) const
34 {
35 FOR_BB_INSNS_REV(insn, &bb)
36 {
37 if (insn->IsStackRevert()) {
38 return insn;
39 }
40 }
41 return nullptr;
42 }
43
InsertCFIDefCfaOffset(BB & bb,Insn & insn,int32 & cfiOffset)44 Insn *GenCfi::InsertCFIDefCfaOffset(BB &bb, Insn &insn, int32 &cfiOffset)
45 {
46 cfiOffset = AddtoOffsetFromCFA(cfiOffset);
47 Insn &cfiInsn = cgFunc.GetInsnBuilder()
48 ->BuildCfiInsn(cfi::OP_CFI_def_cfa_offset)
49 .AddOpndChain(cgFunc.CreateCfiImmOperand(cfiOffset, k64BitSize));
50 (void)bb.InsertInsnAfter(insn, cfiInsn);
51 return &cfiInsn;
52 }
53
GenerateStartDirective(BB & bb)54 void GenCfi::GenerateStartDirective(BB &bb)
55 {
56 Insn &startProcInsn = cgFunc.GetInsnBuilder()->BuildCfiInsn(cfi::OP_CFI_startproc);
57 if (bb.GetFirstInsn() != nullptr) {
58 (void)bb.InsertInsnBefore(*bb.GetFirstInsn(), startProcInsn);
59 } else {
60 bb.AppendInsn(startProcInsn);
61 }
62 }
63
GenerateEndDirective(BB & bb)64 void GenCfi::GenerateEndDirective(BB &bb)
65 {
66 bb.AppendInsn(cgFunc.GetInsnBuilder()->BuildCfiInsn(cfi::OP_CFI_endproc));
67 }
68
GenerateRegisterStateDirective(BB & bb)69 void GenCfi::GenerateRegisterStateDirective(BB &bb)
70 {
71 if (&bb == cgFunc.GetLastBB() || bb.GetNext() == nullptr) {
72 return;
73 }
74
75 BB *nextBB = bb.GetNext();
76 do {
77 if (nextBB == cgFunc.GetLastBB() || !nextBB->IsEmpty()) {
78 break;
79 }
80 nextBB = nextBB->GetNext();
81 } while (nextBB != nullptr);
82
83 if (nextBB != nullptr && !nextBB->IsEmpty()) {
84 bb.InsertInsnBegin(cgFunc.GetInsnBuilder()->BuildCfiInsn(cfi::OP_CFI_remember_state));
85 nextBB->InsertInsnBegin(cgFunc.GetInsnBuilder()->BuildCfiInsn(cfi::OP_CFI_restore_state));
86 }
87 }
88
Run()89 void GenCfi::Run()
90 {
91 auto *startBB = cgFunc.GetFirstBB();
92 GenerateStartDirective(*startBB);
93
94 if (cgFunc.GetHasProEpilogue()) {
95 FOR_ALL_BB(bb, &cgFunc)
96 {
97 auto *stackDefInsn = FindStackDefInsn(*bb);
98 if (stackDefInsn != nullptr) {
99 GenerateRegisterSaveDirective(*bb, *stackDefInsn);
100 }
101 auto *stackRevertInsn = FinsStackRevertInsn(*bb);
102 if (stackRevertInsn != nullptr) {
103 GenerateRegisterStateDirective(*bb);
104 GenerateRegisterRestoreDirective(*bb, *stackRevertInsn);
105 }
106 }
107 }
108
109 GenerateEndDirective(*(cgFunc.GetLastBB()));
110 if (cgFunc.GetLastBB()->IsUnreachable()) {
111 cgFunc.SetExitBBLost(true);
112 }
113 }
114
PhaseRun(maplebe::CGFunc & f)115 bool CgGenCfi::PhaseRun(maplebe::CGFunc &f)
116 {
117 GenCfi *genCfi = GetPhaseAllocator()->New<AArch64GenCfi>(f);
118 genCfi->Run();
119 return true;
120 }
121 MAPLE_TRANSFORM_PHASE_REGISTER(CgGenCfi, gencfi)
122 } /* namespace maplebe */
123