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 /// Symbols - Bindings of names to symbols.
49 StringMap<MCSymbol*> Symbols;
51 /// UsedNames - Keeps tracks of names that were used both for used declared
52 /// and artificial symbols.
53 StringMap<bool> UsedNames;
55 /// NextUniqueID - The next ID to dole out to an unnamed assembler temporary
57 unsigned NextUniqueID;
59 /// Instances of directional local labels.
60 DenseMap<unsigned, MCLabel *> Instances;
61 /// NextInstance() creates the next instance of the directional local label
62 /// for the LocalLabelVal and adds it to the map if needed.
63 unsigned NextInstance(int64_t LocalLabelVal);
64 /// GetInstance() gets the current instance of the directional local label
65 /// for the LocalLabelVal and adds it to the map if needed.
66 unsigned GetInstance(int64_t LocalLabelVal);
68 /// The file name of the log file from the environment variable
69 /// AS_SECURE_LOG_FILE. Which must be set before the .secure_log_unique
70 /// directive is used or it is an error.
72 /// The stream that gets written to for the .secure_log_unique directive.
73 raw_ostream *SecureLog;
74 /// Boolean toggled when .secure_log_unique / .secure_log_reset is seen to
75 /// catch errors if .secure_log_unique appears twice without
76 /// .secure_log_reset appearing between them.
79 /// The dwarf file and directory tables from the dwarf .file directive.
80 std::vector<MCDwarfFile *> MCDwarfFiles;
81 std::vector<StringRef> MCDwarfDirs;
83 /// The current dwarf line information from the last dwarf .loc directive.
84 MCDwarfLoc CurrentDwarfLoc;
87 /// The dwarf line information from the .loc directives for the sections
88 /// with assembled machine instructions have after seeing .loc directives.
89 DenseMap<const MCSection *, MCLineSection *> MCLineSections;
90 /// We need a deterministic iteration order, so we remember the order
91 /// the elements were added.
92 std::vector<const MCSection *> MCLineSectionOrder;
94 /// Allocator - Allocator object used for creating machine code objects.
96 /// We use a bump pointer allocator to avoid the need to track all allocated
98 BumpPtrAllocator Allocator;
100 void *MachOUniquingMap, *ELFUniquingMap, *COFFUniquingMap;
102 MCSymbol *CreateSymbol(StringRef Name);
105 explicit MCContext(const MCAsmInfo &MAI, const TargetAsmInfo *TAI);
108 const MCAsmInfo &getAsmInfo() const { return MAI; }
110 const TargetAsmInfo &getTargetAsmInfo() const { return *TAI; }
112 /// @name Symbol Management
115 /// CreateTempSymbol - Create and return a new assembler temporary symbol
116 /// with a unique but unspecified name.
117 MCSymbol *CreateTempSymbol();
119 /// CreateDirectionalLocalSymbol - Create the definition of a directional
120 /// local symbol for numbered label (used for "1:" definitions).
121 MCSymbol *CreateDirectionalLocalSymbol(int64_t LocalLabelVal);
123 /// GetDirectionalLocalSymbol - Create and return a directional local
124 /// symbol for numbered label (used for "1b" or 1f" references).
125 MCSymbol *GetDirectionalLocalSymbol(int64_t LocalLabelVal, int bORf);
127 /// GetOrCreateSymbol - Lookup the symbol inside with the specified
128 /// @p Name. If it exists, return it. If not, create a forward
129 /// reference and return it.
131 /// @param Name - The symbol name, which must be unique across all symbols.
132 MCSymbol *GetOrCreateSymbol(StringRef Name);
133 MCSymbol *GetOrCreateSymbol(const Twine &Name);
135 /// LookupSymbol - Get the symbol for \p Name, or null.
136 MCSymbol *LookupSymbol(StringRef Name) const;
140 /// @name Section Management
143 /// getMachOSection - Return the MCSection for the specified mach-o section.
144 /// This requires the operands to be valid.
145 const MCSectionMachO *getMachOSection(StringRef Segment,
147 unsigned TypeAndAttributes,
150 const MCSectionMachO *getMachOSection(StringRef Segment,
152 unsigned TypeAndAttributes,
154 return getMachOSection(Segment, Section, TypeAndAttributes, 0, K);
157 const MCSectionELF *getELFSection(StringRef Section, unsigned Type,
158 unsigned Flags, SectionKind Kind);
160 const MCSectionELF *getELFSection(StringRef Section, unsigned Type,
161 unsigned Flags, SectionKind Kind,
162 unsigned EntrySize, StringRef Group);
164 const MCSectionELF *CreateELFGroupSection();
166 const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
167 int Selection, SectionKind Kind);
169 const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
171 return getCOFFSection (Section, Characteristics, 0, Kind);
177 /// @name Dwarf Management
180 /// GetDwarfFile - creates an entry in the dwarf file and directory tables.
181 unsigned GetDwarfFile(StringRef FileName, unsigned FileNumber);
183 bool isValidDwarfFileNumber(unsigned FileNumber);
185 bool hasDwarfFiles() const {
186 return !MCDwarfFiles.empty();
189 const std::vector<MCDwarfFile *> &getMCDwarfFiles() {
192 const std::vector<StringRef> &getMCDwarfDirs() {
196 const DenseMap<const MCSection *, MCLineSection *>
197 &getMCLineSections() const {
198 return MCLineSections;
200 const std::vector<const MCSection *> &getMCLineSectionOrder() const {
201 return MCLineSectionOrder;
203 void addMCLineSection(const MCSection *Sec, MCLineSection *Line) {
204 MCLineSections[Sec] = Line;
205 MCLineSectionOrder.push_back(Sec);
208 /// setCurrentDwarfLoc - saves the information from the currently parsed
209 /// dwarf .loc directive and sets DwarfLocSeen. When the next instruction
210 /// is assembled an entry in the line number table with this information and
211 /// the address of the instruction will be created.
212 void setCurrentDwarfLoc(unsigned FileNum, unsigned Line, unsigned Column,
213 unsigned Flags, unsigned Isa,
214 unsigned Discriminator) {
215 CurrentDwarfLoc.setFileNum(FileNum);
216 CurrentDwarfLoc.setLine(Line);
217 CurrentDwarfLoc.setColumn(Column);
218 CurrentDwarfLoc.setFlags(Flags);
219 CurrentDwarfLoc.setIsa(Isa);
220 CurrentDwarfLoc.setDiscriminator(Discriminator);
223 void ClearDwarfLocSeen() { DwarfLocSeen = false; }
225 bool getDwarfLocSeen() { return DwarfLocSeen; }
226 const MCDwarfLoc &getCurrentDwarfLoc() { return CurrentDwarfLoc; }
230 char *getSecureLogFile() { return SecureLogFile; }
231 raw_ostream *getSecureLog() { return SecureLog; }
232 bool getSecureLogUsed() { return SecureLogUsed; }
233 void setSecureLog(raw_ostream *Value) {
236 void setSecureLogUsed(bool Value) {
237 SecureLogUsed = Value;
240 void *Allocate(unsigned Size, unsigned Align = 8) {
241 return Allocator.Allocate(Size, Align);
243 void Deallocate(void *Ptr) {
247 } // end namespace llvm
249 // operator new and delete aren't allowed inside namespaces.
250 // The throw specifications are mandated by the standard.
251 /// @brief Placement new for using the MCContext's allocator.
253 /// This placement form of operator new uses the MCContext's allocator for
254 /// obtaining memory. It is a non-throwing new, which means that it returns
255 /// null on error. (If that is what the allocator does. The current does, so if
256 /// this ever changes, this operator will have to be changed, too.)
257 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
259 /// // Default alignment (16)
260 /// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
261 /// // Specific alignment
262 /// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
264 /// Please note that you cannot use delete on the pointer; it must be
265 /// deallocated using an explicit destructor call followed by
266 /// @c Context.Deallocate(Ptr).
268 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
269 /// @param C The MCContext that provides the allocator.
270 /// @param Alignment The alignment of the allocated memory (if the underlying
271 /// allocator supports it).
272 /// @return The allocated memory. Could be NULL.
273 inline void *operator new(size_t Bytes, llvm::MCContext &C,
274 size_t Alignment = 16) throw () {
275 return C.Allocate(Bytes, Alignment);
277 /// @brief Placement delete companion to the new above.
279 /// This operator is just a companion to the new above. There is no way of
280 /// invoking it directly; see the new operator for more details. This operator
281 /// is called implicitly by the compiler if a placement new expression using
282 /// the MCContext throws in the object constructor.
283 inline void operator delete(void *Ptr, llvm::MCContext &C, size_t)
288 /// This placement form of operator new[] uses the MCContext's allocator for
289 /// obtaining memory. It is a non-throwing new[], which means that it returns
291 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
293 /// // Default alignment (16)
294 /// char *data = new (Context) char[10];
295 /// // Specific alignment
296 /// char *data = new (Context, 8) char[10];
298 /// Please note that you cannot use delete on the pointer; it must be
299 /// deallocated using an explicit destructor call followed by
300 /// @c Context.Deallocate(Ptr).
302 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
303 /// @param C The MCContext that provides the allocator.
304 /// @param Alignment The alignment of the allocated memory (if the underlying
305 /// allocator supports it).
306 /// @return The allocated memory. Could be NULL.
307 inline void *operator new[](size_t Bytes, llvm::MCContext& C,
308 size_t Alignment = 16) throw () {
309 return C.Allocate(Bytes, Alignment);
312 /// @brief Placement delete[] companion to the new[] above.
314 /// This operator is just a companion to the new[] above. There is no way of
315 /// invoking it directly; see the new[] operator for more details. This operator
316 /// is called implicitly by the compiler if a placement new[] expression using
317 /// the MCContext throws in the object constructor.
318 inline void operator delete[](void *Ptr, llvm::MCContext &C) throw () {