Record a symbol's size which is needed for ELF symbol tables.
[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                                    unsigned EntrySize = 0);
134
135     const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
136                                     int Selection, SectionKind Kind);
137
138     const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
139                                     SectionKind Kind) {
140       return getCOFFSection (Section, Characteristics, 0, Kind);
141     }
142
143     
144     /// @}
145
146     /// @name Dwarf Managment
147     /// @{
148
149     /// GetDwarfFile - creates an entry in the dwarf file and directory tables.
150     unsigned GetDwarfFile(StringRef FileName, unsigned FileNumber);
151
152     const std::vector<MCDwarfFile *> &getMCDwarfFiles() {
153       return MCDwarfFiles;
154     }
155     const std::vector<StringRef> &getMCDwarfDirs() {
156       return MCDwarfDirs;
157     }
158
159     /// @}
160
161     char *getSecureLogFile() { return SecureLogFile; }
162     raw_ostream *getSecureLog() { return SecureLog; }
163     bool getSecureLogUsed() { return SecureLogUsed; }
164     void setSecureLog(raw_ostream *Value) {
165       SecureLog = Value;
166     }
167     void setSecureLogUsed(bool Value) {
168       SecureLogUsed = Value;
169     }
170
171     void *Allocate(unsigned Size, unsigned Align = 8) {
172       return Allocator.Allocate(Size, Align);
173     }
174     void Deallocate(void *Ptr) {
175     }
176   };
177
178 } // end namespace llvm
179
180 // operator new and delete aren't allowed inside namespaces.
181 // The throw specifications are mandated by the standard.
182 /// @brief Placement new for using the MCContext's allocator.
183 ///
184 /// This placement form of operator new uses the MCContext's allocator for
185 /// obtaining memory. It is a non-throwing new, which means that it returns
186 /// null on error. (If that is what the allocator does. The current does, so if
187 /// this ever changes, this operator will have to be changed, too.)
188 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
189 /// @code
190 /// // Default alignment (16)
191 /// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
192 /// // Specific alignment
193 /// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
194 /// @endcode
195 /// Please note that you cannot use delete on the pointer; it must be
196 /// deallocated using an explicit destructor call followed by
197 /// @c Context.Deallocate(Ptr).
198 ///
199 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
200 /// @param C The MCContext that provides the allocator.
201 /// @param Alignment The alignment of the allocated memory (if the underlying
202 ///                  allocator supports it).
203 /// @return The allocated memory. Could be NULL.
204 inline void *operator new(size_t Bytes, llvm::MCContext &C,
205                           size_t Alignment = 16) throw () {
206   return C.Allocate(Bytes, Alignment);
207 }
208 /// @brief Placement delete companion to the new above.
209 ///
210 /// This operator is just a companion to the new above. There is no way of
211 /// invoking it directly; see the new operator for more details. This operator
212 /// is called implicitly by the compiler if a placement new expression using
213 /// the MCContext throws in the object constructor.
214 inline void operator delete(void *Ptr, llvm::MCContext &C, size_t)
215               throw () {
216   C.Deallocate(Ptr);
217 }
218
219 /// This placement form of operator new[] uses the MCContext's allocator for
220 /// obtaining memory. It is a non-throwing new[], which means that it returns
221 /// null on error.
222 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
223 /// @code
224 /// // Default alignment (16)
225 /// char *data = new (Context) char[10];
226 /// // Specific alignment
227 /// char *data = new (Context, 8) char[10];
228 /// @endcode
229 /// Please note that you cannot use delete on the pointer; it must be
230 /// deallocated using an explicit destructor call followed by
231 /// @c Context.Deallocate(Ptr).
232 ///
233 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
234 /// @param C The MCContext that provides the allocator.
235 /// @param Alignment The alignment of the allocated memory (if the underlying
236 ///                  allocator supports it).
237 /// @return The allocated memory. Could be NULL.
238 inline void *operator new[](size_t Bytes, llvm::MCContext& C,
239                             size_t Alignment = 16) throw () {
240   return C.Allocate(Bytes, Alignment);
241 }
242
243 /// @brief Placement delete[] companion to the new[] above.
244 ///
245 /// This operator is just a companion to the new[] above. There is no way of
246 /// invoking it directly; see the new[] operator for more details. This operator
247 /// is called implicitly by the compiler if a placement new[] expression using
248 /// the MCContext throws in the object constructor.
249 inline void operator delete[](void *Ptr, llvm::MCContext &C) throw () {
250   C.Deallocate(Ptr);
251 }
252
253 #endif