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 /// The MCAsmInfo for this target.
46 const TargetAsmInfo *TAI;
48 /// Allocator - Allocator object used for creating machine code objects.
50 /// We use a bump pointer allocator to avoid the need to track all allocated
52 BumpPtrAllocator Allocator;
54 /// Symbols - Bindings of names to symbols.
55 StringMap<MCSymbol*, BumpPtrAllocator&> Symbols;
57 /// UsedNames - Keeps tracks of names that were used both for used declared
58 /// and artificial symbols.
59 StringMap<bool, BumpPtrAllocator&> UsedNames;
61 /// NextUniqueID - The next ID to dole out to an unnamed assembler temporary
63 unsigned NextUniqueID;
65 /// Instances of directional local labels.
66 DenseMap<unsigned, MCLabel *> Instances;
67 /// NextInstance() creates the next instance of the directional local label
68 /// for the LocalLabelVal and adds it to the map if needed.
69 unsigned NextInstance(int64_t LocalLabelVal);
70 /// GetInstance() gets the current instance of the directional local label
71 /// for the LocalLabelVal and adds it to the map if needed.
72 unsigned GetInstance(int64_t LocalLabelVal);
74 /// The file name of the log file from the environment variable
75 /// AS_SECURE_LOG_FILE. Which must be set before the .secure_log_unique
76 /// directive is used or it is an error.
78 /// The stream that gets written to for the .secure_log_unique directive.
79 raw_ostream *SecureLog;
80 /// Boolean toggled when .secure_log_unique / .secure_log_reset is seen to
81 /// catch errors if .secure_log_unique appears twice without
82 /// .secure_log_reset appearing between them.
85 /// The dwarf file and directory tables from the dwarf .file directive.
86 std::vector<MCDwarfFile *> MCDwarfFiles;
87 std::vector<StringRef> MCDwarfDirs;
89 /// The current dwarf line information from the last dwarf .loc directive.
90 MCDwarfLoc CurrentDwarfLoc;
93 /// Honor temporary labels, this is useful for debugging semantic
94 /// differences between temporary and non-temporary labels (primarily on
96 bool AllowTemporaryLabels;
98 /// The dwarf line information from the .loc directives for the sections
99 /// with assembled machine instructions have after seeing .loc directives.
100 DenseMap<const MCSection *, MCLineSection *> MCLineSections;
101 /// We need a deterministic iteration order, so we remember the order
102 /// the elements were added.
103 std::vector<const MCSection *> MCLineSectionOrder;
105 void *MachOUniquingMap, *ELFUniquingMap, *COFFUniquingMap;
107 MCSymbol *CreateSymbol(StringRef Name);
110 explicit MCContext(const MCAsmInfo &MAI, const TargetAsmInfo *TAI);
113 const MCAsmInfo &getAsmInfo() const { return MAI; }
115 const TargetAsmInfo &getTargetAsmInfo() const { return *TAI; }
117 void setAllowTemporaryLabels(bool Value) { AllowTemporaryLabels = Value; }
119 /// @name Symbol Management
122 /// CreateTempSymbol - Create and return a new assembler temporary symbol
123 /// with a unique but unspecified name.
124 MCSymbol *CreateTempSymbol();
126 /// CreateDirectionalLocalSymbol - Create the definition of a directional
127 /// local symbol for numbered label (used for "1:" definitions).
128 MCSymbol *CreateDirectionalLocalSymbol(int64_t LocalLabelVal);
130 /// GetDirectionalLocalSymbol - Create and return a directional local
131 /// symbol for numbered label (used for "1b" or 1f" references).
132 MCSymbol *GetDirectionalLocalSymbol(int64_t LocalLabelVal, int bORf);
134 /// GetOrCreateSymbol - Lookup the symbol inside with the specified
135 /// @p Name. If it exists, return it. If not, create a forward
136 /// reference and return it.
138 /// @param Name - The symbol name, which must be unique across all symbols.
139 MCSymbol *GetOrCreateSymbol(StringRef Name);
140 MCSymbol *GetOrCreateSymbol(const Twine &Name);
142 /// LookupSymbol - Get the symbol for \p Name, or null.
143 MCSymbol *LookupSymbol(StringRef Name) const;
147 /// @name Section Management
150 /// getMachOSection - Return the MCSection for the specified mach-o section.
151 /// This requires the operands to be valid.
152 const MCSectionMachO *getMachOSection(StringRef Segment,
154 unsigned TypeAndAttributes,
157 const MCSectionMachO *getMachOSection(StringRef Segment,
159 unsigned TypeAndAttributes,
161 return getMachOSection(Segment, Section, TypeAndAttributes, 0, K);
164 const MCSectionELF *getELFSection(StringRef Section, unsigned Type,
165 unsigned Flags, SectionKind Kind);
167 const MCSectionELF *getELFSection(StringRef Section, unsigned Type,
168 unsigned Flags, SectionKind Kind,
169 unsigned EntrySize, StringRef Group);
171 const MCSectionELF *CreateELFGroupSection();
173 const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
174 int Selection, SectionKind Kind);
176 const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
178 return getCOFFSection (Section, Characteristics, 0, Kind);
184 /// @name Dwarf Management
187 /// GetDwarfFile - creates an entry in the dwarf file and directory tables.
188 unsigned GetDwarfFile(StringRef FileName, unsigned FileNumber);
190 bool isValidDwarfFileNumber(unsigned FileNumber);
192 bool hasDwarfFiles() const {
193 return !MCDwarfFiles.empty();
196 const std::vector<MCDwarfFile *> &getMCDwarfFiles() {
199 const std::vector<StringRef> &getMCDwarfDirs() {
203 const DenseMap<const MCSection *, MCLineSection *>
204 &getMCLineSections() const {
205 return MCLineSections;
207 const std::vector<const MCSection *> &getMCLineSectionOrder() const {
208 return MCLineSectionOrder;
210 void addMCLineSection(const MCSection *Sec, MCLineSection *Line) {
211 MCLineSections[Sec] = Line;
212 MCLineSectionOrder.push_back(Sec);
215 /// setCurrentDwarfLoc - saves the information from the currently parsed
216 /// dwarf .loc directive and sets DwarfLocSeen. When the next instruction
217 /// is assembled an entry in the line number table with this information and
218 /// the address of the instruction will be created.
219 void setCurrentDwarfLoc(unsigned FileNum, unsigned Line, unsigned Column,
220 unsigned Flags, unsigned Isa,
221 unsigned Discriminator) {
222 CurrentDwarfLoc.setFileNum(FileNum);
223 CurrentDwarfLoc.setLine(Line);
224 CurrentDwarfLoc.setColumn(Column);
225 CurrentDwarfLoc.setFlags(Flags);
226 CurrentDwarfLoc.setIsa(Isa);
227 CurrentDwarfLoc.setDiscriminator(Discriminator);
230 void ClearDwarfLocSeen() { DwarfLocSeen = false; }
232 bool getDwarfLocSeen() { return DwarfLocSeen; }
233 const MCDwarfLoc &getCurrentDwarfLoc() { return CurrentDwarfLoc; }
237 char *getSecureLogFile() { return SecureLogFile; }
238 raw_ostream *getSecureLog() { return SecureLog; }
239 bool getSecureLogUsed() { return SecureLogUsed; }
240 void setSecureLog(raw_ostream *Value) {
243 void setSecureLogUsed(bool Value) {
244 SecureLogUsed = Value;
247 void *Allocate(unsigned Size, unsigned Align = 8) {
248 return Allocator.Allocate(Size, Align);
250 void Deallocate(void *Ptr) {
254 } // end namespace llvm
256 // operator new and delete aren't allowed inside namespaces.
257 // The throw specifications are mandated by the standard.
258 /// @brief Placement new for using the MCContext's allocator.
260 /// This placement form of operator new uses the MCContext's allocator for
261 /// obtaining memory. It is a non-throwing new, which means that it returns
262 /// null on error. (If that is what the allocator does. The current does, so if
263 /// this ever changes, this operator will have to be changed, too.)
264 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
266 /// // Default alignment (16)
267 /// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
268 /// // Specific alignment
269 /// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
271 /// Please note that you cannot use delete on the pointer; it must be
272 /// deallocated using an explicit destructor call followed by
273 /// @c Context.Deallocate(Ptr).
275 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
276 /// @param C The MCContext that provides the allocator.
277 /// @param Alignment The alignment of the allocated memory (if the underlying
278 /// allocator supports it).
279 /// @return The allocated memory. Could be NULL.
280 inline void *operator new(size_t Bytes, llvm::MCContext &C,
281 size_t Alignment = 16) throw () {
282 return C.Allocate(Bytes, Alignment);
284 /// @brief Placement delete companion to the new above.
286 /// This operator is just a companion to the new above. There is no way of
287 /// invoking it directly; see the new operator for more details. This operator
288 /// is called implicitly by the compiler if a placement new expression using
289 /// the MCContext throws in the object constructor.
290 inline void operator delete(void *Ptr, llvm::MCContext &C, size_t)
295 /// This placement form of operator new[] uses the MCContext's allocator for
296 /// obtaining memory. It is a non-throwing new[], which means that it returns
298 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
300 /// // Default alignment (16)
301 /// char *data = new (Context) char[10];
302 /// // Specific alignment
303 /// char *data = new (Context, 8) char[10];
305 /// Please note that you cannot use delete on the pointer; it must be
306 /// deallocated using an explicit destructor call followed by
307 /// @c Context.Deallocate(Ptr).
309 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
310 /// @param C The MCContext that provides the allocator.
311 /// @param Alignment The alignment of the allocated memory (if the underlying
312 /// allocator supports it).
313 /// @return The allocated memory. Could be NULL.
314 inline void *operator new[](size_t Bytes, llvm::MCContext& C,
315 size_t Alignment = 16) throw () {
316 return C.Allocate(Bytes, Alignment);
319 /// @brief Placement delete[] companion to the new[] above.
321 /// This operator is just a companion to the new[] above. There is no way of
322 /// invoking it directly; see the new[] operator for more details. This operator
323 /// is called implicitly by the compiler if a placement new[] expression using
324 /// the MCContext throws in the object constructor.
325 inline void operator delete[](void *Ptr, llvm::MCContext &C) throw () {