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.
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 const TargetAsmInfo *TAI;
55 /// Allocator - Allocator object used for creating machine code objects.
57 /// We use a bump pointer allocator to avoid the need to track all allocated
59 BumpPtrAllocator Allocator;
61 /// Symbols - Bindings of names to symbols.
64 /// UsedNames - Keeps tracks of names that were used both for used declared
65 /// and artificial symbols.
66 StringMap<bool, BumpPtrAllocator&> UsedNames;
68 /// NextUniqueID - The next ID to dole out to an unnamed assembler temporary
70 unsigned NextUniqueID;
72 /// Instances of directional local labels.
73 DenseMap<unsigned, MCLabel *> Instances;
74 /// NextInstance() creates the next instance of the directional local label
75 /// for the LocalLabelVal and adds it to the map if needed.
76 unsigned NextInstance(int64_t LocalLabelVal);
77 /// GetInstance() gets the current instance of the directional local label
78 /// for the LocalLabelVal and adds it to the map if needed.
79 unsigned GetInstance(int64_t LocalLabelVal);
81 /// The file name of the log file from the environment variable
82 /// AS_SECURE_LOG_FILE. Which must be set before the .secure_log_unique
83 /// directive is used or it is an error.
85 /// The stream that gets written to for the .secure_log_unique directive.
86 raw_ostream *SecureLog;
87 /// Boolean toggled when .secure_log_unique / .secure_log_reset is seen to
88 /// catch errors if .secure_log_unique appears twice without
89 /// .secure_log_reset appearing between them.
92 /// The dwarf file and directory tables from the dwarf .file directive.
93 std::vector<MCDwarfFile *> MCDwarfFiles;
94 std::vector<StringRef> MCDwarfDirs;
96 /// The current dwarf line information from the last dwarf .loc directive.
97 MCDwarfLoc CurrentDwarfLoc;
100 /// Honor temporary labels, this is useful for debugging semantic
101 /// differences between temporary and non-temporary labels (primarily on
103 bool AllowTemporaryLabels;
105 /// The dwarf line information from the .loc directives for the sections
106 /// with assembled machine instructions have after seeing .loc directives.
107 DenseMap<const MCSection *, MCLineSection *> MCLineSections;
108 /// We need a deterministic iteration order, so we remember the order
109 /// the elements were added.
110 std::vector<const MCSection *> MCLineSectionOrder;
112 void *MachOUniquingMap, *ELFUniquingMap, *COFFUniquingMap;
114 MCSymbol *CreateSymbol(StringRef Name);
117 explicit MCContext(const MCAsmInfo &MAI, const MCRegisterInfo &MRI,
118 const TargetAsmInfo *TAI);
121 const MCAsmInfo &getAsmInfo() const { return MAI; }
123 const MCRegisterInfo &getRegisterInfo() const { return MRI; }
125 const TargetAsmInfo &getTargetAsmInfo() const { return *TAI; }
127 void setAllowTemporaryLabels(bool Value) { AllowTemporaryLabels = Value; }
129 /// @name Symbol Management
132 /// CreateTempSymbol - Create and return a new assembler temporary symbol
133 /// with a unique but unspecified name.
134 MCSymbol *CreateTempSymbol();
136 /// CreateDirectionalLocalSymbol - Create the definition of a directional
137 /// local symbol for numbered label (used for "1:" definitions).
138 MCSymbol *CreateDirectionalLocalSymbol(int64_t LocalLabelVal);
140 /// GetDirectionalLocalSymbol - Create and return a directional local
141 /// symbol for numbered label (used for "1b" or 1f" references).
142 MCSymbol *GetDirectionalLocalSymbol(int64_t LocalLabelVal, int bORf);
144 /// GetOrCreateSymbol - Lookup the symbol inside with the specified
145 /// @p Name. If it exists, return it. If not, create a forward
146 /// reference and return it.
148 /// @param Name - The symbol name, which must be unique across all symbols.
149 MCSymbol *GetOrCreateSymbol(StringRef Name);
150 MCSymbol *GetOrCreateSymbol(const Twine &Name);
152 /// LookupSymbol - Get the symbol for \p Name, or null.
153 MCSymbol *LookupSymbol(StringRef Name) const;
155 /// getSymbols - Get a reference for the symbol table for clients that
156 /// want to, for example, iterate over all symbols. 'const' because we
157 /// still want any modifications to the table itself to use the MCContext
159 const SymbolTable &getSymbols() const {
165 /// @name Section Management
168 /// getMachOSection - Return the MCSection for the specified mach-o section.
169 /// This requires the operands to be valid.
170 const MCSectionMachO *getMachOSection(StringRef Segment,
172 unsigned TypeAndAttributes,
175 const MCSectionMachO *getMachOSection(StringRef Segment,
177 unsigned TypeAndAttributes,
179 return getMachOSection(Segment, Section, TypeAndAttributes, 0, K);
182 const MCSectionELF *getELFSection(StringRef Section, unsigned Type,
183 unsigned Flags, SectionKind Kind);
185 const MCSectionELF *getELFSection(StringRef Section, unsigned Type,
186 unsigned Flags, SectionKind Kind,
187 unsigned EntrySize, StringRef Group);
189 const MCSectionELF *CreateELFGroupSection();
191 const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
192 int Selection, SectionKind Kind);
194 const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
196 return getCOFFSection (Section, Characteristics, 0, Kind);
202 /// @name Dwarf Management
205 /// GetDwarfFile - creates an entry in the dwarf file and directory tables.
206 unsigned GetDwarfFile(StringRef FileName, unsigned FileNumber);
208 bool isValidDwarfFileNumber(unsigned FileNumber);
210 bool hasDwarfFiles() const {
211 return !MCDwarfFiles.empty();
214 const std::vector<MCDwarfFile *> &getMCDwarfFiles() {
217 const std::vector<StringRef> &getMCDwarfDirs() {
221 const DenseMap<const MCSection *, MCLineSection *>
222 &getMCLineSections() const {
223 return MCLineSections;
225 const std::vector<const MCSection *> &getMCLineSectionOrder() const {
226 return MCLineSectionOrder;
228 void addMCLineSection(const MCSection *Sec, MCLineSection *Line) {
229 MCLineSections[Sec] = Line;
230 MCLineSectionOrder.push_back(Sec);
233 /// setCurrentDwarfLoc - saves the information from the currently parsed
234 /// dwarf .loc directive and sets DwarfLocSeen. When the next instruction
235 /// is assembled an entry in the line number table with this information and
236 /// the address of the instruction will be created.
237 void setCurrentDwarfLoc(unsigned FileNum, unsigned Line, unsigned Column,
238 unsigned Flags, unsigned Isa,
239 unsigned Discriminator) {
240 CurrentDwarfLoc.setFileNum(FileNum);
241 CurrentDwarfLoc.setLine(Line);
242 CurrentDwarfLoc.setColumn(Column);
243 CurrentDwarfLoc.setFlags(Flags);
244 CurrentDwarfLoc.setIsa(Isa);
245 CurrentDwarfLoc.setDiscriminator(Discriminator);
248 void ClearDwarfLocSeen() { DwarfLocSeen = false; }
250 bool getDwarfLocSeen() { return DwarfLocSeen; }
251 const MCDwarfLoc &getCurrentDwarfLoc() { return CurrentDwarfLoc; }
255 char *getSecureLogFile() { return SecureLogFile; }
256 raw_ostream *getSecureLog() { return SecureLog; }
257 bool getSecureLogUsed() { return SecureLogUsed; }
258 void setSecureLog(raw_ostream *Value) {
261 void setSecureLogUsed(bool Value) {
262 SecureLogUsed = Value;
265 void *Allocate(unsigned Size, unsigned Align = 8) {
266 return Allocator.Allocate(Size, Align);
268 void Deallocate(void *Ptr) {
272 } // end namespace llvm
274 // operator new and delete aren't allowed inside namespaces.
275 // The throw specifications are mandated by the standard.
276 /// @brief Placement new for using the MCContext's allocator.
278 /// This placement form of operator new uses the MCContext's allocator for
279 /// obtaining memory. It is a non-throwing new, which means that it returns
280 /// null on error. (If that is what the allocator does. The current does, so if
281 /// this ever changes, this operator will have to be changed, too.)
282 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
284 /// // Default alignment (16)
285 /// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
286 /// // Specific alignment
287 /// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
289 /// Please note that you cannot use delete on the pointer; it must be
290 /// deallocated using an explicit destructor call followed by
291 /// @c Context.Deallocate(Ptr).
293 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
294 /// @param C The MCContext that provides the allocator.
295 /// @param Alignment The alignment of the allocated memory (if the underlying
296 /// allocator supports it).
297 /// @return The allocated memory. Could be NULL.
298 inline void *operator new(size_t Bytes, llvm::MCContext &C,
299 size_t Alignment = 16) throw () {
300 return C.Allocate(Bytes, Alignment);
302 /// @brief Placement delete companion to the new above.
304 /// This operator is just a companion to the new above. There is no way of
305 /// invoking it directly; see the new operator for more details. This operator
306 /// is called implicitly by the compiler if a placement new expression using
307 /// the MCContext throws in the object constructor.
308 inline void operator delete(void *Ptr, llvm::MCContext &C, size_t)
313 /// This placement form of operator new[] uses the MCContext's allocator for
314 /// obtaining memory. It is a non-throwing new[], which means that it returns
316 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
318 /// // Default alignment (16)
319 /// char *data = new (Context) char[10];
320 /// // Specific alignment
321 /// char *data = new (Context, 8) char[10];
323 /// Please note that you cannot use delete on the pointer; it must be
324 /// deallocated using an explicit destructor call followed by
325 /// @c Context.Deallocate(Ptr).
327 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
328 /// @param C The MCContext that provides the allocator.
329 /// @param Alignment The alignment of the allocated memory (if the underlying
330 /// allocator supports it).
331 /// @return The allocated memory. Could be NULL.
332 inline void *operator new[](size_t Bytes, llvm::MCContext& C,
333 size_t Alignment = 16) throw () {
334 return C.Allocate(Bytes, Alignment);
337 /// @brief Placement delete[] companion to the new[] above.
339 /// This operator is just a companion to the new[] above. There is no way of
340 /// invoking it directly; see the new[] operator for more details. This operator
341 /// is called implicitly by the compiler if a placement new[] expression using
342 /// the MCContext throws in the object constructor.
343 inline void operator delete[](void *Ptr, llvm::MCContext &C) throw () {