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 | |
| 42 | /// Consume one operation. |
| 43 | Optional<DIExpression::ExprOperand> take() { |
| 44 | if (Start == End) |
| 45 | return None; |
| 46 | return *(Start++); |
| 47 | } |
| 48 | |
| 49 | /// Consume N operations. |
| 50 | void consume(unsigned N) { std::advance(Start, N); } |
| 51 | |
| 52 | /// Return the current operation. |
| 53 | Optional<DIExpression::ExprOperand> peek() const { |
| 54 | if (Start == End) |
| 55 | return None; |
| 56 | return *(Start); |
| 57 | } |
| 58 | |
| 59 | /// Return the next operation. |
| 60 | Optional<DIExpression::ExprOperand> peekNext() const { |
| 61 | if (Start == End) |
| 62 | return None; |
| 63 | |
| 64 | auto Next = Start.getNext(); |
| 65 | if (Next == End) |
| 66 | return None; |
| 67 | |
| 68 | return *Next; |
| 69 | } |
| 70 | operator bool() const { return Start != End; } |
| 71 | }; |
| 72 | |
Adrian Prantl | b16d9eb | 2015-01-12 22:19:22 +0000 | [diff] [blame] | 73 | /// Base class containing the logic for constructing DWARF expressions |
| 74 | /// independently of whether they are emitted into a DIE or into a .debug_loc |
| 75 | /// entry. |
| 76 | class DwarfExpression { |
Adrian Prantl | 00dbc2a | 2015-01-12 22:19:26 +0000 | [diff] [blame] | 77 | protected: |
Adrian Prantl | a4c30d6 | 2015-01-12 23:36:56 +0000 | [diff] [blame] | 78 | // Various convenience accessors that extract things out of AsmPrinter. |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 79 | unsigned DwarfVersion; |
Adrian Prantl | a4c30d6 | 2015-01-12 23:36:56 +0000 | [diff] [blame] | 80 | |
Adrian Prantl | b16d9eb | 2015-01-12 22:19:22 +0000 | [diff] [blame] | 81 | public: |
Peter Collingbourne | 96c9ae6 | 2016-05-20 19:35:17 +0000 | [diff] [blame] | 82 | DwarfExpression(unsigned DwarfVersion) : DwarfVersion(DwarfVersion) {} |
Adrian Prantl | 9cffbd8 | 2015-01-12 23:36:50 +0000 | [diff] [blame] | 83 | virtual ~DwarfExpression() {} |
Adrian Prantl | b16d9eb | 2015-01-12 22:19:22 +0000 | [diff] [blame] | 84 | |
Adrian Prantl | 172ab66 | 2015-01-13 23:11:07 +0000 | [diff] [blame] | 85 | /// Output a dwarf operand and an optional assembler comment. |
| 86 | virtual void EmitOp(uint8_t Op, const char *Comment = nullptr) = 0; |
| 87 | /// Emit a raw signed value. |
Adrian Prantl | 5123368 | 2015-03-10 19:23:37 +0000 | [diff] [blame] | 88 | virtual void EmitSigned(int64_t Value) = 0; |
Adrian Prantl | 172ab66 | 2015-01-13 23:11:07 +0000 | [diff] [blame] | 89 | /// Emit a raw unsigned value. |
Adrian Prantl | 5123368 | 2015-03-10 19:23:37 +0000 | [diff] [blame] | 90 | virtual void EmitUnsigned(uint64_t Value) = 0; |
Adrian Prantl | 172ab66 | 2015-01-13 23:11:07 +0000 | [diff] [blame] | 91 | /// Return whether the given machine register is the frame register in the |
| 92 | /// current function. |
Peter Collingbourne | 96c9ae6 | 2016-05-20 19:35:17 +0000 | [diff] [blame] | 93 | virtual bool isFrameRegister(const TargetRegisterInfo &TRI, unsigned MachineReg) = 0; |
Adrian Prantl | 00dbc2a | 2015-01-12 22:19:26 +0000 | [diff] [blame] | 94 | |
Adrian Prantl | b16d9eb | 2015-01-12 22:19:22 +0000 | [diff] [blame] | 95 | /// Emit a dwarf register operation. |
Adrian Prantl | 172ab66 | 2015-01-13 23:11:07 +0000 | [diff] [blame] | 96 | void AddReg(int DwarfReg, const char *Comment = nullptr); |
Adrian Prantl | b16d9eb | 2015-01-12 22:19:22 +0000 | [diff] [blame] | 97 | /// Emit an (double-)indirect dwarf register operation. |
| 98 | void AddRegIndirect(int DwarfReg, int Offset, bool Deref = false); |
| 99 | |
| 100 | /// Emit a dwarf register operation for describing |
| 101 | /// - a small value occupying only part of a register or |
| 102 | /// - a register representing only part of a value. |
| 103 | void AddOpPiece(unsigned SizeInBits, unsigned OffsetInBits = 0); |
| 104 | /// Emit a shift-right dwarf expression. |
| 105 | void AddShr(unsigned ShiftBy); |
Adrian Prantl | 3e9c887 | 2016-04-08 00:38:37 +0000 | [diff] [blame] | 106 | /// Emit a DW_OP_stack_value, if supported. |
| 107 | /// |
| 108 | /// The proper way to describe a constant value is |
| 109 | /// DW_OP_constu <const>, DW_OP_stack_value. |
| 110 | /// Unfortunately, DW_OP_stack_value was not available until DWARF-4, |
| 111 | /// so we will continue to generate DW_OP_constu <const> for DWARF-2 |
| 112 | /// and DWARF-3. Technically, this is incorrect since DW_OP_const <const> |
| 113 | /// actually describes a value at a constant addess, not a constant value. |
| 114 | /// However, in the past there was no better way to describe a constant |
| 115 | /// value, so the producers and consumers started to rely on heuristics |
| 116 | /// to disambiguate the value vs. location status of the expression. |
| 117 | /// See PR21176 for more details. |
| 118 | void AddStackValue(); |
Adrian Prantl | b16d9eb | 2015-01-12 22:19:22 +0000 | [diff] [blame] | 119 | |
Adrian Prantl | 00dbc2a | 2015-01-12 22:19:26 +0000 | [diff] [blame] | 120 | /// Emit an indirect dwarf register operation for the given machine register. |
Adrian Prantl | ad768c3 | 2015-01-14 01:01:28 +0000 | [diff] [blame] | 121 | /// \return false if no DWARF register exists for MachineReg. |
Peter Collingbourne | 96c9ae6 | 2016-05-20 19:35:17 +0000 | [diff] [blame] | 122 | bool AddMachineRegIndirect(const TargetRegisterInfo &TRI, unsigned MachineReg, |
| 123 | int Offset = 0); |
Adrian Prantl | 00dbc2a | 2015-01-12 22:19:26 +0000 | [diff] [blame] | 124 | |
Adrian Prantl | 54286bd | 2016-11-02 16:12:20 +0000 | [diff] [blame] | 125 | /// Emit a partial DWARF register operation. |
Adrian Prantl | 172ab66 | 2015-01-13 23:11:07 +0000 | [diff] [blame] | 126 | /// \param MachineReg the register |
| 127 | /// \param PieceSizeInBits size and |
| 128 | /// \param PieceOffsetInBits offset of the piece in bits, if this is one |
| 129 | /// piece of an aggregate value. |
Adrian Prantl | b16d9eb | 2015-01-12 22:19:22 +0000 | [diff] [blame] | 130 | /// |
| 131 | /// If size and offset is zero an operation for the entire |
| 132 | /// register is emitted: Some targets do not provide a DWARF |
| 133 | /// register number for every register. If this is the case, this |
| 134 | /// function will attempt to emit a DWARF register by emitting a |
| 135 | /// piece of a super-register or by piecing together multiple |
| 136 | /// subregisters that alias the register. |
Adrian Prantl | ad768c3 | 2015-01-14 01:01:28 +0000 | [diff] [blame] | 137 | /// |
| 138 | /// \return false if no DWARF register exists for MachineReg. |
Peter Collingbourne | 96c9ae6 | 2016-05-20 19:35:17 +0000 | [diff] [blame] | 139 | bool AddMachineRegPiece(const TargetRegisterInfo &TRI, unsigned MachineReg, |
| 140 | unsigned PieceSizeInBits = 0, |
Adrian Prantl | b16d9eb | 2015-01-12 22:19:22 +0000 | [diff] [blame] | 141 | unsigned PieceOffsetInBits = 0); |
Adrian Prantl | 66f2595 | 2015-01-13 00:04:06 +0000 | [diff] [blame] | 142 | |
| 143 | /// Emit a signed constant. |
Adrian Prantl | 29ce701 | 2016-06-24 21:35:09 +0000 | [diff] [blame] | 144 | void AddSignedConstant(int64_t Value); |
Adrian Prantl | 66f2595 | 2015-01-13 00:04:06 +0000 | [diff] [blame] | 145 | /// Emit an unsigned constant. |
Adrian Prantl | 29ce701 | 2016-06-24 21:35:09 +0000 | [diff] [blame] | 146 | void AddUnsignedConstant(uint64_t Value); |
Adrian Prantl | 3e9c887 | 2016-04-08 00:38:37 +0000 | [diff] [blame] | 147 | /// Emit an unsigned constant. |
Benjamin Kramer | c321e53 | 2016-06-08 19:09:22 +0000 | [diff] [blame] | 148 | void AddUnsignedConstant(const APInt &Value); |
Adrian Prantl | 66f2595 | 2015-01-13 00:04:06 +0000 | [diff] [blame] | 149 | |
Adrian Prantl | 54286bd | 2016-11-02 16:12:20 +0000 | [diff] [blame] | 150 | /// Emit a machine register location while consuming the prefix of a |
| 151 | /// DwarfExpression. |
Duncan P. N. Exon Smith | 60635e3 | 2015-04-21 18:44:06 +0000 | [diff] [blame] | 152 | /// |
Adrian Prantl | 54286bd | 2016-11-02 16:12:20 +0000 | [diff] [blame] | 153 | /// \param PieceOffsetInBits If this is one piece out of a fragmented |
Adrian Prantl | 092d948 | 2015-01-13 23:39:11 +0000 | [diff] [blame] | 154 | /// location, this is the offset of the piece inside the entire variable. |
Adrian Prantl | ad768c3 | 2015-01-14 01:01:28 +0000 | [diff] [blame] | 155 | /// \return false if no DWARF register exists for MachineReg. |
Peter Collingbourne | 96c9ae6 | 2016-05-20 19:35:17 +0000 | [diff] [blame] | 156 | bool AddMachineRegExpression(const TargetRegisterInfo &TRI, |
Adrian Prantl | 54286bd | 2016-11-02 16:12:20 +0000 | [diff] [blame] | 157 | DIExpressionCursor &Expr, unsigned MachineReg, |
Adrian Prantl | 092d948 | 2015-01-13 23:39:11 +0000 | [diff] [blame] | 158 | unsigned PieceOffsetInBits = 0); |
Adrian Prantl | 54286bd | 2016-11-02 16:12:20 +0000 | [diff] [blame] | 159 | /// Emit all remaining operations in the DIExpressionCursor. |
| 160 | /// \param PieceOffsetInBits If this is one piece out of a fragmented |
Adrian Prantl | 092d948 | 2015-01-13 23:39:11 +0000 | [diff] [blame] | 161 | /// location, this is the offset of the piece inside the entire variable. |
Adrian Prantl | 54286bd | 2016-11-02 16:12:20 +0000 | [diff] [blame] | 162 | void AddExpression(DIExpressionCursor &&Expr, |
Duncan P. N. Exon Smith | 57bab0b | 2015-02-17 22:30:56 +0000 | [diff] [blame] | 163 | unsigned PieceOffsetInBits = 0); |
Adrian Prantl | 092d948 | 2015-01-13 23:39:11 +0000 | [diff] [blame] | 164 | }; |
Adrian Prantl | 66f2595 | 2015-01-13 00:04:06 +0000 | [diff] [blame] | 165 | |
| 166 | /// DwarfExpression implementation for .debug_loc entries. |
| 167 | class DebugLocDwarfExpression : public DwarfExpression { |
| 168 | ByteStreamer &BS; |
| 169 | |
| 170 | public: |
Peter Collingbourne | 96c9ae6 | 2016-05-20 19:35:17 +0000 | [diff] [blame] | 171 | DebugLocDwarfExpression(unsigned DwarfVersion, ByteStreamer &BS) |
| 172 | : DwarfExpression(DwarfVersion), BS(BS) {} |
Adrian Prantl | 66f2595 | 2015-01-13 00:04:06 +0000 | [diff] [blame] | 173 | |
Adrian Prantl | 172ab66 | 2015-01-13 23:11:07 +0000 | [diff] [blame] | 174 | void EmitOp(uint8_t Op, const char *Comment = nullptr) override; |
Adrian Prantl | 5123368 | 2015-03-10 19:23:37 +0000 | [diff] [blame] | 175 | void EmitSigned(int64_t Value) override; |
| 176 | void EmitUnsigned(uint64_t Value) override; |
Peter Collingbourne | 96c9ae6 | 2016-05-20 19:35:17 +0000 | [diff] [blame] | 177 | bool isFrameRegister(const TargetRegisterInfo &TRI, |
| 178 | unsigned MachineReg) override; |
Adrian Prantl | b16d9eb | 2015-01-12 22:19:22 +0000 | [diff] [blame] | 179 | }; |
Adrian Prantl | 658676c | 2015-01-14 01:01:22 +0000 | [diff] [blame] | 180 | |
| 181 | /// DwarfExpression implementation for singular DW_AT_location. |
| 182 | class DIEDwarfExpression : public DwarfExpression { |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 183 | const AsmPrinter &AP; |
Adrian Prantl | 658676c | 2015-01-14 01:01:22 +0000 | [diff] [blame] | 184 | DwarfUnit &DU; |
| 185 | DIELoc &DIE; |
| 186 | |
| 187 | public: |
Adrian Prantl | 92da14b | 2015-03-02 22:02:33 +0000 | [diff] [blame] | 188 | DIEDwarfExpression(const AsmPrinter &AP, DwarfUnit &DU, DIELoc &DIE); |
Adrian Prantl | 658676c | 2015-01-14 01:01:22 +0000 | [diff] [blame] | 189 | void EmitOp(uint8_t Op, const char *Comment = nullptr) override; |
Adrian Prantl | 5123368 | 2015-03-10 19:23:37 +0000 | [diff] [blame] | 190 | void EmitSigned(int64_t Value) override; |
| 191 | void EmitUnsigned(uint64_t Value) override; |
Peter Collingbourne | 96c9ae6 | 2016-05-20 19:35:17 +0000 | [diff] [blame] | 192 | bool isFrameRegister(const TargetRegisterInfo &TRI, |
| 193 | unsigned MachineReg) override; |
Adrian Prantl | 658676c | 2015-01-14 01:01:22 +0000 | [diff] [blame] | 194 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 195 | } |
Adrian Prantl | b16d9eb | 2015-01-12 22:19:22 +0000 | [diff] [blame] | 196 | |
| 197 | #endif |