LLVM API Documentation

MCAsmInfo.h
Go to the documentation of this file.
00001 //===-- llvm/MC/MCAsmInfo.h - Asm info --------------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains a class to be used as the basis for target specific
00011 // asm writers.  This class primarily takes care of global printing constants,
00012 // which are used in very similar ways across all targets.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #ifndef LLVM_MC_MCASMINFO_H
00017 #define LLVM_MC_MCASMINFO_H
00018 
00019 #include "llvm/MC/MCDirectives.h"
00020 #include "llvm/MC/MCDwarf.h"
00021 #include "llvm/MC/MachineLocation.h"
00022 #include <cassert>
00023 #include <vector>
00024 
00025 namespace llvm {
00026 class MCExpr;
00027 class MCSection;
00028 class MCStreamer;
00029 class MCSymbol;
00030 class MCContext;
00031 
00032 namespace WinEH {
00033 enum class EncodingType {
00034   Invalid, /// Invalid
00035   Alpha,   /// Windows Alpha
00036   Alpha64, /// Windows AXP64
00037   ARM,     /// Windows NT (Windows on ARM)
00038   CE,      /// Windows CE ARM, PowerPC, SH3, SH4
00039   Itanium, /// Windows x64, Windows Itanium (IA-64)
00040   MIPS = Alpha,
00041 };
00042 }
00043 
00044 enum class ExceptionHandling {
00045   None,         /// No exception support
00046   DwarfCFI,     /// DWARF-like instruction based exceptions
00047   SjLj,         /// setjmp/longjmp based exceptions
00048   ARM,          /// ARM EHABI
00049   ItaniumWinEH, /// Itanium EH built on Windows unwind info (.pdata and .xdata)
00050   MSVC,         /// MSVC compatible exception handling
00051 };
00052 
00053 namespace LCOMM {
00054 enum LCOMMType { NoAlignment, ByteAlignment, Log2Alignment };
00055 }
00056 
00057 /// This class is intended to be used as a base class for asm
00058 /// properties and features specific to the target.
00059 class MCAsmInfo {
00060 protected:
00061   //===------------------------------------------------------------------===//
00062   // Properties to be set by the target writer, used to configure asm printer.
00063   //
00064 
00065   /// Pointer size in bytes.  Default is 4.
00066   unsigned PointerSize;
00067 
00068   /// Size of the stack slot reserved for callee-saved registers, in bytes.
00069   /// Default is same as pointer size.
00070   unsigned CalleeSaveStackSlotSize;
00071 
00072   /// True if target is little endian.  Default is true.
00073   bool IsLittleEndian;
00074 
00075   /// True if target stack grow up.  Default is false.
00076   bool StackGrowsUp;
00077 
00078   /// True if this target has the MachO .subsections_via_symbols directive.
00079   /// Default is false.
00080   bool HasSubsectionsViaSymbols;
00081 
00082   /// True if this is a MachO target that supports the macho-specific .zerofill
00083   /// directive for emitting BSS Symbols.  Default is false.
00084   bool HasMachoZeroFillDirective;
00085 
00086   /// True if this is a MachO target that supports the macho-specific .tbss
00087   /// directive for emitting thread local BSS Symbols.  Default is false.
00088   bool HasMachoTBSSDirective;
00089 
00090   /// True if the compiler should emit a ".reference .constructors_used" or
00091   /// ".reference .destructors_used" directive after the static ctor/dtor
00092   /// list.  This directive is only emitted in Static relocation model.  Default
00093   /// is false.
00094   bool HasStaticCtorDtorReferenceInStaticMode;
00095 
00096   /// This is the maximum possible length of an instruction, which is needed to
00097   /// compute the size of an inline asm.  Defaults to 4.
00098   unsigned MaxInstLength;
00099 
00100   /// Every possible instruction length is a multiple of this value.  Factored
00101   /// out in .debug_frame and .debug_line.  Defaults to 1.
00102   unsigned MinInstAlignment;
00103 
00104   /// The '$' token, when not referencing an identifier or constant, refers to
00105   /// the current PC.  Defaults to false.
00106   bool DollarIsPC;
00107 
00108   /// This string, if specified, is used to separate instructions from each
00109   /// other when on the same line.  Defaults to ';'
00110   const char *SeparatorString;
00111 
00112   /// This indicates the comment character used by the assembler.  Defaults to
00113   /// "#"
00114   const char *CommentString;
00115 
00116   /// This is appended to emitted labels.  Defaults to ":"
00117   const char *LabelSuffix;
00118 
00119   // Print the EH begin symbol with an assignment. Defaults to false.
00120   bool UseAssignmentForEHBegin;
00121 
00122   /// This prefix is used for globals like constant pool entries that are
00123   /// completely private to the .s file and should not have names in the .o
00124   /// file.  Defaults to "L"
00125   const char *PrivateGlobalPrefix;
00126 
00127   /// This prefix is used for labels for basic blocks. Defaults to the same as
00128   /// PrivateGlobalPrefix.
00129   const char *PrivateLabelPrefix;
00130 
00131   /// This prefix is used for symbols that should be passed through the
00132   /// assembler but be removed by the linker.  This is 'l' on Darwin, currently
00133   /// used for some ObjC metadata.  The default of "" meast that for this system
00134   /// a plain private symbol should be used.  Defaults to "".
00135   const char *LinkerPrivateGlobalPrefix;
00136 
00137   /// If these are nonempty, they contain a directive to emit before and after
00138   /// an inline assembly statement.  Defaults to "#APP\n", "#NO_APP\n"
00139   const char *InlineAsmStart;
00140   const char *InlineAsmEnd;
00141 
00142   /// These are assembly directives that tells the assembler to interpret the
00143   /// following instructions differently.  Defaults to ".code16", ".code32",
00144   /// ".code64".
00145   const char *Code16Directive;
00146   const char *Code32Directive;
00147   const char *Code64Directive;
00148 
00149   /// Which dialect of an assembler variant to use.  Defaults to 0
00150   unsigned AssemblerDialect;
00151 
00152   /// This is true if the assembler allows @ characters in symbol names.
00153   /// Defaults to false.
00154   bool AllowAtInName;
00155 
00156   /// This is true if data region markers should be printed as
00157   /// ".data_region/.end_data_region" directives. If false, use "$d/$a" labels
00158   /// instead.
00159   bool UseDataRegionDirectives;
00160 
00161   //===--- Data Emission Directives -------------------------------------===//
00162 
00163   /// This should be set to the directive used to get some number of zero bytes
00164   /// emitted to the current section.  Common cases are "\t.zero\t" and
00165   /// "\t.space\t".  If this is set to null, the Data*bitsDirective's will be
00166   /// used to emit zero bytes.  Defaults to "\t.zero\t"
00167   const char *ZeroDirective;
00168 
00169   /// This directive allows emission of an ascii string with the standard C
00170   /// escape characters embedded into it.  Defaults to "\t.ascii\t"
00171   const char *AsciiDirective;
00172 
00173   /// If not null, this allows for special handling of zero terminated strings
00174   /// on this target.  This is commonly supported as ".asciz".  If a target
00175   /// doesn't support this, it can be set to null.  Defaults to "\t.asciz\t"
00176   const char *AscizDirective;
00177 
00178   /// These directives are used to output some unit of integer data to the
00179   /// current section.  If a data directive is set to null, smaller data
00180   /// directives will be used to emit the large sizes.  Defaults to "\t.byte\t",
00181   /// "\t.short\t", "\t.long\t", "\t.quad\t"
00182   const char *Data8bitsDirective;
00183   const char *Data16bitsDirective;
00184   const char *Data32bitsDirective;
00185   const char *Data64bitsDirective;
00186 
00187   /// If non-null, a directive that is used to emit a word which should be
00188   /// relocated as a 64-bit GP-relative offset, e.g. .gpdword on Mips.  Defaults
00189   /// to NULL.
00190   const char *GPRel64Directive;
00191 
00192   /// If non-null, a directive that is used to emit a word which should be
00193   /// relocated as a 32-bit GP-relative offset, e.g. .gpword on Mips or .gprel32
00194   /// on Alpha.  Defaults to NULL.
00195   const char *GPRel32Directive;
00196 
00197   /// This is true if this target uses "Sun Style" syntax for section switching
00198   /// ("#alloc,#write" etc) instead of the normal ELF syntax (,"a,w") in
00199   /// .section directives.  Defaults to false.
00200   bool SunStyleELFSectionSwitchSyntax;
00201 
00202   /// This is true if this target uses ELF '.section' directive before the
00203   /// '.bss' one. It's used for PPC/Linux which doesn't support the '.bss'
00204   /// directive only.  Defaults to false.
00205   bool UsesELFSectionDirectiveForBSS;
00206 
00207   bool NeedsDwarfSectionOffsetDirective;
00208 
00209   //===--- Alignment Information ----------------------------------------===//
00210 
00211   /// If this is true (the default) then the asmprinter emits ".align N"
00212   /// directives, where N is the number of bytes to align to.  Otherwise, it
00213   /// emits ".align log2(N)", e.g. 3 to align to an 8 byte boundary.  Defaults
00214   /// to true.
00215   bool AlignmentIsInBytes;
00216 
00217   /// If non-zero, this is used to fill the executable space created as the
00218   /// result of a alignment directive.  Defaults to 0
00219   unsigned TextAlignFillValue;
00220 
00221   //===--- Global Variable Emission Directives --------------------------===//
00222 
00223   /// This is the directive used to declare a global entity. Defaults to
00224   /// ".globl".
00225   const char *GlobalDirective;
00226 
00227   /// True if the expression
00228   ///   .long f - g
00229   /// uses an relocation but it can be supressed by writting
00230   ///   a = f - g
00231   ///   .long a
00232   bool SetDirectiveSuppressesReloc;
00233 
00234   /// False if the assembler requires that we use
00235   /// \code
00236   ///   Lc = a - b
00237   ///   .long Lc
00238   /// \endcode
00239   //
00240   /// instead of
00241   //
00242   /// \code
00243   ///   .long a - b
00244   /// \endcode
00245   ///
00246   ///  Defaults to true.
00247   bool HasAggressiveSymbolFolding;
00248 
00249   /// True is .comm's and .lcomms optional alignment is to be specified in bytes
00250   /// instead of log2(n).  Defaults to true.
00251   bool COMMDirectiveAlignmentIsInBytes;
00252 
00253   /// Describes if the .lcomm directive for the target supports an alignment
00254   /// argument and how it is interpreted.  Defaults to NoAlignment.
00255   LCOMM::LCOMMType LCOMMDirectiveAlignmentType;
00256 
00257   /// True if the target has .type and .size directives, this is true for most
00258   /// ELF targets.  Defaults to true.
00259   bool HasDotTypeDotSizeDirective;
00260 
00261   /// True if the target has a single parameter .file directive, this is true
00262   /// for ELF targets.  Defaults to true.
00263   bool HasSingleParameterDotFile;
00264 
00265   /// True if the target has a .ident directive, this is true for ELF targets.
00266   /// Defaults to false.
00267   bool HasIdentDirective;
00268 
00269   /// True if this target supports the MachO .no_dead_strip directive.  Defaults
00270   /// to false.
00271   bool HasNoDeadStrip;
00272 
00273   /// Used to declare a global as being a weak symbol. Defaults to ".weak".
00274   const char *WeakDirective;
00275 
00276   /// This directive, if non-null, is used to declare a global as being a weak
00277   /// undefined symbol.  Defaults to NULL.
00278   const char *WeakRefDirective;
00279 
00280   /// True if we have a directive to declare a global as being a weak defined
00281   /// symbol.  Defaults to false.
00282   bool HasWeakDefDirective;
00283 
00284   /// True if we have a directive to declare a global as being a weak defined
00285   /// symbol that can be hidden (unexported).  Defaults to false.
00286   bool HasWeakDefCanBeHiddenDirective;
00287 
00288   /// True if we have a .linkonce directive.  This is used on cygwin/mingw.
00289   /// Defaults to false.
00290   bool HasLinkOnceDirective;
00291 
00292   /// This attribute, if not MCSA_Invalid, is used to declare a symbol as having
00293   /// hidden visibility.  Defaults to MCSA_Hidden.
00294   MCSymbolAttr HiddenVisibilityAttr;
00295 
00296   /// This attribute, if not MCSA_Invalid, is used to declare an undefined
00297   /// symbol as having hidden visibility. Defaults to MCSA_Hidden.
00298   MCSymbolAttr HiddenDeclarationVisibilityAttr;
00299 
00300   /// This attribute, if not MCSA_Invalid, is used to declare a symbol as having
00301   /// protected visibility.  Defaults to MCSA_Protected
00302   MCSymbolAttr ProtectedVisibilityAttr;
00303 
00304   //===--- Dwarf Emission Directives -----------------------------------===//
00305 
00306   /// True if target supports emission of debugging information.  Defaults to
00307   /// false.
00308   bool SupportsDebugInformation;
00309 
00310   /// Exception handling format for the target.  Defaults to None.
00311   ExceptionHandling ExceptionsType;
00312 
00313   /// Windows exception handling data (.pdata) encoding.  Defaults to Invalid.
00314   WinEH::EncodingType WinEHEncodingType;
00315 
00316   /// True if Dwarf2 output generally uses relocations for references to other
00317   /// .debug_* sections.
00318   bool DwarfUsesRelocationsAcrossSections;
00319 
00320   /// True if DWARF FDE symbol reference relocations should be replaced by an
00321   /// absolute difference.
00322   bool DwarfFDESymbolsUseAbsDiff;
00323 
00324   /// True if dwarf register numbers are printed instead of symbolic register
00325   /// names in .cfi_* directives.  Defaults to false.
00326   bool DwarfRegNumForCFI;
00327 
00328   /// True if target uses parens to indicate the symbol variant instead of @.
00329   /// For example, foo(plt) instead of foo@plt.  Defaults to false.
00330   bool UseParensForSymbolVariant;
00331 
00332   //===--- Prologue State ----------------------------------------------===//
00333 
00334   std::vector<MCCFIInstruction> InitialFrameState;
00335 
00336   //===--- Integrated Assembler State ----------------------------------===//
00337 
00338   /// Should we use the integrated assembler?
00339   /// The integrated assembler should be enabled by default (by the
00340   /// constructors) when failing to parse a valid piece of assembly (inline
00341   /// or otherwise) is considered a bug. It may then be overridden after
00342   /// construction (see LLVMTargetMachine::initAsmInfo()).
00343   bool UseIntegratedAssembler;
00344 
00345   /// Compress DWARF debug sections. Defaults to false.
00346   bool CompressDebugSections;
00347 
00348 public:
00349   explicit MCAsmInfo();
00350   virtual ~MCAsmInfo();
00351 
00352   /// Get the pointer size in bytes.
00353   unsigned getPointerSize() const { return PointerSize; }
00354 
00355   /// Get the callee-saved register stack slot
00356   /// size in bytes.
00357   unsigned getCalleeSaveStackSlotSize() const {
00358     return CalleeSaveStackSlotSize;
00359   }
00360 
00361   /// True if the target is little endian.
00362   bool isLittleEndian() const { return IsLittleEndian; }
00363 
00364   /// True if target stack grow up.
00365   bool isStackGrowthDirectionUp() const { return StackGrowsUp; }
00366 
00367   bool hasSubsectionsViaSymbols() const { return HasSubsectionsViaSymbols; }
00368 
00369   // Data directive accessors.
00370 
00371   const char *getData8bitsDirective() const { return Data8bitsDirective; }
00372   const char *getData16bitsDirective() const { return Data16bitsDirective; }
00373   const char *getData32bitsDirective() const { return Data32bitsDirective; }
00374   const char *getData64bitsDirective() const { return Data64bitsDirective; }
00375   const char *getGPRel64Directive() const { return GPRel64Directive; }
00376   const char *getGPRel32Directive() const { return GPRel32Directive; }
00377 
00378   /// Targets can implement this method to specify a section to switch to if the
00379   /// translation unit doesn't have any trampolines that require an executable
00380   /// stack.
00381   virtual const MCSection *getNonexecutableStackSection(MCContext &Ctx) const {
00382     return nullptr;
00383   }
00384 
00385   virtual const MCExpr *getExprForPersonalitySymbol(const MCSymbol *Sym,
00386                                                     unsigned Encoding,
00387                                                     MCStreamer &Streamer) const;
00388 
00389   virtual const MCExpr *getExprForFDESymbol(const MCSymbol *Sym,
00390                                             unsigned Encoding,
00391                                             MCStreamer &Streamer) const;
00392 
00393   bool usesSunStyleELFSectionSwitchSyntax() const {
00394     return SunStyleELFSectionSwitchSyntax;
00395   }
00396 
00397   bool usesELFSectionDirectiveForBSS() const {
00398     return UsesELFSectionDirectiveForBSS;
00399   }
00400 
00401   bool needsDwarfSectionOffsetDirective() const {
00402     return NeedsDwarfSectionOffsetDirective;
00403   }
00404 
00405   // Accessors.
00406 
00407   bool hasMachoZeroFillDirective() const { return HasMachoZeroFillDirective; }
00408   bool hasMachoTBSSDirective() const { return HasMachoTBSSDirective; }
00409   bool hasStaticCtorDtorReferenceInStaticMode() const {
00410     return HasStaticCtorDtorReferenceInStaticMode;
00411   }
00412   unsigned getMaxInstLength() const { return MaxInstLength; }
00413   unsigned getMinInstAlignment() const { return MinInstAlignment; }
00414   bool getDollarIsPC() const { return DollarIsPC; }
00415   const char *getSeparatorString() const { return SeparatorString; }
00416 
00417   /// This indicates the column (zero-based) at which asm comments should be
00418   /// printed.
00419   unsigned getCommentColumn() const { return 40; }
00420 
00421   const char *getCommentString() const { return CommentString; }
00422   const char *getLabelSuffix() const { return LabelSuffix; }
00423 
00424   bool useAssignmentForEHBegin() const { return UseAssignmentForEHBegin; }
00425   const char *getPrivateGlobalPrefix() const { return PrivateGlobalPrefix; }
00426   const char *getPrivateLabelPrefix() const { return PrivateLabelPrefix; }
00427   bool hasLinkerPrivateGlobalPrefix() const {
00428     return LinkerPrivateGlobalPrefix[0] != '\0';
00429   }
00430   const char *getLinkerPrivateGlobalPrefix() const {
00431     if (hasLinkerPrivateGlobalPrefix())
00432       return LinkerPrivateGlobalPrefix;
00433     return getPrivateGlobalPrefix();
00434   }
00435   const char *getInlineAsmStart() const { return InlineAsmStart; }
00436   const char *getInlineAsmEnd() const { return InlineAsmEnd; }
00437   const char *getCode16Directive() const { return Code16Directive; }
00438   const char *getCode32Directive() const { return Code32Directive; }
00439   const char *getCode64Directive() const { return Code64Directive; }
00440   unsigned getAssemblerDialect() const { return AssemblerDialect; }
00441   bool doesAllowAtInName() const { return AllowAtInName; }
00442   bool doesSupportDataRegionDirectives() const {
00443     return UseDataRegionDirectives;
00444   }
00445   const char *getZeroDirective() const { return ZeroDirective; }
00446   const char *getAsciiDirective() const { return AsciiDirective; }
00447   const char *getAscizDirective() const { return AscizDirective; }
00448   bool getAlignmentIsInBytes() const { return AlignmentIsInBytes; }
00449   unsigned getTextAlignFillValue() const { return TextAlignFillValue; }
00450   const char *getGlobalDirective() const { return GlobalDirective; }
00451   bool doesSetDirectiveSuppressesReloc() const {
00452     return SetDirectiveSuppressesReloc;
00453   }
00454   bool hasAggressiveSymbolFolding() const { return HasAggressiveSymbolFolding; }
00455   bool getCOMMDirectiveAlignmentIsInBytes() const {
00456     return COMMDirectiveAlignmentIsInBytes;
00457   }
00458   LCOMM::LCOMMType getLCOMMDirectiveAlignmentType() const {
00459     return LCOMMDirectiveAlignmentType;
00460   }
00461   bool hasDotTypeDotSizeDirective() const { return HasDotTypeDotSizeDirective; }
00462   bool hasSingleParameterDotFile() const { return HasSingleParameterDotFile; }
00463   bool hasIdentDirective() const { return HasIdentDirective; }
00464   bool hasNoDeadStrip() const { return HasNoDeadStrip; }
00465   const char *getWeakDirective() const { return WeakDirective; }
00466   const char *getWeakRefDirective() const { return WeakRefDirective; }
00467   bool hasWeakDefDirective() const { return HasWeakDefDirective; }
00468   bool hasWeakDefCanBeHiddenDirective() const {
00469     return HasWeakDefCanBeHiddenDirective;
00470   }
00471   bool hasLinkOnceDirective() const { return HasLinkOnceDirective; }
00472 
00473   MCSymbolAttr getHiddenVisibilityAttr() const { return HiddenVisibilityAttr; }
00474   MCSymbolAttr getHiddenDeclarationVisibilityAttr() const {
00475     return HiddenDeclarationVisibilityAttr;
00476   }
00477   MCSymbolAttr getProtectedVisibilityAttr() const {
00478     return ProtectedVisibilityAttr;
00479   }
00480   bool doesSupportDebugInformation() const { return SupportsDebugInformation; }
00481   bool doesSupportExceptionHandling() const {
00482     return ExceptionsType != ExceptionHandling::None;
00483   }
00484   ExceptionHandling getExceptionHandlingType() const { return ExceptionsType; }
00485   WinEH::EncodingType getWinEHEncodingType() const { return WinEHEncodingType; }
00486 
00487   /// Return true if the exception handling type uses the language-specific data
00488   /// area (LSDA) format specified by the Itanium C++ ABI.
00489   bool usesItaniumLSDAForExceptions() const {
00490     return (ExceptionsType == ExceptionHandling::DwarfCFI ||
00491             ExceptionsType == ExceptionHandling::ARM ||
00492             // This Windows EH type uses the Itanium LSDA encoding.
00493             ExceptionsType == ExceptionHandling::ItaniumWinEH);
00494   }
00495 
00496   bool usesWindowsCFI() const {
00497     return ExceptionsType == ExceptionHandling::ItaniumWinEH ||
00498            ExceptionsType == ExceptionHandling::MSVC;
00499   }
00500 
00501   bool doesDwarfUseRelocationsAcrossSections() const {
00502     return DwarfUsesRelocationsAcrossSections;
00503   }
00504   bool doDwarfFDESymbolsUseAbsDiff() const { return DwarfFDESymbolsUseAbsDiff; }
00505   bool useDwarfRegNumForCFI() const { return DwarfRegNumForCFI; }
00506   bool useParensForSymbolVariant() const { return UseParensForSymbolVariant; }
00507 
00508   void addInitialFrameState(const MCCFIInstruction &Inst) {
00509     InitialFrameState.push_back(Inst);
00510   }
00511 
00512   const std::vector<MCCFIInstruction> &getInitialFrameState() const {
00513     return InitialFrameState;
00514   }
00515 
00516   /// Return true if assembly (inline or otherwise) should be parsed.
00517   bool useIntegratedAssembler() const { return UseIntegratedAssembler; }
00518 
00519   /// Set whether assembly (inline or otherwise) should be parsed.
00520   virtual void setUseIntegratedAssembler(bool Value) {
00521     UseIntegratedAssembler = Value;
00522   }
00523 
00524   bool compressDebugSections() const { return CompressDebugSections; }
00525 
00526   void setCompressDebugSections(bool CompressDebugSections) {
00527     this->CompressDebugSections = CompressDebugSections;
00528   }
00529 };
00530 }
00531 
00532 #endif