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