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