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