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/Compiler.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include <vector> // FIXME: Shouldn't be needed.
30 class MCObjectFileInfo;
39 /// MCContext - Context object for machine code objects. This class owns all
40 /// of the sections that it creates.
43 MCContext(const MCContext&); // DO NOT IMPLEMENT
44 MCContext &operator=(const MCContext&); // DO NOT IMPLEMENT
46 typedef StringMap<MCSymbol*, BumpPtrAllocator&> SymbolTable;
48 /// The SourceMgr for this object, if any.
49 const SourceMgr *SrcMgr;
51 /// The MCAsmInfo for this target.
54 /// The MCRegisterInfo for this target.
55 const MCRegisterInfo &MRI;
57 /// The MCObjectFileInfo for this target.
58 const MCObjectFileInfo *MOFI;
60 /// Allocator - Allocator object used for creating machine code objects.
62 /// We use a bump pointer allocator to avoid the need to track all allocated
64 BumpPtrAllocator Allocator;
66 /// Symbols - Bindings of names to symbols.
69 /// UsedNames - Keeps tracks of names that were used both for used declared
70 /// and artificial symbols.
71 StringMap<bool, BumpPtrAllocator&> UsedNames;
73 /// NextUniqueID - The next ID to dole out to an unnamed assembler temporary
75 unsigned NextUniqueID;
77 /// Instances of directional local labels.
78 DenseMap<unsigned, MCLabel *> Instances;
79 /// NextInstance() creates the next instance of the directional local label
80 /// for the LocalLabelVal and adds it to the map if needed.
81 unsigned NextInstance(int64_t LocalLabelVal);
82 /// GetInstance() gets the current instance of the directional local label
83 /// for the LocalLabelVal and adds it to the map if needed.
84 unsigned GetInstance(int64_t LocalLabelVal);
86 /// The file name of the log file from the environment variable
87 /// AS_SECURE_LOG_FILE. Which must be set before the .secure_log_unique
88 /// directive is used or it is an error.
90 /// The stream that gets written to for the .secure_log_unique directive.
91 raw_ostream *SecureLog;
92 /// Boolean toggled when .secure_log_unique / .secure_log_reset is seen to
93 /// catch errors if .secure_log_unique appears twice without
94 /// .secure_log_reset appearing between them.
97 /// The dwarf file and directory tables from the dwarf .file directive.
98 std::vector<MCDwarfFile *> MCDwarfFiles;
99 std::vector<StringRef> MCDwarfDirs;
101 /// The current dwarf line information from the last dwarf .loc directive.
102 MCDwarfLoc CurrentDwarfLoc;
105 /// Generate dwarf debugging info for assembly source files.
106 bool GenDwarfForAssembly;
108 /// The current dwarf file number when generate dwarf debugging info for
109 /// assembly source files.
110 unsigned GenDwarfFileNumber;
112 /// The default initial text section that we generate dwarf debugging line
113 /// info for when generating dwarf assembly source files.
114 const MCSection *GenDwarfSection;
115 /// Symbols created for the start and end of this section.
116 MCSymbol *GenDwarfSectionStartSym, *GenDwarfSectionEndSym;
118 /// The information gathered from labels that will have dwarf label
119 /// entries when generating dwarf assembly source files.
120 std::vector<const MCGenDwarfLabelEntry *> MCGenDwarfLabelEntries;
122 /// The string to embed in the debug information for the compile unit, if
124 StringRef DwarfDebugFlags;
126 /// Honor temporary labels, this is useful for debugging semantic
127 /// differences between temporary and non-temporary labels (primarily on
129 bool AllowTemporaryLabels;
131 /// The dwarf line information from the .loc directives for the sections
132 /// with assembled machine instructions have after seeing .loc directives.
133 DenseMap<const MCSection *, MCLineSection *> MCLineSections;
134 /// We need a deterministic iteration order, so we remember the order
135 /// the elements were added.
136 std::vector<const MCSection *> MCLineSectionOrder;
138 void *MachOUniquingMap, *ELFUniquingMap, *COFFUniquingMap;
140 MCSymbol *CreateSymbol(StringRef Name);
143 explicit MCContext(const MCAsmInfo &MAI, const MCRegisterInfo &MRI,
144 const MCObjectFileInfo *MOFI, const SourceMgr *Mgr = 0);
147 const SourceMgr *getSourceManager() const { return SrcMgr; }
149 const MCAsmInfo &getAsmInfo() const { return MAI; }
151 const MCRegisterInfo &getRegisterInfo() const { return MRI; }
153 const MCObjectFileInfo *getObjectFileInfo() const { return MOFI; }
155 void setAllowTemporaryLabels(bool Value) { AllowTemporaryLabels = Value; }
157 /// @name Symbol Management
160 /// CreateTempSymbol - Create and return a new assembler temporary symbol
161 /// with a unique but unspecified name.
162 MCSymbol *CreateTempSymbol();
164 /// getUniqueSymbolID() - Return a unique identifier for use in constructing
166 unsigned getUniqueSymbolID() { return NextUniqueID++; }
168 /// CreateDirectionalLocalSymbol - Create the definition of a directional
169 /// local symbol for numbered label (used for "1:" definitions).
170 MCSymbol *CreateDirectionalLocalSymbol(int64_t LocalLabelVal);
172 /// GetDirectionalLocalSymbol - Create and return a directional local
173 /// symbol for numbered label (used for "1b" or 1f" references).
174 MCSymbol *GetDirectionalLocalSymbol(int64_t LocalLabelVal, int bORf);
176 /// GetOrCreateSymbol - Lookup the symbol inside with the specified
177 /// @p Name. If it exists, return it. If not, create a forward
178 /// reference and return it.
180 /// @param Name - The symbol name, which must be unique across all symbols.
181 MCSymbol *GetOrCreateSymbol(StringRef Name);
182 MCSymbol *GetOrCreateSymbol(const Twine &Name);
184 /// LookupSymbol - Get the symbol for \p Name, or null.
185 MCSymbol *LookupSymbol(StringRef Name) const;
187 /// getSymbols - Get a reference for the symbol table for clients that
188 /// want to, for example, iterate over all symbols. 'const' because we
189 /// still want any modifications to the table itself to use the MCContext
191 const SymbolTable &getSymbols() const {
197 /// @name Section Management
200 /// getMachOSection - Return the MCSection for the specified mach-o section.
201 /// This requires the operands to be valid.
202 const MCSectionMachO *getMachOSection(StringRef Segment,
204 unsigned TypeAndAttributes,
207 const MCSectionMachO *getMachOSection(StringRef Segment,
209 unsigned TypeAndAttributes,
211 return getMachOSection(Segment, Section, TypeAndAttributes, 0, K);
214 const MCSectionELF *getELFSection(StringRef Section, unsigned Type,
215 unsigned Flags, SectionKind Kind);
217 const MCSectionELF *getELFSection(StringRef Section, unsigned Type,
218 unsigned Flags, SectionKind Kind,
219 unsigned EntrySize, StringRef Group);
221 const MCSectionELF *CreateELFGroupSection();
223 const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
224 int Selection, SectionKind Kind);
226 const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
228 return getCOFFSection (Section, Characteristics, 0, Kind);
234 /// @name Dwarf Management
237 /// GetDwarfFile - creates an entry in the dwarf file and directory tables.
238 unsigned GetDwarfFile(StringRef Directory, StringRef FileName,
239 unsigned FileNumber);
241 bool isValidDwarfFileNumber(unsigned FileNumber);
243 bool hasDwarfFiles() const {
244 return !MCDwarfFiles.empty();
247 const std::vector<MCDwarfFile *> &getMCDwarfFiles() {
250 const std::vector<StringRef> &getMCDwarfDirs() {
254 const DenseMap<const MCSection *, MCLineSection *>
255 &getMCLineSections() const {
256 return MCLineSections;
258 const std::vector<const MCSection *> &getMCLineSectionOrder() const {
259 return MCLineSectionOrder;
261 void addMCLineSection(const MCSection *Sec, MCLineSection *Line) {
262 MCLineSections[Sec] = Line;
263 MCLineSectionOrder.push_back(Sec);
266 /// setCurrentDwarfLoc - saves the information from the currently parsed
267 /// dwarf .loc directive and sets DwarfLocSeen. When the next instruction
268 /// is assembled an entry in the line number table with this information and
269 /// the address of the instruction will be created.
270 void setCurrentDwarfLoc(unsigned FileNum, unsigned Line, unsigned Column,
271 unsigned Flags, unsigned Isa,
272 unsigned Discriminator) {
273 CurrentDwarfLoc.setFileNum(FileNum);
274 CurrentDwarfLoc.setLine(Line);
275 CurrentDwarfLoc.setColumn(Column);
276 CurrentDwarfLoc.setFlags(Flags);
277 CurrentDwarfLoc.setIsa(Isa);
278 CurrentDwarfLoc.setDiscriminator(Discriminator);
281 void ClearDwarfLocSeen() { DwarfLocSeen = false; }
283 bool getDwarfLocSeen() { return DwarfLocSeen; }
284 const MCDwarfLoc &getCurrentDwarfLoc() { return CurrentDwarfLoc; }
286 bool getGenDwarfForAssembly() { return GenDwarfForAssembly; }
287 void setGenDwarfForAssembly(bool Value) { GenDwarfForAssembly = Value; }
288 unsigned getGenDwarfFileNumber() { return GenDwarfFileNumber; }
289 unsigned nextGenDwarfFileNumber() { return ++GenDwarfFileNumber; }
290 const MCSection *getGenDwarfSection() { return GenDwarfSection; }
291 void setGenDwarfSection(const MCSection *Sec) { GenDwarfSection = Sec; }
292 MCSymbol *getGenDwarfSectionStartSym() { return GenDwarfSectionStartSym; }
293 void setGenDwarfSectionStartSym(MCSymbol *Sym) {
294 GenDwarfSectionStartSym = Sym;
296 MCSymbol *getGenDwarfSectionEndSym() { return GenDwarfSectionEndSym; }
297 void setGenDwarfSectionEndSym(MCSymbol *Sym) {
298 GenDwarfSectionEndSym = Sym;
300 const std::vector<const MCGenDwarfLabelEntry *>
301 &getMCGenDwarfLabelEntries() const {
302 return MCGenDwarfLabelEntries;
304 void addMCGenDwarfLabelEntry(const MCGenDwarfLabelEntry *E) {
305 MCGenDwarfLabelEntries.push_back(E);
308 void setDwarfDebugFlags(StringRef S) { DwarfDebugFlags = S; }
309 StringRef getDwarfDebugFlags() { return DwarfDebugFlags; }
313 char *getSecureLogFile() { return SecureLogFile; }
314 raw_ostream *getSecureLog() { return SecureLog; }
315 bool getSecureLogUsed() { return SecureLogUsed; }
316 void setSecureLog(raw_ostream *Value) {
319 void setSecureLogUsed(bool Value) {
320 SecureLogUsed = Value;
323 void *Allocate(unsigned Size, unsigned Align = 8) {
324 return Allocator.Allocate(Size, Align);
326 void Deallocate(void *Ptr) {
329 // Unrecoverable error has occured. Display the best diagnostic we can
330 // and bail via exit(1). For now, most MC backend errors are unrecoverable.
331 // FIXME: We should really do something about that.
332 LLVM_ATTRIBUTE_NORETURN void FatalError(SMLoc L, const Twine &Msg);
335 } // end namespace llvm
337 // operator new and delete aren't allowed inside namespaces.
338 // The throw specifications are mandated by the standard.
339 /// @brief Placement new for using the MCContext's allocator.
341 /// This placement form of operator new uses the MCContext's allocator for
342 /// obtaining memory. It is a non-throwing new, which means that it returns
343 /// null on error. (If that is what the allocator does. The current does, so if
344 /// this ever changes, this operator will have to be changed, too.)
345 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
347 /// // Default alignment (16)
348 /// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
349 /// // Specific alignment
350 /// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
352 /// Please note that you cannot use delete on the pointer; it must be
353 /// deallocated using an explicit destructor call followed by
354 /// @c Context.Deallocate(Ptr).
356 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
357 /// @param C The MCContext that provides the allocator.
358 /// @param Alignment The alignment of the allocated memory (if the underlying
359 /// allocator supports it).
360 /// @return The allocated memory. Could be NULL.
361 inline void *operator new(size_t Bytes, llvm::MCContext &C,
362 size_t Alignment = 16) throw () {
363 return C.Allocate(Bytes, Alignment);
365 /// @brief Placement delete companion to the new above.
367 /// This operator is just a companion to the new above. There is no way of
368 /// invoking it directly; see the new operator for more details. This operator
369 /// is called implicitly by the compiler if a placement new expression using
370 /// the MCContext throws in the object constructor.
371 inline void operator delete(void *Ptr, llvm::MCContext &C, size_t)
376 /// This placement form of operator new[] uses the MCContext's allocator for
377 /// obtaining memory. It is a non-throwing new[], which means that it returns
379 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
381 /// // Default alignment (16)
382 /// char *data = new (Context) char[10];
383 /// // Specific alignment
384 /// char *data = new (Context, 8) char[10];
386 /// Please note that you cannot use delete on the pointer; it must be
387 /// deallocated using an explicit destructor call followed by
388 /// @c Context.Deallocate(Ptr).
390 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
391 /// @param C The MCContext that provides the allocator.
392 /// @param Alignment The alignment of the allocated memory (if the underlying
393 /// allocator supports it).
394 /// @return The allocated memory. Could be NULL.
395 inline void *operator new[](size_t Bytes, llvm::MCContext& C,
396 size_t Alignment = 16) throw () {
397 return C.Allocate(Bytes, Alignment);
400 /// @brief Placement delete[] companion to the new[] above.
402 /// This operator is just a companion to the new[] above. There is no way of
403 /// invoking it directly; see the new[] operator for more details. This operator
404 /// is called implicitly by the compiler if a placement new[] expression using
405 /// the MCContext throws in the object constructor.
406 inline void operator delete[](void *Ptr, llvm::MCContext &C) throw () {