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.
36 /// MCContext - Context object for machine code objects. This class owns all
37 /// of the sections that it creates.
40 MCContext(const MCContext&); // DO NOT IMPLEMENT
41 MCContext &operator=(const MCContext&); // DO NOT IMPLEMENT
43 typedef StringMap<MCSymbol*, BumpPtrAllocator&> SymbolTable;
46 /// The MCAsmInfo for this target.
49 const TargetAsmInfo *TAI;
51 /// Allocator - Allocator object used for creating machine code objects.
53 /// We use a bump pointer allocator to avoid the need to track all allocated
55 BumpPtrAllocator Allocator;
57 /// Symbols - Bindings of names to symbols.
60 /// UsedNames - Keeps tracks of names that were used both for used declared
61 /// and artificial symbols.
62 StringMap<bool, BumpPtrAllocator&> UsedNames;
64 /// NextUniqueID - The next ID to dole out to an unnamed assembler temporary
66 unsigned NextUniqueID;
68 /// Instances of directional local labels.
69 DenseMap<unsigned, MCLabel *> Instances;
70 /// NextInstance() creates the next instance of the directional local label
71 /// for the LocalLabelVal and adds it to the map if needed.
72 unsigned NextInstance(int64_t LocalLabelVal);
73 /// GetInstance() gets the current instance of the directional local label
74 /// for the LocalLabelVal and adds it to the map if needed.
75 unsigned GetInstance(int64_t LocalLabelVal);
77 /// The file name of the log file from the environment variable
78 /// AS_SECURE_LOG_FILE. Which must be set before the .secure_log_unique
79 /// directive is used or it is an error.
81 /// The stream that gets written to for the .secure_log_unique directive.
82 raw_ostream *SecureLog;
83 /// Boolean toggled when .secure_log_unique / .secure_log_reset is seen to
84 /// catch errors if .secure_log_unique appears twice without
85 /// .secure_log_reset appearing between them.
88 /// The dwarf file and directory tables from the dwarf .file directive.
89 std::vector<MCDwarfFile *> MCDwarfFiles;
90 std::vector<StringRef> MCDwarfDirs;
92 /// The current dwarf line information from the last dwarf .loc directive.
93 MCDwarfLoc CurrentDwarfLoc;
96 /// Honor temporary labels, this is useful for debugging semantic
97 /// differences between temporary and non-temporary labels (primarily on
99 bool AllowTemporaryLabels;
101 /// The dwarf line information from the .loc directives for the sections
102 /// with assembled machine instructions have after seeing .loc directives.
103 DenseMap<const MCSection *, MCLineSection *> MCLineSections;
104 /// We need a deterministic iteration order, so we remember the order
105 /// the elements were added.
106 std::vector<const MCSection *> MCLineSectionOrder;
108 void *MachOUniquingMap, *ELFUniquingMap, *COFFUniquingMap;
110 MCSymbol *CreateSymbol(StringRef Name);
113 explicit MCContext(const MCAsmInfo &MAI, const TargetAsmInfo *TAI);
116 const MCAsmInfo &getAsmInfo() const { return MAI; }
118 const TargetAsmInfo &getTargetAsmInfo() const { return *TAI; }
120 void setAllowTemporaryLabels(bool Value) { AllowTemporaryLabels = Value; }
122 /// @name Symbol Management
125 /// CreateTempSymbol - Create and return a new assembler temporary symbol
126 /// with a unique but unspecified name.
127 MCSymbol *CreateTempSymbol();
129 /// CreateDirectionalLocalSymbol - Create the definition of a directional
130 /// local symbol for numbered label (used for "1:" definitions).
131 MCSymbol *CreateDirectionalLocalSymbol(int64_t LocalLabelVal);
133 /// GetDirectionalLocalSymbol - Create and return a directional local
134 /// symbol for numbered label (used for "1b" or 1f" references).
135 MCSymbol *GetDirectionalLocalSymbol(int64_t LocalLabelVal, int bORf);
137 /// GetOrCreateSymbol - Lookup the symbol inside with the specified
138 /// @p Name. If it exists, return it. If not, create a forward
139 /// reference and return it.
141 /// @param Name - The symbol name, which must be unique across all symbols.
142 MCSymbol *GetOrCreateSymbol(StringRef Name);
143 MCSymbol *GetOrCreateSymbol(const Twine &Name);
145 /// LookupSymbol - Get the symbol for \p Name, or null.
146 MCSymbol *LookupSymbol(StringRef Name) const;
148 /// getSymbols - Get a reference for the symbol table for clients that
149 /// want to, for example, iterate over all symbols. 'const' because we
150 /// still want any modifications to the table itself to use the MCContext
152 const SymbolTable &getSymbols() const {
158 /// @name Section Management
161 /// getMachOSection - Return the MCSection for the specified mach-o section.
162 /// This requires the operands to be valid.
163 const MCSectionMachO *getMachOSection(StringRef Segment,
165 unsigned TypeAndAttributes,
168 const MCSectionMachO *getMachOSection(StringRef Segment,
170 unsigned TypeAndAttributes,
172 return getMachOSection(Segment, Section, TypeAndAttributes, 0, K);
175 const MCSectionELF *getELFSection(StringRef Section, unsigned Type,
176 unsigned Flags, SectionKind Kind);
178 const MCSectionELF *getELFSection(StringRef Section, unsigned Type,
179 unsigned Flags, SectionKind Kind,
180 unsigned EntrySize, StringRef Group);
182 const MCSectionELF *CreateELFGroupSection();
184 const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
185 int Selection, SectionKind Kind);
187 const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
189 return getCOFFSection (Section, Characteristics, 0, Kind);
195 /// @name Dwarf Management
198 /// GetDwarfFile - creates an entry in the dwarf file and directory tables.
199 unsigned GetDwarfFile(StringRef FileName, unsigned FileNumber);
201 bool isValidDwarfFileNumber(unsigned FileNumber);
203 bool hasDwarfFiles() const {
204 return !MCDwarfFiles.empty();
207 const std::vector<MCDwarfFile *> &getMCDwarfFiles() {
210 const std::vector<StringRef> &getMCDwarfDirs() {
214 const DenseMap<const MCSection *, MCLineSection *>
215 &getMCLineSections() const {
216 return MCLineSections;
218 const std::vector<const MCSection *> &getMCLineSectionOrder() const {
219 return MCLineSectionOrder;
221 void addMCLineSection(const MCSection *Sec, MCLineSection *Line) {
222 MCLineSections[Sec] = Line;
223 MCLineSectionOrder.push_back(Sec);
226 /// setCurrentDwarfLoc - saves the information from the currently parsed
227 /// dwarf .loc directive and sets DwarfLocSeen. When the next instruction
228 /// is assembled an entry in the line number table with this information and
229 /// the address of the instruction will be created.
230 void setCurrentDwarfLoc(unsigned FileNum, unsigned Line, unsigned Column,
231 unsigned Flags, unsigned Isa,
232 unsigned Discriminator) {
233 CurrentDwarfLoc.setFileNum(FileNum);
234 CurrentDwarfLoc.setLine(Line);
235 CurrentDwarfLoc.setColumn(Column);
236 CurrentDwarfLoc.setFlags(Flags);
237 CurrentDwarfLoc.setIsa(Isa);
238 CurrentDwarfLoc.setDiscriminator(Discriminator);
241 void ClearDwarfLocSeen() { DwarfLocSeen = false; }
243 bool getDwarfLocSeen() { return DwarfLocSeen; }
244 const MCDwarfLoc &getCurrentDwarfLoc() { return CurrentDwarfLoc; }
248 char *getSecureLogFile() { return SecureLogFile; }
249 raw_ostream *getSecureLog() { return SecureLog; }
250 bool getSecureLogUsed() { return SecureLogUsed; }
251 void setSecureLog(raw_ostream *Value) {
254 void setSecureLogUsed(bool Value) {
255 SecureLogUsed = Value;
258 void *Allocate(unsigned Size, unsigned Align = 8) {
259 return Allocator.Allocate(Size, Align);
261 void Deallocate(void *Ptr) {
265 } // end namespace llvm
267 // operator new and delete aren't allowed inside namespaces.
268 // The throw specifications are mandated by the standard.
269 /// @brief Placement new for using the MCContext's allocator.
271 /// This placement form of operator new uses the MCContext's allocator for
272 /// obtaining memory. It is a non-throwing new, which means that it returns
273 /// null on error. (If that is what the allocator does. The current does, so if
274 /// this ever changes, this operator will have to be changed, too.)
275 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
277 /// // Default alignment (16)
278 /// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
279 /// // Specific alignment
280 /// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
282 /// Please note that you cannot use delete on the pointer; it must be
283 /// deallocated using an explicit destructor call followed by
284 /// @c Context.Deallocate(Ptr).
286 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
287 /// @param C The MCContext that provides the allocator.
288 /// @param Alignment The alignment of the allocated memory (if the underlying
289 /// allocator supports it).
290 /// @return The allocated memory. Could be NULL.
291 inline void *operator new(size_t Bytes, llvm::MCContext &C,
292 size_t Alignment = 16) throw () {
293 return C.Allocate(Bytes, Alignment);
295 /// @brief Placement delete companion to the new above.
297 /// This operator is just a companion to the new above. There is no way of
298 /// invoking it directly; see the new operator for more details. This operator
299 /// is called implicitly by the compiler if a placement new expression using
300 /// the MCContext throws in the object constructor.
301 inline void operator delete(void *Ptr, llvm::MCContext &C, size_t)
306 /// This placement form of operator new[] uses the MCContext's allocator for
307 /// obtaining memory. It is a non-throwing new[], which means that it returns
309 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
311 /// // Default alignment (16)
312 /// char *data = new (Context) char[10];
313 /// // Specific alignment
314 /// char *data = new (Context, 8) char[10];
316 /// Please note that you cannot use delete on the pointer; it must be
317 /// deallocated using an explicit destructor call followed by
318 /// @c Context.Deallocate(Ptr).
320 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
321 /// @param C The MCContext that provides the allocator.
322 /// @param Alignment The alignment of the allocated memory (if the underlying
323 /// allocator supports it).
324 /// @return The allocated memory. Could be NULL.
325 inline void *operator new[](size_t Bytes, llvm::MCContext& C,
326 size_t Alignment = 16) throw () {
327 return C.Allocate(Bytes, Alignment);
330 /// @brief Placement delete[] companion to the new[] above.
332 /// This operator is just a companion to the new[] above. There is no way of
333 /// invoking it directly; see the new[] operator for more details. This operator
334 /// is called implicitly by the compiler if a placement new[] expression using
335 /// the MCContext throws in the object constructor.
336 inline void operator delete[](void *Ptr, llvm::MCContext &C) throw () {