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