1 //===- MCContext.h - Machine Code Context -----------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_MC_MCCONTEXT_H
11 #define LLVM_MC_MCCONTEXT_H
12
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.
20
21 namespace llvm {
22 class MCAsmInfo;
23 class MCExpr;
24 class MCSection;
25 class MCSymbol;
26 class MCLabel;
27 class MCDwarfFile;
28 class MCDwarfLoc;
29 class MCObjectFileInfo;
30 class MCRegisterInfo;
31 class MCLineSection;
32 class StringRef;
33 class Twine;
34 class TargetAsmInfo;
35 class MCSectionMachO;
36 class MCSectionELF;
37
38 /// MCContext - Context object for machine code objects. This class owns all
39 /// of the sections that it creates.
40 ///
41 class MCContext {
42 MCContext(const MCContext&); // DO NOT IMPLEMENT
43 MCContext &operator=(const MCContext&); // DO NOT IMPLEMENT
44 public:
45 typedef StringMap<MCSymbol*, BumpPtrAllocator&> SymbolTable;
46 private:
47
48 /// The MCAsmInfo for this target.
49 const MCAsmInfo &MAI;
50
51 /// The MCRegisterInfo for this target.
52 const MCRegisterInfo &MRI;
53
54 /// The MCObjectFileInfo for this target.
55 const MCObjectFileInfo *MOFI;
56
57 const TargetAsmInfo *TAI;
58
59 /// Allocator - Allocator object used for creating machine code objects.
60 ///
61 /// We use a bump pointer allocator to avoid the need to track all allocated
62 /// objects.
63 BumpPtrAllocator Allocator;
64
65 /// Symbols - Bindings of names to symbols.
66 SymbolTable Symbols;
67
68 /// UsedNames - Keeps tracks of names that were used both for used declared
69 /// and artificial symbols.
70 StringMap<bool, BumpPtrAllocator&> UsedNames;
71
72 /// NextUniqueID - The next ID to dole out to an unnamed assembler temporary
73 /// symbol.
74 unsigned NextUniqueID;
75
76 /// Instances of directional local labels.
77 DenseMap<unsigned, MCLabel *> Instances;
78 /// NextInstance() creates the next instance of the directional local label
79 /// for the LocalLabelVal and adds it to the map if needed.
80 unsigned NextInstance(int64_t LocalLabelVal);
81 /// GetInstance() gets the current instance of the directional local label
82 /// for the LocalLabelVal and adds it to the map if needed.
83 unsigned GetInstance(int64_t LocalLabelVal);
84
85 /// The file name of the log file from the environment variable
86 /// AS_SECURE_LOG_FILE. Which must be set before the .secure_log_unique
87 /// directive is used or it is an error.
88 char *SecureLogFile;
89 /// The stream that gets written to for the .secure_log_unique directive.
90 raw_ostream *SecureLog;
91 /// Boolean toggled when .secure_log_unique / .secure_log_reset is seen to
92 /// catch errors if .secure_log_unique appears twice without
93 /// .secure_log_reset appearing between them.
94 bool SecureLogUsed;
95
96 /// The dwarf file and directory tables from the dwarf .file directive.
97 std::vector<MCDwarfFile *> MCDwarfFiles;
98 std::vector<StringRef> MCDwarfDirs;
99
100 /// The current dwarf line information from the last dwarf .loc directive.
101 MCDwarfLoc CurrentDwarfLoc;
102 bool DwarfLocSeen;
103
104 /// Honor temporary labels, this is useful for debugging semantic
105 /// differences between temporary and non-temporary labels (primarily on
106 /// Darwin).
107 bool AllowTemporaryLabels;
108
109 /// The dwarf line information from the .loc directives for the sections
110 /// with assembled machine instructions have after seeing .loc directives.
111 DenseMap<const MCSection *, MCLineSection *> MCLineSections;
112 /// We need a deterministic iteration order, so we remember the order
113 /// the elements were added.
114 std::vector<const MCSection *> MCLineSectionOrder;
115
116 void *MachOUniquingMap, *ELFUniquingMap, *COFFUniquingMap;
117
118 MCSymbol *CreateSymbol(StringRef Name);
119
120 public:
121 explicit MCContext(const MCAsmInfo &MAI, const MCRegisterInfo &MRI,
122 const MCObjectFileInfo *MOFI, const TargetAsmInfo *TAI);
123 ~MCContext();
124
getAsmInfo()125 const MCAsmInfo &getAsmInfo() const { return MAI; }
126
getRegisterInfo()127 const MCRegisterInfo &getRegisterInfo() const { return MRI; }
128
getObjectFileInfo()129 const MCObjectFileInfo *getObjectFileInfo() const { return MOFI; }
130
getTargetAsmInfo()131 const TargetAsmInfo &getTargetAsmInfo() const { return *TAI; }
132
setAllowTemporaryLabels(bool Value)133 void setAllowTemporaryLabels(bool Value) { AllowTemporaryLabels = Value; }
134
135 /// @name Symbol Management
136 /// @{
137
138 /// CreateTempSymbol - Create and return a new assembler temporary symbol
139 /// with a unique but unspecified name.
140 MCSymbol *CreateTempSymbol();
141
142 /// CreateDirectionalLocalSymbol - Create the definition of a directional
143 /// local symbol for numbered label (used for "1:" definitions).
144 MCSymbol *CreateDirectionalLocalSymbol(int64_t LocalLabelVal);
145
146 /// GetDirectionalLocalSymbol - Create and return a directional local
147 /// symbol for numbered label (used for "1b" or 1f" references).
148 MCSymbol *GetDirectionalLocalSymbol(int64_t LocalLabelVal, int bORf);
149
150 /// GetOrCreateSymbol - Lookup the symbol inside with the specified
151 /// @p Name. If it exists, return it. If not, create a forward
152 /// reference and return it.
153 ///
154 /// @param Name - The symbol name, which must be unique across all symbols.
155 MCSymbol *GetOrCreateSymbol(StringRef Name);
156 MCSymbol *GetOrCreateSymbol(const Twine &Name);
157
158 /// LookupSymbol - Get the symbol for \p Name, or null.
159 MCSymbol *LookupSymbol(StringRef Name) const;
160
161 /// getSymbols - Get a reference for the symbol table for clients that
162 /// want to, for example, iterate over all symbols. 'const' because we
163 /// still want any modifications to the table itself to use the MCContext
164 /// APIs.
getSymbols()165 const SymbolTable &getSymbols() const {
166 return Symbols;
167 }
168
169 /// @}
170
171 /// @name Section Management
172 /// @{
173
174 /// getMachOSection - Return the MCSection for the specified mach-o section.
175 /// This requires the operands to be valid.
176 const MCSectionMachO *getMachOSection(StringRef Segment,
177 StringRef Section,
178 unsigned TypeAndAttributes,
179 unsigned Reserved2,
180 SectionKind K);
getMachOSection(StringRef Segment,StringRef Section,unsigned TypeAndAttributes,SectionKind K)181 const MCSectionMachO *getMachOSection(StringRef Segment,
182 StringRef Section,
183 unsigned TypeAndAttributes,
184 SectionKind K) {
185 return getMachOSection(Segment, Section, TypeAndAttributes, 0, K);
186 }
187
188 const MCSectionELF *getELFSection(StringRef Section, unsigned Type,
189 unsigned Flags, SectionKind Kind);
190
191 const MCSectionELF *getELFSection(StringRef Section, unsigned Type,
192 unsigned Flags, SectionKind Kind,
193 unsigned EntrySize, StringRef Group);
194
195 const MCSectionELF *CreateELFGroupSection();
196
197 const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
198 int Selection, SectionKind Kind);
199
getCOFFSection(StringRef Section,unsigned Characteristics,SectionKind Kind)200 const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
201 SectionKind Kind) {
202 return getCOFFSection (Section, Characteristics, 0, Kind);
203 }
204
205
206 /// @}
207
208 /// @name Dwarf Management
209 /// @{
210
211 /// GetDwarfFile - creates an entry in the dwarf file and directory tables.
212 unsigned GetDwarfFile(StringRef FileName, unsigned FileNumber);
213
214 bool isValidDwarfFileNumber(unsigned FileNumber);
215
hasDwarfFiles()216 bool hasDwarfFiles() const {
217 return !MCDwarfFiles.empty();
218 }
219
getMCDwarfFiles()220 const std::vector<MCDwarfFile *> &getMCDwarfFiles() {
221 return MCDwarfFiles;
222 }
getMCDwarfDirs()223 const std::vector<StringRef> &getMCDwarfDirs() {
224 return MCDwarfDirs;
225 }
226
227 const DenseMap<const MCSection *, MCLineSection *>
getMCLineSections()228 &getMCLineSections() const {
229 return MCLineSections;
230 }
getMCLineSectionOrder()231 const std::vector<const MCSection *> &getMCLineSectionOrder() const {
232 return MCLineSectionOrder;
233 }
addMCLineSection(const MCSection * Sec,MCLineSection * Line)234 void addMCLineSection(const MCSection *Sec, MCLineSection *Line) {
235 MCLineSections[Sec] = Line;
236 MCLineSectionOrder.push_back(Sec);
237 }
238
239 /// setCurrentDwarfLoc - saves the information from the currently parsed
240 /// dwarf .loc directive and sets DwarfLocSeen. When the next instruction
241 /// is assembled an entry in the line number table with this information and
242 /// the address of the instruction will be created.
setCurrentDwarfLoc(unsigned FileNum,unsigned Line,unsigned Column,unsigned Flags,unsigned Isa,unsigned Discriminator)243 void setCurrentDwarfLoc(unsigned FileNum, unsigned Line, unsigned Column,
244 unsigned Flags, unsigned Isa,
245 unsigned Discriminator) {
246 CurrentDwarfLoc.setFileNum(FileNum);
247 CurrentDwarfLoc.setLine(Line);
248 CurrentDwarfLoc.setColumn(Column);
249 CurrentDwarfLoc.setFlags(Flags);
250 CurrentDwarfLoc.setIsa(Isa);
251 CurrentDwarfLoc.setDiscriminator(Discriminator);
252 DwarfLocSeen = true;
253 }
ClearDwarfLocSeen()254 void ClearDwarfLocSeen() { DwarfLocSeen = false; }
255
getDwarfLocSeen()256 bool getDwarfLocSeen() { return DwarfLocSeen; }
getCurrentDwarfLoc()257 const MCDwarfLoc &getCurrentDwarfLoc() { return CurrentDwarfLoc; }
258
259 /// @}
260
getSecureLogFile()261 char *getSecureLogFile() { return SecureLogFile; }
getSecureLog()262 raw_ostream *getSecureLog() { return SecureLog; }
getSecureLogUsed()263 bool getSecureLogUsed() { return SecureLogUsed; }
setSecureLog(raw_ostream * Value)264 void setSecureLog(raw_ostream *Value) {
265 SecureLog = Value;
266 }
setSecureLogUsed(bool Value)267 void setSecureLogUsed(bool Value) {
268 SecureLogUsed = Value;
269 }
270
271 void *Allocate(unsigned Size, unsigned Align = 8) {
272 return Allocator.Allocate(Size, Align);
273 }
Deallocate(void * Ptr)274 void Deallocate(void *Ptr) {
275 }
276 };
277
278 } // end namespace llvm
279
280 // operator new and delete aren't allowed inside namespaces.
281 // The throw specifications are mandated by the standard.
282 /// @brief Placement new for using the MCContext's allocator.
283 ///
284 /// This placement form of operator new uses the MCContext's allocator for
285 /// obtaining memory. It is a non-throwing new, which means that it returns
286 /// null on error. (If that is what the allocator does. The current does, so if
287 /// this ever changes, this operator will have to be changed, too.)
288 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
289 /// @code
290 /// // Default alignment (16)
291 /// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
292 /// // Specific alignment
293 /// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
294 /// @endcode
295 /// Please note that you cannot use delete on the pointer; it must be
296 /// deallocated using an explicit destructor call followed by
297 /// @c Context.Deallocate(Ptr).
298 ///
299 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
300 /// @param C The MCContext that provides the allocator.
301 /// @param Alignment The alignment of the allocated memory (if the underlying
302 /// allocator supports it).
303 /// @return The allocated memory. Could be NULL.
304 inline void *operator new(size_t Bytes, llvm::MCContext &C,
throw()305 size_t Alignment = 16) throw () {
306 return C.Allocate(Bytes, Alignment);
307 }
308 /// @brief Placement delete companion to the new above.
309 ///
310 /// This operator is just a companion to the new above. There is no way of
311 /// invoking it directly; see the new operator for more details. This operator
312 /// is called implicitly by the compiler if a placement new expression using
313 /// the MCContext throws in the object constructor.
delete(void * Ptr,llvm::MCContext & C,size_t)314 inline void operator delete(void *Ptr, llvm::MCContext &C, size_t)
315 throw () {
316 C.Deallocate(Ptr);
317 }
318
319 /// This placement form of operator new[] uses the MCContext's allocator for
320 /// obtaining memory. It is a non-throwing new[], which means that it returns
321 /// null on error.
322 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
323 /// @code
324 /// // Default alignment (16)
325 /// char *data = new (Context) char[10];
326 /// // Specific alignment
327 /// char *data = new (Context, 8) char[10];
328 /// @endcode
329 /// Please note that you cannot use delete on the pointer; it must be
330 /// deallocated using an explicit destructor call followed by
331 /// @c Context.Deallocate(Ptr).
332 ///
333 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
334 /// @param C The MCContext that provides the allocator.
335 /// @param Alignment The alignment of the allocated memory (if the underlying
336 /// allocator supports it).
337 /// @return The allocated memory. Could be NULL.
338 inline void *operator new[](size_t Bytes, llvm::MCContext& C,
throw()339 size_t Alignment = 16) throw () {
340 return C.Allocate(Bytes, Alignment);
341 }
342
343 /// @brief Placement delete[] companion to the new[] above.
344 ///
345 /// This operator is just a companion to the new[] above. There is no way of
346 /// invoking it directly; see the new[] operator for more details. This operator
347 /// is called implicitly by the compiler if a placement new[] expression using
348 /// the MCContext throws in the object constructor.
throw()349 inline void operator delete[](void *Ptr, llvm::MCContext &C) throw () {
350 C.Deallocate(Ptr);
351 }
352
353 #endif
354