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