Daniel Dunbar | 25e0d8f | 2009-06-23 20:24:17 +0000 | [diff] [blame] | 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 | |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/DenseMap.h" |
| 14 | #include "llvm/ADT/StringMap.h" |
| 15 | #include "llvm/Support/Allocator.h" |
| 16 | |
Daniel Dunbar | 25e0d8f | 2009-06-23 20:24:17 +0000 | [diff] [blame] | 17 | namespace llvm { |
Daniel Dunbar | e579849 | 2009-10-16 01:33:11 +0000 | [diff] [blame] | 18 | class MCExpr; |
Daniel Dunbar | 25e0d8f | 2009-06-23 20:24:17 +0000 | [diff] [blame] | 19 | class MCSection; |
| 20 | class MCSymbol; |
Daniel Dunbar | b5261eb | 2009-07-27 21:22:30 +0000 | [diff] [blame] | 21 | class StringRef; |
Chris Lattner | 7c5b021 | 2009-10-19 22:49:00 +0000 | [diff] [blame] | 22 | class Twine; |
Daniel Dunbar | 25e0d8f | 2009-06-23 20:24:17 +0000 | [diff] [blame] | 23 | |
Chris Lattner | c9d3152 | 2009-08-13 00:21:53 +0000 | [diff] [blame] | 24 | /// MCContext - Context object for machine code objects. This class owns all |
| 25 | /// of the sections that it creates. |
| 26 | /// |
Daniel Dunbar | 25e0d8f | 2009-06-23 20:24:17 +0000 | [diff] [blame] | 27 | class MCContext { |
| 28 | MCContext(const MCContext&); // DO NOT IMPLEMENT |
| 29 | MCContext &operator=(const MCContext&); // DO NOT IMPLEMENT |
| 30 | |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 31 | /// Sections - Bindings of names to allocated sections. |
| 32 | StringMap<MCSection*> Sections; |
| 33 | |
| 34 | /// Symbols - Bindings of names to symbols. |
| 35 | StringMap<MCSymbol*> Symbols; |
| 36 | |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 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; |
Daniel Dunbar | 25e0d8f | 2009-06-23 20:24:17 +0000 | [diff] [blame] | 42 | public: |
| 43 | MCContext(); |
| 44 | ~MCContext(); |
Daniel Dunbar | 869a5e7 | 2009-08-31 08:07:08 +0000 | [diff] [blame] | 45 | |
| 46 | /// @name Symbol Managment |
| 47 | /// @{ |
| 48 | |
Dan Gohman | fb76fe0 | 2010-02-22 04:10:52 +0000 | [diff] [blame^] | 49 | /// CreateSymbol - Create a new symbol with the specified @p Name. |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 50 | /// |
| 51 | /// @param Name - The symbol name, which must be unique across all symbols. |
Daniel Dunbar | 2928c83 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 52 | MCSymbol *CreateSymbol(StringRef Name); |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 53 | |
Chris Lattner | c69485e | 2009-06-24 04:31:49 +0000 | [diff] [blame] | 54 | /// GetOrCreateSymbol - Lookup the symbol inside with the specified |
Dan Gohman | fb76fe0 | 2010-02-22 04:10:52 +0000 | [diff] [blame^] | 55 | /// @p Name. If it exists, return it. If not, create a forward |
Chris Lattner | c69485e | 2009-06-24 04:31:49 +0000 | [diff] [blame] | 56 | /// reference and return it. |
| 57 | /// |
| 58 | /// @param Name - The symbol name, which must be unique across all symbols. |
Daniel Dunbar | 2928c83 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 59 | MCSymbol *GetOrCreateSymbol(StringRef Name); |
Chris Lattner | 7c5b021 | 2009-10-19 22:49:00 +0000 | [diff] [blame] | 60 | MCSymbol *GetOrCreateSymbol(const Twine &Name); |
Daniel Dunbar | e579849 | 2009-10-16 01:33:11 +0000 | [diff] [blame] | 61 | |
Daniel Dunbar | 71d259b | 2009-06-24 17:00:42 +0000 | [diff] [blame] | 62 | /// CreateTemporarySymbol - Create a new temporary symbol with the specified |
Dan Gohman | fb76fe0 | 2010-02-22 04:10:52 +0000 | [diff] [blame^] | 63 | /// @p Name. |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 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. |
Daniel Dunbar | 2928c83 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 68 | MCSymbol *CreateTemporarySymbol(StringRef Name = ""); |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 69 | |
Dan Gohman | fb76fe0 | 2010-02-22 04:10:52 +0000 | [diff] [blame^] | 70 | /// LookupSymbol - Get the symbol for \p Name, or null. |
Daniel Dunbar | 2928c83 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 71 | MCSymbol *LookupSymbol(StringRef Name) const; |
Daniel Dunbar | 25e0d8f | 2009-06-23 20:24:17 +0000 | [diff] [blame] | 72 | |
Daniel Dunbar | 869a5e7 | 2009-08-31 08:07:08 +0000 | [diff] [blame] | 73 | /// @} |
Daniel Dunbar | 869a5e7 | 2009-08-31 08:07:08 +0000 | [diff] [blame] | 74 | |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 75 | void *Allocate(unsigned Size, unsigned Align = 8) { |
| 76 | return Allocator.Allocate(Size, Align); |
| 77 | } |
Daniel Dunbar | e579849 | 2009-10-16 01:33:11 +0000 | [diff] [blame] | 78 | void Deallocate(void *Ptr) { |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 79 | } |
Daniel Dunbar | 25e0d8f | 2009-06-23 20:24:17 +0000 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | } // end namespace llvm |
| 83 | |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 84 | // operator new and delete aren't allowed inside namespaces. |
| 85 | // The throw specifications are mandated by the standard. |
| 86 | /// @brief Placement new for using the MCContext's allocator. |
| 87 | /// |
| 88 | /// This placement form of operator new uses the MCContext's allocator for |
| 89 | /// obtaining memory. It is a non-throwing new, which means that it returns |
| 90 | /// null on error. (If that is what the allocator does. The current does, so if |
| 91 | /// this ever changes, this operator will have to be changed, too.) |
| 92 | /// Usage looks like this (assuming there's an MCContext 'Context' in scope): |
| 93 | /// @code |
| 94 | /// // Default alignment (16) |
| 95 | /// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments); |
| 96 | /// // Specific alignment |
| 97 | /// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments); |
| 98 | /// @endcode |
| 99 | /// Please note that you cannot use delete on the pointer; it must be |
| 100 | /// deallocated using an explicit destructor call followed by |
| 101 | /// @c Context.Deallocate(Ptr). |
| 102 | /// |
| 103 | /// @param Bytes The number of bytes to allocate. Calculated by the compiler. |
| 104 | /// @param C The MCContext that provides the allocator. |
| 105 | /// @param Alignment The alignment of the allocated memory (if the underlying |
| 106 | /// allocator supports it). |
| 107 | /// @return The allocated memory. Could be NULL. |
| 108 | inline void *operator new(size_t Bytes, llvm::MCContext &C, |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 109 | size_t Alignment = 16) throw () { |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 110 | return C.Allocate(Bytes, Alignment); |
| 111 | } |
| 112 | /// @brief Placement delete companion to the new above. |
| 113 | /// |
| 114 | /// This operator is just a companion to the new above. There is no way of |
| 115 | /// invoking it directly; see the new operator for more details. This operator |
| 116 | /// is called implicitly by the compiler if a placement new expression using |
| 117 | /// the MCContext throws in the object constructor. |
| 118 | inline void operator delete(void *Ptr, llvm::MCContext &C, size_t) |
| 119 | throw () { |
| 120 | C.Deallocate(Ptr); |
| 121 | } |
| 122 | |
| 123 | /// This placement form of operator new[] uses the MCContext's allocator for |
| 124 | /// obtaining memory. It is a non-throwing new[], which means that it returns |
| 125 | /// null on error. |
| 126 | /// Usage looks like this (assuming there's an MCContext 'Context' in scope): |
| 127 | /// @code |
| 128 | /// // Default alignment (16) |
| 129 | /// char *data = new (Context) char[10]; |
| 130 | /// // Specific alignment |
| 131 | /// char *data = new (Context, 8) char[10]; |
| 132 | /// @endcode |
| 133 | /// Please note that you cannot use delete on the pointer; it must be |
| 134 | /// deallocated using an explicit destructor call followed by |
| 135 | /// @c Context.Deallocate(Ptr). |
| 136 | /// |
| 137 | /// @param Bytes The number of bytes to allocate. Calculated by the compiler. |
| 138 | /// @param C The MCContext that provides the allocator. |
| 139 | /// @param Alignment The alignment of the allocated memory (if the underlying |
| 140 | /// allocator supports it). |
| 141 | /// @return The allocated memory. Could be NULL. |
| 142 | inline void *operator new[](size_t Bytes, llvm::MCContext& C, |
| 143 | size_t Alignment = 16) throw () { |
| 144 | return C.Allocate(Bytes, Alignment); |
| 145 | } |
| 146 | |
| 147 | /// @brief Placement delete[] companion to the new[] above. |
| 148 | /// |
| 149 | /// This operator is just a companion to the new[] above. There is no way of |
| 150 | /// invoking it directly; see the new[] operator for more details. This operator |
| 151 | /// is called implicitly by the compiler if a placement new[] expression using |
| 152 | /// the MCContext throws in the object constructor. |
| 153 | inline void operator delete[](void *Ptr, llvm::MCContext &C) throw () { |
| 154 | C.Deallocate(Ptr); |
| 155 | } |
| 156 | |
Daniel Dunbar | 25e0d8f | 2009-06-23 20:24:17 +0000 | [diff] [blame] | 157 | #endif |