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