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