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