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