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/ADT/DenseMap.h"
14 #include "llvm/ADT/StringMap.h"
15 #include "llvm/MC/MCDwarf.h"
16 #include "llvm/MC/SectionKind.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&) LLVM_DELETED_FUNCTION;
44 MCContext &operator=(const MCContext&) LLVM_DELETED_FUNCTION;
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 /// Do automatic initialization in constructor and finalization in
142 bool AutoInitializationFinalization;
144 MCSymbol *CreateSymbol(StringRef Name);
147 explicit MCContext(const MCAsmInfo &MAI, const MCRegisterInfo &MRI,
148 const MCObjectFileInfo *MOFI, const SourceMgr *Mgr = 0,
149 bool AutoInitializationFinalization = true);
152 const SourceMgr *getSourceManager() const { return SrcMgr; }
154 const MCAsmInfo &getAsmInfo() const { return MAI; }
156 const MCRegisterInfo &getRegisterInfo() const { return MRI; }
158 const MCObjectFileInfo *getObjectFileInfo() const { return MOFI; }
160 void setAllowTemporaryLabels(bool Value) { AllowTemporaryLabels = Value; }
162 /// @name Module Lifetime Management
165 /// doInitialization - prepare to process a new module
166 void doInitialization();
168 /// doFinalization - clean up state from the current module
169 void doFinalization();
173 /// @name Symbol Management
176 /// CreateTempSymbol - Create and return a new assembler temporary symbol
177 /// with a unique but unspecified name.
178 MCSymbol *CreateTempSymbol();
180 /// getUniqueSymbolID() - Return a unique identifier for use in constructing
182 unsigned getUniqueSymbolID() { return NextUniqueID++; }
184 /// CreateDirectionalLocalSymbol - Create the definition of a directional
185 /// local symbol for numbered label (used for "1:" definitions).
186 MCSymbol *CreateDirectionalLocalSymbol(int64_t LocalLabelVal);
188 /// GetDirectionalLocalSymbol - Create and return a directional local
189 /// symbol for numbered label (used for "1b" or 1f" references).
190 MCSymbol *GetDirectionalLocalSymbol(int64_t LocalLabelVal, int bORf);
192 /// GetOrCreateSymbol - Lookup the symbol inside with the specified
193 /// @p Name. If it exists, return it. If not, create a forward
194 /// reference and return it.
196 /// @param Name - The symbol name, which must be unique across all symbols.
197 MCSymbol *GetOrCreateSymbol(StringRef Name);
198 MCSymbol *GetOrCreateSymbol(const Twine &Name);
200 /// LookupSymbol - Get the symbol for \p Name, or null.
201 MCSymbol *LookupSymbol(StringRef Name) const;
202 MCSymbol *LookupSymbol(const Twine &Name) const;
204 /// getSymbols - Get a reference for the symbol table for clients that
205 /// want to, for example, iterate over all symbols. 'const' because we
206 /// still want any modifications to the table itself to use the MCContext
208 const SymbolTable &getSymbols() const {
214 /// @name Section Management
217 /// getMachOSection - Return the MCSection for the specified mach-o section.
218 /// This requires the operands to be valid.
219 const MCSectionMachO *getMachOSection(StringRef Segment,
221 unsigned TypeAndAttributes,
224 const MCSectionMachO *getMachOSection(StringRef Segment,
226 unsigned TypeAndAttributes,
228 return getMachOSection(Segment, Section, TypeAndAttributes, 0, K);
231 const MCSectionELF *getELFSection(StringRef Section, unsigned Type,
232 unsigned Flags, SectionKind Kind);
234 const MCSectionELF *getELFSection(StringRef Section, unsigned Type,
235 unsigned Flags, SectionKind Kind,
236 unsigned EntrySize, StringRef Group);
238 const MCSectionELF *CreateELFGroupSection();
240 const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
241 int Selection, SectionKind Kind);
243 const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
245 return getCOFFSection (Section, Characteristics, 0, Kind);
251 /// @name Dwarf Management
254 /// GetDwarfFile - creates an entry in the dwarf file and directory tables.
255 unsigned GetDwarfFile(StringRef Directory, StringRef FileName,
256 unsigned FileNumber);
258 bool isValidDwarfFileNumber(unsigned FileNumber);
260 bool hasDwarfFiles() const {
261 return !MCDwarfFiles.empty();
264 const std::vector<MCDwarfFile *> &getMCDwarfFiles() {
267 const std::vector<StringRef> &getMCDwarfDirs() {
271 const DenseMap<const MCSection *, MCLineSection *>
272 &getMCLineSections() const {
273 return MCLineSections;
275 const std::vector<const MCSection *> &getMCLineSectionOrder() const {
276 return MCLineSectionOrder;
278 void addMCLineSection(const MCSection *Sec, MCLineSection *Line) {
279 MCLineSections[Sec] = Line;
280 MCLineSectionOrder.push_back(Sec);
283 /// setCurrentDwarfLoc - saves the information from the currently parsed
284 /// dwarf .loc directive and sets DwarfLocSeen. When the next instruction
285 /// is assembled an entry in the line number table with this information and
286 /// the address of the instruction will be created.
287 void setCurrentDwarfLoc(unsigned FileNum, unsigned Line, unsigned Column,
288 unsigned Flags, unsigned Isa,
289 unsigned Discriminator) {
290 CurrentDwarfLoc.setFileNum(FileNum);
291 CurrentDwarfLoc.setLine(Line);
292 CurrentDwarfLoc.setColumn(Column);
293 CurrentDwarfLoc.setFlags(Flags);
294 CurrentDwarfLoc.setIsa(Isa);
295 CurrentDwarfLoc.setDiscriminator(Discriminator);
298 void ClearDwarfLocSeen() { DwarfLocSeen = false; }
300 bool getDwarfLocSeen() { return DwarfLocSeen; }
301 const MCDwarfLoc &getCurrentDwarfLoc() { return CurrentDwarfLoc; }
303 bool getGenDwarfForAssembly() { return GenDwarfForAssembly; }
304 void setGenDwarfForAssembly(bool Value) { GenDwarfForAssembly = Value; }
305 unsigned getGenDwarfFileNumber() { return GenDwarfFileNumber; }
306 unsigned nextGenDwarfFileNumber() { return ++GenDwarfFileNumber; }
307 const MCSection *getGenDwarfSection() { return GenDwarfSection; }
308 void setGenDwarfSection(const MCSection *Sec) { GenDwarfSection = Sec; }
309 MCSymbol *getGenDwarfSectionStartSym() { return GenDwarfSectionStartSym; }
310 void setGenDwarfSectionStartSym(MCSymbol *Sym) {
311 GenDwarfSectionStartSym = Sym;
313 MCSymbol *getGenDwarfSectionEndSym() { return GenDwarfSectionEndSym; }
314 void setGenDwarfSectionEndSym(MCSymbol *Sym) {
315 GenDwarfSectionEndSym = Sym;
317 const std::vector<const MCGenDwarfLabelEntry *>
318 &getMCGenDwarfLabelEntries() const {
319 return MCGenDwarfLabelEntries;
321 void addMCGenDwarfLabelEntry(const MCGenDwarfLabelEntry *E) {
322 MCGenDwarfLabelEntries.push_back(E);
325 void setDwarfDebugFlags(StringRef S) { DwarfDebugFlags = S; }
326 StringRef getDwarfDebugFlags() { return DwarfDebugFlags; }
330 char *getSecureLogFile() { return SecureLogFile; }
331 raw_ostream *getSecureLog() { return SecureLog; }
332 bool getSecureLogUsed() { return SecureLogUsed; }
333 void setSecureLog(raw_ostream *Value) {
336 void setSecureLogUsed(bool Value) {
337 SecureLogUsed = Value;
340 void *Allocate(unsigned Size, unsigned Align = 8) {
341 return Allocator.Allocate(Size, Align);
343 void Deallocate(void *Ptr) {
346 // Unrecoverable error has occured. Display the best diagnostic we can
347 // and bail via exit(1). For now, most MC backend errors are unrecoverable.
348 // FIXME: We should really do something about that.
349 LLVM_ATTRIBUTE_NORETURN void FatalError(SMLoc L, const Twine &Msg);
352 } // end namespace llvm
354 // operator new and delete aren't allowed inside namespaces.
355 // The throw specifications are mandated by the standard.
356 /// @brief Placement new for using the MCContext's allocator.
358 /// This placement form of operator new uses the MCContext's allocator for
359 /// obtaining memory. It is a non-throwing new, which means that it returns
360 /// null on error. (If that is what the allocator does. The current does, so if
361 /// this ever changes, this operator will have to be changed, too.)
362 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
364 /// // Default alignment (16)
365 /// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
366 /// // Specific alignment
367 /// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
369 /// Please note that you cannot use delete on the pointer; it must be
370 /// deallocated using an explicit destructor call followed by
371 /// @c Context.Deallocate(Ptr).
373 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
374 /// @param C The MCContext that provides the allocator.
375 /// @param Alignment The alignment of the allocated memory (if the underlying
376 /// allocator supports it).
377 /// @return The allocated memory. Could be NULL.
378 inline void *operator new(size_t Bytes, llvm::MCContext &C,
379 size_t Alignment = 16) throw () {
380 return C.Allocate(Bytes, Alignment);
382 /// @brief Placement delete companion to the new above.
384 /// This operator is just a companion to the new above. There is no way of
385 /// invoking it directly; see the new operator for more details. This operator
386 /// is called implicitly by the compiler if a placement new expression using
387 /// the MCContext throws in the object constructor.
388 inline void operator delete(void *Ptr, llvm::MCContext &C, size_t)
393 /// This placement form of operator new[] uses the MCContext's allocator for
394 /// obtaining memory. It is a non-throwing new[], which means that it returns
396 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
398 /// // Default alignment (16)
399 /// char *data = new (Context) char[10];
400 /// // Specific alignment
401 /// char *data = new (Context, 8) char[10];
403 /// Please note that you cannot use delete on the pointer; it must be
404 /// deallocated using an explicit destructor call followed by
405 /// @c Context.Deallocate(Ptr).
407 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
408 /// @param C The MCContext that provides the allocator.
409 /// @param Alignment The alignment of the allocated memory (if the underlying
410 /// allocator supports it).
411 /// @return The allocated memory. Could be NULL.
412 inline void *operator new[](size_t Bytes, llvm::MCContext& C,
413 size_t Alignment = 16) throw () {
414 return C.Allocate(Bytes, Alignment);
417 /// @brief Placement delete[] companion to the new[] above.
419 /// This operator is just a companion to the new[] above. There is no way of
420 /// invoking it directly; see the new[] operator for more details. This operator
421 /// is called implicitly by the compiler if a placement new[] expression using
422 /// the MCContext throws in the object constructor.
423 inline void operator delete[](void *Ptr, llvm::MCContext &C) throw () {