blob: e8a8025add98ddc4fa1889a29acca48670115522 [file] [log] [blame]
Adrian Prantlb16d9eb2015-01-12 22:19:22 +00001//===-- 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 Prantl092d9482015-01-13 23:39:11 +000017#include "llvm/IR/DebugInfo.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000018#include "llvm/Support/DataTypes.h"
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000019
20namespace llvm {
21
Adrian Prantla4c30d62015-01-12 23:36:56 +000022class AsmPrinter;
Adrian Prantl66f25952015-01-13 00:04:06 +000023class ByteStreamer;
Adrian Prantla4c30d62015-01-12 23:36:56 +000024class TargetRegisterInfo;
Adrian Prantl658676c2015-01-14 01:01:22 +000025class DwarfUnit;
26class DIELoc;
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000027
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.
31class DwarfExpression {
Adrian Prantl00dbc2a2015-01-12 22:19:26 +000032protected:
Adrian Prantla4c30d62015-01-12 23:36:56 +000033 // Various convenience accessors that extract things out of AsmPrinter.
Adrian Prantl92da14b2015-03-02 22:02:33 +000034 unsigned DwarfVersion;
Adrian Prantla4c30d62015-01-12 23:36:56 +000035
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000036public:
Peter Collingbourne96c9ae62016-05-20 19:35:17 +000037 DwarfExpression(unsigned DwarfVersion) : DwarfVersion(DwarfVersion) {}
Adrian Prantl9cffbd82015-01-12 23:36:50 +000038 virtual ~DwarfExpression() {}
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000039
Adrian Prantl172ab662015-01-13 23:11:07 +000040 /// Output a dwarf operand and an optional assembler comment.
41 virtual void EmitOp(uint8_t Op, const char *Comment = nullptr) = 0;
42 /// Emit a raw signed value.
Adrian Prantl51233682015-03-10 19:23:37 +000043 virtual void EmitSigned(int64_t Value) = 0;
Adrian Prantl172ab662015-01-13 23:11:07 +000044 /// Emit a raw unsigned value.
Adrian Prantl51233682015-03-10 19:23:37 +000045 virtual void EmitUnsigned(uint64_t Value) = 0;
Adrian Prantl172ab662015-01-13 23:11:07 +000046 /// Return whether the given machine register is the frame register in the
47 /// current function.
Peter Collingbourne96c9ae62016-05-20 19:35:17 +000048 virtual bool isFrameRegister(const TargetRegisterInfo &TRI, unsigned MachineReg) = 0;
Adrian Prantl00dbc2a2015-01-12 22:19:26 +000049
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000050 /// Emit a dwarf register operation.
Adrian Prantl172ab662015-01-13 23:11:07 +000051 void AddReg(int DwarfReg, const char *Comment = nullptr);
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000052 /// Emit an (double-)indirect dwarf register operation.
53 void AddRegIndirect(int DwarfReg, int Offset, bool Deref = false);
54
55 /// Emit a dwarf register operation for describing
56 /// - a small value occupying only part of a register or
57 /// - a register representing only part of a value.
58 void AddOpPiece(unsigned SizeInBits, unsigned OffsetInBits = 0);
59 /// Emit a shift-right dwarf expression.
60 void AddShr(unsigned ShiftBy);
Adrian Prantl3e9c8872016-04-08 00:38:37 +000061 /// Emit a DW_OP_stack_value, if supported.
62 ///
63 /// The proper way to describe a constant value is
64 /// DW_OP_constu <const>, DW_OP_stack_value.
65 /// Unfortunately, DW_OP_stack_value was not available until DWARF-4,
66 /// so we will continue to generate DW_OP_constu <const> for DWARF-2
67 /// and DWARF-3. Technically, this is incorrect since DW_OP_const <const>
68 /// actually describes a value at a constant addess, not a constant value.
69 /// However, in the past there was no better way to describe a constant
70 /// value, so the producers and consumers started to rely on heuristics
71 /// to disambiguate the value vs. location status of the expression.
72 /// See PR21176 for more details.
73 void AddStackValue();
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000074
Adrian Prantl00dbc2a2015-01-12 22:19:26 +000075 /// Emit an indirect dwarf register operation for the given machine register.
Adrian Prantlad768c32015-01-14 01:01:28 +000076 /// \return false if no DWARF register exists for MachineReg.
Peter Collingbourne96c9ae62016-05-20 19:35:17 +000077 bool AddMachineRegIndirect(const TargetRegisterInfo &TRI, unsigned MachineReg,
78 int Offset = 0);
Adrian Prantl00dbc2a2015-01-12 22:19:26 +000079
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000080 /// \brief Emit a partial DWARF register operation.
Adrian Prantl172ab662015-01-13 23:11:07 +000081 /// \param MachineReg the register
82 /// \param PieceSizeInBits size and
83 /// \param PieceOffsetInBits offset of the piece in bits, if this is one
84 /// piece of an aggregate value.
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000085 ///
86 /// If size and offset is zero an operation for the entire
87 /// register is emitted: Some targets do not provide a DWARF
88 /// register number for every register. If this is the case, this
89 /// function will attempt to emit a DWARF register by emitting a
90 /// piece of a super-register or by piecing together multiple
91 /// subregisters that alias the register.
Adrian Prantlad768c32015-01-14 01:01:28 +000092 ///
93 /// \return false if no DWARF register exists for MachineReg.
Peter Collingbourne96c9ae62016-05-20 19:35:17 +000094 bool AddMachineRegPiece(const TargetRegisterInfo &TRI, unsigned MachineReg,
95 unsigned PieceSizeInBits = 0,
Adrian Prantlb16d9eb2015-01-12 22:19:22 +000096 unsigned PieceOffsetInBits = 0);
Adrian Prantl66f25952015-01-13 00:04:06 +000097
98 /// Emit a signed constant.
99 void AddSignedConstant(int Value);
100 /// Emit an unsigned constant.
101 void AddUnsignedConstant(unsigned Value);
Adrian Prantl3e9c8872016-04-08 00:38:37 +0000102 /// Emit an unsigned constant.
Benjamin Kramerc321e532016-06-08 19:09:22 +0000103 void AddUnsignedConstant(const APInt &Value);
Adrian Prantl66f25952015-01-13 00:04:06 +0000104
Duncan P. N. Exon Smith60635e32015-04-21 18:44:06 +0000105 /// \brief Emit an entire expression on top of a machine register location.
106 ///
Adrian Prantl092d9482015-01-13 23:39:11 +0000107 /// \param PieceOffsetInBits If this is one piece out of a fragmented
108 /// location, this is the offset of the piece inside the entire variable.
Adrian Prantlad768c32015-01-14 01:01:28 +0000109 /// \return false if no DWARF register exists for MachineReg.
Peter Collingbourne96c9ae62016-05-20 19:35:17 +0000110 bool AddMachineRegExpression(const TargetRegisterInfo &TRI,
111 const DIExpression *Expr, unsigned MachineReg,
Adrian Prantl092d9482015-01-13 23:39:11 +0000112 unsigned PieceOffsetInBits = 0);
Adrian Prantl531641a2015-01-22 00:00:59 +0000113 /// Emit a the operations remaining the DIExpressionIterator I.
Adrian Prantl092d9482015-01-13 23:39:11 +0000114 /// \param PieceOffsetInBits If this is one piece out of a fragmented
115 /// location, this is the offset of the piece inside the entire variable.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000116 void AddExpression(DIExpression::expr_op_iterator I,
117 DIExpression::expr_op_iterator E,
Duncan P. N. Exon Smith57bab0b2015-02-17 22:30:56 +0000118 unsigned PieceOffsetInBits = 0);
Adrian Prantl092d9482015-01-13 23:39:11 +0000119};
Adrian Prantl66f25952015-01-13 00:04:06 +0000120
121/// DwarfExpression implementation for .debug_loc entries.
122class DebugLocDwarfExpression : public DwarfExpression {
123 ByteStreamer &BS;
124
125public:
Peter Collingbourne96c9ae62016-05-20 19:35:17 +0000126 DebugLocDwarfExpression(unsigned DwarfVersion, ByteStreamer &BS)
127 : DwarfExpression(DwarfVersion), BS(BS) {}
Adrian Prantl66f25952015-01-13 00:04:06 +0000128
Adrian Prantl172ab662015-01-13 23:11:07 +0000129 void EmitOp(uint8_t Op, const char *Comment = nullptr) override;
Adrian Prantl51233682015-03-10 19:23:37 +0000130 void EmitSigned(int64_t Value) override;
131 void EmitUnsigned(uint64_t Value) override;
Peter Collingbourne96c9ae62016-05-20 19:35:17 +0000132 bool isFrameRegister(const TargetRegisterInfo &TRI,
133 unsigned MachineReg) override;
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000134};
Adrian Prantl658676c2015-01-14 01:01:22 +0000135
136/// DwarfExpression implementation for singular DW_AT_location.
137class DIEDwarfExpression : public DwarfExpression {
Adrian Prantl92da14b2015-03-02 22:02:33 +0000138const AsmPrinter &AP;
Adrian Prantl658676c2015-01-14 01:01:22 +0000139 DwarfUnit &DU;
140 DIELoc &DIE;
141
142public:
Adrian Prantl92da14b2015-03-02 22:02:33 +0000143 DIEDwarfExpression(const AsmPrinter &AP, DwarfUnit &DU, DIELoc &DIE);
Adrian Prantl658676c2015-01-14 01:01:22 +0000144 void EmitOp(uint8_t Op, const char *Comment = nullptr) override;
Adrian Prantl51233682015-03-10 19:23:37 +0000145 void EmitSigned(int64_t Value) override;
146 void EmitUnsigned(uint64_t Value) override;
Peter Collingbourne96c9ae62016-05-20 19:35:17 +0000147 bool isFrameRegister(const TargetRegisterInfo &TRI,
148 unsigned MachineReg) override;
Adrian Prantl658676c2015-01-14 01:01:22 +0000149};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000150}
Adrian Prantlb16d9eb2015-01-12 22:19:22 +0000151
152#endif