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