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 /// CreateSymbol - Create a new symbol with the specified @param Name.
51 /// @param Name - The symbol name, which must be unique across all symbols.
52 MCSymbol *CreateSymbol(StringRef Name);
54 /// GetOrCreateSymbol - Lookup the symbol inside with the specified
55 /// @param Name. If it exists, return it. If not, create a forward
56 /// reference and return it.
58 /// @param Name - The symbol name, which must be unique across all symbols.
59 /// @param IsTemporary - Whether this symbol is an assembler temporary,
60 /// which should not survive into the symbol table for the translation unit.
61 MCSymbol *GetOrCreateSymbol(StringRef Name);
62 MCSymbol *GetOrCreateSymbol(const Twine &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(StringRef Name = "");
72 /// LookupSymbol - Get the symbol for @param Name, or null.
73 MCSymbol *LookupSymbol(StringRef Name) const;
77 void *Allocate(unsigned Size, unsigned Align = 8) {
78 return Allocator.Allocate(Size, Align);
80 void Deallocate(void *Ptr) {
84 } // end namespace llvm
86 // operator new and delete aren't allowed inside namespaces.
87 // The throw specifications are mandated by the standard.
88 /// @brief Placement new for using the MCContext's allocator.
90 /// This placement form of operator new uses the MCContext's allocator for
91 /// obtaining memory. It is a non-throwing new, which means that it returns
92 /// null on error. (If that is what the allocator does. The current does, so if
93 /// this ever changes, this operator will have to be changed, too.)
94 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
96 /// // Default alignment (16)
97 /// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
98 /// // Specific alignment
99 /// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
101 /// Please note that you cannot use delete on the pointer; it must be
102 /// deallocated using an explicit destructor call followed by
103 /// @c Context.Deallocate(Ptr).
105 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
106 /// @param C The MCContext that provides the allocator.
107 /// @param Alignment The alignment of the allocated memory (if the underlying
108 /// allocator supports it).
109 /// @return The allocated memory. Could be NULL.
110 inline void *operator new(size_t Bytes, llvm::MCContext &C,
111 size_t Alignment = 16) throw () {
112 return C.Allocate(Bytes, Alignment);
114 /// @brief Placement delete companion to the new above.
116 /// This operator is just a companion to the new above. There is no way of
117 /// invoking it directly; see the new operator for more details. This operator
118 /// is called implicitly by the compiler if a placement new expression using
119 /// the MCContext throws in the object constructor.
120 inline void operator delete(void *Ptr, llvm::MCContext &C, size_t)
125 /// This placement form of operator new[] uses the MCContext's allocator for
126 /// obtaining memory. It is a non-throwing new[], which means that it returns
128 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
130 /// // Default alignment (16)
131 /// char *data = new (Context) char[10];
132 /// // Specific alignment
133 /// char *data = new (Context, 8) char[10];
135 /// Please note that you cannot use delete on the pointer; it must be
136 /// deallocated using an explicit destructor call followed by
137 /// @c Context.Deallocate(Ptr).
139 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
140 /// @param C The MCContext that provides the allocator.
141 /// @param Alignment The alignment of the allocated memory (if the underlying
142 /// allocator supports it).
143 /// @return The allocated memory. Could be NULL.
144 inline void *operator new[](size_t Bytes, llvm::MCContext& C,
145 size_t Alignment = 16) throw () {
146 return C.Allocate(Bytes, Alignment);
149 /// @brief Placement delete[] companion to the new[] above.
151 /// This operator is just a companion to the new[] above. There is no way of
152 /// invoking it directly; see the new[] operator for more details. This operator
153 /// is called implicitly by the compiler if a placement new[] expression using
154 /// the MCContext throws in the object constructor.
155 inline void operator delete[](void *Ptr, llvm::MCContext &C) throw () {