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;
46 /// The SourceMgr for this object, if any.
47 const SourceMgr *SrcMgr;
49 /// The MCAsmInfo for this target.
52 /// The MCRegisterInfo for this target.
53 const MCRegisterInfo &MRI;
55 /// The MCObjectFileInfo for this target.
56 const MCObjectFileInfo *MOFI;
58 /// Allocator - Allocator object used for creating machine code objects.
60 /// We use a bump pointer allocator to avoid the need to track all allocated
62 BumpPtrAllocator Allocator;
64 /// Symbols - Bindings of names to symbols.
67 /// UsedNames - Keeps tracks of names that were used both for used declared
68 /// and artificial symbols.
69 StringMap<bool, BumpPtrAllocator&> UsedNames;
71 /// NextUniqueID - The next ID to dole out to an unnamed assembler temporary
73 unsigned NextUniqueID;
75 /// Instances of directional local labels.
76 DenseMap<unsigned, MCLabel *> Instances;
77 /// NextInstance() creates the next instance of the directional local label
78 /// for the LocalLabelVal and adds it to the map if needed.
79 unsigned NextInstance(int64_t LocalLabelVal);
80 /// GetInstance() gets the current instance of the directional local label
81 /// for the LocalLabelVal and adds it to the map if needed.
82 unsigned GetInstance(int64_t LocalLabelVal);
84 /// The file name of the log file from the environment variable
85 /// AS_SECURE_LOG_FILE. Which must be set before the .secure_log_unique
86 /// directive is used or it is an error.
88 /// The stream that gets written to for the .secure_log_unique directive.
89 raw_ostream *SecureLog;
90 /// Boolean toggled when .secure_log_unique / .secure_log_reset is seen to
91 /// catch errors if .secure_log_unique appears twice without
92 /// .secure_log_reset appearing between them.
95 /// The dwarf file and directory tables from the dwarf .file directive.
96 std::vector<MCDwarfFile *> MCDwarfFiles;
97 std::vector<StringRef> MCDwarfDirs;
99 /// The current dwarf line information from the last dwarf .loc directive.
100 MCDwarfLoc CurrentDwarfLoc;
103 /// Generate dwarf debugging info for assembly source files.
104 bool GenDwarfForAssembly;
106 /// The current dwarf file number when generate dwarf debugging info for
107 /// assembly source files.
108 unsigned GenDwarfFileNumber;
110 /// The default initial text section that we generate dwarf debugging line
111 /// info for when generating dwarf assembly source files.
112 const MCSection *GenDwarfSection;
113 /// Symbols created for the start and end of this section.
114 MCSymbol *GenDwarfSectionStartSym, *GenDwarfSectionEndSym;
116 /// The information gathered from labels that will have dwarf label
117 /// entries when generating dwarf assembly source files.
118 std::vector<const MCGenDwarfLabelEntry *> MCGenDwarfLabelEntries;
120 /// The string to embed in the debug information for the compile unit, if
122 StringRef DwarfDebugFlags;
124 /// Honor temporary labels, this is useful for debugging semantic
125 /// differences between temporary and non-temporary labels (primarily on
127 bool AllowTemporaryLabels;
129 /// The dwarf line information from the .loc directives for the sections
130 /// with assembled machine instructions have after seeing .loc directives.
131 DenseMap<const MCSection *, MCLineSection *> MCLineSections;
132 /// We need a deterministic iteration order, so we remember the order
133 /// the elements were added.
134 std::vector<const MCSection *> MCLineSectionOrder;
136 void *MachOUniquingMap, *ELFUniquingMap, *COFFUniquingMap;
138 MCSymbol *CreateSymbol(StringRef Name);
141 explicit MCContext(const MCAsmInfo &MAI, const MCRegisterInfo &MRI,
142 const MCObjectFileInfo *MOFI, const SourceMgr *Mgr = 0);
145 const SourceMgr *getSourceManager() const { return SrcMgr; }
147 const MCAsmInfo &getAsmInfo() const { return MAI; }
149 const MCRegisterInfo &getRegisterInfo() const { return MRI; }
151 const MCObjectFileInfo *getObjectFileInfo() const { return MOFI; }
153 void setAllowTemporaryLabels(bool Value) { AllowTemporaryLabels = Value; }
155 /// @name Symbol Management
158 /// CreateTempSymbol - Create and return a new assembler temporary symbol
159 /// with a unique but unspecified name.
160 MCSymbol *CreateTempSymbol();
162 /// CreateDirectionalLocalSymbol - Create the definition of a directional
163 /// local symbol for numbered label (used for "1:" definitions).
164 MCSymbol *CreateDirectionalLocalSymbol(int64_t LocalLabelVal);
166 /// GetDirectionalLocalSymbol - Create and return a directional local
167 /// symbol for numbered label (used for "1b" or 1f" references).
168 MCSymbol *GetDirectionalLocalSymbol(int64_t LocalLabelVal, int bORf);
170 /// GetOrCreateSymbol - Lookup the symbol inside with the specified
171 /// @p Name. If it exists, return it. If not, create a forward
172 /// reference and return it.
174 /// @param Name - The symbol name, which must be unique across all symbols.
175 MCSymbol *GetOrCreateSymbol(StringRef Name);
176 MCSymbol *GetOrCreateSymbol(const Twine &Name);
178 /// LookupSymbol - Get the symbol for \p Name, or null.
179 MCSymbol *LookupSymbol(StringRef Name) const;
181 /// getSymbols - Get a reference for the symbol table for clients that
182 /// want to, for example, iterate over all symbols. 'const' because we
183 /// still want any modifications to the table itself to use the MCContext
185 const SymbolTable &getSymbols() const {
191 /// @name Section Management
194 /// getMachOSection - Return the MCSection for the specified mach-o section.
195 /// This requires the operands to be valid.
196 const MCSectionMachO *getMachOSection(StringRef Segment,
198 unsigned TypeAndAttributes,
201 const MCSectionMachO *getMachOSection(StringRef Segment,
203 unsigned TypeAndAttributes,
205 return getMachOSection(Segment, Section, TypeAndAttributes, 0, K);
208 const MCSectionELF *getELFSection(StringRef Section, unsigned Type,
209 unsigned Flags, SectionKind Kind);
211 const MCSectionELF *getELFSection(StringRef Section, unsigned Type,
212 unsigned Flags, SectionKind Kind,
213 unsigned EntrySize, StringRef Group);
215 const MCSectionELF *CreateELFGroupSection();
217 const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
218 int Selection, SectionKind Kind);
220 const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
222 return getCOFFSection (Section, Characteristics, 0, Kind);
228 /// @name Dwarf Management
231 /// GetDwarfFile - creates an entry in the dwarf file and directory tables.
232 unsigned GetDwarfFile(StringRef Directory, StringRef FileName,
233 unsigned FileNumber);
235 bool isValidDwarfFileNumber(unsigned FileNumber);
237 bool hasDwarfFiles() const {
238 return !MCDwarfFiles.empty();
241 const std::vector<MCDwarfFile *> &getMCDwarfFiles() {
244 const std::vector<StringRef> &getMCDwarfDirs() {
248 const DenseMap<const MCSection *, MCLineSection *>
249 &getMCLineSections() const {
250 return MCLineSections;
252 const std::vector<const MCSection *> &getMCLineSectionOrder() const {
253 return MCLineSectionOrder;
255 void addMCLineSection(const MCSection *Sec, MCLineSection *Line) {
256 MCLineSections[Sec] = Line;
257 MCLineSectionOrder.push_back(Sec);
260 /// setCurrentDwarfLoc - saves the information from the currently parsed
261 /// dwarf .loc directive and sets DwarfLocSeen. When the next instruction
262 /// is assembled an entry in the line number table with this information and
263 /// the address of the instruction will be created.
264 void setCurrentDwarfLoc(unsigned FileNum, unsigned Line, unsigned Column,
265 unsigned Flags, unsigned Isa,
266 unsigned Discriminator) {
267 CurrentDwarfLoc.setFileNum(FileNum);
268 CurrentDwarfLoc.setLine(Line);
269 CurrentDwarfLoc.setColumn(Column);
270 CurrentDwarfLoc.setFlags(Flags);
271 CurrentDwarfLoc.setIsa(Isa);
272 CurrentDwarfLoc.setDiscriminator(Discriminator);
275 void ClearDwarfLocSeen() { DwarfLocSeen = false; }
277 bool getDwarfLocSeen() { return DwarfLocSeen; }
278 const MCDwarfLoc &getCurrentDwarfLoc() { return CurrentDwarfLoc; }
280 bool getGenDwarfForAssembly() { return GenDwarfForAssembly; }
281 void setGenDwarfForAssembly(bool Value) { GenDwarfForAssembly = Value; }
282 unsigned getGenDwarfFileNumber() { return GenDwarfFileNumber; }
283 unsigned nextGenDwarfFileNumber() { return ++GenDwarfFileNumber; }
284 const MCSection *getGenDwarfSection() { return GenDwarfSection; }
285 void setGenDwarfSection(const MCSection *Sec) { GenDwarfSection = Sec; }
286 MCSymbol *getGenDwarfSectionStartSym() { return GenDwarfSectionStartSym; }
287 void setGenDwarfSectionStartSym(MCSymbol *Sym) {
288 GenDwarfSectionStartSym = Sym;
290 MCSymbol *getGenDwarfSectionEndSym() { return GenDwarfSectionEndSym; }
291 void setGenDwarfSectionEndSym(MCSymbol *Sym) {
292 GenDwarfSectionEndSym = Sym;
294 const std::vector<const MCGenDwarfLabelEntry *>
295 &getMCGenDwarfLabelEntries() const {
296 return MCGenDwarfLabelEntries;
298 void addMCGenDwarfLabelEntry(const MCGenDwarfLabelEntry *E) {
299 MCGenDwarfLabelEntries.push_back(E);
302 void setDwarfDebugFlags(StringRef S) { DwarfDebugFlags = S; }
303 StringRef getDwarfDebugFlags() { return DwarfDebugFlags; }
307 char *getSecureLogFile() { return SecureLogFile; }
308 raw_ostream *getSecureLog() { return SecureLog; }
309 bool getSecureLogUsed() { return SecureLogUsed; }
310 void setSecureLog(raw_ostream *Value) {
313 void setSecureLogUsed(bool Value) {
314 SecureLogUsed = Value;
317 void *Allocate(unsigned Size, unsigned Align = 8) {
318 return Allocator.Allocate(Size, Align);
320 void Deallocate(void *Ptr) {
324 } // end namespace llvm
326 // operator new and delete aren't allowed inside namespaces.
327 // The throw specifications are mandated by the standard.
328 /// @brief Placement new for using the MCContext's allocator.
330 /// This placement form of operator new uses the MCContext's allocator for
331 /// obtaining memory. It is a non-throwing new, which means that it returns
332 /// null on error. (If that is what the allocator does. The current does, so if
333 /// this ever changes, this operator will have to be changed, too.)
334 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
336 /// // Default alignment (16)
337 /// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
338 /// // Specific alignment
339 /// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
341 /// Please note that you cannot use delete on the pointer; it must be
342 /// deallocated using an explicit destructor call followed by
343 /// @c Context.Deallocate(Ptr).
345 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
346 /// @param C The MCContext that provides the allocator.
347 /// @param Alignment The alignment of the allocated memory (if the underlying
348 /// allocator supports it).
349 /// @return The allocated memory. Could be NULL.
350 inline void *operator new(size_t Bytes, llvm::MCContext &C,
351 size_t Alignment = 16) throw () {
352 return C.Allocate(Bytes, Alignment);
354 /// @brief Placement delete companion to the new above.
356 /// This operator is just a companion to the new above. There is no way of
357 /// invoking it directly; see the new operator for more details. This operator
358 /// is called implicitly by the compiler if a placement new expression using
359 /// the MCContext throws in the object constructor.
360 inline void operator delete(void *Ptr, llvm::MCContext &C, size_t)
365 /// This placement form of operator new[] uses the MCContext's allocator for
366 /// obtaining memory. It is a non-throwing new[], which means that it returns
368 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
370 /// // Default alignment (16)
371 /// char *data = new (Context) char[10];
372 /// // Specific alignment
373 /// char *data = new (Context, 8) char[10];
375 /// Please note that you cannot use delete on the pointer; it must be
376 /// deallocated using an explicit destructor call followed by
377 /// @c Context.Deallocate(Ptr).
379 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
380 /// @param C The MCContext that provides the allocator.
381 /// @param Alignment The alignment of the allocated memory (if the underlying
382 /// allocator supports it).
383 /// @return The allocated memory. Could be NULL.
384 inline void *operator new[](size_t Bytes, llvm::MCContext& C,
385 size_t Alignment = 16) throw () {
386 return C.Allocate(Bytes, Alignment);
389 /// @brief Placement delete[] companion to the new[] above.
391 /// This operator is just a companion to the new[] above. There is no way of
392 /// invoking it directly; see the new[] operator for more details. This operator
393 /// is called implicitly by the compiler if a placement new[] expression using
394 /// the MCContext throws in the object constructor.
395 inline void operator delete[](void *Ptr, llvm::MCContext &C) throw () {