First bit of support for the dwarf .loc directive. This patch updates the
[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 StringRef;
30   class Twine;
31   class MCSectionMachO;
32
33   /// MCContext - Context object for machine code objects.  This class owns all
34   /// of the sections that it creates.
35   ///
36   class MCContext {
37     MCContext(const MCContext&); // DO NOT IMPLEMENT
38     MCContext &operator=(const MCContext&); // DO NOT IMPLEMENT
39
40     /// The MCAsmInfo for this target.
41     const MCAsmInfo &MAI;
42
43     /// Symbols - Bindings of names to symbols.
44     StringMap<MCSymbol*> Symbols;
45
46     /// NextUniqueID - The next ID to dole out to an unnamed assembler temporary
47     /// symbol.
48     unsigned NextUniqueID;
49
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);
58     
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.
62     char *SecureLogFile;
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.
68     bool SecureLogUsed;
69
70     /// The dwarf file and directory tables from the dwarf .file directive.
71     std::vector<MCDwarfFile *> MCDwarfFiles;
72     std::vector<StringRef> MCDwarfDirs;
73
74     /// The current dwarf line information from the last dwarf .loc directive.
75     MCDwarfLoc CurrentDwarfLoc;
76     bool DwarfLocSeen;
77
78     /// Allocator - Allocator object used for creating machine code objects.
79     ///
80     /// We use a bump pointer allocator to avoid the need to track all allocated
81     /// objects.
82     BumpPtrAllocator Allocator;
83     
84     void *MachOUniquingMap, *ELFUniquingMap, *COFFUniquingMap;
85   public:
86     explicit MCContext(const MCAsmInfo &MAI);
87     ~MCContext();
88     
89     const MCAsmInfo &getAsmInfo() const { return MAI; }
90
91     /// @name Symbol Managment
92     /// @{
93     
94     /// CreateTempSymbol - Create and return a new assembler temporary symbol
95     /// with a unique but unspecified name.
96     MCSymbol *CreateTempSymbol();
97
98     /// CreateDirectionalLocalSymbol - Create the defintion of a directional
99     /// local symbol for numbered label (used for "1:" defintions).
100     MCSymbol *CreateDirectionalLocalSymbol(int64_t LocalLabelVal);
101
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);
105
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.
109     ///
110     /// @param Name - The symbol name, which must be unique across all symbols.
111     MCSymbol *GetOrCreateSymbol(StringRef Name);
112     MCSymbol *GetOrCreateSymbol(const Twine &Name);
113
114     /// LookupSymbol - Get the symbol for \p Name, or null.
115     MCSymbol *LookupSymbol(StringRef Name) const;
116
117     /// @}
118     
119     /// @name Section Managment
120     /// @{
121
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,
125                                           StringRef Section,
126                                           unsigned TypeAndAttributes,
127                                           unsigned Reserved2,
128                                           SectionKind K);
129     const MCSectionMachO *getMachOSection(StringRef Segment,
130                                           StringRef Section,
131                                           unsigned TypeAndAttributes,
132                                           SectionKind K) {
133       return getMachOSection(Segment, Section, TypeAndAttributes, 0, K);
134     }
135     
136     const MCSection *getELFSection(StringRef Section, unsigned Type,
137                                    unsigned Flags, SectionKind Kind,
138                                    bool IsExplicit = false,
139                                    unsigned EntrySize = 0);
140
141     const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
142                                     int Selection, SectionKind Kind);
143
144     const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics,
145                                     SectionKind Kind) {
146       return getCOFFSection (Section, Characteristics, 0, Kind);
147     }
148
149     
150     /// @}
151
152     /// @name Dwarf Managment
153     /// @{
154
155     /// GetDwarfFile - creates an entry in the dwarf file and directory tables.
156     unsigned GetDwarfFile(StringRef FileName, unsigned FileNumber);
157
158     bool ValidateDwarfFileNumber(unsigned FileNumber);
159
160     const std::vector<MCDwarfFile *> &getMCDwarfFiles() {
161       return MCDwarfFiles;
162     }
163     const std::vector<StringRef> &getMCDwarfDirs() {
164       return MCDwarfDirs;
165     }
166
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);
177       DwarfLocSeen = true;
178     }
179
180     /// @}
181
182     char *getSecureLogFile() { return SecureLogFile; }
183     raw_ostream *getSecureLog() { return SecureLog; }
184     bool getSecureLogUsed() { return SecureLogUsed; }
185     void setSecureLog(raw_ostream *Value) {
186       SecureLog = Value;
187     }
188     void setSecureLogUsed(bool Value) {
189       SecureLogUsed = Value;
190     }
191
192     void *Allocate(unsigned Size, unsigned Align = 8) {
193       return Allocator.Allocate(Size, Align);
194     }
195     void Deallocate(void *Ptr) {
196     }
197   };
198
199 } // end namespace llvm
200
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.
204 ///
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):
210 /// @code
211 /// // Default alignment (16)
212 /// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments);
213 /// // Specific alignment
214 /// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments);
215 /// @endcode
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).
219 ///
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);
228 }
229 /// @brief Placement delete companion to the new above.
230 ///
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)
236               throw () {
237   C.Deallocate(Ptr);
238 }
239
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
242 /// null on error.
243 /// Usage looks like this (assuming there's an MCContext 'Context' in scope):
244 /// @code
245 /// // Default alignment (16)
246 /// char *data = new (Context) char[10];
247 /// // Specific alignment
248 /// char *data = new (Context, 8) char[10];
249 /// @endcode
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).
253 ///
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);
262 }
263
264 /// @brief Placement delete[] companion to the new[] above.
265 ///
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 () {
271   C.Deallocate(Ptr);
272 }
273
274 #endif