MCContext.h revision 6e5ce287b0e53c264af0ba37169ad964e19b5bb7
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/MC/SectionKind.h" 14#include "llvm/ADT/DenseMap.h" 15#include "llvm/ADT/StringMap.h" 16#include "llvm/Support/Allocator.h" 17 18namespace llvm { 19 class MCAsmInfo; 20 class MCExpr; 21 class MCSection; 22 class MCSymbol; 23 class StringRef; 24 class Twine; 25 class MCSectionMachO; 26 27 /// MCContext - Context object for machine code objects. This class owns all 28 /// of the sections that it creates. 29 /// 30 class MCContext { 31 MCContext(const MCContext&); // DO NOT IMPLEMENT 32 MCContext &operator=(const MCContext&); // DO NOT IMPLEMENT 33 34 /// The MCAsmInfo for this target. 35 const MCAsmInfo &MAI; 36 37 /// Sections - Bindings of names to allocated sections. 38 StringMap<MCSection*> Sections; 39 40 /// Symbols - Bindings of names to symbols. 41 StringMap<MCSymbol*> Symbols; 42 43 /// NextUniqueID - The next ID to dole out to an unnamed assembler temporary 44 /// symbol. 45 unsigned NextUniqueID; 46 47 /// Allocator - Allocator object used for creating machine code objects. 48 /// 49 /// We use a bump pointer allocator to avoid the need to track all allocated 50 /// objects. 51 BumpPtrAllocator Allocator; 52 53 void *MachOUniquingMap, *ELFUniquingMap, *COFFUniquingMap; 54 public: 55 explicit MCContext(const MCAsmInfo &MAI); 56 ~MCContext(); 57 58 const MCAsmInfo &getAsmInfo() const { return MAI; } 59 60 /// @name Symbol Managment 61 /// @{ 62 63 /// CreateTempSymbol - Create and return a new assembler temporary symbol 64 /// with a unique but unspecified name. 65 MCSymbol *CreateTempSymbol(); 66 67 /// GetOrCreateSymbol - Lookup the symbol inside with the specified 68 /// @p Name. If it exists, return it. If not, create a forward 69 /// reference and return it. 70 /// 71 /// @param Name - The symbol name, which must be unique across all symbols. 72 MCSymbol *GetOrCreateSymbol(StringRef Name); 73 MCSymbol *GetOrCreateSymbol(const Twine &Name); 74 75 /// LookupSymbol - Get the symbol for \p Name, or null. 76 MCSymbol *LookupSymbol(StringRef Name) const; 77 78 /// @} 79 80 /// @name Section Managment 81 /// @{ 82 83 /// getMachOSection - Return the MCSection for the specified mach-o section. 84 /// This requires the operands to be valid. 85 const MCSectionMachO *getMachOSection(StringRef Segment, 86 StringRef Section, 87 unsigned TypeAndAttributes, 88 unsigned Reserved2, 89 SectionKind K); 90 const MCSectionMachO *getMachOSection(StringRef Segment, 91 StringRef Section, 92 unsigned TypeAndAttributes, 93 SectionKind K) { 94 return getMachOSection(Segment, Section, TypeAndAttributes, 0, K); 95 } 96 97 const MCSection *getELFSection(StringRef Section, unsigned Type, 98 unsigned Flags, SectionKind Kind, 99 bool IsExplicit = false); 100 101 const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics, 102 int Selection, SectionKind Kind); 103 104 const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics, 105 SectionKind Kind) { 106 return getCOFFSection (Section, Characteristics, 0, Kind); 107 } 108 109 110 /// @} 111 112 void *Allocate(unsigned Size, unsigned Align = 8) { 113 return Allocator.Allocate(Size, Align); 114 } 115 void Deallocate(void *Ptr) { 116 } 117 }; 118 119} // end namespace llvm 120 121// operator new and delete aren't allowed inside namespaces. 122// The throw specifications are mandated by the standard. 123/// @brief Placement new for using the MCContext's allocator. 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. (If that is what the allocator does. The current does, so if 128/// this ever changes, this operator will have to be changed, too.) 129/// Usage looks like this (assuming there's an MCContext 'Context' in scope): 130/// @code 131/// // Default alignment (16) 132/// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments); 133/// // Specific alignment 134/// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments); 135/// @endcode 136/// Please note that you cannot use delete on the pointer; it must be 137/// deallocated using an explicit destructor call followed by 138/// @c Context.Deallocate(Ptr). 139/// 140/// @param Bytes The number of bytes to allocate. Calculated by the compiler. 141/// @param C The MCContext that provides the allocator. 142/// @param Alignment The alignment of the allocated memory (if the underlying 143/// allocator supports it). 144/// @return The allocated memory. Could be NULL. 145inline void *operator new(size_t Bytes, llvm::MCContext &C, 146 size_t Alignment = 16) throw () { 147 return C.Allocate(Bytes, Alignment); 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, size_t) 156 throw () { 157 C.Deallocate(Ptr); 158} 159 160/// This placement form of operator new[] uses the MCContext's allocator for 161/// obtaining memory. It is a non-throwing new[], which means that it returns 162/// null on error. 163/// Usage looks like this (assuming there's an MCContext 'Context' in scope): 164/// @code 165/// // Default alignment (16) 166/// char *data = new (Context) char[10]; 167/// // Specific alignment 168/// char *data = new (Context, 8) char[10]; 169/// @endcode 170/// Please note that you cannot use delete on the pointer; it must be 171/// deallocated using an explicit destructor call followed by 172/// @c Context.Deallocate(Ptr). 173/// 174/// @param Bytes The number of bytes to allocate. Calculated by the compiler. 175/// @param C The MCContext that provides the allocator. 176/// @param Alignment The alignment of the allocated memory (if the underlying 177/// allocator supports it). 178/// @return The allocated memory. Could be NULL. 179inline void *operator new[](size_t Bytes, llvm::MCContext& C, 180 size_t Alignment = 16) throw () { 181 return C.Allocate(Bytes, Alignment); 182} 183 184/// @brief Placement delete[] companion to the new[] above. 185/// 186/// This operator is just a companion to the new[] above. There is no way of 187/// invoking it directly; see the new[] operator for more details. This operator 188/// is called implicitly by the compiler if a placement new[] expression using 189/// the MCContext throws in the object constructor. 190inline void operator delete[](void *Ptr, llvm::MCContext &C) throw () { 191 C.Deallocate(Ptr); 192} 193 194#endif 195