1195098Sed//===- MCContext.h - Machine Code Context -----------------------*- C++ -*-===//
2195098Sed//
3195098Sed//                     The LLVM Compiler Infrastructure
4195098Sed//
5195098Sed// This file is distributed under the University of Illinois Open Source
6195098Sed// License. See LICENSE.TXT for details.
7195098Sed//
8195098Sed//===----------------------------------------------------------------------===//
9195098Sed
10195098Sed#ifndef LLVM_MC_MCCONTEXT_H
11195098Sed#define LLVM_MC_MCCONTEXT_H
12195098Sed
13195098Sed#include "llvm/ADT/DenseMap.h"
14263508Sdim#include "llvm/ADT/SmallString.h"
15249423Sdim#include "llvm/ADT/SmallVector.h"
16195098Sed#include "llvm/ADT/StringMap.h"
17249423Sdim#include "llvm/MC/MCDwarf.h"
18249423Sdim#include "llvm/MC/SectionKind.h"
19195098Sed#include "llvm/Support/Allocator.h"
20234353Sdim#include "llvm/Support/Compiler.h"
21210299Sed#include "llvm/Support/raw_ostream.h"
22249423Sdim#include <map>
23212904Sdim#include <vector> // FIXME: Shouldn't be needed.
24195098Sed
25195098Sednamespace llvm {
26205218Srdivacky  class MCAsmInfo;
27198396Srdivacky  class MCExpr;
28195098Sed  class MCSection;
29195098Sed  class MCSymbol;
30208599Srdivacky  class MCLabel;
31212904Sdim  class MCDwarfFile;
32212904Sdim  class MCDwarfLoc;
33226633Sdim  class MCObjectFileInfo;
34226633Sdim  class MCRegisterInfo;
35212904Sdim  class MCLineSection;
36234353Sdim  class SMLoc;
37198090Srdivacky  class StringRef;
38198396Srdivacky  class Twine;
39207618Srdivacky  class MCSectionMachO;
40218893Sdim  class MCSectionELF;
41263508Sdim  class MCSectionCOFF;
42195098Sed
43198090Srdivacky  /// MCContext - Context object for machine code objects.  This class owns all
44198090Srdivacky  /// of the sections that it creates.
45198090Srdivacky  ///
46195098Sed  class MCContext {
47243830Sdim    MCContext(const MCContext&) LLVM_DELETED_FUNCTION;
48243830Sdim    MCContext &operator=(const MCContext&) LLVM_DELETED_FUNCTION;
49224145Sdim  public:
50224145Sdim    typedef StringMap<MCSymbol*, BumpPtrAllocator&> SymbolTable;
51224145Sdim  private:
52234353Sdim    /// The SourceMgr for this object, if any.
53234353Sdim    const SourceMgr *SrcMgr;
54195098Sed
55205218Srdivacky    /// The MCAsmInfo for this target.
56263508Sdim    const MCAsmInfo *MAI;
57195098Sed
58226633Sdim    /// The MCRegisterInfo for this target.
59263508Sdim    const MCRegisterInfo *MRI;
60218893Sdim
61226633Sdim    /// The MCObjectFileInfo for this target.
62226633Sdim    const MCObjectFileInfo *MOFI;
63226633Sdim
64221345Sdim    /// Allocator - Allocator object used for creating machine code objects.
65221345Sdim    ///
66221345Sdim    /// We use a bump pointer allocator to avoid the need to track all allocated
67221345Sdim    /// objects.
68221345Sdim    BumpPtrAllocator Allocator;
69221345Sdim
70195098Sed    /// Symbols - Bindings of names to symbols.
71224145Sdim    SymbolTable Symbols;
72195098Sed
73218893Sdim    /// UsedNames - Keeps tracks of names that were used both for used declared
74218893Sdim    /// and artificial symbols.
75221345Sdim    StringMap<bool, BumpPtrAllocator&> UsedNames;
76218893Sdim
77205218Srdivacky    /// NextUniqueID - The next ID to dole out to an unnamed assembler temporary
78205218Srdivacky    /// symbol.
79205218Srdivacky    unsigned NextUniqueID;
80208599Srdivacky
81208599Srdivacky    /// Instances of directional local labels.
82208599Srdivacky    DenseMap<unsigned, MCLabel *> Instances;
83208599Srdivacky    /// NextInstance() creates the next instance of the directional local label
84208599Srdivacky    /// for the LocalLabelVal and adds it to the map if needed.
85208599Srdivacky    unsigned NextInstance(int64_t LocalLabelVal);
86208599Srdivacky    /// GetInstance() gets the current instance of the directional local label
87208599Srdivacky    /// for the LocalLabelVal and adds it to the map if needed.
88208599Srdivacky    unsigned GetInstance(int64_t LocalLabelVal);
89218893Sdim
90218893Sdim    /// The file name of the log file from the environment variable
91210299Sed    /// AS_SECURE_LOG_FILE.  Which must be set before the .secure_log_unique
92210299Sed    /// directive is used or it is an error.
93210299Sed    char *SecureLogFile;
94210299Sed    /// The stream that gets written to for the .secure_log_unique directive.
95210299Sed    raw_ostream *SecureLog;
96210299Sed    /// Boolean toggled when .secure_log_unique / .secure_log_reset is seen to
97210299Sed    /// catch errors if .secure_log_unique appears twice without
98210299Sed    /// .secure_log_reset appearing between them.
99210299Sed    bool SecureLogUsed;
100210299Sed
101249423Sdim    /// The compilation directory to use for DW_AT_comp_dir.
102263508Sdim    SmallString<128> CompilationDir;
103249423Sdim
104249423Sdim    /// The main file name if passed in explicitly.
105249423Sdim    std::string MainFileName;
106249423Sdim
107212904Sdim    /// The dwarf file and directory tables from the dwarf .file directive.
108249423Sdim    /// We now emit a line table for each compile unit. To reduce the prologue
109249423Sdim    /// size of each line table, the files and directories used by each compile
110249423Sdim    /// unit are separated.
111249423Sdim    typedef std::map<unsigned, SmallVector<MCDwarfFile *, 4> > MCDwarfFilesMap;
112249423Sdim    MCDwarfFilesMap MCDwarfFilesCUMap;
113249423Sdim    std::map<unsigned, SmallVector<StringRef, 4> > MCDwarfDirsCUMap;
114212904Sdim
115212904Sdim    /// The current dwarf line information from the last dwarf .loc directive.
116212904Sdim    MCDwarfLoc CurrentDwarfLoc;
117212904Sdim    bool DwarfLocSeen;
118212904Sdim
119234353Sdim    /// Generate dwarf debugging info for assembly source files.
120234353Sdim    bool GenDwarfForAssembly;
121234353Sdim
122234353Sdim    /// The current dwarf file number when generate dwarf debugging info for
123234353Sdim    /// assembly source files.
124234353Sdim    unsigned GenDwarfFileNumber;
125234353Sdim
126234353Sdim    /// The default initial text section that we generate dwarf debugging line
127234353Sdim    /// info for when generating dwarf assembly source files.
128234353Sdim    const MCSection *GenDwarfSection;
129234353Sdim    /// Symbols created for the start and end of this section.
130234353Sdim    MCSymbol *GenDwarfSectionStartSym, *GenDwarfSectionEndSym;
131234353Sdim
132234353Sdim    /// The information gathered from labels that will have dwarf label
133234353Sdim    /// entries when generating dwarf assembly source files.
134234353Sdim    std::vector<const MCGenDwarfLabelEntry *> MCGenDwarfLabelEntries;
135234353Sdim
136234353Sdim    /// The string to embed in the debug information for the compile unit, if
137234353Sdim    /// non-empty.
138234353Sdim    StringRef DwarfDebugFlags;
139234353Sdim
140249423Sdim    /// The string to embed in as the dwarf AT_producer for the compile unit, if
141249423Sdim    /// non-empty.
142249423Sdim    StringRef DwarfDebugProducer;
143249423Sdim
144221345Sdim    /// Honor temporary labels, this is useful for debugging semantic
145221345Sdim    /// differences between temporary and non-temporary labels (primarily on
146221345Sdim    /// Darwin).
147221345Sdim    bool AllowTemporaryLabels;
148221345Sdim
149212904Sdim    /// The dwarf line information from the .loc directives for the sections
150212904Sdim    /// with assembled machine instructions have after seeing .loc directives.
151212904Sdim    DenseMap<const MCSection *, MCLineSection *> MCLineSections;
152218893Sdim    /// We need a deterministic iteration order, so we remember the order
153218893Sdim    /// the elements were added.
154218893Sdim    std::vector<const MCSection *> MCLineSectionOrder;
155249423Sdim    /// The Compile Unit ID that we are currently processing.
156249423Sdim    unsigned DwarfCompileUnitID;
157249423Sdim    /// The line table start symbol for each Compile Unit.
158249423Sdim    DenseMap<unsigned, MCSymbol *> MCLineTableSymbols;
159212904Sdim
160208599Srdivacky    void *MachOUniquingMap, *ELFUniquingMap, *COFFUniquingMap;
161218893Sdim
162249423Sdim    /// Do automatic reset in destructor
163249423Sdim    bool AutoReset;
164249423Sdim
165218893Sdim    MCSymbol *CreateSymbol(StringRef Name);
166218893Sdim
167195098Sed  public:
168263508Sdim    explicit MCContext(const MCAsmInfo *MAI, const MCRegisterInfo *MRI,
169249423Sdim                       const MCObjectFileInfo *MOFI, const SourceMgr *Mgr = 0,
170249423Sdim                       bool DoAutoReset = true);
171195098Sed    ~MCContext();
172218893Sdim
173234353Sdim    const SourceMgr *getSourceManager() const { return SrcMgr; }
174234353Sdim
175263508Sdim    const MCAsmInfo *getAsmInfo() const { return MAI; }
176195098Sed
177263508Sdim    const MCRegisterInfo *getRegisterInfo() const { return MRI; }
178218893Sdim
179226633Sdim    const MCObjectFileInfo *getObjectFileInfo() const { return MOFI; }
180226633Sdim
181221345Sdim    void setAllowTemporaryLabels(bool Value) { AllowTemporaryLabels = Value; }
182221345Sdim
183249423Sdim    /// @name Module Lifetime Management
184249423Sdim    /// @{
185249423Sdim
186249423Sdim    /// reset - return object to right after construction state to prepare
187249423Sdim    /// to process a new module
188249423Sdim    void reset();
189249423Sdim
190249423Sdim    /// @}
191249423Sdim
192218893Sdim    /// @name Symbol Management
193198090Srdivacky    /// @{
194218893Sdim
195205218Srdivacky    /// CreateTempSymbol - Create and return a new assembler temporary symbol
196205218Srdivacky    /// with a unique but unspecified name.
197205218Srdivacky    MCSymbol *CreateTempSymbol();
198198090Srdivacky
199239462Sdim    /// getUniqueSymbolID() - Return a unique identifier for use in constructing
200239462Sdim    /// symbol names.
201239462Sdim    unsigned getUniqueSymbolID() { return NextUniqueID++; }
202239462Sdim
203218893Sdim    /// CreateDirectionalLocalSymbol - Create the definition of a directional
204218893Sdim    /// local symbol for numbered label (used for "1:" definitions).
205208599Srdivacky    MCSymbol *CreateDirectionalLocalSymbol(int64_t LocalLabelVal);
206208599Srdivacky
207208599Srdivacky    /// GetDirectionalLocalSymbol - Create and return a directional local
208208599Srdivacky    /// symbol for numbered label (used for "1b" or 1f" references).
209208599Srdivacky    MCSymbol *GetDirectionalLocalSymbol(int64_t LocalLabelVal, int bORf);
210208599Srdivacky
211195098Sed    /// GetOrCreateSymbol - Lookup the symbol inside with the specified
212204642Srdivacky    /// @p Name.  If it exists, return it.  If not, create a forward
213195098Sed    /// reference and return it.
214195098Sed    ///
215195098Sed    /// @param Name - The symbol name, which must be unique across all symbols.
216206083Srdivacky    MCSymbol *GetOrCreateSymbol(StringRef Name);
217206083Srdivacky    MCSymbol *GetOrCreateSymbol(const Twine &Name);
218198396Srdivacky
219204642Srdivacky    /// LookupSymbol - Get the symbol for \p Name, or null.
220199481Srdivacky    MCSymbol *LookupSymbol(StringRef Name) const;
221243830Sdim    MCSymbol *LookupSymbol(const Twine &Name) const;
222195098Sed
223224145Sdim    /// getSymbols - Get a reference for the symbol table for clients that
224224145Sdim    /// want to, for example, iterate over all symbols. 'const' because we
225224145Sdim    /// still want any modifications to the table itself to use the MCContext
226224145Sdim    /// APIs.
227224145Sdim    const SymbolTable &getSymbols() const {
228224145Sdim      return Symbols;
229224145Sdim    }
230224145Sdim
231198090Srdivacky    /// @}
232218893Sdim
233218893Sdim    /// @name Section Management
234207618Srdivacky    /// @{
235195098Sed
236207618Srdivacky    /// getMachOSection - Return the MCSection for the specified mach-o section.
237207618Srdivacky    /// This requires the operands to be valid.
238207618Srdivacky    const MCSectionMachO *getMachOSection(StringRef Segment,
239207618Srdivacky                                          StringRef Section,
240207618Srdivacky                                          unsigned TypeAndAttributes,
241207618Srdivacky                                          unsigned Reserved2,
242207618Srdivacky                                          SectionKind K);
243207618Srdivacky    const MCSectionMachO *getMachOSection(StringRef Segment,
244207618Srdivacky                                          StringRef Section,
245207618Srdivacky                                          unsigned TypeAndAttributes,
246207618Srdivacky                                          SectionKind K) {
247207618Srdivacky      return getMachOSection(Segment, Section, TypeAndAttributes, 0, K);
248207618Srdivacky    }
249208599Srdivacky
250218893Sdim    const MCSectionELF *getELFSection(StringRef Section, unsigned Type,
251218893Sdim                                      unsigned Flags, SectionKind Kind);
252218893Sdim
253218893Sdim    const MCSectionELF *getELFSection(StringRef Section, unsigned Type,
254218893Sdim                                      unsigned Flags, SectionKind Kind,
255218893Sdim                                      unsigned EntrySize, StringRef Group);
256218893Sdim
257218893Sdim    const MCSectionELF *CreateELFGroupSection();
258218893Sdim
259263508Sdim    const MCSectionCOFF *getCOFFSection(StringRef Section,
260263508Sdim                                        unsigned Characteristics,
261263508Sdim                                        SectionKind Kind,
262263508Sdim                                        StringRef COMDATSymName,
263263508Sdim                                        int Selection,
264263508Sdim                                        const MCSectionCOFF *Assoc = 0);
265208599Srdivacky
266263508Sdim    const MCSectionCOFF *getCOFFSection(StringRef Section,
267263508Sdim                                        unsigned Characteristics,
268263508Sdim                                        SectionKind Kind);
269208599Srdivacky
270263508Sdim    const MCSectionCOFF *getCOFFSection(StringRef Section);
271218893Sdim
272207618Srdivacky    /// @}
273207618Srdivacky
274218893Sdim    /// @name Dwarf Management
275212904Sdim    /// @{
276212904Sdim
277249423Sdim    /// \brief Get the compilation directory for DW_AT_comp_dir
278249423Sdim    /// This can be overridden by clients which want to control the reported
279249423Sdim    /// compilation directory and have it be something other than the current
280249423Sdim    /// working directory.
281263508Sdim    /// Returns an empty string if the current directory cannot be determined.
282263508Sdim    StringRef getCompilationDir() const { return CompilationDir; }
283249423Sdim
284249423Sdim    /// \brief Set the compilation directory for DW_AT_comp_dir
285249423Sdim    /// Override the default (CWD) compilation directory.
286249423Sdim    void setCompilationDir(StringRef S) { CompilationDir = S.str(); }
287249423Sdim
288249423Sdim    /// \brief Get the main file name for use in error messages and debug
289249423Sdim    /// info. This can be set to ensure we've got the correct file name
290249423Sdim    /// after preprocessing or for -save-temps.
291249423Sdim    const std::string &getMainFileName() const { return MainFileName; }
292249423Sdim
293249423Sdim    /// \brief Set the main file name and override the default.
294249423Sdim    void setMainFileName(StringRef S) { MainFileName = S.str(); }
295249423Sdim
296212904Sdim    /// GetDwarfFile - creates an entry in the dwarf file and directory tables.
297234353Sdim    unsigned GetDwarfFile(StringRef Directory, StringRef FileName,
298249423Sdim                          unsigned FileNumber, unsigned CUID);
299212904Sdim
300249423Sdim    bool isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID = 0);
301212904Sdim
302218893Sdim    bool hasDwarfFiles() const {
303249423Sdim      // Traverse MCDwarfFilesCUMap and check whether each entry is empty.
304249423Sdim      MCDwarfFilesMap::const_iterator MapB, MapE;
305249423Sdim      for (MapB = MCDwarfFilesCUMap.begin(), MapE = MCDwarfFilesCUMap.end();
306249423Sdim           MapB != MapE; MapB++)
307249423Sdim        if (!MapB->second.empty())
308249423Sdim           return true;
309249423Sdim      return false;
310218893Sdim    }
311218893Sdim
312249423Sdim    const SmallVectorImpl<MCDwarfFile *> &getMCDwarfFiles(unsigned CUID = 0) {
313249423Sdim      return MCDwarfFilesCUMap[CUID];
314212904Sdim    }
315249423Sdim    const SmallVectorImpl<StringRef> &getMCDwarfDirs(unsigned CUID = 0) {
316249423Sdim      return MCDwarfDirsCUMap[CUID];
317212904Sdim    }
318218893Sdim
319218893Sdim    const DenseMap<const MCSection *, MCLineSection *>
320218893Sdim    &getMCLineSections() const {
321212904Sdim      return MCLineSections;
322212904Sdim    }
323218893Sdim    const std::vector<const MCSection *> &getMCLineSectionOrder() const {
324218893Sdim      return MCLineSectionOrder;
325218893Sdim    }
326218893Sdim    void addMCLineSection(const MCSection *Sec, MCLineSection *Line) {
327218893Sdim      MCLineSections[Sec] = Line;
328218893Sdim      MCLineSectionOrder.push_back(Sec);
329218893Sdim    }
330249423Sdim    unsigned getDwarfCompileUnitID() {
331249423Sdim      return DwarfCompileUnitID;
332249423Sdim    }
333249423Sdim    void setDwarfCompileUnitID(unsigned CUIndex) {
334249423Sdim      DwarfCompileUnitID = CUIndex;
335249423Sdim    }
336249423Sdim    const DenseMap<unsigned, MCSymbol *> &getMCLineTableSymbols() const {
337249423Sdim      return MCLineTableSymbols;
338249423Sdim    }
339249423Sdim    MCSymbol *getMCLineTableSymbol(unsigned ID) const {
340249423Sdim      DenseMap<unsigned, MCSymbol *>::const_iterator CIter =
341249423Sdim        MCLineTableSymbols.find(ID);
342249423Sdim      if (CIter == MCLineTableSymbols.end())
343249423Sdim        return NULL;
344249423Sdim      return CIter->second;
345249423Sdim    }
346249423Sdim    void setMCLineTableSymbol(MCSymbol *Sym, unsigned ID) {
347249423Sdim      MCLineTableSymbols[ID] = Sym;
348249423Sdim    }
349212904Sdim
350212904Sdim    /// setCurrentDwarfLoc - saves the information from the currently parsed
351218893Sdim    /// dwarf .loc directive and sets DwarfLocSeen.  When the next instruction
352218893Sdim    /// is assembled an entry in the line number table with this information and
353212904Sdim    /// the address of the instruction will be created.
354212904Sdim    void setCurrentDwarfLoc(unsigned FileNum, unsigned Line, unsigned Column,
355218893Sdim                            unsigned Flags, unsigned Isa,
356218893Sdim                            unsigned Discriminator) {
357212904Sdim      CurrentDwarfLoc.setFileNum(FileNum);
358212904Sdim      CurrentDwarfLoc.setLine(Line);
359212904Sdim      CurrentDwarfLoc.setColumn(Column);
360212904Sdim      CurrentDwarfLoc.setFlags(Flags);
361212904Sdim      CurrentDwarfLoc.setIsa(Isa);
362218893Sdim      CurrentDwarfLoc.setDiscriminator(Discriminator);
363212904Sdim      DwarfLocSeen = true;
364212904Sdim    }
365218893Sdim    void ClearDwarfLocSeen() { DwarfLocSeen = false; }
366212904Sdim
367212904Sdim    bool getDwarfLocSeen() { return DwarfLocSeen; }
368212904Sdim    const MCDwarfLoc &getCurrentDwarfLoc() { return CurrentDwarfLoc; }
369212904Sdim
370234353Sdim    bool getGenDwarfForAssembly() { return GenDwarfForAssembly; }
371234353Sdim    void setGenDwarfForAssembly(bool Value) { GenDwarfForAssembly = Value; }
372234353Sdim    unsigned getGenDwarfFileNumber() { return GenDwarfFileNumber; }
373234353Sdim    unsigned nextGenDwarfFileNumber() { return ++GenDwarfFileNumber; }
374234353Sdim    const MCSection *getGenDwarfSection() { return GenDwarfSection; }
375234353Sdim    void setGenDwarfSection(const MCSection *Sec) { GenDwarfSection = Sec; }
376234353Sdim    MCSymbol *getGenDwarfSectionStartSym() { return GenDwarfSectionStartSym; }
377234353Sdim    void setGenDwarfSectionStartSym(MCSymbol *Sym) {
378234353Sdim      GenDwarfSectionStartSym = Sym;
379234353Sdim    }
380234353Sdim    MCSymbol *getGenDwarfSectionEndSym() { return GenDwarfSectionEndSym; }
381234353Sdim    void setGenDwarfSectionEndSym(MCSymbol *Sym) {
382234353Sdim      GenDwarfSectionEndSym = Sym;
383234353Sdim    }
384234353Sdim    const std::vector<const MCGenDwarfLabelEntry *>
385234353Sdim      &getMCGenDwarfLabelEntries() const {
386234353Sdim      return MCGenDwarfLabelEntries;
387234353Sdim    }
388234353Sdim    void addMCGenDwarfLabelEntry(const MCGenDwarfLabelEntry *E) {
389234353Sdim      MCGenDwarfLabelEntries.push_back(E);
390234353Sdim    }
391234353Sdim
392234353Sdim    void setDwarfDebugFlags(StringRef S) { DwarfDebugFlags = S; }
393234353Sdim    StringRef getDwarfDebugFlags() { return DwarfDebugFlags; }
394234353Sdim
395249423Sdim    void setDwarfDebugProducer(StringRef S) { DwarfDebugProducer = S; }
396249423Sdim    StringRef getDwarfDebugProducer() { return DwarfDebugProducer; }
397249423Sdim
398212904Sdim    /// @}
399212904Sdim
400210299Sed    char *getSecureLogFile() { return SecureLogFile; }
401210299Sed    raw_ostream *getSecureLog() { return SecureLog; }
402210299Sed    bool getSecureLogUsed() { return SecureLogUsed; }
403210299Sed    void setSecureLog(raw_ostream *Value) {
404210299Sed      SecureLog = Value;
405210299Sed    }
406210299Sed    void setSecureLogUsed(bool Value) {
407210299Sed      SecureLogUsed = Value;
408210299Sed    }
409210299Sed
410195098Sed    void *Allocate(unsigned Size, unsigned Align = 8) {
411195098Sed      return Allocator.Allocate(Size, Align);
412195098Sed    }
413198396Srdivacky    void Deallocate(void *Ptr) {
414195098Sed    }
415234353Sdim
416263508Sdim    // Unrecoverable error has occurred. Display the best diagnostic we can
417234353Sdim    // and bail via exit(1). For now, most MC backend errors are unrecoverable.
418234353Sdim    // FIXME: We should really do something about that.
419234353Sdim    LLVM_ATTRIBUTE_NORETURN void FatalError(SMLoc L, const Twine &Msg);
420195098Sed  };
421195098Sed
422195098Sed} // end namespace llvm
423195098Sed
424195098Sed// operator new and delete aren't allowed inside namespaces.
425195098Sed// The throw specifications are mandated by the standard.
426195098Sed/// @brief Placement new for using the MCContext's allocator.
427195098Sed///
428195098Sed/// This placement form of operator new uses the MCContext's allocator for
429195098Sed/// obtaining memory. It is a non-throwing new, which means that it returns
430195098Sed/// null on error. (If that is what the allocator does. The current does, so if
431195098Sed/// this ever changes, this operator will have to be changed, too.)
432195098Sed/// Usage looks like this (assuming there's an MCContext 'Context' in scope):
433195098Sed/// @code
434195098Sed/// // Default alignment (16)
435195098Sed/// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
436195098Sed/// // Specific alignment
437195098Sed/// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
438195098Sed/// @endcode
439195098Sed/// Please note that you cannot use delete on the pointer; it must be
440195098Sed/// deallocated using an explicit destructor call followed by
441195098Sed/// @c Context.Deallocate(Ptr).
442195098Sed///
443195098Sed/// @param Bytes The number of bytes to allocate. Calculated by the compiler.
444195098Sed/// @param C The MCContext that provides the allocator.
445195098Sed/// @param Alignment The alignment of the allocated memory (if the underlying
446195098Sed///                  allocator supports it).
447195098Sed/// @return The allocated memory. Could be NULL.
448195098Sedinline void *operator new(size_t Bytes, llvm::MCContext &C,
449195098Sed                          size_t Alignment = 16) throw () {
450195098Sed  return C.Allocate(Bytes, Alignment);
451195098Sed}
452195098Sed/// @brief Placement delete companion to the new above.
453195098Sed///
454195098Sed/// This operator is just a companion to the new above. There is no way of
455195098Sed/// invoking it directly; see the new operator for more details. This operator
456195098Sed/// is called implicitly by the compiler if a placement new expression using
457195098Sed/// the MCContext throws in the object constructor.
458195098Sedinline void operator delete(void *Ptr, llvm::MCContext &C, size_t)
459195098Sed              throw () {
460195098Sed  C.Deallocate(Ptr);
461195098Sed}
462195098Sed
463195098Sed/// This placement form of operator new[] uses the MCContext's allocator for
464195098Sed/// obtaining memory. It is a non-throwing new[], which means that it returns
465195098Sed/// null on error.
466195098Sed/// Usage looks like this (assuming there's an MCContext 'Context' in scope):
467195098Sed/// @code
468195098Sed/// // Default alignment (16)
469195098Sed/// char *data = new (Context) char[10];
470195098Sed/// // Specific alignment
471195098Sed/// char *data = new (Context, 8) char[10];
472195098Sed/// @endcode
473195098Sed/// Please note that you cannot use delete on the pointer; it must be
474195098Sed/// deallocated using an explicit destructor call followed by
475195098Sed/// @c Context.Deallocate(Ptr).
476195098Sed///
477195098Sed/// @param Bytes The number of bytes to allocate. Calculated by the compiler.
478195098Sed/// @param C The MCContext that provides the allocator.
479195098Sed/// @param Alignment The alignment of the allocated memory (if the underlying
480195098Sed///                  allocator supports it).
481195098Sed/// @return The allocated memory. Could be NULL.
482195098Sedinline void *operator new[](size_t Bytes, llvm::MCContext& C,
483195098Sed                            size_t Alignment = 16) throw () {
484195098Sed  return C.Allocate(Bytes, Alignment);
485195098Sed}
486195098Sed
487195098Sed/// @brief Placement delete[] companion to the new[] above.
488195098Sed///
489195098Sed/// This operator is just a companion to the new[] above. There is no way of
490195098Sed/// invoking it directly; see the new[] operator for more details. This operator
491195098Sed/// is called implicitly by the compiler if a placement new[] expression using
492195098Sed/// the MCContext throws in the object constructor.
493195098Sedinline void operator delete[](void *Ptr, llvm::MCContext &C) throw () {
494195098Sed  C.Deallocate(Ptr);
495195098Sed}
496195098Sed
497195098Sed#endif
498