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