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 #ifndef MAPLEBE_INCLUDE_CG_DBG_H 17 #define MAPLEBE_INCLUDE_CG_DBG_H 18 19 #include "insn.h" 20 #include "mempool_allocator.h" 21 #include "mir_symbol.h" 22 #include "debug_info.h" 23 24 namespace mpldbg { 25 using namespace maple; 26 27 /* https://sourceware.org/binutils/docs-2.28/as/Loc.html */ 28 enum LocOpt { kBB, kProEnd, kEpiBeg, kIsStmt, kIsa, kDisc }; 29 30 enum DbgOpcode : uint8 { 31 #define DBG_DEFINE(k, sub, n, o0, o1, o2) OP_DBG_##k##sub, 32 #define ARM_DIRECTIVES_DEFINE(k, sub, n, o0, o1, o2) OP_ARM_DIRECTIVES_##k##sub, 33 #include "dbg.def" 34 #undef DBG_DEFINE 35 #undef ARM_DIRECTIVES_DEFINE 36 kOpDbgLast 37 }; 38 39 class DbgInsn : public maplebe::Insn { 40 public: DbgInsn(MemPool & memPool,maplebe::MOperator op)41 DbgInsn(MemPool &memPool, maplebe::MOperator op) : Insn(memPool, op) {} 42 DbgInsn(MemPool & memPool,maplebe::MOperator op,maplebe::Operand & opnd0)43 DbgInsn(MemPool &memPool, maplebe::MOperator op, maplebe::Operand &opnd0) : Insn(memPool, op, opnd0) {} 44 DbgInsn(MemPool & memPool,maplebe::MOperator op,maplebe::Operand & opnd0,maplebe::Operand & opnd1)45 DbgInsn(MemPool &memPool, maplebe::MOperator op, maplebe::Operand &opnd0, maplebe::Operand &opnd1) 46 : Insn(memPool, op, opnd0, opnd1) 47 { 48 } 49 DbgInsn(MemPool & memPool,maplebe::MOperator op,maplebe::Operand & opnd0,maplebe::Operand & opnd1,maplebe::Operand & opnd2)50 DbgInsn(MemPool &memPool, maplebe::MOperator op, maplebe::Operand &opnd0, maplebe::Operand &opnd1, 51 maplebe::Operand &opnd2) 52 : Insn(memPool, op, opnd0, opnd1, opnd2) 53 { 54 } 55 56 ~DbgInsn() = default; 57 IsMachineInstruction()58 bool IsMachineInstruction() const override 59 { 60 return false; 61 } 62 63 void Dump() const override; 64 65 #if DEBUG 66 void Check() const override; 67 #endif 68 IsTargetInsn()69 bool IsTargetInsn() const override 70 { 71 return false; 72 } 73 IsDbgInsn()74 bool IsDbgInsn() const override 75 { 76 return true; 77 } 78 IsRegDefined(maplebe::regno_t regNO)79 bool IsRegDefined(maplebe::regno_t regNO) const override 80 { 81 CHECK_FATAL(false, "dbg insn do not def regs"); 82 return false; 83 } 84 GetDefRegs()85 std::set<uint32> GetDefRegs() const override 86 { 87 CHECK_FATAL(false, "dbg insn do not def regs"); 88 return std::set<uint32>(); 89 } 90 GetBothDefUseOpnd()91 uint32 GetBothDefUseOpnd() const override 92 { 93 return maplebe::kInsnMaxOpnd; 94 } 95 96 uint32 GetLoc() const; 97 98 private: 99 DbgInsn &operator=(const DbgInsn &); 100 }; 101 102 class ImmOperand : public maplebe::OperandVisitable<ImmOperand> { 103 public: ImmOperand(int64 val)104 explicit ImmOperand(int64 val) : OperandVisitable(kOpdImmediate, k32BitSize), val(val) {} 105 106 ~ImmOperand() = default; 107 using OperandVisitable<ImmOperand>::OperandVisitable; 108 Clone(MemPool & memPool)109 Operand *Clone(MemPool &memPool) const override 110 { 111 Operand *opnd = memPool.Clone<ImmOperand>(*this); 112 return opnd; 113 } 114 115 void Dump() const override; 116 Less(const Operand & right)117 bool Less(const Operand &right) const override 118 { 119 (void)right; 120 return false; 121 } 122 GetVal()123 int64 GetVal() const 124 { 125 return val; 126 } 127 128 private: 129 int64 val; 130 }; 131 132 class DBGOpndEmitVisitor : public maplebe::OperandVisitorBase, public maplebe::OperandVisitor<ImmOperand> { 133 public: DBGOpndEmitVisitor(maplebe::Emitter & asmEmitter)134 explicit DBGOpndEmitVisitor(maplebe::Emitter &asmEmitter) : emitter(asmEmitter) {} 135 virtual ~DBGOpndEmitVisitor() = default; 136 137 protected: 138 maplebe::Emitter &emitter; 139 140 private: 141 void Visit(ImmOperand *v) final; 142 }; 143 144 } /* namespace mpldbg */ 145 146 #endif /* MAPLEBE_INCLUDE_CG_DBG_H */ 147