Adrian Prantl | b16d9eb | 2015-01-12 22:19:22 +0000 | [diff] [blame] | 1 | //===-- llvm/CodeGen/DwarfExpression.h - Dwarf Compile Unit ---*- C++ -*--===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains support for writing dwarf compile unit. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFEXPRESSION_H |
| 15 | #define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFEXPRESSION_H |
| 16 | |
Adrian Prantl | 092d948 | 2015-01-13 23:39:11 +0000 | [diff] [blame] | 17 | #include "llvm/IR/DebugInfo.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 18 | #include "llvm/Support/DataTypes.h" |
Adrian Prantl | b16d9eb | 2015-01-12 22:19:22 +0000 | [diff] [blame] | 19 | |
| 20 | namespace llvm { |
| 21 | |
Adrian Prantl | a4c30d6 | 2015-01-12 23:36:56 +0000 | [diff] [blame] | 22 | class AsmPrinter; |
Adrian Prantl | 66f2595 | 2015-01-13 00:04:06 +0000 | [diff] [blame] | 23 | class ByteStreamer; |
Adrian Prantl | a4c30d6 | 2015-01-12 23:36:56 +0000 | [diff] [blame] | 24 | class TargetRegisterInfo; |
Adrian Prantl | 658676c | 2015-01-14 01:01:22 +0000 | [diff] [blame] | 25 | class DwarfUnit; |
| 26 | class DIELoc; |
Adrian Prantl | b16d9eb | 2015-01-12 22:19:22 +0000 | [diff] [blame] | 27 | |
Adrian Prantl | 54286bd | 2016-11-02 16:12:20 +0000 | [diff] [blame] | 28 | /// Holds a DIExpression and keeps track of how many operands have been consumed |
| 29 | /// so far. |
| 30 | class DIExpressionCursor { |
| 31 | DIExpression::expr_op_iterator Start, End; |
| 32 | public: |
| 33 | DIExpressionCursor(const DIExpression *Expr) { |
| 34 | if (!Expr) { |
| 35 | assert(Start == End); |
| 36 | return; |
| 37 | } |
| 38 | Start = Expr->expr_op_begin(); |
| 39 | End = Expr->expr_op_end(); |
| 40 | } |
| 41 | |
Adrian Prantl | 8fafb8d | 2016-12-09 20:43:40 +0000 | [diff] [blame] | 42 | DIExpressionCursor(ArrayRef<uint64_t> Expr) |
| 43 | : Start(Expr.begin()), End(Expr.end()) {} |
| 44 | |
Adrian Prantl | 54286bd | 2016-11-02 16:12:20 +0000 | [diff] [blame] | 45 | /// Consume one operation. |
| 46 | Optional<DIExpression::ExprOperand> take() { |
| 47 | if (Start == End) |
| 48 | return None; |
| 49 | return *(Start++); |
| 50 | } |
| 51 | |
| 52 | /// Consume N operations. |
| 53 | void consume(unsigned N) { std::advance(Start, N); } |
| 54 | |
| 55 | /// Return the current operation. |
| 56 | Optional<DIExpression::ExprOperand> peek() const { |
| 57 | if (Start == End) |
| 58 | return None; |
| 59 | return *(Start); |
| 60 | } |
| 61 | |
| 62 | /// Return the next operation. |
| 63 | Optional<DIExpression::ExprOperand> peekNext() const { |
| 64 | if (Start == End) |
| 65 | return None; |
| 66 | |
| 67 | auto Next = Start.getNext(); |
| 68 | if (Next == End) |
| 69 | return None; |
| 70 | |
| 71 | return *Next; |
| 72 | } |
Adrian Prantl | f148d69 | 2016-11-02 16:20:37 +0000 | [diff] [blame] | 73 | /// Determine whether there are any operations left in this expression. |
Adrian Prantl | 54286bd | 2016-11-02 16:12:20 +0000 | [diff] [blame] | 74 | operator bool() const { return Start != End; } |
Adrian Prantl | ada1048 | 2017-04-20 20:42:33 +0000 | [diff] [blame^] | 75 | DIExpression::expr_op_iterator begin() const { return Start; } |
| 76 | DIExpression::expr_op_iterator end() const { return End; } |
Adrian Prantl | 5542da4 | 2016-12-22 06:10:41 +0000 | [diff] [blame] | 77 | |
| 78 | /// Retrieve the fragment information, if any. |
| 79 | Optional<DIExpression::FragmentInfo> getFragmentInfo() const { |
| 80 | return DIExpression::getFragmentInfo(Start, End); |
| 81 | } |
Adrian Prantl | 54286bd | 2016-11-02 16:12:20 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
Adrian Prantl | b16d9eb | 2015-01-12 22:19:22 +0000 | [diff] [blame] | 84 | /// Base class containing the logic for constructing DWARF expressions |
| 85 | /// independently of whether they are emitted into a DIE or into a .debug_loc |
| 86 | /// entry. |
| 87 | class DwarfExpression { |
Adrian Prantl | 00dbc2a | 2015-01-12 22:19:26 +0000 | [diff] [blame] | 88 | protected: |
Adrian Prantl | 80e188d | 2017-03-22 01:15:57 +0000 | [diff] [blame] | 89 | /// Holds information about all subregisters comprising a register location. |
| 90 | struct Register { |
| 91 | int DwarfRegNo; |
| 92 | unsigned Size; |
| 93 | const char *Comment; |
| 94 | }; |
| 95 | |
| 96 | /// The register location, if any. |
| 97 | SmallVector<Register, 2> DwarfRegs; |
| 98 | |
Adrian Prantl | 8fafb8d | 2016-12-09 20:43:40 +0000 | [diff] [blame] | 99 | /// Current Fragment Offset in Bits. |
| 100 | uint64_t OffsetInBits = 0; |
Adrian Prantl | 3621309 | 2017-03-16 17:42:47 +0000 | [diff] [blame] | 101 | unsigned DwarfVersion; |
Adrian Prantl | 8fafb8d | 2016-12-09 20:43:40 +0000 | [diff] [blame] | 102 | |
| 103 | /// Sometimes we need to add a DW_OP_bit_piece to describe a subregister. |
| 104 | unsigned SubRegisterSizeInBits = 0; |
| 105 | unsigned SubRegisterOffsetInBits = 0; |
| 106 | |
Adrian Prantl | 6825fb6 | 2017-04-18 01:21:53 +0000 | [diff] [blame] | 107 | /// The kind of location description being produced. |
| 108 | enum { Unknown = 0, Register, Memory, Implicit } LocationKind = Unknown; |
| 109 | |
Adrian Prantl | 8fafb8d | 2016-12-09 20:43:40 +0000 | [diff] [blame] | 110 | /// Push a DW_OP_piece / DW_OP_bit_piece for emitting later, if one is needed |
| 111 | /// to represent a subregister. |
| 112 | void setSubRegisterPiece(unsigned SizeInBits, unsigned OffsetInBits) { |
| 113 | SubRegisterSizeInBits = SizeInBits; |
| 114 | SubRegisterOffsetInBits = OffsetInBits; |
| 115 | } |
Adrian Prantl | a4c30d6 | 2015-01-12 23:36:56 +0000 | [diff] [blame] | 116 | |
Adrian Prantl | 981f03e | 2017-03-16 17:14:56 +0000 | [diff] [blame] | 117 | /// Add masking operations to stencil out a subregister. |
| 118 | void maskSubRegister(); |
| 119 | |
Adrian Prantl | 172ab66 | 2015-01-13 23:11:07 +0000 | [diff] [blame] | 120 | /// Output a dwarf operand and an optional assembler comment. |
Adrian Prantl | a63b8e8 | 2017-03-16 17:42:45 +0000 | [diff] [blame] | 121 | virtual void emitOp(uint8_t Op, const char *Comment = nullptr) = 0; |
Adrian Prantl | 172ab66 | 2015-01-13 23:11:07 +0000 | [diff] [blame] | 122 | /// Emit a raw signed value. |
Adrian Prantl | a63b8e8 | 2017-03-16 17:42:45 +0000 | [diff] [blame] | 123 | virtual void emitSigned(int64_t Value) = 0; |
Adrian Prantl | 172ab66 | 2015-01-13 23:11:07 +0000 | [diff] [blame] | 124 | /// Emit a raw unsigned value. |
Adrian Prantl | a63b8e8 | 2017-03-16 17:42:45 +0000 | [diff] [blame] | 125 | virtual void emitUnsigned(uint64_t Value) = 0; |
Adrian Prantl | 172ab66 | 2015-01-13 23:11:07 +0000 | [diff] [blame] | 126 | /// Return whether the given machine register is the frame register in the |
| 127 | /// current function. |
Peter Collingbourne | 96c9ae6 | 2016-05-20 19:35:17 +0000 | [diff] [blame] | 128 | virtual bool isFrameRegister(const TargetRegisterInfo &TRI, unsigned MachineReg) = 0; |
Adrian Prantl | 00dbc2a | 2015-01-12 22:19:26 +0000 | [diff] [blame] | 129 | |
Adrian Prantl | 6825fb6 | 2017-04-18 01:21:53 +0000 | [diff] [blame] | 130 | /// Emit a DW_OP_reg operation. Note that this is only legal inside a DWARF |
| 131 | /// register location description. |
Adrian Prantl | a63b8e8 | 2017-03-16 17:42:45 +0000 | [diff] [blame] | 132 | void addReg(int DwarfReg, const char *Comment = nullptr); |
Adrian Prantl | a271988 | 2017-03-22 17:19:55 +0000 | [diff] [blame] | 133 | /// Emit a DW_OP_breg operation. |
| 134 | void addBReg(int DwarfReg, int Offset); |
Adrian Prantl | 80e188d | 2017-03-22 01:15:57 +0000 | [diff] [blame] | 135 | /// Emit DW_OP_fbreg <Offset>. |
| 136 | void addFBReg(int Offset); |
Adrian Prantl | 956484b | 2017-03-20 21:35:09 +0000 | [diff] [blame] | 137 | |
| 138 | /// Emit a partial DWARF register operation. |
| 139 | /// |
| 140 | /// \param MachineReg The register number. |
| 141 | /// \param MaxSize If the register must be composed from |
| 142 | /// sub-registers this is an upper bound |
| 143 | /// for how many bits the emitted DW_OP_piece |
| 144 | /// may cover. |
| 145 | /// |
| 146 | /// If size and offset is zero an operation for the entire register is |
| 147 | /// emitted: Some targets do not provide a DWARF register number for every |
| 148 | /// register. If this is the case, this function will attempt to emit a DWARF |
| 149 | /// register by emitting a fragment of a super-register or by piecing together |
| 150 | /// multiple subregisters that alias the register. |
| 151 | /// |
| 152 | /// \return false if no DWARF register exists for MachineReg. |
| 153 | bool addMachineReg(const TargetRegisterInfo &TRI, unsigned MachineReg, |
| 154 | unsigned MaxSize = ~1U); |
| 155 | |
| 156 | |
Adrian Prantl | 8fafb8d | 2016-12-09 20:43:40 +0000 | [diff] [blame] | 157 | /// Emit a DW_OP_piece or DW_OP_bit_piece operation for a variable fragment. |
| 158 | /// \param OffsetInBits This is an optional offset into the location that |
| 159 | /// is at the top of the DWARF stack. |
Adrian Prantl | a63b8e8 | 2017-03-16 17:42:45 +0000 | [diff] [blame] | 160 | void addOpPiece(unsigned SizeInBits, unsigned OffsetInBits = 0); |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 161 | |
Adrian Prantl | 981f03e | 2017-03-16 17:14:56 +0000 | [diff] [blame] | 162 | /// Emit a shift-right dwarf operation. |
Adrian Prantl | a63b8e8 | 2017-03-16 17:42:45 +0000 | [diff] [blame] | 163 | void addShr(unsigned ShiftBy); |
Adrian Prantl | 981f03e | 2017-03-16 17:14:56 +0000 | [diff] [blame] | 164 | /// Emit a bitwise and dwarf operation. |
Adrian Prantl | a63b8e8 | 2017-03-16 17:42:45 +0000 | [diff] [blame] | 165 | void addAnd(unsigned Mask); |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 166 | |
Adrian Prantl | 3e9c887 | 2016-04-08 00:38:37 +0000 | [diff] [blame] | 167 | /// Emit a DW_OP_stack_value, if supported. |
| 168 | /// |
Adrian Prantl | f148d69 | 2016-11-02 16:20:37 +0000 | [diff] [blame] | 169 | /// The proper way to describe a constant value is DW_OP_constu <const>, |
| 170 | /// DW_OP_stack_value. Unfortunately, DW_OP_stack_value was not available |
| 171 | /// until DWARF 4, so we will continue to generate DW_OP_constu <const> for |
| 172 | /// DWARF 2 and DWARF 3. Technically, this is incorrect since DW_OP_const |
| 173 | /// <const> actually describes a value at a constant addess, not a constant |
| 174 | /// value. However, in the past there was no better way to describe a |
| 175 | /// constant value, so the producers and consumers started to rely on |
| 176 | /// heuristics to disambiguate the value vs. location status of the |
| 177 | /// expression. See PR21176 for more details. |
Adrian Prantl | a63b8e8 | 2017-03-16 17:42:45 +0000 | [diff] [blame] | 178 | void addStackValue(); |
Adrian Prantl | b16d9eb | 2015-01-12 22:19:22 +0000 | [diff] [blame] | 179 | |
Adrian Prantl | 035862b | 2017-03-27 17:34:04 +0000 | [diff] [blame] | 180 | ~DwarfExpression() = default; |
Adrian Prantl | 52884b7 | 2017-03-20 21:34:19 +0000 | [diff] [blame] | 181 | public: |
| 182 | DwarfExpression(unsigned DwarfVersion) : DwarfVersion(DwarfVersion) {} |
Adrian Prantl | 52884b7 | 2017-03-20 21:34:19 +0000 | [diff] [blame] | 183 | |
| 184 | /// This needs to be called last to commit any pending changes. |
| 185 | void finalize(); |
| 186 | |
Adrian Prantl | 66f2595 | 2015-01-13 00:04:06 +0000 | [diff] [blame] | 187 | /// Emit a signed constant. |
Adrian Prantl | a63b8e8 | 2017-03-16 17:42:45 +0000 | [diff] [blame] | 188 | void addSignedConstant(int64_t Value); |
Adrian Prantl | 66f2595 | 2015-01-13 00:04:06 +0000 | [diff] [blame] | 189 | /// Emit an unsigned constant. |
Adrian Prantl | a63b8e8 | 2017-03-16 17:42:45 +0000 | [diff] [blame] | 190 | void addUnsignedConstant(uint64_t Value); |
Adrian Prantl | 3e9c887 | 2016-04-08 00:38:37 +0000 | [diff] [blame] | 191 | /// Emit an unsigned constant. |
Adrian Prantl | a63b8e8 | 2017-03-16 17:42:45 +0000 | [diff] [blame] | 192 | void addUnsignedConstant(const APInt &Value); |
Adrian Prantl | 66f2595 | 2015-01-13 00:04:06 +0000 | [diff] [blame] | 193 | |
Adrian Prantl | c12cee3 | 2017-04-19 23:42:25 +0000 | [diff] [blame] | 194 | /// Lock this down to become a memory location description. |
| 195 | void setMemoryLocationKind() { |
| 196 | assert(LocationKind == Unknown); |
| 197 | LocationKind = Memory; |
| 198 | } |
| 199 | |
Adrian Prantl | f148d69 | 2016-11-02 16:20:37 +0000 | [diff] [blame] | 200 | /// Emit a machine register location. As an optimization this may also consume |
| 201 | /// the prefix of a DwarfExpression if a more efficient representation for |
| 202 | /// combining the register location and the first operation exists. |
Duncan P. N. Exon Smith | 60635e3 | 2015-04-21 18:44:06 +0000 | [diff] [blame] | 203 | /// |
Adrian Prantl | c12cee3 | 2017-04-19 23:42:25 +0000 | [diff] [blame] | 204 | /// \param FragmentOffsetInBits If this is one fragment out of a |
| 205 | /// fragmented |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 206 | /// location, this is the offset of the |
| 207 | /// fragment inside the entire variable. |
| 208 | /// \return false if no DWARF register exists |
| 209 | /// for MachineReg. |
Adrian Prantl | c12cee3 | 2017-04-19 23:42:25 +0000 | [diff] [blame] | 210 | bool addMachineRegExpression(const TargetRegisterInfo &TRI, |
| 211 | DIExpressionCursor &Expr, unsigned MachineReg, |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 212 | unsigned FragmentOffsetInBits = 0); |
Adrian Prantl | 54286bd | 2016-11-02 16:12:20 +0000 | [diff] [blame] | 213 | /// Emit all remaining operations in the DIExpressionCursor. |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 214 | /// |
| 215 | /// \param FragmentOffsetInBits If this is one fragment out of multiple |
| 216 | /// locations, this is the offset of the |
| 217 | /// fragment inside the entire variable. |
Adrian Prantl | a63b8e8 | 2017-03-16 17:42:45 +0000 | [diff] [blame] | 218 | void addExpression(DIExpressionCursor &&Expr, |
Adrian Prantl | 941fa75 | 2016-12-05 18:04:47 +0000 | [diff] [blame] | 219 | unsigned FragmentOffsetInBits = 0); |
Adrian Prantl | 8fafb8d | 2016-12-09 20:43:40 +0000 | [diff] [blame] | 220 | |
| 221 | /// If applicable, emit an empty DW_OP_piece / DW_OP_bit_piece to advance to |
| 222 | /// the fragment described by \c Expr. |
| 223 | void addFragmentOffset(const DIExpression *Expr); |
Adrian Prantl | 092d948 | 2015-01-13 23:39:11 +0000 | [diff] [blame] | 224 | }; |
Adrian Prantl | 66f2595 | 2015-01-13 00:04:06 +0000 | [diff] [blame] | 225 | |
| 226 | /// DwarfExpression implementation for .debug_loc entries. |
Adrian Prantl | 035862b | 2017-03-27 17:34:04 +0000 | [diff] [blame] | 227 | class DebugLocDwarfExpression final : public DwarfExpression { |
Adrian Prantl | 66f2595 | 2015-01-13 00:04:06 +0000 | [diff] [blame] | 228 | ByteStreamer &BS; |
| 229 | |
Adrian Prantl | a63b8e8 | 2017-03-16 17:42:45 +0000 | [diff] [blame] | 230 | void emitOp(uint8_t Op, const char *Comment = nullptr) override; |
| 231 | void emitSigned(int64_t Value) override; |
| 232 | void emitUnsigned(uint64_t Value) override; |
Peter Collingbourne | 96c9ae6 | 2016-05-20 19:35:17 +0000 | [diff] [blame] | 233 | bool isFrameRegister(const TargetRegisterInfo &TRI, |
| 234 | unsigned MachineReg) override; |
Adrian Prantl | 52884b7 | 2017-03-20 21:34:19 +0000 | [diff] [blame] | 235 | public: |
| 236 | DebugLocDwarfExpression(unsigned DwarfVersion, ByteStreamer &BS) |
| 237 | : DwarfExpression(DwarfVersion), BS(BS) {} |
Adrian Prantl | b16d9eb | 2015-01-12 22:19:22 +0000 | [diff] [blame] | 238 | }; |
Adrian Prantl | 658676c | 2015-01-14 01:01:22 +0000 | [diff] [blame] | 239 | |
| 240 | /// DwarfExpression implementation for singular DW_AT_location. |
Adrian Prantl | 035862b | 2017-03-27 17:34:04 +0000 | [diff] [blame] | 241 | class DIEDwarfExpression final : public DwarfExpression { |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 242 | const AsmPrinter &AP; |
Adrian Prantl | 658676c | 2015-01-14 01:01:22 +0000 | [diff] [blame] | 243 | DwarfUnit &DU; |
| 244 | DIELoc &DIE; |
| 245 | |
Adrian Prantl | a63b8e8 | 2017-03-16 17:42:45 +0000 | [diff] [blame] | 246 | void emitOp(uint8_t Op, const char *Comment = nullptr) override; |
| 247 | void emitSigned(int64_t Value) override; |
| 248 | void emitUnsigned(uint64_t Value) override; |
Peter Collingbourne | 96c9ae6 | 2016-05-20 19:35:17 +0000 | [diff] [blame] | 249 | bool isFrameRegister(const TargetRegisterInfo &TRI, |
| 250 | unsigned MachineReg) override; |
Adrian Prantl | 52884b7 | 2017-03-20 21:34:19 +0000 | [diff] [blame] | 251 | public: |
| 252 | DIEDwarfExpression(const AsmPrinter &AP, DwarfUnit &DU, DIELoc &DIE); |
Adrian Prantl | bceaaa9 | 2016-12-20 02:09:43 +0000 | [diff] [blame] | 253 | DIELoc *finalize() { |
| 254 | DwarfExpression::finalize(); |
| 255 | return &DIE; |
| 256 | } |
Adrian Prantl | 658676c | 2015-01-14 01:01:22 +0000 | [diff] [blame] | 257 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 258 | } |
Adrian Prantl | b16d9eb | 2015-01-12 22:19:22 +0000 | [diff] [blame] | 259 | |
| 260 | #endif |