Initial support for the cfi directives. This is just enough to get
[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/MC/MCDwarf.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/StringMap.h"
17 #include "llvm/Support/Allocator.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include <vector> // FIXME: Shouldn't be needed.
20
21 namespace llvm {
22   class MCAsmInfo;
23   class MCExpr;
24   class MCSection;
25   class MCSymbol;
26   class MCLabel;
27   class MCDwarfFile;
28   class MCDwarfLoc;
29   class MCLineSection;
30   class StringRef;
31   class Twine;
32   class TargetAsmInfo;
33   class MCSectionMachO;
34   class MCSectionELF;
35
36   /// MCContext - Context object for machine code objects.  This class owns all
37   /// of the sections that it creates.
38   ///
39   class MCContext {
40     MCContext(const MCContext&); // DO NOT IMPLEMENT
41     MCContext &operator=(const MCContext&); // DO NOT IMPLEMENT
42
43     /// The MCAsmInfo for this target.
44     const MCAsmInfo &MAI;
45
46     const TargetAsmInfo *TAI;
47
48     /// Symbols - Bindings of names to symbols.
49     StringMap<MCSymbol*> Symbols;
50
51     /// UsedNames - Keeps tracks of names that were used both for used declared
52     /// and artificial symbols.
53     StringMap<bool> UsedNames;
54
55     /// NextUniqueID - The next ID to dole out to an unnamed assembler temporary
56     /// symbol.
57     unsigned NextUniqueID;
58
59     /// Instances of directional local labels.
60     DenseMap<unsigned, MCLabel *> Instances;
61     /// NextInstance() creates the next instance of the directional local label
62     /// for the LocalLabelVal and adds it to the map if needed.
63     unsigned NextInstance(int64_t LocalLabelVal);
64     /// GetInstance() gets the current instance of the directional local label
65     /// for the LocalLabelVal and adds it to the map if needed.
66     unsigned GetInstance(int64_t LocalLabelVal);
67
68     /// The file name of the log file from the environment variable
69     /// AS_SECURE_LOG_FILE.  Which must be set before the .secure_log_unique
70     /// directive is used or it is an error.
71     char *SecureLogFile;
72     /// The stream that gets written to for the .secure_log_unique directive.
73     raw_ostream *SecureLog;
74     /// Boolean toggled when .secure_log_unique / .secure_log_reset is seen to
75     /// catch errors if .secure_log_unique appears twice without
76     /// .secure_log_reset appearing between them.
77     bool SecureLogUsed;
78
79     /// The dwarf file and directory tables from the dwarf .file directive.
80     std::vector<MCDwarfFile *> MCDwarfFiles;
81     std::vector<StringRef> MCDwarfDirs;
82
83     /// The current dwarf line information from the last dwarf .loc directive.
84     MCDwarfLoc CurrentDwarfLoc;
85     bool DwarfLocSeen;
86
87     /// The dwarf line information from the .loc directives for the sections
88     /// with assembled machine instructions have after seeing .loc directives.
89     DenseMap<const MCSection *, MCLineSection *> MCLineSections;
90     /// We need a deterministic iteration order, so we remember the order
91     /// the elements were added.
92     std::vector<const MCSection *> MCLineSectionOrder;
93
94     /// Allocator - Allocator object used for creating machine code objects.
95     ///
96     /// We use a bump pointer allocator to avoid the need to track all allocated
97     /// objects.
98     BumpPtrAllocator Allocator;
99
100     void *MachOUniquingMap, *ELFUniquingMap, *COFFUniquingMap;
101
102     MCSymbol *CreateSymbol(StringRef Name);
103
104   public:
105     explicit MCContext(const MCAsmInfo &MAI, const TargetAsmInfo *TAI);
106     ~MCContext();
107
108     const MCAsmInfo &getAsmInfo() const { return MAI; }
109
110     const TargetAsmInfo &getTargetAsmInfo() const { return *TAI; }
111
112     /// @name Symbol Management
113     /// @{
114
115     /// CreateTempSymbol - Create and return a new assembler temporary symbol
116     /// with a unique but unspecified name.
117     MCSymbol *CreateTempSymbol();
118
119     /// CreateDirectionalLocalSymbol - Create the definition of a directional
120     /// local symbol for numbered label (used for "1:" definitions).
121     MCSymbol *CreateDirectionalLocalSymbol(int64_t LocalLabelVal);
122
123     /// GetDirectionalLocalSymbol - Create and return a directional local
124     /// symbol for numbered label (used for "1b" or 1f" references).
125     MCSymbol *GetDirectionalLocalSymbol(int64_t LocalLabelVal, int bORf);
126
127     /// GetOrCreateSymbol - Lookup the symbol inside with the specified
128     /// @p Name.  If it exists, return it.  If not, create a forward
129     /// reference and return it.
130     ///
131     /// @param Name - The symbol name, which must be unique across all symbols.
132     MCSymbol *GetOrCreateSymbol(StringRef Name);
133     MCSymbol *GetOrCreateSymbol(const Twine &Name);
134
135     /// LookupSymbol - Get the symbol for \p Name, or null.
136     MCSymbol *LookupSymbol(StringRef Name) const;
137
138     /// @}
139
140     /// @name Section Management
141     /// @{
142
143     /// getMachOSection - Return the MCSection for the specified mach-o section.
144     /// This requires the operands to be valid.
145     const MCSectionMachO *getMachOSection(StringRef Segment,
146                                           StringRef Section,
147                                           unsigned TypeAndAttributes,
148                                           unsigned Reserved2,
149                                           SectionKind K);
150     const MCSectionMachO *getMachOSection(StringRef Segment,
151                                           StringRef Section,
152                                           unsigned TypeAndAttributes,
153                                           SectionKind K) {
154       return getMachOSection(Segment, Section, TypeAndAttributes, 0, K);
155     }
156
157     const MCSectionELF *getELFSection(StringRef Section, unsigned Type,
158                                       unsigned Flags, SectionKind Kind);
159
160     const MCSectionELF *getELFSection(StringRef Section, unsigned Type,
161                                       unsigned Flags, SectionKind Kind,
162                                       unsigned EntrySize, StringRef Group);
163
164     const MCSectionELF *CreateELFGroupSection();
165
166     const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
167                                     int Selection, SectionKind Kind);
168
169     const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
170                                     SectionKind Kind) {
171       return getCOFFSection (Section, Characteristics, 0, Kind);
172     }
173
174
175     /// @}
176
177     /// @name Dwarf Management
178     /// @{
179
180     /// GetDwarfFile - creates an entry in the dwarf file and directory tables.
181     unsigned GetDwarfFile(StringRef FileName, unsigned FileNumber);
182
183     bool isValidDwarfFileNumber(unsigned FileNumber);
184
185     bool hasDwarfFiles() const {
186       return !MCDwarfFiles.empty();
187     }
188
189     const std::vector<MCDwarfFile *> &getMCDwarfFiles() {
190       return MCDwarfFiles;
191     }
192     const std::vector<StringRef> &getMCDwarfDirs() {
193       return MCDwarfDirs;
194     }
195
196     const DenseMap<const MCSection *, MCLineSection *>
197     &getMCLineSections() const {
198       return MCLineSections;
199     }
200     const std::vector<const MCSection *> &getMCLineSectionOrder() const {
201       return MCLineSectionOrder;
202     }
203     void addMCLineSection(const MCSection *Sec, MCLineSection *Line) {
204       MCLineSections[Sec] = Line;
205       MCLineSectionOrder.push_back(Sec);
206     }
207
208     /// setCurrentDwarfLoc - saves the information from the currently parsed
209     /// dwarf .loc directive and sets DwarfLocSeen.  When the next instruction
210     /// is assembled an entry in the line number table with this information and
211     /// the address of the instruction will be created.
212     void setCurrentDwarfLoc(unsigned FileNum, unsigned Line, unsigned Column,
213                             unsigned Flags, unsigned Isa,
214                             unsigned Discriminator) {
215       CurrentDwarfLoc.setFileNum(FileNum);
216       CurrentDwarfLoc.setLine(Line);
217       CurrentDwarfLoc.setColumn(Column);
218       CurrentDwarfLoc.setFlags(Flags);
219       CurrentDwarfLoc.setIsa(Isa);
220       CurrentDwarfLoc.setDiscriminator(Discriminator);
221       DwarfLocSeen = true;
222     }
223     void ClearDwarfLocSeen() { DwarfLocSeen = false; }
224
225     bool getDwarfLocSeen() { return DwarfLocSeen; }
226     const MCDwarfLoc &getCurrentDwarfLoc() { return CurrentDwarfLoc; }
227
228     /// @}
229
230     char *getSecureLogFile() { return SecureLogFile; }
231     raw_ostream *getSecureLog() { return SecureLog; }
232     bool getSecureLogUsed() { return SecureLogUsed; }
233     void setSecureLog(raw_ostream *Value) {
234       SecureLog = Value;
235     }
236     void setSecureLogUsed(bool Value) {
237       SecureLogUsed = Value;
238     }
239
240     void *Allocate(unsigned Size, unsigned Align = 8) {
241       return Allocator.Allocate(Size, Align);
242     }
243     void Deallocate(void *Ptr) {
244     }
245   };
246
247 } // end namespace llvm
248
249 // operator new and delete aren't allowed inside namespaces.
250 // The throw specifications are mandated by the standard.
251 /// @brief Placement new for using the MCContext's allocator.
252 ///
253 /// This placement form of operator new uses the MCContext's allocator for
254 /// obtaining memory. It is a non-throwing new, which means that it returns
255 /// null on error. (If that is what the allocator does. The current does, so if
256 /// this ever changes, this operator will have to be changed, too.)
257 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
258 /// @code
259 /// // Default alignment (16)
260 /// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
261 /// // Specific alignment
262 /// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
263 /// @endcode
264 /// Please note that you cannot use delete on the pointer; it must be
265 /// deallocated using an explicit destructor call followed by
266 /// @c Context.Deallocate(Ptr).
267 ///
268 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
269 /// @param C The MCContext that provides the allocator.
270 /// @param Alignment The alignment of the allocated memory (if the underlying
271 ///                  allocator supports it).
272 /// @return The allocated memory. Could be NULL.
273 inline void *operator new(size_t Bytes, llvm::MCContext &C,
274                           size_t Alignment = 16) throw () {
275   return C.Allocate(Bytes, Alignment);
276 }
277 /// @brief Placement delete companion to the new above.
278 ///
279 /// This operator is just a companion to the new above. There is no way of
280 /// invoking it directly; see the new operator for more details. This operator
281 /// is called implicitly by the compiler if a placement new expression using
282 /// the MCContext throws in the object constructor.
283 inline void operator delete(void *Ptr, llvm::MCContext &C, size_t)
284               throw () {
285   C.Deallocate(Ptr);
286 }
287
288 /// This placement form of operator new[] uses the MCContext's allocator for
289 /// obtaining memory. It is a non-throwing new[], which means that it returns
290 /// null on error.
291 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
292 /// @code
293 /// // Default alignment (16)
294 /// char *data = new (Context) char[10];
295 /// // Specific alignment
296 /// char *data = new (Context, 8) char[10];
297 /// @endcode
298 /// Please note that you cannot use delete on the pointer; it must be
299 /// deallocated using an explicit destructor call followed by
300 /// @c Context.Deallocate(Ptr).
301 ///
302 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
303 /// @param C The MCContext that provides the allocator.
304 /// @param Alignment The alignment of the allocated memory (if the underlying
305 ///                  allocator supports it).
306 /// @return The allocated memory. Could be NULL.
307 inline void *operator new[](size_t Bytes, llvm::MCContext& C,
308                             size_t Alignment = 16) throw () {
309   return C.Allocate(Bytes, Alignment);
310 }
311
312 /// @brief Placement delete[] companion to the new[] above.
313 ///
314 /// This operator is just a companion to the new[] above. There is no way of
315 /// invoking it directly; see the new[] operator for more details. This operator
316 /// is called implicitly by the compiler if a placement new[] expression using
317 /// the MCContext throws in the object constructor.
318 inline void operator delete[](void *Ptr, llvm::MCContext &C) throw () {
319   C.Deallocate(Ptr);
320 }
321
322 #endif