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