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() const {
181 return !MCDwarfFiles.empty();
184 const std::vector<MCDwarfFile *> &getMCDwarfFiles() {
187 const std::vector<StringRef> &getMCDwarfDirs() {
191 const DenseMap<const MCSection *, MCLineSection *>
192 &getMCLineSections() const {
193 return MCLineSections;
195 const std::vector<const MCSection *> &getMCLineSectionOrder() const {
196 return MCLineSectionOrder;
198 void addMCLineSection(const MCSection *Sec, MCLineSection *Line) {
199 MCLineSections[Sec] = Line;
200 MCLineSectionOrder.push_back(Sec);
203 /// setCurrentDwarfLoc - saves the information from the currently parsed
204 /// dwarf .loc directive and sets DwarfLocSeen. When the next instruction
205 /// is assembled an entry in the line number table with this information and
206 /// the address of the instruction will be created.
207 void setCurrentDwarfLoc(unsigned FileNum, unsigned Line, unsigned Column,
208 unsigned Flags, unsigned Isa,
209 unsigned Discriminator) {
210 CurrentDwarfLoc.setFileNum(FileNum);
211 CurrentDwarfLoc.setLine(Line);
212 CurrentDwarfLoc.setColumn(Column);
213 CurrentDwarfLoc.setFlags(Flags);
214 CurrentDwarfLoc.setIsa(Isa);
215 CurrentDwarfLoc.setDiscriminator(Discriminator);
218 void ClearDwarfLocSeen() { DwarfLocSeen = false; }
220 bool getDwarfLocSeen() { return DwarfLocSeen; }
221 const MCDwarfLoc &getCurrentDwarfLoc() { return CurrentDwarfLoc; }
225 char *getSecureLogFile() { return SecureLogFile; }
226 raw_ostream *getSecureLog() { return SecureLog; }
227 bool getSecureLogUsed() { return SecureLogUsed; }
228 void setSecureLog(raw_ostream *Value) {
231 void setSecureLogUsed(bool Value) {
232 SecureLogUsed = Value;
235 void *Allocate(unsigned Size, unsigned Align = 8) {
236 return Allocator.Allocate(Size, Align);
238 void Deallocate(void *Ptr) {
242 } // end namespace llvm
244 // operator new and delete aren't allowed inside namespaces.
245 // The throw specifications are mandated by the standard.
246 /// @brief Placement new for using the MCContext's allocator.
248 /// This placement form of operator new uses the MCContext's allocator for
249 /// obtaining memory. It is a non-throwing new, which means that it returns
250 /// null on error. (If that is what the allocator does. The current does, so if
251 /// this ever changes, this operator will have to be changed, too.)
252 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
254 /// // Default alignment (16)
255 /// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
256 /// // Specific alignment
257 /// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
259 /// Please note that you cannot use delete on the pointer; it must be
260 /// deallocated using an explicit destructor call followed by
261 /// @c Context.Deallocate(Ptr).
263 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
264 /// @param C The MCContext that provides the allocator.
265 /// @param Alignment The alignment of the allocated memory (if the underlying
266 /// allocator supports it).
267 /// @return The allocated memory. Could be NULL.
268 inline void *operator new(size_t Bytes, llvm::MCContext &C,
269 size_t Alignment = 16) throw () {
270 return C.Allocate(Bytes, Alignment);
272 /// @brief Placement delete companion to the new above.
274 /// This operator is just a companion to the new above. There is no way of
275 /// invoking it directly; see the new operator for more details. This operator
276 /// is called implicitly by the compiler if a placement new expression using
277 /// the MCContext throws in the object constructor.
278 inline void operator delete(void *Ptr, llvm::MCContext &C, size_t)
283 /// This placement form of operator new[] uses the MCContext's allocator for
284 /// obtaining memory. It is a non-throwing new[], which means that it returns
286 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
288 /// // Default alignment (16)
289 /// char *data = new (Context) char[10];
290 /// // Specific alignment
291 /// char *data = new (Context, 8) char[10];
293 /// Please note that you cannot use delete on the pointer; it must be
294 /// deallocated using an explicit destructor call followed by
295 /// @c Context.Deallocate(Ptr).
297 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
298 /// @param C The MCContext that provides the allocator.
299 /// @param Alignment The alignment of the allocated memory (if the underlying
300 /// allocator supports it).
301 /// @return The allocated memory. Could be NULL.
302 inline void *operator new[](size_t Bytes, llvm::MCContext& C,
303 size_t Alignment = 16) throw () {
304 return C.Allocate(Bytes, Alignment);
307 /// @brief Placement delete[] companion to the new[] above.
309 /// This operator is just a companion to the new[] above. There is no way of
310 /// invoking it directly; see the new[] operator for more details. This operator
311 /// is called implicitly by the compiler if a placement new[] expression using
312 /// the MCContext throws in the object constructor.
313 inline void operator delete[](void *Ptr, llvm::MCContext &C) throw () {