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.
35 /// MCContext - Context object for machine code objects. This class owns all
36 /// of the sections that it creates.
39 MCContext(const MCContext&); // DO NOT IMPLEMENT
40 MCContext &operator=(const MCContext&); // DO NOT IMPLEMENT
42 /// The MCAsmInfo for this target.
45 /// Symbols - Bindings of names to symbols.
46 StringMap<MCSymbol*> Symbols;
48 /// UsedNames - Keeps tracks of names that were used both for used declared
49 /// and artificial symbols.
50 StringMap<bool> UsedNames;
52 /// NextUniqueID - The next ID to dole out to an unnamed assembler temporary
54 unsigned NextUniqueID;
56 /// Instances of directional local labels.
57 DenseMap<unsigned, MCLabel *> Instances;
58 /// NextInstance() creates the next instance of the directional local label
59 /// for the LocalLabelVal and adds it to the map if needed.
60 unsigned NextInstance(int64_t LocalLabelVal);
61 /// GetInstance() gets the current instance of the directional local label
62 /// for the LocalLabelVal and adds it to the map if needed.
63 unsigned GetInstance(int64_t LocalLabelVal);
65 /// The file name of the log file from the environment variable
66 /// AS_SECURE_LOG_FILE. Which must be set before the .secure_log_unique
67 /// directive is used or it is an error.
69 /// The stream that gets written to for the .secure_log_unique directive.
70 raw_ostream *SecureLog;
71 /// Boolean toggled when .secure_log_unique / .secure_log_reset is seen to
72 /// catch errors if .secure_log_unique appears twice without
73 /// .secure_log_reset appearing between them.
76 /// The dwarf file and directory tables from the dwarf .file directive.
77 std::vector<MCDwarfFile *> MCDwarfFiles;
78 std::vector<StringRef> MCDwarfDirs;
80 /// The current dwarf line information from the last dwarf .loc directive.
81 MCDwarfLoc CurrentDwarfLoc;
84 /// The dwarf line information from the .loc directives for the sections
85 /// with assembled machine instructions have after seeing .loc directives.
86 DenseMap<const MCSection *, MCLineSection *> MCLineSections;
87 /// We need a deterministic iteration order, so we remember the order
88 /// the elements were added.
89 std::vector<const MCSection *> MCLineSectionOrder;
91 /// Allocator - Allocator object used for creating machine code objects.
93 /// We use a bump pointer allocator to avoid the need to track all allocated
95 BumpPtrAllocator Allocator;
97 void *MachOUniquingMap, *ELFUniquingMap, *COFFUniquingMap;
99 MCSymbol *CreateSymbol(StringRef Name);
102 explicit MCContext(const MCAsmInfo &MAI);
105 const MCAsmInfo &getAsmInfo() const { return MAI; }
107 /// @name Symbol Management
110 /// CreateTempSymbol - Create and return a new assembler temporary symbol
111 /// with a unique but unspecified name.
112 MCSymbol *CreateTempSymbol();
114 /// CreateDirectionalLocalSymbol - Create the definition of a directional
115 /// local symbol for numbered label (used for "1:" definitions).
116 MCSymbol *CreateDirectionalLocalSymbol(int64_t LocalLabelVal);
118 /// GetDirectionalLocalSymbol - Create and return a directional local
119 /// symbol for numbered label (used for "1b" or 1f" references).
120 MCSymbol *GetDirectionalLocalSymbol(int64_t LocalLabelVal, int bORf);
122 /// GetOrCreateSymbol - Lookup the symbol inside with the specified
123 /// @p Name. If it exists, return it. If not, create a forward
124 /// reference and return it.
126 /// @param Name - The symbol name, which must be unique across all symbols.
127 MCSymbol *GetOrCreateSymbol(StringRef Name);
128 MCSymbol *GetOrCreateSymbol(const Twine &Name);
130 /// LookupSymbol - Get the symbol for \p Name, or null.
131 MCSymbol *LookupSymbol(StringRef Name) const;
135 /// @name Section Management
138 /// getMachOSection - Return the MCSection for the specified mach-o section.
139 /// This requires the operands to be valid.
140 const MCSectionMachO *getMachOSection(StringRef Segment,
142 unsigned TypeAndAttributes,
145 const MCSectionMachO *getMachOSection(StringRef Segment,
147 unsigned TypeAndAttributes,
149 return getMachOSection(Segment, Section, TypeAndAttributes, 0, K);
152 const MCSectionELF *getELFSection(StringRef Section, unsigned Type,
153 unsigned Flags, SectionKind Kind);
155 const MCSectionELF *getELFSection(StringRef Section, unsigned Type,
156 unsigned Flags, SectionKind Kind,
157 unsigned EntrySize, StringRef Group);
159 const MCSectionELF *CreateELFGroupSection();
161 const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
162 int Selection, SectionKind Kind);
164 const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
166 return getCOFFSection (Section, Characteristics, 0, Kind);
172 /// @name Dwarf Management
175 /// GetDwarfFile - creates an entry in the dwarf file and directory tables.
176 unsigned GetDwarfFile(StringRef FileName, unsigned FileNumber);
178 bool isValidDwarfFileNumber(unsigned FileNumber);
180 bool hasDwarfFiles(void) {
181 return MCDwarfFiles.size() != 0;
183 bool hasDwarfLines(void) {
184 return MCLineSectionOrder.size() != 0;
187 const std::vector<MCDwarfFile *> &getMCDwarfFiles() {
190 const std::vector<StringRef> &getMCDwarfDirs() {
194 const DenseMap<const MCSection *, MCLineSection *>
195 &getMCLineSections() const {
196 return MCLineSections;
198 const std::vector<const MCSection *> &getMCLineSectionOrder() const {
199 return MCLineSectionOrder;
201 void addMCLineSection(const MCSection *Sec, MCLineSection *Line) {
202 MCLineSections[Sec] = Line;
203 MCLineSectionOrder.push_back(Sec);
206 /// setCurrentDwarfLoc - saves the information from the currently parsed
207 /// dwarf .loc directive and sets DwarfLocSeen. When the next instruction
208 /// is assembled an entry in the line number table with this information and
209 /// the address of the instruction will be created.
210 void setCurrentDwarfLoc(unsigned FileNum, unsigned Line, unsigned Column,
211 unsigned Flags, unsigned Isa,
212 unsigned Discriminator) {
213 CurrentDwarfLoc.setFileNum(FileNum);
214 CurrentDwarfLoc.setLine(Line);
215 CurrentDwarfLoc.setColumn(Column);
216 CurrentDwarfLoc.setFlags(Flags);
217 CurrentDwarfLoc.setIsa(Isa);
218 CurrentDwarfLoc.setDiscriminator(Discriminator);
221 void ClearDwarfLocSeen() { DwarfLocSeen = false; }
223 bool getDwarfLocSeen() { return DwarfLocSeen; }
224 const MCDwarfLoc &getCurrentDwarfLoc() { return CurrentDwarfLoc; }
228 char *getSecureLogFile() { return SecureLogFile; }
229 raw_ostream *getSecureLog() { return SecureLog; }
230 bool getSecureLogUsed() { return SecureLogUsed; }
231 void setSecureLog(raw_ostream *Value) {
234 void setSecureLogUsed(bool Value) {
235 SecureLogUsed = Value;
238 void *Allocate(unsigned Size, unsigned Align = 8) {
239 return Allocator.Allocate(Size, Align);
241 void Deallocate(void *Ptr) {
245 } // end namespace llvm
247 // operator new and delete aren't allowed inside namespaces.
248 // The throw specifications are mandated by the standard.
249 /// @brief Placement new for using the MCContext's allocator.
251 /// This placement form of operator new uses the MCContext's allocator for
252 /// obtaining memory. It is a non-throwing new, which means that it returns
253 /// null on error. (If that is what the allocator does. The current does, so if
254 /// this ever changes, this operator will have to be changed, too.)
255 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
257 /// // Default alignment (16)
258 /// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
259 /// // Specific alignment
260 /// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
262 /// Please note that you cannot use delete on the pointer; it must be
263 /// deallocated using an explicit destructor call followed by
264 /// @c Context.Deallocate(Ptr).
266 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
267 /// @param C The MCContext that provides the allocator.
268 /// @param Alignment The alignment of the allocated memory (if the underlying
269 /// allocator supports it).
270 /// @return The allocated memory. Could be NULL.
271 inline void *operator new(size_t Bytes, llvm::MCContext &C,
272 size_t Alignment = 16) throw () {
273 return C.Allocate(Bytes, Alignment);
275 /// @brief Placement delete companion to the new above.
277 /// This operator is just a companion to the new above. There is no way of
278 /// invoking it directly; see the new operator for more details. This operator
279 /// is called implicitly by the compiler if a placement new expression using
280 /// the MCContext throws in the object constructor.
281 inline void operator delete(void *Ptr, llvm::MCContext &C, size_t)
286 /// This placement form of operator new[] uses the MCContext's allocator for
287 /// obtaining memory. It is a non-throwing new[], which means that it returns
289 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
291 /// // Default alignment (16)
292 /// char *data = new (Context) char[10];
293 /// // Specific alignment
294 /// char *data = new (Context, 8) char[10];
296 /// Please note that you cannot use delete on the pointer; it must be
297 /// deallocated using an explicit destructor call followed by
298 /// @c Context.Deallocate(Ptr).
300 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
301 /// @param C The MCContext that provides the allocator.
302 /// @param Alignment The alignment of the allocated memory (if the underlying
303 /// allocator supports it).
304 /// @return The allocated memory. Could be NULL.
305 inline void *operator new[](size_t Bytes, llvm::MCContext& C,
306 size_t Alignment = 16) throw () {
307 return C.Allocate(Bytes, Alignment);
310 /// @brief Placement delete[] companion to the new[] above.
312 /// This operator is just a companion to the new[] above. There is no way of
313 /// invoking it directly; see the new[] operator for more details. This operator
314 /// is called implicitly by the compiler if a placement new[] expression using
315 /// the MCContext throws in the object constructor.
316 inline void operator delete[](void *Ptr, llvm::MCContext &C) throw () {