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*, MCValue> 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 a value binding for @arg Symbol, if one exists.
83 void ClearSymbolValue(const MCSymbol *Symbol);
85 /// SetSymbolValue - Set the value binding for @arg Symbol to @arg Value.
86 void SetSymbolValue(const MCSymbol *Symbol, const MCValue &Value);
88 /// GetSymbolValue - Return the current value for @arg Symbol, or null if
90 const MCValue *GetSymbolValue(const MCSymbol *Symbol) const;
94 void *Allocate(unsigned Size, unsigned Align = 8) {
95 return Allocator.Allocate(Size, Align);
97 void Deallocate(void *Ptr) {
101 } // end namespace llvm
103 // operator new and delete aren't allowed inside namespaces.
104 // The throw specifications are mandated by the standard.
105 /// @brief Placement new for using the MCContext's allocator.
107 /// This placement form of operator new uses the MCContext's allocator for
108 /// obtaining memory. It is a non-throwing new, which means that it returns
109 /// null on error. (If that is what the allocator does. The current does, so if
110 /// this ever changes, this operator will have to be changed, too.)
111 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
113 /// // Default alignment (16)
114 /// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
115 /// // Specific alignment
116 /// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
118 /// Please note that you cannot use delete on the pointer; it must be
119 /// deallocated using an explicit destructor call followed by
120 /// @c Context.Deallocate(Ptr).
122 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
123 /// @param C The MCContext that provides the allocator.
124 /// @param Alignment The alignment of the allocated memory (if the underlying
125 /// allocator supports it).
126 /// @return The allocated memory. Could be NULL.
127 inline void *operator new(size_t Bytes, llvm::MCContext &C,
128 size_t Alignment = 16) throw () {
129 return C.Allocate(Bytes, Alignment);
131 /// @brief Placement delete companion to the new above.
133 /// This operator is just a companion to the new above. There is no way of
134 /// invoking it directly; see the new operator for more details. This operator
135 /// is called implicitly by the compiler if a placement new expression using
136 /// the MCContext throws in the object constructor.
137 inline void operator delete(void *Ptr, llvm::MCContext &C, size_t)
142 /// This placement form of operator new[] uses the MCContext's allocator for
143 /// obtaining memory. It is a non-throwing new[], which means that it returns
145 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
147 /// // Default alignment (16)
148 /// char *data = new (Context) char[10];
149 /// // Specific alignment
150 /// char *data = new (Context, 8) char[10];
152 /// Please note that you cannot use delete on the pointer; it must be
153 /// deallocated using an explicit destructor call followed by
154 /// @c Context.Deallocate(Ptr).
156 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
157 /// @param C The MCContext that provides the allocator.
158 /// @param Alignment The alignment of the allocated memory (if the underlying
159 /// allocator supports it).
160 /// @return The allocated memory. Could be NULL.
161 inline void *operator new[](size_t Bytes, llvm::MCContext& C,
162 size_t Alignment = 16) throw () {
163 return C.Allocate(Bytes, Alignment);
166 /// @brief Placement delete[] companion to the new[] above.
168 /// This operator is just a companion to the new[] above. There is no way of
169 /// invoking it directly; see the new[] operator for more details. This operator
170 /// is called implicitly by the compiler if a placement new[] expression using
171 /// the MCContext throws in the object constructor.
172 inline void operator delete[](void *Ptr, llvm::MCContext &C) throw () {