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 "dbg.h"
17 #include "emit.h"
18
19 namespace mpldbg {
20 using maplebe::CG;
21 using maplebe::Emitter;
22 using maplebe::MOperator;
23 using maplebe::Operand;
24 using maplebe::OpndDesc;
25
26 struct DbgDescr {
27 const std::string name;
28 uint32 opndCount;
29 /* create 3 OperandType array to store dbg instruction's operand type */
30 std::array<Operand::OperandType, 3> opndTypes;
31 };
32
33 static DbgDescr dbgDescrTable[kOpDbgLast + 1] = {
34 #define DBG_DEFINE(k, sub, n, o0, o1, o2) {#k, n, {Operand::kOpd##o0, Operand::kOpd##o1, Operand::kOpd##o2}},
35 #include "dbg.def"
36 #undef DBG_DEFINE
37 {"undef", 0, {Operand::kOpdUndef, Operand::kOpdUndef, Operand::kOpdUndef}}};
38
Dump() const39 void DbgInsn::Dump() const
40 {
41 MOperator mOp = GetMachineOpcode();
42 DbgDescr &dbgDescr = dbgDescrTable[mOp];
43 LogInfo::MapleLogger() << "DBG " << dbgDescr.name;
44 for (uint32 i = 0; i < dbgDescr.opndCount; ++i) {
45 LogInfo::MapleLogger() << (i == 0 ? " : " : " ");
46 Operand &curOperand = GetOperand(i);
47 curOperand.Dump();
48 }
49 LogInfo::MapleLogger() << "\n";
50 }
51
CheckMD() const52 bool DbgInsn::CheckMD() const
53 {
54 DbgDescr &dbgDescr = dbgDescrTable[GetMachineOpcode()];
55 /* dbg instruction's 3rd /4th/5th operand must be null */
56 for (uint32 i = 0; i < dbgDescr.opndCount; ++i) {
57 Operand &opnd = GetOperand(i);
58 if (opnd.GetKind() != dbgDescr.opndTypes[i]) {
59 return false;
60 }
61 }
62 return true;
63 }
64
GetLoc() const65 uint32 DbgInsn::GetLoc() const
66 {
67 if (mOp != OP_DBG_loc) {
68 return 0;
69 }
70 return static_cast<uint32>(static_cast<ImmOperand *>(opnds[0])->GetVal());
71 }
72
Dump() const73 void ImmOperand::Dump() const
74 {
75 LogInfo::MapleLogger() << " " << val;
76 }
Visit(ImmOperand * v)77 void DBGOpndEmitVisitor::Visit(ImmOperand *v)
78 {
79 emitter.Emit(v->GetVal());
80 }
81 } // namespace mpldbg
82