MCContext.h revision 2928c83b010f7cfdb0f819199d806f6942a7d995
1//===- MCContext.h - Machine Code Context -----------------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9 10#ifndef LLVM_MC_MCCONTEXT_H 11#define LLVM_MC_MCCONTEXT_H 12 13#include "llvm/ADT/DenseMap.h" 14#include "llvm/ADT/StringMap.h" 15#include "llvm/Support/Allocator.h" 16 17namespace llvm { 18 class MCExpr; 19 class MCSection; 20 class MCSymbol; 21 class StringRef; 22 class Twine; 23 24 /// MCContext - Context object for machine code objects. This class owns all 25 /// of the sections that it creates. 26 /// 27 class MCContext { 28 MCContext(const MCContext&); // DO NOT IMPLEMENT 29 MCContext &operator=(const MCContext&); // DO NOT IMPLEMENT 30 31 /// Sections - Bindings of names to allocated sections. 32 StringMap<MCSection*> Sections; 33 34 /// Symbols - Bindings of names to symbols. 35 StringMap<MCSymbol*> Symbols; 36 37 /// Allocator - Allocator object used for creating machine code objects. 38 /// 39 /// We use a bump pointer allocator to avoid the need to track all allocated 40 /// objects. 41 BumpPtrAllocator Allocator; 42 public: 43 MCContext(); 44 ~MCContext(); 45 46 /// @name Symbol Managment 47 /// @{ 48 49 /// CreateSymbol - Create a new symbol with the specified @param Name. 50 /// 51 /// @param Name - The symbol name, which must be unique across all symbols. 52 MCSymbol *CreateSymbol(StringRef Name); 53 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. 57 /// 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); 63 64 /// CreateTemporarySymbol - Create a new temporary symbol with the specified 65 /// @param Name. 66 /// 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 = ""); 71 72 /// LookupSymbol - Get the symbol for @param Name, or null. 73 MCSymbol *LookupSymbol(StringRef Name) const; 74 75 /// @} 76 77 void *Allocate(unsigned Size, unsigned Align = 8) { 78 return Allocator.Allocate(Size, Align); 79 } 80 void Deallocate(void *Ptr) { 81 } 82 }; 83 84} // end namespace llvm 85 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. 89/// 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): 95/// @code 96/// // Default alignment (16) 97/// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments); 98/// // Specific alignment 99/// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments); 100/// @endcode 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). 104/// 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. 110inline void *operator new(size_t Bytes, llvm::MCContext &C, 111 size_t Alignment = 16) throw () { 112 return C.Allocate(Bytes, Alignment); 113} 114/// @brief Placement delete companion to the new above. 115/// 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. 120inline void operator delete(void *Ptr, llvm::MCContext &C, size_t) 121 throw () { 122 C.Deallocate(Ptr); 123} 124 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 127/// null on error. 128/// Usage looks like this (assuming there's an MCContext 'Context' in scope): 129/// @code 130/// // Default alignment (16) 131/// char *data = new (Context) char[10]; 132/// // Specific alignment 133/// char *data = new (Context, 8) char[10]; 134/// @endcode 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). 138/// 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. 144inline void *operator new[](size_t Bytes, llvm::MCContext& C, 145 size_t Alignment = 16) throw () { 146 return C.Allocate(Bytes, Alignment); 147} 148 149/// @brief Placement delete[] companion to the new[] above. 150/// 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. 155inline void operator delete[](void *Ptr, llvm::MCContext &C) throw () { 156 C.Deallocate(Ptr); 157} 158 159#endif 160