86f83ad240f8d2c12aee0ad105380e2504709a07
[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     /// Sections - Bindings of names to allocated sections.
42     StringMap<MCSection*> Sections;
43
44     /// Symbols - Bindings of names to symbols.
45     StringMap<MCSymbol*> Symbols;
46
47     /// NextUniqueID - The next ID to dole out to an unnamed assembler temporary
48     /// symbol.
49     unsigned NextUniqueID;
50
51     /// Instances of directional local labels.
52     DenseMap<unsigned, MCLabel *> Instances;
53     /// NextInstance() creates the next instance of the directional local label
54     /// for the LocalLabelVal and adds it to the map if needed.
55     unsigned NextInstance(int64_t LocalLabelVal);
56     /// GetInstance() gets the current instance of the directional local label
57     /// for the LocalLabelVal and adds it to the map if needed.
58     unsigned GetInstance(int64_t LocalLabelVal);
59     
60     /// The file name of the log file from the enviromment variable
61     /// AS_SECURE_LOG_FILE.  Which must be set before the .secure_log_unique
62     /// directive is used or it is an error.
63     char *SecureLogFile;
64     /// The stream that gets written to for the .secure_log_unique directive.
65     raw_ostream *SecureLog;
66     /// Boolean toggled when .secure_log_unique / .secure_log_reset is seen to
67     /// catch errors if .secure_log_unique appears twice without
68     /// .secure_log_reset appearing between them.
69     bool SecureLogUsed;
70
71     /// The dwarf file and directory tables from the dwarf .file directive.
72     std::vector<MCDwarfFile *> MCDwarfFiles;
73     std::vector<std::string *> MCDwarfDirs;
74
75     /// Allocator - Allocator object used for creating machine code objects.
76     ///
77     /// We use a bump pointer allocator to avoid the need to track all allocated
78     /// objects.
79     BumpPtrAllocator Allocator;
80     
81     void *MachOUniquingMap, *ELFUniquingMap, *COFFUniquingMap;
82   public:
83     explicit MCContext(const MCAsmInfo &MAI);
84     ~MCContext();
85     
86     const MCAsmInfo &getAsmInfo() const { return MAI; }
87
88     /// @name Symbol Managment
89     /// @{
90     
91     /// CreateTempSymbol - Create and return a new assembler temporary symbol
92     /// with a unique but unspecified name.
93     MCSymbol *CreateTempSymbol();
94
95     /// CreateDirectionalLocalSymbol - Create the defintion of a directional
96     /// local symbol for numbered label (used for "1:" defintions).
97     MCSymbol *CreateDirectionalLocalSymbol(int64_t LocalLabelVal);
98
99     /// GetDirectionalLocalSymbol - Create and return a directional local
100     /// symbol for numbered label (used for "1b" or 1f" references).
101     MCSymbol *GetDirectionalLocalSymbol(int64_t LocalLabelVal, int bORf);
102
103     /// GetOrCreateSymbol - Lookup the symbol inside with the specified
104     /// @p Name.  If it exists, return it.  If not, create a forward
105     /// reference and return it.
106     ///
107     /// @param Name - The symbol name, which must be unique across all symbols.
108     MCSymbol *GetOrCreateSymbol(StringRef Name);
109     MCSymbol *GetOrCreateSymbol(const Twine &Name);
110
111     /// LookupSymbol - Get the symbol for \p Name, or null.
112     MCSymbol *LookupSymbol(StringRef Name) const;
113
114     /// @}
115     
116     /// @name Section Managment
117     /// @{
118
119     /// getMachOSection - Return the MCSection for the specified mach-o section.
120     /// This requires the operands to be valid.
121     const MCSectionMachO *getMachOSection(StringRef Segment,
122                                           StringRef Section,
123                                           unsigned TypeAndAttributes,
124                                           unsigned Reserved2,
125                                           SectionKind K);
126     const MCSectionMachO *getMachOSection(StringRef Segment,
127                                           StringRef Section,
128                                           unsigned TypeAndAttributes,
129                                           SectionKind K) {
130       return getMachOSection(Segment, Section, TypeAndAttributes, 0, K);
131     }
132     
133     const MCSection *getELFSection(StringRef Section, unsigned Type,
134                                    unsigned Flags, SectionKind Kind,
135                                    bool IsExplicit = false);
136
137     const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
138                                     int Selection, SectionKind Kind);
139
140     const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
141                                     SectionKind Kind) {
142       return getCOFFSection (Section, Characteristics, 0, Kind);
143     }
144
145     
146     /// @}
147
148     /// @name Dwarf Managment
149     /// @{
150
151     /// GetDwarfFile - creates an entry in the dwarf file and directory tables.
152     unsigned GetDwarfFile(StringRef FileName, unsigned FileNumber);
153
154     const std::vector<MCDwarfFile *> &getMCDwarfFiles() {
155       return MCDwarfFiles;
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