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"
23 /// MCContext - Context object for machine code objects.
25 MCContext(const MCContext&); // DO NOT IMPLEMENT
26 MCContext &operator=(const MCContext&); // DO NOT IMPLEMENT
28 /// Sections - Bindings of names to allocated sections.
29 StringMap<MCSection*> Sections;
31 /// Symbols - Bindings of names to symbols.
32 StringMap<MCSymbol*> Symbols;
34 /// SymbolValues - Bindings of symbols to values.
36 // FIXME: Is there a good reason to not just put this in the MCSymbol?
37 DenseMap<MCSymbol*, MCValue> SymbolValues;
39 /// Allocator - Allocator object used for creating machine code objects.
41 /// We use a bump pointer allocator to avoid the need to track all allocated
43 BumpPtrAllocator Allocator;
45 friend class MCSection;
50 /// GetSection - Look up a section with the given @param Name, returning
51 /// null if it doesn't exist.
52 MCSection *GetSection(const StringRef &Name) const;
54 /// CreateSymbol - Create a new symbol with the specified @param Name.
56 /// @param Name - The symbol name, which must be unique across all symbols.
57 MCSymbol *CreateSymbol(const StringRef &Name);
59 /// GetOrCreateSymbol - Lookup the symbol inside with the specified
60 /// @param Name. If it exists, return it. If not, create a forward
61 /// reference and return it.
63 /// @param Name - The symbol name, which must be unique across all symbols.
64 MCSymbol *GetOrCreateSymbol(const StringRef &Name);
66 /// CreateTemporarySymbol - Create a new temporary symbol with the specified
69 /// @param Name - The symbol name, for debugging purposes only, temporary
70 /// symbols do not surive assembly. If non-empty the name must be unique
71 /// across all symbols.
72 MCSymbol *CreateTemporarySymbol(const StringRef &Name = "");
74 /// LookupSymbol - Get the symbol for @param Name, or null.
75 MCSymbol *LookupSymbol(const StringRef &Name) const;
77 /// ClearSymbolValue - Erase a value binding for @param Symbol, if one
79 void ClearSymbolValue(MCSymbol *Symbol);
81 /// SetSymbolValue - Set the value binding for @param Symbol to @param
83 void SetSymbolValue(MCSymbol *Symbol, const MCValue &Value);
85 /// GetSymbolValue - Return the current value for @param Symbol, or null if
87 const MCValue *GetSymbolValue(MCSymbol *Symbol) const;
89 void *Allocate(unsigned Size, unsigned Align = 8) {
90 return Allocator.Allocate(Size, Align);
92 void Deallocate(void *Ptr) {
96 } // end namespace llvm
98 // operator new and delete aren't allowed inside namespaces.
99 // The throw specifications are mandated by the standard.
100 /// @brief Placement new for using the MCContext's allocator.
102 /// This placement form of operator new uses the MCContext's allocator for
103 /// obtaining memory. It is a non-throwing new, which means that it returns
104 /// null on error. (If that is what the allocator does. The current does, so if
105 /// this ever changes, this operator will have to be changed, too.)
106 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
108 /// // Default alignment (16)
109 /// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
110 /// // Specific alignment
111 /// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
113 /// Please note that you cannot use delete on the pointer; it must be
114 /// deallocated using an explicit destructor call followed by
115 /// @c Context.Deallocate(Ptr).
117 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
118 /// @param C The MCContext that provides the allocator.
119 /// @param Alignment The alignment of the allocated memory (if the underlying
120 /// allocator supports it).
121 /// @return The allocated memory. Could be NULL.
122 inline void *operator new(size_t Bytes, llvm::MCContext &C,
123 size_t Alignment = 16) throw () {
124 return C.Allocate(Bytes, Alignment);
126 /// @brief Placement delete companion to the new above.
128 /// This operator is just a companion to the new above. There is no way of
129 /// invoking it directly; see the new operator for more details. This operator
130 /// is called implicitly by the compiler if a placement new expression using
131 /// the MCContext throws in the object constructor.
132 inline void operator delete(void *Ptr, llvm::MCContext &C, size_t)
137 /// This placement form of operator new[] uses the MCContext's allocator for
138 /// obtaining memory. It is a non-throwing new[], which means that it returns
140 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
142 /// // Default alignment (16)
143 /// char *data = new (Context) char[10];
144 /// // Specific alignment
145 /// char *data = new (Context, 8) char[10];
147 /// Please note that you cannot use delete on the pointer; it must be
148 /// deallocated using an explicit destructor call followed by
149 /// @c Context.Deallocate(Ptr).
151 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
152 /// @param C The MCContext that provides the allocator.
153 /// @param Alignment The alignment of the allocated memory (if the underlying
154 /// allocator supports it).
155 /// @return The allocated memory. Could be NULL.
156 inline void *operator new[](size_t Bytes, llvm::MCContext& C,
157 size_t Alignment = 16) throw () {
158 return C.Allocate(Bytes, Alignment);
161 /// @brief Placement delete[] companion to the new[] above.
163 /// This operator is just a companion to the new[] above. There is no way of
164 /// invoking it directly; see the new[] operator for more details. This operator
165 /// is called implicitly by the compiler if a placement new[] expression using
166 /// the MCContext throws in the object constructor.
167 inline void operator delete[](void *Ptr, llvm::MCContext &C) throw () {