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