1 //===- MCContext.h - Machine Code Context -----------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_MC_MCCONTEXT_H
10 #define LLVM_MC_MCCONTEXT_H
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/BinaryFormat/XCOFF.h"
22 #include "llvm/MC/MCAsmMacro.h"
23 #include "llvm/MC/MCDwarf.h"
24 #include "llvm/MC/MCSubtargetInfo.h"
25 #include "llvm/MC/SectionKind.h"
26 #include "llvm/Support/Allocator.h"
27 #include "llvm/Support/Compiler.h"
28 #include "llvm/Support/Error.h"
29 #include "llvm/Support/MD5.h"
30 #include "llvm/Support/raw_ostream.h"
43 class CodeViewContext;
46 class MCObjectFileInfo;
61 /// Context object for machine code objects. This class owns all of the
62 /// sections that it creates.
66 using SymbolTable = StringMap<MCSymbol *, BumpPtrAllocator &>;
69 /// The SourceMgr for this object, if any.
70 const SourceMgr *SrcMgr;
72 /// The SourceMgr for inline assembly, if any.
73 SourceMgr *InlineSrcMgr;
75 /// The MCAsmInfo for this target.
78 /// The MCRegisterInfo for this target.
79 const MCRegisterInfo *MRI;
81 /// The MCObjectFileInfo for this target.
82 const MCObjectFileInfo *MOFI;
84 std::unique_ptr<CodeViewContext> CVContext;
86 /// Allocator object used for creating machine code objects.
88 /// We use a bump pointer allocator to avoid the need to track all allocated
90 BumpPtrAllocator Allocator;
92 SpecificBumpPtrAllocator<MCSectionCOFF> COFFAllocator;
93 SpecificBumpPtrAllocator<MCSectionELF> ELFAllocator;
94 SpecificBumpPtrAllocator<MCSectionMachO> MachOAllocator;
95 SpecificBumpPtrAllocator<MCSectionWasm> WasmAllocator;
96 SpecificBumpPtrAllocator<MCSectionXCOFF> XCOFFAllocator;
98 /// Bindings of names to symbols.
101 /// A mapping from a local label number and an instance count to a symbol.
102 /// For example, in the assembly
106 /// We have three labels represented by the pairs (1, 0), (2, 0) and (1, 1)
107 DenseMap<std::pair<unsigned, unsigned>, MCSymbol *> LocalSymbols;
109 /// Keeps tracks of names that were used both for used declared and
110 /// artificial symbols. The value is "true" if the name has been used for a
111 /// non-section symbol (there can be at most one of those, plus an unlimited
112 /// number of section symbols with the same name).
113 StringMap<bool, BumpPtrAllocator &> UsedNames;
115 /// Keeps track of labels that are used in inline assembly.
116 SymbolTable InlineAsmUsedLabelNames;
118 /// The next ID to dole out to an unnamed assembler temporary symbol with
120 StringMap<unsigned> NextID;
122 /// Instances of directional local labels.
123 DenseMap<unsigned, MCLabel *> Instances;
124 /// NextInstance() creates the next instance of the directional local label
125 /// for the LocalLabelVal and adds it to the map if needed.
126 unsigned NextInstance(unsigned LocalLabelVal);
127 /// GetInstance() gets the current instance of the directional local label
128 /// for the LocalLabelVal and adds it to the map if needed.
129 unsigned GetInstance(unsigned LocalLabelVal);
131 /// The file name of the log file from the environment variable
132 /// AS_SECURE_LOG_FILE. Which must be set before the .secure_log_unique
133 /// directive is used or it is an error.
135 /// The stream that gets written to for the .secure_log_unique directive.
136 std::unique_ptr<raw_fd_ostream> SecureLog;
137 /// Boolean toggled when .secure_log_unique / .secure_log_reset is seen to
138 /// catch errors if .secure_log_unique appears twice without
139 /// .secure_log_reset appearing between them.
140 bool SecureLogUsed = false;
142 /// The compilation directory to use for DW_AT_comp_dir.
143 SmallString<128> CompilationDir;
145 /// Prefix replacement map for source file information.
146 std::map<const std::string, const std::string> DebugPrefixMap;
148 /// The main file name if passed in explicitly.
149 std::string MainFileName;
151 /// The dwarf file and directory tables from the dwarf .file directive.
152 /// We now emit a line table for each compile unit. To reduce the prologue
153 /// size of each line table, the files and directories used by each compile
154 /// unit are separated.
155 std::map<unsigned, MCDwarfLineTable> MCDwarfLineTablesCUMap;
157 /// The current dwarf line information from the last dwarf .loc directive.
158 MCDwarfLoc CurrentDwarfLoc;
159 bool DwarfLocSeen = false;
161 /// Generate dwarf debugging info for assembly source files.
162 bool GenDwarfForAssembly = false;
164 /// The current dwarf file number when generate dwarf debugging info for
165 /// assembly source files.
166 unsigned GenDwarfFileNumber = 0;
168 /// Sections for generating the .debug_ranges and .debug_aranges sections.
169 SetVector<MCSection *> SectionsForRanges;
171 /// The information gathered from labels that will have dwarf label
172 /// entries when generating dwarf assembly source files.
173 std::vector<MCGenDwarfLabelEntry> MCGenDwarfLabelEntries;
175 /// The string to embed in the debug information for the compile unit, if
177 StringRef DwarfDebugFlags;
179 /// The string to embed in as the dwarf AT_producer for the compile unit, if
181 StringRef DwarfDebugProducer;
183 /// The maximum version of dwarf that we should emit.
184 uint16_t DwarfVersion = 4;
186 /// Honor temporary labels, this is useful for debugging semantic
187 /// differences between temporary and non-temporary labels (primarily on
189 bool AllowTemporaryLabels = true;
190 bool UseNamesOnTempLabels = true;
192 /// The Compile Unit ID that we are currently processing.
193 unsigned DwarfCompileUnitID = 0;
195 struct ELFSectionKey {
196 std::string SectionName;
200 ELFSectionKey(StringRef SectionName, StringRef GroupName,
202 : SectionName(SectionName), GroupName(GroupName), UniqueID(UniqueID) {
205 bool operator<(const ELFSectionKey &Other) const {
206 if (SectionName != Other.SectionName)
207 return SectionName < Other.SectionName;
208 if (GroupName != Other.GroupName)
209 return GroupName < Other.GroupName;
210 return UniqueID < Other.UniqueID;
214 struct COFFSectionKey {
215 std::string SectionName;
220 COFFSectionKey(StringRef SectionName, StringRef GroupName,
221 int SelectionKey, unsigned UniqueID)
222 : SectionName(SectionName), GroupName(GroupName),
223 SelectionKey(SelectionKey), UniqueID(UniqueID) {}
225 bool operator<(const COFFSectionKey &Other) const {
226 if (SectionName != Other.SectionName)
227 return SectionName < Other.SectionName;
228 if (GroupName != Other.GroupName)
229 return GroupName < Other.GroupName;
230 if (SelectionKey != Other.SelectionKey)
231 return SelectionKey < Other.SelectionKey;
232 return UniqueID < Other.UniqueID;
236 struct WasmSectionKey {
237 std::string SectionName;
241 WasmSectionKey(StringRef SectionName, StringRef GroupName,
243 : SectionName(SectionName), GroupName(GroupName), UniqueID(UniqueID) {
246 bool operator<(const WasmSectionKey &Other) const {
247 if (SectionName != Other.SectionName)
248 return SectionName < Other.SectionName;
249 if (GroupName != Other.GroupName)
250 return GroupName < Other.GroupName;
251 return UniqueID < Other.UniqueID;
255 struct XCOFFSectionKey {
256 std::string SectionName;
257 XCOFF::StorageMappingClass MappingClass;
259 XCOFFSectionKey(StringRef SectionName,
260 XCOFF::StorageMappingClass MappingClass)
261 : SectionName(SectionName), MappingClass(MappingClass) {}
263 bool operator<(const XCOFFSectionKey &Other) const {
264 return std::tie(SectionName, MappingClass) <
265 std::tie(Other.SectionName, Other.MappingClass);
269 StringMap<MCSectionMachO *> MachOUniquingMap;
270 std::map<ELFSectionKey, MCSectionELF *> ELFUniquingMap;
271 std::map<COFFSectionKey, MCSectionCOFF *> COFFUniquingMap;
272 std::map<WasmSectionKey, MCSectionWasm *> WasmUniquingMap;
273 std::map<XCOFFSectionKey, MCSectionXCOFF *> XCOFFUniquingMap;
274 StringMap<bool> RelSecNames;
276 SpecificBumpPtrAllocator<MCSubtargetInfo> MCSubtargetAllocator;
278 /// Do automatic reset in destructor
281 bool HadError = false;
283 MCSymbol *createSymbolImpl(const StringMapEntry<bool> *Name,
285 MCSymbol *createSymbol(StringRef Name, bool AlwaysAddSuffix,
288 MCSymbol *getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal,
291 MCSectionELF *createELFSectionImpl(StringRef Section, unsigned Type,
292 unsigned Flags, SectionKind K,
294 const MCSymbolELF *Group,
296 const MCSymbolELF *Associated);
298 /// Map of currently defined macros.
299 StringMap<MCAsmMacro> MacroMap;
302 explicit MCContext(const MCAsmInfo *MAI, const MCRegisterInfo *MRI,
303 const MCObjectFileInfo *MOFI,
304 const SourceMgr *Mgr = nullptr, bool DoAutoReset = true);
305 MCContext(const MCContext &) = delete;
306 MCContext &operator=(const MCContext &) = delete;
309 const SourceMgr *getSourceManager() const { return SrcMgr; }
311 void setInlineSourceManager(SourceMgr *SM) { InlineSrcMgr = SM; }
313 const MCAsmInfo *getAsmInfo() const { return MAI; }
315 const MCRegisterInfo *getRegisterInfo() const { return MRI; }
317 const MCObjectFileInfo *getObjectFileInfo() const { return MOFI; }
319 CodeViewContext &getCVContext();
321 void setAllowTemporaryLabels(bool Value) { AllowTemporaryLabels = Value; }
322 void setUseNamesOnTempLabels(bool Value) { UseNamesOnTempLabels = Value; }
324 /// \name Module Lifetime Management
327 /// reset - return object to right after construction state to prepare
328 /// to process a new module
333 /// \name Symbol Management
336 /// Create and return a new linker temporary symbol with a unique but
337 /// unspecified name.
338 MCSymbol *createLinkerPrivateTempSymbol();
340 /// Create and return a new assembler temporary symbol with a unique but
341 /// unspecified name.
342 MCSymbol *createTempSymbol(bool CanBeUnnamed = true);
344 MCSymbol *createTempSymbol(const Twine &Name, bool AlwaysAddSuffix,
345 bool CanBeUnnamed = true);
347 /// Create the definition of a directional local symbol for numbered label
348 /// (used for "1:" definitions).
349 MCSymbol *createDirectionalLocalSymbol(unsigned LocalLabelVal);
351 /// Create and return a directional local symbol for numbered label (used
352 /// for "1b" or 1f" references).
353 MCSymbol *getDirectionalLocalSymbol(unsigned LocalLabelVal, bool Before);
355 /// Lookup the symbol inside with the specified \p Name. If it exists,
356 /// return it. If not, create a forward reference and return it.
358 /// \param Name - The symbol name, which must be unique across all symbols.
359 MCSymbol *getOrCreateSymbol(const Twine &Name);
361 /// Gets a symbol that will be defined to the final stack offset of a local
362 /// variable after codegen.
364 /// \param Idx - The index of a local variable passed to \@llvm.localescape.
365 MCSymbol *getOrCreateFrameAllocSymbol(StringRef FuncName, unsigned Idx);
367 MCSymbol *getOrCreateParentFrameOffsetSymbol(StringRef FuncName);
369 MCSymbol *getOrCreateLSDASymbol(StringRef FuncName);
371 /// Get the symbol for \p Name, or null.
372 MCSymbol *lookupSymbol(const Twine &Name) const;
374 /// Set value for a symbol.
375 void setSymbolValue(MCStreamer &Streamer, StringRef Sym, uint64_t Val);
377 /// getSymbols - Get a reference for the symbol table for clients that
378 /// want to, for example, iterate over all symbols. 'const' because we
379 /// still want any modifications to the table itself to use the MCContext
381 const SymbolTable &getSymbols() const { return Symbols; }
383 /// isInlineAsmLabel - Return true if the name is a label referenced in
385 MCSymbol *getInlineAsmLabel(StringRef Name) const {
386 return InlineAsmUsedLabelNames.lookup(Name);
389 /// registerInlineAsmLabel - Records that the name is a label referenced in
391 void registerInlineAsmLabel(MCSymbol *Sym);
395 /// \name Section Management
399 /// Pass this value as the UniqueID during section creation to get the
400 /// generic section with the given name and characteristics. The usual
401 /// sections such as .text use this ID.
402 GenericSectionID = ~0U
405 /// Return the MCSection for the specified mach-o section. This requires
406 /// the operands to be valid.
407 MCSectionMachO *getMachOSection(StringRef Segment, StringRef Section,
408 unsigned TypeAndAttributes,
409 unsigned Reserved2, SectionKind K,
410 const char *BeginSymName = nullptr);
412 MCSectionMachO *getMachOSection(StringRef Segment, StringRef Section,
413 unsigned TypeAndAttributes, SectionKind K,
414 const char *BeginSymName = nullptr) {
415 return getMachOSection(Segment, Section, TypeAndAttributes, 0, K,
419 MCSectionELF *getELFSection(const Twine &Section, unsigned Type,
421 return getELFSection(Section, Type, Flags, 0, "");
424 MCSectionELF *getELFSection(const Twine &Section, unsigned Type,
425 unsigned Flags, unsigned EntrySize,
426 const Twine &Group) {
427 return getELFSection(Section, Type, Flags, EntrySize, Group, ~0);
430 MCSectionELF *getELFSection(const Twine &Section, unsigned Type,
431 unsigned Flags, unsigned EntrySize,
432 const Twine &Group, unsigned UniqueID) {
433 return getELFSection(Section, Type, Flags, EntrySize, Group, UniqueID,
437 MCSectionELF *getELFSection(const Twine &Section, unsigned Type,
438 unsigned Flags, unsigned EntrySize,
439 const Twine &Group, unsigned UniqueID,
440 const MCSymbolELF *Associated);
442 MCSectionELF *getELFSection(const Twine &Section, unsigned Type,
443 unsigned Flags, unsigned EntrySize,
444 const MCSymbolELF *Group, unsigned UniqueID,
445 const MCSymbolELF *Associated);
447 /// Get a section with the provided group identifier. This section is
448 /// named by concatenating \p Prefix with '.' then \p Suffix. The \p Type
449 /// describes the type of the section and \p Flags are used to further
450 /// configure this named section.
451 MCSectionELF *getELFNamedSection(const Twine &Prefix, const Twine &Suffix,
452 unsigned Type, unsigned Flags,
453 unsigned EntrySize = 0);
455 MCSectionELF *createELFRelSection(const Twine &Name, unsigned Type,
456 unsigned Flags, unsigned EntrySize,
457 const MCSymbolELF *Group,
458 const MCSectionELF *RelInfoSection);
460 void renameELFSection(MCSectionELF *Section, StringRef Name);
462 MCSectionELF *createELFGroupSection(const MCSymbolELF *Group);
464 MCSectionCOFF *getCOFFSection(StringRef Section, unsigned Characteristics,
465 SectionKind Kind, StringRef COMDATSymName,
467 unsigned UniqueID = GenericSectionID,
468 const char *BeginSymName = nullptr);
470 MCSectionCOFF *getCOFFSection(StringRef Section, unsigned Characteristics,
472 const char *BeginSymName = nullptr);
474 /// Gets or creates a section equivalent to Sec that is associated with the
475 /// section containing KeySym. For example, to create a debug info section
476 /// associated with an inline function, pass the normal debug info section
477 /// as Sec and the function symbol as KeySym.
479 getAssociativeCOFFSection(MCSectionCOFF *Sec, const MCSymbol *KeySym,
480 unsigned UniqueID = GenericSectionID);
482 MCSectionWasm *getWasmSection(const Twine &Section, SectionKind K) {
483 return getWasmSection(Section, K, nullptr);
486 MCSectionWasm *getWasmSection(const Twine &Section, SectionKind K,
487 const char *BeginSymName) {
488 return getWasmSection(Section, K, "", ~0, BeginSymName);
491 MCSectionWasm *getWasmSection(const Twine &Section, SectionKind K,
492 const Twine &Group, unsigned UniqueID) {
493 return getWasmSection(Section, K, Group, UniqueID, nullptr);
496 MCSectionWasm *getWasmSection(const Twine &Section, SectionKind K,
497 const Twine &Group, unsigned UniqueID,
498 const char *BeginSymName);
500 MCSectionWasm *getWasmSection(const Twine &Section, SectionKind K,
501 const MCSymbolWasm *Group, unsigned UniqueID,
502 const char *BeginSymName);
504 MCSectionXCOFF *getXCOFFSection(StringRef Section,
505 XCOFF::StorageMappingClass MappingClass,
507 const char *BeginSymName = nullptr);
509 // Create and save a copy of STI and return a reference to the copy.
510 MCSubtargetInfo &getSubtargetCopy(const MCSubtargetInfo &STI);
514 /// \name Dwarf Management
517 /// Get the compilation directory for DW_AT_comp_dir
518 /// The compilation directory should be set with \c setCompilationDir before
519 /// calling this function. If it is unset, an empty string will be returned.
520 StringRef getCompilationDir() const { return CompilationDir; }
522 /// Set the compilation directory for DW_AT_comp_dir
523 void setCompilationDir(StringRef S) { CompilationDir = S.str(); }
525 /// Add an entry to the debug prefix map.
526 void addDebugPrefixMapEntry(const std::string &From, const std::string &To);
528 // Remaps all debug directory paths in-place as per the debug prefix map.
529 void RemapDebugPaths();
531 /// Get the main file name for use in error messages and debug
532 /// info. This can be set to ensure we've got the correct file name
533 /// after preprocessing or for -save-temps.
534 const std::string &getMainFileName() const { return MainFileName; }
536 /// Set the main file name and override the default.
537 void setMainFileName(StringRef S) { MainFileName = S; }
539 /// Creates an entry in the dwarf file and directory tables.
540 Expected<unsigned> getDwarfFile(StringRef Directory, StringRef FileName,
542 Optional<MD5::MD5Result> Checksum,
543 Optional<StringRef> Source, unsigned CUID);
545 bool isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID = 0);
547 const std::map<unsigned, MCDwarfLineTable> &getMCDwarfLineTables() const {
548 return MCDwarfLineTablesCUMap;
551 MCDwarfLineTable &getMCDwarfLineTable(unsigned CUID) {
552 return MCDwarfLineTablesCUMap[CUID];
555 const MCDwarfLineTable &getMCDwarfLineTable(unsigned CUID) const {
556 auto I = MCDwarfLineTablesCUMap.find(CUID);
557 assert(I != MCDwarfLineTablesCUMap.end());
561 const SmallVectorImpl<MCDwarfFile> &getMCDwarfFiles(unsigned CUID = 0) {
562 return getMCDwarfLineTable(CUID).getMCDwarfFiles();
565 const SmallVectorImpl<std::string> &getMCDwarfDirs(unsigned CUID = 0) {
566 return getMCDwarfLineTable(CUID).getMCDwarfDirs();
569 unsigned getDwarfCompileUnitID() { return DwarfCompileUnitID; }
571 void setDwarfCompileUnitID(unsigned CUIndex) {
572 DwarfCompileUnitID = CUIndex;
575 /// Specifies the "root" file and directory of the compilation unit.
576 /// These are "file 0" and "directory 0" in DWARF v5.
577 void setMCLineTableRootFile(unsigned CUID, StringRef CompilationDir,
579 Optional<MD5::MD5Result> Checksum,
580 Optional<StringRef> Source) {
581 getMCDwarfLineTable(CUID).setRootFile(CompilationDir, Filename, Checksum,
585 /// Reports whether MD5 checksum usage is consistent (all-or-none).
586 bool isDwarfMD5UsageConsistent(unsigned CUID) const {
587 return getMCDwarfLineTable(CUID).isMD5UsageConsistent();
590 /// Saves the information from the currently parsed dwarf .loc directive
591 /// and sets DwarfLocSeen. When the next instruction is assembled an entry
592 /// in the line number table with this information and the address of the
593 /// instruction will be created.
594 void setCurrentDwarfLoc(unsigned FileNum, unsigned Line, unsigned Column,
595 unsigned Flags, unsigned Isa,
596 unsigned Discriminator) {
597 CurrentDwarfLoc.setFileNum(FileNum);
598 CurrentDwarfLoc.setLine(Line);
599 CurrentDwarfLoc.setColumn(Column);
600 CurrentDwarfLoc.setFlags(Flags);
601 CurrentDwarfLoc.setIsa(Isa);
602 CurrentDwarfLoc.setDiscriminator(Discriminator);
606 void clearDwarfLocSeen() { DwarfLocSeen = false; }
608 bool getDwarfLocSeen() { return DwarfLocSeen; }
609 const MCDwarfLoc &getCurrentDwarfLoc() { return CurrentDwarfLoc; }
611 bool getGenDwarfForAssembly() { return GenDwarfForAssembly; }
612 void setGenDwarfForAssembly(bool Value) { GenDwarfForAssembly = Value; }
613 unsigned getGenDwarfFileNumber() { return GenDwarfFileNumber; }
615 void setGenDwarfFileNumber(unsigned FileNumber) {
616 GenDwarfFileNumber = FileNumber;
619 /// Specifies information about the "root file" for assembler clients
620 /// (e.g., llvm-mc). Assumes compilation dir etc. have been set up.
621 void setGenDwarfRootFile(StringRef FileName, StringRef Buffer);
623 const SetVector<MCSection *> &getGenDwarfSectionSyms() {
624 return SectionsForRanges;
627 bool addGenDwarfSection(MCSection *Sec) {
628 return SectionsForRanges.insert(Sec);
631 void finalizeDwarfSections(MCStreamer &MCOS);
633 const std::vector<MCGenDwarfLabelEntry> &getMCGenDwarfLabelEntries() const {
634 return MCGenDwarfLabelEntries;
637 void addMCGenDwarfLabelEntry(const MCGenDwarfLabelEntry &E) {
638 MCGenDwarfLabelEntries.push_back(E);
641 void setDwarfDebugFlags(StringRef S) { DwarfDebugFlags = S; }
642 StringRef getDwarfDebugFlags() { return DwarfDebugFlags; }
644 void setDwarfDebugProducer(StringRef S) { DwarfDebugProducer = S; }
645 StringRef getDwarfDebugProducer() { return DwarfDebugProducer; }
647 dwarf::DwarfFormat getDwarfFormat() const {
648 // TODO: Support DWARF64
649 return dwarf::DWARF32;
652 void setDwarfVersion(uint16_t v) { DwarfVersion = v; }
653 uint16_t getDwarfVersion() const { return DwarfVersion; }
657 char *getSecureLogFile() { return SecureLogFile; }
658 raw_fd_ostream *getSecureLog() { return SecureLog.get(); }
660 void setSecureLog(std::unique_ptr<raw_fd_ostream> Value) {
661 SecureLog = std::move(Value);
664 bool getSecureLogUsed() { return SecureLogUsed; }
665 void setSecureLogUsed(bool Value) { SecureLogUsed = Value; }
667 void *allocate(unsigned Size, unsigned Align = 8) {
668 return Allocator.Allocate(Size, Align);
671 void deallocate(void *Ptr) {}
673 bool hadError() { return HadError; }
674 void reportError(SMLoc L, const Twine &Msg);
675 // Unrecoverable error has occurred. Display the best diagnostic we can
676 // and bail via exit(1). For now, most MC backend errors are unrecoverable.
677 // FIXME: We should really do something about that.
678 LLVM_ATTRIBUTE_NORETURN void reportFatalError(SMLoc L,
681 const MCAsmMacro *lookupMacro(StringRef Name) {
682 StringMap<MCAsmMacro>::iterator I = MacroMap.find(Name);
683 return (I == MacroMap.end()) ? nullptr : &I->getValue();
686 void defineMacro(StringRef Name, MCAsmMacro Macro) {
687 MacroMap.insert(std::make_pair(Name, std::move(Macro)));
690 void undefineMacro(StringRef Name) { MacroMap.erase(Name); }
693 } // end namespace llvm
695 // operator new and delete aren't allowed inside namespaces.
696 // The throw specifications are mandated by the standard.
697 /// Placement new for using the MCContext's allocator.
699 /// This placement form of operator new uses the MCContext's allocator for
700 /// obtaining memory. It is a non-throwing new, which means that it returns
701 /// null on error. (If that is what the allocator does. The current does, so if
702 /// this ever changes, this operator will have to be changed, too.)
703 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
705 /// // Default alignment (8)
706 /// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
707 /// // Specific alignment
708 /// IntegerLiteral *Ex2 = new (Context, 4) IntegerLiteral(arguments);
710 /// Please note that you cannot use delete on the pointer; it must be
711 /// deallocated using an explicit destructor call followed by
712 /// \c Context.Deallocate(Ptr).
714 /// \param Bytes The number of bytes to allocate. Calculated by the compiler.
715 /// \param C The MCContext that provides the allocator.
716 /// \param Alignment The alignment of the allocated memory (if the underlying
717 /// allocator supports it).
718 /// \return The allocated memory. Could be NULL.
719 inline void *operator new(size_t Bytes, llvm::MCContext &C,
720 size_t Alignment = 8) noexcept {
721 return C.allocate(Bytes, Alignment);
723 /// Placement delete companion to the new above.
725 /// This operator is just a companion to the new above. There is no way of
726 /// invoking it directly; see the new operator for more details. This operator
727 /// is called implicitly by the compiler if a placement new expression using
728 /// the MCContext throws in the object constructor.
729 inline void operator delete(void *Ptr, llvm::MCContext &C, size_t) noexcept {
733 /// This placement form of operator new[] uses the MCContext's allocator for
734 /// obtaining memory. It is a non-throwing new[], which means that it returns
736 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
738 /// // Default alignment (8)
739 /// char *data = new (Context) char[10];
740 /// // Specific alignment
741 /// char *data = new (Context, 4) char[10];
743 /// Please note that you cannot use delete on the pointer; it must be
744 /// deallocated using an explicit destructor call followed by
745 /// \c Context.Deallocate(Ptr).
747 /// \param Bytes The number of bytes to allocate. Calculated by the compiler.
748 /// \param C The MCContext that provides the allocator.
749 /// \param Alignment The alignment of the allocated memory (if the underlying
750 /// allocator supports it).
751 /// \return The allocated memory. Could be NULL.
752 inline void *operator new[](size_t Bytes, llvm::MCContext &C,
753 size_t Alignment = 8) noexcept {
754 return C.allocate(Bytes, Alignment);
757 /// Placement delete[] companion to the new[] above.
759 /// This operator is just a companion to the new[] above. There is no way of
760 /// invoking it directly; see the new[] operator for more details. This operator
761 /// is called implicitly by the compiler if a placement new[] expression using
762 /// the MCContext throws in the object constructor.
763 inline void operator delete[](void *Ptr, llvm::MCContext &C) noexcept {
767 #endif // LLVM_MC_MCCONTEXT_H