OSDN Git Service

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