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