Daniel Dunbar | 25e0d8f | 2009-06-23 20:24:17 +0000 | [diff] [blame] | 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 | |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 13 | #include "llvm/MC/SectionKind.h" |
Kevin Enderby | c1840b3 | 2010-08-24 20:32:42 +0000 | [diff] [blame^] | 14 | #include "llvm/MC/MCDwarf.h" |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/DenseMap.h" |
| 16 | #include "llvm/ADT/StringMap.h" |
| 17 | #include "llvm/Support/Allocator.h" |
Kevin Enderby | f187ac5 | 2010-06-28 21:45:58 +0000 | [diff] [blame] | 18 | #include "llvm/Support/raw_ostream.h" |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 19 | #include <vector> // FIXME: Shouldn't be needed. |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 20 | |
Daniel Dunbar | 25e0d8f | 2009-06-23 20:24:17 +0000 | [diff] [blame] | 21 | namespace llvm { |
Chris Lattner | c18409a | 2010-03-11 22:53:35 +0000 | [diff] [blame] | 22 | class MCAsmInfo; |
Daniel Dunbar | e579849 | 2009-10-16 01:33:11 +0000 | [diff] [blame] | 23 | class MCExpr; |
Daniel Dunbar | 25e0d8f | 2009-06-23 20:24:17 +0000 | [diff] [blame] | 24 | class MCSection; |
| 25 | class MCSymbol; |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 26 | class MCLabel; |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 27 | class MCDwarfFile; |
Kevin Enderby | c1840b3 | 2010-08-24 20:32:42 +0000 | [diff] [blame^] | 28 | class MCDwarfLoc; |
Daniel Dunbar | b5261eb | 2009-07-27 21:22:30 +0000 | [diff] [blame] | 29 | class StringRef; |
Chris Lattner | 7c5b021 | 2009-10-19 22:49:00 +0000 | [diff] [blame] | 30 | class Twine; |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 31 | class MCSectionMachO; |
Daniel Dunbar | 25e0d8f | 2009-06-23 20:24:17 +0000 | [diff] [blame] | 32 | |
Chris Lattner | c9d3152 | 2009-08-13 00:21:53 +0000 | [diff] [blame] | 33 | /// MCContext - Context object for machine code objects. This class owns all |
| 34 | /// of the sections that it creates. |
| 35 | /// |
Daniel Dunbar | 25e0d8f | 2009-06-23 20:24:17 +0000 | [diff] [blame] | 36 | class MCContext { |
| 37 | MCContext(const MCContext&); // DO NOT IMPLEMENT |
| 38 | MCContext &operator=(const MCContext&); // DO NOT IMPLEMENT |
| 39 | |
Chris Lattner | c18409a | 2010-03-11 22:53:35 +0000 | [diff] [blame] | 40 | /// The MCAsmInfo for this target. |
| 41 | const MCAsmInfo &MAI; |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 42 | |
| 43 | /// Symbols - Bindings of names to symbols. |
| 44 | StringMap<MCSymbol*> Symbols; |
| 45 | |
Chris Lattner | c18409a | 2010-03-11 22:53:35 +0000 | [diff] [blame] | 46 | /// NextUniqueID - The next ID to dole out to an unnamed assembler temporary |
| 47 | /// symbol. |
| 48 | unsigned NextUniqueID; |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 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); |
Chris Lattner | c18409a | 2010-03-11 22:53:35 +0000 | [diff] [blame] | 58 | |
Kevin Enderby | f187ac5 | 2010-06-28 21:45:58 +0000 | [diff] [blame] | 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 | |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 70 | /// The dwarf file and directory tables from the dwarf .file directive. |
| 71 | std::vector<MCDwarfFile *> MCDwarfFiles; |
Benjamin Kramer | 3bce5ad | 2010-07-29 13:53:19 +0000 | [diff] [blame] | 72 | std::vector<StringRef> MCDwarfDirs; |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 73 | |
Kevin Enderby | c1840b3 | 2010-08-24 20:32:42 +0000 | [diff] [blame^] | 74 | /// The current dwarf line information from the last dwarf .loc directive. |
| 75 | MCDwarfLoc CurrentDwarfLoc; |
| 76 | bool DwarfLocSeen; |
| 77 | |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 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; |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 83 | |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 84 | void *MachOUniquingMap, *ELFUniquingMap, *COFFUniquingMap; |
Daniel Dunbar | 25e0d8f | 2009-06-23 20:24:17 +0000 | [diff] [blame] | 85 | public: |
Chris Lattner | 5ef31a0 | 2010-03-12 18:44:54 +0000 | [diff] [blame] | 86 | explicit MCContext(const MCAsmInfo &MAI); |
Daniel Dunbar | 25e0d8f | 2009-06-23 20:24:17 +0000 | [diff] [blame] | 87 | ~MCContext(); |
Chris Lattner | c18409a | 2010-03-11 22:53:35 +0000 | [diff] [blame] | 88 | |
| 89 | const MCAsmInfo &getAsmInfo() const { return MAI; } |
Daniel Dunbar | 869a5e7 | 2009-08-31 08:07:08 +0000 | [diff] [blame] | 90 | |
| 91 | /// @name Symbol Managment |
| 92 | /// @{ |
Chris Lattner | 1d72a76 | 2010-03-14 08:23:30 +0000 | [diff] [blame] | 93 | |
| 94 | /// CreateTempSymbol - Create and return a new assembler temporary symbol |
| 95 | /// with a unique but unspecified name. |
| 96 | MCSymbol *CreateTempSymbol(); |
Daniel Dunbar | 869a5e7 | 2009-08-31 08:07:08 +0000 | [diff] [blame] | 97 | |
Kevin Enderby | ebe7fcd | 2010-05-17 23:08:19 +0000 | [diff] [blame] | 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 | |
Chris Lattner | c69485e | 2009-06-24 04:31:49 +0000 | [diff] [blame] | 106 | /// GetOrCreateSymbol - Lookup the symbol inside with the specified |
Dan Gohman | fb76fe0 | 2010-02-22 04:10:52 +0000 | [diff] [blame] | 107 | /// @p Name. If it exists, return it. If not, create a forward |
Chris Lattner | c69485e | 2009-06-24 04:31:49 +0000 | [diff] [blame] | 108 | /// reference and return it. |
| 109 | /// |
| 110 | /// @param Name - The symbol name, which must be unique across all symbols. |
Chris Lattner | 9b97a73 | 2010-03-30 18:10:53 +0000 | [diff] [blame] | 111 | MCSymbol *GetOrCreateSymbol(StringRef Name); |
| 112 | MCSymbol *GetOrCreateSymbol(const Twine &Name); |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 113 | |
Dan Gohman | fb76fe0 | 2010-02-22 04:10:52 +0000 | [diff] [blame] | 114 | /// LookupSymbol - Get the symbol for \p Name, or null. |
Daniel Dunbar | 2928c83 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 115 | MCSymbol *LookupSymbol(StringRef Name) const; |
Daniel Dunbar | 25e0d8f | 2009-06-23 20:24:17 +0000 | [diff] [blame] | 116 | |
Daniel Dunbar | 869a5e7 | 2009-08-31 08:07:08 +0000 | [diff] [blame] | 117 | /// @} |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 118 | |
| 119 | /// @name Section Managment |
| 120 | /// @{ |
| 121 | |
Chris Lattner | 2277221 | 2010-04-08 20:40:11 +0000 | [diff] [blame] | 122 | /// getMachOSection - Return the MCSection for the specified mach-o section. |
| 123 | /// This requires the operands to be valid. |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 124 | const MCSectionMachO *getMachOSection(StringRef Segment, |
| 125 | StringRef Section, |
| 126 | unsigned TypeAndAttributes, |
| 127 | unsigned Reserved2, |
| 128 | SectionKind K); |
Chris Lattner | 2277221 | 2010-04-08 20:40:11 +0000 | [diff] [blame] | 129 | const MCSectionMachO *getMachOSection(StringRef Segment, |
| 130 | StringRef Section, |
| 131 | unsigned TypeAndAttributes, |
| 132 | SectionKind K) { |
| 133 | return getMachOSection(Segment, Section, TypeAndAttributes, 0, K); |
| 134 | } |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 135 | |
Chris Lattner | 74aae47 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 136 | const MCSection *getELFSection(StringRef Section, unsigned Type, |
| 137 | unsigned Flags, SectionKind Kind, |
Matt Fleming | 3e09669 | 2010-08-16 18:33:46 +0000 | [diff] [blame] | 138 | bool IsExplicit = false, |
| 139 | unsigned EntrySize = 0); |
Chris Lattner | eb40a0f | 2010-05-07 17:17:41 +0000 | [diff] [blame] | 140 | |
Chris Lattner | 6e5ce28 | 2010-05-07 21:49:09 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 74aae47 | 2010-04-08 21:26:26 +0000 | [diff] [blame] | 149 | |
Chris Lattner | f0559e4 | 2010-04-08 20:30:37 +0000 | [diff] [blame] | 150 | /// @} |
Daniel Dunbar | 869a5e7 | 2009-08-31 08:07:08 +0000 | [diff] [blame] | 151 | |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 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 | |
Kevin Enderby | c1840b3 | 2010-08-24 20:32:42 +0000 | [diff] [blame^] | 158 | bool ValidateDwarfFileNumber(unsigned FileNumber); |
| 159 | |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 160 | const std::vector<MCDwarfFile *> &getMCDwarfFiles() { |
| 161 | return MCDwarfFiles; |
| 162 | } |
Kevin Enderby | b07ce60 | 2010-08-09 22:52:14 +0000 | [diff] [blame] | 163 | const std::vector<StringRef> &getMCDwarfDirs() { |
| 164 | return MCDwarfDirs; |
| 165 | } |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 166 | |
Kevin Enderby | c1840b3 | 2010-08-24 20:32:42 +0000 | [diff] [blame^] | 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 | |
Kevin Enderby | 7cbf73a | 2010-07-28 20:55:35 +0000 | [diff] [blame] | 180 | /// @} |
| 181 | |
Kevin Enderby | f187ac5 | 2010-06-28 21:45:58 +0000 | [diff] [blame] | 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 | |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 192 | void *Allocate(unsigned Size, unsigned Align = 8) { |
| 193 | return Allocator.Allocate(Size, Align); |
| 194 | } |
Daniel Dunbar | e579849 | 2009-10-16 01:33:11 +0000 | [diff] [blame] | 195 | void Deallocate(void *Ptr) { |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 196 | } |
Daniel Dunbar | 25e0d8f | 2009-06-23 20:24:17 +0000 | [diff] [blame] | 197 | }; |
| 198 | |
| 199 | } // end namespace llvm |
| 200 | |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 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, |
Daniel Dunbar | a11af53 | 2009-06-24 01:03:06 +0000 | [diff] [blame] | 226 | size_t Alignment = 16) throw () { |
Daniel Dunbar | ecc63f8 | 2009-06-23 22:01:43 +0000 | [diff] [blame] | 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 | |
Daniel Dunbar | 25e0d8f | 2009-06-23 20:24:17 +0000 | [diff] [blame] | 274 | #endif |