MCContext.h revision 36b56886974eae4f9c5ebc96befd3e7bfe5de338
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/SmallString.h" 15#include "llvm/ADT/SmallVector.h" 16#include "llvm/ADT/StringMap.h" 17#include "llvm/MC/MCDwarf.h" 18#include "llvm/MC/SectionKind.h" 19#include "llvm/Support/Allocator.h" 20#include "llvm/Support/Compiler.h" 21#include "llvm/Support/raw_ostream.h" 22#include <map> 23#include <vector> // FIXME: Shouldn't be needed. 24 25namespace llvm { 26 class MCAsmInfo; 27 class MCExpr; 28 class MCSection; 29 class MCSymbol; 30 class MCLabel; 31 struct MCDwarfFile; 32 class MCDwarfLoc; 33 class MCObjectFileInfo; 34 class MCRegisterInfo; 35 class MCLineSection; 36 class SMLoc; 37 class StringRef; 38 class Twine; 39 class MCSectionMachO; 40 class MCSectionELF; 41 class MCSectionCOFF; 42 43 /// MCContext - Context object for machine code objects. This class owns all 44 /// of the sections that it creates. 45 /// 46 class MCContext { 47 MCContext(const MCContext&) LLVM_DELETED_FUNCTION; 48 MCContext &operator=(const MCContext&) LLVM_DELETED_FUNCTION; 49 public: 50 typedef StringMap<MCSymbol*, BumpPtrAllocator&> SymbolTable; 51 private: 52 /// The SourceMgr for this object, if any. 53 const SourceMgr *SrcMgr; 54 55 /// The MCAsmInfo for this target. 56 const MCAsmInfo *MAI; 57 58 /// The MCRegisterInfo for this target. 59 const MCRegisterInfo *MRI; 60 61 /// The MCObjectFileInfo for this target. 62 const MCObjectFileInfo *MOFI; 63 64 /// Allocator - Allocator object used for creating machine code objects. 65 /// 66 /// We use a bump pointer allocator to avoid the need to track all allocated 67 /// objects. 68 BumpPtrAllocator Allocator; 69 70 /// Symbols - Bindings of names to symbols. 71 SymbolTable Symbols; 72 73 /// A maping from a local label number and an instance count to a symbol. 74 /// For example, in the assembly 75 /// 1: 76 /// 2: 77 /// 1: 78 /// We have three labels represented by the pairs (1, 0), (2, 0) and (1, 1) 79 DenseMap<std::pair<unsigned, unsigned>, MCSymbol*> LocalSymbols; 80 81 /// UsedNames - Keeps tracks of names that were used both for used declared 82 /// and artificial symbols. 83 StringMap<bool, BumpPtrAllocator&> UsedNames; 84 85 /// NextUniqueID - The next ID to dole out to an unnamed assembler temporary 86 /// symbol. 87 unsigned NextUniqueID; 88 89 /// Instances of directional local labels. 90 DenseMap<unsigned, MCLabel *> Instances; 91 /// NextInstance() creates the next instance of the directional local label 92 /// for the LocalLabelVal and adds it to the map if needed. 93 unsigned NextInstance(unsigned LocalLabelVal); 94 /// GetInstance() gets the current instance of the directional local label 95 /// for the LocalLabelVal and adds it to the map if needed. 96 unsigned GetInstance(unsigned LocalLabelVal); 97 98 /// The file name of the log file from the environment variable 99 /// AS_SECURE_LOG_FILE. Which must be set before the .secure_log_unique 100 /// directive is used or it is an error. 101 char *SecureLogFile; 102 /// The stream that gets written to for the .secure_log_unique directive. 103 raw_ostream *SecureLog; 104 /// Boolean toggled when .secure_log_unique / .secure_log_reset is seen to 105 /// catch errors if .secure_log_unique appears twice without 106 /// .secure_log_reset appearing between them. 107 bool SecureLogUsed; 108 109 /// The compilation directory to use for DW_AT_comp_dir. 110 SmallString<128> CompilationDir; 111 112 /// The main file name if passed in explicitly. 113 std::string MainFileName; 114 115 /// The dwarf file and directory tables from the dwarf .file directive. 116 /// We now emit a line table for each compile unit. To reduce the prologue 117 /// size of each line table, the files and directories used by each compile 118 /// unit are separated. 119 std::map<unsigned, MCDwarfLineTable> MCDwarfLineTablesCUMap; 120 121 /// The current dwarf line information from the last dwarf .loc directive. 122 MCDwarfLoc CurrentDwarfLoc; 123 bool DwarfLocSeen; 124 125 /// Generate dwarf debugging info for assembly source files. 126 bool GenDwarfForAssembly; 127 128 /// The current dwarf file number when generate dwarf debugging info for 129 /// assembly source files. 130 unsigned GenDwarfFileNumber; 131 132 /// The default initial text section that we generate dwarf debugging line 133 /// info for when generating dwarf assembly source files. 134 const MCSection *GenDwarfSection; 135 /// Symbols created for the start and end of this section. 136 MCSymbol *GenDwarfSectionStartSym, *GenDwarfSectionEndSym; 137 138 /// The information gathered from labels that will have dwarf label 139 /// entries when generating dwarf assembly source files. 140 std::vector<const MCGenDwarfLabelEntry *> MCGenDwarfLabelEntries; 141 142 /// The string to embed in the debug information for the compile unit, if 143 /// non-empty. 144 StringRef DwarfDebugFlags; 145 146 /// The string to embed in as the dwarf AT_producer for the compile unit, if 147 /// non-empty. 148 StringRef DwarfDebugProducer; 149 150 /// Honor temporary labels, this is useful for debugging semantic 151 /// differences between temporary and non-temporary labels (primarily on 152 /// Darwin). 153 bool AllowTemporaryLabels; 154 155 /// The Compile Unit ID that we are currently processing. 156 unsigned DwarfCompileUnitID; 157 158 void *MachOUniquingMap, *ELFUniquingMap, *COFFUniquingMap; 159 160 /// Do automatic reset in destructor 161 bool AutoReset; 162 163 MCSymbol *CreateSymbol(StringRef Name); 164 165 MCSymbol *getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal, 166 unsigned Instance); 167 168 public: 169 explicit MCContext(const MCAsmInfo *MAI, const MCRegisterInfo *MRI, 170 const MCObjectFileInfo *MOFI, const SourceMgr *Mgr = 0, 171 bool DoAutoReset = true); 172 ~MCContext(); 173 174 const SourceMgr *getSourceManager() const { return SrcMgr; } 175 176 const MCAsmInfo *getAsmInfo() const { return MAI; } 177 178 const MCRegisterInfo *getRegisterInfo() const { return MRI; } 179 180 const MCObjectFileInfo *getObjectFileInfo() const { return MOFI; } 181 182 void setAllowTemporaryLabels(bool Value) { AllowTemporaryLabels = Value; } 183 184 /// @name Module Lifetime Management 185 /// @{ 186 187 /// reset - return object to right after construction state to prepare 188 /// to process a new module 189 void reset(); 190 191 /// @} 192 193 /// @name Symbol Management 194 /// @{ 195 196 /// CreateLinkerPrivateTempSymbol - Create and return a new linker temporary 197 /// symbol with a unique but unspecified name. 198 MCSymbol *CreateLinkerPrivateTempSymbol(); 199 200 /// CreateTempSymbol - Create and return a new assembler temporary symbol 201 /// with a unique but unspecified name. 202 MCSymbol *CreateTempSymbol(); 203 204 /// getUniqueSymbolID() - Return a unique identifier for use in constructing 205 /// symbol names. 206 unsigned getUniqueSymbolID() { return NextUniqueID++; } 207 208 /// Create the definition of a directional local symbol for numbered label 209 /// (used for "1:" definitions). 210 MCSymbol *CreateDirectionalLocalSymbol(unsigned LocalLabelVal); 211 212 /// Create and return a directional local symbol for numbered label (used 213 /// for "1b" or 1f" references). 214 MCSymbol *GetDirectionalLocalSymbol(unsigned LocalLabelVal, bool Before); 215 216 /// GetOrCreateSymbol - Lookup the symbol inside with the specified 217 /// @p Name. If it exists, return it. If not, create a forward 218 /// reference and return it. 219 /// 220 /// @param Name - The symbol name, which must be unique across all symbols. 221 MCSymbol *GetOrCreateSymbol(StringRef Name); 222 MCSymbol *GetOrCreateSymbol(const Twine &Name); 223 224 /// LookupSymbol - Get the symbol for \p Name, or null. 225 MCSymbol *LookupSymbol(StringRef Name) const; 226 MCSymbol *LookupSymbol(const Twine &Name) const; 227 228 /// getSymbols - Get a reference for the symbol table for clients that 229 /// want to, for example, iterate over all symbols. 'const' because we 230 /// still want any modifications to the table itself to use the MCContext 231 /// APIs. 232 const SymbolTable &getSymbols() const { 233 return Symbols; 234 } 235 236 /// @} 237 238 /// @name Section Management 239 /// @{ 240 241 /// getMachOSection - Return the MCSection for the specified mach-o section. 242 /// This requires the operands to be valid. 243 const MCSectionMachO *getMachOSection(StringRef Segment, 244 StringRef Section, 245 unsigned TypeAndAttributes, 246 unsigned Reserved2, 247 SectionKind K); 248 const MCSectionMachO *getMachOSection(StringRef Segment, 249 StringRef Section, 250 unsigned TypeAndAttributes, 251 SectionKind K) { 252 return getMachOSection(Segment, Section, TypeAndAttributes, 0, K); 253 } 254 255 const MCSectionELF *getELFSection(StringRef Section, unsigned Type, 256 unsigned Flags, SectionKind Kind); 257 258 const MCSectionELF *getELFSection(StringRef Section, unsigned Type, 259 unsigned Flags, SectionKind Kind, 260 unsigned EntrySize, StringRef Group); 261 262 const MCSectionELF *CreateELFGroupSection(); 263 264 const MCSectionCOFF *getCOFFSection(StringRef Section, 265 unsigned Characteristics, 266 SectionKind Kind, 267 StringRef COMDATSymName, 268 int Selection, 269 const MCSectionCOFF *Assoc = 0); 270 271 const MCSectionCOFF *getCOFFSection(StringRef Section, 272 unsigned Characteristics, 273 SectionKind Kind); 274 275 const MCSectionCOFF *getCOFFSection(StringRef Section); 276 277 /// @} 278 279 /// @name Dwarf Management 280 /// @{ 281 282 /// \brief Get the compilation directory for DW_AT_comp_dir 283 /// This can be overridden by clients which want to control the reported 284 /// compilation directory and have it be something other than the current 285 /// working directory. 286 /// Returns an empty string if the current directory cannot be determined. 287 StringRef getCompilationDir() const { return CompilationDir; } 288 289 /// \brief Set the compilation directory for DW_AT_comp_dir 290 /// Override the default (CWD) compilation directory. 291 void setCompilationDir(StringRef S) { CompilationDir = S.str(); } 292 293 /// \brief Get the main file name for use in error messages and debug 294 /// info. This can be set to ensure we've got the correct file name 295 /// after preprocessing or for -save-temps. 296 const std::string &getMainFileName() const { return MainFileName; } 297 298 /// \brief Set the main file name and override the default. 299 void setMainFileName(StringRef S) { MainFileName = S; } 300 301 /// GetDwarfFile - creates an entry in the dwarf file and directory tables. 302 unsigned GetDwarfFile(StringRef Directory, StringRef FileName, 303 unsigned FileNumber, unsigned CUID); 304 305 bool isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID = 0); 306 307 bool hasDwarfFiles() const { 308 // Traverse MCDwarfFilesCUMap and check whether each entry is empty. 309 for (const auto &FileTable : MCDwarfLineTablesCUMap) 310 if (!FileTable.second.getMCDwarfFiles().empty()) 311 return true; 312 return false; 313 } 314 315 const std::map<unsigned, MCDwarfLineTable> &getMCDwarfLineTables() const { 316 return MCDwarfLineTablesCUMap; 317 } 318 319 MCDwarfLineTable &getMCDwarfLineTable(unsigned CUID) { 320 return MCDwarfLineTablesCUMap[CUID]; 321 } 322 323 const MCDwarfLineTable &getMCDwarfLineTable(unsigned CUID) const { 324 auto I = MCDwarfLineTablesCUMap.find(CUID); 325 assert(I != MCDwarfLineTablesCUMap.end()); 326 return I->second; 327 } 328 329 const SmallVectorImpl<MCDwarfFile> &getMCDwarfFiles(unsigned CUID = 0) { 330 return getMCDwarfLineTable(CUID).getMCDwarfFiles(); 331 } 332 const SmallVectorImpl<std::string> &getMCDwarfDirs(unsigned CUID = 0) { 333 return getMCDwarfLineTable(CUID).getMCDwarfDirs(); 334 } 335 336 bool hasMCLineSections() const { 337 for (const auto &Table : MCDwarfLineTablesCUMap) 338 if (!Table.second.getMCDwarfFiles().empty() || Table.second.getLabel()) 339 return true; 340 return false; 341 } 342 unsigned getDwarfCompileUnitID() { 343 return DwarfCompileUnitID; 344 } 345 void setDwarfCompileUnitID(unsigned CUIndex) { 346 DwarfCompileUnitID = CUIndex; 347 } 348 void setMCLineTableCompilationDir(unsigned CUID, StringRef CompilationDir) { 349 getMCDwarfLineTable(CUID).setCompilationDir(CompilationDir); 350 } 351 352 /// setCurrentDwarfLoc - saves the information from the currently parsed 353 /// dwarf .loc directive and sets DwarfLocSeen. When the next instruction 354 /// is assembled an entry in the line number table with this information and 355 /// the address of the instruction will be created. 356 void setCurrentDwarfLoc(unsigned FileNum, unsigned Line, unsigned Column, 357 unsigned Flags, unsigned Isa, 358 unsigned Discriminator) { 359 CurrentDwarfLoc.setFileNum(FileNum); 360 CurrentDwarfLoc.setLine(Line); 361 CurrentDwarfLoc.setColumn(Column); 362 CurrentDwarfLoc.setFlags(Flags); 363 CurrentDwarfLoc.setIsa(Isa); 364 CurrentDwarfLoc.setDiscriminator(Discriminator); 365 DwarfLocSeen = true; 366 } 367 void ClearDwarfLocSeen() { DwarfLocSeen = false; } 368 369 bool getDwarfLocSeen() { return DwarfLocSeen; } 370 const MCDwarfLoc &getCurrentDwarfLoc() { return CurrentDwarfLoc; } 371 372 bool getGenDwarfForAssembly() { return GenDwarfForAssembly; } 373 void setGenDwarfForAssembly(bool Value) { GenDwarfForAssembly = Value; } 374 unsigned getGenDwarfFileNumber() { return GenDwarfFileNumber; } 375 void setGenDwarfFileNumber(unsigned FileNumber) { 376 GenDwarfFileNumber = FileNumber; 377 } 378 const MCSection *getGenDwarfSection() { return GenDwarfSection; } 379 void setGenDwarfSection(const MCSection *Sec) { GenDwarfSection = Sec; } 380 MCSymbol *getGenDwarfSectionStartSym() { return GenDwarfSectionStartSym; } 381 void setGenDwarfSectionStartSym(MCSymbol *Sym) { 382 GenDwarfSectionStartSym = Sym; 383 } 384 MCSymbol *getGenDwarfSectionEndSym() { return GenDwarfSectionEndSym; } 385 void setGenDwarfSectionEndSym(MCSymbol *Sym) { 386 GenDwarfSectionEndSym = Sym; 387 } 388 const std::vector<const MCGenDwarfLabelEntry *> 389 &getMCGenDwarfLabelEntries() const { 390 return MCGenDwarfLabelEntries; 391 } 392 void addMCGenDwarfLabelEntry(const MCGenDwarfLabelEntry *E) { 393 MCGenDwarfLabelEntries.push_back(E); 394 } 395 396 void setDwarfDebugFlags(StringRef S) { DwarfDebugFlags = S; } 397 StringRef getDwarfDebugFlags() { return DwarfDebugFlags; } 398 399 void setDwarfDebugProducer(StringRef S) { DwarfDebugProducer = S; } 400 StringRef getDwarfDebugProducer() { return DwarfDebugProducer; } 401 402 /// @} 403 404 char *getSecureLogFile() { return SecureLogFile; } 405 raw_ostream *getSecureLog() { return SecureLog; } 406 bool getSecureLogUsed() { return SecureLogUsed; } 407 void setSecureLog(raw_ostream *Value) { 408 SecureLog = Value; 409 } 410 void setSecureLogUsed(bool Value) { 411 SecureLogUsed = Value; 412 } 413 414 void *Allocate(unsigned Size, unsigned Align = 8) { 415 return Allocator.Allocate(Size, Align); 416 } 417 void Deallocate(void *Ptr) { 418 } 419 420 // Unrecoverable error has occurred. Display the best diagnostic we can 421 // and bail via exit(1). For now, most MC backend errors are unrecoverable. 422 // FIXME: We should really do something about that. 423 LLVM_ATTRIBUTE_NORETURN void FatalError(SMLoc L, const Twine &Msg); 424 }; 425 426} // end namespace llvm 427 428// operator new and delete aren't allowed inside namespaces. 429// The throw specifications are mandated by the standard. 430/// @brief Placement new for using the MCContext's allocator. 431/// 432/// This placement form of operator new uses the MCContext's allocator for 433/// obtaining memory. It is a non-throwing new, which means that it returns 434/// null on error. (If that is what the allocator does. The current does, so if 435/// this ever changes, this operator will have to be changed, too.) 436/// Usage looks like this (assuming there's an MCContext 'Context' in scope): 437/// @code 438/// // Default alignment (16) 439/// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments); 440/// // Specific alignment 441/// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments); 442/// @endcode 443/// Please note that you cannot use delete on the pointer; it must be 444/// deallocated using an explicit destructor call followed by 445/// @c Context.Deallocate(Ptr). 446/// 447/// @param Bytes The number of bytes to allocate. Calculated by the compiler. 448/// @param C The MCContext that provides the allocator. 449/// @param Alignment The alignment of the allocated memory (if the underlying 450/// allocator supports it). 451/// @return The allocated memory. Could be NULL. 452inline void *operator new(size_t Bytes, llvm::MCContext &C, 453 size_t Alignment = 16) throw () { 454 return C.Allocate(Bytes, Alignment); 455} 456/// @brief Placement delete companion to the new above. 457/// 458/// This operator is just a companion to the new above. There is no way of 459/// invoking it directly; see the new operator for more details. This operator 460/// is called implicitly by the compiler if a placement new expression using 461/// the MCContext throws in the object constructor. 462inline void operator delete(void *Ptr, llvm::MCContext &C, size_t) 463 throw () { 464 C.Deallocate(Ptr); 465} 466 467/// This placement form of operator new[] uses the MCContext's allocator for 468/// obtaining memory. It is a non-throwing new[], which means that it returns 469/// null on error. 470/// Usage looks like this (assuming there's an MCContext 'Context' in scope): 471/// @code 472/// // Default alignment (16) 473/// char *data = new (Context) char[10]; 474/// // Specific alignment 475/// char *data = new (Context, 8) char[10]; 476/// @endcode 477/// Please note that you cannot use delete on the pointer; it must be 478/// deallocated using an explicit destructor call followed by 479/// @c Context.Deallocate(Ptr). 480/// 481/// @param Bytes The number of bytes to allocate. Calculated by the compiler. 482/// @param C The MCContext that provides the allocator. 483/// @param Alignment The alignment of the allocated memory (if the underlying 484/// allocator supports it). 485/// @return The allocated memory. Could be NULL. 486inline void *operator new[](size_t Bytes, llvm::MCContext& C, 487 size_t Alignment = 16) throw () { 488 return C.Allocate(Bytes, Alignment); 489} 490 491/// @brief Placement delete[] companion to the new[] above. 492/// 493/// This operator is just a companion to the new[] above. There is no way of 494/// invoking it directly; see the new[] operator for more details. This operator 495/// is called implicitly by the compiler if a placement new[] expression using 496/// the MCContext throws in the object constructor. 497inline void operator delete[](void *Ptr, llvm::MCContext &C) throw () { 498 C.Deallocate(Ptr); 499} 500 501#endif 502