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::MOperator;
21 using maplebe::Operand;
22
23 struct CfiDescr {
24 const std::string name;
25 uint32 opndCount;
26 /* create 3 OperandType array to store cfi instruction's operand type */
27 std::array<Operand::OperandType, 3> opndTypes;
28 };
29
30 static CfiDescr cfiDescrTable[kOpCfiLast + 1] = {
31 #define CFI_DEFINE(k, sub, n, o0, o1, o2) {".cfi_" #k, n, {Operand::kOpd##o0, Operand::kOpd##o1, Operand::kOpd##o2}},
32 #define ARM_DIRECTIVES_DEFINE(k, sub, n, o0, o1, o2) \
33 {"." #k, n, {Operand::kOpd##o0, Operand::kOpd##o1, Operand::kOpd##o2}},
34 #include "cfi.def"
35 #undef CFI_DEFINE
36 #undef ARM_DIRECTIVES_DEFINE
37 {".cfi_undef", 0, {Operand::kOpdUndef, Operand::kOpdUndef, Operand::kOpdUndef}}};
38
Dump() const39 void CfiInsn::Dump() const
40 {
41 #ifdef ARK_LITECG_DEBUG
42 MOperator mOp = GetMachineOpcode();
43 CfiDescr &cfiDescr = cfiDescrTable[mOp];
44 LogInfo::MapleLogger() << "CFI " << cfiDescr.name;
45 for (uint32 i = 0; i < static_cast<uint32>(cfiDescr.opndCount); ++i) {
46 LogInfo::MapleLogger() << (i == 0 ? " : " : " ");
47 Operand &curOperand = GetOperand(i);
48 curOperand.Dump();
49 }
50 LogInfo::MapleLogger() << "\n";
51 #endif
52 }
53
CheckMD() const54 bool CfiInsn::CheckMD() const
55 {
56 #ifdef ARK_LITECG_DEBUG
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 #endif
66 return true;
67 }
68
Dump() const69 void RegOperand::Dump() const
70 {
71 #ifdef ARK_LITECG_DEBUG
72 LogInfo::MapleLogger() << "reg: " << regNO << "[ size: " << GetSize() << "] ";
73 #endif
74 }
75
Dump() const76 void ImmOperand::Dump() const
77 {
78 #ifdef ARK_LITECG_DEBUG
79 LogInfo::MapleLogger() << "imm: " << val << "[ size: " << GetSize() << "] ";
80 #endif
81 }
82
Dump() const83 void StrOperand::Dump() const
84 {
85 #ifdef ARK_LITECG_DEBUG
86 LogInfo::MapleLogger() << str;
87 #endif
88 }
89
Dump() const90 void LabelOperand::Dump() const
91 {
92 #ifdef ARK_LITECG_DEBUG
93 LogInfo::MapleLogger() << "label:" << labelIndex;
94 #endif
95 }
Visit(RegOperand *v)96 void CFIOpndEmitVisitor::Visit(RegOperand *v)
97 {
98 #ifdef ARK_LITECG_DEBUG
99 emitter.Emit(v->GetRegisterNO());
100 #endif
101 }
Visit(ImmOperand *v)102 void CFIOpndEmitVisitor::Visit(ImmOperand *v)
103 {
104 #ifdef ARK_LITECG_DEBUG
105 emitter.Emit(v->GetValue());
106 #endif
107 }
Visit(SymbolOperand *v)108 void CFIOpndEmitVisitor::Visit(SymbolOperand *v)
109 {
110 #ifdef ARK_LITECG_DEBUG
111 CHECK_FATAL(false, "NIY");
112 #endif
113 }
Visit(StrOperand *v)114 void CFIOpndEmitVisitor::Visit(StrOperand *v)
115 {
116 #ifdef ARK_LITECG_DEBUG
117 emitter.Emit(v->GetStr());
118 #endif
119 }
Visit(LabelOperand *v)120 void CFIOpndEmitVisitor::Visit(LabelOperand *v)
121 {
122 #ifdef ARK_LITECG_DEBUG
123 emitter.Emit(".label.").Emit(v->GetParentFunc()).Emit(v->GetIabelIdx());
124 #endif
125 }
126 } /* namespace cfi */
127