OSDN Git Service

Change the representation of MCCFIInstruction.
[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/raw_ostream.h"
20 #include "llvm/Support/Dwarf.h"
21 #include "llvm/Support/Compiler.h"
22 #include <vector>
23
24 namespace llvm {
25   class MCContext;
26   class MCObjectWriter;
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   public:
54     /// getName - Get the base name of this MCDwarfFile.
55     StringRef getName() const { return Name; }
56
57     /// getDirIndex - Get the dirIndex of this MCDwarfFile.
58     unsigned getDirIndex() const { return DirIndex; }
59
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) : MCDwarfLoc(loc),
163                 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) {
190       MCLineEntries.push_back(LineEntry);
191     }
192
193     typedef std::vector<MCLineEntry> MCLineEntryCollection;
194     typedef MCLineEntryCollection::iterator iterator;
195     typedef MCLineEntryCollection::const_iterator const_iterator;
196
197   private:
198     MCLineEntryCollection MCLineEntries;
199
200   public:
201     const MCLineEntryCollection *getMCLineEntries() const {
202       return &MCLineEntries;
203     }
204   };
205
206   class MCDwarfFileTable {
207   public:
208     //
209     // This emits the Dwarf file and the line tables.
210     //
211     static const MCSymbol *Emit(MCStreamer *MCOS);
212   };
213
214   class MCDwarfLineAddr {
215   public:
216     /// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
217     static void Encode(int64_t LineDelta, uint64_t AddrDelta, raw_ostream &OS);
218
219     /// Utility function to emit the encoding to a streamer.
220     static void Emit(MCStreamer *MCOS,
221                      int64_t LineDelta,uint64_t AddrDelta);
222
223     /// Utility function to write the encoding to an object writer.
224     static void Write(MCObjectWriter *OW,
225                       int64_t LineDelta, uint64_t AddrDelta);
226   };
227
228   class MCGenDwarfInfo {
229   public:
230     //
231     // When generating dwarf for assembly source files this emits the Dwarf
232     // sections.
233     //
234     static void Emit(MCStreamer *MCOS, const MCSymbol *LineSectionSymbol);
235   };
236
237   // When generating dwarf for assembly source files this is the info that is
238   // needed to be gathered for each symbol that will have a dwarf label.
239   class MCGenDwarfLabelEntry {
240   private:
241     // Name of the symbol without a leading underbar, if any.
242     StringRef Name;
243     // The dwarf file number this symbol is in.
244     unsigned FileNumber;
245     // The line number this symbol is at.
246     unsigned LineNumber;
247     // The low_pc for the dwarf label is taken from this symbol.
248     MCSymbol *Label;
249
250   public:
251     MCGenDwarfLabelEntry(StringRef name, unsigned fileNumber,
252                          unsigned lineNumber, MCSymbol *label) :
253       Name(name), FileNumber(fileNumber), LineNumber(lineNumber), Label(label){}
254
255     StringRef getName() const { return Name; }
256     unsigned getFileNumber() const { return FileNumber; }
257     unsigned getLineNumber() const { return LineNumber; }
258     MCSymbol *getLabel() const { return Label; }
259
260     // This is called when label is created when we are generating dwarf for
261     // assembly source files.
262     static void Make(MCSymbol *Symbol, MCStreamer *MCOS, SourceMgr &SrcMgr,
263                      SMLoc &Loc);
264   };
265
266   class MCCFIInstruction {
267   public:
268     enum OpType { OpSameValue, OpRememberState, OpRestoreState, OpOffset,
269                   OpDefCfaRegister, OpDefCfaOffset, OpDefCfa, OpRelOffset,
270                   OpAdjustCfaOffset, OpEscape, OpRestore, OpUndefined };
271   private:
272     OpType Operation;
273     MCSymbol *Label;
274     unsigned Register;
275     int Offset;
276     std::vector<char> Values;
277
278     MCCFIInstruction(OpType Op, MCSymbol *L, unsigned R,
279                      int O, StringRef V) :
280       Operation(Op), Label(L), Register(R), Offset(O),
281       Values(V.begin(), V.end()) {
282     }
283
284   public:
285     static MCCFIInstruction
286     createOffset(MCSymbol *L, unsigned Register, int Offset) {
287       MCCFIInstruction Ret(OpOffset, L, Register, Offset, "");
288       return Ret;
289     }
290
291     static MCCFIInstruction
292     createDefCfaRegister(MCSymbol *L, unsigned Register) {
293       MCCFIInstruction Ret(OpDefCfaRegister, L, Register, 0, "");
294       return Ret;
295     }
296
297     static MCCFIInstruction createDefCfaOffset(MCSymbol *L, int Offset) {
298       MCCFIInstruction Ret(OpDefCfaOffset, L, 0, -Offset, "");
299       return Ret;
300     }
301
302     static MCCFIInstruction
303     createDefCfa(MCSymbol *L, unsigned Register, int Offset) {
304       MCCFIInstruction Ret(OpDefCfa, L, Register, -Offset, "");
305       return Ret;
306     }
307
308     static MCCFIInstruction createUndefined(MCSymbol *L, unsigned Register) {
309       MCCFIInstruction Ret(OpUndefined, L, Register, 0, "");
310       return Ret;
311     }
312
313     static MCCFIInstruction createRestore(MCSymbol *L, unsigned Register) {
314       MCCFIInstruction Ret(OpRestore, L, Register, 0, "");
315       return Ret;
316     }
317
318     static MCCFIInstruction createSameValue(MCSymbol *L, unsigned Register) {
319       MCCFIInstruction Ret(OpSameValue, L, Register, 0, "");
320       return Ret;
321     }
322
323     static MCCFIInstruction createRestoreState(MCSymbol *L) {
324       MCCFIInstruction Ret(OpRestoreState, L, 0, 0, "");
325       return Ret;
326     }
327
328     static MCCFIInstruction createRememberState(MCSymbol *L) {
329       MCCFIInstruction Ret(OpRememberState, L, 0, 0, "");
330       return Ret;
331     }
332
333     static MCCFIInstruction
334     createRelOffset(MCSymbol *L, unsigned Register, int Offset) {
335       MCCFIInstruction Ret(OpRelOffset, L, Register, Offset, "");
336       return Ret;
337     }
338
339     static MCCFIInstruction
340     createAdjustCfaOffset(MCSymbol *L, int Adjustment) {
341       MCCFIInstruction Ret(OpAdjustCfaOffset, L, 0, Adjustment, "");
342       return Ret;
343     }
344
345     static MCCFIInstruction createEscape(MCSymbol *L, StringRef Vals) {
346       MCCFIInstruction Ret(OpEscape, L, 0, 0, Vals);
347       return Ret;
348     }
349
350     OpType getOperation() const { return Operation; }
351     MCSymbol *getLabel() const { return Label; }
352
353     unsigned getRegister() const {
354       assert(Operation == OpDefCfa || Operation == OpOffset ||
355              Operation == OpRestore || Operation == OpUndefined ||
356              Operation == OpSameValue || Operation == OpDefCfaRegister ||
357              Operation == OpRelOffset);
358       return Register;
359     }
360
361     int getOffset() const {
362       assert(Operation == OpDefCfa || Operation == OpOffset ||
363              Operation == OpRelOffset || Operation == OpDefCfaOffset ||
364              Operation == OpAdjustCfaOffset);
365       return Offset;
366     }
367
368     const StringRef getValues() const {
369       assert(Operation == OpEscape);
370       return StringRef(&Values[0], Values.size());
371     }
372   };
373
374   struct MCDwarfFrameInfo {
375     MCDwarfFrameInfo() : Begin(0), End(0), Personality(0), Lsda(0),
376                          Function(0), Instructions(), PersonalityEncoding(),
377                          LsdaEncoding(0), CompactUnwindEncoding(0),
378                          IsSignalFrame(false) {}
379     MCSymbol *Begin;
380     MCSymbol *End;
381     const MCSymbol *Personality;
382     const MCSymbol *Lsda;
383     const MCSymbol *Function;
384     std::vector<MCCFIInstruction> Instructions;
385     unsigned PersonalityEncoding;
386     unsigned LsdaEncoding;
387     uint32_t CompactUnwindEncoding;
388     bool IsSignalFrame;
389   };
390
391   class MCDwarfFrameEmitter {
392   public:
393     //
394     // This emits the frame info section.
395     //
396     static void Emit(MCStreamer &streamer, bool usingCFI,
397                      bool isEH);
398     static void EmitAdvanceLoc(MCStreamer &Streamer, uint64_t AddrDelta);
399     static void EncodeAdvanceLoc(uint64_t AddrDelta, raw_ostream &OS);
400   };
401 } // end namespace llvm
402
403 #endif