Home | History | Annotate | Download | only in MC
      1 //===-- llvm/MC/MCAsmInfo.h - Asm info --------------------------*- 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 // This file contains a class to be used as the basis for target specific
     11 // asm writers.  This class primarily takes care of global printing constants,
     12 // which are used in very similar ways across all targets.
     13 //
     14 //===----------------------------------------------------------------------===//
     15 
     16 #ifndef LLVM_MC_MCASMINFO_H
     17 #define LLVM_MC_MCASMINFO_H
     18 
     19 #include "llvm/MC/MCDirectives.h"
     20 #include "llvm/MC/MCDwarf.h"
     21 #include "llvm/MC/MachineLocation.h"
     22 #include <cassert>
     23 #include <vector>
     24 
     25 namespace llvm {
     26 class MCExpr;
     27 class MCSection;
     28 class MCStreamer;
     29 class MCSymbol;
     30 class MCContext;
     31 
     32 namespace WinEH {
     33 enum class EncodingType {
     34   Invalid, /// Invalid
     35   Alpha,   /// Windows Alpha
     36   Alpha64, /// Windows AXP64
     37   ARM,     /// Windows NT (Windows on ARM)
     38   CE,      /// Windows CE ARM, PowerPC, SH3, SH4
     39   Itanium, /// Windows x64, Windows Itanium (IA-64)
     40   MIPS = Alpha,
     41 };
     42 }
     43 
     44 enum class ExceptionHandling {
     45   None,     /// No exception support
     46   DwarfCFI, /// DWARF-like instruction based exceptions
     47   SjLj,     /// setjmp/longjmp based exceptions
     48   ARM,      /// ARM EHABI
     49   WinEH,    /// Windows Exception Handling
     50 };
     51 
     52 namespace LCOMM {
     53 enum LCOMMType { NoAlignment, ByteAlignment, Log2Alignment };
     54 }
     55 
     56 /// This class is intended to be used as a base class for asm
     57 /// properties and features specific to the target.
     58 class MCAsmInfo {
     59 protected:
     60   //===------------------------------------------------------------------===//
     61   // Properties to be set by the target writer, used to configure asm printer.
     62   //
     63 
     64   /// Pointer size in bytes.  Default is 4.
     65   unsigned PointerSize;
     66 
     67   /// Size of the stack slot reserved for callee-saved registers, in bytes.
     68   /// Default is same as pointer size.
     69   unsigned CalleeSaveStackSlotSize;
     70 
     71   /// True if target is little endian.  Default is true.
     72   bool IsLittleEndian;
     73 
     74   /// True if target stack grow up.  Default is false.
     75   bool StackGrowsUp;
     76 
     77   /// True if this target has the MachO .subsections_via_symbols directive.
     78   /// Default is false.
     79   bool HasSubsectionsViaSymbols;
     80 
     81   /// True if this is a MachO target that supports the macho-specific .zerofill
     82   /// directive for emitting BSS Symbols.  Default is false.
     83   bool HasMachoZeroFillDirective;
     84 
     85   /// True if this is a MachO target that supports the macho-specific .tbss
     86   /// directive for emitting thread local BSS Symbols.  Default is false.
     87   bool HasMachoTBSSDirective;
     88 
     89   /// True if the compiler should emit a ".reference .constructors_used" or
     90   /// ".reference .destructors_used" directive after the static ctor/dtor
     91   /// list.  This directive is only emitted in Static relocation model.  Default
     92   /// is false.
     93   bool HasStaticCtorDtorReferenceInStaticMode;
     94 
     95   /// This is the maximum possible length of an instruction, which is needed to
     96   /// compute the size of an inline asm.  Defaults to 4.
     97   unsigned MaxInstLength;
     98 
     99   /// Every possible instruction length is a multiple of this value.  Factored
    100   /// out in .debug_frame and .debug_line.  Defaults to 1.
    101   unsigned MinInstAlignment;
    102 
    103   /// The '$' token, when not referencing an identifier or constant, refers to
    104   /// the current PC.  Defaults to false.
    105   bool DollarIsPC;
    106 
    107   /// This string, if specified, is used to separate instructions from each
    108   /// other when on the same line.  Defaults to ';'
    109   const char *SeparatorString;
    110 
    111   /// This indicates the comment character used by the assembler.  Defaults to
    112   /// "#"
    113   const char *CommentString;
    114 
    115   /// This is appended to emitted labels.  Defaults to ":"
    116   const char *LabelSuffix;
    117 
    118   // Print the EH begin symbol with an assignment. Defaults to false.
    119   bool UseAssignmentForEHBegin;
    120 
    121   // Do we need to create a local symbol for .size?
    122   bool NeedsLocalForSize;
    123 
    124   /// This prefix is used for globals like constant pool entries that are
    125   /// completely private to the .s file and should not have names in the .o
    126   /// file.  Defaults to "L"
    127   const char *PrivateGlobalPrefix;
    128 
    129   /// This prefix is used for labels for basic blocks. Defaults to the same as
    130   /// PrivateGlobalPrefix.
    131   const char *PrivateLabelPrefix;
    132 
    133   /// This prefix is used for symbols that should be passed through the
    134   /// assembler but be removed by the linker.  This is 'l' on Darwin, currently
    135   /// used for some ObjC metadata.  The default of "" meast that for this system
    136   /// a plain private symbol should be used.  Defaults to "".
    137   const char *LinkerPrivateGlobalPrefix;
    138 
    139   /// If these are nonempty, they contain a directive to emit before and after
    140   /// an inline assembly statement.  Defaults to "#APP\n", "#NO_APP\n"
    141   const char *InlineAsmStart;
    142   const char *InlineAsmEnd;
    143 
    144   /// These are assembly directives that tells the assembler to interpret the
    145   /// following instructions differently.  Defaults to ".code16", ".code32",
    146   /// ".code64".
    147   const char *Code16Directive;
    148   const char *Code32Directive;
    149   const char *Code64Directive;
    150 
    151   /// Which dialect of an assembler variant to use.  Defaults to 0
    152   unsigned AssemblerDialect;
    153 
    154   /// This is true if the assembler allows @ characters in symbol names.
    155   /// Defaults to false.
    156   bool AllowAtInName;
    157 
    158   /// This is true if data region markers should be printed as
    159   /// ".data_region/.end_data_region" directives. If false, use "$d/$a" labels
    160   /// instead.
    161   bool UseDataRegionDirectives;
    162 
    163   //===--- Data Emission Directives -------------------------------------===//
    164 
    165   /// This should be set to the directive used to get some number of zero bytes
    166   /// emitted to the current section.  Common cases are "\t.zero\t" and
    167   /// "\t.space\t".  If this is set to null, the Data*bitsDirective's will be
    168   /// used to emit zero bytes.  Defaults to "\t.zero\t"
    169   const char *ZeroDirective;
    170 
    171   /// This directive allows emission of an ascii string with the standard C
    172   /// escape characters embedded into it.  Defaults to "\t.ascii\t"
    173   const char *AsciiDirective;
    174 
    175   /// If not null, this allows for special handling of zero terminated strings
    176   /// on this target.  This is commonly supported as ".asciz".  If a target
    177   /// doesn't support this, it can be set to null.  Defaults to "\t.asciz\t"
    178   const char *AscizDirective;
    179 
    180   /// These directives are used to output some unit of integer data to the
    181   /// current section.  If a data directive is set to null, smaller data
    182   /// directives will be used to emit the large sizes.  Defaults to "\t.byte\t",
    183   /// "\t.short\t", "\t.long\t", "\t.quad\t"
    184   const char *Data8bitsDirective;
    185   const char *Data16bitsDirective;
    186   const char *Data32bitsDirective;
    187   const char *Data64bitsDirective;
    188 
    189   /// If non-null, a directive that is used to emit a word which should be
    190   /// relocated as a 64-bit GP-relative offset, e.g. .gpdword on Mips.  Defaults
    191   /// to NULL.
    192   const char *GPRel64Directive;
    193 
    194   /// If non-null, a directive that is used to emit a word which should be
    195   /// relocated as a 32-bit GP-relative offset, e.g. .gpword on Mips or .gprel32
    196   /// on Alpha.  Defaults to NULL.
    197   const char *GPRel32Directive;
    198 
    199   /// This is true if this target uses "Sun Style" syntax for section switching
    200   /// ("#alloc,#write" etc) instead of the normal ELF syntax (,"a,w") in
    201   /// .section directives.  Defaults to false.
    202   bool SunStyleELFSectionSwitchSyntax;
    203 
    204   /// This is true if this target uses ELF '.section' directive before the
    205   /// '.bss' one. It's used for PPC/Linux which doesn't support the '.bss'
    206   /// directive only.  Defaults to false.
    207   bool UsesELFSectionDirectiveForBSS;
    208 
    209   bool NeedsDwarfSectionOffsetDirective;
    210 
    211   //===--- Alignment Information ----------------------------------------===//
    212 
    213   /// If this is true (the default) then the asmprinter emits ".align N"
    214   /// directives, where N is the number of bytes to align to.  Otherwise, it
    215   /// emits ".align log2(N)", e.g. 3 to align to an 8 byte boundary.  Defaults
    216   /// to true.
    217   bool AlignmentIsInBytes;
    218 
    219   /// If non-zero, this is used to fill the executable space created as the
    220   /// result of a alignment directive.  Defaults to 0
    221   unsigned TextAlignFillValue;
    222 
    223   //===--- Global Variable Emission Directives --------------------------===//
    224 
    225   /// This is the directive used to declare a global entity. Defaults to
    226   /// ".globl".
    227   const char *GlobalDirective;
    228 
    229   /// True if the expression
    230   ///   .long f - g
    231   /// uses an relocation but it can be supressed by writting
    232   ///   a = f - g
    233   ///   .long a
    234   bool SetDirectiveSuppressesReloc;
    235 
    236   /// False if the assembler requires that we use
    237   /// \code
    238   ///   Lc = a - b
    239   ///   .long Lc
    240   /// \endcode
    241   //
    242   /// instead of
    243   //
    244   /// \code
    245   ///   .long a - b
    246   /// \endcode
    247   ///
    248   ///  Defaults to true.
    249   bool HasAggressiveSymbolFolding;
    250 
    251   /// True is .comm's and .lcomms optional alignment is to be specified in bytes
    252   /// instead of log2(n).  Defaults to true.
    253   bool COMMDirectiveAlignmentIsInBytes;
    254 
    255   /// Describes if the .lcomm directive for the target supports an alignment
    256   /// argument and how it is interpreted.  Defaults to NoAlignment.
    257   LCOMM::LCOMMType LCOMMDirectiveAlignmentType;
    258 
    259   // True if the target allows .align directives on funtions. This is true for
    260   // most targets, so defaults to true.
    261   bool HasFunctionAlignment;
    262 
    263   /// True if the target has .type and .size directives, this is true for most
    264   /// ELF targets.  Defaults to true.
    265   bool HasDotTypeDotSizeDirective;
    266 
    267   /// True if the target has a single parameter .file directive, this is true
    268   /// for ELF targets.  Defaults to true.
    269   bool HasSingleParameterDotFile;
    270 
    271   /// True if the target has a .ident directive, this is true for ELF targets.
    272   /// Defaults to false.
    273   bool HasIdentDirective;
    274 
    275   /// True if this target supports the MachO .no_dead_strip directive.  Defaults
    276   /// to false.
    277   bool HasNoDeadStrip;
    278 
    279   /// Used to declare a global as being a weak symbol. Defaults to ".weak".
    280   const char *WeakDirective;
    281 
    282   /// This directive, if non-null, is used to declare a global as being a weak
    283   /// undefined symbol.  Defaults to NULL.
    284   const char *WeakRefDirective;
    285 
    286   /// True if we have a directive to declare a global as being a weak defined
    287   /// symbol.  Defaults to false.
    288   bool HasWeakDefDirective;
    289 
    290   /// True if we have a directive to declare a global as being a weak defined
    291   /// symbol that can be hidden (unexported).  Defaults to false.
    292   bool HasWeakDefCanBeHiddenDirective;
    293 
    294   /// True if we have a .linkonce directive.  This is used on cygwin/mingw.
    295   /// Defaults to false.
    296   bool HasLinkOnceDirective;
    297 
    298   /// This attribute, if not MCSA_Invalid, is used to declare a symbol as having
    299   /// hidden visibility.  Defaults to MCSA_Hidden.
    300   MCSymbolAttr HiddenVisibilityAttr;
    301 
    302   /// This attribute, if not MCSA_Invalid, is used to declare an undefined
    303   /// symbol as having hidden visibility. Defaults to MCSA_Hidden.
    304   MCSymbolAttr HiddenDeclarationVisibilityAttr;
    305 
    306   /// This attribute, if not MCSA_Invalid, is used to declare a symbol as having
    307   /// protected visibility.  Defaults to MCSA_Protected
    308   MCSymbolAttr ProtectedVisibilityAttr;
    309 
    310   //===--- Dwarf Emission Directives -----------------------------------===//
    311 
    312   /// True if target supports emission of debugging information.  Defaults to
    313   /// false.
    314   bool SupportsDebugInformation;
    315 
    316   /// Exception handling format for the target.  Defaults to None.
    317   ExceptionHandling ExceptionsType;
    318 
    319   /// Windows exception handling data (.pdata) encoding.  Defaults to Invalid.
    320   WinEH::EncodingType WinEHEncodingType;
    321 
    322   /// True if Dwarf2 output generally uses relocations for references to other
    323   /// .debug_* sections.
    324   bool DwarfUsesRelocationsAcrossSections;
    325 
    326   /// True if DWARF FDE symbol reference relocations should be replaced by an
    327   /// absolute difference.
    328   bool DwarfFDESymbolsUseAbsDiff;
    329 
    330   /// True if dwarf register numbers are printed instead of symbolic register
    331   /// names in .cfi_* directives.  Defaults to false.
    332   bool DwarfRegNumForCFI;
    333 
    334   /// True if target uses parens to indicate the symbol variant instead of @.
    335   /// For example, foo(plt) instead of foo@plt.  Defaults to false.
    336   bool UseParensForSymbolVariant;
    337 
    338   //===--- Prologue State ----------------------------------------------===//
    339 
    340   std::vector<MCCFIInstruction> InitialFrameState;
    341 
    342   //===--- Integrated Assembler State ----------------------------------===//
    343 
    344   /// Should we use the integrated assembler?
    345   /// The integrated assembler should be enabled by default (by the
    346   /// constructors) when failing to parse a valid piece of assembly (inline
    347   /// or otherwise) is considered a bug. It may then be overridden after
    348   /// construction (see LLVMTargetMachine::initAsmInfo()).
    349   bool UseIntegratedAssembler;
    350 
    351   /// Compress DWARF debug sections. Defaults to false.
    352   bool CompressDebugSections;
    353 
    354 public:
    355   explicit MCAsmInfo();
    356   virtual ~MCAsmInfo();
    357 
    358   /// Get the pointer size in bytes.
    359   unsigned getPointerSize() const { return PointerSize; }
    360 
    361   /// Get the callee-saved register stack slot
    362   /// size in bytes.
    363   unsigned getCalleeSaveStackSlotSize() const {
    364     return CalleeSaveStackSlotSize;
    365   }
    366 
    367   /// True if the target is little endian.
    368   bool isLittleEndian() const { return IsLittleEndian; }
    369 
    370   /// True if target stack grow up.
    371   bool isStackGrowthDirectionUp() const { return StackGrowsUp; }
    372 
    373   bool hasSubsectionsViaSymbols() const { return HasSubsectionsViaSymbols; }
    374 
    375   // Data directive accessors.
    376 
    377   const char *getData8bitsDirective() const { return Data8bitsDirective; }
    378   const char *getData16bitsDirective() const { return Data16bitsDirective; }
    379   const char *getData32bitsDirective() const { return Data32bitsDirective; }
    380   const char *getData64bitsDirective() const { return Data64bitsDirective; }
    381   const char *getGPRel64Directive() const { return GPRel64Directive; }
    382   const char *getGPRel32Directive() const { return GPRel32Directive; }
    383 
    384   /// Targets can implement this method to specify a section to switch to if the
    385   /// translation unit doesn't have any trampolines that require an executable
    386   /// stack.
    387   virtual const MCSection *getNonexecutableStackSection(MCContext &Ctx) const {
    388     return nullptr;
    389   }
    390 
    391   /// \brief True if the section is atomized using the symbols in it.
    392   /// This is false if the section is not atomized at all (most ELF sections) or
    393   /// if it is atomized based on its contents (MachO' __TEXT,__cstring for
    394   /// example).
    395   virtual bool isSectionAtomizableBySymbols(const MCSection &Section) const;
    396 
    397   virtual const MCExpr *getExprForPersonalitySymbol(const MCSymbol *Sym,
    398                                                     unsigned Encoding,
    399                                                     MCStreamer &Streamer) const;
    400 
    401   virtual const MCExpr *getExprForFDESymbol(const MCSymbol *Sym,
    402                                             unsigned Encoding,
    403                                             MCStreamer &Streamer) const;
    404 
    405   bool usesSunStyleELFSectionSwitchSyntax() const {
    406     return SunStyleELFSectionSwitchSyntax;
    407   }
    408 
    409   bool usesELFSectionDirectiveForBSS() const {
    410     return UsesELFSectionDirectiveForBSS;
    411   }
    412 
    413   bool needsDwarfSectionOffsetDirective() const {
    414     return NeedsDwarfSectionOffsetDirective;
    415   }
    416 
    417   // Accessors.
    418 
    419   bool hasMachoZeroFillDirective() const { return HasMachoZeroFillDirective; }
    420   bool hasMachoTBSSDirective() const { return HasMachoTBSSDirective; }
    421   bool hasStaticCtorDtorReferenceInStaticMode() const {
    422     return HasStaticCtorDtorReferenceInStaticMode;
    423   }
    424   unsigned getMaxInstLength() const { return MaxInstLength; }
    425   unsigned getMinInstAlignment() const { return MinInstAlignment; }
    426   bool getDollarIsPC() const { return DollarIsPC; }
    427   const char *getSeparatorString() const { return SeparatorString; }
    428 
    429   /// This indicates the column (zero-based) at which asm comments should be
    430   /// printed.
    431   unsigned getCommentColumn() const { return 40; }
    432 
    433   const char *getCommentString() const { return CommentString; }
    434   const char *getLabelSuffix() const { return LabelSuffix; }
    435 
    436   bool useAssignmentForEHBegin() const { return UseAssignmentForEHBegin; }
    437   bool needsLocalForSize() const { return NeedsLocalForSize; }
    438   const char *getPrivateGlobalPrefix() const { return PrivateGlobalPrefix; }
    439   const char *getPrivateLabelPrefix() const { return PrivateLabelPrefix; }
    440   bool hasLinkerPrivateGlobalPrefix() const {
    441     return LinkerPrivateGlobalPrefix[0] != '\0';
    442   }
    443   const char *getLinkerPrivateGlobalPrefix() const {
    444     if (hasLinkerPrivateGlobalPrefix())
    445       return LinkerPrivateGlobalPrefix;
    446     return getPrivateGlobalPrefix();
    447   }
    448   const char *getInlineAsmStart() const { return InlineAsmStart; }
    449   const char *getInlineAsmEnd() const { return InlineAsmEnd; }
    450   const char *getCode16Directive() const { return Code16Directive; }
    451   const char *getCode32Directive() const { return Code32Directive; }
    452   const char *getCode64Directive() const { return Code64Directive; }
    453   unsigned getAssemblerDialect() const { return AssemblerDialect; }
    454   bool doesAllowAtInName() const { return AllowAtInName; }
    455   bool doesSupportDataRegionDirectives() const {
    456     return UseDataRegionDirectives;
    457   }
    458   const char *getZeroDirective() const { return ZeroDirective; }
    459   const char *getAsciiDirective() const { return AsciiDirective; }
    460   const char *getAscizDirective() const { return AscizDirective; }
    461   bool getAlignmentIsInBytes() const { return AlignmentIsInBytes; }
    462   unsigned getTextAlignFillValue() const { return TextAlignFillValue; }
    463   const char *getGlobalDirective() const { return GlobalDirective; }
    464   bool doesSetDirectiveSuppressesReloc() const {
    465     return SetDirectiveSuppressesReloc;
    466   }
    467   bool hasAggressiveSymbolFolding() const { return HasAggressiveSymbolFolding; }
    468   bool getCOMMDirectiveAlignmentIsInBytes() const {
    469     return COMMDirectiveAlignmentIsInBytes;
    470   }
    471   LCOMM::LCOMMType getLCOMMDirectiveAlignmentType() const {
    472     return LCOMMDirectiveAlignmentType;
    473   }
    474   bool hasFunctionAlignment() const { return HasFunctionAlignment; }
    475   bool hasDotTypeDotSizeDirective() const { return HasDotTypeDotSizeDirective; }
    476   bool hasSingleParameterDotFile() const { return HasSingleParameterDotFile; }
    477   bool hasIdentDirective() const { return HasIdentDirective; }
    478   bool hasNoDeadStrip() const { return HasNoDeadStrip; }
    479   const char *getWeakDirective() const { return WeakDirective; }
    480   const char *getWeakRefDirective() const { return WeakRefDirective; }
    481   bool hasWeakDefDirective() const { return HasWeakDefDirective; }
    482   bool hasWeakDefCanBeHiddenDirective() const {
    483     return HasWeakDefCanBeHiddenDirective;
    484   }
    485   bool hasLinkOnceDirective() const { return HasLinkOnceDirective; }
    486 
    487   MCSymbolAttr getHiddenVisibilityAttr() const { return HiddenVisibilityAttr; }
    488   MCSymbolAttr getHiddenDeclarationVisibilityAttr() const {
    489     return HiddenDeclarationVisibilityAttr;
    490   }
    491   MCSymbolAttr getProtectedVisibilityAttr() const {
    492     return ProtectedVisibilityAttr;
    493   }
    494   bool doesSupportDebugInformation() const { return SupportsDebugInformation; }
    495   bool doesSupportExceptionHandling() const {
    496     return ExceptionsType != ExceptionHandling::None;
    497   }
    498   ExceptionHandling getExceptionHandlingType() const { return ExceptionsType; }
    499   WinEH::EncodingType getWinEHEncodingType() const { return WinEHEncodingType; }
    500 
    501   /// Returns true if the exception handling method for the platform uses call
    502   /// frame information to unwind.
    503   bool usesCFIForEH() const {
    504     return (ExceptionsType == ExceptionHandling::DwarfCFI ||
    505             ExceptionsType == ExceptionHandling::ARM ||
    506             ExceptionsType == ExceptionHandling::WinEH);
    507   }
    508 
    509   bool usesWindowsCFI() const {
    510     return ExceptionsType == ExceptionHandling::WinEH;
    511   }
    512 
    513   bool doesDwarfUseRelocationsAcrossSections() const {
    514     return DwarfUsesRelocationsAcrossSections;
    515   }
    516   bool doDwarfFDESymbolsUseAbsDiff() const { return DwarfFDESymbolsUseAbsDiff; }
    517   bool useDwarfRegNumForCFI() const { return DwarfRegNumForCFI; }
    518   bool useParensForSymbolVariant() const { return UseParensForSymbolVariant; }
    519 
    520   void addInitialFrameState(const MCCFIInstruction &Inst) {
    521     InitialFrameState.push_back(Inst);
    522   }
    523 
    524   const std::vector<MCCFIInstruction> &getInitialFrameState() const {
    525     return InitialFrameState;
    526   }
    527 
    528   /// Return true if assembly (inline or otherwise) should be parsed.
    529   bool useIntegratedAssembler() const { return UseIntegratedAssembler; }
    530 
    531   /// Set whether assembly (inline or otherwise) should be parsed.
    532   virtual void setUseIntegratedAssembler(bool Value) {
    533     UseIntegratedAssembler = Value;
    534   }
    535 
    536   bool compressDebugSections() const { return CompressDebugSections; }
    537 
    538   void setCompressDebugSections(bool CompressDebugSections) {
    539     this->CompressDebugSections = CompressDebugSections;
    540   }
    541 };
    542 }
    543 
    544 #endif
    545