MCDwarf.h revision 40a7dbbeff44c4cbd8c7e4f07f28dd614f8a5d08
1//===- MCDwarf.h - Machine Code Dwarf support -------------------*- 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 the declaration of the MCDwarfFile to support the dwarf
11// .file directive and the .loc directive.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_MC_MCDWARF_H
16#define LLVM_MC_MCDWARF_H
17
18#include "llvm/ADT/StringRef.h"
19#include "llvm/CodeGen/MachineLocation.h" // FIXME
20#include "llvm/MC/MCObjectWriter.h"
21#include "llvm/Support/raw_ostream.h"
22#include "llvm/Support/Dwarf.h"
23#include <vector>
24
25namespace llvm {
26  class TargetAsmInfo;
27  class MachineMove;
28  class MCContext;
29  class MCExpr;
30  class MCSection;
31  class MCSectionData;
32  class MCStreamer;
33  class MCSymbol;
34  class MCObjectStreamer;
35  class raw_ostream;
36
37  /// MCDwarfFile - Instances of this class represent the name of the dwarf
38  /// .file directive and its associated dwarf file number in the MC file,
39  /// and MCDwarfFile's are created and unique'd by the MCContext class where
40  /// the file number for each is its index into the vector of DwarfFiles (note
41  /// index 0 is not used and not a valid dwarf file number).
42  class MCDwarfFile {
43    // Name - the base name of the file without its directory path.
44    // The StringRef references memory allocated in the MCContext.
45    StringRef Name;
46
47    // DirIndex - the index into the list of directory names for this file name.
48    unsigned DirIndex;
49
50  private:  // MCContext creates and uniques these.
51    friend class MCContext;
52    MCDwarfFile(StringRef name, unsigned dirIndex)
53      : Name(name), DirIndex(dirIndex) {}
54
55    MCDwarfFile(const MCDwarfFile&);       // DO NOT IMPLEMENT
56    void operator=(const MCDwarfFile&); // DO NOT IMPLEMENT
57  public:
58    /// getName - Get the base name of this MCDwarfFile.
59    StringRef getName() const { return Name; }
60
61    /// getDirIndex - Get the dirIndex of this MCDwarfFile.
62    unsigned getDirIndex() const { return DirIndex; }
63
64
65    /// print - Print the value to the stream \arg OS.
66    void print(raw_ostream &OS) const;
67
68    /// dump - Print the value to stderr.
69    void dump() const;
70  };
71
72  inline raw_ostream &operator<<(raw_ostream &OS, const MCDwarfFile &DwarfFile){
73    DwarfFile.print(OS);
74    return OS;
75  }
76
77  /// MCDwarfLoc - Instances of this class represent the information from a
78  /// dwarf .loc directive.
79  class MCDwarfLoc {
80    // FileNum - the file number.
81    unsigned FileNum;
82    // Line - the line number.
83    unsigned Line;
84    // Column - the column position.
85    unsigned Column;
86    // Flags (see #define's below)
87    unsigned Flags;
88    // Isa
89    unsigned Isa;
90    // Discriminator
91    unsigned Discriminator;
92
93// Flag that indicates the initial value of the is_stmt_start flag.
94#define DWARF2_LINE_DEFAULT_IS_STMT     1
95
96#define DWARF2_FLAG_IS_STMT        (1 << 0)
97#define DWARF2_FLAG_BASIC_BLOCK    (1 << 1)
98#define DWARF2_FLAG_PROLOGUE_END   (1 << 2)
99#define DWARF2_FLAG_EPILOGUE_BEGIN (1 << 3)
100
101  private:  // MCContext manages these
102    friend class MCContext;
103    friend class MCLineEntry;
104    MCDwarfLoc(unsigned fileNum, unsigned line, unsigned column, unsigned flags,
105               unsigned isa, unsigned discriminator)
106      : FileNum(fileNum), Line(line), Column(column), Flags(flags), Isa(isa),
107        Discriminator(discriminator) {}
108
109    // Allow the default copy constructor and assignment operator to be used
110    // for an MCDwarfLoc object.
111
112  public:
113    /// getFileNum - Get the FileNum of this MCDwarfLoc.
114    unsigned getFileNum() const { return FileNum; }
115
116    /// getLine - Get the Line of this MCDwarfLoc.
117    unsigned getLine() const { return Line; }
118
119    /// getColumn - Get the Column of this MCDwarfLoc.
120    unsigned getColumn() const { return Column; }
121
122    /// getFlags - Get the Flags of this MCDwarfLoc.
123    unsigned getFlags() const { return Flags; }
124
125    /// getIsa - Get the Isa of this MCDwarfLoc.
126    unsigned getIsa() const { return Isa; }
127
128    /// getDiscriminator - Get the Discriminator of this MCDwarfLoc.
129    unsigned getDiscriminator() const { return Discriminator; }
130
131    /// setFileNum - Set the FileNum of this MCDwarfLoc.
132    void setFileNum(unsigned fileNum) { FileNum = fileNum; }
133
134    /// setLine - Set the Line of this MCDwarfLoc.
135    void setLine(unsigned line) { Line = line; }
136
137    /// setColumn - Set the Column of this MCDwarfLoc.
138    void setColumn(unsigned column) { Column = column; }
139
140    /// setFlags - Set the Flags of this MCDwarfLoc.
141    void setFlags(unsigned flags) { Flags = flags; }
142
143    /// setIsa - Set the Isa of this MCDwarfLoc.
144    void setIsa(unsigned isa) { Isa = isa; }
145
146    /// setDiscriminator - Set the Discriminator of this MCDwarfLoc.
147    void setDiscriminator(unsigned discriminator) {
148      Discriminator = discriminator;
149    }
150  };
151
152  /// MCLineEntry - Instances of this class represent the line information for
153  /// the dwarf line table entries.  Which is created after a machine
154  /// instruction is assembled and uses an address from a temporary label
155  /// created at the current address in the current section and the info from
156  /// the last .loc directive seen as stored in the context.
157  class MCLineEntry : public MCDwarfLoc {
158    MCSymbol *Label;
159
160  private:
161    // Allow the default copy constructor and assignment operator to be used
162    // for an MCLineEntry object.
163
164  public:
165    // Constructor to create an MCLineEntry given a symbol and the dwarf loc.
166    MCLineEntry(MCSymbol *label, const MCDwarfLoc loc) : MCDwarfLoc(loc),
167                Label(label) {}
168
169    MCSymbol *getLabel() const { return Label; }
170
171    // This is called when an instruction is assembled into the specified
172    // section and if there is information from the last .loc directive that
173    // has yet to have a line entry made for it is made.
174    static void Make(MCStreamer *MCOS, const MCSection *Section);
175  };
176
177  /// MCLineSection - Instances of this class represent the line information
178  /// for a section where machine instructions have been assembled after seeing
179  /// .loc directives.  This is the information used to build the dwarf line
180  /// table for a section.
181  class MCLineSection {
182
183  private:
184    MCLineSection(const MCLineSection&);  // DO NOT IMPLEMENT
185    void operator=(const MCLineSection&); // DO NOT IMPLEMENT
186
187  public:
188    // Constructor to create an MCLineSection with an empty MCLineEntries
189    // vector.
190    MCLineSection() {}
191
192    // addLineEntry - adds an entry to this MCLineSection's line entries
193    void addLineEntry(const MCLineEntry &LineEntry) {
194      MCLineEntries.push_back(LineEntry);
195    }
196
197    typedef std::vector<MCLineEntry> MCLineEntryCollection;
198    typedef MCLineEntryCollection::iterator iterator;
199    typedef MCLineEntryCollection::const_iterator const_iterator;
200
201  private:
202    MCLineEntryCollection MCLineEntries;
203
204  public:
205    const MCLineEntryCollection *getMCLineEntries() const {
206      return &MCLineEntries;
207    }
208  };
209
210  class MCDwarfFileTable {
211  public:
212    //
213    // This emits the Dwarf file and the line tables.
214    //
215    static void Emit(MCStreamer *MCOS);
216  };
217
218  class MCDwarfLineAddr {
219  public:
220    /// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
221    static void Encode(int64_t LineDelta, uint64_t AddrDelta, raw_ostream &OS);
222
223    /// Utility function to emit the encoding to a streamer.
224    static void Emit(MCStreamer *MCOS,
225                     int64_t LineDelta,uint64_t AddrDelta);
226
227    /// Utility function to write the encoding to an object writer.
228    static void Write(MCObjectWriter *OW,
229                      int64_t LineDelta, uint64_t AddrDelta);
230  };
231
232  class MCCFIInstruction {
233  public:
234    enum OpType { SameValue, Remember, Restore, Move, RelMove };
235  private:
236    OpType Operation;
237    MCSymbol *Label;
238    // Move to & from location.
239    MachineLocation Destination;
240    MachineLocation Source;
241  public:
242    MCCFIInstruction(OpType Op, MCSymbol *L)
243      : Operation(Op), Label(L) {
244      assert(Op == Remember || Op == Restore);
245    }
246    MCCFIInstruction(OpType Op, MCSymbol *L, unsigned Register)
247      : Operation(Op), Label(L), Destination(Register) {
248      assert(Op == SameValue);
249    }
250    MCCFIInstruction(MCSymbol *L, const MachineLocation &D,
251                     const MachineLocation &S)
252      : Operation(Move), Label(L), Destination(D), Source(S) {
253    }
254    MCCFIInstruction(OpType Op, MCSymbol *L, const MachineLocation &D,
255                     const MachineLocation &S)
256      : Operation(Op), Label(L), Destination(D), Source(S) {
257      assert(Op == RelMove);
258    }
259    OpType getOperation() const { return Operation; }
260    MCSymbol *getLabel() const { return Label; }
261    const MachineLocation &getDestination() const { return Destination; }
262    const MachineLocation &getSource() const { return Source; }
263  };
264
265  struct MCDwarfFrameInfo {
266    MCDwarfFrameInfo() : Begin(0), End(0), Personality(0), Lsda(0),
267                         Function(0), Instructions(), PersonalityEncoding(),
268                         LsdaEncoding(0) {}
269    MCSymbol *Begin;
270    MCSymbol *End;
271    const MCSymbol *Personality;
272    const MCSymbol *Lsda;
273    const MCSymbol *Function;
274    std::vector<MCCFIInstruction> Instructions;
275    unsigned PersonalityEncoding;
276    unsigned LsdaEncoding;
277  };
278
279  class MCDwarfFrameEmitter {
280  public:
281    //
282    // This emits the frame info section.
283    //
284    static void Emit(MCStreamer &streamer, bool usingCFI,
285                     bool isEH);
286    static void EmitAdvanceLoc(MCStreamer &Streamer, uint64_t AddrDelta);
287    static void EncodeAdvanceLoc(uint64_t AddrDelta, raw_ostream &OS);
288  };
289} // end namespace llvm
290
291#endif
292