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