1 //===- MCContext.h - Machine Code Context -----------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_MC_MCCONTEXT_H
11 #define LLVM_MC_MCCONTEXT_H
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"
29 /// MCContext - Context object for machine code objects. This class owns all
30 /// of the sections that it creates.
33 MCContext(const MCContext&); // DO NOT IMPLEMENT
34 MCContext &operator=(const MCContext&); // DO NOT IMPLEMENT
36 /// The MCAsmInfo for this target.
39 /// Sections - Bindings of names to allocated sections.
40 StringMap<MCSection*> Sections;
42 /// Symbols - Bindings of names to symbols.
43 StringMap<MCSymbol*> Symbols;
45 /// NextUniqueID - The next ID to dole out to an unnamed assembler temporary
47 unsigned NextUniqueID;
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);
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.
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.
69 /// Allocator - Allocator object used for creating machine code objects.
71 /// We use a bump pointer allocator to avoid the need to track all allocated
73 BumpPtrAllocator Allocator;
75 void *MachOUniquingMap, *ELFUniquingMap, *COFFUniquingMap;
77 explicit MCContext(const MCAsmInfo &MAI);
80 const MCAsmInfo &getAsmInfo() const { return MAI; }
82 /// @name Symbol Managment
85 /// CreateTempSymbol - Create and return a new assembler temporary symbol
86 /// with a unique but unspecified name.
87 MCSymbol *CreateTempSymbol();
89 /// CreateDirectionalLocalSymbol - Create the defintion of a directional
90 /// local symbol for numbered label (used for "1:" defintions).
91 MCSymbol *CreateDirectionalLocalSymbol(int64_t LocalLabelVal);
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);
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.
101 /// @param Name - The symbol name, which must be unique across all symbols.
102 MCSymbol *GetOrCreateSymbol(StringRef Name);
103 MCSymbol *GetOrCreateSymbol(const Twine &Name);
105 /// LookupSymbol - Get the symbol for \p Name, or null.
106 MCSymbol *LookupSymbol(StringRef Name) const;
110 /// @name Section Managment
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,
117 unsigned TypeAndAttributes,
120 const MCSectionMachO *getMachOSection(StringRef Segment,
122 unsigned TypeAndAttributes,
124 return getMachOSection(Segment, Section, TypeAndAttributes, 0, K);
127 const MCSection *getELFSection(StringRef Section, unsigned Type,
128 unsigned Flags, SectionKind Kind,
129 bool IsExplicit = false);
131 const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
132 int Selection, SectionKind Kind);
134 const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
136 return getCOFFSection (Section, Characteristics, 0, Kind);
142 char *getSecureLogFile() { return SecureLogFile; }
143 raw_ostream *getSecureLog() { return SecureLog; }
144 bool getSecureLogUsed() { return SecureLogUsed; }
145 void setSecureLog(raw_ostream *Value) {
148 void setSecureLogUsed(bool Value) {
149 SecureLogUsed = Value;
152 void *Allocate(unsigned Size, unsigned Align = 8) {
153 return Allocator.Allocate(Size, Align);
155 void Deallocate(void *Ptr) {
159 } // end namespace llvm
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.
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):
171 /// // Default alignment (16)
172 /// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
173 /// // Specific alignment
174 /// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
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).
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);
189 /// @brief Placement delete companion to the new above.
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)
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
203 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
205 /// // Default alignment (16)
206 /// char *data = new (Context) char[10];
207 /// // Specific alignment
208 /// char *data = new (Context, 8) char[10];
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).
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);
224 /// @brief Placement delete[] companion to the new[] above.
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 () {