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