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"
24 /// MCContext - Context object for machine code objects. This class owns all
25 /// of the sections that it creates.
28 MCContext(const MCContext&); // DO NOT IMPLEMENT
29 MCContext &operator=(const MCContext&); // DO NOT IMPLEMENT
31 /// Sections - Bindings of names to allocated sections.
32 StringMap<MCSection*> Sections;
34 /// Symbols - Bindings of names to symbols.
35 StringMap<MCSymbol*> Symbols;
37 /// Allocator - Allocator object used for creating machine code objects.
39 /// We use a bump pointer allocator to avoid the need to track all allocated
41 BumpPtrAllocator Allocator;
46 /// @name Symbol Managment
49 /// GetOrCreateSymbol - Lookup the symbol inside with the specified
50 /// @p Name. If it exists, return it. If not, create a forward
51 /// reference and return it.
53 /// @param Name - The symbol name, which must be unique across all symbols.
54 MCSymbol *GetOrCreateSymbol(StringRef Name);
55 MCSymbol *GetOrCreateSymbol(const Twine &Name);
57 /// GetOrCreateTemporarySymbol - Create a new assembler temporary symbol
58 /// with the specified @p Name if it doesn't exist or return the existing
61 /// @param Name - The symbol name, for debugging purposes only, temporary
62 /// symbols do not surive assembly. If non-empty the name must be unique
63 /// across all symbols.
64 MCSymbol *GetOrCreateTemporarySymbol(StringRef Name = "");
65 MCSymbol *GetOrCreateTemporarySymbol(const Twine &Name);
67 /// LookupSymbol - Get the symbol for \p Name, or null.
68 MCSymbol *LookupSymbol(StringRef Name) const;
72 void *Allocate(unsigned Size, unsigned Align = 8) {
73 return Allocator.Allocate(Size, Align);
75 void Deallocate(void *Ptr) {
79 } // end namespace llvm
81 // operator new and delete aren't allowed inside namespaces.
82 // The throw specifications are mandated by the standard.
83 /// @brief Placement new for using the MCContext's allocator.
85 /// This placement form of operator new uses the MCContext's allocator for
86 /// obtaining memory. It is a non-throwing new, which means that it returns
87 /// null on error. (If that is what the allocator does. The current does, so if
88 /// this ever changes, this operator will have to be changed, too.)
89 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
91 /// // Default alignment (16)
92 /// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
93 /// // Specific alignment
94 /// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
96 /// Please note that you cannot use delete on the pointer; it must be
97 /// deallocated using an explicit destructor call followed by
98 /// @c Context.Deallocate(Ptr).
100 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
101 /// @param C The MCContext that provides the allocator.
102 /// @param Alignment The alignment of the allocated memory (if the underlying
103 /// allocator supports it).
104 /// @return The allocated memory. Could be NULL.
105 inline void *operator new(size_t Bytes, llvm::MCContext &C,
106 size_t Alignment = 16) throw () {
107 return C.Allocate(Bytes, Alignment);
109 /// @brief Placement delete companion to the new above.
111 /// This operator is just a companion to the new above. There is no way of
112 /// invoking it directly; see the new operator for more details. This operator
113 /// is called implicitly by the compiler if a placement new expression using
114 /// the MCContext throws in the object constructor.
115 inline void operator delete(void *Ptr, llvm::MCContext &C, size_t)
120 /// This placement form of operator new[] uses the MCContext's allocator for
121 /// obtaining memory. It is a non-throwing new[], which means that it returns
123 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
125 /// // Default alignment (16)
126 /// char *data = new (Context) char[10];
127 /// // Specific alignment
128 /// char *data = new (Context, 8) char[10];
130 /// Please note that you cannot use delete on the pointer; it must be
131 /// deallocated using an explicit destructor call followed by
132 /// @c Context.Deallocate(Ptr).
134 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
135 /// @param C The MCContext that provides the allocator.
136 /// @param Alignment The alignment of the allocated memory (if the underlying
137 /// allocator supports it).
138 /// @return The allocated memory. Could be NULL.
139 inline void *operator new[](size_t Bytes, llvm::MCContext& C,
140 size_t Alignment = 16) throw () {
141 return C.Allocate(Bytes, Alignment);
144 /// @brief Placement delete[] companion to the new[] above.
146 /// This operator is just a companion to the new[] above. There is no way of
147 /// invoking it directly; see the new[] operator for more details. This operator
148 /// is called implicitly by the compiler if a placement new[] expression using
149 /// the MCContext throws in the object constructor.
150 inline void operator delete[](void *Ptr, llvm::MCContext &C) throw () {