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/ADT/DenseMap.h"
14 #include "llvm/ADT/StringMap.h"
15 #include "llvm/Support/Allocator.h"
25 /// MCContext - Context object for machine code objects. This class owns all
26 /// of the sections that it creates.
29 MCContext(const MCContext&); // DO NOT IMPLEMENT
30 MCContext &operator=(const MCContext&); // DO NOT IMPLEMENT
32 /// The MCAsmInfo for this target.
35 /// Sections - Bindings of names to allocated sections.
36 StringMap<MCSection*> Sections;
38 /// Symbols - Bindings of names to symbols.
39 StringMap<MCSymbol*> Symbols;
41 /// NextUniqueID - The next ID to dole out to an unnamed assembler temporary
43 unsigned NextUniqueID;
45 /// Allocator - Allocator object used for creating machine code objects.
47 /// We use a bump pointer allocator to avoid the need to track all allocated
49 BumpPtrAllocator Allocator;
51 explicit MCContext(const MCAsmInfo &MAI);
54 const MCAsmInfo &getAsmInfo() const { return MAI; }
56 /// @name Symbol Managment
59 /// CreateTempSymbol - Create and return a new assembler temporary symbol
60 /// with a unique but unspecified name.
61 MCSymbol *CreateTempSymbol();
63 /// GetOrCreateSymbol - Lookup the symbol inside with the specified
64 /// @p Name. If it exists, return it. If not, create a forward
65 /// reference and return it.
67 /// @param Name - The symbol name, which must be unique across all symbols.
68 MCSymbol *GetOrCreateSymbol(StringRef Name, bool isTemporary = false);
69 MCSymbol *GetOrCreateSymbol(const Twine &Name, bool isTemporary = false);
71 /// GetOrCreateTemporarySymbol - Create a new assembler temporary symbol
72 /// with the specified @p Name if it doesn't exist or return the existing
75 /// @param Name - The symbol name, for debugging purposes only, temporary
76 /// symbols do not surive assembly.
77 MCSymbol *GetOrCreateTemporarySymbol(StringRef Name) {
78 return GetOrCreateSymbol(Name, true);
80 MCSymbol *GetOrCreateTemporarySymbol(const Twine &Name);
82 /// LookupSymbol - Get the symbol for \p Name, or null.
83 MCSymbol *LookupSymbol(StringRef Name) const;
87 void *Allocate(unsigned Size, unsigned Align = 8) {
88 return Allocator.Allocate(Size, Align);
90 void Deallocate(void *Ptr) {
94 } // end namespace llvm
96 // operator new and delete aren't allowed inside namespaces.
97 // The throw specifications are mandated by the standard.
98 /// @brief Placement new for using the MCContext's allocator.
100 /// This placement form of operator new uses the MCContext's allocator for
101 /// obtaining memory. It is a non-throwing new, which means that it returns
102 /// null on error. (If that is what the allocator does. The current does, so if
103 /// this ever changes, this operator will have to be changed, too.)
104 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
106 /// // Default alignment (16)
107 /// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
108 /// // Specific alignment
109 /// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
111 /// Please note that you cannot use delete on the pointer; it must be
112 /// deallocated using an explicit destructor call followed by
113 /// @c Context.Deallocate(Ptr).
115 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
116 /// @param C The MCContext that provides the allocator.
117 /// @param Alignment The alignment of the allocated memory (if the underlying
118 /// allocator supports it).
119 /// @return The allocated memory. Could be NULL.
120 inline void *operator new(size_t Bytes, llvm::MCContext &C,
121 size_t Alignment = 16) throw () {
122 return C.Allocate(Bytes, Alignment);
124 /// @brief Placement delete companion to the new above.
126 /// This operator is just a companion to the new above. There is no way of
127 /// invoking it directly; see the new operator for more details. This operator
128 /// is called implicitly by the compiler if a placement new expression using
129 /// the MCContext throws in the object constructor.
130 inline void operator delete(void *Ptr, llvm::MCContext &C, size_t)
135 /// This placement form of operator new[] uses the MCContext's allocator for
136 /// obtaining memory. It is a non-throwing new[], which means that it returns
138 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
140 /// // Default alignment (16)
141 /// char *data = new (Context) char[10];
142 /// // Specific alignment
143 /// char *data = new (Context, 8) char[10];
145 /// Please note that you cannot use delete on the pointer; it must be
146 /// deallocated using an explicit destructor call followed by
147 /// @c Context.Deallocate(Ptr).
149 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
150 /// @param C The MCContext that provides the allocator.
151 /// @param Alignment The alignment of the allocated memory (if the underlying
152 /// allocator supports it).
153 /// @return The allocated memory. Could be NULL.
154 inline void *operator new[](size_t Bytes, llvm::MCContext& C,
155 size_t Alignment = 16) throw () {
156 return C.Allocate(Bytes, Alignment);
159 /// @brief Placement delete[] companion to the new[] above.
161 /// This operator is just a companion to the new[] above. There is no way of
162 /// invoking it directly; see the new[] operator for more details. This operator
163 /// is called implicitly by the compiler if a placement new[] expression using
164 /// the MCContext throws in the object constructor.
165 inline void operator delete[](void *Ptr, llvm::MCContext &C) throw () {