MCContext.h revision 205218
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"
14195098Sed#include "llvm/ADT/StringMap.h"
15195098Sed#include "llvm/Support/Allocator.h"
16195098Sed
17195098Sednamespace llvm {
18205218Srdivacky  class MCAsmInfo;
19198396Srdivacky  class MCExpr;
20195098Sed  class MCSection;
21195098Sed  class MCSymbol;
22198090Srdivacky  class StringRef;
23198396Srdivacky  class Twine;
24195098Sed
25198090Srdivacky  /// MCContext - Context object for machine code objects.  This class owns all
26198090Srdivacky  /// of the sections that it creates.
27198090Srdivacky  ///
28195098Sed  class MCContext {
29195098Sed    MCContext(const MCContext&); // DO NOT IMPLEMENT
30195098Sed    MCContext &operator=(const MCContext&); // DO NOT IMPLEMENT
31195098Sed
32205218Srdivacky    /// The MCAsmInfo for this target.
33205218Srdivacky    const MCAsmInfo &MAI;
34205218Srdivacky
35195098Sed    /// Sections - Bindings of names to allocated sections.
36195098Sed    StringMap<MCSection*> Sections;
37195098Sed
38195098Sed    /// Symbols - Bindings of names to symbols.
39195098Sed    StringMap<MCSymbol*> Symbols;
40195098Sed
41205218Srdivacky    /// NextUniqueID - The next ID to dole out to an unnamed assembler temporary
42205218Srdivacky    /// symbol.
43205218Srdivacky    unsigned NextUniqueID;
44205218Srdivacky
45195098Sed    /// Allocator - Allocator object used for creating machine code objects.
46195098Sed    ///
47195098Sed    /// We use a bump pointer allocator to avoid the need to track all allocated
48195098Sed    /// objects.
49195098Sed    BumpPtrAllocator Allocator;
50195098Sed  public:
51205218Srdivacky    explicit MCContext(const MCAsmInfo &MAI);
52195098Sed    ~MCContext();
53205218Srdivacky
54205218Srdivacky    const MCAsmInfo &getAsmInfo() const { return MAI; }
55195098Sed
56198090Srdivacky    /// @name Symbol Managment
57198090Srdivacky    /// @{
58205218Srdivacky
59205218Srdivacky    /// CreateTempSymbol - Create and return a new assembler temporary symbol
60205218Srdivacky    /// with a unique but unspecified name.
61205218Srdivacky    MCSymbol *CreateTempSymbol();
62198090Srdivacky
63195098Sed    /// GetOrCreateSymbol - Lookup the symbol inside with the specified
64204642Srdivacky    /// @p Name.  If it exists, return it.  If not, create a forward
65195098Sed    /// reference and return it.
66195098Sed    ///
67195098Sed    /// @param Name - The symbol name, which must be unique across all symbols.
68205218Srdivacky    MCSymbol *GetOrCreateSymbol(StringRef Name, bool isTemporary = false);
69205218Srdivacky    MCSymbol *GetOrCreateSymbol(const Twine &Name, bool isTemporary = false);
70198396Srdivacky
71204961Srdivacky    /// GetOrCreateTemporarySymbol - Create a new assembler temporary symbol
72204961Srdivacky    /// with the specified @p Name if it doesn't exist or return the existing
73204961Srdivacky    /// one if it does.
74195098Sed    ///
75195098Sed    /// @param Name - The symbol name, for debugging purposes only, temporary
76195098Sed    /// symbols do not surive assembly. If non-empty the name must be unique
77195098Sed    /// across all symbols.
78204961Srdivacky    MCSymbol *GetOrCreateTemporarySymbol(StringRef Name = "");
79204961Srdivacky    MCSymbol *GetOrCreateTemporarySymbol(const Twine &Name);
80195098Sed
81204642Srdivacky    /// LookupSymbol - Get the symbol for \p Name, or null.
82199481Srdivacky    MCSymbol *LookupSymbol(StringRef Name) const;
83195098Sed
84198090Srdivacky    /// @}
85195098Sed
86195098Sed    void *Allocate(unsigned Size, unsigned Align = 8) {
87195098Sed      return Allocator.Allocate(Size, Align);
88195098Sed    }
89198396Srdivacky    void Deallocate(void *Ptr) {
90195098Sed    }
91195098Sed  };
92195098Sed
93195098Sed} // end namespace llvm
94195098Sed
95195098Sed// operator new and delete aren't allowed inside namespaces.
96195098Sed// The throw specifications are mandated by the standard.
97195098Sed/// @brief Placement new for using the MCContext's allocator.
98195098Sed///
99195098Sed/// This placement form of operator new uses the MCContext's allocator for
100195098Sed/// obtaining memory. It is a non-throwing new, which means that it returns
101195098Sed/// null on error. (If that is what the allocator does. The current does, so if
102195098Sed/// this ever changes, this operator will have to be changed, too.)
103195098Sed/// Usage looks like this (assuming there's an MCContext 'Context' in scope):
104195098Sed/// @code
105195098Sed/// // Default alignment (16)
106195098Sed/// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
107195098Sed/// // Specific alignment
108195098Sed/// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
109195098Sed/// @endcode
110195098Sed/// Please note that you cannot use delete on the pointer; it must be
111195098Sed/// deallocated using an explicit destructor call followed by
112195098Sed/// @c Context.Deallocate(Ptr).
113195098Sed///
114195098Sed/// @param Bytes The number of bytes to allocate. Calculated by the compiler.
115195098Sed/// @param C The MCContext that provides the allocator.
116195098Sed/// @param Alignment The alignment of the allocated memory (if the underlying
117195098Sed///                  allocator supports it).
118195098Sed/// @return The allocated memory. Could be NULL.
119195098Sedinline void *operator new(size_t Bytes, llvm::MCContext &C,
120195098Sed                          size_t Alignment = 16) throw () {
121195098Sed  return C.Allocate(Bytes, Alignment);
122195098Sed}
123195098Sed/// @brief Placement delete companion to the new above.
124195098Sed///
125195098Sed/// This operator is just a companion to the new above. There is no way of
126195098Sed/// invoking it directly; see the new operator for more details. This operator
127195098Sed/// is called implicitly by the compiler if a placement new expression using
128195098Sed/// the MCContext throws in the object constructor.
129195098Sedinline void operator delete(void *Ptr, llvm::MCContext &C, size_t)
130195098Sed              throw () {
131195098Sed  C.Deallocate(Ptr);
132195098Sed}
133195098Sed
134195098Sed/// This placement form of operator new[] uses the MCContext's allocator for
135195098Sed/// obtaining memory. It is a non-throwing new[], which means that it returns
136195098Sed/// null on error.
137195098Sed/// Usage looks like this (assuming there's an MCContext 'Context' in scope):
138195098Sed/// @code
139195098Sed/// // Default alignment (16)
140195098Sed/// char *data = new (Context) char[10];
141195098Sed/// // Specific alignment
142195098Sed/// char *data = new (Context, 8) char[10];
143195098Sed/// @endcode
144195098Sed/// Please note that you cannot use delete on the pointer; it must be
145195098Sed/// deallocated using an explicit destructor call followed by
146195098Sed/// @c Context.Deallocate(Ptr).
147195098Sed///
148195098Sed/// @param Bytes The number of bytes to allocate. Calculated by the compiler.
149195098Sed/// @param C The MCContext that provides the allocator.
150195098Sed/// @param Alignment The alignment of the allocated memory (if the underlying
151195098Sed///                  allocator supports it).
152195098Sed/// @return The allocated memory. Could be NULL.
153195098Sedinline void *operator new[](size_t Bytes, llvm::MCContext& C,
154195098Sed                            size_t Alignment = 16) throw () {
155195098Sed  return C.Allocate(Bytes, Alignment);
156195098Sed}
157195098Sed
158195098Sed/// @brief Placement delete[] companion to the new[] above.
159195098Sed///
160195098Sed/// This operator is just a companion to the new[] above. There is no way of
161195098Sed/// invoking it directly; see the new[] operator for more details. This operator
162195098Sed/// is called implicitly by the compiler if a placement new[] expression using
163195098Sed/// the MCContext throws in the object constructor.
164195098Sedinline void operator delete[](void *Ptr, llvm::MCContext &C) throw () {
165195098Sed  C.Deallocate(Ptr);
166195098Sed}
167195098Sed
168195098Sed#endif
169