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/ADT/DenseMap.h"
15 #include "llvm/ADT/StringMap.h"
16 #include "llvm/Support/Allocator.h"
17 #include "llvm/Support/raw_ostream.h"
18 #include <vector> // FIXME: Shouldn't be needed.
31 /// MCContext - Context object for machine code objects. This class owns all
32 /// of the sections that it creates.
35 MCContext(const MCContext&); // DO NOT IMPLEMENT
36 MCContext &operator=(const MCContext&); // DO NOT IMPLEMENT
38 /// The MCAsmInfo for this target.
41 /// Symbols - Bindings of names to symbols.
42 StringMap<MCSymbol*> Symbols;
44 /// NextUniqueID - The next ID to dole out to an unnamed assembler temporary
46 unsigned NextUniqueID;
48 /// Instances of directional local labels.
49 DenseMap<unsigned, MCLabel *> Instances;
50 /// NextInstance() creates the next instance of the directional local label
51 /// for the LocalLabelVal and adds it to the map if needed.
52 unsigned NextInstance(int64_t LocalLabelVal);
53 /// GetInstance() gets the current instance of the directional local label
54 /// for the LocalLabelVal and adds it to the map if needed.
55 unsigned GetInstance(int64_t LocalLabelVal);
57 /// The file name of the log file from the enviromment variable
58 /// AS_SECURE_LOG_FILE. Which must be set before the .secure_log_unique
59 /// directive is used or it is an error.
61 /// The stream that gets written to for the .secure_log_unique directive.
62 raw_ostream *SecureLog;
63 /// Boolean toggled when .secure_log_unique / .secure_log_reset is seen to
64 /// catch errors if .secure_log_unique appears twice without
65 /// .secure_log_reset appearing between them.
68 /// The dwarf file and directory tables from the dwarf .file directive.
69 std::vector<MCDwarfFile *> MCDwarfFiles;
70 std::vector<StringRef> MCDwarfDirs;
72 /// Allocator - Allocator object used for creating machine code objects.
74 /// We use a bump pointer allocator to avoid the need to track all allocated
76 BumpPtrAllocator Allocator;
78 void *MachOUniquingMap, *ELFUniquingMap, *COFFUniquingMap;
80 explicit MCContext(const MCAsmInfo &MAI);
83 const MCAsmInfo &getAsmInfo() const { return MAI; }
85 /// @name Symbol Managment
88 /// CreateTempSymbol - Create and return a new assembler temporary symbol
89 /// with a unique but unspecified name.
90 MCSymbol *CreateTempSymbol();
92 /// CreateDirectionalLocalSymbol - Create the defintion of a directional
93 /// local symbol for numbered label (used for "1:" defintions).
94 MCSymbol *CreateDirectionalLocalSymbol(int64_t LocalLabelVal);
96 /// GetDirectionalLocalSymbol - Create and return a directional local
97 /// symbol for numbered label (used for "1b" or 1f" references).
98 MCSymbol *GetDirectionalLocalSymbol(int64_t LocalLabelVal, int bORf);
100 /// GetOrCreateSymbol - Lookup the symbol inside with the specified
101 /// @p Name. If it exists, return it. If not, create a forward
102 /// reference and return it.
104 /// @param Name - The symbol name, which must be unique across all symbols.
105 MCSymbol *GetOrCreateSymbol(StringRef Name);
106 MCSymbol *GetOrCreateSymbol(const Twine &Name);
108 /// LookupSymbol - Get the symbol for \p Name, or null.
109 MCSymbol *LookupSymbol(StringRef Name) const;
113 /// @name Section Managment
116 /// getMachOSection - Return the MCSection for the specified mach-o section.
117 /// This requires the operands to be valid.
118 const MCSectionMachO *getMachOSection(StringRef Segment,
120 unsigned TypeAndAttributes,
123 const MCSectionMachO *getMachOSection(StringRef Segment,
125 unsigned TypeAndAttributes,
127 return getMachOSection(Segment, Section, TypeAndAttributes, 0, K);
130 const MCSection *getELFSection(StringRef Section, unsigned Type,
131 unsigned Flags, SectionKind Kind,
132 bool IsExplicit = false,
133 unsigned EntrySize = 0);
135 const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
136 int Selection, SectionKind Kind);
138 const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
140 return getCOFFSection (Section, Characteristics, 0, Kind);
146 /// @name Dwarf Managment
149 /// GetDwarfFile - creates an entry in the dwarf file and directory tables.
150 unsigned GetDwarfFile(StringRef FileName, unsigned FileNumber);
152 const std::vector<MCDwarfFile *> &getMCDwarfFiles() {
155 const std::vector<StringRef> &getMCDwarfDirs() {
161 char *getSecureLogFile() { return SecureLogFile; }
162 raw_ostream *getSecureLog() { return SecureLog; }
163 bool getSecureLogUsed() { return SecureLogUsed; }
164 void setSecureLog(raw_ostream *Value) {
167 void setSecureLogUsed(bool Value) {
168 SecureLogUsed = Value;
171 void *Allocate(unsigned Size, unsigned Align = 8) {
172 return Allocator.Allocate(Size, Align);
174 void Deallocate(void *Ptr) {
178 } // end namespace llvm
180 // operator new and delete aren't allowed inside namespaces.
181 // The throw specifications are mandated by the standard.
182 /// @brief Placement new for using the MCContext's allocator.
184 /// This placement form of operator new uses the MCContext's allocator for
185 /// obtaining memory. It is a non-throwing new, which means that it returns
186 /// null on error. (If that is what the allocator does. The current does, so if
187 /// this ever changes, this operator will have to be changed, too.)
188 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
190 /// // Default alignment (16)
191 /// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
192 /// // Specific alignment
193 /// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
195 /// Please note that you cannot use delete on the pointer; it must be
196 /// deallocated using an explicit destructor call followed by
197 /// @c Context.Deallocate(Ptr).
199 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
200 /// @param C The MCContext that provides the allocator.
201 /// @param Alignment The alignment of the allocated memory (if the underlying
202 /// allocator supports it).
203 /// @return The allocated memory. Could be NULL.
204 inline void *operator new(size_t Bytes, llvm::MCContext &C,
205 size_t Alignment = 16) throw () {
206 return C.Allocate(Bytes, Alignment);
208 /// @brief Placement delete companion to the new above.
210 /// This operator is just a companion to the new above. There is no way of
211 /// invoking it directly; see the new operator for more details. This operator
212 /// is called implicitly by the compiler if a placement new expression using
213 /// the MCContext throws in the object constructor.
214 inline void operator delete(void *Ptr, llvm::MCContext &C, size_t)
219 /// This placement form of operator new[] uses the MCContext's allocator for
220 /// obtaining memory. It is a non-throwing new[], which means that it returns
222 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
224 /// // Default alignment (16)
225 /// char *data = new (Context) char[10];
226 /// // Specific alignment
227 /// char *data = new (Context, 8) char[10];
229 /// Please note that you cannot use delete on the pointer; it must be
230 /// deallocated using an explicit destructor call followed by
231 /// @c Context.Deallocate(Ptr).
233 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
234 /// @param C The MCContext that provides the allocator.
235 /// @param Alignment The alignment of the allocated memory (if the underlying
236 /// allocator supports it).
237 /// @return The allocated memory. Could be NULL.
238 inline void *operator new[](size_t Bytes, llvm::MCContext& C,
239 size_t Alignment = 16) throw () {
240 return C.Allocate(Bytes, Alignment);
243 /// @brief Placement delete[] companion to the new[] above.
245 /// This operator is just a companion to the new[] above. There is no way of
246 /// invoking it directly; see the new[] operator for more details. This operator
247 /// is called implicitly by the compiler if a placement new[] expression using
248 /// the MCContext throws in the object constructor.
249 inline void operator delete[](void *Ptr, llvm::MCContext &C) throw () {