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.
33 /// MCContext - Context object for machine code objects. This class owns all
34 /// of the sections that it creates.
37 MCContext(const MCContext&); // DO NOT IMPLEMENT
38 MCContext &operator=(const MCContext&); // DO NOT IMPLEMENT
40 /// The MCAsmInfo for this target.
43 /// Symbols - Bindings of names to symbols.
44 StringMap<MCSymbol*> Symbols;
46 /// NextUniqueID - The next ID to dole out to an unnamed assembler temporary
48 unsigned NextUniqueID;
50 /// Instances of directional local labels.
51 DenseMap<unsigned, MCLabel *> Instances;
52 /// NextInstance() creates the next instance of the directional local label
53 /// for the LocalLabelVal and adds it to the map if needed.
54 unsigned NextInstance(int64_t LocalLabelVal);
55 /// GetInstance() gets the current instance of the directional local label
56 /// for the LocalLabelVal and adds it to the map if needed.
57 unsigned GetInstance(int64_t LocalLabelVal);
59 /// The file name of the log file from the enviromment variable
60 /// AS_SECURE_LOG_FILE. Which must be set before the .secure_log_unique
61 /// directive is used or it is an error.
63 /// The stream that gets written to for the .secure_log_unique directive.
64 raw_ostream *SecureLog;
65 /// Boolean toggled when .secure_log_unique / .secure_log_reset is seen to
66 /// catch errors if .secure_log_unique appears twice without
67 /// .secure_log_reset appearing between them.
70 /// The dwarf file and directory tables from the dwarf .file directive.
71 std::vector<MCDwarfFile *> MCDwarfFiles;
72 std::vector<StringRef> MCDwarfDirs;
74 /// The current dwarf line information from the last dwarf .loc directive.
75 MCDwarfLoc CurrentDwarfLoc;
78 /// Allocator - Allocator object used for creating machine code objects.
80 /// We use a bump pointer allocator to avoid the need to track all allocated
82 BumpPtrAllocator Allocator;
84 void *MachOUniquingMap, *ELFUniquingMap, *COFFUniquingMap;
86 explicit MCContext(const MCAsmInfo &MAI);
89 const MCAsmInfo &getAsmInfo() const { return MAI; }
91 /// @name Symbol Managment
94 /// CreateTempSymbol - Create and return a new assembler temporary symbol
95 /// with a unique but unspecified name.
96 MCSymbol *CreateTempSymbol();
98 /// CreateDirectionalLocalSymbol - Create the defintion of a directional
99 /// local symbol for numbered label (used for "1:" defintions).
100 MCSymbol *CreateDirectionalLocalSymbol(int64_t LocalLabelVal);
102 /// GetDirectionalLocalSymbol - Create and return a directional local
103 /// symbol for numbered label (used for "1b" or 1f" references).
104 MCSymbol *GetDirectionalLocalSymbol(int64_t LocalLabelVal, int bORf);
106 /// GetOrCreateSymbol - Lookup the symbol inside with the specified
107 /// @p Name. If it exists, return it. If not, create a forward
108 /// reference and return it.
110 /// @param Name - The symbol name, which must be unique across all symbols.
111 MCSymbol *GetOrCreateSymbol(StringRef Name);
112 MCSymbol *GetOrCreateSymbol(const Twine &Name);
114 /// LookupSymbol - Get the symbol for \p Name, or null.
115 MCSymbol *LookupSymbol(StringRef Name) const;
119 /// @name Section Managment
122 /// getMachOSection - Return the MCSection for the specified mach-o section.
123 /// This requires the operands to be valid.
124 const MCSectionMachO *getMachOSection(StringRef Segment,
126 unsigned TypeAndAttributes,
129 const MCSectionMachO *getMachOSection(StringRef Segment,
131 unsigned TypeAndAttributes,
133 return getMachOSection(Segment, Section, TypeAndAttributes, 0, K);
136 const MCSection *getELFSection(StringRef Section, unsigned Type,
137 unsigned Flags, SectionKind Kind,
138 bool IsExplicit = false,
139 unsigned EntrySize = 0);
141 const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
142 int Selection, SectionKind Kind);
144 const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
146 return getCOFFSection (Section, Characteristics, 0, Kind);
152 /// @name Dwarf Managment
155 /// GetDwarfFile - creates an entry in the dwarf file and directory tables.
156 unsigned GetDwarfFile(StringRef FileName, unsigned FileNumber);
158 bool ValidateDwarfFileNumber(unsigned FileNumber);
160 const std::vector<MCDwarfFile *> &getMCDwarfFiles() {
163 const std::vector<StringRef> &getMCDwarfDirs() {
167 /// setCurrentDwarfLoc - saves the information from the currently parsed
168 /// dwarf .loc directive and sets DwarfLocSeen. When the next instruction /// is assembled an entry in the line number table with this information and
169 /// the address of the instruction will be created.
170 void setCurrentDwarfLoc(unsigned FileNum, unsigned Line, unsigned Column,
171 unsigned Flags, unsigned Isa) {
172 CurrentDwarfLoc.setFileNum(FileNum);
173 CurrentDwarfLoc.setLine(Line);
174 CurrentDwarfLoc.setColumn(Column);
175 CurrentDwarfLoc.setFlags(Flags);
176 CurrentDwarfLoc.setIsa(Isa);
182 char *getSecureLogFile() { return SecureLogFile; }
183 raw_ostream *getSecureLog() { return SecureLog; }
184 bool getSecureLogUsed() { return SecureLogUsed; }
185 void setSecureLog(raw_ostream *Value) {
188 void setSecureLogUsed(bool Value) {
189 SecureLogUsed = Value;
192 void *Allocate(unsigned Size, unsigned Align = 8) {
193 return Allocator.Allocate(Size, Align);
195 void Deallocate(void *Ptr) {
199 } // end namespace llvm
201 // operator new and delete aren't allowed inside namespaces.
202 // The throw specifications are mandated by the standard.
203 /// @brief Placement new for using the MCContext's allocator.
205 /// This placement form of operator new uses the MCContext's allocator for
206 /// obtaining memory. It is a non-throwing new, which means that it returns
207 /// null on error. (If that is what the allocator does. The current does, so if
208 /// this ever changes, this operator will have to be changed, too.)
209 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
211 /// // Default alignment (16)
212 /// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
213 /// // Specific alignment
214 /// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
216 /// Please note that you cannot use delete on the pointer; it must be
217 /// deallocated using an explicit destructor call followed by
218 /// @c Context.Deallocate(Ptr).
220 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
221 /// @param C The MCContext that provides the allocator.
222 /// @param Alignment The alignment of the allocated memory (if the underlying
223 /// allocator supports it).
224 /// @return The allocated memory. Could be NULL.
225 inline void *operator new(size_t Bytes, llvm::MCContext &C,
226 size_t Alignment = 16) throw () {
227 return C.Allocate(Bytes, Alignment);
229 /// @brief Placement delete companion to the new above.
231 /// This operator is just a companion to the new above. There is no way of
232 /// invoking it directly; see the new operator for more details. This operator
233 /// is called implicitly by the compiler if a placement new expression using
234 /// the MCContext throws in the object constructor.
235 inline void operator delete(void *Ptr, llvm::MCContext &C, size_t)
240 /// This placement form of operator new[] uses the MCContext's allocator for
241 /// obtaining memory. It is a non-throwing new[], which means that it returns
243 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
245 /// // Default alignment (16)
246 /// char *data = new (Context) char[10];
247 /// // Specific alignment
248 /// char *data = new (Context, 8) char[10];
250 /// Please note that you cannot use delete on the pointer; it must be
251 /// deallocated using an explicit destructor call followed by
252 /// @c Context.Deallocate(Ptr).
254 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
255 /// @param C The MCContext that provides the allocator.
256 /// @param Alignment The alignment of the allocated memory (if the underlying
257 /// allocator supports it).
258 /// @return The allocated memory. Could be NULL.
259 inline void *operator new[](size_t Bytes, llvm::MCContext& C,
260 size_t Alignment = 16) throw () {
261 return C.Allocate(Bytes, Alignment);
264 /// @brief Placement delete[] companion to the new[] above.
266 /// This operator is just a companion to the new[] above. There is no way of
267 /// invoking it directly; see the new[] operator for more details. This operator
268 /// is called implicitly by the compiler if a placement new[] expression using
269 /// the MCContext throws in the object constructor.
270 inline void operator delete[](void *Ptr, llvm::MCContext &C) throw () {