Did my commit for the last patch for the .loc directory from the wrong place and
[oota-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/MC/MCStreamer.h"
20 #include "llvm/MC/MCObjectStreamer.h"
21 #include "llvm/MC/MCObjectWriter.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include "llvm/Support/Dwarf.h"
24 #include <vector>
25
26 namespace llvm {
27   class MCContext;
28   class MCSection;
29   class MCSymbol;
30   class MCObjectStreamer;
31   class raw_ostream;
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&);       // DO NOT IMPLEMENT
52     void operator=(const MCDwarfFile&); // DO NOT IMPLEMENT
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 \arg 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
87 // Flag that indicates the initial value of the is_stmt_start flag.
88 #define DWARF2_LINE_DEFAULT_IS_STMT     1
89
90 #define DWARF2_FLAG_IS_STMT        (1 << 0)
91 #define DWARF2_FLAG_BASIC_BLOCK    (1 << 1)
92 #define DWARF2_FLAG_PROLOGUE_END   (1 << 2)
93 #define DWARF2_FLAG_EPILOGUE_BEGIN (1 << 3)
94
95   private:  // MCContext manages these
96     friend class MCContext;
97     friend class MCLineEntry;
98     MCDwarfLoc(unsigned fileNum, unsigned line, unsigned column, unsigned flags,
99                unsigned isa)
100       : FileNum(fileNum), Line(line), Column(column), Flags(flags), Isa(isa) {}
101
102     // Allow the default copy constructor and assignment operator to be used
103     // for an MCDwarfLoc object.
104
105   public:
106     /// getFileNum - Get the FileNum of this MCDwarfLoc.
107     unsigned getFileNum() { return FileNum; }
108
109     /// getLine - Get the Line of this MCDwarfLoc.
110     unsigned getLine() { return Line; }
111
112     /// getColumn - Get the Column of this MCDwarfLoc.
113     unsigned getColumn() { return Column; }
114
115     /// getFlags - Get the Flags of this MCDwarfLoc.
116     unsigned getFlags() { return Flags; }
117
118     /// getIsa - Get the Isa of this MCDwarfLoc.
119     unsigned getIsa() { return Isa; }
120
121     /// setFileNum - Set the FileNum of this MCDwarfLoc.
122     void setFileNum(unsigned fileNum) { FileNum = fileNum; }
123
124     /// setLine - Set the Line of this MCDwarfLoc.
125     void setLine(unsigned line) { Line = line; }
126
127     /// setColumn - Set the Column of this MCDwarfLoc.
128     void setColumn(unsigned column) { Column = column; }
129
130     /// setFlags - Set the Flags of this MCDwarfLoc.
131     void setFlags(unsigned flags) { Flags = flags; }
132
133     /// setIsa - Set the Isa of this MCDwarfLoc.
134     void setIsa(unsigned isa) { Isa = isa; }
135   };
136
137   /// MCLineEntry - Instances of this class represent the line information for
138   /// the dwarf line table entries.  Which is created after a machine
139   /// instruction is assembled and uses an address from a temporary label
140   /// created at the current address in the current section and the info from
141   /// the last .loc directive seen as stored in the context.
142   class MCLineEntry : public MCDwarfLoc {
143     MCSymbol *Label;
144
145   private:
146     // Allow the default copy constructor and assignment operator to be used
147     // for an MCLineEntry object.
148
149   public:
150     // Constructor to create an MCLineEntry given a symbol and the dwarf loc.
151     MCLineEntry(MCSymbol *label, const MCDwarfLoc loc) : MCDwarfLoc(loc),
152                 Label(label) {}
153
154     MCSymbol *getLabel() { return Label; }
155
156     // This is called when an instruction is assembled into the specified
157     // section and if there is information from the last .loc directive that
158     // has yet to have a line entry made for it is made.
159     static void Make(MCObjectStreamer *MCOS, const MCSection *Section);
160   };
161
162   /// MCLineSection - Instances of this class represent the line information
163   /// for a section where machine instructions have been assembled after seeing
164   /// .loc directives.  This is the information used to build the dwarf line
165   /// table for a section.
166   class MCLineSection {
167
168   private:
169     MCLineSection(const MCLineSection&);  // DO NOT IMPLEMENT
170     void operator=(const MCLineSection&); // DO NOT IMPLEMENT
171
172   public:
173     // Constructor to create an MCLineSection with an empty MCLineEntries
174     // vector.
175     MCLineSection() {}
176
177     // addLineEntry - adds an entry to this MCLineSection's line entries
178     void addLineEntry(const MCLineEntry &LineEntry) {
179       MCLineEntries.push_back(LineEntry);
180     }
181
182     typedef std::vector<MCLineEntry> MCLineEntryCollection;
183     typedef MCLineEntryCollection::iterator iterator;
184
185   private:
186     MCLineEntryCollection MCLineEntries;
187
188   public:
189     MCLineEntryCollection *getMCLineEntries() { return &MCLineEntries; }
190   };
191
192   class MCDwarfFileTable {
193   public:
194     //
195     // This emits the Dwarf file and the line tables.
196     //
197     static void Emit(MCObjectStreamer *MCOS, const MCSection *DwarfLineSection);
198   };
199
200   class MCDwarfLineAddr {
201   public:
202     /// Utility function to encode a Dwarf pair of LineDelta and AddrDeltas.
203     static void Encode(int64_t LineDelta, uint64_t AddrDelta, raw_ostream &OS);
204
205     /// Utility function to emit the encoding to a streamer.
206     static void Emit(MCObjectStreamer *MCOS,
207                      int64_t LineDelta,uint64_t AddrDelta);
208
209     /// Utility function to compute the size of the encoding.
210     static uint64_t ComputeSize(int64_t LineDelta, uint64_t AddrDelta);
211
212     /// Utility function to write the encoding to an object writer.
213     static void Write(MCObjectWriter *OW,
214                       int64_t LineDelta, uint64_t AddrDelta);
215   };
216 } // end namespace llvm
217
218 #endif