blob: 8cfe39d2ca3fe089c5647423b2aad3c83f002b3b [file] [log] [blame]
David Blaikie1275e4f2014-04-01 21:49:04 +00001//===-- llvm/CodeGen/DebugLocEntry.h - Entry in debug_loc list -*- 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#ifndef CODEGEN_ASMPRINTER_DEBUGLOCENTRY_H__
11#define CODEGEN_ASMPRINTER_DEBUGLOCENTRY_H__
12#include "llvm/IR/Constants.h"
Adrian Prantlb1416832014-08-01 22:11:58 +000013#include "llvm/IR/DebugInfo.h"
David Blaikie1275e4f2014-04-01 21:49:04 +000014#include "llvm/MC/MachineLocation.h"
15#include "llvm/MC/MCSymbol.h"
16
17namespace llvm {
18class DwarfCompileUnit;
19class MDNode;
20/// \brief This struct describes location entries emitted in the .debug_loc
21/// section.
22class DebugLocEntry {
23 // Begin and end symbols for the address range that this location is valid.
24 const MCSymbol *Begin;
25 const MCSymbol *End;
26
Adrian Prantle19e5ef2014-04-27 18:25:40 +000027public:
28 /// A single location or constant.
29 struct Value {
30 Value(const MDNode *Var, int64_t i)
31 : Variable(Var), EntryKind(E_Integer) {
32 Constant.Int = i;
33 }
34 Value(const MDNode *Var, const ConstantFP *CFP)
35 : Variable(Var), EntryKind(E_ConstantFP) {
36 Constant.CFP = CFP;
37 }
38 Value(const MDNode *Var, const ConstantInt *CIP)
39 : Variable(Var), EntryKind(E_ConstantInt) {
40 Constant.CIP = CIP;
41 }
42 Value(const MDNode *Var, MachineLocation Loc)
43 : Variable(Var), EntryKind(E_Location), Loc(Loc) {
David Blaikied306baf2014-04-01 22:04:07 +000044 }
45
Adrian Prantle19e5ef2014-04-27 18:25:40 +000046 // The variable to which this location entry corresponds.
47 const MDNode *Variable;
48
49 // Type of entry that this represents.
50 enum EntryType { E_Location, E_Integer, E_ConstantFP, E_ConstantInt };
51 enum EntryType EntryKind;
52
53 // Either a constant,
54 union {
55 int64_t Int;
56 const ConstantFP *CFP;
57 const ConstantInt *CIP;
58 } Constant;
59
60 // Or a location in the machine frame.
61 MachineLocation Loc;
62
63 bool operator==(const Value &other) const {
64 if (EntryKind != other.EntryKind)
65 return false;
66
67 switch (EntryKind) {
68 case E_Location:
69 return Loc == other.Loc;
70 case E_Integer:
71 return Constant.Int == other.Constant.Int;
72 case E_ConstantFP:
73 return Constant.CFP == other.Constant.CFP;
74 case E_ConstantInt:
75 return Constant.CIP == other.Constant.CIP;
76 }
77 llvm_unreachable("unhandled EntryKind");
78 }
79
80 bool isLocation() const { return EntryKind == E_Location; }
81 bool isInt() const { return EntryKind == E_Integer; }
82 bool isConstantFP() const { return EntryKind == E_ConstantFP; }
83 bool isConstantInt() const { return EntryKind == E_ConstantInt; }
84 int64_t getInt() const { return Constant.Int; }
85 const ConstantFP *getConstantFP() const { return Constant.CFP; }
86 const ConstantInt *getConstantInt() const { return Constant.CIP; }
87 MachineLocation getLoc() const { return Loc; }
88 const MDNode *getVariable() const { return Variable; }
89 };
90private:
91 /// A list of locations/constants belonging to this entry.
92 SmallVector<Value, 1> Values;
93
94 /// The compile unit that this location entry is referenced by.
95 const DwarfCompileUnit *Unit;
David Blaikied306baf2014-04-01 22:04:07 +000096
David Blaikie1275e4f2014-04-01 21:49:04 +000097public:
Craig Toppere73658d2014-04-28 04:05:08 +000098 DebugLocEntry() : Begin(nullptr), End(nullptr), Unit(nullptr) {}
Adrian Prantle19e5ef2014-04-27 18:25:40 +000099 DebugLocEntry(const MCSymbol *B, const MCSymbol *E,
100 Value Val, const DwarfCompileUnit *U)
101 : Begin(B), End(E), Unit(U) {
102 Values.push_back(std::move(Val));
David Blaikie1275e4f2014-04-01 21:49:04 +0000103 }
104
Adrian Prantl3c5453c2014-04-01 23:34:45 +0000105 /// \brief Attempt to merge this DebugLocEntry with Next and return
106 /// true if the merge was successful. Entries can be merged if they
Adrian Prantla731cf02014-04-02 15:49:45 +0000107 /// share the same Loc/Constant and if Next immediately follows this
108 /// Entry.
David Blaikie1275e4f2014-04-01 21:49:04 +0000109 bool Merge(const DebugLocEntry &Next) {
Adrian Prantlb1416832014-08-01 22:11:58 +0000110 // If this and Next are describing different pieces of the same
111 // variable, merge them by appending next's values to the current
112 // list of values.
113 if (Begin == Next.Begin && Values.size() > 0 && Next.Values.size() > 0) {
114 DIVariable Var(Values[0].Variable);
115 DIVariable NextVar(Next.Values[0].Variable);
116 if (Var.getName() == NextVar.getName() &&
117 Var.isVariablePiece() && NextVar.isVariablePiece()) {
118 Values.append(Next.Values.begin(), Next.Values.end());
119 End = Next.End;
120 return true;
121 }
122 }
123 // If this and Next are describing the same variable, merge them.
Adrian Prantle19e5ef2014-04-27 18:25:40 +0000124 if ((End == Next.Begin && Values == Next.Values)) {
David Blaikie6fa99662014-04-01 23:19:23 +0000125 End = Next.End;
126 return true;
127 }
128 return false;
David Blaikie1275e4f2014-04-01 21:49:04 +0000129 }
Adrian Prantlb1416832014-08-01 22:11:58 +0000130
David Blaikie1275e4f2014-04-01 21:49:04 +0000131 const MCSymbol *getBeginSym() const { return Begin; }
132 const MCSymbol *getEndSym() const { return End; }
133 const DwarfCompileUnit *getCU() const { return Unit; }
Adrian Prantle19e5ef2014-04-27 18:25:40 +0000134 const ArrayRef<Value> getValues() const { return Values; }
Adrian Prantlb1416832014-08-01 22:11:58 +0000135 void addValue(Value Val) {
136 assert(DIVariable(Val.Variable).isVariablePiece() &&
137 "multi-value DebugLocEntries must be pieces");
138 Values.push_back(Val);
139 }
David Blaikie1275e4f2014-04-01 21:49:04 +0000140};
141
142}
143#endif