Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- MCContext.h - Machine Code Context -----------------------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef LLVM_MC_MCCONTEXT_H |
| 10 | #define LLVM_MC_MCCONTEXT_H |
| 11 | |
| 12 | #include "llvm/ADT/DenseMap.h" |
| 13 | #include "llvm/ADT/Optional.h" |
| 14 | #include "llvm/ADT/SetVector.h" |
| 15 | #include "llvm/ADT/SmallString.h" |
| 16 | #include "llvm/ADT/SmallVector.h" |
| 17 | #include "llvm/ADT/StringMap.h" |
| 18 | #include "llvm/ADT/StringRef.h" |
| 19 | #include "llvm/ADT/Twine.h" |
| 20 | #include "llvm/BinaryFormat/Dwarf.h" |
| 21 | #include "llvm/MC/MCAsmMacro.h" |
| 22 | #include "llvm/MC/MCDwarf.h" |
| 23 | #include "llvm/MC/MCSubtargetInfo.h" |
| 24 | #include "llvm/MC/SectionKind.h" |
| 25 | #include "llvm/Support/Allocator.h" |
| 26 | #include "llvm/Support/Compiler.h" |
| 27 | #include "llvm/Support/Error.h" |
| 28 | #include "llvm/Support/MD5.h" |
| 29 | #include "llvm/Support/raw_ostream.h" |
| 30 | #include <algorithm> |
| 31 | #include <cassert> |
| 32 | #include <cstddef> |
| 33 | #include <cstdint> |
| 34 | #include <map> |
| 35 | #include <memory> |
| 36 | #include <string> |
| 37 | #include <utility> |
| 38 | #include <vector> |
| 39 | |
| 40 | namespace llvm { |
| 41 | |
| 42 | class CodeViewContext; |
| 43 | class MCAsmInfo; |
| 44 | class MCLabel; |
| 45 | class MCObjectFileInfo; |
| 46 | class MCRegisterInfo; |
| 47 | class MCSection; |
| 48 | class MCSectionCOFF; |
| 49 | class MCSectionELF; |
| 50 | class MCSectionMachO; |
| 51 | class MCSectionWasm; |
| 52 | class MCStreamer; |
| 53 | class MCSymbol; |
| 54 | class MCSymbolELF; |
| 55 | class MCSymbolWasm; |
| 56 | class SMLoc; |
| 57 | class SourceMgr; |
| 58 | |
| 59 | /// Context object for machine code objects. This class owns all of the |
| 60 | /// sections that it creates. |
| 61 | /// |
| 62 | class MCContext { |
| 63 | public: |
| 64 | using SymbolTable = StringMap<MCSymbol *, BumpPtrAllocator &>; |
| 65 | |
| 66 | private: |
| 67 | /// The SourceMgr for this object, if any. |
| 68 | const SourceMgr *SrcMgr; |
| 69 | |
| 70 | /// The SourceMgr for inline assembly, if any. |
| 71 | SourceMgr *InlineSrcMgr; |
| 72 | |
| 73 | /// The MCAsmInfo for this target. |
| 74 | const MCAsmInfo *MAI; |
| 75 | |
| 76 | /// The MCRegisterInfo for this target. |
| 77 | const MCRegisterInfo *MRI; |
| 78 | |
| 79 | /// The MCObjectFileInfo for this target. |
| 80 | const MCObjectFileInfo *MOFI; |
| 81 | |
| 82 | std::unique_ptr<CodeViewContext> CVContext; |
| 83 | |
| 84 | /// Allocator object used for creating machine code objects. |
| 85 | /// |
| 86 | /// We use a bump pointer allocator to avoid the need to track all allocated |
| 87 | /// objects. |
| 88 | BumpPtrAllocator Allocator; |
| 89 | |
| 90 | SpecificBumpPtrAllocator<MCSectionCOFF> COFFAllocator; |
| 91 | SpecificBumpPtrAllocator<MCSectionELF> ELFAllocator; |
| 92 | SpecificBumpPtrAllocator<MCSectionMachO> MachOAllocator; |
| 93 | SpecificBumpPtrAllocator<MCSectionWasm> WasmAllocator; |
| 94 | |
| 95 | /// Bindings of names to symbols. |
| 96 | SymbolTable Symbols; |
| 97 | |
| 98 | /// A mapping from a local label number and an instance count to a symbol. |
| 99 | /// For example, in the assembly |
| 100 | /// 1: |
| 101 | /// 2: |
| 102 | /// 1: |
| 103 | /// We have three labels represented by the pairs (1, 0), (2, 0) and (1, 1) |
| 104 | DenseMap<std::pair<unsigned, unsigned>, MCSymbol *> LocalSymbols; |
| 105 | |
| 106 | /// Keeps tracks of names that were used both for used declared and |
| 107 | /// artificial symbols. The value is "true" if the name has been used for a |
| 108 | /// non-section symbol (there can be at most one of those, plus an unlimited |
| 109 | /// number of section symbols with the same name). |
| 110 | StringMap<bool, BumpPtrAllocator &> UsedNames; |
| 111 | |
| 112 | /// The next ID to dole out to an unnamed assembler temporary symbol with |
| 113 | /// a given prefix. |
| 114 | StringMap<unsigned> NextID; |
| 115 | |
| 116 | /// Instances of directional local labels. |
| 117 | DenseMap<unsigned, MCLabel *> Instances; |
| 118 | /// NextInstance() creates the next instance of the directional local label |
| 119 | /// for the LocalLabelVal and adds it to the map if needed. |
| 120 | unsigned NextInstance(unsigned LocalLabelVal); |
| 121 | /// GetInstance() gets the current instance of the directional local label |
| 122 | /// for the LocalLabelVal and adds it to the map if needed. |
| 123 | unsigned GetInstance(unsigned LocalLabelVal); |
| 124 | |
| 125 | /// The file name of the log file from the environment variable |
| 126 | /// AS_SECURE_LOG_FILE. Which must be set before the .secure_log_unique |
| 127 | /// directive is used or it is an error. |
| 128 | char *SecureLogFile; |
| 129 | /// The stream that gets written to for the .secure_log_unique directive. |
| 130 | std::unique_ptr<raw_fd_ostream> SecureLog; |
| 131 | /// Boolean toggled when .secure_log_unique / .secure_log_reset is seen to |
| 132 | /// catch errors if .secure_log_unique appears twice without |
| 133 | /// .secure_log_reset appearing between them. |
| 134 | bool SecureLogUsed = false; |
| 135 | |
| 136 | /// The compilation directory to use for DW_AT_comp_dir. |
| 137 | SmallString<128> CompilationDir; |
| 138 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 139 | /// Prefix replacement map for source file information. |
| 140 | std::map<const std::string, const std::string> DebugPrefixMap; |
| 141 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 142 | /// The main file name if passed in explicitly. |
| 143 | std::string MainFileName; |
| 144 | |
| 145 | /// The dwarf file and directory tables from the dwarf .file directive. |
| 146 | /// We now emit a line table for each compile unit. To reduce the prologue |
| 147 | /// size of each line table, the files and directories used by each compile |
| 148 | /// unit are separated. |
| 149 | std::map<unsigned, MCDwarfLineTable> MCDwarfLineTablesCUMap; |
| 150 | |
| 151 | /// The current dwarf line information from the last dwarf .loc directive. |
| 152 | MCDwarfLoc CurrentDwarfLoc; |
| 153 | bool DwarfLocSeen = false; |
| 154 | |
| 155 | /// Generate dwarf debugging info for assembly source files. |
| 156 | bool GenDwarfForAssembly = false; |
| 157 | |
| 158 | /// The current dwarf file number when generate dwarf debugging info for |
| 159 | /// assembly source files. |
| 160 | unsigned GenDwarfFileNumber = 0; |
| 161 | |
| 162 | /// Sections for generating the .debug_ranges and .debug_aranges sections. |
| 163 | SetVector<MCSection *> SectionsForRanges; |
| 164 | |
| 165 | /// The information gathered from labels that will have dwarf label |
| 166 | /// entries when generating dwarf assembly source files. |
| 167 | std::vector<MCGenDwarfLabelEntry> MCGenDwarfLabelEntries; |
| 168 | |
| 169 | /// The string to embed in the debug information for the compile unit, if |
| 170 | /// non-empty. |
| 171 | StringRef DwarfDebugFlags; |
| 172 | |
| 173 | /// The string to embed in as the dwarf AT_producer for the compile unit, if |
| 174 | /// non-empty. |
| 175 | StringRef DwarfDebugProducer; |
| 176 | |
| 177 | /// The maximum version of dwarf that we should emit. |
| 178 | uint16_t DwarfVersion = 4; |
| 179 | |
| 180 | /// Honor temporary labels, this is useful for debugging semantic |
| 181 | /// differences between temporary and non-temporary labels (primarily on |
| 182 | /// Darwin). |
| 183 | bool AllowTemporaryLabels = true; |
| 184 | bool UseNamesOnTempLabels = true; |
| 185 | |
| 186 | /// The Compile Unit ID that we are currently processing. |
| 187 | unsigned DwarfCompileUnitID = 0; |
| 188 | |
| 189 | struct ELFSectionKey { |
| 190 | std::string SectionName; |
| 191 | StringRef GroupName; |
| 192 | unsigned UniqueID; |
| 193 | |
| 194 | ELFSectionKey(StringRef SectionName, StringRef GroupName, |
| 195 | unsigned UniqueID) |
| 196 | : SectionName(SectionName), GroupName(GroupName), UniqueID(UniqueID) { |
| 197 | } |
| 198 | |
| 199 | bool operator<(const ELFSectionKey &Other) const { |
| 200 | if (SectionName != Other.SectionName) |
| 201 | return SectionName < Other.SectionName; |
| 202 | if (GroupName != Other.GroupName) |
| 203 | return GroupName < Other.GroupName; |
| 204 | return UniqueID < Other.UniqueID; |
| 205 | } |
| 206 | }; |
| 207 | |
| 208 | struct COFFSectionKey { |
| 209 | std::string SectionName; |
| 210 | StringRef GroupName; |
| 211 | int SelectionKey; |
| 212 | unsigned UniqueID; |
| 213 | |
| 214 | COFFSectionKey(StringRef SectionName, StringRef GroupName, |
| 215 | int SelectionKey, unsigned UniqueID) |
| 216 | : SectionName(SectionName), GroupName(GroupName), |
| 217 | SelectionKey(SelectionKey), UniqueID(UniqueID) {} |
| 218 | |
| 219 | bool operator<(const COFFSectionKey &Other) const { |
| 220 | if (SectionName != Other.SectionName) |
| 221 | return SectionName < Other.SectionName; |
| 222 | if (GroupName != Other.GroupName) |
| 223 | return GroupName < Other.GroupName; |
| 224 | if (SelectionKey != Other.SelectionKey) |
| 225 | return SelectionKey < Other.SelectionKey; |
| 226 | return UniqueID < Other.UniqueID; |
| 227 | } |
| 228 | }; |
| 229 | |
| 230 | struct WasmSectionKey { |
| 231 | std::string SectionName; |
| 232 | StringRef GroupName; |
| 233 | unsigned UniqueID; |
| 234 | |
| 235 | WasmSectionKey(StringRef SectionName, StringRef GroupName, |
| 236 | unsigned UniqueID) |
| 237 | : SectionName(SectionName), GroupName(GroupName), UniqueID(UniqueID) { |
| 238 | } |
| 239 | |
| 240 | bool operator<(const WasmSectionKey &Other) const { |
| 241 | if (SectionName != Other.SectionName) |
| 242 | return SectionName < Other.SectionName; |
| 243 | if (GroupName != Other.GroupName) |
| 244 | return GroupName < Other.GroupName; |
| 245 | return UniqueID < Other.UniqueID; |
| 246 | } |
| 247 | }; |
| 248 | |
| 249 | StringMap<MCSectionMachO *> MachOUniquingMap; |
| 250 | std::map<ELFSectionKey, MCSectionELF *> ELFUniquingMap; |
| 251 | std::map<COFFSectionKey, MCSectionCOFF *> COFFUniquingMap; |
| 252 | std::map<WasmSectionKey, MCSectionWasm *> WasmUniquingMap; |
| 253 | StringMap<bool> RelSecNames; |
| 254 | |
| 255 | SpecificBumpPtrAllocator<MCSubtargetInfo> MCSubtargetAllocator; |
| 256 | |
| 257 | /// Do automatic reset in destructor |
| 258 | bool AutoReset; |
| 259 | |
| 260 | bool HadError = false; |
| 261 | |
| 262 | MCSymbol *createSymbolImpl(const StringMapEntry<bool> *Name, |
| 263 | bool CanBeUnnamed); |
| 264 | MCSymbol *createSymbol(StringRef Name, bool AlwaysAddSuffix, |
| 265 | bool IsTemporary); |
| 266 | |
| 267 | MCSymbol *getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal, |
| 268 | unsigned Instance); |
| 269 | |
| 270 | MCSectionELF *createELFSectionImpl(StringRef Section, unsigned Type, |
| 271 | unsigned Flags, SectionKind K, |
| 272 | unsigned EntrySize, |
| 273 | const MCSymbolELF *Group, |
| 274 | unsigned UniqueID, |
| 275 | const MCSymbolELF *Associated); |
| 276 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 277 | /// Map of currently defined macros. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 278 | StringMap<MCAsmMacro> MacroMap; |
| 279 | |
| 280 | public: |
| 281 | explicit MCContext(const MCAsmInfo *MAI, const MCRegisterInfo *MRI, |
| 282 | const MCObjectFileInfo *MOFI, |
| 283 | const SourceMgr *Mgr = nullptr, bool DoAutoReset = true); |
| 284 | MCContext(const MCContext &) = delete; |
| 285 | MCContext &operator=(const MCContext &) = delete; |
| 286 | ~MCContext(); |
| 287 | |
| 288 | const SourceMgr *getSourceManager() const { return SrcMgr; } |
| 289 | |
| 290 | void setInlineSourceManager(SourceMgr *SM) { InlineSrcMgr = SM; } |
| 291 | |
| 292 | const MCAsmInfo *getAsmInfo() const { return MAI; } |
| 293 | |
| 294 | const MCRegisterInfo *getRegisterInfo() const { return MRI; } |
| 295 | |
| 296 | const MCObjectFileInfo *getObjectFileInfo() const { return MOFI; } |
| 297 | |
| 298 | CodeViewContext &getCVContext(); |
| 299 | |
| 300 | void setAllowTemporaryLabels(bool Value) { AllowTemporaryLabels = Value; } |
| 301 | void setUseNamesOnTempLabels(bool Value) { UseNamesOnTempLabels = Value; } |
| 302 | |
| 303 | /// \name Module Lifetime Management |
| 304 | /// @{ |
| 305 | |
| 306 | /// reset - return object to right after construction state to prepare |
| 307 | /// to process a new module |
| 308 | void reset(); |
| 309 | |
| 310 | /// @} |
| 311 | |
| 312 | /// \name Symbol Management |
| 313 | /// @{ |
| 314 | |
| 315 | /// Create and return a new linker temporary symbol with a unique but |
| 316 | /// unspecified name. |
| 317 | MCSymbol *createLinkerPrivateTempSymbol(); |
| 318 | |
| 319 | /// Create and return a new assembler temporary symbol with a unique but |
| 320 | /// unspecified name. |
| 321 | MCSymbol *createTempSymbol(bool CanBeUnnamed = true); |
| 322 | |
| 323 | MCSymbol *createTempSymbol(const Twine &Name, bool AlwaysAddSuffix, |
| 324 | bool CanBeUnnamed = true); |
| 325 | |
| 326 | /// Create the definition of a directional local symbol for numbered label |
| 327 | /// (used for "1:" definitions). |
| 328 | MCSymbol *createDirectionalLocalSymbol(unsigned LocalLabelVal); |
| 329 | |
| 330 | /// Create and return a directional local symbol for numbered label (used |
| 331 | /// for "1b" or 1f" references). |
| 332 | MCSymbol *getDirectionalLocalSymbol(unsigned LocalLabelVal, bool Before); |
| 333 | |
| 334 | /// Lookup the symbol inside with the specified \p Name. If it exists, |
| 335 | /// return it. If not, create a forward reference and return it. |
| 336 | /// |
| 337 | /// \param Name - The symbol name, which must be unique across all symbols. |
| 338 | MCSymbol *getOrCreateSymbol(const Twine &Name); |
| 339 | |
| 340 | /// Gets a symbol that will be defined to the final stack offset of a local |
| 341 | /// variable after codegen. |
| 342 | /// |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 343 | /// \param Idx - The index of a local variable passed to \@llvm.localescape. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 344 | MCSymbol *getOrCreateFrameAllocSymbol(StringRef FuncName, unsigned Idx); |
| 345 | |
| 346 | MCSymbol *getOrCreateParentFrameOffsetSymbol(StringRef FuncName); |
| 347 | |
| 348 | MCSymbol *getOrCreateLSDASymbol(StringRef FuncName); |
| 349 | |
| 350 | /// Get the symbol for \p Name, or null. |
| 351 | MCSymbol *lookupSymbol(const Twine &Name) const; |
| 352 | |
| 353 | /// Set value for a symbol. |
| 354 | void setSymbolValue(MCStreamer &Streamer, StringRef Sym, uint64_t Val); |
| 355 | |
| 356 | /// getSymbols - Get a reference for the symbol table for clients that |
| 357 | /// want to, for example, iterate over all symbols. 'const' because we |
| 358 | /// still want any modifications to the table itself to use the MCContext |
| 359 | /// APIs. |
| 360 | const SymbolTable &getSymbols() const { return Symbols; } |
| 361 | |
| 362 | /// @} |
| 363 | |
| 364 | /// \name Section Management |
| 365 | /// @{ |
| 366 | |
| 367 | enum : unsigned { |
| 368 | /// Pass this value as the UniqueID during section creation to get the |
| 369 | /// generic section with the given name and characteristics. The usual |
| 370 | /// sections such as .text use this ID. |
| 371 | GenericSectionID = ~0U |
| 372 | }; |
| 373 | |
| 374 | /// Return the MCSection for the specified mach-o section. This requires |
| 375 | /// the operands to be valid. |
| 376 | MCSectionMachO *getMachOSection(StringRef Segment, StringRef Section, |
| 377 | unsigned TypeAndAttributes, |
| 378 | unsigned Reserved2, SectionKind K, |
| 379 | const char *BeginSymName = nullptr); |
| 380 | |
| 381 | MCSectionMachO *getMachOSection(StringRef Segment, StringRef Section, |
| 382 | unsigned TypeAndAttributes, SectionKind K, |
| 383 | const char *BeginSymName = nullptr) { |
| 384 | return getMachOSection(Segment, Section, TypeAndAttributes, 0, K, |
| 385 | BeginSymName); |
| 386 | } |
| 387 | |
| 388 | MCSectionELF *getELFSection(const Twine &Section, unsigned Type, |
| 389 | unsigned Flags) { |
| 390 | return getELFSection(Section, Type, Flags, 0, ""); |
| 391 | } |
| 392 | |
| 393 | MCSectionELF *getELFSection(const Twine &Section, unsigned Type, |
| 394 | unsigned Flags, unsigned EntrySize, |
| 395 | const Twine &Group) { |
| 396 | return getELFSection(Section, Type, Flags, EntrySize, Group, ~0); |
| 397 | } |
| 398 | |
| 399 | MCSectionELF *getELFSection(const Twine &Section, unsigned Type, |
| 400 | unsigned Flags, unsigned EntrySize, |
| 401 | const Twine &Group, unsigned UniqueID) { |
| 402 | return getELFSection(Section, Type, Flags, EntrySize, Group, UniqueID, |
| 403 | nullptr); |
| 404 | } |
| 405 | |
| 406 | MCSectionELF *getELFSection(const Twine &Section, unsigned Type, |
| 407 | unsigned Flags, unsigned EntrySize, |
| 408 | const Twine &Group, unsigned UniqueID, |
| 409 | const MCSymbolELF *Associated); |
| 410 | |
| 411 | MCSectionELF *getELFSection(const Twine &Section, unsigned Type, |
| 412 | unsigned Flags, unsigned EntrySize, |
| 413 | const MCSymbolELF *Group, unsigned UniqueID, |
| 414 | const MCSymbolELF *Associated); |
| 415 | |
| 416 | /// Get a section with the provided group identifier. This section is |
| 417 | /// named by concatenating \p Prefix with '.' then \p Suffix. The \p Type |
| 418 | /// describes the type of the section and \p Flags are used to further |
| 419 | /// configure this named section. |
| 420 | MCSectionELF *getELFNamedSection(const Twine &Prefix, const Twine &Suffix, |
| 421 | unsigned Type, unsigned Flags, |
| 422 | unsigned EntrySize = 0); |
| 423 | |
| 424 | MCSectionELF *createELFRelSection(const Twine &Name, unsigned Type, |
| 425 | unsigned Flags, unsigned EntrySize, |
| 426 | const MCSymbolELF *Group, |
| 427 | const MCSectionELF *RelInfoSection); |
| 428 | |
| 429 | void renameELFSection(MCSectionELF *Section, StringRef Name); |
| 430 | |
| 431 | MCSectionELF *createELFGroupSection(const MCSymbolELF *Group); |
| 432 | |
| 433 | MCSectionCOFF *getCOFFSection(StringRef Section, unsigned Characteristics, |
| 434 | SectionKind Kind, StringRef COMDATSymName, |
| 435 | int Selection, |
| 436 | unsigned UniqueID = GenericSectionID, |
| 437 | const char *BeginSymName = nullptr); |
| 438 | |
| 439 | MCSectionCOFF *getCOFFSection(StringRef Section, unsigned Characteristics, |
| 440 | SectionKind Kind, |
| 441 | const char *BeginSymName = nullptr); |
| 442 | |
| 443 | MCSectionCOFF *getCOFFSection(StringRef Section); |
| 444 | |
| 445 | /// Gets or creates a section equivalent to Sec that is associated with the |
| 446 | /// section containing KeySym. For example, to create a debug info section |
| 447 | /// associated with an inline function, pass the normal debug info section |
| 448 | /// as Sec and the function symbol as KeySym. |
| 449 | MCSectionCOFF * |
| 450 | getAssociativeCOFFSection(MCSectionCOFF *Sec, const MCSymbol *KeySym, |
| 451 | unsigned UniqueID = GenericSectionID); |
| 452 | |
| 453 | MCSectionWasm *getWasmSection(const Twine &Section, SectionKind K) { |
| 454 | return getWasmSection(Section, K, nullptr); |
| 455 | } |
| 456 | |
| 457 | MCSectionWasm *getWasmSection(const Twine &Section, SectionKind K, |
| 458 | const char *BeginSymName) { |
| 459 | return getWasmSection(Section, K, "", ~0, BeginSymName); |
| 460 | } |
| 461 | |
| 462 | MCSectionWasm *getWasmSection(const Twine &Section, SectionKind K, |
| 463 | const Twine &Group, unsigned UniqueID) { |
| 464 | return getWasmSection(Section, K, Group, UniqueID, nullptr); |
| 465 | } |
| 466 | |
| 467 | MCSectionWasm *getWasmSection(const Twine &Section, SectionKind K, |
| 468 | const Twine &Group, unsigned UniqueID, |
| 469 | const char *BeginSymName); |
| 470 | |
| 471 | MCSectionWasm *getWasmSection(const Twine &Section, SectionKind K, |
| 472 | const MCSymbolWasm *Group, unsigned UniqueID, |
| 473 | const char *BeginSymName); |
| 474 | |
| 475 | // Create and save a copy of STI and return a reference to the copy. |
| 476 | MCSubtargetInfo &getSubtargetCopy(const MCSubtargetInfo &STI); |
| 477 | |
| 478 | /// @} |
| 479 | |
| 480 | /// \name Dwarf Management |
| 481 | /// @{ |
| 482 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 483 | /// Get the compilation directory for DW_AT_comp_dir |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 484 | /// The compilation directory should be set with \c setCompilationDir before |
| 485 | /// calling this function. If it is unset, an empty string will be returned. |
| 486 | StringRef getCompilationDir() const { return CompilationDir; } |
| 487 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 488 | /// Set the compilation directory for DW_AT_comp_dir |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 489 | void setCompilationDir(StringRef S) { CompilationDir = S.str(); } |
| 490 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 491 | /// Get the debug prefix map. |
| 492 | const std::map<const std::string, const std::string> & |
| 493 | getDebugPrefixMap() const { |
| 494 | return DebugPrefixMap; |
| 495 | } |
| 496 | |
| 497 | /// Add an entry to the debug prefix map. |
| 498 | void addDebugPrefixMapEntry(const std::string &From, const std::string &To); |
| 499 | |
| 500 | // Remaps all debug directory paths in-place as per the debug prefix map. |
| 501 | void RemapDebugPaths(); |
| 502 | |
| 503 | /// Get the main file name for use in error messages and debug |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 504 | /// info. This can be set to ensure we've got the correct file name |
| 505 | /// after preprocessing or for -save-temps. |
| 506 | const std::string &getMainFileName() const { return MainFileName; } |
| 507 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 508 | /// Set the main file name and override the default. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 509 | void setMainFileName(StringRef S) { MainFileName = S; } |
| 510 | |
| 511 | /// Creates an entry in the dwarf file and directory tables. |
| 512 | Expected<unsigned> getDwarfFile(StringRef Directory, StringRef FileName, |
| 513 | unsigned FileNumber, |
| 514 | MD5::MD5Result *Checksum, |
| 515 | Optional<StringRef> Source, unsigned CUID); |
| 516 | |
| 517 | bool isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID = 0); |
| 518 | |
| 519 | const std::map<unsigned, MCDwarfLineTable> &getMCDwarfLineTables() const { |
| 520 | return MCDwarfLineTablesCUMap; |
| 521 | } |
| 522 | |
| 523 | MCDwarfLineTable &getMCDwarfLineTable(unsigned CUID) { |
| 524 | return MCDwarfLineTablesCUMap[CUID]; |
| 525 | } |
| 526 | |
| 527 | const MCDwarfLineTable &getMCDwarfLineTable(unsigned CUID) const { |
| 528 | auto I = MCDwarfLineTablesCUMap.find(CUID); |
| 529 | assert(I != MCDwarfLineTablesCUMap.end()); |
| 530 | return I->second; |
| 531 | } |
| 532 | |
| 533 | const SmallVectorImpl<MCDwarfFile> &getMCDwarfFiles(unsigned CUID = 0) { |
| 534 | return getMCDwarfLineTable(CUID).getMCDwarfFiles(); |
| 535 | } |
| 536 | |
| 537 | const SmallVectorImpl<std::string> &getMCDwarfDirs(unsigned CUID = 0) { |
| 538 | return getMCDwarfLineTable(CUID).getMCDwarfDirs(); |
| 539 | } |
| 540 | |
| 541 | bool hasMCLineSections() const { |
| 542 | for (const auto &Table : MCDwarfLineTablesCUMap) |
| 543 | if (!Table.second.getMCDwarfFiles().empty() || Table.second.getLabel()) |
| 544 | return true; |
| 545 | return false; |
| 546 | } |
| 547 | |
| 548 | unsigned getDwarfCompileUnitID() { return DwarfCompileUnitID; } |
| 549 | |
| 550 | void setDwarfCompileUnitID(unsigned CUIndex) { |
| 551 | DwarfCompileUnitID = CUIndex; |
| 552 | } |
| 553 | |
| 554 | /// Specifies the "root" file and directory of the compilation unit. |
| 555 | /// These are "file 0" and "directory 0" in DWARF v5. |
| 556 | void setMCLineTableRootFile(unsigned CUID, StringRef CompilationDir, |
| 557 | StringRef Filename, MD5::MD5Result *Checksum, |
| 558 | Optional<StringRef> Source) { |
| 559 | getMCDwarfLineTable(CUID).setRootFile(CompilationDir, Filename, Checksum, |
| 560 | Source); |
| 561 | } |
| 562 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 563 | /// Reports whether MD5 checksum usage is consistent (all-or-none). |
| 564 | bool isDwarfMD5UsageConsistent(unsigned CUID) const { |
| 565 | return getMCDwarfLineTable(CUID).isMD5UsageConsistent(); |
| 566 | } |
| 567 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 568 | /// Saves the information from the currently parsed dwarf .loc directive |
| 569 | /// and sets DwarfLocSeen. When the next instruction is assembled an entry |
| 570 | /// in the line number table with this information and the address of the |
| 571 | /// instruction will be created. |
| 572 | void setCurrentDwarfLoc(unsigned FileNum, unsigned Line, unsigned Column, |
| 573 | unsigned Flags, unsigned Isa, |
| 574 | unsigned Discriminator) { |
| 575 | CurrentDwarfLoc.setFileNum(FileNum); |
| 576 | CurrentDwarfLoc.setLine(Line); |
| 577 | CurrentDwarfLoc.setColumn(Column); |
| 578 | CurrentDwarfLoc.setFlags(Flags); |
| 579 | CurrentDwarfLoc.setIsa(Isa); |
| 580 | CurrentDwarfLoc.setDiscriminator(Discriminator); |
| 581 | DwarfLocSeen = true; |
| 582 | } |
| 583 | |
| 584 | void clearDwarfLocSeen() { DwarfLocSeen = false; } |
| 585 | |
| 586 | bool getDwarfLocSeen() { return DwarfLocSeen; } |
| 587 | const MCDwarfLoc &getCurrentDwarfLoc() { return CurrentDwarfLoc; } |
| 588 | |
| 589 | bool getGenDwarfForAssembly() { return GenDwarfForAssembly; } |
| 590 | void setGenDwarfForAssembly(bool Value) { GenDwarfForAssembly = Value; } |
| 591 | unsigned getGenDwarfFileNumber() { return GenDwarfFileNumber; } |
| 592 | |
| 593 | void setGenDwarfFileNumber(unsigned FileNumber) { |
| 594 | GenDwarfFileNumber = FileNumber; |
| 595 | } |
| 596 | |
| 597 | const SetVector<MCSection *> &getGenDwarfSectionSyms() { |
| 598 | return SectionsForRanges; |
| 599 | } |
| 600 | |
| 601 | bool addGenDwarfSection(MCSection *Sec) { |
| 602 | return SectionsForRanges.insert(Sec); |
| 603 | } |
| 604 | |
| 605 | void finalizeDwarfSections(MCStreamer &MCOS); |
| 606 | |
| 607 | const std::vector<MCGenDwarfLabelEntry> &getMCGenDwarfLabelEntries() const { |
| 608 | return MCGenDwarfLabelEntries; |
| 609 | } |
| 610 | |
| 611 | void addMCGenDwarfLabelEntry(const MCGenDwarfLabelEntry &E) { |
| 612 | MCGenDwarfLabelEntries.push_back(E); |
| 613 | } |
| 614 | |
| 615 | void setDwarfDebugFlags(StringRef S) { DwarfDebugFlags = S; } |
| 616 | StringRef getDwarfDebugFlags() { return DwarfDebugFlags; } |
| 617 | |
| 618 | void setDwarfDebugProducer(StringRef S) { DwarfDebugProducer = S; } |
| 619 | StringRef getDwarfDebugProducer() { return DwarfDebugProducer; } |
| 620 | |
| 621 | dwarf::DwarfFormat getDwarfFormat() const { |
| 622 | // TODO: Support DWARF64 |
| 623 | return dwarf::DWARF32; |
| 624 | } |
| 625 | |
| 626 | void setDwarfVersion(uint16_t v) { DwarfVersion = v; } |
| 627 | uint16_t getDwarfVersion() const { return DwarfVersion; } |
| 628 | |
| 629 | /// @} |
| 630 | |
| 631 | char *getSecureLogFile() { return SecureLogFile; } |
| 632 | raw_fd_ostream *getSecureLog() { return SecureLog.get(); } |
| 633 | |
| 634 | void setSecureLog(std::unique_ptr<raw_fd_ostream> Value) { |
| 635 | SecureLog = std::move(Value); |
| 636 | } |
| 637 | |
| 638 | bool getSecureLogUsed() { return SecureLogUsed; } |
| 639 | void setSecureLogUsed(bool Value) { SecureLogUsed = Value; } |
| 640 | |
| 641 | void *allocate(unsigned Size, unsigned Align = 8) { |
| 642 | return Allocator.Allocate(Size, Align); |
| 643 | } |
| 644 | |
| 645 | void deallocate(void *Ptr) {} |
| 646 | |
| 647 | bool hadError() { return HadError; } |
| 648 | void reportError(SMLoc L, const Twine &Msg); |
| 649 | // Unrecoverable error has occurred. Display the best diagnostic we can |
| 650 | // and bail via exit(1). For now, most MC backend errors are unrecoverable. |
| 651 | // FIXME: We should really do something about that. |
| 652 | LLVM_ATTRIBUTE_NORETURN void reportFatalError(SMLoc L, |
| 653 | const Twine &Msg); |
| 654 | |
| 655 | const MCAsmMacro *lookupMacro(StringRef Name) { |
| 656 | StringMap<MCAsmMacro>::iterator I = MacroMap.find(Name); |
| 657 | return (I == MacroMap.end()) ? nullptr : &I->getValue(); |
| 658 | } |
| 659 | |
| 660 | void defineMacro(StringRef Name, MCAsmMacro Macro) { |
| 661 | MacroMap.insert(std::make_pair(Name, std::move(Macro))); |
| 662 | } |
| 663 | |
| 664 | void undefineMacro(StringRef Name) { MacroMap.erase(Name); } |
| 665 | }; |
| 666 | |
| 667 | } // end namespace llvm |
| 668 | |
| 669 | // operator new and delete aren't allowed inside namespaces. |
| 670 | // The throw specifications are mandated by the standard. |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 671 | /// Placement new for using the MCContext's allocator. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 672 | /// |
| 673 | /// This placement form of operator new uses the MCContext's allocator for |
| 674 | /// obtaining memory. It is a non-throwing new, which means that it returns |
| 675 | /// null on error. (If that is what the allocator does. The current does, so if |
| 676 | /// this ever changes, this operator will have to be changed, too.) |
| 677 | /// Usage looks like this (assuming there's an MCContext 'Context' in scope): |
| 678 | /// \code |
| 679 | /// // Default alignment (8) |
| 680 | /// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments); |
| 681 | /// // Specific alignment |
| 682 | /// IntegerLiteral *Ex2 = new (Context, 4) IntegerLiteral(arguments); |
| 683 | /// \endcode |
| 684 | /// Please note that you cannot use delete on the pointer; it must be |
| 685 | /// deallocated using an explicit destructor call followed by |
| 686 | /// \c Context.Deallocate(Ptr). |
| 687 | /// |
| 688 | /// \param Bytes The number of bytes to allocate. Calculated by the compiler. |
| 689 | /// \param C The MCContext that provides the allocator. |
| 690 | /// \param Alignment The alignment of the allocated memory (if the underlying |
| 691 | /// allocator supports it). |
| 692 | /// \return The allocated memory. Could be NULL. |
| 693 | inline void *operator new(size_t Bytes, llvm::MCContext &C, |
| 694 | size_t Alignment = 8) noexcept { |
| 695 | return C.allocate(Bytes, Alignment); |
| 696 | } |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 697 | /// Placement delete companion to the new above. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 698 | /// |
| 699 | /// This operator is just a companion to the new above. There is no way of |
| 700 | /// invoking it directly; see the new operator for more details. This operator |
| 701 | /// is called implicitly by the compiler if a placement new expression using |
| 702 | /// the MCContext throws in the object constructor. |
| 703 | inline void operator delete(void *Ptr, llvm::MCContext &C, size_t) noexcept { |
| 704 | C.deallocate(Ptr); |
| 705 | } |
| 706 | |
| 707 | /// This placement form of operator new[] uses the MCContext's allocator for |
| 708 | /// obtaining memory. It is a non-throwing new[], which means that it returns |
| 709 | /// null on error. |
| 710 | /// Usage looks like this (assuming there's an MCContext 'Context' in scope): |
| 711 | /// \code |
| 712 | /// // Default alignment (8) |
| 713 | /// char *data = new (Context) char[10]; |
| 714 | /// // Specific alignment |
| 715 | /// char *data = new (Context, 4) char[10]; |
| 716 | /// \endcode |
| 717 | /// Please note that you cannot use delete on the pointer; it must be |
| 718 | /// deallocated using an explicit destructor call followed by |
| 719 | /// \c Context.Deallocate(Ptr). |
| 720 | /// |
| 721 | /// \param Bytes The number of bytes to allocate. Calculated by the compiler. |
| 722 | /// \param C The MCContext that provides the allocator. |
| 723 | /// \param Alignment The alignment of the allocated memory (if the underlying |
| 724 | /// allocator supports it). |
| 725 | /// \return The allocated memory. Could be NULL. |
| 726 | inline void *operator new[](size_t Bytes, llvm::MCContext &C, |
| 727 | size_t Alignment = 8) noexcept { |
| 728 | return C.allocate(Bytes, Alignment); |
| 729 | } |
| 730 | |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 731 | /// Placement delete[] companion to the new[] above. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 732 | /// |
| 733 | /// This operator is just a companion to the new[] above. There is no way of |
| 734 | /// invoking it directly; see the new[] operator for more details. This operator |
| 735 | /// is called implicitly by the compiler if a placement new[] expression using |
| 736 | /// the MCContext throws in the object constructor. |
| 737 | inline void operator delete[](void *Ptr, llvm::MCContext &C) noexcept { |
| 738 | C.deallocate(Ptr); |
| 739 | } |
| 740 | |
| 741 | #endif // LLVM_MC_MCCONTEXT_H |