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