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