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