1 //===- MCContext.h - Machine Code Context -----------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_MC_MCCONTEXT_H
11 #define LLVM_MC_MCCONTEXT_H
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.
29 class MCObjectFileInfo;
37 /// MCContext - Context object for machine code objects. This class owns all
38 /// of the sections that it creates.
41 MCContext(const MCContext&); // DO NOT IMPLEMENT
42 MCContext &operator=(const MCContext&); // DO NOT IMPLEMENT
44 typedef StringMap<MCSymbol*, BumpPtrAllocator&> SymbolTable;
47 /// The MCAsmInfo for this target.
50 /// The MCRegisterInfo for this target.
51 const MCRegisterInfo &MRI;
53 /// The MCObjectFileInfo for this target.
54 const MCObjectFileInfo *MOFI;
56 /// Allocator - Allocator object used for creating machine code objects.
58 /// We use a bump pointer allocator to avoid the need to track all allocated
60 BumpPtrAllocator Allocator;
62 /// Symbols - Bindings of names to symbols.
65 /// UsedNames - Keeps tracks of names that were used both for used declared
66 /// and artificial symbols.
67 StringMap<bool, BumpPtrAllocator&> UsedNames;
69 /// NextUniqueID - The next ID to dole out to an unnamed assembler temporary
71 unsigned NextUniqueID;
73 /// Instances of directional local labels.
74 DenseMap<unsigned, MCLabel *> Instances;
75 /// NextInstance() creates the next instance of the directional local label
76 /// for the LocalLabelVal and adds it to the map if needed.
77 unsigned NextInstance(int64_t LocalLabelVal);
78 /// GetInstance() gets the current instance of the directional local label
79 /// for the LocalLabelVal and adds it to the map if needed.
80 unsigned GetInstance(int64_t LocalLabelVal);
82 /// The file name of the log file from the environment variable
83 /// AS_SECURE_LOG_FILE. Which must be set before the .secure_log_unique
84 /// directive is used or it is an error.
86 /// The stream that gets written to for the .secure_log_unique directive.
87 raw_ostream *SecureLog;
88 /// Boolean toggled when .secure_log_unique / .secure_log_reset is seen to
89 /// catch errors if .secure_log_unique appears twice without
90 /// .secure_log_reset appearing between them.
93 /// The dwarf file and directory tables from the dwarf .file directive.
94 std::vector<MCDwarfFile *> MCDwarfFiles;
95 std::vector<StringRef> MCDwarfDirs;
97 /// The current dwarf line information from the last dwarf .loc directive.
98 MCDwarfLoc CurrentDwarfLoc;
101 /// Honor temporary labels, this is useful for debugging semantic
102 /// differences between temporary and non-temporary labels (primarily on
104 bool AllowTemporaryLabels;
106 /// The dwarf line information from the .loc directives for the sections
107 /// with assembled machine instructions have after seeing .loc directives.
108 DenseMap<const MCSection *, MCLineSection *> MCLineSections;
109 /// We need a deterministic iteration order, so we remember the order
110 /// the elements were added.
111 std::vector<const MCSection *> MCLineSectionOrder;
113 void *MachOUniquingMap, *ELFUniquingMap, *COFFUniquingMap;
115 MCSymbol *CreateSymbol(StringRef Name);
118 explicit MCContext(const MCAsmInfo &MAI, const MCRegisterInfo &MRI,
119 const MCObjectFileInfo *MOFI);
122 const MCAsmInfo &getAsmInfo() const { return MAI; }
124 const MCRegisterInfo &getRegisterInfo() const { return MRI; }
126 const MCObjectFileInfo *getObjectFileInfo() const { return MOFI; }
128 void setAllowTemporaryLabels(bool Value) { AllowTemporaryLabels = Value; }
130 /// @name Symbol Management
133 /// CreateTempSymbol - Create and return a new assembler temporary symbol
134 /// with a unique but unspecified name.
135 MCSymbol *CreateTempSymbol();
137 /// CreateDirectionalLocalSymbol - Create the definition of a directional
138 /// local symbol for numbered label (used for "1:" definitions).
139 MCSymbol *CreateDirectionalLocalSymbol(int64_t LocalLabelVal);
141 /// GetDirectionalLocalSymbol - Create and return a directional local
142 /// symbol for numbered label (used for "1b" or 1f" references).
143 MCSymbol *GetDirectionalLocalSymbol(int64_t LocalLabelVal, int bORf);
145 /// GetOrCreateSymbol - Lookup the symbol inside with the specified
146 /// @p Name. If it exists, return it. If not, create a forward
147 /// reference and return it.
149 /// @param Name - The symbol name, which must be unique across all symbols.
150 MCSymbol *GetOrCreateSymbol(StringRef Name);
151 MCSymbol *GetOrCreateSymbol(const Twine &Name);
153 /// LookupSymbol - Get the symbol for \p Name, or null.
154 MCSymbol *LookupSymbol(StringRef Name) const;
156 /// getSymbols - Get a reference for the symbol table for clients that
157 /// want to, for example, iterate over all symbols. 'const' because we
158 /// still want any modifications to the table itself to use the MCContext
160 const SymbolTable &getSymbols() const {
166 /// @name Section Management
169 /// getMachOSection - Return the MCSection for the specified mach-o section.
170 /// This requires the operands to be valid.
171 const MCSectionMachO *getMachOSection(StringRef Segment,
173 unsigned TypeAndAttributes,
176 const MCSectionMachO *getMachOSection(StringRef Segment,
178 unsigned TypeAndAttributes,
180 return getMachOSection(Segment, Section, TypeAndAttributes, 0, K);
183 const MCSectionELF *getELFSection(StringRef Section, unsigned Type,
184 unsigned Flags, SectionKind Kind);
186 const MCSectionELF *getELFSection(StringRef Section, unsigned Type,
187 unsigned Flags, SectionKind Kind,
188 unsigned EntrySize, StringRef Group);
190 const MCSectionELF *CreateELFGroupSection();
192 const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
193 int Selection, SectionKind Kind);
195 const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
197 return getCOFFSection (Section, Characteristics, 0, Kind);
203 /// @name Dwarf Management
206 /// GetDwarfFile - creates an entry in the dwarf file and directory tables.
207 unsigned GetDwarfFile(StringRef Directory, StringRef FileName,
208 unsigned FileNumber);
210 bool isValidDwarfFileNumber(unsigned FileNumber);
212 bool hasDwarfFiles() const {
213 return !MCDwarfFiles.empty();
216 const std::vector<MCDwarfFile *> &getMCDwarfFiles() {
219 const std::vector<StringRef> &getMCDwarfDirs() {
223 const DenseMap<const MCSection *, MCLineSection *>
224 &getMCLineSections() const {
225 return MCLineSections;
227 const std::vector<const MCSection *> &getMCLineSectionOrder() const {
228 return MCLineSectionOrder;
230 void addMCLineSection(const MCSection *Sec, MCLineSection *Line) {
231 MCLineSections[Sec] = Line;
232 MCLineSectionOrder.push_back(Sec);
235 /// setCurrentDwarfLoc - saves the information from the currently parsed
236 /// dwarf .loc directive and sets DwarfLocSeen. When the next instruction
237 /// is assembled an entry in the line number table with this information and
238 /// the address of the instruction will be created.
239 void setCurrentDwarfLoc(unsigned FileNum, unsigned Line, unsigned Column,
240 unsigned Flags, unsigned Isa,
241 unsigned Discriminator) {
242 CurrentDwarfLoc.setFileNum(FileNum);
243 CurrentDwarfLoc.setLine(Line);
244 CurrentDwarfLoc.setColumn(Column);
245 CurrentDwarfLoc.setFlags(Flags);
246 CurrentDwarfLoc.setIsa(Isa);
247 CurrentDwarfLoc.setDiscriminator(Discriminator);
250 void ClearDwarfLocSeen() { DwarfLocSeen = false; }
252 bool getDwarfLocSeen() { return DwarfLocSeen; }
253 const MCDwarfLoc &getCurrentDwarfLoc() { return CurrentDwarfLoc; }
257 char *getSecureLogFile() { return SecureLogFile; }
258 raw_ostream *getSecureLog() { return SecureLog; }
259 bool getSecureLogUsed() { return SecureLogUsed; }
260 void setSecureLog(raw_ostream *Value) {
263 void setSecureLogUsed(bool Value) {
264 SecureLogUsed = Value;
267 void *Allocate(unsigned Size, unsigned Align = 8) {
268 return Allocator.Allocate(Size, Align);
270 void Deallocate(void *Ptr) {
274 } // end namespace llvm
276 // operator new and delete aren't allowed inside namespaces.
277 // The throw specifications are mandated by the standard.
278 /// @brief Placement new for using the MCContext's allocator.
280 /// This placement form of operator new uses the MCContext's allocator for
281 /// obtaining memory. It is a non-throwing new, which means that it returns
282 /// null on error. (If that is what the allocator does. The current does, so if
283 /// this ever changes, this operator will have to be changed, too.)
284 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
286 /// // Default alignment (16)
287 /// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
288 /// // Specific alignment
289 /// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
291 /// Please note that you cannot use delete on the pointer; it must be
292 /// deallocated using an explicit destructor call followed by
293 /// @c Context.Deallocate(Ptr).
295 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
296 /// @param C The MCContext that provides the allocator.
297 /// @param Alignment The alignment of the allocated memory (if the underlying
298 /// allocator supports it).
299 /// @return The allocated memory. Could be NULL.
300 inline void *operator new(size_t Bytes, llvm::MCContext &C,
301 size_t Alignment = 16) throw () {
302 return C.Allocate(Bytes, Alignment);
304 /// @brief Placement delete companion to the new above.
306 /// This operator is just a companion to the new above. There is no way of
307 /// invoking it directly; see the new operator for more details. This operator
308 /// is called implicitly by the compiler if a placement new expression using
309 /// the MCContext throws in the object constructor.
310 inline void operator delete(void *Ptr, llvm::MCContext &C, size_t)
315 /// This placement form of operator new[] uses the MCContext's allocator for
316 /// obtaining memory. It is a non-throwing new[], which means that it returns
318 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
320 /// // Default alignment (16)
321 /// char *data = new (Context) char[10];
322 /// // Specific alignment
323 /// char *data = new (Context, 8) char[10];
325 /// Please note that you cannot use delete on the pointer; it must be
326 /// deallocated using an explicit destructor call followed by
327 /// @c Context.Deallocate(Ptr).
329 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
330 /// @param C The MCContext that provides the allocator.
331 /// @param Alignment The alignment of the allocated memory (if the underlying
332 /// allocator supports it).
333 /// @return The allocated memory. Could be NULL.
334 inline void *operator new[](size_t Bytes, llvm::MCContext& C,
335 size_t Alignment = 16) throw () {
336 return C.Allocate(Bytes, Alignment);
339 /// @brief Placement delete[] companion to the new[] above.
341 /// This operator is just a companion to the new[] above. There is no way of
342 /// invoking it directly; see the new[] operator for more details. This operator
343 /// is called implicitly by the compiler if a placement new[] expression using
344 /// the MCContext throws in the object constructor.
345 inline void operator delete[](void *Ptr, llvm::MCContext &C) throw () {