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 CHECK_FATAL(mOp <= kOpCfiLast, "check overflow");
44 CfiDescr &cfiDescr = cfiDescrTable[mOp];
45 LogInfo::MapleLogger() << "CFI " << cfiDescr.name;
46 for (uint32 i = 0; i < static_cast<uint32>(cfiDescr.opndCount); ++i) {
47 LogInfo::MapleLogger() << (i == 0 ? " : " : " ");
48 Operand &curOperand = GetOperand(i);
49 curOperand.Dump();
50 }
51 LogInfo::MapleLogger() << "\n";
52 #endif
53 }
54
CheckMD() const55 bool CfiInsn::CheckMD() const
56 {
57 #ifdef ARK_LITECG_DEBUG
58 CfiDescr &cfiDescr = cfiDescrTable[GetMachineOpcode()];
59 /* cfi instruction's 3rd /4th/5th operand must be null */
60 for (uint32 i = 0; i < static_cast<uint32>(cfiDescr.opndCount); ++i) {
61 Operand &opnd = GetOperand(i);
62 if (opnd.GetKind() != cfiDescr.opndTypes[i]) {
63 return false;
64 }
65 }
66 #endif
67 return true;
68 }
69
Dump() const70 void RegOperand::Dump() const
71 {
72 #ifdef ARK_LITECG_DEBUG
73 LogInfo::MapleLogger() << "reg: " << regNO << "[ size: " << GetSize() << "] ";
74 #endif
75 }
76
Dump() const77 void ImmOperand::Dump() const
78 {
79 #ifdef ARK_LITECG_DEBUG
80 LogInfo::MapleLogger() << "imm: " << val << "[ size: " << GetSize() << "] ";
81 #endif
82 }
83
Dump() const84 void StrOperand::Dump() const
85 {
86 #ifdef ARK_LITECG_DEBUG
87 LogInfo::MapleLogger() << str;
88 #endif
89 }
90
Dump() const91 void LabelOperand::Dump() const
92 {
93 #ifdef ARK_LITECG_DEBUG
94 LogInfo::MapleLogger() << "label:" << labelIndex;
95 #endif
96 }
Visit(RegOperand * v)97 void CFIOpndEmitVisitor::Visit(RegOperand *v)
98 {
99 #ifdef ARK_LITECG_DEBUG
100 emitter.Emit(v->GetRegisterNO());
101 #endif
102 }
Visit(ImmOperand * v)103 void CFIOpndEmitVisitor::Visit(ImmOperand *v)
104 {
105 #ifdef ARK_LITECG_DEBUG
106 emitter.Emit(v->GetValue());
107 #endif
108 }
Visit(SymbolOperand * v)109 void CFIOpndEmitVisitor::Visit(SymbolOperand *v)
110 {
111 #ifdef ARK_LITECG_DEBUG
112 CHECK_FATAL(false, "NIY");
113 #endif
114 }
Visit(StrOperand * v)115 void CFIOpndEmitVisitor::Visit(StrOperand *v)
116 {
117 #ifdef ARK_LITECG_DEBUG
118 emitter.Emit(v->GetStr());
119 #endif
120 }
Visit(LabelOperand * v)121 void CFIOpndEmitVisitor::Visit(LabelOperand *v)
122 {
123 #ifdef ARK_LITECG_DEBUG
124 emitter.Emit(".label.").Emit(v->GetParentFunc()).Emit(v->GetIabelIdx());
125 #endif
126 }
127 } /* namespace cfi */
128