1 //===-- llvm/CodeGen/DwarfExpression.h - Dwarf Compile Unit ---*- C++ -*--===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains support for writing dwarf compile unit.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFEXPRESSION_H
15 #define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFEXPRESSION_H
17 #include "llvm/IR/DebugInfo.h"
18 #include "llvm/Support/DataTypes.h"
24 class TargetRegisterInfo;
28 /// Base class containing the logic for constructing DWARF expressions
29 /// independently of whether they are emitted into a DIE or into a .debug_loc
31 class DwarfExpression {
33 // Various convenience accessors that extract things out of AsmPrinter.
34 const TargetRegisterInfo &TRI;
35 unsigned DwarfVersion;
37 /// \brief Set to true if we want comments to be emitted. This is usually
38 /// only the case when the AsmPrinter is emitting to a text stream with
43 DwarfExpression(const TargetRegisterInfo &TRI,
44 unsigned DwarfVersion, bool PrintComments)
45 : TRI(TRI), DwarfVersion(DwarfVersion), PrintComments(PrintComments) {}
46 virtual ~DwarfExpression() {}
48 /// Output a dwarf operand and an optional assembler comment.
49 virtual void EmitOp(uint8_t Op, const char *Comment = nullptr) = 0;
50 /// Emit a raw signed value.
51 virtual void EmitSigned(int64_t Value) = 0;
52 /// Emit a raw unsigned value.
53 virtual void EmitUnsigned(uint64_t Value) = 0;
54 /// Return whether the given machine register is the frame register in the
56 virtual bool isFrameRegister(unsigned MachineReg) = 0;
58 /// Emit a dwarf register operation.
59 void AddReg(int DwarfReg, const char *Comment = nullptr);
60 /// Emit an (double-)indirect dwarf register operation.
61 void AddRegIndirect(int DwarfReg, int Offset, bool Deref = false);
63 /// Emit a dwarf register operation for describing
64 /// - a small value occupying only part of a register or
65 /// - a register representing only part of a value.
66 void AddOpPiece(unsigned SizeInBits, unsigned OffsetInBits = 0);
67 /// Emit a shift-right dwarf expression.
68 void AddShr(unsigned ShiftBy);
70 /// Emit an indirect dwarf register operation for the given machine register.
71 /// \return false if no DWARF register exists for MachineReg.
72 bool AddMachineRegIndirect(unsigned MachineReg, int Offset = 0);
74 /// \brief Emit a partial DWARF register operation.
75 /// \param MachineReg the register
76 /// \param PieceSizeInBits size and
77 /// \param PieceOffsetInBits offset of the piece in bits, if this is one
78 /// piece of an aggregate value.
80 /// If size and offset is zero an operation for the entire
81 /// register is emitted: Some targets do not provide a DWARF
82 /// register number for every register. If this is the case, this
83 /// function will attempt to emit a DWARF register by emitting a
84 /// piece of a super-register or by piecing together multiple
85 /// subregisters that alias the register.
87 /// \return false if no DWARF register exists for MachineReg.
88 bool AddMachineRegPiece(unsigned MachineReg, unsigned PieceSizeInBits = 0,
89 unsigned PieceOffsetInBits = 0);
91 /// Emit a signed constant.
92 void AddSignedConstant(int Value);
93 /// Emit an unsigned constant.
94 void AddUnsignedConstant(unsigned Value);
96 /// \brief Emit an entire expression on top of a machine register location.
98 /// \param PieceOffsetInBits If this is one piece out of a fragmented
99 /// location, this is the offset of the piece inside the entire variable.
100 /// \return false if no DWARF register exists for MachineReg.
101 bool AddMachineRegExpression(const DIExpression *Expr, unsigned MachineReg,
102 unsigned PieceOffsetInBits = 0);
103 /// Emit a the operations remaining the DIExpressionIterator I.
104 /// \param PieceOffsetInBits If this is one piece out of a fragmented
105 /// location, this is the offset of the piece inside the entire variable.
106 void AddExpression(DIExpression::expr_op_iterator I,
107 DIExpression::expr_op_iterator E,
108 unsigned PieceOffsetInBits = 0);
111 /// DwarfExpression implementation for .debug_loc entries.
112 class DebugLocDwarfExpression : public DwarfExpression {
116 DebugLocDwarfExpression(const TargetRegisterInfo &TRI,
117 unsigned DwarfVersion, bool PrintComments,
119 : DwarfExpression(TRI, DwarfVersion, PrintComments), BS(BS) {}
121 void EmitOp(uint8_t Op, const char *Comment = nullptr) override;
122 void EmitSigned(int64_t Value) override;
123 void EmitUnsigned(uint64_t Value) override;
124 bool isFrameRegister(unsigned MachineReg) override;
127 /// DwarfExpression implementation for singular DW_AT_location.
128 class DIEDwarfExpression : public DwarfExpression {
129 const AsmPrinter ≈
134 DIEDwarfExpression(const AsmPrinter &AP, DwarfUnit &DU, DIELoc &DIE);
135 void EmitOp(uint8_t Op, const char *Comment = nullptr) override;
136 void EmitSigned(int64_t Value) override;
137 void EmitUnsigned(uint64_t Value) override;
138 bool isFrameRegister(unsigned MachineReg) override;