First part of support for generating dwarf for assembly source files with the
[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 MCObjectFileInfo;
30   class MCRegisterInfo;
31   class MCLineSection;
32   class StringRef;
33   class Twine;
34   class MCSectionMachO;
35   class MCSectionELF;
36
37   /// MCContext - Context object for machine code objects.  This class owns all
38   /// of the sections that it creates.
39   ///
40   class MCContext {
41     MCContext(const MCContext&); // DO NOT IMPLEMENT
42     MCContext &operator=(const MCContext&); // DO NOT IMPLEMENT
43   public:
44     typedef StringMap<MCSymbol*, BumpPtrAllocator&> SymbolTable;
45   private:
46
47     /// The MCAsmInfo for this target.
48     const MCAsmInfo &MAI;
49
50     /// The MCRegisterInfo for this target.
51     const MCRegisterInfo &MRI;
52
53     /// The MCObjectFileInfo for this target.
54     const MCObjectFileInfo *MOFI;
55
56     /// Allocator - Allocator object used for creating machine code objects.
57     ///
58     /// We use a bump pointer allocator to avoid the need to track all allocated
59     /// objects.
60     BumpPtrAllocator Allocator;
61
62     /// Symbols - Bindings of names to symbols.
63     SymbolTable Symbols;
64
65     /// UsedNames - Keeps tracks of names that were used both for used declared
66     /// and artificial symbols.
67     StringMap<bool, BumpPtrAllocator&> UsedNames;
68
69     /// NextUniqueID - The next ID to dole out to an unnamed assembler temporary
70     /// symbol.
71     unsigned NextUniqueID;
72
73     /// Instances of directional local labels.
74     DenseMap<unsigned, MCLabel *> Instances;
75     /// NextInstance() creates the next instance of the directional local label
76     /// for the LocalLabelVal and adds it to the map if needed.
77     unsigned NextInstance(int64_t LocalLabelVal);
78     /// GetInstance() gets the current instance of the directional local label
79     /// for the LocalLabelVal and adds it to the map if needed.
80     unsigned GetInstance(int64_t LocalLabelVal);
81
82     /// The file name of the log file from the environment variable
83     /// AS_SECURE_LOG_FILE.  Which must be set before the .secure_log_unique
84     /// directive is used or it is an error.
85     char *SecureLogFile;
86     /// The stream that gets written to for the .secure_log_unique directive.
87     raw_ostream *SecureLog;
88     /// Boolean toggled when .secure_log_unique / .secure_log_reset is seen to
89     /// catch errors if .secure_log_unique appears twice without
90     /// .secure_log_reset appearing between them.
91     bool SecureLogUsed;
92
93     /// The dwarf file and directory tables from the dwarf .file directive.
94     std::vector<MCDwarfFile *> MCDwarfFiles;
95     std::vector<StringRef> MCDwarfDirs;
96
97     /// The current dwarf line information from the last dwarf .loc directive.
98     MCDwarfLoc CurrentDwarfLoc;
99     bool DwarfLocSeen;
100
101     /// Generate dwarf debugging info for assembly source files.
102     bool GenDwarfForAssembly;
103
104     /// The current dwarf file number when generate dwarf debugging info for
105     /// assembly source files.
106     unsigned GenDwarfFileNumber;
107
108     /// The default initial text section that we generate dwarf debugging line
109     /// info for when generating dwarf assembly source files.
110     const MCSection *GenDwarfSection;
111
112     /// Honor temporary labels, this is useful for debugging semantic
113     /// differences between temporary and non-temporary labels (primarily on
114     /// Darwin).
115     bool AllowTemporaryLabels;
116
117     /// The dwarf line information from the .loc directives for the sections
118     /// with assembled machine instructions have after seeing .loc directives.
119     DenseMap<const MCSection *, MCLineSection *> MCLineSections;
120     /// We need a deterministic iteration order, so we remember the order
121     /// the elements were added.
122     std::vector<const MCSection *> MCLineSectionOrder;
123
124     void *MachOUniquingMap, *ELFUniquingMap, *COFFUniquingMap;
125
126     MCSymbol *CreateSymbol(StringRef Name);
127
128   public:
129     explicit MCContext(const MCAsmInfo &MAI, const MCRegisterInfo &MRI,
130                        const MCObjectFileInfo *MOFI);
131     ~MCContext();
132
133     const MCAsmInfo &getAsmInfo() const { return MAI; }
134
135     const MCRegisterInfo &getRegisterInfo() const { return MRI; }
136
137     const MCObjectFileInfo *getObjectFileInfo() const { return MOFI; }
138
139     void setAllowTemporaryLabels(bool Value) { AllowTemporaryLabels = Value; }
140
141     /// @name Symbol Management
142     /// @{
143
144     /// CreateTempSymbol - Create and return a new assembler temporary symbol
145     /// with a unique but unspecified name.
146     MCSymbol *CreateTempSymbol();
147
148     /// CreateDirectionalLocalSymbol - Create the definition of a directional
149     /// local symbol for numbered label (used for "1:" definitions).
150     MCSymbol *CreateDirectionalLocalSymbol(int64_t LocalLabelVal);
151
152     /// GetDirectionalLocalSymbol - Create and return a directional local
153     /// symbol for numbered label (used for "1b" or 1f" references).
154     MCSymbol *GetDirectionalLocalSymbol(int64_t LocalLabelVal, int bORf);
155
156     /// GetOrCreateSymbol - Lookup the symbol inside with the specified
157     /// @p Name.  If it exists, return it.  If not, create a forward
158     /// reference and return it.
159     ///
160     /// @param Name - The symbol name, which must be unique across all symbols.
161     MCSymbol *GetOrCreateSymbol(StringRef Name);
162     MCSymbol *GetOrCreateSymbol(const Twine &Name);
163
164     /// LookupSymbol - Get the symbol for \p Name, or null.
165     MCSymbol *LookupSymbol(StringRef Name) const;
166
167     /// getSymbols - Get a reference for the symbol table for clients that
168     /// want to, for example, iterate over all symbols. 'const' because we
169     /// still want any modifications to the table itself to use the MCContext
170     /// APIs.
171     const SymbolTable &getSymbols() const {
172       return Symbols;
173     }
174
175     /// @}
176
177     /// @name Section Management
178     /// @{
179
180     /// getMachOSection - Return the MCSection for the specified mach-o section.
181     /// This requires the operands to be valid.
182     const MCSectionMachO *getMachOSection(StringRef Segment,
183                                           StringRef Section,
184                                           unsigned TypeAndAttributes,
185                                           unsigned Reserved2,
186                                           SectionKind K);
187     const MCSectionMachO *getMachOSection(StringRef Segment,
188                                           StringRef Section,
189                                           unsigned TypeAndAttributes,
190                                           SectionKind K) {
191       return getMachOSection(Segment, Section, TypeAndAttributes, 0, K);
192     }
193
194     const MCSectionELF *getELFSection(StringRef Section, unsigned Type,
195                                       unsigned Flags, SectionKind Kind);
196
197     const MCSectionELF *getELFSection(StringRef Section, unsigned Type,
198                                       unsigned Flags, SectionKind Kind,
199                                       unsigned EntrySize, StringRef Group);
200
201     const MCSectionELF *CreateELFGroupSection();
202
203     const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
204                                     int Selection, SectionKind Kind);
205
206     const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
207                                     SectionKind Kind) {
208       return getCOFFSection (Section, Characteristics, 0, Kind);
209     }
210
211
212     /// @}
213
214     /// @name Dwarf Management
215     /// @{
216
217     /// GetDwarfFile - creates an entry in the dwarf file and directory tables.
218     unsigned GetDwarfFile(StringRef Directory, StringRef FileName,
219                           unsigned FileNumber);
220
221     bool isValidDwarfFileNumber(unsigned FileNumber);
222
223     bool hasDwarfFiles() const {
224       return !MCDwarfFiles.empty();
225     }
226
227     const std::vector<MCDwarfFile *> &getMCDwarfFiles() {
228       return MCDwarfFiles;
229     }
230     const std::vector<StringRef> &getMCDwarfDirs() {
231       return MCDwarfDirs;
232     }
233
234     const DenseMap<const MCSection *, MCLineSection *>
235     &getMCLineSections() const {
236       return MCLineSections;
237     }
238     const std::vector<const MCSection *> &getMCLineSectionOrder() const {
239       return MCLineSectionOrder;
240     }
241     void addMCLineSection(const MCSection *Sec, MCLineSection *Line) {
242       MCLineSections[Sec] = Line;
243       MCLineSectionOrder.push_back(Sec);
244     }
245
246     /// setCurrentDwarfLoc - saves the information from the currently parsed
247     /// dwarf .loc directive and sets DwarfLocSeen.  When the next instruction
248     /// is assembled an entry in the line number table with this information and
249     /// the address of the instruction will be created.
250     void setCurrentDwarfLoc(unsigned FileNum, unsigned Line, unsigned Column,
251                             unsigned Flags, unsigned Isa,
252                             unsigned Discriminator) {
253       CurrentDwarfLoc.setFileNum(FileNum);
254       CurrentDwarfLoc.setLine(Line);
255       CurrentDwarfLoc.setColumn(Column);
256       CurrentDwarfLoc.setFlags(Flags);
257       CurrentDwarfLoc.setIsa(Isa);
258       CurrentDwarfLoc.setDiscriminator(Discriminator);
259       DwarfLocSeen = true;
260     }
261     void ClearDwarfLocSeen() { DwarfLocSeen = false; }
262
263     bool getDwarfLocSeen() { return DwarfLocSeen; }
264     const MCDwarfLoc &getCurrentDwarfLoc() { return CurrentDwarfLoc; }
265
266     bool getGenDwarfForAssembly() { return GenDwarfForAssembly; }
267     void setGenDwarfForAssembly(bool Value) { GenDwarfForAssembly = Value; }
268     unsigned getGenDwarfFileNumber() { return GenDwarfFileNumber; }
269     unsigned nextGenDwarfFileNumber() { return ++GenDwarfFileNumber; }
270     const MCSection *getGenDwarfSection() { return GenDwarfSection; }
271     void setGenDwarfSection(const MCSection *Sec) { GenDwarfSection = Sec; }
272
273     /// @}
274
275     char *getSecureLogFile() { return SecureLogFile; }
276     raw_ostream *getSecureLog() { return SecureLog; }
277     bool getSecureLogUsed() { return SecureLogUsed; }
278     void setSecureLog(raw_ostream *Value) {
279       SecureLog = Value;
280     }
281     void setSecureLogUsed(bool Value) {
282       SecureLogUsed = Value;
283     }
284
285     void *Allocate(unsigned Size, unsigned Align = 8) {
286       return Allocator.Allocate(Size, Align);
287     }
288     void Deallocate(void *Ptr) {
289     }
290   };
291
292 } // end namespace llvm
293
294 // operator new and delete aren't allowed inside namespaces.
295 // The throw specifications are mandated by the standard.
296 /// @brief Placement new for using the MCContext's allocator.
297 ///
298 /// This placement form of operator new uses the MCContext's allocator for
299 /// obtaining memory. It is a non-throwing new, which means that it returns
300 /// null on error. (If that is what the allocator does. The current does, so if
301 /// this ever changes, this operator will have to be changed, too.)
302 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
303 /// @code
304 /// // Default alignment (16)
305 /// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
306 /// // Specific alignment
307 /// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
308 /// @endcode
309 /// Please note that you cannot use delete on the pointer; it must be
310 /// deallocated using an explicit destructor call followed by
311 /// @c Context.Deallocate(Ptr).
312 ///
313 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
314 /// @param C The MCContext that provides the allocator.
315 /// @param Alignment The alignment of the allocated memory (if the underlying
316 ///                  allocator supports it).
317 /// @return The allocated memory. Could be NULL.
318 inline void *operator new(size_t Bytes, llvm::MCContext &C,
319                           size_t Alignment = 16) throw () {
320   return C.Allocate(Bytes, Alignment);
321 }
322 /// @brief Placement delete companion to the new above.
323 ///
324 /// This operator is just a companion to the new above. There is no way of
325 /// invoking it directly; see the new operator for more details. This operator
326 /// is called implicitly by the compiler if a placement new expression using
327 /// the MCContext throws in the object constructor.
328 inline void operator delete(void *Ptr, llvm::MCContext &C, size_t)
329               throw () {
330   C.Deallocate(Ptr);
331 }
332
333 /// This placement form of operator new[] uses the MCContext's allocator for
334 /// obtaining memory. It is a non-throwing new[], which means that it returns
335 /// null on error.
336 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
337 /// @code
338 /// // Default alignment (16)
339 /// char *data = new (Context) char[10];
340 /// // Specific alignment
341 /// char *data = new (Context, 8) char[10];
342 /// @endcode
343 /// Please note that you cannot use delete on the pointer; it must be
344 /// deallocated using an explicit destructor call followed by
345 /// @c Context.Deallocate(Ptr).
346 ///
347 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
348 /// @param C The MCContext that provides the allocator.
349 /// @param Alignment The alignment of the allocated memory (if the underlying
350 ///                  allocator supports it).
351 /// @return The allocated memory. Could be NULL.
352 inline void *operator new[](size_t Bytes, llvm::MCContext& C,
353                             size_t Alignment = 16) throw () {
354   return C.Allocate(Bytes, Alignment);
355 }
356
357 /// @brief Placement delete[] companion to the new[] above.
358 ///
359 /// This operator is just a companion to the new[] above. There is no way of
360 /// invoking it directly; see the new[] operator for more details. This operator
361 /// is called implicitly by the compiler if a placement new[] expression using
362 /// the MCContext throws in the object constructor.
363 inline void operator delete[](void *Ptr, llvm::MCContext &C) throw () {
364   C.Deallocate(Ptr);
365 }
366
367 #endif