MCContext.h revision 9f44724be058d17944dcd9ef6a6b57734b3744b8
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/MC/MCDwarf.h" 15#include "llvm/ADT/DenseMap.h" 16#include "llvm/ADT/StringMap.h" 17#include "llvm/Support/Allocator.h" 18#include "llvm/Support/raw_ostream.h" 19#include <vector> // FIXME: Shouldn't be needed. 20 21namespace llvm { 22 class MCAsmInfo; 23 class MCExpr; 24 class MCSection; 25 class MCSymbol; 26 class MCLabel; 27 class MCDwarfFile; 28 class MCDwarfLoc; 29 class MCLineSection; 30 class StringRef; 31 class Twine; 32 class MCSectionMachO; 33 class MCSectionELF; 34 35 /// MCContext - Context object for machine code objects. This class owns all 36 /// of the sections that it creates. 37 /// 38 class MCContext { 39 MCContext(const MCContext&); // DO NOT IMPLEMENT 40 MCContext &operator=(const MCContext&); // DO NOT IMPLEMENT 41 42 /// The MCAsmInfo for this target. 43 const MCAsmInfo &MAI; 44 45 /// Symbols - Bindings of names to symbols. 46 StringMap<MCSymbol*> Symbols; 47 48 /// UsedNames - Keeps tracks of names that were used both for used declared 49 /// and artificial symbols. 50 StringMap<bool> UsedNames; 51 52 /// NextUniqueID - The next ID to dole out to an unnamed assembler temporary 53 /// symbol. 54 unsigned NextUniqueID; 55 56 /// Instances of directional local labels. 57 DenseMap<unsigned, MCLabel *> Instances; 58 /// NextInstance() creates the next instance of the directional local label 59 /// for the LocalLabelVal and adds it to the map if needed. 60 unsigned NextInstance(int64_t LocalLabelVal); 61 /// GetInstance() gets the current instance of the directional local label 62 /// for the LocalLabelVal and adds it to the map if needed. 63 unsigned GetInstance(int64_t LocalLabelVal); 64 65 /// The file name of the log file from the environment variable 66 /// AS_SECURE_LOG_FILE. Which must be set before the .secure_log_unique 67 /// directive is used or it is an error. 68 char *SecureLogFile; 69 /// The stream that gets written to for the .secure_log_unique directive. 70 raw_ostream *SecureLog; 71 /// Boolean toggled when .secure_log_unique / .secure_log_reset is seen to 72 /// catch errors if .secure_log_unique appears twice without 73 /// .secure_log_reset appearing between them. 74 bool SecureLogUsed; 75 76 /// The dwarf file and directory tables from the dwarf .file directive. 77 std::vector<MCDwarfFile *> MCDwarfFiles; 78 std::vector<StringRef> MCDwarfDirs; 79 80 /// The current dwarf line information from the last dwarf .loc directive. 81 MCDwarfLoc CurrentDwarfLoc; 82 bool DwarfLocSeen; 83 84 /// The dwarf line information from the .loc directives for the sections 85 /// with assembled machine instructions have after seeing .loc directives. 86 DenseMap<const MCSection *, MCLineSection *> MCLineSections; 87 /// We need a deterministic iteration order, so we remember the order 88 /// the elements were added. 89 std::vector<const MCSection *> MCLineSectionOrder; 90 91 /// Allocator - Allocator object used for creating machine code objects. 92 /// 93 /// We use a bump pointer allocator to avoid the need to track all allocated 94 /// objects. 95 BumpPtrAllocator Allocator; 96 97 void *MachOUniquingMap, *ELFUniquingMap, *COFFUniquingMap; 98 99 MCSymbol *CreateSymbol(StringRef Name); 100 101 public: 102 explicit MCContext(const MCAsmInfo &MAI); 103 ~MCContext(); 104 105 const MCAsmInfo &getAsmInfo() const { return MAI; } 106 107 /// @name Symbol Management 108 /// @{ 109 110 /// CreateTempSymbol - Create and return a new assembler temporary symbol 111 /// with a unique but unspecified name. 112 MCSymbol *CreateTempSymbol(); 113 114 /// CreateDirectionalLocalSymbol - Create the definition of a directional 115 /// local symbol for numbered label (used for "1:" definitions). 116 MCSymbol *CreateDirectionalLocalSymbol(int64_t LocalLabelVal); 117 118 /// GetDirectionalLocalSymbol - Create and return a directional local 119 /// symbol for numbered label (used for "1b" or 1f" references). 120 MCSymbol *GetDirectionalLocalSymbol(int64_t LocalLabelVal, int bORf); 121 122 /// GetOrCreateSymbol - Lookup the symbol inside with the specified 123 /// @p Name. If it exists, return it. If not, create a forward 124 /// reference and return it. 125 /// 126 /// @param Name - The symbol name, which must be unique across all symbols. 127 MCSymbol *GetOrCreateSymbol(StringRef Name); 128 MCSymbol *GetOrCreateSymbol(const Twine &Name); 129 130 /// LookupSymbol - Get the symbol for \p Name, or null. 131 MCSymbol *LookupSymbol(StringRef Name) const; 132 133 /// @} 134 135 /// @name Section Management 136 /// @{ 137 138 /// getMachOSection - Return the MCSection for the specified mach-o section. 139 /// This requires the operands to be valid. 140 const MCSectionMachO *getMachOSection(StringRef Segment, 141 StringRef Section, 142 unsigned TypeAndAttributes, 143 unsigned Reserved2, 144 SectionKind K); 145 const MCSectionMachO *getMachOSection(StringRef Segment, 146 StringRef Section, 147 unsigned TypeAndAttributes, 148 SectionKind K) { 149 return getMachOSection(Segment, Section, TypeAndAttributes, 0, K); 150 } 151 152 const MCSectionELF *getELFSection(StringRef Section, unsigned Type, 153 unsigned Flags, SectionKind Kind); 154 155 const MCSectionELF *getELFSection(StringRef Section, unsigned Type, 156 unsigned Flags, SectionKind Kind, 157 unsigned EntrySize, StringRef Group); 158 159 const MCSectionELF *CreateELFGroupSection(); 160 161 const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics, 162 int Selection, SectionKind Kind); 163 164 const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics, 165 SectionKind Kind) { 166 return getCOFFSection (Section, Characteristics, 0, Kind); 167 } 168 169 170 /// @} 171 172 /// @name Dwarf Management 173 /// @{ 174 175 /// GetDwarfFile - creates an entry in the dwarf file and directory tables. 176 unsigned GetDwarfFile(StringRef FileName, unsigned FileNumber); 177 178 bool isValidDwarfFileNumber(unsigned FileNumber); 179 180 bool hasDwarfFiles(void) { 181 return MCDwarfFiles.size() != 0; 182 } 183 184 const std::vector<MCDwarfFile *> &getMCDwarfFiles() { 185 return MCDwarfFiles; 186 } 187 const std::vector<StringRef> &getMCDwarfDirs() { 188 return MCDwarfDirs; 189 } 190 191 const DenseMap<const MCSection *, MCLineSection *> 192 &getMCLineSections() const { 193 return MCLineSections; 194 } 195 const std::vector<const MCSection *> &getMCLineSectionOrder() const { 196 return MCLineSectionOrder; 197 } 198 void addMCLineSection(const MCSection *Sec, MCLineSection *Line) { 199 MCLineSections[Sec] = Line; 200 MCLineSectionOrder.push_back(Sec); 201 } 202 203 /// setCurrentDwarfLoc - saves the information from the currently parsed 204 /// dwarf .loc directive and sets DwarfLocSeen. When the next instruction 205 /// is assembled an entry in the line number table with this information and 206 /// the address of the instruction will be created. 207 void setCurrentDwarfLoc(unsigned FileNum, unsigned Line, unsigned Column, 208 unsigned Flags, unsigned Isa, 209 unsigned Discriminator) { 210 CurrentDwarfLoc.setFileNum(FileNum); 211 CurrentDwarfLoc.setLine(Line); 212 CurrentDwarfLoc.setColumn(Column); 213 CurrentDwarfLoc.setFlags(Flags); 214 CurrentDwarfLoc.setIsa(Isa); 215 CurrentDwarfLoc.setDiscriminator(Discriminator); 216 DwarfLocSeen = true; 217 } 218 void ClearDwarfLocSeen() { DwarfLocSeen = false; } 219 220 bool getDwarfLocSeen() { return DwarfLocSeen; } 221 const MCDwarfLoc &getCurrentDwarfLoc() { return CurrentDwarfLoc; } 222 223 /// @} 224 225 char *getSecureLogFile() { return SecureLogFile; } 226 raw_ostream *getSecureLog() { return SecureLog; } 227 bool getSecureLogUsed() { return SecureLogUsed; } 228 void setSecureLog(raw_ostream *Value) { 229 SecureLog = Value; 230 } 231 void setSecureLogUsed(bool Value) { 232 SecureLogUsed = Value; 233 } 234 235 void *Allocate(unsigned Size, unsigned Align = 8) { 236 return Allocator.Allocate(Size, Align); 237 } 238 void Deallocate(void *Ptr) { 239 } 240 }; 241 242} // end namespace llvm 243 244// operator new and delete aren't allowed inside namespaces. 245// The throw specifications are mandated by the standard. 246/// @brief Placement new for using the MCContext's allocator. 247/// 248/// This placement form of operator new uses the MCContext's allocator for 249/// obtaining memory. It is a non-throwing new, which means that it returns 250/// null on error. (If that is what the allocator does. The current does, so if 251/// this ever changes, this operator will have to be changed, too.) 252/// Usage looks like this (assuming there's an MCContext 'Context' in scope): 253/// @code 254/// // Default alignment (16) 255/// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments); 256/// // Specific alignment 257/// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments); 258/// @endcode 259/// Please note that you cannot use delete on the pointer; it must be 260/// deallocated using an explicit destructor call followed by 261/// @c Context.Deallocate(Ptr). 262/// 263/// @param Bytes The number of bytes to allocate. Calculated by the compiler. 264/// @param C The MCContext that provides the allocator. 265/// @param Alignment The alignment of the allocated memory (if the underlying 266/// allocator supports it). 267/// @return The allocated memory. Could be NULL. 268inline void *operator new(size_t Bytes, llvm::MCContext &C, 269 size_t Alignment = 16) throw () { 270 return C.Allocate(Bytes, Alignment); 271} 272/// @brief Placement delete companion to the new above. 273/// 274/// This operator is just a companion to the new above. There is no way of 275/// invoking it directly; see the new operator for more details. This operator 276/// is called implicitly by the compiler if a placement new expression using 277/// the MCContext throws in the object constructor. 278inline void operator delete(void *Ptr, llvm::MCContext &C, size_t) 279 throw () { 280 C.Deallocate(Ptr); 281} 282 283/// This placement form of operator new[] uses the MCContext's allocator for 284/// obtaining memory. It is a non-throwing new[], which means that it returns 285/// null on error. 286/// Usage looks like this (assuming there's an MCContext 'Context' in scope): 287/// @code 288/// // Default alignment (16) 289/// char *data = new (Context) char[10]; 290/// // Specific alignment 291/// char *data = new (Context, 8) char[10]; 292/// @endcode 293/// Please note that you cannot use delete on the pointer; it must be 294/// deallocated using an explicit destructor call followed by 295/// @c Context.Deallocate(Ptr). 296/// 297/// @param Bytes The number of bytes to allocate. Calculated by the compiler. 298/// @param C The MCContext that provides the allocator. 299/// @param Alignment The alignment of the allocated memory (if the underlying 300/// allocator supports it). 301/// @return The allocated memory. Could be NULL. 302inline void *operator new[](size_t Bytes, llvm::MCContext& C, 303 size_t Alignment = 16) throw () { 304 return C.Allocate(Bytes, Alignment); 305} 306 307/// @brief Placement delete[] companion to the new[] above. 308/// 309/// This operator is just a companion to the new[] above. There is no way of 310/// invoking it directly; see the new[] operator for more details. This operator 311/// is called implicitly by the compiler if a placement new[] expression using 312/// the MCContext throws in the object constructor. 313inline void operator delete[](void *Ptr, llvm::MCContext &C) throw () { 314 C.Deallocate(Ptr); 315} 316 317#endif 318