OSDN Git Service

Implement .cfi_remember_state and .cfi_restore_state.
[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/CodeGen/MachineLocation.h" // FIXME
20 #include "llvm/MC/MCObjectWriter.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include "llvm/Support/Dwarf.h"
23 #include <vector>
24
25 namespace llvm {
26   class MachineMove;
27   class MCContext;
28   class MCExpr;
29   class MCSection;
30   class MCSectionData;
31   class MCStreamer;
32   class MCSymbol;
33   class MCObjectStreamer;
34   class raw_ostream;
35
36   /// MCDwarfFile - Instances of this class represent the name of the dwarf
37   /// .file directive and its associated dwarf file number in the MC file,
38   /// and MCDwarfFile's are created and unique'd by the MCContext class where
39   /// the file number for each is its index into the vector of DwarfFiles (note
40   /// index 0 is not used and not a valid dwarf file number).
41   class MCDwarfFile {
42     // Name - the base name of the file without its directory path.
43     // The StringRef references memory allocated in the MCContext.
44     StringRef Name;
45
46     // DirIndex - the index into the list of directory names for this file name.
47     unsigned DirIndex;
48
49   private:  // MCContext creates and uniques these.
50     friend class MCContext;
51     MCDwarfFile(StringRef name, unsigned dirIndex)
52       : Name(name), DirIndex(dirIndex) {}
53
54     MCDwarfFile(const MCDwarfFile&);       // DO NOT IMPLEMENT
55     void operator=(const MCDwarfFile&); // DO NOT IMPLEMENT
56   public:
57     /// getName - Get the base name of this MCDwarfFile.
58     StringRef getName() const { return Name; }
59
60     /// getDirIndex - Get the dirIndex of this MCDwarfFile.
61     unsigned getDirIndex() const { return DirIndex; }
62
63
64     /// print - Print the value to the stream \arg OS.
65     void print(raw_ostream &OS) const;
66
67     /// dump - Print the value to stderr.
68     void dump() const;
69   };
70
71   inline raw_ostream &operator<<(raw_ostream &OS, const MCDwarfFile &DwarfFile){
72     DwarfFile.print(OS);
73     return OS;
74   }
75
76   /// MCDwarfLoc - Instances of this class represent the information from a
77   /// dwarf .loc directive.
78   class MCDwarfLoc {
79     // FileNum - the file number.
80     unsigned FileNum;
81     // Line - the line number.
82     unsigned Line;
83     // Column - the column position.
84     unsigned Column;
85     // Flags (see #define's below)
86     unsigned Flags;
87     // Isa
88     unsigned Isa;
89     // Discriminator
90     unsigned Discriminator;
91
92 // Flag that indicates the initial value of the is_stmt_start flag.
93 #define DWARF2_LINE_DEFAULT_IS_STMT     1
94
95 #define DWARF2_FLAG_IS_STMT        (1 << 0)
96 #define DWARF2_FLAG_BASIC_BLOCK    (1 << 1)
97 #define DWARF2_FLAG_PROLOGUE_END   (1 << 2)
98 #define DWARF2_FLAG_EPILOGUE_BEGIN (1 << 3)
99
100   private:  // MCContext manages these
101     friend class MCContext;
102     friend class MCLineEntry;
103     MCDwarfLoc(unsigned fileNum, unsigned line, unsigned column, unsigned flags,
104                unsigned isa, unsigned discriminator)
105       : FileNum(fileNum), Line(line), Column(column), Flags(flags), Isa(isa),
106         Discriminator(discriminator) {}
107
108     // Allow the default copy constructor and assignment operator to be used
109     // for an MCDwarfLoc object.
110
111   public:
112     /// getFileNum - Get the FileNum of this MCDwarfLoc.
113     unsigned getFileNum() const { return FileNum; }
114
115     /// getLine - Get the Line of this MCDwarfLoc.
116     unsigned getLine() const { return Line; }
117
118     /// getColumn - Get the Column of this MCDwarfLoc.
119     unsigned getColumn() const { return Column; }
120
121     /// getFlags - Get the Flags of this MCDwarfLoc.
122     unsigned getFlags() const { return Flags; }
123
124     /// getIsa - Get the Isa of this MCDwarfLoc.
125     unsigned getIsa() const { return Isa; }
126
127     /// getDiscriminator - Get the Discriminator of this MCDwarfLoc.
128     unsigned getDiscriminator() const { return Discriminator; }
129
130     /// setFileNum - Set the FileNum of this MCDwarfLoc.
131     void setFileNum(unsigned fileNum) { FileNum = fileNum; }
132
133     /// setLine - Set the Line of this MCDwarfLoc.
134     void setLine(unsigned line) { Line = line; }
135
136     /// setColumn - Set the Column of this MCDwarfLoc.
137     void setColumn(unsigned column) { Column = column; }
138
139     /// setFlags - Set the Flags of this MCDwarfLoc.
140     void setFlags(unsigned flags) { Flags = flags; }
141
142     /// setIsa - Set the Isa of this MCDwarfLoc.
143     void setIsa(unsigned isa) { Isa = isa; }
144
145     /// setDiscriminator - Set the Discriminator of this MCDwarfLoc.
146     void setDiscriminator(unsigned discriminator) {
147       Discriminator = discriminator;
148     }
149   };
150
151   /// MCLineEntry - Instances of this class represent the line information for
152   /// the dwarf line table entries.  Which is created after a machine
153   /// instruction is assembled and uses an address from a temporary label
154   /// created at the current address in the current section and the info from
155   /// the last .loc directive seen as stored in the context.
156   class MCLineEntry : public MCDwarfLoc {
157     MCSymbol *Label;
158
159   private:
160     // Allow the default copy constructor and assignment operator to be used
161     // for an MCLineEntry object.
162
163   public:
164     // Constructor to create an MCLineEntry given a symbol and the dwarf loc.
165     MCLineEntry(MCSymbol *label, const MCDwarfLoc loc) : MCDwarfLoc(loc),
166                 Label(label) {}
167
168     MCSymbol *getLabel() const { return Label; }
169
170     // This is called when an instruction is assembled into the specified
171     // section and if there is information from the last .loc directive that
172     // has yet to have a line entry made for it is made.
173     static void Make(MCStreamer *MCOS, const MCSection *Section);
174   };
175
176   /// MCLineSection - Instances of this class represent the line information
177   /// for a section where machine instructions have been assembled after seeing
178   /// .loc directives.  This is the information used to build the dwarf line
179   /// table for a section.
180   class MCLineSection {
181
182   private:
183     MCLineSection(const MCLineSection&);  // DO NOT IMPLEMENT
184     void operator=(const MCLineSection&); // DO NOT IMPLEMENT
185
186   public:
187     // Constructor to create an MCLineSection with an empty MCLineEntries
188     // vector.
189     MCLineSection() {}
190
191     // addLineEntry - adds an entry to this MCLineSection's line entries
192     void addLineEntry(const MCLineEntry &LineEntry) {
193       MCLineEntries.push_back(LineEntry);
194     }
195
196     typedef std::vector<MCLineEntry> MCLineEntryCollection;
197     typedef MCLineEntryCollection::iterator iterator;
198     typedef MCLineEntryCollection::const_iterator const_iterator;
199
200   private:
201     MCLineEntryCollection MCLineEntries;
202
203   public:
204     const MCLineEntryCollection *getMCLineEntries() const {
205       return &MCLineEntries;
206     }
207   };
208
209   class MCDwarfFileTable {
210   public:
211     //
212     // This emits the Dwarf file and the line tables.
213     //
214     static void Emit(MCStreamer *MCOS);
215   };
216
217   class MCDwarfLineAddr {
218   public:
219     /// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
220     static void Encode(int64_t LineDelta, uint64_t AddrDelta, raw_ostream &OS);
221
222     /// Utility function to emit the encoding to a streamer.
223     static void Emit(MCStreamer *MCOS,
224                      int64_t LineDelta,uint64_t AddrDelta);
225
226     /// Utility function to write the encoding to an object writer.
227     static void Write(MCObjectWriter *OW,
228                       int64_t LineDelta, uint64_t AddrDelta);
229   };
230
231   class MCCFIInstruction {
232   public:
233     enum OpType { Remember, Restore, Move };
234   private:
235     OpType Operation;
236     MCSymbol *Label;
237     // Move to & from location.
238     MachineLocation Destination;
239     MachineLocation Source;
240   public:
241     MCCFIInstruction(OpType Op, MCSymbol *L)
242       : Operation(Op), Label(L) {
243       assert(Op == Remember || Op == Restore);
244     }
245     MCCFIInstruction(MCSymbol *L, const MachineLocation &D,
246                      const MachineLocation &S)
247       : Operation(Move), Label(L), Destination(D), Source(S) {
248     }
249     OpType getOperation() const { return Operation; }
250     MCSymbol *getLabel() const { return Label; }
251     const MachineLocation &getDestination() const { return Destination; }
252     const MachineLocation &getSource() const { return Source; }
253   };
254
255   struct MCDwarfFrameInfo {
256     MCDwarfFrameInfo() : Begin(0), End(0), Personality(0), Lsda(0),
257                          Instructions(), PersonalityEncoding(0),
258                          LsdaEncoding(0) {}
259     MCSymbol *Begin;
260     MCSymbol *End;
261     const MCSymbol *Personality;
262     const MCSymbol *Lsda;
263     std::vector<MCCFIInstruction> Instructions;
264     unsigned PersonalityEncoding;
265     unsigned LsdaEncoding;
266   };
267
268   class MCDwarfFrameEmitter {
269   public:
270     //
271     // This emits the frame info section.
272     //
273     static void Emit(MCStreamer &streamer);
274     static void EmitAdvanceLoc(MCStreamer &Streamer, uint64_t AddrDelta);
275     static void EncodeAdvanceLoc(uint64_t AddrDelta, raw_ostream &OS);
276   };
277 } // end namespace llvm
278
279 #endif