MCDwarf.h revision 7f74d2c2c197eec76ae2b41fed9c227c0dcc04cb
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/Support/raw_ostream.h"
21#include "llvm/Support/Dwarf.h"
22#include "llvm/Support/Compiler.h"
23#include <vector>
24
25namespace llvm {
26  class MCContext;
27  class MCObjectWriter;
28  class MCSection;
29  class MCStreamer;
30  class MCSymbol;
31  class SourceMgr;
32  class SMLoc;
33
34  /// MCDwarfFile - Instances of this class represent the name of the dwarf
35  /// .file directive and its associated dwarf file number in the MC file,
36  /// and MCDwarfFile's are created and unique'd by the MCContext class where
37  /// the file number for each is its index into the vector of DwarfFiles (note
38  /// index 0 is not used and not a valid dwarf file number).
39  class MCDwarfFile {
40    // Name - the base name of the file without its directory path.
41    // The StringRef references memory allocated in the MCContext.
42    StringRef Name;
43
44    // DirIndex - the index into the list of directory names for this file name.
45    unsigned DirIndex;
46
47  private:  // MCContext creates and uniques these.
48    friend class MCContext;
49    MCDwarfFile(StringRef name, unsigned dirIndex)
50      : Name(name), DirIndex(dirIndex) {}
51
52    MCDwarfFile(const MCDwarfFile&) LLVM_DELETED_FUNCTION;
53    void operator=(const MCDwarfFile&) LLVM_DELETED_FUNCTION;
54  public:
55    /// getName - Get the base name of this MCDwarfFile.
56    StringRef getName() const { return Name; }
57
58    /// getDirIndex - Get the dirIndex of this MCDwarfFile.
59    unsigned getDirIndex() const { return DirIndex; }
60
61
62    /// print - Print the value to the stream \p OS.
63    void print(raw_ostream &OS) const;
64
65    /// dump - Print the value to stderr.
66    void dump() const;
67  };
68
69  inline raw_ostream &operator<<(raw_ostream &OS, const MCDwarfFile &DwarfFile){
70    DwarfFile.print(OS);
71    return OS;
72  }
73
74  /// MCDwarfLoc - Instances of this class represent the information from a
75  /// dwarf .loc directive.
76  class MCDwarfLoc {
77    // FileNum - the file number.
78    unsigned FileNum;
79    // Line - the line number.
80    unsigned Line;
81    // Column - the column position.
82    unsigned Column;
83    // Flags (see #define's below)
84    unsigned Flags;
85    // Isa
86    unsigned Isa;
87    // Discriminator
88    unsigned Discriminator;
89
90// Flag that indicates the initial value of the is_stmt_start flag.
91#define DWARF2_LINE_DEFAULT_IS_STMT     1
92
93#define DWARF2_FLAG_IS_STMT        (1 << 0)
94#define DWARF2_FLAG_BASIC_BLOCK    (1 << 1)
95#define DWARF2_FLAG_PROLOGUE_END   (1 << 2)
96#define DWARF2_FLAG_EPILOGUE_BEGIN (1 << 3)
97
98  private:  // MCContext manages these
99    friend class MCContext;
100    friend class MCLineEntry;
101    MCDwarfLoc(unsigned fileNum, unsigned line, unsigned column, unsigned flags,
102               unsigned isa, unsigned discriminator)
103      : FileNum(fileNum), Line(line), Column(column), Flags(flags), Isa(isa),
104        Discriminator(discriminator) {}
105
106    // Allow the default copy constructor and assignment operator to be used
107    // for an MCDwarfLoc object.
108
109  public:
110    /// getFileNum - Get the FileNum of this MCDwarfLoc.
111    unsigned getFileNum() const { return FileNum; }
112
113    /// getLine - Get the Line of this MCDwarfLoc.
114    unsigned getLine() const { return Line; }
115
116    /// getColumn - Get the Column of this MCDwarfLoc.
117    unsigned getColumn() const { return Column; }
118
119    /// getFlags - Get the Flags of this MCDwarfLoc.
120    unsigned getFlags() const { return Flags; }
121
122    /// getIsa - Get the Isa of this MCDwarfLoc.
123    unsigned getIsa() const { return Isa; }
124
125    /// getDiscriminator - Get the Discriminator of this MCDwarfLoc.
126    unsigned getDiscriminator() const { return Discriminator; }
127
128    /// setFileNum - Set the FileNum of this MCDwarfLoc.
129    void setFileNum(unsigned fileNum) { FileNum = fileNum; }
130
131    /// setLine - Set the Line of this MCDwarfLoc.
132    void setLine(unsigned line) { Line = line; }
133
134    /// setColumn - Set the Column of this MCDwarfLoc.
135    void setColumn(unsigned column) { Column = column; }
136
137    /// setFlags - Set the Flags of this MCDwarfLoc.
138    void setFlags(unsigned flags) { Flags = flags; }
139
140    /// setIsa - Set the Isa of this MCDwarfLoc.
141    void setIsa(unsigned isa) { Isa = isa; }
142
143    /// setDiscriminator - Set the Discriminator of this MCDwarfLoc.
144    void setDiscriminator(unsigned discriminator) {
145      Discriminator = discriminator;
146    }
147  };
148
149  /// MCLineEntry - Instances of this class represent the line information for
150  /// the dwarf line table entries.  Which is created after a machine
151  /// instruction is assembled and uses an address from a temporary label
152  /// created at the current address in the current section and the info from
153  /// the last .loc directive seen as stored in the context.
154  class MCLineEntry : public MCDwarfLoc {
155    MCSymbol *Label;
156
157  private:
158    // Allow the default copy constructor and assignment operator to be used
159    // for an MCLineEntry object.
160
161  public:
162    // Constructor to create an MCLineEntry given a symbol and the dwarf loc.
163    MCLineEntry(MCSymbol *label, const MCDwarfLoc loc) : MCDwarfLoc(loc),
164                Label(label) {}
165
166    MCSymbol *getLabel() const { return Label; }
167
168    // This is called when an instruction is assembled into the specified
169    // section and if there is information from the last .loc directive that
170    // has yet to have a line entry made for it is made.
171    static void Make(MCStreamer *MCOS, const MCSection *Section);
172  };
173
174  /// MCLineSection - Instances of this class represent the line information
175  /// for a section where machine instructions have been assembled after seeing
176  /// .loc directives.  This is the information used to build the dwarf line
177  /// table for a section.
178  class MCLineSection {
179
180  private:
181    MCLineSection(const MCLineSection&) LLVM_DELETED_FUNCTION;
182    void operator=(const MCLineSection&) LLVM_DELETED_FUNCTION;
183
184  public:
185    // Constructor to create an MCLineSection with an empty MCLineEntries
186    // vector.
187    MCLineSection() {}
188
189    // addLineEntry - adds an entry to this MCLineSection's line entries
190    void addLineEntry(const MCLineEntry &LineEntry) {
191      MCLineEntries.push_back(LineEntry);
192    }
193
194    typedef std::vector<MCLineEntry> MCLineEntryCollection;
195    typedef MCLineEntryCollection::iterator iterator;
196    typedef MCLineEntryCollection::const_iterator const_iterator;
197
198  private:
199    MCLineEntryCollection MCLineEntries;
200
201  public:
202    const MCLineEntryCollection *getMCLineEntries() const {
203      return &MCLineEntries;
204    }
205  };
206
207  class MCDwarfFileTable {
208  public:
209    //
210    // This emits the Dwarf file and the line tables.
211    //
212    static const MCSymbol *Emit(MCStreamer *MCOS);
213  };
214
215  class MCDwarfLineAddr {
216  public:
217    /// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
218    static void Encode(int64_t LineDelta, uint64_t AddrDelta, raw_ostream &OS);
219
220    /// Utility function to emit the encoding to a streamer.
221    static void Emit(MCStreamer *MCOS,
222                     int64_t LineDelta,uint64_t AddrDelta);
223
224    /// Utility function to write the encoding to an object writer.
225    static void Write(MCObjectWriter *OW,
226                      int64_t LineDelta, uint64_t AddrDelta);
227  };
228
229  class MCGenDwarfInfo {
230  public:
231    //
232    // When generating dwarf for assembly source files this emits the Dwarf
233    // sections.
234    //
235    static void Emit(MCStreamer *MCOS, const MCSymbol *LineSectionSymbol);
236  };
237
238  // When generating dwarf for assembly source files this is the info that is
239  // needed to be gathered for each symbol that will have a dwarf label.
240  class MCGenDwarfLabelEntry {
241  private:
242    // Name of the symbol without a leading underbar, if any.
243    StringRef Name;
244    // The dwarf file number this symbol is in.
245    unsigned FileNumber;
246    // The line number this symbol is at.
247    unsigned LineNumber;
248    // The low_pc for the dwarf label is taken from this symbol.
249    MCSymbol *Label;
250
251  public:
252    MCGenDwarfLabelEntry(StringRef name, unsigned fileNumber,
253                         unsigned lineNumber, MCSymbol *label) :
254      Name(name), FileNumber(fileNumber), LineNumber(lineNumber), Label(label){}
255
256    StringRef getName() const { return Name; }
257    unsigned getFileNumber() const { return FileNumber; }
258    unsigned getLineNumber() const { return LineNumber; }
259    MCSymbol *getLabel() const { return Label; }
260
261    // This is called when label is created when we are generating dwarf for
262    // assembly source files.
263    static void Make(MCSymbol *Symbol, MCStreamer *MCOS, SourceMgr &SrcMgr,
264                     SMLoc &Loc);
265  };
266
267  class MCCFIInstruction {
268  public:
269    enum OpType { OpSameValue, OpRememberState, OpRestoreState, OpOffset,
270                  OpDefCfaRegister, OpDefCfaOffset, OpDefCfa, OpRelOffset,
271                  OpAdjustCfaOffset, OpEscape, OpRestore, OpUndefined };
272  private:
273    OpType Operation;
274    MCSymbol *Label;
275    // Move to & from location.
276    MachineLocation Destination;
277    MachineLocation Source;
278    std::vector<char> Values;
279    MCCFIInstruction(OpType Op, MCSymbol *L, const MachineLocation &D,
280                     const MachineLocation &S, StringRef V) :
281      Operation(Op), Label(L), Destination(D), Source(S),
282      Values(V.begin(), V.end()) {
283    }
284
285  public:
286    static MCCFIInstruction
287    createOffset(MCSymbol *L, unsigned Register, int Offset) {
288      MachineLocation Dest(Register, Offset);
289      MachineLocation Source(Register, Offset);
290      MCCFIInstruction Ret(OpOffset, L, Dest, Source, "");
291      return Ret;
292    }
293
294    static MCCFIInstruction
295    createDefCfaRegister(MCSymbol *L, unsigned Register) {
296      MachineLocation Dest(Register);
297      MachineLocation Source(MachineLocation::VirtualFP);
298      MCCFIInstruction Ret(OpDefCfaRegister, L, Dest, Source, "");
299      return Ret;
300    }
301
302    static MCCFIInstruction createDefCfaOffset(MCSymbol *L, int Offset) {
303      MachineLocation Dest(MachineLocation::VirtualFP);
304      MachineLocation Source(MachineLocation::VirtualFP, -Offset);
305      MCCFIInstruction Ret(OpDefCfaOffset, L, Dest, Source, "");
306      return Ret;
307    }
308
309    static MCCFIInstruction
310    createDefCfa(MCSymbol *L, unsigned Register, int Offset) {
311      MachineLocation Dest(MachineLocation::VirtualFP);
312      MachineLocation Source(Register, -Offset);
313      MCCFIInstruction Ret(OpDefCfa, L, Dest, Source, "");
314      return Ret;
315    }
316
317    static MCCFIInstruction createUndefined(MCSymbol *L, unsigned Register) {
318      MachineLocation Dummy;
319      MachineLocation Dest(Register);
320      MCCFIInstruction Ret(OpUndefined, L, Dest, Dummy, "");
321      return Ret;
322    }
323
324    static MCCFIInstruction createRestore(MCSymbol *L, unsigned Register) {
325      MachineLocation Dummy;
326      MachineLocation Dest(Register);
327      MCCFIInstruction Ret(OpRestore, L, Dest, Dummy, "");
328      return Ret;
329    }
330
331    static MCCFIInstruction createSameValue(MCSymbol *L, unsigned Register) {
332      MachineLocation Dummy;
333      MachineLocation Dest(Register);
334      MCCFIInstruction Ret(OpSameValue, L, Dest, Dummy, "");
335      return Ret;
336    }
337
338    static MCCFIInstruction createRestoreState(MCSymbol *L) {
339      MachineLocation Dummy;
340      MCCFIInstruction Ret(OpRestoreState, L, Dummy, Dummy, "");
341      return Ret;
342    }
343
344    static MCCFIInstruction createRememberState(MCSymbol *L) {
345      MachineLocation Dummy;
346      MCCFIInstruction Ret(OpRememberState, L, Dummy, Dummy, "");
347      return Ret;
348    }
349
350    static MCCFIInstruction
351    createRelOffset(MCSymbol *L, unsigned Register, int Offset) {
352      MachineLocation Dest(Register, Offset);
353      MachineLocation Source(Register, Offset);
354      MCCFIInstruction Ret(OpRelOffset, L, Dest, Source, "");
355      return Ret;
356    }
357
358    static MCCFIInstruction
359    createAdjustCfaOffset(MCSymbol *L, int Adjustment) {
360      MachineLocation Dest(MachineLocation::VirtualFP);
361      MachineLocation Source(MachineLocation::VirtualFP, Adjustment);
362      MCCFIInstruction Ret(OpAdjustCfaOffset, L, Dest, Source, "");
363      return Ret;
364    }
365
366    static MCCFIInstruction createEscape(MCSymbol *L, StringRef Vals) {
367      MachineLocation Dummy;
368      MCCFIInstruction Ret(OpEscape, L, Dummy, Dummy, Vals);
369      return Ret;
370    }
371
372    OpType getOperation() const { return Operation; }
373    MCSymbol *getLabel() const { return Label; }
374    const MachineLocation &getDestination() const { return Destination; }
375    const MachineLocation &getSource() const { return Source; }
376    const StringRef getValues() const {
377      return StringRef(&Values[0], Values.size());
378    }
379  };
380
381  struct MCDwarfFrameInfo {
382    MCDwarfFrameInfo() : Begin(0), End(0), Personality(0), Lsda(0),
383                         Function(0), Instructions(), PersonalityEncoding(),
384                         LsdaEncoding(0), CompactUnwindEncoding(0),
385                         IsSignalFrame(false) {}
386    MCSymbol *Begin;
387    MCSymbol *End;
388    const MCSymbol *Personality;
389    const MCSymbol *Lsda;
390    const MCSymbol *Function;
391    std::vector<MCCFIInstruction> Instructions;
392    unsigned PersonalityEncoding;
393    unsigned LsdaEncoding;
394    uint32_t CompactUnwindEncoding;
395    bool IsSignalFrame;
396  };
397
398  class MCDwarfFrameEmitter {
399  public:
400    //
401    // This emits the frame info section.
402    //
403    static void Emit(MCStreamer &streamer, bool usingCFI,
404                     bool isEH);
405    static void EmitAdvanceLoc(MCStreamer &Streamer, uint64_t AddrDelta);
406    static void EncodeAdvanceLoc(uint64_t AddrDelta, raw_ostream &OS);
407  };
408} // end namespace llvm
409
410#endif
411