• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "proepilog.h"
17 #if TARGAARCH64
18 #include "aarch64_proepilog.h"
19 #elif TARGRISCV64
20 #include "riscv64_proepilog.h"
21 #endif
22 #if TARGARM32
23 #include "arm32_proepilog.h"
24 #endif
25 #if TARGX86_64
26 #include "x64_proepilog.h"
27 #endif
28 #include "cgfunc.h"
29 #include "cg.h"
30 
31 namespace maplebe {
32 using namespace maple;
33 
InsertCFIDefCfaOffset(int32 & cfiOffset,Insn & insertAfter)34 Insn *GenProEpilog::InsertCFIDefCfaOffset(int32 &cfiOffset, Insn &insertAfter)
35 {
36     if (!cgFunc.GenCfi()) {
37         return &insertAfter;
38     }
39     cfiOffset = AddtoOffsetFromCFA(cfiOffset);
40     Insn &cfiInsn = cgFunc.GetInsnBuilder()
41                         ->BuildCfiInsn(cfi::OP_CFI_def_cfa_offset)
42                         .AddOpndChain(cgFunc.CreateCfiImmOperand(cfiOffset, k64BitSize));
43     Insn *newIPoint = cgFunc.GetCurBB()->InsertInsnAfter(insertAfter, cfiInsn);
44     cgFunc.SetDbgCallFrameOffset(cfiOffset);
45     return newIPoint;
46 }
47 
48 /* there are two stack protector:
49  * 1. stack protector all: for all function
50  * 2. stack protector strong: for some functon that
51  *   <1> invoke alloca functon;
52  *   <2> use stack address;
53  *   <3> callee use return stack slot;
54  *   <4> local symbol is vector type;
55  * */
NeedStackProtect()56 void GenProEpilog::NeedStackProtect()
57 {
58     DEBUG_ASSERT(stackProtect == false, "no stack protect default");
59     CG *currCG = cgFunc.GetCG();
60     if (currCG->IsStackProtectorAll()) {
61         stackProtect = true;
62         return;
63     }
64 
65     if (!currCG->IsStackProtectorStrong()) {
66         return;
67     }
68 
69     if (cgFunc.HasAlloca()) {
70         stackProtect = true;
71         return;
72     }
73 
74     /* check if function use stack address or callee function return stack slot */
75     auto stackProtectInfo = cgFunc.GetStackProtectInfo();
76     if ((stackProtectInfo & kAddrofStack) != 0 || (stackProtectInfo & kRetureStackSlot) != 0) {
77         stackProtect = true;
78         return;
79     }
80 
81     /* check if local symbol is vector type */
82     auto &mirFunction = cgFunc.GetFunction();
83     uint32 symTabSize = static_cast<uint32>(mirFunction.GetSymTab()->GetSymbolTableSize());
84     for (uint32 i = 0; i < symTabSize; ++i) {
85         MIRSymbol *symbol = mirFunction.GetSymTab()->GetSymbolFromStIdx(i);
86         if (symbol == nullptr || symbol->GetStorageClass() != kScAuto || symbol->IsDeleted()) {
87             continue;
88         }
89         TyIdx tyIdx = symbol->GetTyIdx();
90         MIRType *type = GlobalTables::GetTypeTable().GetTypeFromTyIdx(tyIdx);
91         if (type->GetKind() == kTypeArray) {
92             stackProtect = true;
93             return;
94         }
95 
96         if (type->IsStructType() && IncludeArray(*type)) {
97             stackProtect = true;
98             return;
99         }
100     }
101 }
102 
IncludeArray(const MIRType & type) const103 bool GenProEpilog::IncludeArray(const MIRType &type) const
104 {
105     DEBUG_ASSERT(type.IsStructType(), "agg must be one of class/struct/union");
106     auto &structType = static_cast<const MIRStructType &>(type);
107     /* all elements of struct. */
108     auto num = static_cast<uint8>(structType.GetFieldsSize());
109     for (uint32 i = 0; i < num; ++i) {
110         MIRType *elemType = structType.GetElemType(i);
111         if (elemType->GetKind() == kTypeArray) {
112             return true;
113         }
114         if (elemType->IsStructType() && IncludeArray(*elemType)) {
115             return true;
116         }
117     }
118     return false;
119 }
120 
PhaseRun(maplebe::CGFunc & f)121 bool CgGenProEpiLog::PhaseRun(maplebe::CGFunc &f)
122 {
123     GenProEpilog *genPE = nullptr;
124 #if TARGAARCH64 || TARGRISCV64
125     genPE = GetPhaseAllocator()->New<AArch64GenProEpilog>(f, *ApplyTempMemPool());
126 #endif
127 #if TARGARM32
128     genPE = GetPhaseAllocator()->New<Arm32GenProEpilog>(f);
129 #endif
130 #if TARGX86_64
131     genPE = GetPhaseAllocator()->New<X64GenProEpilog>(f);
132 #endif
133     genPE->Run();
134     return false;
135 }
136 } /* namespace maplebe */
137