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