LLVM API Documentation
00001 //===- MCContext.h - Machine Code Context -----------------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 00010 #ifndef LLVM_MC_MCCONTEXT_H 00011 #define LLVM_MC_MCCONTEXT_H 00012 00013 #include "llvm/ADT/DenseMap.h" 00014 #include "llvm/ADT/StringMap.h" 00015 #include "llvm/Support/Allocator.h" 00016 00017 namespace llvm { 00018 class MCValue; 00019 class MCSection; 00020 class MCSymbol; 00021 class StringRef; 00022 00023 /// MCContext - Context object for machine code objects. This class owns all 00024 /// of the sections that it creates. 00025 /// 00026 class MCContext { 00027 MCContext(const MCContext&); // DO NOT IMPLEMENT 00028 MCContext &operator=(const MCContext&); // DO NOT IMPLEMENT 00029 00030 /// Sections - Bindings of names to allocated sections. 00031 StringMap<MCSection*> Sections; 00032 00033 /// Symbols - Bindings of names to symbols. 00034 StringMap<MCSymbol*> Symbols; 00035 00036 /// SymbolValues - Bindings of symbols to values. 00037 // 00038 // FIXME: Is there a good reason to not just put this in the MCSymbol? 00039 DenseMap<MCSymbol*, MCValue> SymbolValues; 00040 00041 /// Allocator - Allocator object used for creating machine code objects. 00042 /// 00043 /// We use a bump pointer allocator to avoid the need to track all allocated 00044 /// objects. 00045 BumpPtrAllocator Allocator; 00046 public: 00047 MCContext(); 00048 ~MCContext(); 00049 00050 /// CreateSymbol - Create a new symbol with the specified @param Name. 00051 /// 00052 /// @param Name - The symbol name, which must be unique across all symbols. 00053 MCSymbol *CreateSymbol(const StringRef &Name); 00054 00055 /// GetOrCreateSymbol - Lookup the symbol inside with the specified 00056 /// @param Name. If it exists, return it. If not, create a forward 00057 /// reference and return it. 00058 /// 00059 /// @param Name - The symbol name, which must be unique across all symbols. 00060 MCSymbol *GetOrCreateSymbol(const StringRef &Name); 00061 00062 /// CreateTemporarySymbol - Create a new temporary symbol with the specified 00063 /// @param Name. 00064 /// 00065 /// @param Name - The symbol name, for debugging purposes only, temporary 00066 /// symbols do not surive assembly. If non-empty the name must be unique 00067 /// across all symbols. 00068 MCSymbol *CreateTemporarySymbol(const StringRef &Name = ""); 00069 00070 /// LookupSymbol - Get the symbol for @param Name, or null. 00071 MCSymbol *LookupSymbol(const StringRef &Name) const; 00072 00073 /// ClearSymbolValue - Erase a value binding for @param Symbol, if one 00074 /// exists. 00075 void ClearSymbolValue(MCSymbol *Symbol); 00076 00077 /// SetSymbolValue - Set the value binding for @param Symbol to @param 00078 /// Value. 00079 void SetSymbolValue(MCSymbol *Symbol, const MCValue &Value); 00080 00081 /// GetSymbolValue - Return the current value for @param Symbol, or null if 00082 /// none exists. 00083 const MCValue *GetSymbolValue(MCSymbol *Symbol) const; 00084 00085 void *Allocate(unsigned Size, unsigned Align = 8) { 00086 return Allocator.Allocate(Size, Align); 00087 } 00088 void Deallocate(void *Ptr) { 00089 } 00090 }; 00091 00092 } // end namespace llvm 00093 00094 // operator new and delete aren't allowed inside namespaces. 00095 // The throw specifications are mandated by the standard. 00096 /// @brief Placement new for using the MCContext's allocator. 00097 /// 00098 /// This placement form of operator new uses the MCContext's allocator for 00099 /// obtaining memory. It is a non-throwing new, which means that it returns 00100 /// null on error. (If that is what the allocator does. The current does, so if 00101 /// this ever changes, this operator will have to be changed, too.) 00102 /// Usage looks like this (assuming there's an MCContext 'Context' in scope): 00103 /// @code 00104 /// // Default alignment (16) 00105 /// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments); 00106 /// // Specific alignment 00107 /// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments); 00108 /// @endcode 00109 /// Please note that you cannot use delete on the pointer; it must be 00110 /// deallocated using an explicit destructor call followed by 00111 /// @c Context.Deallocate(Ptr). 00112 /// 00113 /// @param Bytes The number of bytes to allocate. Calculated by the compiler. 00114 /// @param C The MCContext that provides the allocator. 00115 /// @param Alignment The alignment of the allocated memory (if the underlying 00116 /// allocator supports it). 00117 /// @return The allocated memory. Could be NULL. 00118 inline void *operator new(size_t Bytes, llvm::MCContext &C, 00119 size_t Alignment = 16) throw () { 00120 return C.Allocate(Bytes, Alignment); 00121 } 00122 /// @brief Placement delete companion to the new above. 00123 /// 00124 /// This operator is just a companion to the new above. There is no way of 00125 /// invoking it directly; see the new operator for more details. This operator 00126 /// is called implicitly by the compiler if a placement new expression using 00127 /// the MCContext throws in the object constructor. 00128 inline void operator delete(void *Ptr, llvm::MCContext &C, size_t) 00129 throw () { 00130 C.Deallocate(Ptr); 00131 } 00132 00133 /// This placement form of operator new[] uses the MCContext's allocator for 00134 /// obtaining memory. It is a non-throwing new[], which means that it returns 00135 /// null on error. 00136 /// Usage looks like this (assuming there's an MCContext 'Context' in scope): 00137 /// @code 00138 /// // Default alignment (16) 00139 /// char *data = new (Context) char[10]; 00140 /// // Specific alignment 00141 /// char *data = new (Context, 8) char[10]; 00142 /// @endcode 00143 /// Please note that you cannot use delete on the pointer; it must be 00144 /// deallocated using an explicit destructor call followed by 00145 /// @c Context.Deallocate(Ptr). 00146 /// 00147 /// @param Bytes The number of bytes to allocate. Calculated by the compiler. 00148 /// @param C The MCContext that provides the allocator. 00149 /// @param Alignment The alignment of the allocated memory (if the underlying 00150 /// allocator supports it). 00151 /// @return The allocated memory. Could be NULL. 00152 inline void *operator new[](size_t Bytes, llvm::MCContext& C, 00153 size_t Alignment = 16) throw () { 00154 return C.Allocate(Bytes, Alignment); 00155 } 00156 00157 /// @brief Placement delete[] companion to the new[] above. 00158 /// 00159 /// This operator is just a companion to the new[] above. There is no way of 00160 /// invoking it directly; see the new[] operator for more details. This operator 00161 /// is called implicitly by the compiler if a placement new[] expression using 00162 /// the MCContext throws in the object constructor. 00163 inline void operator delete[](void *Ptr, llvm::MCContext &C) throw () { 00164 C.Deallocate(Ptr); 00165 } 00166 00167 #endif