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