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 /// Generate dwarf debugging info for assembly source files.
102 bool GenDwarfForAssembly;
104 /// The current dwarf file number when generate dwarf debugging info for
105 /// assembly source files.
106 unsigned GenDwarfFileNumber;
108 /// The default initial text section that we generate dwarf debugging line
109 /// info for when generating dwarf assembly source files.
110 const MCSection *GenDwarfSection;
112 /// Honor temporary labels, this is useful for debugging semantic
113 /// differences between temporary and non-temporary labels (primarily on
115 bool AllowTemporaryLabels;
117 /// The dwarf line information from the .loc directives for the sections
118 /// with assembled machine instructions have after seeing .loc directives.
119 DenseMap<const MCSection *, MCLineSection *> MCLineSections;
120 /// We need a deterministic iteration order, so we remember the order
121 /// the elements were added.
122 std::vector<const MCSection *> MCLineSectionOrder;
124 void *MachOUniquingMap, *ELFUniquingMap, *COFFUniquingMap;
126 MCSymbol *CreateSymbol(StringRef Name);
129 explicit MCContext(const MCAsmInfo &MAI, const MCRegisterInfo &MRI,
130 const MCObjectFileInfo *MOFI);
133 const MCAsmInfo &getAsmInfo() const { return MAI; }
135 const MCRegisterInfo &getRegisterInfo() const { return MRI; }
137 const MCObjectFileInfo *getObjectFileInfo() const { return MOFI; }
139 void setAllowTemporaryLabels(bool Value) { AllowTemporaryLabels = Value; }
141 /// @name Symbol Management
144 /// CreateTempSymbol - Create and return a new assembler temporary symbol
145 /// with a unique but unspecified name.
146 MCSymbol *CreateTempSymbol();
148 /// CreateDirectionalLocalSymbol - Create the definition of a directional
149 /// local symbol for numbered label (used for "1:" definitions).
150 MCSymbol *CreateDirectionalLocalSymbol(int64_t LocalLabelVal);
152 /// GetDirectionalLocalSymbol - Create and return a directional local
153 /// symbol for numbered label (used for "1b" or 1f" references).
154 MCSymbol *GetDirectionalLocalSymbol(int64_t LocalLabelVal, int bORf);
156 /// GetOrCreateSymbol - Lookup the symbol inside with the specified
157 /// @p Name. If it exists, return it. If not, create a forward
158 /// reference and return it.
160 /// @param Name - The symbol name, which must be unique across all symbols.
161 MCSymbol *GetOrCreateSymbol(StringRef Name);
162 MCSymbol *GetOrCreateSymbol(const Twine &Name);
164 /// LookupSymbol - Get the symbol for \p Name, or null.
165 MCSymbol *LookupSymbol(StringRef Name) const;
167 /// getSymbols - Get a reference for the symbol table for clients that
168 /// want to, for example, iterate over all symbols. 'const' because we
169 /// still want any modifications to the table itself to use the MCContext
171 const SymbolTable &getSymbols() const {
177 /// @name Section Management
180 /// getMachOSection - Return the MCSection for the specified mach-o section.
181 /// This requires the operands to be valid.
182 const MCSectionMachO *getMachOSection(StringRef Segment,
184 unsigned TypeAndAttributes,
187 const MCSectionMachO *getMachOSection(StringRef Segment,
189 unsigned TypeAndAttributes,
191 return getMachOSection(Segment, Section, TypeAndAttributes, 0, K);
194 const MCSectionELF *getELFSection(StringRef Section, unsigned Type,
195 unsigned Flags, SectionKind Kind);
197 const MCSectionELF *getELFSection(StringRef Section, unsigned Type,
198 unsigned Flags, SectionKind Kind,
199 unsigned EntrySize, StringRef Group);
201 const MCSectionELF *CreateELFGroupSection();
203 const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
204 int Selection, SectionKind Kind);
206 const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
208 return getCOFFSection (Section, Characteristics, 0, Kind);
214 /// @name Dwarf Management
217 /// GetDwarfFile - creates an entry in the dwarf file and directory tables.
218 unsigned GetDwarfFile(StringRef Directory, StringRef FileName,
219 unsigned FileNumber);
221 bool isValidDwarfFileNumber(unsigned FileNumber);
223 bool hasDwarfFiles() const {
224 return !MCDwarfFiles.empty();
227 const std::vector<MCDwarfFile *> &getMCDwarfFiles() {
230 const std::vector<StringRef> &getMCDwarfDirs() {
234 const DenseMap<const MCSection *, MCLineSection *>
235 &getMCLineSections() const {
236 return MCLineSections;
238 const std::vector<const MCSection *> &getMCLineSectionOrder() const {
239 return MCLineSectionOrder;
241 void addMCLineSection(const MCSection *Sec, MCLineSection *Line) {
242 MCLineSections[Sec] = Line;
243 MCLineSectionOrder.push_back(Sec);
246 /// setCurrentDwarfLoc - saves the information from the currently parsed
247 /// dwarf .loc directive and sets DwarfLocSeen. When the next instruction
248 /// is assembled an entry in the line number table with this information and
249 /// the address of the instruction will be created.
250 void setCurrentDwarfLoc(unsigned FileNum, unsigned Line, unsigned Column,
251 unsigned Flags, unsigned Isa,
252 unsigned Discriminator) {
253 CurrentDwarfLoc.setFileNum(FileNum);
254 CurrentDwarfLoc.setLine(Line);
255 CurrentDwarfLoc.setColumn(Column);
256 CurrentDwarfLoc.setFlags(Flags);
257 CurrentDwarfLoc.setIsa(Isa);
258 CurrentDwarfLoc.setDiscriminator(Discriminator);
261 void ClearDwarfLocSeen() { DwarfLocSeen = false; }
263 bool getDwarfLocSeen() { return DwarfLocSeen; }
264 const MCDwarfLoc &getCurrentDwarfLoc() { return CurrentDwarfLoc; }
266 bool getGenDwarfForAssembly() { return GenDwarfForAssembly; }
267 void setGenDwarfForAssembly(bool Value) { GenDwarfForAssembly = Value; }
268 unsigned getGenDwarfFileNumber() { return GenDwarfFileNumber; }
269 unsigned nextGenDwarfFileNumber() { return ++GenDwarfFileNumber; }
270 const MCSection *getGenDwarfSection() { return GenDwarfSection; }
271 void setGenDwarfSection(const MCSection *Sec) { GenDwarfSection = Sec; }
275 char *getSecureLogFile() { return SecureLogFile; }
276 raw_ostream *getSecureLog() { return SecureLog; }
277 bool getSecureLogUsed() { return SecureLogUsed; }
278 void setSecureLog(raw_ostream *Value) {
281 void setSecureLogUsed(bool Value) {
282 SecureLogUsed = Value;
285 void *Allocate(unsigned Size, unsigned Align = 8) {
286 return Allocator.Allocate(Size, Align);
288 void Deallocate(void *Ptr) {
292 } // end namespace llvm
294 // operator new and delete aren't allowed inside namespaces.
295 // The throw specifications are mandated by the standard.
296 /// @brief Placement new for using the MCContext's allocator.
298 /// This placement form of operator new uses the MCContext's allocator for
299 /// obtaining memory. It is a non-throwing new, which means that it returns
300 /// null on error. (If that is what the allocator does. The current does, so if
301 /// this ever changes, this operator will have to be changed, too.)
302 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
304 /// // Default alignment (16)
305 /// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
306 /// // Specific alignment
307 /// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
309 /// Please note that you cannot use delete on the pointer; it must be
310 /// deallocated using an explicit destructor call followed by
311 /// @c Context.Deallocate(Ptr).
313 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
314 /// @param C The MCContext that provides the allocator.
315 /// @param Alignment The alignment of the allocated memory (if the underlying
316 /// allocator supports it).
317 /// @return The allocated memory. Could be NULL.
318 inline void *operator new(size_t Bytes, llvm::MCContext &C,
319 size_t Alignment = 16) throw () {
320 return C.Allocate(Bytes, Alignment);
322 /// @brief Placement delete companion to the new above.
324 /// This operator is just a companion to the new above. There is no way of
325 /// invoking it directly; see the new operator for more details. This operator
326 /// is called implicitly by the compiler if a placement new expression using
327 /// the MCContext throws in the object constructor.
328 inline void operator delete(void *Ptr, llvm::MCContext &C, size_t)
333 /// This placement form of operator new[] uses the MCContext's allocator for
334 /// obtaining memory. It is a non-throwing new[], which means that it returns
336 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
338 /// // Default alignment (16)
339 /// char *data = new (Context) char[10];
340 /// // Specific alignment
341 /// char *data = new (Context, 8) char[10];
343 /// Please note that you cannot use delete on the pointer; it must be
344 /// deallocated using an explicit destructor call followed by
345 /// @c Context.Deallocate(Ptr).
347 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
348 /// @param C The MCContext that provides the allocator.
349 /// @param Alignment The alignment of the allocated memory (if the underlying
350 /// allocator supports it).
351 /// @return The allocated memory. Could be NULL.
352 inline void *operator new[](size_t Bytes, llvm::MCContext& C,
353 size_t Alignment = 16) throw () {
354 return C.Allocate(Bytes, Alignment);
357 /// @brief Placement delete[] companion to the new[] above.
359 /// This operator is just a companion to the new[] above. There is no way of
360 /// invoking it directly; see the new[] operator for more details. This operator
361 /// is called implicitly by the compiler if a placement new[] expression using
362 /// the MCContext throws in the object constructor.
363 inline void operator delete[](void *Ptr, llvm::MCContext &C) throw () {