MCContext.h revision 212904
1179404Sobrien//===- MCContext.h - Machine Code Context -----------------------*- C++ -*-===// 2218822Sdim// 3218822Sdim// The LLVM Compiler Infrastructure 4179404Sobrien// 5179404Sobrien// This file is distributed under the University of Illinois Open Source 6179404Sobrien// License. See LICENSE.TXT for details. 7179404Sobrien// 8179404Sobrien//===----------------------------------------------------------------------===// 9179404Sobrien 10179404Sobrien#ifndef LLVM_MC_MCCONTEXT_H 11179404Sobrien#define LLVM_MC_MCCONTEXT_H 12179404Sobrien 13179404Sobrien#include "llvm/MC/SectionKind.h" 14179404Sobrien#include "llvm/MC/MCDwarf.h" 15179404Sobrien#include "llvm/ADT/DenseMap.h" 16179404Sobrien#include "llvm/ADT/StringMap.h" 17179404Sobrien#include "llvm/Support/Allocator.h" 18179404Sobrien#include "llvm/Support/raw_ostream.h" 19179404Sobrien#include <vector> // FIXME: Shouldn't be needed. 20179404Sobrien 21179404Sobriennamespace llvm { 22218822Sdim class MCAsmInfo; 23218822Sdim class MCExpr; 24179404Sobrien class MCSection; 25179404Sobrien class MCSymbol; 26179404Sobrien class MCLabel; 27179404Sobrien class MCDwarfFile; 28179404Sobrien class MCDwarfLoc; 29179404Sobrien class MCLineSection; 30179404Sobrien class StringRef; 31179404Sobrien class Twine; 32179404Sobrien class MCSectionMachO; 33179404Sobrien 34179404Sobrien /// MCContext - Context object for machine code objects. This class owns all 35179404Sobrien /// of the sections that it creates. 36179404Sobrien /// 37179404Sobrien class MCContext { 38179404Sobrien MCContext(const MCContext&); // DO NOT IMPLEMENT 39179404Sobrien MCContext &operator=(const MCContext&); // DO NOT IMPLEMENT 40179404Sobrien 41179404Sobrien /// The MCAsmInfo for this target. 42179404Sobrien const MCAsmInfo &MAI; 43179404Sobrien 44179404Sobrien /// Symbols - Bindings of names to symbols. 45179404Sobrien StringMap<MCSymbol*> Symbols; 46179404Sobrien 47179404Sobrien /// NextUniqueID - The next ID to dole out to an unnamed assembler temporary 48179404Sobrien /// symbol. 49179404Sobrien unsigned NextUniqueID; 50179404Sobrien 51179404Sobrien /// Instances of directional local labels. 52179404Sobrien DenseMap<unsigned, MCLabel *> Instances; 53179404Sobrien /// NextInstance() creates the next instance of the directional local label 54179404Sobrien /// for the LocalLabelVal and adds it to the map if needed. 55179404Sobrien unsigned NextInstance(int64_t LocalLabelVal); 56179404Sobrien /// GetInstance() gets the current instance of the directional local label 57179404Sobrien /// for the LocalLabelVal and adds it to the map if needed. 58179404Sobrien unsigned GetInstance(int64_t LocalLabelVal); 59179404Sobrien 60179404Sobrien /// The file name of the log file from the enviromment variable 61218822Sdim /// AS_SECURE_LOG_FILE. Which must be set before the .secure_log_unique 62218822Sdim /// directive is used or it is an error. 63179404Sobrien char *SecureLogFile; 64218822Sdim /// The stream that gets written to for the .secure_log_unique directive. 65218822Sdim raw_ostream *SecureLog; 66218822Sdim /// Boolean toggled when .secure_log_unique / .secure_log_reset is seen to 67179404Sobrien /// catch errors if .secure_log_unique appears twice without 68179404Sobrien /// .secure_log_reset appearing between them. 69179404Sobrien bool SecureLogUsed; 70179404Sobrien 71179404Sobrien /// The dwarf file and directory tables from the dwarf .file directive. 72179404Sobrien std::vector<MCDwarfFile *> MCDwarfFiles; 73179404Sobrien std::vector<StringRef> MCDwarfDirs; 74179404Sobrien 75179404Sobrien /// The current dwarf line information from the last dwarf .loc directive. 76179404Sobrien MCDwarfLoc CurrentDwarfLoc; 77179404Sobrien bool DwarfLocSeen; 78179404Sobrien 79179404Sobrien /// The dwarf line information from the .loc directives for the sections 80179404Sobrien /// with assembled machine instructions have after seeing .loc directives. 81179404Sobrien DenseMap<const MCSection *, MCLineSection *> MCLineSections; 82179404Sobrien 83179404Sobrien /// Allocator - Allocator object used for creating machine code objects. 84179404Sobrien /// 85218822Sdim /// We use a bump pointer allocator to avoid the need to track all allocated 86218822Sdim /// objects. 87179404Sobrien BumpPtrAllocator Allocator; 88179404Sobrien 89179404Sobrien void *MachOUniquingMap, *ELFUniquingMap, *COFFUniquingMap; 90179404Sobrien public: 91179404Sobrien explicit MCContext(const MCAsmInfo &MAI); 92179404Sobrien ~MCContext(); 93179404Sobrien 94179404Sobrien const MCAsmInfo &getAsmInfo() const { return MAI; } 95179404Sobrien 96179404Sobrien /// @name Symbol Managment 97179404Sobrien /// @{ 98179404Sobrien 99179404Sobrien /// CreateTempSymbol - Create and return a new assembler temporary symbol 100179404Sobrien /// with a unique but unspecified name. 101179404Sobrien MCSymbol *CreateTempSymbol(); 102179404Sobrien 103179404Sobrien /// CreateDirectionalLocalSymbol - Create the defintion of a directional 104179404Sobrien /// local symbol for numbered label (used for "1:" defintions). 105179404Sobrien MCSymbol *CreateDirectionalLocalSymbol(int64_t LocalLabelVal); 106179404Sobrien 107179404Sobrien /// GetDirectionalLocalSymbol - Create and return a directional local 108179404Sobrien /// symbol for numbered label (used for "1b" or 1f" references). 109179404Sobrien MCSymbol *GetDirectionalLocalSymbol(int64_t LocalLabelVal, int bORf); 110179404Sobrien 111179404Sobrien /// GetOrCreateSymbol - Lookup the symbol inside with the specified 112179404Sobrien /// @p Name. If it exists, return it. If not, create a forward 113179404Sobrien /// reference and return it. 114179404Sobrien /// 115179404Sobrien /// @param Name - The symbol name, which must be unique across all symbols. 116179404Sobrien MCSymbol *GetOrCreateSymbol(StringRef Name); 117179404Sobrien MCSymbol *GetOrCreateSymbol(const Twine &Name); 118179404Sobrien 119218822Sdim /// LookupSymbol - Get the symbol for \p Name, or null. 120179404Sobrien MCSymbol *LookupSymbol(StringRef Name) const; 121179404Sobrien 122218822Sdim /// @} 123179404Sobrien 124218822Sdim /// @name Section Managment 125179404Sobrien /// @{ 126218822Sdim 127218822Sdim /// getMachOSection - Return the MCSection for the specified mach-o section. 128179404Sobrien /// This requires the operands to be valid. 129179404Sobrien const MCSectionMachO *getMachOSection(StringRef Segment, 130179404Sobrien StringRef Section, 131179404Sobrien unsigned TypeAndAttributes, 132179404Sobrien unsigned Reserved2, 133179404Sobrien SectionKind K); 134179404Sobrien const MCSectionMachO *getMachOSection(StringRef Segment, 135179404Sobrien StringRef Section, 136179404Sobrien unsigned TypeAndAttributes, 137179404Sobrien SectionKind K) { 138179404Sobrien return getMachOSection(Segment, Section, TypeAndAttributes, 0, K); 139179404Sobrien } 140179404Sobrien 141179404Sobrien const MCSection *getELFSection(StringRef Section, unsigned Type, 142179404Sobrien unsigned Flags, SectionKind Kind, 143179404Sobrien bool IsExplicit = false, 144179404Sobrien unsigned EntrySize = 0); 145179404Sobrien 146179404Sobrien const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics, 147179404Sobrien int Selection, SectionKind Kind); 148179404Sobrien 149179404Sobrien const MCSection *getCOFFSection(StringRef Section, unsigned Characteristics, 150179404Sobrien SectionKind Kind) { 151179404Sobrien return getCOFFSection (Section, Characteristics, 0, Kind); 152208737Sjmallett } 153208737Sjmallett 154179404Sobrien 155179404Sobrien /// @} 156179404Sobrien 157179404Sobrien /// @name Dwarf Managment 158218822Sdim /// @{ 159179404Sobrien 160179404Sobrien /// GetDwarfFile - creates an entry in the dwarf file and directory tables. 161179404Sobrien unsigned GetDwarfFile(StringRef FileName, unsigned FileNumber); 162218822Sdim 163179404Sobrien bool ValidateDwarfFileNumber(unsigned FileNumber); 164179404Sobrien 165218822Sdim const std::vector<MCDwarfFile *> &getMCDwarfFiles() { 166218822Sdim return MCDwarfFiles; 167218822Sdim } 168218822Sdim const std::vector<StringRef> &getMCDwarfDirs() { 169218822Sdim return MCDwarfDirs; 170218822Sdim } 171218822Sdim DenseMap<const MCSection *, MCLineSection *> &getMCLineSections() { 172218822Sdim return MCLineSections; 173218822Sdim } 174218822Sdim 175218822Sdim /// setCurrentDwarfLoc - saves the information from the currently parsed 176179404Sobrien /// dwarf .loc directive and sets DwarfLocSeen. When the next instruction /// is assembled an entry in the line number table with this information and 177 /// the address of the instruction will be created. 178 void setCurrentDwarfLoc(unsigned FileNum, unsigned Line, unsigned Column, 179 unsigned Flags, unsigned Isa) { 180 CurrentDwarfLoc.setFileNum(FileNum); 181 CurrentDwarfLoc.setLine(Line); 182 CurrentDwarfLoc.setColumn(Column); 183 CurrentDwarfLoc.setFlags(Flags); 184 CurrentDwarfLoc.setIsa(Isa); 185 DwarfLocSeen = true; 186 } 187 void clearDwarfLocSeen() { DwarfLocSeen = false; } 188 189 bool getDwarfLocSeen() { return DwarfLocSeen; } 190 const MCDwarfLoc &getCurrentDwarfLoc() { return CurrentDwarfLoc; } 191 192 /// @} 193 194 char *getSecureLogFile() { return SecureLogFile; } 195 raw_ostream *getSecureLog() { return SecureLog; } 196 bool getSecureLogUsed() { return SecureLogUsed; } 197 void setSecureLog(raw_ostream *Value) { 198 SecureLog = Value; 199 } 200 void setSecureLogUsed(bool Value) { 201 SecureLogUsed = Value; 202 } 203 204 void *Allocate(unsigned Size, unsigned Align = 8) { 205 return Allocator.Allocate(Size, Align); 206 } 207 void Deallocate(void *Ptr) { 208 } 209 }; 210 211} // end namespace llvm 212 213// operator new and delete aren't allowed inside namespaces. 214// The throw specifications are mandated by the standard. 215/// @brief Placement new for using the MCContext's allocator. 216/// 217/// This placement form of operator new uses the MCContext's allocator for 218/// obtaining memory. It is a non-throwing new, which means that it returns 219/// null on error. (If that is what the allocator does. The current does, so if 220/// this ever changes, this operator will have to be changed, too.) 221/// Usage looks like this (assuming there's an MCContext 'Context' in scope): 222/// @code 223/// // Default alignment (16) 224/// IntegerLiteral *Ex = new (Context) IntegerLiteral(arguments); 225/// // Specific alignment 226/// IntegerLiteral *Ex2 = new (Context, 8) IntegerLiteral(arguments); 227/// @endcode 228/// Please note that you cannot use delete on the pointer; it must be 229/// deallocated using an explicit destructor call followed by 230/// @c Context.Deallocate(Ptr). 231/// 232/// @param Bytes The number of bytes to allocate. Calculated by the compiler. 233/// @param C The MCContext that provides the allocator. 234/// @param Alignment The alignment of the allocated memory (if the underlying 235/// allocator supports it). 236/// @return The allocated memory. Could be NULL. 237inline void *operator new(size_t Bytes, llvm::MCContext &C, 238 size_t Alignment = 16) throw () { 239 return C.Allocate(Bytes, Alignment); 240} 241/// @brief Placement delete companion to the new above. 242/// 243/// This operator is just a companion to the new above. There is no way of 244/// invoking it directly; see the new operator for more details. This operator 245/// is called implicitly by the compiler if a placement new expression using 246/// the MCContext throws in the object constructor. 247inline void operator delete(void *Ptr, llvm::MCContext &C, size_t) 248 throw () { 249 C.Deallocate(Ptr); 250} 251 252/// This placement form of operator new[] uses the MCContext's allocator for 253/// obtaining memory. It is a non-throwing new[], which means that it returns 254/// null on error. 255/// Usage looks like this (assuming there's an MCContext 'Context' in scope): 256/// @code 257/// // Default alignment (16) 258/// char *data = new (Context) char[10]; 259/// // Specific alignment 260/// char *data = new (Context, 8) char[10]; 261/// @endcode 262/// Please note that you cannot use delete on the pointer; it must be 263/// deallocated using an explicit destructor call followed by 264/// @c Context.Deallocate(Ptr). 265/// 266/// @param Bytes The number of bytes to allocate. Calculated by the compiler. 267/// @param C The MCContext that provides the allocator. 268/// @param Alignment The alignment of the allocated memory (if the underlying 269/// allocator supports it). 270/// @return The allocated memory. Could be NULL. 271inline void *operator new[](size_t Bytes, llvm::MCContext& C, 272 size_t Alignment = 16) throw () { 273 return C.Allocate(Bytes, Alignment); 274} 275 276/// @brief Placement delete[] companion to the new[] above. 277/// 278/// This operator is just a companion to the new[] above. There is no way of 279/// invoking it directly; see the new[] operator for more details. This operator 280/// is called implicitly by the compiler if a placement new[] expression using 281/// the MCContext throws in the object constructor. 282inline void operator delete[](void *Ptr, llvm::MCContext &C) throw () { 283 C.Deallocate(Ptr); 284} 285 286#endif 287