MCDwarf: Rename MCDwarfFileTable to MCDwarfLineTable
[oota-llvm.git] / include / llvm / MC / MCDwarf.h
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/ADT/MapVector.h"
20 #include "llvm/Support/Compiler.h"
21 #include "llvm/Support/Dwarf.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include <map>
24 #include <vector>
25 #include <string>
26 #include <utility>
27
28 namespace llvm {
29 class MCAsmBackend;
30 class MCContext;
31 class MCSection;
32 class MCStreamer;
33 class MCSymbol;
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 struct MCDwarfFile {
43   // Name - the base name of the file without its directory path.
44   // The StringRef references memory allocated in the MCContext.
45   std::string Name;
46
47   // DirIndex - the index into the list of directory names for this file name.
48   unsigned DirIndex;
49 };
50
51 /// MCDwarfLoc - Instances of this class represent the information from a
52 /// dwarf .loc directive.
53 class MCDwarfLoc {
54   // FileNum - the file number.
55   unsigned FileNum;
56   // Line - the line number.
57   unsigned Line;
58   // Column - the column position.
59   unsigned Column;
60   // Flags (see #define's below)
61   unsigned Flags;
62   // Isa
63   unsigned Isa;
64   // Discriminator
65   unsigned Discriminator;
66
67 // Flag that indicates the initial value of the is_stmt_start flag.
68 #define DWARF2_LINE_DEFAULT_IS_STMT 1
69
70 #define DWARF2_FLAG_IS_STMT (1 << 0)
71 #define DWARF2_FLAG_BASIC_BLOCK (1 << 1)
72 #define DWARF2_FLAG_PROLOGUE_END (1 << 2)
73 #define DWARF2_FLAG_EPILOGUE_BEGIN (1 << 3)
74
75 private: // MCContext manages these
76   friend class MCContext;
77   friend class MCLineEntry;
78   MCDwarfLoc(unsigned fileNum, unsigned line, unsigned column, unsigned flags,
79              unsigned isa, unsigned discriminator)
80       : FileNum(fileNum), Line(line), Column(column), Flags(flags), Isa(isa),
81         Discriminator(discriminator) {}
82
83   // Allow the default copy constructor and assignment operator to be used
84   // for an MCDwarfLoc object.
85
86 public:
87   /// getFileNum - Get the FileNum of this MCDwarfLoc.
88   unsigned getFileNum() const { return FileNum; }
89
90   /// getLine - Get the Line of this MCDwarfLoc.
91   unsigned getLine() const { return Line; }
92
93   /// getColumn - Get the Column of this MCDwarfLoc.
94   unsigned getColumn() const { return Column; }
95
96   /// getFlags - Get the Flags of this MCDwarfLoc.
97   unsigned getFlags() const { return Flags; }
98
99   /// getIsa - Get the Isa of this MCDwarfLoc.
100   unsigned getIsa() const { return Isa; }
101
102   /// getDiscriminator - Get the Discriminator of this MCDwarfLoc.
103   unsigned getDiscriminator() const { return Discriminator; }
104
105   /// setFileNum - Set the FileNum of this MCDwarfLoc.
106   void setFileNum(unsigned fileNum) { FileNum = fileNum; }
107
108   /// setLine - Set the Line of this MCDwarfLoc.
109   void setLine(unsigned line) { Line = line; }
110
111   /// setColumn - Set the Column of this MCDwarfLoc.
112   void setColumn(unsigned column) { Column = column; }
113
114   /// setFlags - Set the Flags of this MCDwarfLoc.
115   void setFlags(unsigned flags) { Flags = flags; }
116
117   /// setIsa - Set the Isa of this MCDwarfLoc.
118   void setIsa(unsigned isa) { Isa = isa; }
119
120   /// setDiscriminator - Set the Discriminator of this MCDwarfLoc.
121   void setDiscriminator(unsigned discriminator) {
122     Discriminator = discriminator;
123   }
124 };
125
126 /// MCLineEntry - Instances of this class represent the line information for
127 /// the dwarf line table entries.  Which is created after a machine
128 /// instruction is assembled and uses an address from a temporary label
129 /// created at the current address in the current section and the info from
130 /// the last .loc directive seen as stored in the context.
131 class MCLineEntry : public MCDwarfLoc {
132   MCSymbol *Label;
133
134 private:
135   // Allow the default copy constructor and assignment operator to be used
136   // for an MCLineEntry object.
137
138 public:
139   // Constructor to create an MCLineEntry given a symbol and the dwarf loc.
140   MCLineEntry(MCSymbol *label, const MCDwarfLoc loc)
141       : MCDwarfLoc(loc), Label(label) {}
142
143   MCSymbol *getLabel() const { return Label; }
144
145   // This is called when an instruction is assembled into the specified
146   // section and if there is information from the last .loc directive that
147   // has yet to have a line entry made for it is made.
148   static void Make(MCStreamer *MCOS, const MCSection *Section);
149 };
150
151 /// MCLineSection - Instances of this class represent the line information
152 /// for a compile unit where machine instructions have been assembled after seeing
153 /// .loc directives.  This is the information used to build the dwarf line
154 /// table for a section.
155 class MCLineSection {
156 public:
157   // addLineEntry - adds an entry to this MCLineSection's line entries
158   void addLineEntry(const MCLineEntry &LineEntry, const MCSection *Sec) {
159     MCLineDivisions[Sec].push_back(LineEntry);
160   }
161
162   typedef std::vector<MCLineEntry> MCLineEntryCollection;
163   typedef MCLineEntryCollection::iterator iterator;
164   typedef MCLineEntryCollection::const_iterator const_iterator;
165   typedef MapVector<const MCSection *, MCLineEntryCollection> MCLineDivisionMap;
166
167 private:
168   // A collection of MCLineEntry for each section.
169   MCLineDivisionMap MCLineDivisions;
170
171 public:
172   // Returns the collection of MCLineEntry for a given Compile Unit ID.
173   const MCLineDivisionMap &getMCLineEntries() const {
174     return MCLineDivisions;
175   }
176 };
177
178 struct MCDwarfLineTableHeader {
179   MCSymbol *Label;
180   SmallVector<std::string, 3> MCDwarfDirs;
181   SmallVector<MCDwarfFile, 3> MCDwarfFiles;
182   unsigned getFile(StringRef Directory, StringRef FileName, unsigned FileNumber);
183   std::pair<MCSymbol *, MCSymbol *> Emit(MCStreamer *MCOS) const;
184 };
185
186 class MCDwarfLineTable {
187   MCDwarfLineTableHeader Header;
188   MCLineSection MCLineSections;
189
190 public:
191   // This emits the Dwarf file and the line tables for all Compile Units.
192   static const MCSymbol *Emit(MCStreamer *MCOS);
193
194   // This emits the Dwarf file and the line tables for a given Compile Unit.
195   const MCSymbol *EmitCU(MCStreamer *MCOS) const;
196
197   unsigned getFile(StringRef Directory, StringRef FileName, unsigned FileNumber);
198
199   MCSymbol *getLabel() const {
200     return Header.Label;
201   }
202
203   void setLabel(MCSymbol *Label) {
204     Header.Label = Label;
205   }
206
207   const SmallVectorImpl<std::string> &getMCDwarfDirs() const {
208     return Header.MCDwarfDirs;
209   }
210
211   SmallVectorImpl<std::string> &getMCDwarfDirs() {
212     return Header.MCDwarfDirs;
213   }
214
215   const SmallVectorImpl<MCDwarfFile> &getMCDwarfFiles() const {
216     return Header.MCDwarfFiles;
217   }
218
219   SmallVectorImpl<MCDwarfFile> &getMCDwarfFiles() {
220     return Header.MCDwarfFiles;
221   }
222
223   const MCLineSection &getMCLineSections() const {
224     return MCLineSections;
225   }
226   MCLineSection &getMCLineSections() {
227     return MCLineSections;
228   }
229 };
230
231 class MCDwarfLineAddr {
232 public:
233   /// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
234   static void Encode(MCContext &Context, int64_t LineDelta, uint64_t AddrDelta,
235                      raw_ostream &OS);
236
237   /// Utility function to emit the encoding to a streamer.
238   static void Emit(MCStreamer *MCOS, int64_t LineDelta, uint64_t AddrDelta);
239 };
240
241 class MCGenDwarfInfo {
242 public:
243   //
244   // When generating dwarf for assembly source files this emits the Dwarf
245   // sections.
246   //
247   static void Emit(MCStreamer *MCOS, const MCSymbol *LineSectionSymbol);
248 };
249
250 // When generating dwarf for assembly source files this is the info that is
251 // needed to be gathered for each symbol that will have a dwarf label.
252 class MCGenDwarfLabelEntry {
253 private:
254   // Name of the symbol without a leading underbar, if any.
255   StringRef Name;
256   // The dwarf file number this symbol is in.
257   unsigned FileNumber;
258   // The line number this symbol is at.
259   unsigned LineNumber;
260   // The low_pc for the dwarf label is taken from this symbol.
261   MCSymbol *Label;
262
263 public:
264   MCGenDwarfLabelEntry(StringRef name, unsigned fileNumber, unsigned lineNumber,
265                        MCSymbol *label)
266       : Name(name), FileNumber(fileNumber), LineNumber(lineNumber),
267         Label(label) {}
268
269   StringRef getName() const { return Name; }
270   unsigned getFileNumber() const { return FileNumber; }
271   unsigned getLineNumber() const { return LineNumber; }
272   MCSymbol *getLabel() const { return Label; }
273
274   // This is called when label is created when we are generating dwarf for
275   // assembly source files.
276   static void Make(MCSymbol *Symbol, MCStreamer *MCOS, SourceMgr &SrcMgr,
277                    SMLoc &Loc);
278 };
279
280 class MCCFIInstruction {
281 public:
282   enum OpType {
283     OpSameValue,
284     OpRememberState,
285     OpRestoreState,
286     OpOffset,
287     OpDefCfaRegister,
288     OpDefCfaOffset,
289     OpDefCfa,
290     OpRelOffset,
291     OpAdjustCfaOffset,
292     OpEscape,
293     OpRestore,
294     OpUndefined,
295     OpRegister,
296     OpWindowSave
297   };
298
299 private:
300   OpType Operation;
301   MCSymbol *Label;
302   unsigned Register;
303   union {
304     int Offset;
305     unsigned Register2;
306   };
307   std::vector<char> Values;
308
309   MCCFIInstruction(OpType Op, MCSymbol *L, unsigned R, int O, StringRef V)
310       : Operation(Op), Label(L), Register(R), Offset(O),
311         Values(V.begin(), V.end()) {
312     assert(Op != OpRegister);
313   }
314
315   MCCFIInstruction(OpType Op, MCSymbol *L, unsigned R1, unsigned R2)
316       : Operation(Op), Label(L), Register(R1), Register2(R2) {
317     assert(Op == OpRegister);
318   }
319
320 public:
321   /// \brief .cfi_def_cfa defines a rule for computing CFA as: take address from
322   /// Register and add Offset to it.
323   static MCCFIInstruction createDefCfa(MCSymbol *L, unsigned Register,
324                                        int Offset) {
325     return MCCFIInstruction(OpDefCfa, L, Register, -Offset, "");
326   }
327
328   /// \brief .cfi_def_cfa_register modifies a rule for computing CFA. From now
329   /// on Register will be used instead of the old one. Offset remains the same.
330   static MCCFIInstruction createDefCfaRegister(MCSymbol *L, unsigned Register) {
331     return MCCFIInstruction(OpDefCfaRegister, L, Register, 0, "");
332   }
333
334   /// \brief .cfi_def_cfa_offset modifies a rule for computing CFA. Register
335   /// remains the same, but offset is new. Note that it is the absolute offset
336   /// that will be added to a defined register to the compute CFA address.
337   static MCCFIInstruction createDefCfaOffset(MCSymbol *L, int Offset) {
338     return MCCFIInstruction(OpDefCfaOffset, L, 0, -Offset, "");
339   }
340
341   /// \brief .cfi_adjust_cfa_offset Same as .cfi_def_cfa_offset, but
342   /// Offset is a relative value that is added/subtracted from the previous
343   /// offset.
344   static MCCFIInstruction createAdjustCfaOffset(MCSymbol *L, int Adjustment) {
345     return MCCFIInstruction(OpAdjustCfaOffset, L, 0, Adjustment, "");
346   }
347
348   /// \brief .cfi_offset Previous value of Register is saved at offset Offset
349   /// from CFA.
350   static MCCFIInstruction createOffset(MCSymbol *L, unsigned Register,
351                                        int Offset) {
352     return MCCFIInstruction(OpOffset, L, Register, Offset, "");
353   }
354
355   /// \brief .cfi_rel_offset Previous value of Register is saved at offset
356   /// Offset from the current CFA register. This is transformed to .cfi_offset
357   /// using the known displacement of the CFA register from the CFA.
358   static MCCFIInstruction createRelOffset(MCSymbol *L, unsigned Register,
359                                           int Offset) {
360     return MCCFIInstruction(OpRelOffset, L, Register, Offset, "");
361   }
362
363   /// \brief .cfi_register Previous value of Register1 is saved in
364   /// register Register2.
365   static MCCFIInstruction createRegister(MCSymbol *L, unsigned Register1,
366                                          unsigned Register2) {
367     return MCCFIInstruction(OpRegister, L, Register1, Register2);
368   }
369
370   /// \brief .cfi_window_save SPARC register window is saved.
371   static MCCFIInstruction createWindowSave(MCSymbol *L) {
372     return MCCFIInstruction(OpWindowSave, L, 0, 0, "");
373   }
374
375   /// \brief .cfi_restore says that the rule for Register is now the same as it
376   /// was at the beginning of the function, after all initial instructions added
377   /// by .cfi_startproc were executed.
378   static MCCFIInstruction createRestore(MCSymbol *L, unsigned Register) {
379     return MCCFIInstruction(OpRestore, L, Register, 0, "");
380   }
381
382   /// \brief .cfi_undefined From now on the previous value of Register can't be
383   /// restored anymore.
384   static MCCFIInstruction createUndefined(MCSymbol *L, unsigned Register) {
385     return MCCFIInstruction(OpUndefined, L, Register, 0, "");
386   }
387
388   /// \brief .cfi_same_value Current value of Register is the same as in the
389   /// previous frame. I.e., no restoration is needed.
390   static MCCFIInstruction createSameValue(MCSymbol *L, unsigned Register) {
391     return MCCFIInstruction(OpSameValue, L, Register, 0, "");
392   }
393
394   /// \brief .cfi_remember_state Save all current rules for all registers.
395   static MCCFIInstruction createRememberState(MCSymbol *L) {
396     return MCCFIInstruction(OpRememberState, L, 0, 0, "");
397   }
398
399   /// \brief .cfi_restore_state Restore the previously saved state.
400   static MCCFIInstruction createRestoreState(MCSymbol *L) {
401     return MCCFIInstruction(OpRestoreState, L, 0, 0, "");
402   }
403
404   /// \brief .cfi_escape Allows the user to add arbitrary bytes to the unwind
405   /// info.
406   static MCCFIInstruction createEscape(MCSymbol *L, StringRef Vals) {
407     return MCCFIInstruction(OpEscape, L, 0, 0, Vals);
408   }
409
410   OpType getOperation() const { return Operation; }
411   MCSymbol *getLabel() const { return Label; }
412
413   unsigned getRegister() const {
414     assert(Operation == OpDefCfa || Operation == OpOffset ||
415            Operation == OpRestore || Operation == OpUndefined ||
416            Operation == OpSameValue || Operation == OpDefCfaRegister ||
417            Operation == OpRelOffset || Operation == OpRegister);
418     return Register;
419   }
420
421   unsigned getRegister2() const {
422     assert(Operation == OpRegister);
423     return Register2;
424   }
425
426   int getOffset() const {
427     assert(Operation == OpDefCfa || Operation == OpOffset ||
428            Operation == OpRelOffset || Operation == OpDefCfaOffset ||
429            Operation == OpAdjustCfaOffset);
430     return Offset;
431   }
432
433   const StringRef getValues() const {
434     assert(Operation == OpEscape);
435     return StringRef(&Values[0], Values.size());
436   }
437 };
438
439 struct MCDwarfFrameInfo {
440   MCDwarfFrameInfo()
441       : Begin(0), End(0), Personality(0), Lsda(0), Function(0), Instructions(),
442         PersonalityEncoding(), LsdaEncoding(0), CompactUnwindEncoding(0),
443         IsSignalFrame(false), IsSimple(false) {}
444   MCSymbol *Begin;
445   MCSymbol *End;
446   const MCSymbol *Personality;
447   const MCSymbol *Lsda;
448   const MCSymbol *Function;
449   std::vector<MCCFIInstruction> Instructions;
450   unsigned PersonalityEncoding;
451   unsigned LsdaEncoding;
452   uint32_t CompactUnwindEncoding;
453   bool IsSignalFrame;
454   bool IsSimple;
455 };
456
457 class MCDwarfFrameEmitter {
458 public:
459   //
460   // This emits the frame info section.
461   //
462   static void Emit(MCStreamer &streamer, MCAsmBackend *MAB,
463                    bool usingCFI, bool isEH);
464   static void EmitAdvanceLoc(MCStreamer &Streamer, uint64_t AddrDelta);
465   static void EncodeAdvanceLoc(MCContext &Context, uint64_t AddrDelta,
466                                raw_ostream &OS);
467 };
468 } // end namespace llvm
469
470 #endif