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 #include "cgfunc.h"
17 #if TARGAARCH64
18 #include "aarch64_cfi_generator.h"
19 #include "aarch64_cgfunc.h"
20 #endif
21
22 namespace maplebe {
FindStackDefInsn(BB & bb) const23 Insn *GenCfi::FindStackDefInsn(BB &bb) const
24 {
25 FOR_BB_INSNS(insn, &bb)
26 {
27 if (insn->IsStackDef()) {
28 return insn;
29 }
30 }
31 return nullptr;
32 }
33
FinsStackRevertInsn(BB & bb) const34 Insn *GenCfi::FinsStackRevertInsn(BB &bb) const
35 {
36 FOR_BB_INSNS_REV(insn, &bb)
37 {
38 if (insn->IsStackRevert()) {
39 return insn;
40 }
41 }
42 return nullptr;
43 }
44
InsertCFIDefCfaOffset(BB & bb,Insn & insn,int32 & cfiOffset)45 Insn *GenCfi::InsertCFIDefCfaOffset(BB &bb, Insn &insn, int32 &cfiOffset)
46 {
47 cfiOffset = AddtoOffsetFromCFA(cfiOffset);
48 Insn &cfiInsn = cgFunc.GetInsnBuilder()
49 ->BuildCfiInsn(cfi::OP_CFI_def_cfa_offset)
50 .AddOpndChain(cgFunc.CreateCfiImmOperand(cfiOffset, k64BitSize));
51 (void)bb.InsertInsnAfter(insn, cfiInsn);
52 cgFunc.SetDbgCallFrameOffset(cfiOffset);
53 return &cfiInsn;
54 }
55
GenerateStartDirective(BB & bb)56 void GenCfi::GenerateStartDirective(BB &bb)
57 {
58 Insn &startProcInsn = cgFunc.GetInsnBuilder()->BuildCfiInsn(cfi::OP_CFI_startproc);
59 if (bb.GetFirstInsn() != nullptr) {
60 (void)bb.InsertInsnBefore(*bb.GetFirstInsn(), startProcInsn);
61 } else {
62 bb.AppendInsn(startProcInsn);
63 }
64 }
65
GenerateEndDirective(BB & bb)66 void GenCfi::GenerateEndDirective(BB &bb)
67 {
68 bb.AppendInsn(cgFunc.GetInsnBuilder()->BuildCfiInsn(cfi::OP_CFI_endproc));
69 }
70
GenerateRegisterStateDirective(BB & bb)71 void GenCfi::GenerateRegisterStateDirective(BB &bb)
72 {
73 if (cg.GetMIRModule()->IsCModule()) {
74 return;
75 }
76
77 if (&bb == cgFunc.GetLastBB() || bb.GetNext() == nullptr) {
78 return;
79 }
80
81 BB *nextBB = bb.GetNext();
82 do {
83 if (nextBB == cgFunc.GetLastBB() || !nextBB->IsEmpty()) {
84 break;
85 }
86 nextBB = nextBB->GetNext();
87 } while (nextBB != nullptr);
88
89 if (nextBB != nullptr && !nextBB->IsEmpty()) {
90 bb.InsertInsnBegin(cgFunc.GetInsnBuilder()->BuildCfiInsn(cfi::OP_CFI_remember_state));
91 nextBB->InsertInsnBegin(cgFunc.GetInsnBuilder()->BuildCfiInsn(cfi::OP_CFI_restore_state));
92 }
93 }
94
InsertFirstLocation(BB & bb)95 void GenCfi::InsertFirstLocation(BB &bb)
96 {
97 MIRSymbol *fSym = GlobalTables::GetGsymTable().GetSymbolFromStidx(cgFunc.GetFunction().GetStIdx().Idx());
98
99 if (fSym == nullptr || !cg.GetCGOptions().WithLoc() || !cg.GetMIRModule()->IsCModule() ||
100 (fSym->GetSrcPosition().FileNum() == 0)) {
101 return;
102 }
103
104 uint32 fileNum = fSym->GetSrcPosition().FileNum();
105 uint32 lineNum = fSym->GetSrcPosition().LineNum();
106 uint32 columnNum = fSym->GetSrcPosition().Column();
107 (void)(bb.InsertInsnBefore(*bb.GetFirstInsn(), cgFunc.BuildLocInsn(fileNum, lineNum, columnNum)));
108 }
109
Run()110 void GenCfi::Run()
111 {
112 auto *startBB = cgFunc.GetFirstBB();
113 GenerateStartDirective(*startBB);
114 InsertFirstLocation(*startBB);
115
116 if (cgFunc.GetHasProEpilogue()) {
117 FOR_ALL_BB(bb, &cgFunc)
118 {
119 auto *stackDefInsn = FindStackDefInsn(*bb);
120 if (stackDefInsn != nullptr) {
121 GenerateRegisterSaveDirective(*bb, *stackDefInsn);
122 }
123 auto *stackRevertInsn = FinsStackRevertInsn(*bb);
124 if (stackRevertInsn != nullptr) {
125 GenerateRegisterStateDirective(*bb);
126 GenerateRegisterRestoreDirective(*bb, *stackRevertInsn);
127 }
128 }
129 }
130
131 GenerateEndDirective(*(cgFunc.GetLastBB()));
132 if (cgFunc.GetLastBB()->IsUnreachable()) {
133 cgFunc.SetExitBBLost(true);
134 }
135 }
136
PhaseRun(maplebe::CGFunc & f)137 bool CgGenCfi::PhaseRun(maplebe::CGFunc &f)
138 {
139 GenCfi *genCfi = GetPhaseAllocator()->New<AArch64GenCfi>(f);
140 genCfi->Run();
141 return true;
142 }
143 MAPLE_TRANSFORM_PHASE_REGISTER(CgGenCfi, gencfi)
144 } /* namespace maplebe */
145