blob: 6686950b72dcb0ac686cd9c8e82d9d9ba191561a [file] [log] [blame]
Daniel Dunbar25e0d8f2009-06-23 20:24:17 +00001//===- 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
Chris Lattnerf0559e42010-04-08 20:30:37 +000013#include "llvm/MC/SectionKind.h"
Kevin Enderbyc1840b32010-08-24 20:32:42 +000014#include "llvm/MC/MCDwarf.h"
Daniel Dunbarecc63f82009-06-23 22:01:43 +000015#include "llvm/ADT/DenseMap.h"
16#include "llvm/ADT/StringMap.h"
17#include "llvm/Support/Allocator.h"
Kevin Enderbyf187ac52010-06-28 21:45:58 +000018#include "llvm/Support/raw_ostream.h"
Kevin Enderby7cbf73a2010-07-28 20:55:35 +000019#include <vector> // FIXME: Shouldn't be needed.
Daniel Dunbarecc63f82009-06-23 22:01:43 +000020
Daniel Dunbar25e0d8f2009-06-23 20:24:17 +000021namespace llvm {
Chris Lattnerc18409a2010-03-11 22:53:35 +000022 class MCAsmInfo;
Daniel Dunbare5798492009-10-16 01:33:11 +000023 class MCExpr;
Daniel Dunbar25e0d8f2009-06-23 20:24:17 +000024 class MCSection;
25 class MCSymbol;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +000026 class MCLabel;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +000027 class MCDwarfFile;
Kevin Enderbyc1840b32010-08-24 20:32:42 +000028 class MCDwarfLoc;
Daniel Dunbarb5261eb2009-07-27 21:22:30 +000029 class StringRef;
Chris Lattner7c5b0212009-10-19 22:49:00 +000030 class Twine;
Chris Lattnerf0559e42010-04-08 20:30:37 +000031 class MCSectionMachO;
Daniel Dunbar25e0d8f2009-06-23 20:24:17 +000032
Chris Lattnerc9d31522009-08-13 00:21:53 +000033 /// MCContext - Context object for machine code objects. This class owns all
34 /// of the sections that it creates.
35 ///
Daniel Dunbar25e0d8f2009-06-23 20:24:17 +000036 class MCContext {
37 MCContext(const MCContext&); // DO NOT IMPLEMENT
38 MCContext &operator=(const MCContext&); // DO NOT IMPLEMENT
39
Chris Lattnerc18409a2010-03-11 22:53:35 +000040 /// The MCAsmInfo for this target.
41 const MCAsmInfo &MAI;
Daniel Dunbarecc63f82009-06-23 22:01:43 +000042
43 /// Symbols - Bindings of names to symbols.
44 StringMap<MCSymbol*> Symbols;
45
Chris Lattnerc18409a2010-03-11 22:53:35 +000046 /// NextUniqueID - The next ID to dole out to an unnamed assembler temporary
47 /// symbol.
48 unsigned NextUniqueID;
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +000049
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);
Chris Lattnerc18409a2010-03-11 22:53:35 +000058
Kevin Enderbyf187ac52010-06-28 21:45:58 +000059 /// 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
Kevin Enderby7cbf73a2010-07-28 20:55:35 +000070 /// The dwarf file and directory tables from the dwarf .file directive.
71 std::vector<MCDwarfFile *> MCDwarfFiles;
Benjamin Kramer3bce5ad2010-07-29 13:53:19 +000072 std::vector<StringRef> MCDwarfDirs;
Kevin Enderby7cbf73a2010-07-28 20:55:35 +000073
Kevin Enderbyc1840b32010-08-24 20:32:42 +000074 /// The current dwarf line information from the last dwarf .loc directive.
75 MCDwarfLoc CurrentDwarfLoc;
76 bool DwarfLocSeen;
77
Daniel Dunbarecc63f82009-06-23 22:01:43 +000078 /// 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;
Chris Lattnerf0559e42010-04-08 20:30:37 +000083
Chris Lattnereb40a0f2010-05-07 17:17:41 +000084 void *MachOUniquingMap, *ELFUniquingMap, *COFFUniquingMap;
Daniel Dunbar25e0d8f2009-06-23 20:24:17 +000085 public:
Chris Lattner5ef31a02010-03-12 18:44:54 +000086 explicit MCContext(const MCAsmInfo &MAI);
Daniel Dunbar25e0d8f2009-06-23 20:24:17 +000087 ~MCContext();
Chris Lattnerc18409a2010-03-11 22:53:35 +000088
89 const MCAsmInfo &getAsmInfo() const { return MAI; }
Daniel Dunbar869a5e72009-08-31 08:07:08 +000090
91 /// @name Symbol Managment
92 /// @{
Chris Lattner1d72a762010-03-14 08:23:30 +000093
94 /// CreateTempSymbol - Create and return a new assembler temporary symbol
95 /// with a unique but unspecified name.
96 MCSymbol *CreateTempSymbol();
Daniel Dunbar869a5e72009-08-31 08:07:08 +000097
Kevin Enderbyebe7fcd2010-05-17 23:08:19 +000098 /// 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
Chris Lattnerc69485e2009-06-24 04:31:49 +0000106 /// GetOrCreateSymbol - Lookup the symbol inside with the specified
Dan Gohmanfb76fe02010-02-22 04:10:52 +0000107 /// @p Name. If it exists, return it. If not, create a forward
Chris Lattnerc69485e2009-06-24 04:31:49 +0000108 /// reference and return it.
109 ///
110 /// @param Name - The symbol name, which must be unique across all symbols.
Chris Lattner9b97a732010-03-30 18:10:53 +0000111 MCSymbol *GetOrCreateSymbol(StringRef Name);
112 MCSymbol *GetOrCreateSymbol(const Twine &Name);
Daniel Dunbarecc63f82009-06-23 22:01:43 +0000113
Dan Gohmanfb76fe02010-02-22 04:10:52 +0000114 /// LookupSymbol - Get the symbol for \p Name, or null.
Daniel Dunbar2928c832009-11-06 10:58:06 +0000115 MCSymbol *LookupSymbol(StringRef Name) const;
Daniel Dunbar25e0d8f2009-06-23 20:24:17 +0000116
Daniel Dunbar869a5e72009-08-31 08:07:08 +0000117 /// @}
Chris Lattnerf0559e42010-04-08 20:30:37 +0000118
119 /// @name Section Managment
120 /// @{
121
Chris Lattner22772212010-04-08 20:40:11 +0000122 /// getMachOSection - Return the MCSection for the specified mach-o section.
123 /// This requires the operands to be valid.
Chris Lattnerf0559e42010-04-08 20:30:37 +0000124 const MCSectionMachO *getMachOSection(StringRef Segment,
125 StringRef Section,
126 unsigned TypeAndAttributes,
127 unsigned Reserved2,
128 SectionKind K);
Chris Lattner22772212010-04-08 20:40:11 +0000129 const MCSectionMachO *getMachOSection(StringRef Segment,
130 StringRef Section,
131 unsigned TypeAndAttributes,
132 SectionKind K) {
133 return getMachOSection(Segment, Section, TypeAndAttributes, 0, K);
134 }
Chris Lattnerf0559e42010-04-08 20:30:37 +0000135
Chris Lattner74aae472010-04-08 21:26:26 +0000136 const MCSection *getELFSection(StringRef Section, unsigned Type,
137 unsigned Flags, SectionKind Kind,
Matt Fleming3e096692010-08-16 18:33:46 +0000138 bool IsExplicit = false,
139 unsigned EntrySize = 0);
Chris Lattnereb40a0f2010-05-07 17:17:41 +0000140
Chris Lattner6e5ce282010-05-07 21:49:09 +0000141 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
Chris Lattner74aae472010-04-08 21:26:26 +0000149
Chris Lattnerf0559e42010-04-08 20:30:37 +0000150 /// @}
Daniel Dunbar869a5e72009-08-31 08:07:08 +0000151
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000152 /// @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
Kevin Enderbyc1840b32010-08-24 20:32:42 +0000158 bool ValidateDwarfFileNumber(unsigned FileNumber);
159
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000160 const std::vector<MCDwarfFile *> &getMCDwarfFiles() {
161 return MCDwarfFiles;
162 }
Kevin Enderbyb07ce602010-08-09 22:52:14 +0000163 const std::vector<StringRef> &getMCDwarfDirs() {
164 return MCDwarfDirs;
165 }
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000166
Kevin Enderbyc1840b32010-08-24 20:32:42 +0000167 /// 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
Kevin Enderby7cbf73a2010-07-28 20:55:35 +0000180 /// @}
181
Kevin Enderbyf187ac52010-06-28 21:45:58 +0000182 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
Daniel Dunbarecc63f82009-06-23 22:01:43 +0000192 void *Allocate(unsigned Size, unsigned Align = 8) {
193 return Allocator.Allocate(Size, Align);
194 }
Daniel Dunbare5798492009-10-16 01:33:11 +0000195 void Deallocate(void *Ptr) {
Daniel Dunbarecc63f82009-06-23 22:01:43 +0000196 }
Daniel Dunbar25e0d8f2009-06-23 20:24:17 +0000197 };
198
199} // end namespace llvm
200
Daniel Dunbarecc63f82009-06-23 22:01:43 +0000201// 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.
225inline void *operator new(size_t Bytes, llvm::MCContext &C,
Daniel Dunbara11af532009-06-24 01:03:06 +0000226 size_t Alignment = 16) throw () {
Daniel Dunbarecc63f82009-06-23 22:01:43 +0000227 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.
235inline 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.
259inline 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.
270inline void operator delete[](void *Ptr, llvm::MCContext &C) throw () {
271 C.Deallocate(Ptr);
272}
273
Daniel Dunbar25e0d8f2009-06-23 20:24:17 +0000274#endif