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
52 #if DEBUG
Check() const53 void DbgInsn::Check() const
54 {
55 DbgDescr &dbgDescr = dbgDescrTable[GetMachineOpcode()];
56 /* dbg instruction's 3rd /4th/5th operand must be null */
57 for (uint32 i = 0; i < dbgDescr.opndCount; ++i) {
58 Operand &opnd = GetOperand(i);
59 if (opnd.GetKind() != dbgDescr.opndTypes[i]) {
60 CHECK_FATAL(false, "incorrect operand in debug insn");
61 }
62 }
63 }
64 #endif
65
GetLoc() const66 uint32 DbgInsn::GetLoc() const
67 {
68 if (mOp != OP_DBG_loc) {
69 return 0;
70 }
71 return static_cast<uint32>(static_cast<ImmOperand *>(opnds[0])->GetVal());
72 }
73
Dump() const74 void ImmOperand::Dump() const
75 {
76 LogInfo::MapleLogger() << " " << val;
77 }
Visit(ImmOperand * v)78 void DBGOpndEmitVisitor::Visit(ImmOperand *v)
79 {
80 emitter.Emit(v->GetVal());
81 }
82 } // namespace mpldbg
83