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