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