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 /// Sections - Bindings of names to allocated sections.
42 StringMap<MCSection*> Sections;
44 /// Symbols - Bindings of names to symbols.
45 StringMap<MCSymbol*> Symbols;
47 /// NextUniqueID - The next ID to dole out to an unnamed assembler temporary
49 unsigned NextUniqueID;
51 /// Instances of directional local labels.
52 DenseMap<unsigned, MCLabel *> Instances;
53 /// NextInstance() creates the next instance of the directional local label
54 /// for the LocalLabelVal and adds it to the map if needed.
55 unsigned NextInstance(int64_t LocalLabelVal);
56 /// GetInstance() gets the current instance of the directional local label
57 /// for the LocalLabelVal and adds it to the map if needed.
58 unsigned GetInstance(int64_t LocalLabelVal);
60 /// The file name of the log file from the enviromment variable
61 /// AS_SECURE_LOG_FILE. Which must be set before the .secure_log_unique
62 /// directive is used or it is an error.
64 /// The stream that gets written to for the .secure_log_unique directive.
65 raw_ostream *SecureLog;
66 /// Boolean toggled when .secure_log_unique / .secure_log_reset is seen to
67 /// catch errors if .secure_log_unique appears twice without
68 /// .secure_log_reset appearing between them.
71 /// The dwarf file and directory tables from the dwarf .file directive.
72 std::vector<MCDwarfFile *> MCDwarfFiles;
73 std::vector<StringRef> MCDwarfDirs;
75 /// Allocator - Allocator object used for creating machine code objects.
77 /// We use a bump pointer allocator to avoid the need to track all allocated
79 BumpPtrAllocator Allocator;
81 void *MachOUniquingMap, *ELFUniquingMap, *COFFUniquingMap;
83 explicit MCContext(const MCAsmInfo &MAI);
86 const MCAsmInfo &getAsmInfo() const { return MAI; }
88 /// @name Symbol Managment
91 /// CreateTempSymbol - Create and return a new assembler temporary symbol
92 /// with a unique but unspecified name.
93 MCSymbol *CreateTempSymbol();
95 /// CreateDirectionalLocalSymbol - Create the defintion of a directional
96 /// local symbol for numbered label (used for "1:" defintions).
97 MCSymbol *CreateDirectionalLocalSymbol(int64_t LocalLabelVal);
99 /// GetDirectionalLocalSymbol - Create and return a directional local
100 /// symbol for numbered label (used for "1b" or 1f" references).
101 MCSymbol *GetDirectionalLocalSymbol(int64_t LocalLabelVal, int bORf);
103 /// GetOrCreateSymbol - Lookup the symbol inside with the specified
104 /// @p Name. If it exists, return it. If not, create a forward
105 /// reference and return it.
107 /// @param Name - The symbol name, which must be unique across all symbols.
108 MCSymbol *GetOrCreateSymbol(StringRef Name);
109 MCSymbol *GetOrCreateSymbol(const Twine &Name);
111 /// LookupSymbol - Get the symbol for \p Name, or null.
112 MCSymbol *LookupSymbol(StringRef Name) const;
116 /// @name Section Managment
119 /// getMachOSection - Return the MCSection for the specified mach-o section.
120 /// This requires the operands to be valid.
121 const MCSectionMachO *getMachOSection(StringRef Segment,
123 unsigned TypeAndAttributes,
126 const MCSectionMachO *getMachOSection(StringRef Segment,
128 unsigned TypeAndAttributes,
130 return getMachOSection(Segment, Section, TypeAndAttributes, 0, K);
133 const MCSection *getELFSection(StringRef Section, unsigned Type,
134 unsigned Flags, SectionKind Kind,
135 bool IsExplicit = false);
137 const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
138 int Selection, SectionKind Kind);
140 const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
142 return getCOFFSection (Section, Characteristics, 0, Kind);
148 /// @name Dwarf Managment
151 /// GetDwarfFile - creates an entry in the dwarf file and directory tables.
152 unsigned GetDwarfFile(StringRef FileName, unsigned FileNumber);
154 const std::vector<MCDwarfFile *> &getMCDwarfFiles() {
160 char *getSecureLogFile() { return SecureLogFile; }
161 raw_ostream *getSecureLog() { return SecureLog; }
162 bool getSecureLogUsed() { return SecureLogUsed; }
163 void setSecureLog(raw_ostream *Value) {
166 void setSecureLogUsed(bool Value) {
167 SecureLogUsed = Value;
170 void *Allocate(unsigned Size, unsigned Align = 8) {
171 return Allocator.Allocate(Size, Align);
173 void Deallocate(void *Ptr) {
177 } // end namespace llvm
179 // operator new and delete aren't allowed inside namespaces.
180 // The throw specifications are mandated by the standard.
181 /// @brief Placement new for using the MCContext's allocator.
183 /// This placement form of operator new uses the MCContext's allocator for
184 /// obtaining memory. It is a non-throwing new, which means that it returns
185 /// null on error. (If that is what the allocator does. The current does, so if
186 /// this ever changes, this operator will have to be changed, too.)
187 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
189 /// // Default alignment (16)
190 /// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
191 /// // Specific alignment
192 /// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
194 /// Please note that you cannot use delete on the pointer; it must be
195 /// deallocated using an explicit destructor call followed by
196 /// @c Context.Deallocate(Ptr).
198 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
199 /// @param C The MCContext that provides the allocator.
200 /// @param Alignment The alignment of the allocated memory (if the underlying
201 /// allocator supports it).
202 /// @return The allocated memory. Could be NULL.
203 inline void *operator new(size_t Bytes, llvm::MCContext &C,
204 size_t Alignment = 16) throw () {
205 return C.Allocate(Bytes, Alignment);
207 /// @brief Placement delete companion to the new above.
209 /// This operator is just a companion to the new above. There is no way of
210 /// invoking it directly; see the new operator for more details. This operator
211 /// is called implicitly by the compiler if a placement new expression using
212 /// the MCContext throws in the object constructor.
213 inline void operator delete(void *Ptr, llvm::MCContext &C, size_t)
218 /// This placement form of operator new[] uses the MCContext's allocator for
219 /// obtaining memory. It is a non-throwing new[], which means that it returns
221 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
223 /// // Default alignment (16)
224 /// char *data = new (Context) char[10];
225 /// // Specific alignment
226 /// char *data = new (Context, 8) char[10];
228 /// Please note that you cannot use delete on the pointer; it must be
229 /// deallocated using an explicit destructor call followed by
230 /// @c Context.Deallocate(Ptr).
232 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
233 /// @param C The MCContext that provides the allocator.
234 /// @param Alignment The alignment of the allocated memory (if the underlying
235 /// allocator supports it).
236 /// @return The allocated memory. Could be NULL.
237 inline void *operator new[](size_t Bytes, llvm::MCContext& C,
238 size_t Alignment = 16) throw () {
239 return C.Allocate(Bytes, Alignment);
242 /// @brief Placement delete[] companion to the new[] above.
244 /// This operator is just a companion to the new[] above. There is no way of
245 /// invoking it directly; see the new[] operator for more details. This operator
246 /// is called implicitly by the compiler if a placement new[] expression using
247 /// the MCContext throws in the object constructor.
248 inline void operator delete[](void *Ptr, llvm::MCContext &C) throw () {