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 /// CreateSymbol - Create a new symbol with the specified @param Name.
52 /// @param Name - The symbol name, which must be unique across all symbols.
53 MCSymbol *CreateSymbol(const StringRef &Name);
55 /// GetOrCreateSymbol - Lookup the symbol inside with the specified
56 /// @param Name. If it exists, return it. If not, create a forward
57 /// reference and return it.
59 /// @param Name - The symbol name, which must be unique across all symbols.
60 /// @param IsTemporary - Whether this symbol is an assembler temporary,
61 /// which should not survive into the symbol table for the translation unit.
62 MCSymbol *GetOrCreateSymbol(const StringRef &Name);
64 /// CreateTemporarySymbol - Create a new temporary symbol with the specified
67 /// @param Name - The symbol name, for debugging purposes only, temporary
68 /// symbols do not surive assembly. If non-empty the name must be unique
69 /// across all symbols.
70 MCSymbol *CreateTemporarySymbol(const StringRef &Name = "");
72 /// LookupSymbol - Get the symbol for @param Name, or null.
73 MCSymbol *LookupSymbol(const StringRef &Name) const;
75 /// ClearSymbolValue - Erase a value binding for @arg Symbol, if one exists.
76 void ClearSymbolValue(const MCSymbol *Symbol);
78 /// SetSymbolValue - Set the value binding for @arg Symbol to @arg Value.
79 void SetSymbolValue(const MCSymbol *Symbol, const MCValue &Value);
81 /// GetSymbolValue - Return the current value for @arg Symbol, or null if
83 const MCValue *GetSymbolValue(const MCSymbol *Symbol) const;
85 void *Allocate(unsigned Size, unsigned Align = 8) {
86 return Allocator.Allocate(Size, Align);
88 void Deallocate(void *Ptr) {
92 } // end namespace llvm
94 // operator new and delete aren't allowed inside namespaces.
95 // The throw specifications are mandated by the standard.
96 /// @brief Placement new for using the MCContext's allocator.
98 /// This placement form of operator new uses the MCContext's allocator for
99 /// obtaining memory. It is a non-throwing new, which means that it returns
100 /// null on error. (If that is what the allocator does. The current does, so if
101 /// this ever changes, this operator will have to be changed, too.)
102 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
104 /// // Default alignment (16)
105 /// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
106 /// // Specific alignment
107 /// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
109 /// Please note that you cannot use delete on the pointer; it must be
110 /// deallocated using an explicit destructor call followed by
111 /// @c Context.Deallocate(Ptr).
113 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
114 /// @param C The MCContext that provides the allocator.
115 /// @param Alignment The alignment of the allocated memory (if the underlying
116 /// allocator supports it).
117 /// @return The allocated memory. Could be NULL.
118 inline void *operator new(size_t Bytes, llvm::MCContext &C,
119 size_t Alignment = 16) throw () {
120 return C.Allocate(Bytes, Alignment);
122 /// @brief Placement delete companion to the new above.
124 /// This operator is just a companion to the new above. There is no way of
125 /// invoking it directly; see the new operator for more details. This operator
126 /// is called implicitly by the compiler if a placement new expression using
127 /// the MCContext throws in the object constructor.
128 inline void operator delete(void *Ptr, llvm::MCContext &C, size_t)
133 /// This placement form of operator new[] uses the MCContext's allocator for
134 /// obtaining memory. It is a non-throwing new[], which means that it returns
136 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
138 /// // Default alignment (16)
139 /// char *data = new (Context) char[10];
140 /// // Specific alignment
141 /// char *data = new (Context, 8) char[10];
143 /// Please note that you cannot use delete on the pointer; it must be
144 /// deallocated using an explicit destructor call followed by
145 /// @c Context.Deallocate(Ptr).
147 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
148 /// @param C The MCContext that provides the allocator.
149 /// @param Alignment The alignment of the allocated memory (if the underlying
150 /// allocator supports it).
151 /// @return The allocated memory. Could be NULL.
152 inline void *operator new[](size_t Bytes, llvm::MCContext& C,
153 size_t Alignment = 16) throw () {
154 return C.Allocate(Bytes, Alignment);
157 /// @brief Placement delete[] companion to the new[] above.
159 /// This operator is just a companion to the new[] above. There is no way of
160 /// invoking it directly; see the new[] operator for more details. This operator
161 /// is called implicitly by the compiler if a placement new[] expression using
162 /// the MCContext throws in the object constructor.
163 inline void operator delete[](void *Ptr, llvm::MCContext &C) throw () {