1 //===- MCDwarf.h - Machine Code Dwarf support -------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the declaration of the MCDwarfFile to support the dwarf
11 // .file directive and the .loc directive.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_MC_MCDWARF_H
16 #define LLVM_MC_MCDWARF_H
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"
34 class MCObjectStreamer;
37 /// MCDwarfFile - Instances of this class represent the name of the dwarf
38 /// .file directive and its associated dwarf file number in the MC file,
39 /// and MCDwarfFile's are created and unique'd by the MCContext class where
40 /// the file number for each is its index into the vector of DwarfFiles (note
41 /// index 0 is not used and not a valid dwarf file number).
43 // Name - the base name of the file without its directory path.
44 // The StringRef references memory allocated in the MCContext.
47 // DirIndex - the index into the list of directory names for this file name.
50 private: // MCContext creates and uniques these.
51 friend class MCContext;
52 MCDwarfFile(StringRef name, unsigned dirIndex)
53 : Name(name), DirIndex(dirIndex) {}
55 MCDwarfFile(const MCDwarfFile&); // DO NOT IMPLEMENT
56 void operator=(const MCDwarfFile&); // DO NOT IMPLEMENT
58 /// getName - Get the base name of this MCDwarfFile.
59 StringRef getName() const { return Name; }
61 /// getDirIndex - Get the dirIndex of this MCDwarfFile.
62 unsigned getDirIndex() const { return DirIndex; }
65 /// print - Print the value to the stream \arg OS.
66 void print(raw_ostream &OS) const;
68 /// dump - Print the value to stderr.
72 inline raw_ostream &operator<<(raw_ostream &OS, const MCDwarfFile &DwarfFile){
77 /// MCDwarfLoc - Instances of this class represent the information from a
78 /// dwarf .loc directive.
80 // FileNum - the file number.
82 // Line - the line number.
84 // Column - the column position.
86 // Flags (see #define's below)
91 unsigned Discriminator;
93 // Flag that indicates the initial value of the is_stmt_start flag.
94 #define DWARF2_LINE_DEFAULT_IS_STMT 1
96 #define DWARF2_FLAG_IS_STMT (1 << 0)
97 #define DWARF2_FLAG_BASIC_BLOCK (1 << 1)
98 #define DWARF2_FLAG_PROLOGUE_END (1 << 2)
99 #define DWARF2_FLAG_EPILOGUE_BEGIN (1 << 3)
101 private: // MCContext manages these
102 friend class MCContext;
103 friend class MCLineEntry;
104 MCDwarfLoc(unsigned fileNum, unsigned line, unsigned column, unsigned flags,
105 unsigned isa, unsigned discriminator)
106 : FileNum(fileNum), Line(line), Column(column), Flags(flags), Isa(isa),
107 Discriminator(discriminator) {}
109 // Allow the default copy constructor and assignment operator to be used
110 // for an MCDwarfLoc object.
113 /// getFileNum - Get the FileNum of this MCDwarfLoc.
114 unsigned getFileNum() const { return FileNum; }
116 /// getLine - Get the Line of this MCDwarfLoc.
117 unsigned getLine() const { return Line; }
119 /// getColumn - Get the Column of this MCDwarfLoc.
120 unsigned getColumn() const { return Column; }
122 /// getFlags - Get the Flags of this MCDwarfLoc.
123 unsigned getFlags() const { return Flags; }
125 /// getIsa - Get the Isa of this MCDwarfLoc.
126 unsigned getIsa() const { return Isa; }
128 /// getDiscriminator - Get the Discriminator of this MCDwarfLoc.
129 unsigned getDiscriminator() const { return Discriminator; }
131 /// setFileNum - Set the FileNum of this MCDwarfLoc.
132 void setFileNum(unsigned fileNum) { FileNum = fileNum; }
134 /// setLine - Set the Line of this MCDwarfLoc.
135 void setLine(unsigned line) { Line = line; }
137 /// setColumn - Set the Column of this MCDwarfLoc.
138 void setColumn(unsigned column) { Column = column; }
140 /// setFlags - Set the Flags of this MCDwarfLoc.
141 void setFlags(unsigned flags) { Flags = flags; }
143 /// setIsa - Set the Isa of this MCDwarfLoc.
144 void setIsa(unsigned isa) { Isa = isa; }
146 /// setDiscriminator - Set the Discriminator of this MCDwarfLoc.
147 void setDiscriminator(unsigned discriminator) {
148 Discriminator = discriminator;
152 /// MCLineEntry - Instances of this class represent the line information for
153 /// the dwarf line table entries. Which is created after a machine
154 /// instruction is assembled and uses an address from a temporary label
155 /// created at the current address in the current section and the info from
156 /// the last .loc directive seen as stored in the context.
157 class MCLineEntry : public MCDwarfLoc {
161 // Allow the default copy constructor and assignment operator to be used
162 // for an MCLineEntry object.
165 // Constructor to create an MCLineEntry given a symbol and the dwarf loc.
166 MCLineEntry(MCSymbol *label, const MCDwarfLoc loc) : MCDwarfLoc(loc),
169 MCSymbol *getLabel() const { return Label; }
171 // This is called when an instruction is assembled into the specified
172 // section and if there is information from the last .loc directive that
173 // has yet to have a line entry made for it is made.
174 static void Make(MCStreamer *MCOS, const MCSection *Section);
177 /// MCLineSection - Instances of this class represent the line information
178 /// for a section where machine instructions have been assembled after seeing
179 /// .loc directives. This is the information used to build the dwarf line
180 /// table for a section.
181 class MCLineSection {
184 MCLineSection(const MCLineSection&); // DO NOT IMPLEMENT
185 void operator=(const MCLineSection&); // DO NOT IMPLEMENT
188 // Constructor to create an MCLineSection with an empty MCLineEntries
192 // addLineEntry - adds an entry to this MCLineSection's line entries
193 void addLineEntry(const MCLineEntry &LineEntry) {
194 MCLineEntries.push_back(LineEntry);
197 typedef std::vector<MCLineEntry> MCLineEntryCollection;
198 typedef MCLineEntryCollection::iterator iterator;
199 typedef MCLineEntryCollection::const_iterator const_iterator;
202 MCLineEntryCollection MCLineEntries;
205 const MCLineEntryCollection *getMCLineEntries() const {
206 return &MCLineEntries;
210 class MCDwarfFileTable {
213 // This emits the Dwarf file and the line tables.
215 static void Emit(MCStreamer *MCOS);
218 class MCDwarfLineAddr {
220 /// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
221 static void Encode(int64_t LineDelta, uint64_t AddrDelta, raw_ostream &OS);
223 /// Utility function to emit the encoding to a streamer.
224 static void Emit(MCStreamer *MCOS,
225 int64_t LineDelta,uint64_t AddrDelta);
227 /// Utility function to write the encoding to an object writer.
228 static void Write(MCObjectWriter *OW,
229 int64_t LineDelta, uint64_t AddrDelta);
232 class MCCFIInstruction {
234 enum OpType { SameValue, Remember, Restore, Move, RelMove };
238 // Move to & from location.
239 MachineLocation Destination;
240 MachineLocation Source;
242 MCCFIInstruction(OpType Op, MCSymbol *L)
243 : Operation(Op), Label(L) {
244 assert(Op == Remember || Op == Restore);
246 MCCFIInstruction(OpType Op, MCSymbol *L, unsigned Register)
247 : Operation(Op), Label(L), Destination(Register) {
248 assert(Op == SameValue);
250 MCCFIInstruction(MCSymbol *L, const MachineLocation &D,
251 const MachineLocation &S)
252 : Operation(Move), Label(L), Destination(D), Source(S) {
254 MCCFIInstruction(OpType Op, MCSymbol *L, const MachineLocation &D,
255 const MachineLocation &S)
256 : Operation(Op), Label(L), Destination(D), Source(S) {
257 assert(Op == RelMove);
259 OpType getOperation() const { return Operation; }
260 MCSymbol *getLabel() const { return Label; }
261 const MachineLocation &getDestination() const { return Destination; }
262 const MachineLocation &getSource() const { return Source; }
265 struct MCDwarfFrameInfo {
266 MCDwarfFrameInfo() : Begin(0), End(0), Personality(0), Lsda(0),
267 Function(0), Instructions(), PersonalityEncoding(),
271 const MCSymbol *Personality;
272 const MCSymbol *Lsda;
273 const MCSymbol *Function;
274 std::vector<MCCFIInstruction> Instructions;
275 unsigned PersonalityEncoding;
276 unsigned LsdaEncoding;
279 class MCDwarfFrameEmitter {
282 // This emits the frame info section.
284 static void Emit(MCStreamer &streamer, bool usingCFI);
285 static void EmitAdvanceLoc(MCStreamer &Streamer, uint64_t AddrDelta);
286 static void EncodeAdvanceLoc(uint64_t AddrDelta, raw_ostream &OS);
288 } // end namespace llvm