MCDwarf.h revision 94c2e85bea1ab1b837a4c055ccc83d5cd32dd027
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/MC/MachineLocation.h"
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 MCContext;
27  class MCExpr;
28  class MCSection;
29  class MCSectionData;
30  class MCStreamer;
31  class MCSymbol;
32  class MCObjectStreamer;
33  class raw_ostream;
34  class SourceMgr;
35  class SMLoc;
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 MCGenDwarfInfo {
233  public:
234    //
235    // When generating dwarf for assembly source files this emits the Dwarf
236    // sections.
237    //
238    static void Emit(MCStreamer *MCOS);
239  };
240
241  // When generating dwarf for assembly source files this is the info that is
242  // needed to be gathered for each symbol that will have a dwarf2_subprogram.
243  class MCGenDwarfSubprogramEntry {
244  private:
245    // Name of the symbol without a leading underbar, if any.
246    StringRef Name;
247    // The dwarf file number this symbol is in.
248    unsigned FileNumber;
249    // The line number this symbol is at.
250    unsigned LineNumber;
251    // The low_pc for the dwarf2_subprogram is taken from this symbol.  The
252    // high_pc is taken from the next symbol's value or the end of the section
253    // for the last symbol
254    MCSymbol *Label;
255
256  public:
257    MCGenDwarfSubprogramEntry(StringRef name, unsigned fileNumber,
258                              unsigned lineNumber, MCSymbol *label) :
259      Name(name), FileNumber(fileNumber), LineNumber(lineNumber), Label(label){}
260
261    StringRef getName() const { return Name; }
262    unsigned getFileNumber() const { return FileNumber; }
263    unsigned getLineNumber() const { return LineNumber; }
264    MCSymbol *getLabel() const { return Label; }
265
266    // This is called when label is created when we are generating dwarf for
267    // assembly source files.
268    static void Make(MCSymbol *Symbol, MCStreamer *MCOS, SourceMgr &SrcMgr,
269                     SMLoc &Loc);
270  };
271
272  class MCCFIInstruction {
273  public:
274    enum OpType { SameValue, Remember, Restore, Move, RelMove };
275  private:
276    OpType Operation;
277    MCSymbol *Label;
278    // Move to & from location.
279    MachineLocation Destination;
280    MachineLocation Source;
281  public:
282    MCCFIInstruction(OpType Op, MCSymbol *L)
283      : Operation(Op), Label(L) {
284      assert(Op == Remember || Op == Restore);
285    }
286    MCCFIInstruction(OpType Op, MCSymbol *L, unsigned Register)
287      : Operation(Op), Label(L), Destination(Register) {
288      assert(Op == SameValue);
289    }
290    MCCFIInstruction(MCSymbol *L, const MachineLocation &D,
291                     const MachineLocation &S)
292      : Operation(Move), Label(L), Destination(D), Source(S) {
293    }
294    MCCFIInstruction(OpType Op, MCSymbol *L, const MachineLocation &D,
295                     const MachineLocation &S)
296      : Operation(Op), Label(L), Destination(D), Source(S) {
297      assert(Op == RelMove);
298    }
299    OpType getOperation() const { return Operation; }
300    MCSymbol *getLabel() const { return Label; }
301    const MachineLocation &getDestination() const { return Destination; }
302    const MachineLocation &getSource() const { return Source; }
303  };
304
305  struct MCDwarfFrameInfo {
306    MCDwarfFrameInfo() : Begin(0), End(0), Personality(0), Lsda(0),
307                         Function(0), Instructions(), PersonalityEncoding(),
308                         LsdaEncoding(0), CompactUnwindEncoding(0) {}
309    MCSymbol *Begin;
310    MCSymbol *End;
311    const MCSymbol *Personality;
312    const MCSymbol *Lsda;
313    const MCSymbol *Function;
314    std::vector<MCCFIInstruction> Instructions;
315    unsigned PersonalityEncoding;
316    unsigned LsdaEncoding;
317    uint32_t CompactUnwindEncoding;
318  };
319
320  class MCDwarfFrameEmitter {
321  public:
322    //
323    // This emits the frame info section.
324    //
325    static void Emit(MCStreamer &streamer, bool usingCFI,
326                     bool isEH);
327    static void EmitAdvanceLoc(MCStreamer &Streamer, uint64_t AddrDelta);
328    static void EncodeAdvanceLoc(uint64_t AddrDelta, raw_ostream &OS);
329  };
330} // end namespace llvm
331
332#endif
333