MC: Switch MCContext value table to storing MCExprs.
[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 MCExpr;
19   class MCSection;
20   class MCSymbol;
21   class StringRef;
22
23   /// MCContext - Context object for machine code objects.  This class owns all
24   /// of the sections that it creates.
25   ///
26   class MCContext {
27     MCContext(const MCContext&); // DO NOT IMPLEMENT
28     MCContext &operator=(const MCContext&); // DO NOT IMPLEMENT
29
30     /// Sections - Bindings of names to allocated sections.
31     StringMap<MCSection*> Sections;
32
33     /// Symbols - Bindings of names to symbols.
34     StringMap<MCSymbol*> Symbols;
35
36     /// SymbolValues - Bindings of symbols to values.
37     //
38     // FIXME: Is there a good reason to not just put this in the MCSymbol?
39     DenseMap<const MCSymbol*, const MCExpr*> SymbolValues;
40
41     /// Allocator - Allocator object used for creating machine code objects.
42     ///
43     /// We use a bump pointer allocator to avoid the need to track all allocated
44     /// objects.
45     BumpPtrAllocator Allocator;
46   public:
47     MCContext();
48     ~MCContext();
49
50     /// @name Symbol Managment
51     /// @{
52
53     /// CreateSymbol - Create a new symbol with the specified @param Name.
54     ///
55     /// @param Name - The symbol name, which must be unique across all symbols.
56     MCSymbol *CreateSymbol(const StringRef &Name);
57
58     /// GetOrCreateSymbol - Lookup the symbol inside with the specified
59     /// @param Name.  If it exists, return it.  If not, create a forward
60     /// reference and return it.
61     ///
62     /// @param Name - The symbol name, which must be unique across all symbols.
63     /// @param IsTemporary - Whether this symbol is an assembler temporary,
64     /// which should not survive into the symbol table for the translation unit.
65     MCSymbol *GetOrCreateSymbol(const StringRef &Name);
66
67     /// CreateTemporarySymbol - Create a new temporary symbol with the specified
68     /// @param Name.
69     ///
70     /// @param Name - The symbol name, for debugging purposes only, temporary
71     /// symbols do not surive assembly. If non-empty the name must be unique
72     /// across all symbols.
73     MCSymbol *CreateTemporarySymbol(const StringRef &Name = "");
74
75     /// LookupSymbol - Get the symbol for @param Name, or null.
76     MCSymbol *LookupSymbol(const StringRef &Name) const;
77
78     /// @}
79     /// @name Symbol Value Table
80     /// @{
81
82     /// ClearSymbolValue - Erase the variable binding for @arg Symbol, if one
83     /// exists.
84     void ClearSymbolValue(const MCSymbol *Symbol) {
85       SymbolValues.erase(Symbol);
86     }
87
88     /// SetSymbolValue - Set the variable binding for @arg Symbol to @arg Value.
89     void SetSymbolValue(const MCSymbol *Symbol, const MCExpr *Value) {
90       assert(Value && "Invalid variable assignment!");
91       SymbolValues.insert(std::make_pair(Symbol, Value));
92     }
93
94     /// GetSymbolValue - Return the current variable value for @arg Symbol, or
95     /// null if @arg Symbol is not a variable.
96     const MCExpr *GetSymbolValue(const MCSymbol *Symbol) const {
97       return SymbolValues.lookup(Symbol);
98     }
99
100     /// @}
101
102     void *Allocate(unsigned Size, unsigned Align = 8) {
103       return Allocator.Allocate(Size, Align);
104     }
105     void Deallocate(void *Ptr) {
106     }
107   };
108
109 } // end namespace llvm
110
111 // operator new and delete aren't allowed inside namespaces.
112 // The throw specifications are mandated by the standard.
113 /// @brief Placement new for using the MCContext's allocator.
114 ///
115 /// This placement form of operator new uses the MCContext's allocator for
116 /// obtaining memory. It is a non-throwing new, which means that it returns
117 /// null on error. (If that is what the allocator does. The current does, so if
118 /// this ever changes, this operator will have to be changed, too.)
119 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
120 /// @code
121 /// // Default alignment (16)
122 /// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
123 /// // Specific alignment
124 /// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
125 /// @endcode
126 /// Please note that you cannot use delete on the pointer; it must be
127 /// deallocated using an explicit destructor call followed by
128 /// @c Context.Deallocate(Ptr).
129 ///
130 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
131 /// @param C The MCContext that provides the allocator.
132 /// @param Alignment The alignment of the allocated memory (if the underlying
133 ///                  allocator supports it).
134 /// @return The allocated memory. Could be NULL.
135 inline void *operator new(size_t Bytes, llvm::MCContext &C,
136                           size_t Alignment = 16) throw () {
137   return C.Allocate(Bytes, Alignment);
138 }
139 /// @brief Placement delete companion to the new above.
140 ///
141 /// This operator is just a companion to the new above. There is no way of
142 /// invoking it directly; see the new operator for more details. This operator
143 /// is called implicitly by the compiler if a placement new expression using
144 /// the MCContext throws in the object constructor.
145 inline void operator delete(void *Ptr, llvm::MCContext &C, size_t)
146               throw () {
147   C.Deallocate(Ptr);
148 }
149
150 /// This placement form of operator new[] uses the MCContext's allocator for
151 /// obtaining memory. It is a non-throwing new[], which means that it returns
152 /// null on error.
153 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
154 /// @code
155 /// // Default alignment (16)
156 /// char *data = new (Context) char[10];
157 /// // Specific alignment
158 /// char *data = new (Context, 8) char[10];
159 /// @endcode
160 /// Please note that you cannot use delete on the pointer; it must be
161 /// deallocated using an explicit destructor call followed by
162 /// @c Context.Deallocate(Ptr).
163 ///
164 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
165 /// @param C The MCContext that provides the allocator.
166 /// @param Alignment The alignment of the allocated memory (if the underlying
167 ///                  allocator supports it).
168 /// @return The allocated memory. Could be NULL.
169 inline void *operator new[](size_t Bytes, llvm::MCContext& C,
170                             size_t Alignment = 16) throw () {
171   return C.Allocate(Bytes, Alignment);
172 }
173
174 /// @brief Placement delete[] companion to the new[] above.
175 ///
176 /// This operator is just a companion to the new[] above. There is no way of
177 /// invoking it directly; see the new[] operator for more details. This operator
178 /// is called implicitly by the compiler if a placement new[] expression using
179 /// the MCContext throws in the object constructor.
180 inline void operator delete[](void *Ptr, llvm::MCContext &C) throw () {
181   C.Deallocate(Ptr);
182 }
183
184 #endif