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