15e29164d2fafbd17bbf8b26bcf92ab4c81d8878
[oota-llvm.git] / lib / CodeGen / AsmPrinter / DwarfExpression.h
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
17 #include "llvm/IR/DebugInfo.h"
18 #include "llvm/Support/DataTypes.h"
19
20 namespace llvm {
21
22 class AsmPrinter;
23 class ByteStreamer;
24 class TargetRegisterInfo;
25 class DwarfUnit;
26 class DIELoc;
27
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
30 /// entry.
31 class DwarfExpression {
32 protected:
33   // Various convenience accessors that extract things out of AsmPrinter.
34   const TargetRegisterInfo &TRI;
35   unsigned DwarfVersion;
36
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
39   /// comments enabled.
40   bool PrintComments;
41
42 public:
43   DwarfExpression(const TargetRegisterInfo &TRI,
44                   unsigned DwarfVersion, bool PrintComments)
45     : TRI(TRI), DwarfVersion(DwarfVersion), PrintComments(PrintComments) {}
46   virtual ~DwarfExpression() {}
47
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
55   /// current function.
56   virtual bool isFrameRegister(unsigned MachineReg) = 0;
57
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);
62
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);
69
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);
73
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.
79   ///
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.
86   ///
87   /// \return false if no DWARF register exists for MachineReg.
88   bool AddMachineRegPiece(unsigned MachineReg, unsigned PieceSizeInBits = 0,
89                           unsigned PieceOffsetInBits = 0);
90
91   /// Emit a signed constant.
92   void AddSignedConstant(int Value);
93   /// Emit an unsigned constant.
94   void AddUnsignedConstant(unsigned Value);
95
96   /// \brief Emit an entire expression on top of a machine register location.
97   ///
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);
109 };
110
111 /// DwarfExpression implementation for .debug_loc entries.
112 class DebugLocDwarfExpression : public DwarfExpression {
113   ByteStreamer &BS;
114
115 public:
116   DebugLocDwarfExpression(const TargetRegisterInfo &TRI,
117                           unsigned DwarfVersion, bool PrintComments,
118                           ByteStreamer &BS)
119     : DwarfExpression(TRI, DwarfVersion, PrintComments), BS(BS) {}
120
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;
125 };
126
127 /// DwarfExpression implementation for singular DW_AT_location.
128 class DIEDwarfExpression : public DwarfExpression {
129 const AsmPrinter ≈
130   DwarfUnit &DU;
131   DIELoc ¨
132
133 public:
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;
139 };
140 }
141
142 #endif