Rip out the 'is temporary' nonsense from the MCContext interface to
[oota-llvm.git] / 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/StringMap.h"
15 #include "llvm/Support/Allocator.h"
16
17 namespace llvm {
18   class MCAsmInfo;
19   class MCExpr;
20   class MCSection;
21   class MCSymbol;
22   class StringRef;
23   class Twine;
24
25   /// MCContext - Context object for machine code objects.  This class owns all
26   /// of the sections that it creates.
27   ///
28   class MCContext {
29     MCContext(const MCContext&); // DO NOT IMPLEMENT
30     MCContext &operator=(const MCContext&); // DO NOT IMPLEMENT
31
32     /// The MCAsmInfo for this target.
33     const MCAsmInfo &MAI;
34     
35     /// Sections - Bindings of names to allocated sections.
36     StringMap<MCSection*> Sections;
37
38     /// Symbols - Bindings of names to symbols.
39     StringMap<MCSymbol*> Symbols;
40
41     /// NextUniqueID - The next ID to dole out to an unnamed assembler temporary
42     /// symbol.
43     unsigned NextUniqueID;
44     
45     /// Allocator - Allocator object used for creating machine code objects.
46     ///
47     /// We use a bump pointer allocator to avoid the need to track all allocated
48     /// objects.
49     BumpPtrAllocator Allocator;
50   public:
51     explicit MCContext(const MCAsmInfo &MAI);
52     ~MCContext();
53     
54     const MCAsmInfo &getAsmInfo() const { return MAI; }
55
56     /// @name Symbol Managment
57     /// @{
58     
59     /// CreateTempSymbol - Create and return a new assembler temporary symbol
60     /// with a unique but unspecified name.
61     MCSymbol *CreateTempSymbol();
62
63     /// GetOrCreateSymbol - Lookup the symbol inside with the specified
64     /// @p Name.  If it exists, return it.  If not, create a forward
65     /// reference and return it.
66     ///
67     /// @param Name - The symbol name, which must be unique across all symbols.
68     MCSymbol *GetOrCreateSymbol(StringRef Name);
69     MCSymbol *GetOrCreateSymbol(const Twine &Name);
70
71     /// LookupSymbol - Get the symbol for \p Name, or null.
72     MCSymbol *LookupSymbol(StringRef Name) const;
73
74     /// @}
75
76     void *Allocate(unsigned Size, unsigned Align = 8) {
77       return Allocator.Allocate(Size, Align);
78     }
79     void Deallocate(void *Ptr) {
80     }
81   };
82
83 } // end namespace llvm
84
85 // operator new and delete aren't allowed inside namespaces.
86 // The throw specifications are mandated by the standard.
87 /// @brief Placement new for using the MCContext's allocator.
88 ///
89 /// This placement form of operator new uses the MCContext's allocator for
90 /// obtaining memory. It is a non-throwing new, which means that it returns
91 /// null on error. (If that is what the allocator does. The current does, so if
92 /// this ever changes, this operator will have to be changed, too.)
93 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
94 /// @code
95 /// // Default alignment (16)
96 /// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
97 /// // Specific alignment
98 /// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
99 /// @endcode
100 /// Please note that you cannot use delete on the pointer; it must be
101 /// deallocated using an explicit destructor call followed by
102 /// @c Context.Deallocate(Ptr).
103 ///
104 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
105 /// @param C The MCContext that provides the allocator.
106 /// @param Alignment The alignment of the allocated memory (if the underlying
107 ///                  allocator supports it).
108 /// @return The allocated memory. Could be NULL.
109 inline void *operator new(size_t Bytes, llvm::MCContext &C,
110                           size_t Alignment = 16) throw () {
111   return C.Allocate(Bytes, Alignment);
112 }
113 /// @brief Placement delete companion to the new above.
114 ///
115 /// This operator is just a companion to the new above. There is no way of
116 /// invoking it directly; see the new operator for more details. This operator
117 /// is called implicitly by the compiler if a placement new expression using
118 /// the MCContext throws in the object constructor.
119 inline void operator delete(void *Ptr, llvm::MCContext &C, size_t)
120               throw () {
121   C.Deallocate(Ptr);
122 }
123
124 /// This placement form of operator new[] uses the MCContext's allocator for
125 /// obtaining memory. It is a non-throwing new[], which means that it returns
126 /// null on error.
127 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
128 /// @code
129 /// // Default alignment (16)
130 /// char *data = new (Context) char[10];
131 /// // Specific alignment
132 /// char *data = new (Context, 8) char[10];
133 /// @endcode
134 /// Please note that you cannot use delete on the pointer; it must be
135 /// deallocated using an explicit destructor call followed by
136 /// @c Context.Deallocate(Ptr).
137 ///
138 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
139 /// @param C The MCContext that provides the allocator.
140 /// @param Alignment The alignment of the allocated memory (if the underlying
141 ///                  allocator supports it).
142 /// @return The allocated memory. Could be NULL.
143 inline void *operator new[](size_t Bytes, llvm::MCContext& C,
144                             size_t Alignment = 16) throw () {
145   return C.Allocate(Bytes, Alignment);
146 }
147
148 /// @brief Placement delete[] companion to the new[] above.
149 ///
150 /// This operator is just a companion to the new[] above. There is no way of
151 /// invoking it directly; see the new[] operator for more details. This operator
152 /// is called implicitly by the compiler if a placement new[] expression using
153 /// the MCContext throws in the object constructor.
154 inline void operator delete[](void *Ptr, llvm::MCContext &C) throw () {
155   C.Deallocate(Ptr);
156 }
157
158 #endif