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