]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/MC/MCContext.h
Merge commit f596f4507 from llvm git (by Sam Elliott):
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / MC / MCContext.h
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"
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"
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:
66     using SymbolTable = StringMap<MCSymbol *, BumpPtrAllocator &>;
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
92     SpecificBumpPtrAllocator<MCSectionCOFF> COFFAllocator;
93     SpecificBumpPtrAllocator<MCSectionELF> ELFAllocator;
94     SpecificBumpPtrAllocator<MCSectionMachO> MachOAllocator;
95     SpecificBumpPtrAllocator<MCSectionWasm> WasmAllocator;
96     SpecificBumpPtrAllocator<MCSectionXCOFF> XCOFFAllocator;
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)
107     DenseMap<std::pair<unsigned, unsigned>, MCSymbol *> LocalSymbols;
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).
113     StringMap<bool, BumpPtrAllocator &> UsedNames;
114
115     /// Keeps track of labels that are used in inline assembly.
116     SymbolTable InlineAsmUsedLabelNames;
117
118     /// The next ID to dole out to an unnamed assembler temporary symbol with
119     /// a given prefix.
120     StringMap<unsigned> NextID;
121
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);
130
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.
134     char *SecureLogFile;
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;
141
142     /// The compilation directory to use for DW_AT_comp_dir.
143     SmallString<128> CompilationDir;
144
145     /// Prefix replacement map for source file information.
146     std::map<const std::string, const std::string> DebugPrefixMap;
147
148     /// The main file name if passed in explicitly.
149     std::string MainFileName;
150
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;
156
157     /// The current dwarf line information from the last dwarf .loc directive.
158     MCDwarfLoc CurrentDwarfLoc;
159     bool DwarfLocSeen = false;
160
161     /// Generate dwarf debugging info for assembly source files.
162     bool GenDwarfForAssembly = false;
163
164     /// The current dwarf file number when generate dwarf debugging info for
165     /// assembly source files.
166     unsigned GenDwarfFileNumber = 0;
167
168     /// Sections for generating the .debug_ranges and .debug_aranges sections.
169     SetVector<MCSection *> SectionsForRanges;
170
171     /// The information gathered from labels that will have dwarf label
172     /// entries when generating dwarf assembly source files.
173     std::vector<MCGenDwarfLabelEntry> MCGenDwarfLabelEntries;
174
175     /// The string to embed in the debug information for the compile unit, if
176     /// non-empty.
177     StringRef DwarfDebugFlags;
178
179     /// The string to embed in as the dwarf AT_producer for the compile unit, if
180     /// non-empty.
181     StringRef DwarfDebugProducer;
182
183     /// The maximum version of dwarf that we should emit.
184     uint16_t DwarfVersion = 4;
185
186     /// Honor temporary labels, this is useful for debugging semantic
187     /// differences between temporary and non-temporary labels (primarily on
188     /// Darwin).
189     bool AllowTemporaryLabels = true;
190     bool UseNamesOnTempLabels = true;
191
192     /// The Compile Unit ID that we are currently processing.
193     unsigned DwarfCompileUnitID = 0;
194
195     struct ELFSectionKey {
196       std::string SectionName;
197       StringRef GroupName;
198       unsigned UniqueID;
199
200       ELFSectionKey(StringRef SectionName, StringRef GroupName,
201                     unsigned UniqueID)
202           : SectionName(SectionName), GroupName(GroupName), UniqueID(UniqueID) {
203       }
204
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;
211       }
212     };
213
214     struct COFFSectionKey {
215       std::string SectionName;
216       StringRef GroupName;
217       int SelectionKey;
218       unsigned UniqueID;
219
220       COFFSectionKey(StringRef SectionName, StringRef GroupName,
221                      int SelectionKey, unsigned UniqueID)
222           : SectionName(SectionName), GroupName(GroupName),
223             SelectionKey(SelectionKey), UniqueID(UniqueID) {}
224
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;
233       }
234     };
235
236     struct WasmSectionKey {
237       std::string SectionName;
238       StringRef GroupName;
239       unsigned UniqueID;
240
241       WasmSectionKey(StringRef SectionName, StringRef GroupName,
242                      unsigned UniqueID)
243           : SectionName(SectionName), GroupName(GroupName), UniqueID(UniqueID) {
244       }
245
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;
252       }
253     };
254
255     struct XCOFFSectionKey {
256       std::string SectionName;
257       XCOFF::StorageMappingClass MappingClass;
258
259       XCOFFSectionKey(StringRef SectionName,
260                       XCOFF::StorageMappingClass MappingClass)
261           : SectionName(SectionName), MappingClass(MappingClass) {}
262
263       bool operator<(const XCOFFSectionKey &Other) const {
264         return std::tie(SectionName, MappingClass) <
265                std::tie(Other.SectionName, Other.MappingClass);
266       }
267     };
268
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;
275
276     SpecificBumpPtrAllocator<MCSubtargetInfo> MCSubtargetAllocator;
277
278     /// Do automatic reset in destructor
279     bool AutoReset;
280
281     bool HadError = false;
282
283     MCSymbol *createSymbolImpl(const StringMapEntry<bool> *Name,
284                                bool CanBeUnnamed);
285     MCSymbol *createSymbol(StringRef Name, bool AlwaysAddSuffix,
286                            bool IsTemporary);
287
288     MCSymbol *getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal,
289                                                 unsigned Instance);
290
291     MCSectionELF *createELFSectionImpl(StringRef Section, unsigned Type,
292                                        unsigned Flags, SectionKind K,
293                                        unsigned EntrySize,
294                                        const MCSymbolELF *Group,
295                                        unsigned UniqueID,
296                                        const MCSymbolELF *Associated);
297
298     /// Map of currently defined macros.
299     StringMap<MCAsmMacro> MacroMap;
300
301   public:
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;
307     ~MCContext();
308
309     const SourceMgr *getSourceManager() const { return SrcMgr; }
310
311     void setInlineSourceManager(SourceMgr *SM) { InlineSrcMgr = SM; }
312
313     const MCAsmInfo *getAsmInfo() const { return MAI; }
314
315     const MCRegisterInfo *getRegisterInfo() const { return MRI; }
316
317     const MCObjectFileInfo *getObjectFileInfo() const { return MOFI; }
318
319     CodeViewContext &getCVContext();
320
321     void setAllowTemporaryLabels(bool Value) { AllowTemporaryLabels = Value; }
322     void setUseNamesOnTempLabels(bool Value) { UseNamesOnTempLabels = Value; }
323
324     /// \name Module Lifetime Management
325     /// @{
326
327     /// reset - return object to right after construction state to prepare
328     /// to process a new module
329     void reset();
330
331     /// @}
332
333     /// \name Symbol Management
334     /// @{
335
336     /// Create and return a new linker temporary symbol with a unique but
337     /// unspecified name.
338     MCSymbol *createLinkerPrivateTempSymbol();
339
340     /// Create and return a new assembler temporary symbol with a unique but
341     /// unspecified name.
342     MCSymbol *createTempSymbol(bool CanBeUnnamed = true);
343
344     MCSymbol *createTempSymbol(const Twine &Name, bool AlwaysAddSuffix,
345                                bool CanBeUnnamed = true);
346
347     /// Create the definition of a directional local symbol for numbered label
348     /// (used for "1:" definitions).
349     MCSymbol *createDirectionalLocalSymbol(unsigned LocalLabelVal);
350
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);
354
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.
357     ///
358     /// \param Name - The symbol name, which must be unique across all symbols.
359     MCSymbol *getOrCreateSymbol(const Twine &Name);
360
361     /// Gets a symbol that will be defined to the final stack offset of a local
362     /// variable after codegen.
363     ///
364     /// \param Idx - The index of a local variable passed to \@llvm.localescape.
365     MCSymbol *getOrCreateFrameAllocSymbol(StringRef FuncName, unsigned Idx);
366
367     MCSymbol *getOrCreateParentFrameOffsetSymbol(StringRef FuncName);
368
369     MCSymbol *getOrCreateLSDASymbol(StringRef FuncName);
370
371     /// Get the symbol for \p Name, or null.
372     MCSymbol *lookupSymbol(const Twine &Name) const;
373
374     /// Set value for a symbol.
375     void setSymbolValue(MCStreamer &Streamer, StringRef Sym, uint64_t Val);
376
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
380     /// APIs.
381     const SymbolTable &getSymbols() const { return Symbols; }
382
383     /// isInlineAsmLabel - Return true if the name is a label referenced in
384     /// inline assembly.
385     MCSymbol *getInlineAsmLabel(StringRef Name) const {
386       return InlineAsmUsedLabelNames.lookup(Name);
387     }
388
389     /// registerInlineAsmLabel - Records that the name is a label referenced in
390     /// inline assembly.
391     void registerInlineAsmLabel(MCSymbol *Sym);
392
393     /// @}
394
395     /// \name Section Management
396     /// @{
397
398     enum : unsigned {
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
403     };
404
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);
411
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,
416                              BeginSymName);
417     }
418
419     MCSectionELF *getELFSection(const Twine &Section, unsigned Type,
420                                 unsigned Flags) {
421       return getELFSection(Section, Type, Flags, 0, "");
422     }
423
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);
428     }
429
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,
434                            nullptr);
435     }
436
437     MCSectionELF *getELFSection(const Twine &Section, unsigned Type,
438                                 unsigned Flags, unsigned EntrySize,
439                                 const Twine &Group, unsigned UniqueID,
440                                 const MCSymbolELF *Associated);
441
442     MCSectionELF *getELFSection(const Twine &Section, unsigned Type,
443                                 unsigned Flags, unsigned EntrySize,
444                                 const MCSymbolELF *Group, unsigned UniqueID,
445                                 const MCSymbolELF *Associated);
446
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);
454
455     MCSectionELF *createELFRelSection(const Twine &Name, unsigned Type,
456                                       unsigned Flags, unsigned EntrySize,
457                                       const MCSymbolELF *Group,
458                                       const MCSectionELF *RelInfoSection);
459
460     void renameELFSection(MCSectionELF *Section, StringRef Name);
461
462     MCSectionELF *createELFGroupSection(const MCSymbolELF *Group);
463
464     MCSectionCOFF *getCOFFSection(StringRef Section, unsigned Characteristics,
465                                   SectionKind Kind, StringRef COMDATSymName,
466                                   int Selection,
467                                   unsigned UniqueID = GenericSectionID,
468                                   const char *BeginSymName = nullptr);
469
470     MCSectionCOFF *getCOFFSection(StringRef Section, unsigned Characteristics,
471                                   SectionKind Kind,
472                                   const char *BeginSymName = nullptr);
473
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.
478     MCSectionCOFF *
479     getAssociativeCOFFSection(MCSectionCOFF *Sec, const MCSymbol *KeySym,
480                               unsigned UniqueID = GenericSectionID);
481
482     MCSectionWasm *getWasmSection(const Twine &Section, SectionKind K) {
483       return getWasmSection(Section, K, nullptr);
484     }
485
486     MCSectionWasm *getWasmSection(const Twine &Section, SectionKind K,
487                                   const char *BeginSymName) {
488       return getWasmSection(Section, K, "", ~0, BeginSymName);
489     }
490
491     MCSectionWasm *getWasmSection(const Twine &Section, SectionKind K,
492                                   const Twine &Group, unsigned UniqueID) {
493       return getWasmSection(Section, K, Group, UniqueID, nullptr);
494     }
495
496     MCSectionWasm *getWasmSection(const Twine &Section, SectionKind K,
497                                   const Twine &Group, unsigned UniqueID,
498                                   const char *BeginSymName);
499
500     MCSectionWasm *getWasmSection(const Twine &Section, SectionKind K,
501                                   const MCSymbolWasm *Group, unsigned UniqueID,
502                                   const char *BeginSymName);
503
504     MCSectionXCOFF *getXCOFFSection(StringRef Section,
505                                     XCOFF::StorageMappingClass MappingClass,
506                                     SectionKind K,
507                                     const char *BeginSymName = nullptr);
508
509     // Create and save a copy of STI and return a reference to the copy.
510     MCSubtargetInfo &getSubtargetCopy(const MCSubtargetInfo &STI);
511
512     /// @}
513
514     /// \name Dwarf Management
515     /// @{
516
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; }
521
522     /// Set the compilation directory for DW_AT_comp_dir
523     void setCompilationDir(StringRef S) { CompilationDir = S.str(); }
524
525     /// Add an entry to the debug prefix map.
526     void addDebugPrefixMapEntry(const std::string &From, const std::string &To);
527
528     // Remaps all debug directory paths in-place as per the debug prefix map.
529     void RemapDebugPaths();
530
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; }
535
536     /// Set the main file name and override the default.
537     void setMainFileName(StringRef S) { MainFileName = S; }
538
539     /// Creates an entry in the dwarf file and directory tables.
540     Expected<unsigned> getDwarfFile(StringRef Directory, StringRef FileName,
541                                     unsigned FileNumber,
542                                     Optional<MD5::MD5Result> Checksum,
543                                     Optional<StringRef> Source, unsigned CUID);
544
545     bool isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID = 0);
546
547     const std::map<unsigned, MCDwarfLineTable> &getMCDwarfLineTables() const {
548       return MCDwarfLineTablesCUMap;
549     }
550
551     MCDwarfLineTable &getMCDwarfLineTable(unsigned CUID) {
552       return MCDwarfLineTablesCUMap[CUID];
553     }
554
555     const MCDwarfLineTable &getMCDwarfLineTable(unsigned CUID) const {
556       auto I = MCDwarfLineTablesCUMap.find(CUID);
557       assert(I != MCDwarfLineTablesCUMap.end());
558       return I->second;
559     }
560
561     const SmallVectorImpl<MCDwarfFile> &getMCDwarfFiles(unsigned CUID = 0) {
562       return getMCDwarfLineTable(CUID).getMCDwarfFiles();
563     }
564
565     const SmallVectorImpl<std::string> &getMCDwarfDirs(unsigned CUID = 0) {
566       return getMCDwarfLineTable(CUID).getMCDwarfDirs();
567     }
568
569     unsigned getDwarfCompileUnitID() { return DwarfCompileUnitID; }
570
571     void setDwarfCompileUnitID(unsigned CUIndex) {
572       DwarfCompileUnitID = CUIndex;
573     }
574
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,
578                                 StringRef Filename,
579                                 Optional<MD5::MD5Result> Checksum,
580                                 Optional<StringRef> Source) {
581       getMCDwarfLineTable(CUID).setRootFile(CompilationDir, Filename, Checksum,
582                                             Source);
583     }
584
585     /// Reports whether MD5 checksum usage is consistent (all-or-none).
586     bool isDwarfMD5UsageConsistent(unsigned CUID) const {
587       return getMCDwarfLineTable(CUID).isMD5UsageConsistent();
588     }
589
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);
603       DwarfLocSeen = true;
604     }
605
606     void clearDwarfLocSeen() { DwarfLocSeen = false; }
607
608     bool getDwarfLocSeen() { return DwarfLocSeen; }
609     const MCDwarfLoc &getCurrentDwarfLoc() { return CurrentDwarfLoc; }
610
611     bool getGenDwarfForAssembly() { return GenDwarfForAssembly; }
612     void setGenDwarfForAssembly(bool Value) { GenDwarfForAssembly = Value; }
613     unsigned getGenDwarfFileNumber() { return GenDwarfFileNumber; }
614
615     void setGenDwarfFileNumber(unsigned FileNumber) {
616       GenDwarfFileNumber = FileNumber;
617     }
618
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);
622
623     const SetVector<MCSection *> &getGenDwarfSectionSyms() {
624       return SectionsForRanges;
625     }
626
627     bool addGenDwarfSection(MCSection *Sec) {
628       return SectionsForRanges.insert(Sec);
629     }
630
631     void finalizeDwarfSections(MCStreamer &MCOS);
632
633     const std::vector<MCGenDwarfLabelEntry> &getMCGenDwarfLabelEntries() const {
634       return MCGenDwarfLabelEntries;
635     }
636
637     void addMCGenDwarfLabelEntry(const MCGenDwarfLabelEntry &E) {
638       MCGenDwarfLabelEntries.push_back(E);
639     }
640
641     void setDwarfDebugFlags(StringRef S) { DwarfDebugFlags = S; }
642     StringRef getDwarfDebugFlags() { return DwarfDebugFlags; }
643
644     void setDwarfDebugProducer(StringRef S) { DwarfDebugProducer = S; }
645     StringRef getDwarfDebugProducer() { return DwarfDebugProducer; }
646
647     dwarf::DwarfFormat getDwarfFormat() const {
648       // TODO: Support DWARF64
649       return dwarf::DWARF32;
650     }
651
652     void setDwarfVersion(uint16_t v) { DwarfVersion = v; }
653     uint16_t getDwarfVersion() const { return DwarfVersion; }
654
655     /// @}
656
657     char *getSecureLogFile() { return SecureLogFile; }
658     raw_fd_ostream *getSecureLog() { return SecureLog.get(); }
659
660     void setSecureLog(std::unique_ptr<raw_fd_ostream> Value) {
661       SecureLog = std::move(Value);
662     }
663
664     bool getSecureLogUsed() { return SecureLogUsed; }
665     void setSecureLogUsed(bool Value) { SecureLogUsed = Value; }
666
667     void *allocate(unsigned Size, unsigned Align = 8) {
668       return Allocator.Allocate(Size, Align);
669     }
670
671     void deallocate(void *Ptr) {}
672
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,
679                                                   const Twine &Msg);
680
681     const MCAsmMacro *lookupMacro(StringRef Name) {
682       StringMap<MCAsmMacro>::iterator I = MacroMap.find(Name);
683       return (I == MacroMap.end()) ? nullptr : &I->getValue();
684     }
685
686     void defineMacro(StringRef Name, MCAsmMacro Macro) {
687       MacroMap.insert(std::make_pair(Name, std::move(Macro)));
688     }
689
690     void undefineMacro(StringRef Name) { MacroMap.erase(Name); }
691   };
692
693 } // end namespace llvm
694
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.
698 ///
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):
704 /// \code
705 /// // Default alignment (8)
706 /// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
707 /// // Specific alignment
708 /// IntegerLiteral *Ex2 = new (Context, 4) IntegerLiteral(arguments);
709 /// \endcode
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).
713 ///
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);
722 }
723 /// Placement delete companion to the new above.
724 ///
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 {
730   C.deallocate(Ptr);
731 }
732
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
735 /// null on error.
736 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
737 /// \code
738 /// // Default alignment (8)
739 /// char *data = new (Context) char[10];
740 /// // Specific alignment
741 /// char *data = new (Context, 4) char[10];
742 /// \endcode
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).
746 ///
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);
755 }
756
757 /// Placement delete[] companion to the new[] above.
758 ///
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 {
764   C.deallocate(Ptr);
765 }
766
767 #endif // LLVM_MC_MCCONTEXT_H