2017-08-17 21:26:39 +00:00
|
|
|
//===- llvm/CodeGen/DwarfExpression.h - Dwarf Compile Unit ------*- C++ -*-===//
|
2015-01-12 22:19:22 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains support for writing dwarf compile unit.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DWARFEXPRESSION_H
|
|
|
|
#define LLVM_LIB_CODEGEN_ASMPRINTER_DWARFEXPRESSION_H
|
|
|
|
|
2017-08-17 21:26:39 +00:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
#include "llvm/ADT/None.h"
|
|
|
|
#include "llvm/ADT/Optional.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/IR/DebugInfoMetadata.h"
|
|
|
|
#include <cassert>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <iterator>
|
2015-01-12 22:19:22 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2015-01-12 23:36:56 +00:00
|
|
|
class AsmPrinter;
|
2017-08-17 21:26:39 +00:00
|
|
|
class APInt;
|
2015-01-13 00:04:06 +00:00
|
|
|
class ByteStreamer;
|
2015-01-14 01:01:22 +00:00
|
|
|
class DwarfUnit;
|
|
|
|
class DIELoc;
|
2017-08-17 21:26:39 +00:00
|
|
|
class TargetRegisterInfo;
|
2015-01-12 22:19:22 +00:00
|
|
|
|
2016-11-02 16:12:20 +00:00
|
|
|
/// Holds a DIExpression and keeps track of how many operands have been consumed
|
|
|
|
/// so far.
|
|
|
|
class DIExpressionCursor {
|
|
|
|
DIExpression::expr_op_iterator Start, End;
|
2017-08-17 21:26:39 +00:00
|
|
|
|
2016-11-02 16:12:20 +00:00
|
|
|
public:
|
|
|
|
DIExpressionCursor(const DIExpression *Expr) {
|
|
|
|
if (!Expr) {
|
|
|
|
assert(Start == End);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Start = Expr->expr_op_begin();
|
|
|
|
End = Expr->expr_op_end();
|
|
|
|
}
|
|
|
|
|
2016-12-09 20:43:40 +00:00
|
|
|
DIExpressionCursor(ArrayRef<uint64_t> Expr)
|
|
|
|
: Start(Expr.begin()), End(Expr.end()) {}
|
|
|
|
|
2017-08-17 21:26:39 +00:00
|
|
|
DIExpressionCursor(const DIExpressionCursor &) = default;
|
2017-06-14 13:14:38 +00:00
|
|
|
|
2016-11-02 16:12:20 +00:00
|
|
|
/// Consume one operation.
|
|
|
|
Optional<DIExpression::ExprOperand> take() {
|
|
|
|
if (Start == End)
|
|
|
|
return None;
|
|
|
|
return *(Start++);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Consume N operations.
|
|
|
|
void consume(unsigned N) { std::advance(Start, N); }
|
|
|
|
|
|
|
|
/// Return the current operation.
|
|
|
|
Optional<DIExpression::ExprOperand> peek() const {
|
|
|
|
if (Start == End)
|
|
|
|
return None;
|
|
|
|
return *(Start);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return the next operation.
|
|
|
|
Optional<DIExpression::ExprOperand> peekNext() const {
|
|
|
|
if (Start == End)
|
|
|
|
return None;
|
|
|
|
|
|
|
|
auto Next = Start.getNext();
|
|
|
|
if (Next == End)
|
|
|
|
return None;
|
|
|
|
|
|
|
|
return *Next;
|
|
|
|
}
|
2017-08-17 21:26:39 +00:00
|
|
|
|
2016-11-02 16:20:37 +00:00
|
|
|
/// Determine whether there are any operations left in this expression.
|
2016-11-02 16:12:20 +00:00
|
|
|
operator bool() const { return Start != End; }
|
2017-08-17 21:26:39 +00:00
|
|
|
|
2017-04-20 20:42:33 +00:00
|
|
|
DIExpression::expr_op_iterator begin() const { return Start; }
|
|
|
|
DIExpression::expr_op_iterator end() const { return End; }
|
2016-12-22 06:10:41 +00:00
|
|
|
|
|
|
|
/// Retrieve the fragment information, if any.
|
|
|
|
Optional<DIExpression::FragmentInfo> getFragmentInfo() const {
|
|
|
|
return DIExpression::getFragmentInfo(Start, End);
|
|
|
|
}
|
2016-11-02 16:12:20 +00:00
|
|
|
};
|
|
|
|
|
2015-01-12 22:19:22 +00:00
|
|
|
/// Base class containing the logic for constructing DWARF expressions
|
|
|
|
/// independently of whether they are emitted into a DIE or into a .debug_loc
|
|
|
|
/// entry.
|
|
|
|
class DwarfExpression {
|
2015-01-12 22:19:26 +00:00
|
|
|
protected:
|
2017-03-22 01:15:57 +00:00
|
|
|
/// Holds information about all subregisters comprising a register location.
|
|
|
|
struct Register {
|
|
|
|
int DwarfRegNo;
|
|
|
|
unsigned Size;
|
|
|
|
const char *Comment;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// The register location, if any.
|
|
|
|
SmallVector<Register, 2> DwarfRegs;
|
|
|
|
|
2016-12-09 20:43:40 +00:00
|
|
|
/// Current Fragment Offset in Bits.
|
|
|
|
uint64_t OffsetInBits = 0;
|
2017-03-16 17:42:47 +00:00
|
|
|
unsigned DwarfVersion;
|
2016-12-09 20:43:40 +00:00
|
|
|
|
|
|
|
/// Sometimes we need to add a DW_OP_bit_piece to describe a subregister.
|
|
|
|
unsigned SubRegisterSizeInBits = 0;
|
|
|
|
unsigned SubRegisterOffsetInBits = 0;
|
|
|
|
|
2017-04-18 01:21:53 +00:00
|
|
|
/// The kind of location description being produced.
|
|
|
|
enum { Unknown = 0, Register, Memory, Implicit } LocationKind = Unknown;
|
|
|
|
|
2016-12-09 20:43:40 +00:00
|
|
|
/// Push a DW_OP_piece / DW_OP_bit_piece for emitting later, if one is needed
|
|
|
|
/// to represent a subregister.
|
|
|
|
void setSubRegisterPiece(unsigned SizeInBits, unsigned OffsetInBits) {
|
|
|
|
SubRegisterSizeInBits = SizeInBits;
|
|
|
|
SubRegisterOffsetInBits = OffsetInBits;
|
|
|
|
}
|
2015-01-12 23:36:56 +00:00
|
|
|
|
2017-03-16 17:14:56 +00:00
|
|
|
/// Add masking operations to stencil out a subregister.
|
|
|
|
void maskSubRegister();
|
|
|
|
|
2015-01-13 23:11:07 +00:00
|
|
|
/// Output a dwarf operand and an optional assembler comment.
|
2017-03-16 17:42:45 +00:00
|
|
|
virtual void emitOp(uint8_t Op, const char *Comment = nullptr) = 0;
|
2017-08-17 21:26:39 +00:00
|
|
|
|
2015-01-13 23:11:07 +00:00
|
|
|
/// Emit a raw signed value.
|
2017-03-16 17:42:45 +00:00
|
|
|
virtual void emitSigned(int64_t Value) = 0;
|
2017-08-17 21:26:39 +00:00
|
|
|
|
2015-01-13 23:11:07 +00:00
|
|
|
/// Emit a raw unsigned value.
|
2017-03-16 17:42:45 +00:00
|
|
|
virtual void emitUnsigned(uint64_t Value) = 0;
|
2017-08-17 21:26:39 +00:00
|
|
|
|
2015-01-13 23:11:07 +00:00
|
|
|
/// Return whether the given machine register is the frame register in the
|
|
|
|
/// current function.
|
2016-05-20 19:35:17 +00:00
|
|
|
virtual bool isFrameRegister(const TargetRegisterInfo &TRI, unsigned MachineReg) = 0;
|
2015-01-12 22:19:26 +00:00
|
|
|
|
2017-04-18 01:21:53 +00:00
|
|
|
/// Emit a DW_OP_reg operation. Note that this is only legal inside a DWARF
|
|
|
|
/// register location description.
|
2017-03-16 17:42:45 +00:00
|
|
|
void addReg(int DwarfReg, const char *Comment = nullptr);
|
2017-08-17 21:26:39 +00:00
|
|
|
|
2017-03-22 17:19:55 +00:00
|
|
|
/// Emit a DW_OP_breg operation.
|
|
|
|
void addBReg(int DwarfReg, int Offset);
|
2017-08-17 21:26:39 +00:00
|
|
|
|
2017-03-22 01:15:57 +00:00
|
|
|
/// Emit DW_OP_fbreg <Offset>.
|
|
|
|
void addFBReg(int Offset);
|
2017-03-20 21:35:09 +00:00
|
|
|
|
|
|
|
/// Emit a partial DWARF register operation.
|
|
|
|
///
|
|
|
|
/// \param MachineReg The register number.
|
|
|
|
/// \param MaxSize If the register must be composed from
|
|
|
|
/// sub-registers this is an upper bound
|
|
|
|
/// for how many bits the emitted DW_OP_piece
|
|
|
|
/// may cover.
|
|
|
|
///
|
|
|
|
/// If size and offset is zero an operation for the entire register is
|
|
|
|
/// emitted: Some targets do not provide a DWARF register number for every
|
|
|
|
/// register. If this is the case, this function will attempt to emit a DWARF
|
|
|
|
/// register by emitting a fragment of a super-register or by piecing together
|
|
|
|
/// multiple subregisters that alias the register.
|
|
|
|
///
|
|
|
|
/// \return false if no DWARF register exists for MachineReg.
|
|
|
|
bool addMachineReg(const TargetRegisterInfo &TRI, unsigned MachineReg,
|
|
|
|
unsigned MaxSize = ~1U);
|
|
|
|
|
2016-12-09 20:43:40 +00:00
|
|
|
/// Emit a DW_OP_piece or DW_OP_bit_piece operation for a variable fragment.
|
|
|
|
/// \param OffsetInBits This is an optional offset into the location that
|
|
|
|
/// is at the top of the DWARF stack.
|
2017-03-16 17:42:45 +00:00
|
|
|
void addOpPiece(unsigned SizeInBits, unsigned OffsetInBits = 0);
|
2016-12-05 18:04:47 +00:00
|
|
|
|
2017-03-16 17:14:56 +00:00
|
|
|
/// Emit a shift-right dwarf operation.
|
2017-03-16 17:42:45 +00:00
|
|
|
void addShr(unsigned ShiftBy);
|
2017-08-17 21:26:39 +00:00
|
|
|
|
2017-03-16 17:14:56 +00:00
|
|
|
/// Emit a bitwise and dwarf operation.
|
2017-03-16 17:42:45 +00:00
|
|
|
void addAnd(unsigned Mask);
|
2016-12-05 18:04:47 +00:00
|
|
|
|
2016-04-08 00:38:37 +00:00
|
|
|
/// Emit a DW_OP_stack_value, if supported.
|
|
|
|
///
|
2016-11-02 16:20:37 +00:00
|
|
|
/// The proper way to describe a constant value is DW_OP_constu <const>,
|
|
|
|
/// DW_OP_stack_value. Unfortunately, DW_OP_stack_value was not available
|
|
|
|
/// until DWARF 4, so we will continue to generate DW_OP_constu <const> for
|
|
|
|
/// DWARF 2 and DWARF 3. Technically, this is incorrect since DW_OP_const
|
|
|
|
/// <const> actually describes a value at a constant addess, not a constant
|
|
|
|
/// value. However, in the past there was no better way to describe a
|
|
|
|
/// constant value, so the producers and consumers started to rely on
|
|
|
|
/// heuristics to disambiguate the value vs. location status of the
|
|
|
|
/// expression. See PR21176 for more details.
|
2017-03-16 17:42:45 +00:00
|
|
|
void addStackValue();
|
2015-01-12 22:19:22 +00:00
|
|
|
|
2017-03-27 17:34:04 +00:00
|
|
|
~DwarfExpression() = default;
|
2017-08-17 21:26:39 +00:00
|
|
|
|
2017-03-20 21:34:19 +00:00
|
|
|
public:
|
|
|
|
DwarfExpression(unsigned DwarfVersion) : DwarfVersion(DwarfVersion) {}
|
|
|
|
|
|
|
|
/// This needs to be called last to commit any pending changes.
|
|
|
|
void finalize();
|
|
|
|
|
2015-01-13 00:04:06 +00:00
|
|
|
/// Emit a signed constant.
|
2017-03-16 17:42:45 +00:00
|
|
|
void addSignedConstant(int64_t Value);
|
2017-08-17 21:26:39 +00:00
|
|
|
|
2015-01-13 00:04:06 +00:00
|
|
|
/// Emit an unsigned constant.
|
2017-03-16 17:42:45 +00:00
|
|
|
void addUnsignedConstant(uint64_t Value);
|
2017-08-17 21:26:39 +00:00
|
|
|
|
2016-04-08 00:38:37 +00:00
|
|
|
/// Emit an unsigned constant.
|
2017-03-16 17:42:45 +00:00
|
|
|
void addUnsignedConstant(const APInt &Value);
|
2015-01-13 00:04:06 +00:00
|
|
|
|
2017-04-19 23:42:25 +00:00
|
|
|
/// Lock this down to become a memory location description.
|
|
|
|
void setMemoryLocationKind() {
|
|
|
|
assert(LocationKind == Unknown);
|
|
|
|
LocationKind = Memory;
|
|
|
|
}
|
|
|
|
|
2016-11-02 16:20:37 +00:00
|
|
|
/// Emit a machine register location. As an optimization this may also consume
|
|
|
|
/// the prefix of a DwarfExpression if a more efficient representation for
|
|
|
|
/// combining the register location and the first operation exists.
|
2015-04-21 18:44:06 +00:00
|
|
|
///
|
2017-04-19 23:42:25 +00:00
|
|
|
/// \param FragmentOffsetInBits If this is one fragment out of a
|
|
|
|
/// fragmented
|
2016-12-05 18:04:47 +00:00
|
|
|
/// location, this is the offset of the
|
|
|
|
/// fragment inside the entire variable.
|
|
|
|
/// \return false if no DWARF register exists
|
|
|
|
/// for MachineReg.
|
2017-04-19 23:42:25 +00:00
|
|
|
bool addMachineRegExpression(const TargetRegisterInfo &TRI,
|
|
|
|
DIExpressionCursor &Expr, unsigned MachineReg,
|
2016-12-05 18:04:47 +00:00
|
|
|
unsigned FragmentOffsetInBits = 0);
|
2017-08-17 21:26:39 +00:00
|
|
|
|
2016-11-02 16:12:20 +00:00
|
|
|
/// Emit all remaining operations in the DIExpressionCursor.
|
2016-12-05 18:04:47 +00:00
|
|
|
///
|
|
|
|
/// \param FragmentOffsetInBits If this is one fragment out of multiple
|
|
|
|
/// locations, this is the offset of the
|
|
|
|
/// fragment inside the entire variable.
|
2017-03-16 17:42:45 +00:00
|
|
|
void addExpression(DIExpressionCursor &&Expr,
|
2016-12-05 18:04:47 +00:00
|
|
|
unsigned FragmentOffsetInBits = 0);
|
2016-12-09 20:43:40 +00:00
|
|
|
|
|
|
|
/// If applicable, emit an empty DW_OP_piece / DW_OP_bit_piece to advance to
|
|
|
|
/// the fragment described by \c Expr.
|
|
|
|
void addFragmentOffset(const DIExpression *Expr);
|
Debug Info: Move the complex expression handling (=the remainder) of
emitDebugLocValue() into DwarfExpression.
Ought to be NFC, but it actually uncovered a bug in the debug-loc-asan.ll
testcase. The testcase checks that the address of variable "y" is stored
at [RSP+16], which also lines up with the comment.
It also check(ed) that the *value* of "y" is stored in RDI before that,
but that is actually incorrect, since RDI is the very value that is
stored in [RSP+16]. Here's the assembler output:
movb 2147450880(%rcx), %r8b
#DEBUG_VALUE: bar:y <- RDI
cmpb $0, %r8b
movq %rax, 32(%rsp) # 8-byte Spill
movq %rsi, 24(%rsp) # 8-byte Spill
movq %rdi, 16(%rsp) # 8-byte Spill
.Ltmp3:
#DEBUG_VALUE: bar:y <- [RSP+16]
Fixed the comment to spell out the correct register and the check to
expect an address rather than a value.
Note that the range that is emitted for the RDI location was and is still
wrong, it claims to begin at the function prologue, but really it should
start where RDI is first assigned.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@225851 91177308-0d34-0410-b5e6-96231b3b80d8
2015-01-13 23:39:11 +00:00
|
|
|
};
|
2015-01-13 00:04:06 +00:00
|
|
|
|
|
|
|
/// DwarfExpression implementation for .debug_loc entries.
|
2017-03-27 17:34:04 +00:00
|
|
|
class DebugLocDwarfExpression final : public DwarfExpression {
|
2015-01-13 00:04:06 +00:00
|
|
|
ByteStreamer &BS;
|
|
|
|
|
2017-03-16 17:42:45 +00:00
|
|
|
void emitOp(uint8_t Op, const char *Comment = nullptr) override;
|
|
|
|
void emitSigned(int64_t Value) override;
|
|
|
|
void emitUnsigned(uint64_t Value) override;
|
2016-05-20 19:35:17 +00:00
|
|
|
bool isFrameRegister(const TargetRegisterInfo &TRI,
|
|
|
|
unsigned MachineReg) override;
|
2017-08-17 21:26:39 +00:00
|
|
|
|
2017-03-20 21:34:19 +00:00
|
|
|
public:
|
|
|
|
DebugLocDwarfExpression(unsigned DwarfVersion, ByteStreamer &BS)
|
|
|
|
: DwarfExpression(DwarfVersion), BS(BS) {}
|
2015-01-12 22:19:22 +00:00
|
|
|
};
|
2015-01-14 01:01:22 +00:00
|
|
|
|
|
|
|
/// DwarfExpression implementation for singular DW_AT_location.
|
2017-03-27 17:34:04 +00:00
|
|
|
class DIEDwarfExpression final : public DwarfExpression {
|
2015-03-02 22:02:33 +00:00
|
|
|
const AsmPrinter &AP;
|
2015-01-14 01:01:22 +00:00
|
|
|
DwarfUnit &DU;
|
|
|
|
DIELoc &DIE;
|
|
|
|
|
2017-03-16 17:42:45 +00:00
|
|
|
void emitOp(uint8_t Op, const char *Comment = nullptr) override;
|
|
|
|
void emitSigned(int64_t Value) override;
|
|
|
|
void emitUnsigned(uint64_t Value) override;
|
2016-05-20 19:35:17 +00:00
|
|
|
bool isFrameRegister(const TargetRegisterInfo &TRI,
|
|
|
|
unsigned MachineReg) override;
|
2017-03-20 21:34:19 +00:00
|
|
|
public:
|
|
|
|
DIEDwarfExpression(const AsmPrinter &AP, DwarfUnit &DU, DIELoc &DIE);
|
2017-08-17 21:26:39 +00:00
|
|
|
|
2016-12-20 02:09:43 +00:00
|
|
|
DIELoc *finalize() {
|
|
|
|
DwarfExpression::finalize();
|
|
|
|
return &DIE;
|
|
|
|
}
|
2015-01-14 01:01:22 +00:00
|
|
|
};
|
2015-01-12 22:19:22 +00:00
|
|
|
|
2017-08-17 21:26:39 +00:00
|
|
|
} // end namespace llvm
|
|
|
|
|
|
|
|
#endif // LLVM_LIB_CODEGEN_ASMPRINTER_DWARFEXPRESSION_H
|