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"
27 /// MCContext - Context object for machine code objects. This class owns all
28 /// of the sections that it creates.
31 MCContext(const MCContext&); // DO NOT IMPLEMENT
32 MCContext &operator=(const MCContext&); // DO NOT IMPLEMENT
34 /// The MCAsmInfo for this target.
37 /// Sections - Bindings of names to allocated sections.
38 StringMap<MCSection*> Sections;
40 /// Symbols - Bindings of names to symbols.
41 StringMap<MCSymbol*> Symbols;
43 /// NextUniqueID - The next ID to dole out to an unnamed assembler temporary
45 unsigned NextUniqueID;
47 /// Allocator - Allocator object used for creating machine code objects.
49 /// We use a bump pointer allocator to avoid the need to track all allocated
51 BumpPtrAllocator Allocator;
53 void *MachOUniquingMap;
55 explicit MCContext(const MCAsmInfo &MAI);
58 const MCAsmInfo &getAsmInfo() const { return MAI; }
60 /// @name Symbol Managment
63 /// CreateTempSymbol - Create and return a new assembler temporary symbol
64 /// with a unique but unspecified name.
65 MCSymbol *CreateTempSymbol();
67 /// GetOrCreateSymbol - Lookup the symbol inside with the specified
68 /// @p Name. If it exists, return it. If not, create a forward
69 /// reference and return it.
71 /// @param Name - The symbol name, which must be unique across all symbols.
72 MCSymbol *GetOrCreateSymbol(StringRef Name);
73 MCSymbol *GetOrCreateSymbol(const Twine &Name);
75 /// LookupSymbol - Get the symbol for \p Name, or null.
76 MCSymbol *LookupSymbol(StringRef Name) const;
80 /// @name Section Managment
83 /// getMachOSection - Return the MCSection for the specified mach-o section.
84 /// This requires the operands to be valid.
85 const MCSectionMachO *getMachOSection(StringRef Segment,
87 unsigned TypeAndAttributes,
90 const MCSectionMachO *getMachOSection(StringRef Segment,
92 unsigned TypeAndAttributes,
94 return getMachOSection(Segment, Section, TypeAndAttributes, 0, K);
99 void *Allocate(unsigned Size, unsigned Align = 8) {
100 return Allocator.Allocate(Size, Align);
102 void Deallocate(void *Ptr) {
106 } // end namespace llvm
108 // operator new and delete aren't allowed inside namespaces.
109 // The throw specifications are mandated by the standard.
110 /// @brief Placement new for using the MCContext's allocator.
112 /// This placement form of operator new uses the MCContext's allocator for
113 /// obtaining memory. It is a non-throwing new, which means that it returns
114 /// null on error. (If that is what the allocator does. The current does, so if
115 /// this ever changes, this operator will have to be changed, too.)
116 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
118 /// // Default alignment (16)
119 /// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
120 /// // Specific alignment
121 /// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
123 /// Please note that you cannot use delete on the pointer; it must be
124 /// deallocated using an explicit destructor call followed by
125 /// @c Context.Deallocate(Ptr).
127 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
128 /// @param C The MCContext that provides the allocator.
129 /// @param Alignment The alignment of the allocated memory (if the underlying
130 /// allocator supports it).
131 /// @return The allocated memory. Could be NULL.
132 inline void *operator new(size_t Bytes, llvm::MCContext &C,
133 size_t Alignment = 16) throw () {
134 return C.Allocate(Bytes, Alignment);
136 /// @brief Placement delete companion to the new above.
138 /// This operator is just a companion to the new above. There is no way of
139 /// invoking it directly; see the new operator for more details. This operator
140 /// is called implicitly by the compiler if a placement new expression using
141 /// the MCContext throws in the object constructor.
142 inline void operator delete(void *Ptr, llvm::MCContext &C, size_t)
147 /// This placement form of operator new[] uses the MCContext's allocator for
148 /// obtaining memory. It is a non-throwing new[], which means that it returns
150 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
152 /// // Default alignment (16)
153 /// char *data = new (Context) char[10];
154 /// // Specific alignment
155 /// char *data = new (Context, 8) char[10];
157 /// Please note that you cannot use delete on the pointer; it must be
158 /// deallocated using an explicit destructor call followed by
159 /// @c Context.Deallocate(Ptr).
161 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
162 /// @param C The MCContext that provides the allocator.
163 /// @param Alignment The alignment of the allocated memory (if the underlying
164 /// allocator supports it).
165 /// @return The allocated memory. Could be NULL.
166 inline void *operator new[](size_t Bytes, llvm::MCContext& C,
167 size_t Alignment = 16) throw () {
168 return C.Allocate(Bytes, Alignment);
171 /// @brief Placement delete[] companion to the new[] above.
173 /// This operator is just a companion to the new[] above. There is no way of
174 /// invoking it directly; see the new[] operator for more details. This operator
175 /// is called implicitly by the compiler if a placement new[] expression using
176 /// the MCContext throws in the object constructor.
177 inline void operator delete[](void *Ptr, llvm::MCContext &C) throw () {