Parse and remember discriminators in .loc line. I try to output them with
[oota-llvm.git] / include / llvm / MC / MCContext.h
1 //===- MCContext.h - Machine Code Context -----------------------*- 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 #ifndef LLVM_MC_MCCONTEXT_H
11 #define LLVM_MC_MCCONTEXT_H
12
13 #include "llvm/MC/SectionKind.h"
14 #include "llvm/MC/MCDwarf.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/StringMap.h"
17 #include "llvm/Support/Allocator.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include <vector> // FIXME: Shouldn't be needed.
20
21 namespace llvm {
22   class MCAsmInfo;
23   class MCExpr;
24   class MCSection;
25   class MCSymbol;
26   class MCLabel;
27   class MCDwarfFile;
28   class MCDwarfLoc;
29   class MCLineSection;
30   class StringRef;
31   class Twine;
32   class MCSectionMachO;
33   class MCSectionELF;
34
35   /// MCContext - Context object for machine code objects.  This class owns all
36   /// of the sections that it creates.
37   ///
38   class MCContext {
39     MCContext(const MCContext&); // DO NOT IMPLEMENT
40     MCContext &operator=(const MCContext&); // DO NOT IMPLEMENT
41
42     /// The MCAsmInfo for this target.
43     const MCAsmInfo &MAI;
44
45     /// Symbols - Bindings of names to symbols.
46     StringMap<MCSymbol*> Symbols;
47
48     /// NextUniqueID - The next ID to dole out to an unnamed assembler temporary
49     /// symbol.
50     unsigned NextUniqueID;
51
52     /// Instances of directional local labels.
53     DenseMap<unsigned, MCLabel *> Instances;
54     /// NextInstance() creates the next instance of the directional local label
55     /// for the LocalLabelVal and adds it to the map if needed.
56     unsigned NextInstance(int64_t LocalLabelVal);
57     /// GetInstance() gets the current instance of the directional local label
58     /// for the LocalLabelVal and adds it to the map if needed.
59     unsigned GetInstance(int64_t LocalLabelVal);
60     
61     /// The file name of the log file from the enviromment variable
62     /// AS_SECURE_LOG_FILE.  Which must be set before the .secure_log_unique
63     /// directive is used or it is an error.
64     char *SecureLogFile;
65     /// The stream that gets written to for the .secure_log_unique directive.
66     raw_ostream *SecureLog;
67     /// Boolean toggled when .secure_log_unique / .secure_log_reset is seen to
68     /// catch errors if .secure_log_unique appears twice without
69     /// .secure_log_reset appearing between them.
70     bool SecureLogUsed;
71
72     /// The dwarf file and directory tables from the dwarf .file directive.
73     std::vector<MCDwarfFile *> MCDwarfFiles;
74     std::vector<StringRef> MCDwarfDirs;
75
76     /// The current dwarf line information from the last dwarf .loc directive.
77     MCDwarfLoc CurrentDwarfLoc;
78     bool DwarfLocSeen;
79
80     /// The dwarf line information from the .loc directives for the sections
81     /// with assembled machine instructions have after seeing .loc directives.
82     DenseMap<const MCSection *, MCLineSection *> MCLineSections;
83
84     /// Allocator - Allocator object used for creating machine code objects.
85     ///
86     /// We use a bump pointer allocator to avoid the need to track all allocated
87     /// objects.
88     BumpPtrAllocator Allocator;
89     
90     void *MachOUniquingMap, *ELFUniquingMap, *COFFUniquingMap;
91   public:
92     explicit MCContext(const MCAsmInfo &MAI);
93     ~MCContext();
94     
95     const MCAsmInfo &getAsmInfo() const { return MAI; }
96
97     /// @name Symbol Managment
98     /// @{
99     
100     /// CreateTempSymbol - Create and return a new assembler temporary symbol
101     /// with a unique but unspecified name.
102     MCSymbol *CreateTempSymbol();
103
104     /// CreateDirectionalLocalSymbol - Create the defintion of a directional
105     /// local symbol for numbered label (used for "1:" defintions).
106     MCSymbol *CreateDirectionalLocalSymbol(int64_t LocalLabelVal);
107
108     /// GetDirectionalLocalSymbol - Create and return a directional local
109     /// symbol for numbered label (used for "1b" or 1f" references).
110     MCSymbol *GetDirectionalLocalSymbol(int64_t LocalLabelVal, int bORf);
111
112     /// GetOrCreateSymbol - Lookup the symbol inside with the specified
113     /// @p Name.  If it exists, return it.  If not, create a forward
114     /// reference and return it.
115     ///
116     /// @param Name - The symbol name, which must be unique across all symbols.
117     MCSymbol *GetOrCreateSymbol(StringRef Name);
118     MCSymbol *GetOrCreateSymbol(const Twine &Name);
119
120     /// LookupSymbol - Get the symbol for \p Name, or null.
121     MCSymbol *LookupSymbol(StringRef Name) const;
122
123     /// @}
124     
125     /// @name Section Managment
126     /// @{
127
128     /// getMachOSection - Return the MCSection for the specified mach-o section.
129     /// This requires the operands to be valid.
130     const MCSectionMachO *getMachOSection(StringRef Segment,
131                                           StringRef Section,
132                                           unsigned TypeAndAttributes,
133                                           unsigned Reserved2,
134                                           SectionKind K);
135     const MCSectionMachO *getMachOSection(StringRef Segment,
136                                           StringRef Section,
137                                           unsigned TypeAndAttributes,
138                                           SectionKind K) {
139       return getMachOSection(Segment, Section, TypeAndAttributes, 0, K);
140     }
141
142     const MCSectionELF *getELFSection(StringRef Section, unsigned Type,
143                                       unsigned Flags, SectionKind Kind);
144
145     const MCSectionELF *getELFSection(StringRef Section, unsigned Type,
146                                       unsigned Flags, SectionKind Kind,
147                                       unsigned EntrySize, StringRef Group);
148
149     const MCSectionELF *CreateELFGroupSection();
150
151     const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
152                                     int Selection, SectionKind Kind);
153
154     const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
155                                     SectionKind Kind) {
156       return getCOFFSection (Section, Characteristics, 0, Kind);
157     }
158
159     
160     /// @}
161
162     /// @name Dwarf Managment
163     /// @{
164
165     /// GetDwarfFile - creates an entry in the dwarf file and directory tables.
166     unsigned GetDwarfFile(StringRef FileName, unsigned FileNumber);
167
168     bool isValidDwarfFileNumber(unsigned FileNumber);
169
170     bool hasDwarfFiles(void) {
171       return MCDwarfFiles.size() != 0;
172     }
173
174     const std::vector<MCDwarfFile *> &getMCDwarfFiles() {
175       return MCDwarfFiles;
176     }
177     const std::vector<StringRef> &getMCDwarfDirs() {
178       return MCDwarfDirs;
179     }
180     DenseMap<const MCSection *, MCLineSection *> &getMCLineSections() {
181       return MCLineSections;
182     }
183
184     /// setCurrentDwarfLoc - saves the information from the currently parsed
185     /// dwarf .loc directive and sets DwarfLocSeen.  When the next instruction
186     /// is assembled an entry in the line number table with this information and
187     /// the address of the instruction will be created.
188     void setCurrentDwarfLoc(unsigned FileNum, unsigned Line, unsigned Column,
189                             unsigned Flags, unsigned Isa,
190                             unsigned Discriminator) {
191       CurrentDwarfLoc.setFileNum(FileNum);
192       CurrentDwarfLoc.setLine(Line);
193       CurrentDwarfLoc.setColumn(Column);
194       CurrentDwarfLoc.setFlags(Flags);
195       CurrentDwarfLoc.setIsa(Isa);
196       CurrentDwarfLoc.setDiscriminator(Discriminator);
197       DwarfLocSeen = true;
198     }
199     void ClearDwarfLocSeen() { DwarfLocSeen = false; }
200
201     bool getDwarfLocSeen() { return DwarfLocSeen; }
202     const MCDwarfLoc &getCurrentDwarfLoc() { return CurrentDwarfLoc; }
203
204     /// @}
205
206     char *getSecureLogFile() { return SecureLogFile; }
207     raw_ostream *getSecureLog() { return SecureLog; }
208     bool getSecureLogUsed() { return SecureLogUsed; }
209     void setSecureLog(raw_ostream *Value) {
210       SecureLog = Value;
211     }
212     void setSecureLogUsed(bool Value) {
213       SecureLogUsed = Value;
214     }
215
216     void *Allocate(unsigned Size, unsigned Align = 8) {
217       return Allocator.Allocate(Size, Align);
218     }
219     void Deallocate(void *Ptr) {
220     }
221   };
222
223 } // end namespace llvm
224
225 // operator new and delete aren't allowed inside namespaces.
226 // The throw specifications are mandated by the standard.
227 /// @brief Placement new for using the MCContext's allocator.
228 ///
229 /// This placement form of operator new uses the MCContext's allocator for
230 /// obtaining memory. It is a non-throwing new, which means that it returns
231 /// null on error. (If that is what the allocator does. The current does, so if
232 /// this ever changes, this operator will have to be changed, too.)
233 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
234 /// @code
235 /// // Default alignment (16)
236 /// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
237 /// // Specific alignment
238 /// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
239 /// @endcode
240 /// Please note that you cannot use delete on the pointer; it must be
241 /// deallocated using an explicit destructor call followed by
242 /// @c Context.Deallocate(Ptr).
243 ///
244 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
245 /// @param C The MCContext that provides the allocator.
246 /// @param Alignment The alignment of the allocated memory (if the underlying
247 ///                  allocator supports it).
248 /// @return The allocated memory. Could be NULL.
249 inline void *operator new(size_t Bytes, llvm::MCContext &C,
250                           size_t Alignment = 16) throw () {
251   return C.Allocate(Bytes, Alignment);
252 }
253 /// @brief Placement delete companion to the new above.
254 ///
255 /// This operator is just a companion to the new above. There is no way of
256 /// invoking it directly; see the new operator for more details. This operator
257 /// is called implicitly by the compiler if a placement new expression using
258 /// the MCContext throws in the object constructor.
259 inline void operator delete(void *Ptr, llvm::MCContext &C, size_t)
260               throw () {
261   C.Deallocate(Ptr);
262 }
263
264 /// This placement form of operator new[] uses the MCContext's allocator for
265 /// obtaining memory. It is a non-throwing new[], which means that it returns
266 /// null on error.
267 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
268 /// @code
269 /// // Default alignment (16)
270 /// char *data = new (Context) char[10];
271 /// // Specific alignment
272 /// char *data = new (Context, 8) char[10];
273 /// @endcode
274 /// Please note that you cannot use delete on the pointer; it must be
275 /// deallocated using an explicit destructor call followed by
276 /// @c Context.Deallocate(Ptr).
277 ///
278 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
279 /// @param C The MCContext that provides the allocator.
280 /// @param Alignment The alignment of the allocated memory (if the underlying
281 ///                  allocator supports it).
282 /// @return The allocated memory. Could be NULL.
283 inline void *operator new[](size_t Bytes, llvm::MCContext& C,
284                             size_t Alignment = 16) throw () {
285   return C.Allocate(Bytes, Alignment);
286 }
287
288 /// @brief Placement delete[] companion to the new[] above.
289 ///
290 /// This operator is just a companion to the new[] above. There is no way of
291 /// invoking it directly; see the new[] operator for more details. This operator
292 /// is called implicitly by the compiler if a placement new[] expression using
293 /// the MCContext throws in the object constructor.
294 inline void operator delete[](void *Ptr, llvm::MCContext &C) throw () {
295   C.Deallocate(Ptr);
296 }
297
298 #endif