MCContext.h revision 210299
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
13207618Srdivacky#include "llvm/MC/SectionKind.h"
14195098Sed#include "llvm/ADT/DenseMap.h"
15195098Sed#include "llvm/ADT/StringMap.h"
16195098Sed#include "llvm/Support/Allocator.h"
17210299Sed#include "llvm/Support/raw_ostream.h"
18195098Sed
19195098Sednamespace llvm {
20205218Srdivacky  class MCAsmInfo;
21198396Srdivacky  class MCExpr;
22195098Sed  class MCSection;
23195098Sed  class MCSymbol;
24208599Srdivacky  class MCLabel;
25198090Srdivacky  class StringRef;
26198396Srdivacky  class Twine;
27207618Srdivacky  class MCSectionMachO;
28195098Sed
29198090Srdivacky  /// MCContext - Context object for machine code objects.  This class owns all
30198090Srdivacky  /// of the sections that it creates.
31198090Srdivacky  ///
32195098Sed  class MCContext {
33195098Sed    MCContext(const MCContext&); // DO NOT IMPLEMENT
34195098Sed    MCContext &operator=(const MCContext&); // DO NOT IMPLEMENT
35195098Sed
36205218Srdivacky    /// The MCAsmInfo for this target.
37205218Srdivacky    const MCAsmInfo &MAI;
38205218Srdivacky
39195098Sed    /// Sections - Bindings of names to allocated sections.
40195098Sed    StringMap<MCSection*> Sections;
41195098Sed
42195098Sed    /// Symbols - Bindings of names to symbols.
43195098Sed    StringMap<MCSymbol*> Symbols;
44195098Sed
45205218Srdivacky    /// NextUniqueID - The next ID to dole out to an unnamed assembler temporary
46205218Srdivacky    /// symbol.
47205218Srdivacky    unsigned NextUniqueID;
48208599Srdivacky
49208599Srdivacky    /// Instances of directional local labels.
50208599Srdivacky    DenseMap<unsigned, MCLabel *> Instances;
51208599Srdivacky    /// NextInstance() creates the next instance of the directional local label
52208599Srdivacky    /// for the LocalLabelVal and adds it to the map if needed.
53208599Srdivacky    unsigned NextInstance(int64_t LocalLabelVal);
54208599Srdivacky    /// GetInstance() gets the current instance of the directional local label
55208599Srdivacky    /// for the LocalLabelVal and adds it to the map if needed.
56208599Srdivacky    unsigned GetInstance(int64_t LocalLabelVal);
57205218Srdivacky
58210299Sed    /// The file name of the log file from the enviromment variable
59210299Sed    /// AS_SECURE_LOG_FILE.  Which must be set before the .secure_log_unique
60210299Sed    /// directive is used or it is an error.
61210299Sed    char *SecureLogFile;
62210299Sed    /// The stream that gets written to for the .secure_log_unique directive.
63210299Sed    raw_ostream *SecureLog;
64210299Sed    /// Boolean toggled when .secure_log_unique / .secure_log_reset is seen to
65210299Sed    /// catch errors if .secure_log_unique appears twice without
66210299Sed    /// .secure_log_reset appearing between them.
67210299Sed    bool SecureLogUsed;
68210299Sed
69195098Sed    /// Allocator - Allocator object used for creating machine code objects.
70195098Sed    ///
71195098Sed    /// We use a bump pointer allocator to avoid the need to track all allocated
72195098Sed    /// objects.
73195098Sed    BumpPtrAllocator Allocator;
74207618Srdivacky
75208599Srdivacky    void *MachOUniquingMap, *ELFUniquingMap, *COFFUniquingMap;
76195098Sed  public:
77205218Srdivacky    explicit MCContext(const MCAsmInfo &MAI);
78195098Sed    ~MCContext();
79205218Srdivacky
80205218Srdivacky    const MCAsmInfo &getAsmInfo() const { return MAI; }
81195098Sed
82198090Srdivacky    /// @name Symbol Managment
83198090Srdivacky    /// @{
84205218Srdivacky
85205218Srdivacky    /// CreateTempSymbol - Create and return a new assembler temporary symbol
86205218Srdivacky    /// with a unique but unspecified name.
87205218Srdivacky    MCSymbol *CreateTempSymbol();
88198090Srdivacky
89208599Srdivacky    /// CreateDirectionalLocalSymbol - Create the defintion of a directional
90208599Srdivacky    /// local symbol for numbered label (used for "1:" defintions).
91208599Srdivacky    MCSymbol *CreateDirectionalLocalSymbol(int64_t LocalLabelVal);
92208599Srdivacky
93208599Srdivacky    /// GetDirectionalLocalSymbol - Create and return a directional local
94208599Srdivacky    /// symbol for numbered label (used for "1b" or 1f" references).
95208599Srdivacky    MCSymbol *GetDirectionalLocalSymbol(int64_t LocalLabelVal, int bORf);
96208599Srdivacky
97195098Sed    /// GetOrCreateSymbol - Lookup the symbol inside with the specified
98204642Srdivacky    /// @p Name.  If it exists, return it.  If not, create a forward
99195098Sed    /// reference and return it.
100195098Sed    ///
101195098Sed    /// @param Name - The symbol name, which must be unique across all symbols.
102206083Srdivacky    MCSymbol *GetOrCreateSymbol(StringRef Name);
103206083Srdivacky    MCSymbol *GetOrCreateSymbol(const Twine &Name);
104198396Srdivacky
105204642Srdivacky    /// LookupSymbol - Get the symbol for \p Name, or null.
106199481Srdivacky    MCSymbol *LookupSymbol(StringRef Name) const;
107195098Sed
108198090Srdivacky    /// @}
109207618Srdivacky
110207618Srdivacky    /// @name Section Managment
111207618Srdivacky    /// @{
112195098Sed
113207618Srdivacky    /// getMachOSection - Return the MCSection for the specified mach-o section.
114207618Srdivacky    /// This requires the operands to be valid.
115207618Srdivacky    const MCSectionMachO *getMachOSection(StringRef Segment,
116207618Srdivacky                                          StringRef Section,
117207618Srdivacky                                          unsigned TypeAndAttributes,
118207618Srdivacky                                          unsigned Reserved2,
119207618Srdivacky                                          SectionKind K);
120207618Srdivacky    const MCSectionMachO *getMachOSection(StringRef Segment,
121207618Srdivacky                                          StringRef Section,
122207618Srdivacky                                          unsigned TypeAndAttributes,
123207618Srdivacky                                          SectionKind K) {
124207618Srdivacky      return getMachOSection(Segment, Section, TypeAndAttributes, 0, K);
125207618Srdivacky    }
126207618Srdivacky
127207618Srdivacky    const MCSection *getELFSection(StringRef Section, unsigned Type,
128207618Srdivacky                                   unsigned Flags, SectionKind Kind,
129207618Srdivacky                                   bool IsExplicit = false);
130208599Srdivacky
131208599Srdivacky    const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
132208599Srdivacky                                    int Selection, SectionKind Kind);
133208599Srdivacky
134208599Srdivacky    const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
135208599Srdivacky                                    SectionKind Kind) {
136208599Srdivacky      return getCOFFSection (Section, Characteristics, 0, Kind);
137208599Srdivacky    }
138208599Srdivacky
139207618Srdivacky
140207618Srdivacky    /// @}
141207618Srdivacky
142210299Sed    char *getSecureLogFile() { return SecureLogFile; }
143210299Sed    raw_ostream *getSecureLog() { return SecureLog; }
144210299Sed    bool getSecureLogUsed() { return SecureLogUsed; }
145210299Sed    void setSecureLog(raw_ostream *Value) {
146210299Sed      SecureLog = Value;
147210299Sed    }
148210299Sed    void setSecureLogUsed(bool Value) {
149210299Sed      SecureLogUsed = Value;
150210299Sed    }
151210299Sed
152195098Sed    void *Allocate(unsigned Size, unsigned Align = 8) {
153195098Sed      return Allocator.Allocate(Size, Align);
154195098Sed    }
155198396Srdivacky    void Deallocate(void *Ptr) {
156195098Sed    }
157195098Sed  };
158195098Sed
159195098Sed} // end namespace llvm
160195098Sed
161195098Sed// operator new and delete aren't allowed inside namespaces.
162195098Sed// The throw specifications are mandated by the standard.
163195098Sed/// @brief Placement new for using the MCContext's allocator.
164195098Sed///
165195098Sed/// This placement form of operator new uses the MCContext's allocator for
166195098Sed/// obtaining memory. It is a non-throwing new, which means that it returns
167195098Sed/// null on error. (If that is what the allocator does. The current does, so if
168195098Sed/// this ever changes, this operator will have to be changed, too.)
169195098Sed/// Usage looks like this (assuming there's an MCContext 'Context' in scope):
170195098Sed/// @code
171195098Sed/// // Default alignment (16)
172195098Sed/// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
173195098Sed/// // Specific alignment
174195098Sed/// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
175195098Sed/// @endcode
176195098Sed/// Please note that you cannot use delete on the pointer; it must be
177195098Sed/// deallocated using an explicit destructor call followed by
178195098Sed/// @c Context.Deallocate(Ptr).
179195098Sed///
180195098Sed/// @param Bytes The number of bytes to allocate. Calculated by the compiler.
181195098Sed/// @param C The MCContext that provides the allocator.
182195098Sed/// @param Alignment The alignment of the allocated memory (if the underlying
183195098Sed///                  allocator supports it).
184195098Sed/// @return The allocated memory. Could be NULL.
185195098Sedinline void *operator new(size_t Bytes, llvm::MCContext &C,
186195098Sed                          size_t Alignment = 16) throw () {
187195098Sed  return C.Allocate(Bytes, Alignment);
188195098Sed}
189195098Sed/// @brief Placement delete companion to the new above.
190195098Sed///
191195098Sed/// This operator is just a companion to the new above. There is no way of
192195098Sed/// invoking it directly; see the new operator for more details. This operator
193195098Sed/// is called implicitly by the compiler if a placement new expression using
194195098Sed/// the MCContext throws in the object constructor.
195195098Sedinline void operator delete(void *Ptr, llvm::MCContext &C, size_t)
196195098Sed              throw () {
197195098Sed  C.Deallocate(Ptr);
198195098Sed}
199195098Sed
200195098Sed/// This placement form of operator new[] uses the MCContext's allocator for
201195098Sed/// obtaining memory. It is a non-throwing new[], which means that it returns
202195098Sed/// null on error.
203195098Sed/// Usage looks like this (assuming there's an MCContext 'Context' in scope):
204195098Sed/// @code
205195098Sed/// // Default alignment (16)
206195098Sed/// char *data = new (Context) char[10];
207195098Sed/// // Specific alignment
208195098Sed/// char *data = new (Context, 8) char[10];
209195098Sed/// @endcode
210195098Sed/// Please note that you cannot use delete on the pointer; it must be
211195098Sed/// deallocated using an explicit destructor call followed by
212195098Sed/// @c Context.Deallocate(Ptr).
213195098Sed///
214195098Sed/// @param Bytes The number of bytes to allocate. Calculated by the compiler.
215195098Sed/// @param C The MCContext that provides the allocator.
216195098Sed/// @param Alignment The alignment of the allocated memory (if the underlying
217195098Sed///                  allocator supports it).
218195098Sed/// @return The allocated memory. Could be NULL.
219195098Sedinline void *operator new[](size_t Bytes, llvm::MCContext& C,
220195098Sed                            size_t Alignment = 16) throw () {
221195098Sed  return C.Allocate(Bytes, Alignment);
222195098Sed}
223195098Sed
224195098Sed/// @brief Placement delete[] companion to the new[] above.
225195098Sed///
226195098Sed/// This operator is just a companion to the new[] above. There is no way of
227195098Sed/// invoking it directly; see the new[] operator for more details. This operator
228195098Sed/// is called implicitly by the compiler if a placement new[] expression using
229195098Sed/// the MCContext throws in the object constructor.
230195098Sedinline void operator delete[](void *Ptr, llvm::MCContext &C) throw () {
231195098Sed  C.Deallocate(Ptr);
232195098Sed}
233195098Sed
234195098Sed#endif
235