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