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. This class owns all
24 /// of the sections that it creates.
27 MCContext(const MCContext&); // DO NOT IMPLEMENT
28 MCContext &operator=(const MCContext&); // DO NOT IMPLEMENT
30 /// Sections - Bindings of names to allocated sections.
31 StringMap<MCSection*> Sections;
33 /// Symbols - Bindings of names to symbols.
34 StringMap<MCSymbol*> Symbols;
36 /// SymbolValues - Bindings of symbols to values.
38 // FIXME: Is there a good reason to not just put this in the MCSymbol?
39 DenseMap<const MCSymbol*, const MCExpr*> SymbolValues;
41 /// Allocator - Allocator object used for creating machine code objects.
43 /// We use a bump pointer allocator to avoid the need to track all allocated
45 BumpPtrAllocator Allocator;
50 /// @name Symbol Managment
53 /// CreateSymbol - Create a new symbol with the specified @param Name.
55 /// @param Name - The symbol name, which must be unique across all symbols.
56 MCSymbol *CreateSymbol(const StringRef &Name);
58 /// GetOrCreateSymbol - Lookup the symbol inside with the specified
59 /// @param Name. If it exists, return it. If not, create a forward
60 /// reference and return it.
62 /// @param Name - The symbol name, which must be unique across all symbols.
63 /// @param IsTemporary - Whether this symbol is an assembler temporary,
64 /// which should not survive into the symbol table for the translation unit.
65 MCSymbol *GetOrCreateSymbol(const StringRef &Name);
67 /// CreateTemporarySymbol - Create a new temporary symbol with the specified
70 /// @param Name - The symbol name, for debugging purposes only, temporary
71 /// symbols do not surive assembly. If non-empty the name must be unique
72 /// across all symbols.
73 MCSymbol *CreateTemporarySymbol(const StringRef &Name = "");
75 /// LookupSymbol - Get the symbol for @param Name, or null.
76 MCSymbol *LookupSymbol(const StringRef &Name) const;
79 /// @name Symbol Value Table
82 /// ClearSymbolValue - Erase the variable binding for @arg Symbol, if one
84 void ClearSymbolValue(const MCSymbol *Symbol) {
85 SymbolValues.erase(Symbol);
88 /// SetSymbolValue - Set the variable binding for @arg Symbol to @arg Value.
89 void SetSymbolValue(const MCSymbol *Symbol, const MCExpr *Value) {
90 assert(Value && "Invalid variable assignment!");
91 SymbolValues.insert(std::make_pair(Symbol, Value));
94 /// GetSymbolValue - Return the current variable value for @arg Symbol, or
95 /// null if @arg Symbol is not a variable.
96 const MCExpr *GetSymbolValue(const MCSymbol *Symbol) const {
97 return SymbolValues.lookup(Symbol);
102 void *Allocate(unsigned Size, unsigned Align = 8) {
103 return Allocator.Allocate(Size, Align);
105 void Deallocate(void *Ptr) {
109 } // end namespace llvm
111 // operator new and delete aren't allowed inside namespaces.
112 // The throw specifications are mandated by the standard.
113 /// @brief Placement new for using the MCContext's allocator.
115 /// This placement form of operator new uses the MCContext's allocator for
116 /// obtaining memory. It is a non-throwing new, which means that it returns
117 /// null on error. (If that is what the allocator does. The current does, so if
118 /// this ever changes, this operator will have to be changed, too.)
119 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
121 /// // Default alignment (16)
122 /// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
123 /// // Specific alignment
124 /// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
126 /// Please note that you cannot use delete on the pointer; it must be
127 /// deallocated using an explicit destructor call followed by
128 /// @c Context.Deallocate(Ptr).
130 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
131 /// @param C The MCContext that provides the allocator.
132 /// @param Alignment The alignment of the allocated memory (if the underlying
133 /// allocator supports it).
134 /// @return The allocated memory. Could be NULL.
135 inline void *operator new(size_t Bytes, llvm::MCContext &C,
136 size_t Alignment = 16) throw () {
137 return C.Allocate(Bytes, Alignment);
139 /// @brief Placement delete companion to the new above.
141 /// This operator is just a companion to the new above. There is no way of
142 /// invoking it directly; see the new operator for more details. This operator
143 /// is called implicitly by the compiler if a placement new expression using
144 /// the MCContext throws in the object constructor.
145 inline void operator delete(void *Ptr, llvm::MCContext &C, size_t)
150 /// This placement form of operator new[] uses the MCContext's allocator for
151 /// obtaining memory. It is a non-throwing new[], which means that it returns
153 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
155 /// // Default alignment (16)
156 /// char *data = new (Context) char[10];
157 /// // Specific alignment
158 /// char *data = new (Context, 8) char[10];
160 /// Please note that you cannot use delete on the pointer; it must be
161 /// deallocated using an explicit destructor call followed by
162 /// @c Context.Deallocate(Ptr).
164 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
165 /// @param C The MCContext that provides the allocator.
166 /// @param Alignment The alignment of the allocated memory (if the underlying
167 /// allocator supports it).
168 /// @return The allocated memory. Could be NULL.
169 inline void *operator new[](size_t Bytes, llvm::MCContext& C,
170 size_t Alignment = 16) throw () {
171 return C.Allocate(Bytes, Alignment);
174 /// @brief Placement delete[] companion to the new[] above.
176 /// This operator is just a companion to the new[] above. There is no way of
177 /// invoking it directly; see the new[] operator for more details. This operator
178 /// is called implicitly by the compiler if a placement new[] expression using
179 /// the MCContext throws in the object constructor.
180 inline void operator delete[](void *Ptr, llvm::MCContext &C) throw () {