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/Support/Compiler.h"
20 #include "llvm/Support/Dwarf.h"
21 #include "llvm/Support/raw_ostream.h"
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).
39 // Name - the base name of the file without its directory path.
40 // The StringRef references memory allocated in the MCContext.
43 // DirIndex - the index into the list of directory names for this file name.
46 private: // MCContext creates and uniques these.
47 friend class MCContext;
48 MCDwarfFile(StringRef name, unsigned dirIndex)
49 : Name(name), DirIndex(dirIndex) {}
51 MCDwarfFile(const MCDwarfFile&) LLVM_DELETED_FUNCTION;
52 void operator=(const MCDwarfFile&) LLVM_DELETED_FUNCTION;
54 /// getName - Get the base name of this MCDwarfFile.
55 StringRef getName() const { return Name; }
57 /// getDirIndex - Get the dirIndex of this MCDwarfFile.
58 unsigned getDirIndex() const { return DirIndex; }
61 /// print - Print the value to the stream \p OS.
62 void print(raw_ostream &OS) const;
64 /// dump - Print the value to stderr.
68 inline raw_ostream &operator<<(raw_ostream &OS, const MCDwarfFile &DwarfFile){
73 /// MCDwarfLoc - Instances of this class represent the information from a
74 /// dwarf .loc directive.
76 // FileNum - the file number.
78 // Line - the line number.
80 // Column - the column position.
82 // Flags (see #define's below)
87 unsigned Discriminator;
89 // Flag that indicates the initial value of the is_stmt_start flag.
90 #define DWARF2_LINE_DEFAULT_IS_STMT 1
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)
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) {}
105 // Allow the default copy constructor and assignment operator to be used
106 // for an MCDwarfLoc object.
109 /// getFileNum - Get the FileNum of this MCDwarfLoc.
110 unsigned getFileNum() const { return FileNum; }
112 /// getLine - Get the Line of this MCDwarfLoc.
113 unsigned getLine() const { return Line; }
115 /// getColumn - Get the Column of this MCDwarfLoc.
116 unsigned getColumn() const { return Column; }
118 /// getFlags - Get the Flags of this MCDwarfLoc.
119 unsigned getFlags() const { return Flags; }
121 /// getIsa - Get the Isa of this MCDwarfLoc.
122 unsigned getIsa() const { return Isa; }
124 /// getDiscriminator - Get the Discriminator of this MCDwarfLoc.
125 unsigned getDiscriminator() const { return Discriminator; }
127 /// setFileNum - Set the FileNum of this MCDwarfLoc.
128 void setFileNum(unsigned fileNum) { FileNum = fileNum; }
130 /// setLine - Set the Line of this MCDwarfLoc.
131 void setLine(unsigned line) { Line = line; }
133 /// setColumn - Set the Column of this MCDwarfLoc.
134 void setColumn(unsigned column) { Column = column; }
136 /// setFlags - Set the Flags of this MCDwarfLoc.
137 void setFlags(unsigned flags) { Flags = flags; }
139 /// setIsa - Set the Isa of this MCDwarfLoc.
140 void setIsa(unsigned isa) { Isa = isa; }
142 /// setDiscriminator - Set the Discriminator of this MCDwarfLoc.
143 void setDiscriminator(unsigned discriminator) {
144 Discriminator = discriminator;
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 {
157 // Allow the default copy constructor and assignment operator to be used
158 // for an MCLineEntry object.
161 // Constructor to create an MCLineEntry given a symbol and the dwarf loc.
162 MCLineEntry(MCSymbol *label, const MCDwarfLoc loc) : MCDwarfLoc(loc),
165 MCSymbol *getLabel() const { return Label; }
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);
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 {
180 MCLineSection(const MCLineSection&) LLVM_DELETED_FUNCTION;
181 void operator=(const MCLineSection&) LLVM_DELETED_FUNCTION;
184 // Constructor to create an MCLineSection with an empty MCLineEntries
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);
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;
199 // A collection of MCLineEntry for each Compile Unit ID.
200 MCLineDivisionMap MCLineDivisions;
203 // Returns whether MCLineSection contains entries for a given Compile
205 bool containEntriesForID(unsigned CUID) const {
206 return MCLineDivisions.count(CUID);
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;
216 class MCDwarfFileTable {
219 // This emits the Dwarf file and the line tables for all Compile Units.
221 static const MCSymbol *Emit(MCStreamer *MCOS);
223 // This emits the Dwarf file and the line tables for a given Compile Unit.
225 static const MCSymbol *EmitCU(MCStreamer *MCOS, unsigned ID);
228 class MCDwarfLineAddr {
230 /// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
231 static void Encode(MCContext &Context, int64_t LineDelta,
232 uint64_t AddrDelta, raw_ostream &OS);
234 /// Utility function to emit the encoding to a streamer.
235 static void Emit(MCStreamer *MCOS,
236 int64_t LineDelta,uint64_t AddrDelta);
239 class MCGenDwarfInfo {
242 // When generating dwarf for assembly source files this emits the Dwarf
245 static void Emit(MCStreamer *MCOS, const MCSymbol *LineSectionSymbol);
248 // When generating dwarf for assembly source files this is the info that is
249 // needed to be gathered for each symbol that will have a dwarf label.
250 class MCGenDwarfLabelEntry {
252 // Name of the symbol without a leading underbar, if any.
254 // The dwarf file number this symbol is in.
256 // The line number this symbol is at.
258 // The low_pc for the dwarf label is taken from this symbol.
262 MCGenDwarfLabelEntry(StringRef name, unsigned fileNumber,
263 unsigned lineNumber, MCSymbol *label) :
264 Name(name), FileNumber(fileNumber), LineNumber(lineNumber), Label(label){}
266 StringRef getName() const { return Name; }
267 unsigned getFileNumber() const { return FileNumber; }
268 unsigned getLineNumber() const { return LineNumber; }
269 MCSymbol *getLabel() const { return Label; }
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,
277 class MCCFIInstruction {
279 enum OpType { OpSameValue, OpRememberState, OpRestoreState, OpOffset,
280 OpDefCfaRegister, OpDefCfaOffset, OpDefCfa, OpRelOffset,
281 OpAdjustCfaOffset, OpEscape, OpRestore, OpUndefined,
291 std::vector<char> Values;
293 MCCFIInstruction(OpType Op, MCSymbol *L, unsigned R, int O, StringRef V) :
294 Operation(Op), Label(L), Register(R), Offset(O),
295 Values(V.begin(), V.end()) {
296 assert(Op != OpRegister);
299 MCCFIInstruction(OpType Op, MCSymbol *L, unsigned R1, unsigned R2) :
300 Operation(Op), Label(L), Register(R1), Register2(R2) {
301 assert(Op == OpRegister);
305 static MCCFIInstruction
306 createOffset(MCSymbol *L, unsigned Register, int Offset) {
307 return MCCFIInstruction(OpOffset, L, Register, Offset, "");
310 static MCCFIInstruction
311 createDefCfaRegister(MCSymbol *L, unsigned Register) {
312 return MCCFIInstruction(OpDefCfaRegister, L, Register, 0, "");
315 static MCCFIInstruction createDefCfaOffset(MCSymbol *L, int Offset) {
316 return MCCFIInstruction(OpDefCfaOffset, L, 0, -Offset, "");
319 static MCCFIInstruction
320 createDefCfa(MCSymbol *L, unsigned Register, int Offset) {
321 return MCCFIInstruction(OpDefCfa, L, Register, -Offset, "");
324 static MCCFIInstruction createUndefined(MCSymbol *L, unsigned Register) {
325 return MCCFIInstruction(OpUndefined, L, Register, 0, "");
328 static MCCFIInstruction createRestore(MCSymbol *L, unsigned Register) {
329 return MCCFIInstruction(OpRestore, L, Register, 0, "");
332 static MCCFIInstruction createSameValue(MCSymbol *L, unsigned Register) {
333 return MCCFIInstruction(OpSameValue, L, Register, 0, "");
336 static MCCFIInstruction createRestoreState(MCSymbol *L) {
337 return MCCFIInstruction(OpRestoreState, L, 0, 0, "");
340 static MCCFIInstruction createRememberState(MCSymbol *L) {
341 return MCCFIInstruction(OpRememberState, L, 0, 0, "");
344 static MCCFIInstruction
345 createRelOffset(MCSymbol *L, unsigned Register, int Offset) {
346 return MCCFIInstruction(OpRelOffset, L, Register, Offset, "");
349 static MCCFIInstruction
350 createAdjustCfaOffset(MCSymbol *L, int Adjustment) {
351 return MCCFIInstruction(OpAdjustCfaOffset, L, 0, Adjustment, "");
354 static MCCFIInstruction createEscape(MCSymbol *L, StringRef Vals) {
355 return MCCFIInstruction(OpEscape, L, 0, 0, Vals);
358 static MCCFIInstruction
359 createRegister(MCSymbol *L, unsigned Register1, unsigned Register2) {
360 return MCCFIInstruction(OpRegister, L, Register1, Register2);
363 OpType getOperation() const { return Operation; }
364 MCSymbol *getLabel() const { return Label; }
366 unsigned getRegister() const {
367 assert(Operation == OpDefCfa || Operation == OpOffset ||
368 Operation == OpRestore || Operation == OpUndefined ||
369 Operation == OpSameValue || Operation == OpDefCfaRegister ||
370 Operation == OpRelOffset || Operation == OpRegister);
374 unsigned getRegister2() const {
375 assert(Operation == OpRegister);
379 int getOffset() const {
380 assert(Operation == OpDefCfa || Operation == OpOffset ||
381 Operation == OpRelOffset || Operation == OpDefCfaOffset ||
382 Operation == OpAdjustCfaOffset);
386 const StringRef getValues() const {
387 assert(Operation == OpEscape);
388 return StringRef(&Values[0], Values.size());
392 struct MCDwarfFrameInfo {
393 MCDwarfFrameInfo() : Begin(0), End(0), Personality(0), Lsda(0),
394 Function(0), Instructions(), PersonalityEncoding(),
395 LsdaEncoding(0), CompactUnwindEncoding(0),
396 IsSignalFrame(false) {}
399 const MCSymbol *Personality;
400 const MCSymbol *Lsda;
401 const MCSymbol *Function;
402 std::vector<MCCFIInstruction> Instructions;
403 unsigned PersonalityEncoding;
404 unsigned LsdaEncoding;
405 uint32_t CompactUnwindEncoding;
409 class MCDwarfFrameEmitter {
412 // This emits the frame info section.
414 static void Emit(MCStreamer &streamer, bool usingCFI,
416 static void EmitAdvanceLoc(MCStreamer &Streamer, uint64_t AddrDelta);
417 static void EncodeAdvanceLoc(MCContext &Context, uint64_t AddrDelta,
420 } // end namespace llvm