MC: Lift SwitchSection() and Finish() into MCObjectStreamer.
[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/MC/SectionKind.h"
14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/ADT/StringMap.h"
16 #include "llvm/Support/Allocator.h"
17
18 namespace llvm {
19   class MCAsmInfo;
20   class MCExpr;
21   class MCSection;
22   class MCSymbol;
23   class MCLabel;
24   class StringRef;
25   class Twine;
26   class MCSectionMachO;
27
28   /// MCContext - Context object for machine code objects.  This class owns all
29   /// of the sections that it creates.
30   ///
31   class MCContext {
32     MCContext(const MCContext&); // DO NOT IMPLEMENT
33     MCContext &operator=(const MCContext&); // DO NOT IMPLEMENT
34
35     /// The MCAsmInfo for this target.
36     const MCAsmInfo &MAI;
37     
38     /// Sections - Bindings of names to allocated sections.
39     StringMap<MCSection*> Sections;
40
41     /// Symbols - Bindings of names to symbols.
42     StringMap<MCSymbol*> Symbols;
43
44     /// NextUniqueID - The next ID to dole out to an unnamed assembler temporary
45     /// symbol.
46     unsigned NextUniqueID;
47
48     /// Instances of directional local labels.
49     DenseMap<unsigned, MCLabel *> Instances;
50     /// NextInstance() creates the next instance of the directional local label
51     /// for the LocalLabelVal and adds it to the map if needed.
52     unsigned NextInstance(int64_t LocalLabelVal);
53     /// GetInstance() gets the current instance of the directional local label
54     /// for the LocalLabelVal and adds it to the map if needed.
55     unsigned GetInstance(int64_t LocalLabelVal);
56     
57     /// Allocator - Allocator object used for creating machine code objects.
58     ///
59     /// We use a bump pointer allocator to avoid the need to track all allocated
60     /// objects.
61     BumpPtrAllocator Allocator;
62     
63     void *MachOUniquingMap, *ELFUniquingMap, *COFFUniquingMap;
64   public:
65     explicit MCContext(const MCAsmInfo &MAI);
66     ~MCContext();
67     
68     const MCAsmInfo &getAsmInfo() const { return MAI; }
69
70     /// @name Symbol Managment
71     /// @{
72     
73     /// CreateTempSymbol - Create and return a new assembler temporary symbol
74     /// with a unique but unspecified name.
75     MCSymbol *CreateTempSymbol();
76
77     /// CreateDirectionalLocalSymbol - Create the defintion of a directional
78     /// local symbol for numbered label (used for "1:" defintions).
79     MCSymbol *CreateDirectionalLocalSymbol(int64_t LocalLabelVal);
80
81     /// GetDirectionalLocalSymbol - Create and return a directional local
82     /// symbol for numbered label (used for "1b" or 1f" references).
83     MCSymbol *GetDirectionalLocalSymbol(int64_t LocalLabelVal, int bORf);
84
85     /// GetOrCreateSymbol - Lookup the symbol inside with the specified
86     /// @p Name.  If it exists, return it.  If not, create a forward
87     /// reference and return it.
88     ///
89     /// @param Name - The symbol name, which must be unique across all symbols.
90     MCSymbol *GetOrCreateSymbol(StringRef Name);
91     MCSymbol *GetOrCreateSymbol(const Twine &Name);
92
93     /// LookupSymbol - Get the symbol for \p Name, or null.
94     MCSymbol *LookupSymbol(StringRef Name) const;
95
96     /// @}
97     
98     /// @name Section Managment
99     /// @{
100
101     /// getMachOSection - Return the MCSection for the specified mach-o section.
102     /// This requires the operands to be valid.
103     const MCSectionMachO *getMachOSection(StringRef Segment,
104                                           StringRef Section,
105                                           unsigned TypeAndAttributes,
106                                           unsigned Reserved2,
107                                           SectionKind K);
108     const MCSectionMachO *getMachOSection(StringRef Segment,
109                                           StringRef Section,
110                                           unsigned TypeAndAttributes,
111                                           SectionKind K) {
112       return getMachOSection(Segment, Section, TypeAndAttributes, 0, K);
113     }
114     
115     const MCSection *getELFSection(StringRef Section, unsigned Type,
116                                    unsigned Flags, SectionKind Kind,
117                                    bool IsExplicit = false);
118
119     const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
120                                     int Selection, SectionKind Kind);
121
122     const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
123                                     SectionKind Kind) {
124       return getCOFFSection (Section, Characteristics, 0, Kind);
125     }
126
127     
128     /// @}
129
130     void *Allocate(unsigned Size, unsigned Align = 8) {
131       return Allocator.Allocate(Size, Align);
132     }
133     void Deallocate(void *Ptr) {
134     }
135   };
136
137 } // end namespace llvm
138
139 // operator new and delete aren't allowed inside namespaces.
140 // The throw specifications are mandated by the standard.
141 /// @brief Placement new for using the MCContext's allocator.
142 ///
143 /// This placement form of operator new uses the MCContext's allocator for
144 /// obtaining memory. It is a non-throwing new, which means that it returns
145 /// null on error. (If that is what the allocator does. The current does, so if
146 /// this ever changes, this operator will have to be changed, too.)
147 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
148 /// @code
149 /// // Default alignment (16)
150 /// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
151 /// // Specific alignment
152 /// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
153 /// @endcode
154 /// Please note that you cannot use delete on the pointer; it must be
155 /// deallocated using an explicit destructor call followed by
156 /// @c Context.Deallocate(Ptr).
157 ///
158 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
159 /// @param C The MCContext that provides the allocator.
160 /// @param Alignment The alignment of the allocated memory (if the underlying
161 ///                  allocator supports it).
162 /// @return The allocated memory. Could be NULL.
163 inline void *operator new(size_t Bytes, llvm::MCContext &C,
164                           size_t Alignment = 16) throw () {
165   return C.Allocate(Bytes, Alignment);
166 }
167 /// @brief Placement delete companion to the new above.
168 ///
169 /// This operator is just a companion to the new above. There is no way of
170 /// invoking it directly; see the new operator for more details. This operator
171 /// is called implicitly by the compiler if a placement new expression using
172 /// the MCContext throws in the object constructor.
173 inline void operator delete(void *Ptr, llvm::MCContext &C, size_t)
174               throw () {
175   C.Deallocate(Ptr);
176 }
177
178 /// This placement form of operator new[] uses the MCContext's allocator for
179 /// obtaining memory. It is a non-throwing new[], which means that it returns
180 /// null on error.
181 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
182 /// @code
183 /// // Default alignment (16)
184 /// char *data = new (Context) char[10];
185 /// // Specific alignment
186 /// char *data = new (Context, 8) char[10];
187 /// @endcode
188 /// Please note that you cannot use delete on the pointer; it must be
189 /// deallocated using an explicit destructor call followed by
190 /// @c Context.Deallocate(Ptr).
191 ///
192 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
193 /// @param C The MCContext that provides the allocator.
194 /// @param Alignment The alignment of the allocated memory (if the underlying
195 ///                  allocator supports it).
196 /// @return The allocated memory. Could be NULL.
197 inline void *operator new[](size_t Bytes, llvm::MCContext& C,
198                             size_t Alignment = 16) throw () {
199   return C.Allocate(Bytes, Alignment);
200 }
201
202 /// @brief Placement delete[] companion to the new[] above.
203 ///
204 /// This operator is just a companion to the new[] above. There is no way of
205 /// invoking it directly; see the new[] operator for more details. This operator
206 /// is called implicitly by the compiler if a placement new[] expression using
207 /// the MCContext throws in the object constructor.
208 inline void operator delete[](void *Ptr, llvm::MCContext &C) throw () {
209   C.Deallocate(Ptr);
210 }
211
212 #endif