MCContext.h revision e579849652f2ba062e6c91a3af4d9a3843411b44
1effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch//===- MCContext.h - Machine Code Context -----------------------*- C++ -*-===//
2effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch//
3effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch//                     The LLVM Compiler Infrastructure
4effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch//
5effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// This file is distributed under the University of Illinois Open Source
6effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// License. See LICENSE.TXT for details.
7effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch//
8effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch//===----------------------------------------------------------------------===//
9effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
10effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#ifndef LLVM_MC_MCCONTEXT_H
11effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#define LLVM_MC_MCCONTEXT_H
12effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
13effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "llvm/ADT/DenseMap.h"
14effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "llvm/ADT/StringMap.h"
151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "llvm/Support/Allocator.h"
16effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
17effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochnamespace llvm {
18effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  class MCExpr;
19effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  class MCSection;
20effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  class MCSymbol;
21effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  class StringRef;
22cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
23cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  /// MCContext - Context object for machine code objects.  This class owns all
24effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  /// of the sections that it creates.
25effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  ///
26effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  class MCContext {
27116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    MCContext(const MCContext&); // DO NOT IMPLEMENT
28effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    MCContext &operator=(const MCContext&); // DO NOT IMPLEMENT
29effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
30effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    /// Sections - Bindings of names to allocated sections.
31effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    StringMap<MCSection*> Sections;
32effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
33effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    /// Symbols - Bindings of names to symbols.
34effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    StringMap<MCSymbol*> Symbols;
35effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
36effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    /// SymbolValues - Bindings of symbols to values.
37effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    //
38effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    // FIXME: Is there a good reason to not just put this in the MCSymbol?
39effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    DenseMap<const MCSymbol*, const MCExpr*> SymbolValues;
405f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
416e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    /// Allocator - Allocator object used for creating machine code objects.
425c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    ///
43effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    /// We use a bump pointer allocator to avoid the need to track all allocated
44effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    /// objects.
45effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    BumpPtrAllocator Allocator;
46effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  public:
47effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    MCContext();
48effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    ~MCContext();
49effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
50effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    /// @name Symbol Managment
51effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    /// @{
52effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
53effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    /// CreateSymbol - Create a new symbol with the specified @param Name.
54effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    ///
55effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    /// @param Name - The symbol name, which must be unique across all symbols.
56effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    MCSymbol *CreateSymbol(const StringRef &Name);
57effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
58effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    /// GetOrCreateSymbol - Lookup the symbol inside with the specified
59effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    /// @param Name.  If it exists, return it.  If not, create a forward
60effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    /// reference and return it.
61effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    ///
62effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    /// @param Name - The symbol name, which must be unique across all symbols.
63effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    /// @param IsTemporary - Whether this symbol is an assembler temporary,
64effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    /// which should not survive into the symbol table for the translation unit.
655f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    MCSymbol *GetOrCreateSymbol(const StringRef &Name);
666e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
67effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    /// CreateTemporarySymbol - Create a new temporary symbol with the specified
68effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    /// @param Name.
69effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    ///
70effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    /// @param Name - The symbol name, for debugging purposes only, temporary
71effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    /// symbols do not surive assembly. If non-empty the name must be unique
72effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    /// across all symbols.
73effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    MCSymbol *CreateTemporarySymbol(const StringRef &Name = "");
74effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
75effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    /// LookupSymbol - Get the symbol for @param Name, or null.
76effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    MCSymbol *LookupSymbol(const StringRef &Name) const;
77effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
78effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    /// @}
79effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    /// @name Symbol Value Table
80effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    /// @{
81effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
82effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    /// ClearSymbolValue - Erase the variable binding for @arg Symbol, if one
83effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    /// exists.
84effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    void ClearSymbolValue(const MCSymbol *Symbol) {
85effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      SymbolValues.erase(Symbol);
86effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    }
87effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
88effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    /// SetSymbolValue - Set the variable binding for @arg Symbol to @arg Value.
89effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    void SetSymbolValue(const MCSymbol *Symbol, const MCExpr *Value) {
90116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      assert(Value && "Invalid variable assignment!");
91effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      SymbolValues.insert(std::make_pair(Symbol, Value));
92effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    }
93effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
94effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    /// GetSymbolValue - Return the current variable value for @arg Symbol, or
95effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    /// null if @arg Symbol is not a variable.
96effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    const MCExpr *GetSymbolValue(const MCSymbol *Symbol) const {
97effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      return SymbolValues.lookup(Symbol);
98effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    }
99effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
100effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    /// @}
101effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
102effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    void *Allocate(unsigned Size, unsigned Align = 8) {
103effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      return Allocator.Allocate(Size, Align);
104effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    }
105effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    void Deallocate(void *Ptr) {
106effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    }
107effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  };
108effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
109effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch} // end namespace llvm
110effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
111effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// operator new and delete aren't allowed inside namespaces.
112effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch// The throw specifications are mandated by the standard.
113effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// @brief Placement new for using the MCContext's allocator.
114effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch///
115effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// This placement form of operator new uses the MCContext's allocator for
116effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// obtaining memory. It is a non-throwing new, which means that it returns
117effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// null on error. (If that is what the allocator does. The current does, so if
118effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// this ever changes, this operator will have to be changed, too.)
119effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// Usage looks like this (assuming there's an MCContext 'Context' in scope):
120effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// @code
121effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// // Default alignment (16)
122effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
123effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// // Specific alignment
124effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
125effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// @endcode
126effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// Please note that you cannot use delete on the pointer; it must be
127effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// deallocated using an explicit destructor call followed by
128effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// @c Context.Deallocate(Ptr).
129effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch///
130effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// @param Bytes The number of bytes to allocate. Calculated by the compiler.
131effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// @param C The MCContext that provides the allocator.
132effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// @param Alignment The alignment of the allocated memory (if the underlying
133cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)///                  allocator supports it).
134effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// @return The allocated memory. Could be NULL.
135effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochinline void *operator new(size_t Bytes, llvm::MCContext &C,
136effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                          size_t Alignment = 16) throw () {
137effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  return C.Allocate(Bytes, Alignment);
138effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
139effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// @brief Placement delete companion to the new above.
140effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch///
141effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// This operator is just a companion to the new above. There is no way of
142effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// invoking it directly; see the new operator for more details. This operator
143effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// is called implicitly by the compiler if a placement new expression using
144effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// the MCContext throws in the object constructor.
145effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochinline void operator delete(void *Ptr, llvm::MCContext &C, size_t)
146effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch              throw () {
147effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  C.Deallocate(Ptr);
148effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
149effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
150effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// This placement form of operator new[] uses the MCContext's allocator for
151effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// obtaining memory. It is a non-throwing new[], which means that it returns
152effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// null on error.
153effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// Usage looks like this (assuming there's an MCContext 'Context' in scope):
154effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// @code
155effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// // Default alignment (16)
156effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// char *data = new (Context) char[10];
157effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// // Specific alignment
158effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// char *data = new (Context, 8) char[10];
159effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// @endcode
160effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// Please note that you cannot use delete on the pointer; it must be
161effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// deallocated using an explicit destructor call followed by
162effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// @c Context.Deallocate(Ptr).
163effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch///
164effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// @param Bytes The number of bytes to allocate. Calculated by the compiler.
165effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// @param C The MCContext that provides the allocator.
166effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// @param Alignment The alignment of the allocated memory (if the underlying
1671320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci///                  allocator supports it).
1681320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci/// @return The allocated memory. Could be NULL.
1691320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciinline void *operator new[](size_t Bytes, llvm::MCContext& C,
170effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                            size_t Alignment = 16) throw () {
171effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  return C.Allocate(Bytes, Alignment);
172effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
173effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
174effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// @brief Placement delete[] companion to the new[] above.
175effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch///
176effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// This operator is just a companion to the new[] above. There is no way of
177effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// invoking it directly; see the new[] operator for more details. This operator
178effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// is called implicitly by the compiler if a placement new[] expression using
179effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch/// the MCContext throws in the object constructor.
180effb81e5f8246d0db0270817048dc992db66e9fbBen Murdochinline void operator delete[](void *Ptr, llvm::MCContext &C) throw () {
181effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  C.Deallocate(Ptr);
182effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch}
183effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
184effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#endif
185effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch