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(const DbgInsn & other)43 DbgInsn(const DbgInsn &other) : maplebe::Insn(other) {} 44 DbgInsn(MemPool & memPool,maplebe::MOperator op,maplebe::Operand & opnd0)45 DbgInsn(MemPool &memPool, maplebe::MOperator op, maplebe::Operand &opnd0) : Insn(memPool, op, opnd0) {} 46 DbgInsn(MemPool & memPool,maplebe::MOperator op,maplebe::Operand & opnd0,maplebe::Operand & opnd1)47 DbgInsn(MemPool &memPool, maplebe::MOperator op, maplebe::Operand &opnd0, maplebe::Operand &opnd1) 48 : Insn(memPool, op, opnd0, opnd1) 49 { 50 } 51 DbgInsn(MemPool & memPool,maplebe::MOperator op,maplebe::Operand & opnd0,maplebe::Operand & opnd1,maplebe::Operand & opnd2)52 DbgInsn(MemPool &memPool, maplebe::MOperator op, maplebe::Operand &opnd0, maplebe::Operand &opnd1, 53 maplebe::Operand &opnd2) 54 : Insn(memPool, op, opnd0, opnd1, opnd2) 55 { 56 } 57 58 ~DbgInsn() = default; 59 CloneTree(MapleAllocator & allocator)60 DbgInsn *CloneTree(MapleAllocator &allocator) const override 61 { 62 auto *insn = allocator.GetMemPool()->New<DbgInsn>(*this); 63 insn->DeepClone(*this, allocator); 64 return insn; 65 } 66 IsMachineInstruction()67 bool IsMachineInstruction() const override 68 { 69 return false; 70 } 71 72 void Dump() const override; 73 74 bool CheckMD() const override; 75 IsTargetInsn()76 bool IsTargetInsn() const override 77 { 78 return false; 79 } 80 IsDbgInsn()81 bool IsDbgInsn() const override 82 { 83 return true; 84 } 85 IsDbgLine()86 bool IsDbgLine() const override 87 { 88 return mOp == OP_DBG_loc; 89 } 90 IsRegDefined(maplebe::regno_t regNO)91 bool IsRegDefined(maplebe::regno_t regNO) const override 92 { 93 CHECK_FATAL(false, "dbg insn do not def regs"); 94 return false; 95 } 96 GetDefRegs()97 std::set<uint32> GetDefRegs() const override 98 { 99 CHECK_FATAL(false, "dbg insn do not def regs"); 100 return std::set<uint32>(); 101 } 102 GetBothDefUseOpnd()103 uint32 GetBothDefUseOpnd() const override 104 { 105 return maplebe::kInsnMaxOpnd; 106 } 107 108 uint32 GetLoc() const; 109 110 private: 111 DbgInsn &operator=(const DbgInsn &); 112 }; 113 114 class ImmOperand : public maplebe::OperandVisitable<ImmOperand> { 115 public: ImmOperand(int64 val)116 explicit ImmOperand(int64 val) : OperandVisitable(kOpdImmediate, k32BitSize), val(val) {} 117 118 ~ImmOperand() = default; 119 using OperandVisitable<ImmOperand>::OperandVisitable; 120 CloneTree(MapleAllocator & allocator)121 ImmOperand *CloneTree(MapleAllocator &allocator) const override 122 { 123 // const MIRSymbol is not changed in cg, so we can do shallow copy 124 return allocator.GetMemPool()->New<ImmOperand>(*this); 125 } 126 Clone(MemPool & memPool)127 Operand *Clone(MemPool &memPool) const override 128 { 129 Operand *opnd = memPool.Clone<ImmOperand>(*this); 130 return opnd; 131 } 132 133 void Dump() const override; 134 Less(const Operand & right)135 bool Less(const Operand &right) const override 136 { 137 (void)right; 138 return false; 139 } 140 GetVal()141 int64 GetVal() const 142 { 143 return val; 144 } 145 146 private: 147 int64 val; 148 }; 149 150 class DBGOpndEmitVisitor : public maplebe::OperandVisitorBase, public maplebe::OperandVisitor<ImmOperand> { 151 public: DBGOpndEmitVisitor(maplebe::Emitter & asmEmitter)152 explicit DBGOpndEmitVisitor(maplebe::Emitter &asmEmitter) : emitter(asmEmitter) {} 153 virtual ~DBGOpndEmitVisitor() = default; 154 155 protected: 156 maplebe::Emitter &emitter; 157 158 private: 159 void Visit(ImmOperand *v) final; 160 }; 161 162 } /* namespace mpldbg */ 163 164 #endif /* MAPLEBE_INCLUDE_CG_DBG_H */ 165