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 "cfi.h"
17 #include "emit.h"
18
19 namespace cfi {
20 using maplebe::CG;
21 using maplebe::Emitter;
22 using maplebe::MOperator;
23 using maplebe::Operand;
24 using maplebe::OpndDesc;
25
26 struct CfiDescr {
27 const std::string name;
28 uint32 opndCount;
29 /* create 3 OperandType array to store cfi instruction's operand type */
30 std::array<Operand::OperandType, 3> opndTypes;
31 };
32
33 static CfiDescr cfiDescrTable[kOpCfiLast + 1] = {
34 #define CFI_DEFINE(k, sub, n, o0, o1, o2) {".cfi_" #k, n, {Operand::kOpd##o0, Operand::kOpd##o1, Operand::kOpd##o2}},
35 #define ARM_DIRECTIVES_DEFINE(k, sub, n, o0, o1, o2) \
36 {"." #k, n, {Operand::kOpd##o0, Operand::kOpd##o1, Operand::kOpd##o2}},
37 #include "cfi.def"
38 #undef CFI_DEFINE
39 #undef ARM_DIRECTIVES_DEFINE
40 {".cfi_undef", 0, {Operand::kOpdUndef, Operand::kOpdUndef, Operand::kOpdUndef}}};
41
Dump() const42 void CfiInsn::Dump() const
43 {
44 MOperator mOp = GetMachineOpcode();
45 CfiDescr &cfiDescr = cfiDescrTable[mOp];
46 LogInfo::MapleLogger() << "CFI " << cfiDescr.name;
47 for (uint32 i = 0; i < static_cast<uint32>(cfiDescr.opndCount); ++i) {
48 LogInfo::MapleLogger() << (i == 0 ? " : " : " ");
49 Operand &curOperand = GetOperand(i);
50 curOperand.Dump();
51 }
52 LogInfo::MapleLogger() << "\n";
53 }
54
CheckMD() const55 bool CfiInsn::CheckMD() const
56 {
57 CfiDescr &cfiDescr = cfiDescrTable[GetMachineOpcode()];
58 /* cfi instruction's 3rd /4th/5th operand must be null */
59 for (uint32 i = 0; i < static_cast<uint32>(cfiDescr.opndCount); ++i) {
60 Operand &opnd = GetOperand(i);
61 if (opnd.GetKind() != cfiDescr.opndTypes[i]) {
62 return false;
63 }
64 }
65 return true;
66 }
67
Dump() const68 void RegOperand::Dump() const
69 {
70 LogInfo::MapleLogger() << "reg: " << regNO << "[ size: " << GetSize() << "] ";
71 }
72
Dump() const73 void ImmOperand::Dump() const
74 {
75 LogInfo::MapleLogger() << "imm: " << val << "[ size: " << GetSize() << "] ";
76 }
77
Dump() const78 void StrOperand::Dump() const
79 {
80 LogInfo::MapleLogger() << str;
81 }
82
Dump() const83 void LabelOperand::Dump() const
84 {
85 LogInfo::MapleLogger() << "label:" << labelIndex;
86 }
Visit(RegOperand * v)87 void CFIOpndEmitVisitor::Visit(RegOperand *v)
88 {
89 emitter.Emit(v->GetRegisterNO());
90 }
Visit(ImmOperand * v)91 void CFIOpndEmitVisitor::Visit(ImmOperand *v)
92 {
93 emitter.Emit(v->GetValue());
94 }
Visit(SymbolOperand * v)95 void CFIOpndEmitVisitor::Visit(SymbolOperand *v)
96 {
97 CHECK_FATAL(false, "NIY");
98 }
Visit(StrOperand * v)99 void CFIOpndEmitVisitor::Visit(StrOperand *v)
100 {
101 emitter.Emit(v->GetStr());
102 }
Visit(LabelOperand * v)103 void CFIOpndEmitVisitor::Visit(LabelOperand *v)
104 {
105 if (emitter.GetCG()->GetMIRModule()->IsCModule()) {
106 CHECK_NULL_FATAL(emitter.GetCG()->GetMIRModule()->CurFunction());
107 PUIdx pIdx = emitter.GetCG()->GetMIRModule()->CurFunction()->GetPuidx();
108 char *idx = strdup(std::to_string(pIdx).c_str());
109 CHECK_FATAL(idx != nullptr, "strdup failed");
110 emitter.Emit(".label.").Emit(idx).Emit("__").Emit(v->GetIabelIdx());
111 free(idx);
112 idx = nullptr;
113 } else {
114 emitter.Emit(".label.").Emit(v->GetParentFunc()).Emit(v->GetIabelIdx());
115 }
116 }
117 } /* namespace cfi */
118