a49a35c8d5baa54977fa269e454cb8cc6146ba23
[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     /// Honor temporary labels, this is useful for debugging semantic
102     /// differences between temporary and non-temporary labels (primarily on
103     /// Darwin).
104     bool AllowTemporaryLabels;
105
106     /// The dwarf line information from the .loc directives for the sections
107     /// with assembled machine instructions have after seeing .loc directives.
108     DenseMap<const MCSection *, MCLineSection *> MCLineSections;
109     /// We need a deterministic iteration order, so we remember the order
110     /// the elements were added.
111     std::vector<const MCSection *> MCLineSectionOrder;
112
113     void *MachOUniquingMap, *ELFUniquingMap, *COFFUniquingMap;
114
115     MCSymbol *CreateSymbol(StringRef Name);
116
117   public:
118     explicit MCContext(const MCAsmInfo &MAI, const MCRegisterInfo &MRI,
119                        const MCObjectFileInfo *MOFI);
120     ~MCContext();
121
122     const MCAsmInfo &getAsmInfo() const { return MAI; }
123
124     const MCRegisterInfo &getRegisterInfo() const { return MRI; }
125
126     const MCObjectFileInfo *getObjectFileInfo() const { return MOFI; }
127
128     void setAllowTemporaryLabels(bool Value) { AllowTemporaryLabels = Value; }
129
130     /// @name Symbol Management
131     /// @{
132
133     /// CreateTempSymbol - Create and return a new assembler temporary symbol
134     /// with a unique but unspecified name.
135     MCSymbol *CreateTempSymbol();
136
137     /// CreateDirectionalLocalSymbol - Create the definition of a directional
138     /// local symbol for numbered label (used for "1:" definitions).
139     MCSymbol *CreateDirectionalLocalSymbol(int64_t LocalLabelVal);
140
141     /// GetDirectionalLocalSymbol - Create and return a directional local
142     /// symbol for numbered label (used for "1b" or 1f" references).
143     MCSymbol *GetDirectionalLocalSymbol(int64_t LocalLabelVal, int bORf);
144
145     /// GetOrCreateSymbol - Lookup the symbol inside with the specified
146     /// @p Name.  If it exists, return it.  If not, create a forward
147     /// reference and return it.
148     ///
149     /// @param Name - The symbol name, which must be unique across all symbols.
150     MCSymbol *GetOrCreateSymbol(StringRef Name);
151     MCSymbol *GetOrCreateSymbol(const Twine &Name);
152
153     /// LookupSymbol - Get the symbol for \p Name, or null.
154     MCSymbol *LookupSymbol(StringRef Name) const;
155
156     /// getSymbols - Get a reference for the symbol table for clients that
157     /// want to, for example, iterate over all symbols. 'const' because we
158     /// still want any modifications to the table itself to use the MCContext
159     /// APIs.
160     const SymbolTable &getSymbols() const {
161       return Symbols;
162     }
163
164     /// @}
165
166     /// @name Section Management
167     /// @{
168
169     /// getMachOSection - Return the MCSection for the specified mach-o section.
170     /// This requires the operands to be valid.
171     const MCSectionMachO *getMachOSection(StringRef Segment,
172                                           StringRef Section,
173                                           unsigned TypeAndAttributes,
174                                           unsigned Reserved2,
175                                           SectionKind K);
176     const MCSectionMachO *getMachOSection(StringRef Segment,
177                                           StringRef Section,
178                                           unsigned TypeAndAttributes,
179                                           SectionKind K) {
180       return getMachOSection(Segment, Section, TypeAndAttributes, 0, K);
181     }
182
183     const MCSectionELF *getELFSection(StringRef Section, unsigned Type,
184                                       unsigned Flags, SectionKind Kind);
185
186     const MCSectionELF *getELFSection(StringRef Section, unsigned Type,
187                                       unsigned Flags, SectionKind Kind,
188                                       unsigned EntrySize, StringRef Group);
189
190     const MCSectionELF *CreateELFGroupSection();
191
192     const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
193                                     int Selection, SectionKind Kind);
194
195     const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
196                                     SectionKind Kind) {
197       return getCOFFSection (Section, Characteristics, 0, Kind);
198     }
199
200
201     /// @}
202
203     /// @name Dwarf Management
204     /// @{
205
206     /// GetDwarfFile - creates an entry in the dwarf file and directory tables.
207     unsigned GetDwarfFile(StringRef FileName, unsigned FileNumber);
208
209     bool isValidDwarfFileNumber(unsigned FileNumber);
210
211     bool hasDwarfFiles() const {
212       return !MCDwarfFiles.empty();
213     }
214
215     const std::vector<MCDwarfFile *> &getMCDwarfFiles() {
216       return MCDwarfFiles;
217     }
218     const std::vector<StringRef> &getMCDwarfDirs() {
219       return MCDwarfDirs;
220     }
221
222     const DenseMap<const MCSection *, MCLineSection *>
223     &getMCLineSections() const {
224       return MCLineSections;
225     }
226     const std::vector<const MCSection *> &getMCLineSectionOrder() const {
227       return MCLineSectionOrder;
228     }
229     void addMCLineSection(const MCSection *Sec, MCLineSection *Line) {
230       MCLineSections[Sec] = Line;
231       MCLineSectionOrder.push_back(Sec);
232     }
233
234     /// setCurrentDwarfLoc - saves the information from the currently parsed
235     /// dwarf .loc directive and sets DwarfLocSeen.  When the next instruction
236     /// is assembled an entry in the line number table with this information and
237     /// the address of the instruction will be created.
238     void setCurrentDwarfLoc(unsigned FileNum, unsigned Line, unsigned Column,
239                             unsigned Flags, unsigned Isa,
240                             unsigned Discriminator) {
241       CurrentDwarfLoc.setFileNum(FileNum);
242       CurrentDwarfLoc.setLine(Line);
243       CurrentDwarfLoc.setColumn(Column);
244       CurrentDwarfLoc.setFlags(Flags);
245       CurrentDwarfLoc.setIsa(Isa);
246       CurrentDwarfLoc.setDiscriminator(Discriminator);
247       DwarfLocSeen = true;
248     }
249     void ClearDwarfLocSeen() { DwarfLocSeen = false; }
250
251     bool getDwarfLocSeen() { return DwarfLocSeen; }
252     const MCDwarfLoc &getCurrentDwarfLoc() { return CurrentDwarfLoc; }
253
254     /// @}
255
256     char *getSecureLogFile() { return SecureLogFile; }
257     raw_ostream *getSecureLog() { return SecureLog; }
258     bool getSecureLogUsed() { return SecureLogUsed; }
259     void setSecureLog(raw_ostream *Value) {
260       SecureLog = Value;
261     }
262     void setSecureLogUsed(bool Value) {
263       SecureLogUsed = Value;
264     }
265
266     void *Allocate(unsigned Size, unsigned Align = 8) {
267       return Allocator.Allocate(Size, Align);
268     }
269     void Deallocate(void *Ptr) {
270     }
271   };
272
273 } // end namespace llvm
274
275 // operator new and delete aren't allowed inside namespaces.
276 // The throw specifications are mandated by the standard.
277 /// @brief Placement new for using the MCContext's allocator.
278 ///
279 /// This placement form of operator new uses the MCContext's allocator for
280 /// obtaining memory. It is a non-throwing new, which means that it returns
281 /// null on error. (If that is what the allocator does. The current does, so if
282 /// this ever changes, this operator will have to be changed, too.)
283 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
284 /// @code
285 /// // Default alignment (16)
286 /// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
287 /// // Specific alignment
288 /// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
289 /// @endcode
290 /// Please note that you cannot use delete on the pointer; it must be
291 /// deallocated using an explicit destructor call followed by
292 /// @c Context.Deallocate(Ptr).
293 ///
294 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
295 /// @param C The MCContext that provides the allocator.
296 /// @param Alignment The alignment of the allocated memory (if the underlying
297 ///                  allocator supports it).
298 /// @return The allocated memory. Could be NULL.
299 inline void *operator new(size_t Bytes, llvm::MCContext &C,
300                           size_t Alignment = 16) throw () {
301   return C.Allocate(Bytes, Alignment);
302 }
303 /// @brief Placement delete companion to the new above.
304 ///
305 /// This operator is just a companion to the new above. There is no way of
306 /// invoking it directly; see the new operator for more details. This operator
307 /// is called implicitly by the compiler if a placement new expression using
308 /// the MCContext throws in the object constructor.
309 inline void operator delete(void *Ptr, llvm::MCContext &C, size_t)
310               throw () {
311   C.Deallocate(Ptr);
312 }
313
314 /// This placement form of operator new[] uses the MCContext's allocator for
315 /// obtaining memory. It is a non-throwing new[], which means that it returns
316 /// null on error.
317 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
318 /// @code
319 /// // Default alignment (16)
320 /// char *data = new (Context) char[10];
321 /// // Specific alignment
322 /// char *data = new (Context, 8) char[10];
323 /// @endcode
324 /// Please note that you cannot use delete on the pointer; it must be
325 /// deallocated using an explicit destructor call followed by
326 /// @c Context.Deallocate(Ptr).
327 ///
328 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
329 /// @param C The MCContext that provides the allocator.
330 /// @param Alignment The alignment of the allocated memory (if the underlying
331 ///                  allocator supports it).
332 /// @return The allocated memory. Could be NULL.
333 inline void *operator new[](size_t Bytes, llvm::MCContext& C,
334                             size_t Alignment = 16) throw () {
335   return C.Allocate(Bytes, Alignment);
336 }
337
338 /// @brief Placement delete[] companion to the new[] above.
339 ///
340 /// This operator is just a companion to the new[] above. There is no way of
341 /// invoking it directly; see the new[] operator for more details. This operator
342 /// is called implicitly by the compiler if a placement new[] expression using
343 /// the MCContext throws in the object constructor.
344 inline void operator delete[](void *Ptr, llvm::MCContext &C) throw () {
345   C.Deallocate(Ptr);
346 }
347
348 #endif