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"
28 /// MCContext - Context object for machine code objects. This class owns all
29 /// of the sections that it creates.
32 MCContext(const MCContext&); // DO NOT IMPLEMENT
33 MCContext &operator=(const MCContext&); // DO NOT IMPLEMENT
35 /// The MCAsmInfo for this target.
38 /// Sections - Bindings of names to allocated sections.
39 StringMap<MCSection*> Sections;
41 /// Symbols - Bindings of names to symbols.
42 StringMap<MCSymbol*> Symbols;
44 /// NextUniqueID - The next ID to dole out to an unnamed assembler temporary
46 unsigned NextUniqueID;
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);
57 /// Allocator - Allocator object used for creating machine code objects.
59 /// We use a bump pointer allocator to avoid the need to track all allocated
61 BumpPtrAllocator Allocator;
63 void *MachOUniquingMap, *ELFUniquingMap, *COFFUniquingMap;
65 explicit MCContext(const MCAsmInfo &MAI);
68 const MCAsmInfo &getAsmInfo() const { return MAI; }
70 /// @name Symbol Managment
73 /// CreateTempSymbol - Create and return a new assembler temporary symbol
74 /// with a unique but unspecified name.
75 MCSymbol *CreateTempSymbol();
77 /// CreateDirectionalLocalSymbol - Create the defintion of a directional
78 /// local symbol for numbered label (used for "1:" defintions).
79 MCSymbol *CreateDirectionalLocalSymbol(int64_t LocalLabelVal);
81 /// GetDirectionalLocalSymbol - Create and return a directional local
82 /// symbol for numbered label (used for "1b" or 1f" references).
83 MCSymbol *GetDirectionalLocalSymbol(int64_t LocalLabelVal, int bORf);
85 /// GetOrCreateSymbol - Lookup the symbol inside with the specified
86 /// @p Name. If it exists, return it. If not, create a forward
87 /// reference and return it.
89 /// @param Name - The symbol name, which must be unique across all symbols.
90 MCSymbol *GetOrCreateSymbol(StringRef Name);
91 MCSymbol *GetOrCreateSymbol(const Twine &Name);
93 /// LookupSymbol - Get the symbol for \p Name, or null.
94 MCSymbol *LookupSymbol(StringRef Name) const;
98 /// @name Section Managment
101 /// getMachOSection - Return the MCSection for the specified mach-o section.
102 /// This requires the operands to be valid.
103 const MCSectionMachO *getMachOSection(StringRef Segment,
105 unsigned TypeAndAttributes,
108 const MCSectionMachO *getMachOSection(StringRef Segment,
110 unsigned TypeAndAttributes,
112 return getMachOSection(Segment, Section, TypeAndAttributes, 0, K);
115 const MCSection *getELFSection(StringRef Section, unsigned Type,
116 unsigned Flags, SectionKind Kind,
117 bool IsExplicit = false);
119 const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
120 int Selection, SectionKind Kind);
122 const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
124 return getCOFFSection (Section, Characteristics, 0, Kind);
130 void *Allocate(unsigned Size, unsigned Align = 8) {
131 return Allocator.Allocate(Size, Align);
133 void Deallocate(void *Ptr) {
137 } // end namespace llvm
139 // operator new and delete aren't allowed inside namespaces.
140 // The throw specifications are mandated by the standard.
141 /// @brief Placement new for using the MCContext's allocator.
143 /// This placement form of operator new uses the MCContext's allocator for
144 /// obtaining memory. It is a non-throwing new, which means that it returns
145 /// null on error. (If that is what the allocator does. The current does, so if
146 /// this ever changes, this operator will have to be changed, too.)
147 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
149 /// // Default alignment (16)
150 /// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
151 /// // Specific alignment
152 /// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
154 /// Please note that you cannot use delete on the pointer; it must be
155 /// deallocated using an explicit destructor call followed by
156 /// @c Context.Deallocate(Ptr).
158 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
159 /// @param C The MCContext that provides the allocator.
160 /// @param Alignment The alignment of the allocated memory (if the underlying
161 /// allocator supports it).
162 /// @return The allocated memory. Could be NULL.
163 inline void *operator new(size_t Bytes, llvm::MCContext &C,
164 size_t Alignment = 16) throw () {
165 return C.Allocate(Bytes, Alignment);
167 /// @brief Placement delete companion to the new above.
169 /// This operator is just a companion to the new above. There is no way of
170 /// invoking it directly; see the new operator for more details. This operator
171 /// is called implicitly by the compiler if a placement new expression using
172 /// the MCContext throws in the object constructor.
173 inline void operator delete(void *Ptr, llvm::MCContext &C, size_t)
178 /// This placement form of operator new[] uses the MCContext's allocator for
179 /// obtaining memory. It is a non-throwing new[], which means that it returns
181 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
183 /// // Default alignment (16)
184 /// char *data = new (Context) char[10];
185 /// // Specific alignment
186 /// char *data = new (Context, 8) char[10];
188 /// Please note that you cannot use delete on the pointer; it must be
189 /// deallocated using an explicit destructor call followed by
190 /// @c Context.Deallocate(Ptr).
192 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
193 /// @param C The MCContext that provides the allocator.
194 /// @param Alignment The alignment of the allocated memory (if the underlying
195 /// allocator supports it).
196 /// @return The allocated memory. Could be NULL.
197 inline void *operator new[](size_t Bytes, llvm::MCContext& C,
198 size_t Alignment = 16) throw () {
199 return C.Allocate(Bytes, Alignment);
202 /// @brief Placement delete[] companion to the new[] above.
204 /// This operator is just a companion to the new[] above. There is no way of
205 /// invoking it directly; see the new[] operator for more details. This operator
206 /// is called implicitly by the compiler if a placement new[] expression using
207 /// the MCContext throws in the object constructor.
208 inline void operator delete[](void *Ptr, llvm::MCContext &C) throw () {