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   ET_Invalid, /// Invalid
00035   ET_Alpha,   /// Windows Alpha
00036   ET_Alpha64, /// Windows AXP64
00037   ET_ARM,     /// Windows NT (Windows on ARM)
00038   ET_CE,      /// Windows CE ARM, PowerPC, SH3, SH4
00039   ET_Itanium, /// Windows x64, Windows Itanium (IA-64)
00040   ET_MIPS = ET_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 a static ctor/dtor
00091   /// list.  This directive is only emitted in Static relocation model.  Default
00092   /// is false.
00093   bool HasStaticCtorDtorReferenceInStaticMode;
00094 
00095   /// True if the linker has a bug and requires that the debug_line section be
00096   /// of a minimum size. In practice such a linker requires a non-empty line
00097   /// sequence if a file is present.  Default to false.
00098   bool LinkerRequiresNonEmptyDwarfLines;
00099 
00100   /// This is the maximum possible length of an instruction, which is needed to
00101   /// compute the size of an inline asm.  Defaults to 4.
00102   unsigned MaxInstLength;
00103 
00104   /// Every possible instruction length is a multiple of this value.  Factored
00105   /// out in .debug_frame and .debug_line.  Defaults to 1.
00106   unsigned MinInstAlignment;
00107 
00108   /// The '$' token, when not referencing an identifier or constant, refers to
00109   /// the current PC.  Defaults to false.
00110   bool DollarIsPC;
00111 
00112   /// This string, if specified, is used to separate instructions from each
00113   /// other when on the same line.  Defaults to ';'
00114   const char *SeparatorString;
00115 
00116   /// This indicates the comment character used by the assembler.  Defaults to
00117   /// "#"
00118   const char *CommentString;
00119 
00120   /// This is appended to emitted labels.  Defaults to ":"
00121   const char *LabelSuffix;
00122 
00123   // Print the EH begin symbol with an assignment. Defaults to false.
00124   bool UseAssignmentForEHBegin;
00125 
00126   /// This prefix is used for globals like constant pool entries that are
00127   /// completely private to the .s file and should not have names in the .o
00128   /// file.  Defaults to "L"
00129   const char *PrivateGlobalPrefix;
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 NULL.
00224   const char *GlobalDirective;
00225 
00226   /// True if the assembler supports the .set directive.  Defaults to true.
00227   bool HasSetDirective;
00228 
00229   /// False if the assembler requires that we use
00230   /// \code
00231   ///   Lc = a - b
00232   ///   .long Lc
00233   /// \endcode
00234   //
00235   /// instead of
00236   //
00237   /// \code
00238   ///   .long a - b
00239   /// \endcode
00240   ///
00241   ///  Defaults to true.
00242   bool HasAggressiveSymbolFolding;
00243 
00244   /// True is .comm's and .lcomms optional alignment is to be specified in bytes
00245   /// instead of log2(n).  Defaults to true.
00246   bool COMMDirectiveAlignmentIsInBytes;
00247 
00248   /// Describes if the .lcomm directive for the target supports an alignment
00249   /// argument and how it is interpreted.  Defaults to NoAlignment.
00250   LCOMM::LCOMMType LCOMMDirectiveAlignmentType;
00251 
00252   /// True if the target has .type and .size directives, this is true for most
00253   /// ELF targets.  Defaults to true.
00254   bool HasDotTypeDotSizeDirective;
00255 
00256   /// True if the target has a single parameter .file directive, this is true
00257   /// for ELF targets.  Defaults to true.
00258   bool HasSingleParameterDotFile;
00259 
00260   /// True if the target has a .ident directive, this is true for ELF targets.
00261   /// Defaults to false.
00262   bool HasIdentDirective;
00263 
00264   /// True if this target supports the MachO .no_dead_strip directive.  Defaults
00265   /// to false.
00266   bool HasNoDeadStrip;
00267 
00268   /// This directive, if non-null, is used to declare a global as being a weak
00269   /// undefined symbol.  Defaults to NULL.
00270   const char *WeakRefDirective;
00271 
00272   /// True if we have a directive to declare a global as being a weak defined
00273   /// symbol.  Defaults to false.
00274   bool HasWeakDefDirective;
00275 
00276   /// True if we have a directive to declare a global as being a weak defined
00277   /// symbol that can be hidden (unexported).  Defaults to false.
00278   bool HasWeakDefCanBeHiddenDirective;
00279 
00280   /// True if we have a .linkonce directive.  This is used on cygwin/mingw.
00281   /// Defaults to false.
00282   bool HasLinkOnceDirective;
00283 
00284   /// This attribute, if not MCSA_Invalid, is used to declare a symbol as having
00285   /// hidden visibility.  Defaults to MCSA_Hidden.
00286   MCSymbolAttr HiddenVisibilityAttr;
00287 
00288   /// This attribute, if not MCSA_Invalid, is used to declare an undefined
00289   /// symbol as having hidden visibility. Defaults to MCSA_Hidden.
00290   MCSymbolAttr HiddenDeclarationVisibilityAttr;
00291 
00292   /// This attribute, if not MCSA_Invalid, is used to declare a symbol as having
00293   /// protected visibility.  Defaults to MCSA_Protected
00294   MCSymbolAttr ProtectedVisibilityAttr;
00295 
00296   //===--- Dwarf Emission Directives -----------------------------------===//
00297 
00298   /// True if target asm supports leb128 directives.  Defaults to false.
00299   bool HasLEB128;
00300 
00301   /// True if target supports emission of debugging information.  Defaults to
00302   /// false.
00303   bool SupportsDebugInformation;
00304 
00305   /// Exception handling format for the target.  Defaults to None.
00306   ExceptionHandling ExceptionsType;
00307 
00308   /// Windows exception handling data (.pdata) encoding.  Defaults to Invalid.
00309   WinEH::EncodingType WinEHEncodingType;
00310 
00311   /// True if Dwarf2 output generally uses relocations for references to other
00312   /// .debug_* sections.
00313   bool DwarfUsesRelocationsAcrossSections;
00314 
00315   /// True if DWARF FDE symbol reference relocations should be replaced by an
00316   /// absolute difference.
00317   bool DwarfFDESymbolsUseAbsDiff;
00318 
00319   /// True if dwarf register numbers are printed instead of symbolic register
00320   /// names in .cfi_* directives.  Defaults to false.
00321   bool DwarfRegNumForCFI;
00322 
00323   /// True if target uses parens to indicate the symbol variant instead of @.
00324   /// For example, foo(plt) instead of foo@plt.  Defaults to false.
00325   bool UseParensForSymbolVariant;
00326 
00327   //===--- Prologue State ----------------------------------------------===//
00328 
00329   std::vector<MCCFIInstruction> InitialFrameState;
00330 
00331   //===--- Integrated Assembler State ----------------------------------===//
00332 
00333   /// Should we use the integrated assembler?
00334   /// The integrated assembler should be enabled by default (by the
00335   /// constructors) when failing to parse a valid piece of assembly (inline
00336   /// or otherwise) is considered a bug. It may then be overridden after
00337   /// construction (see LLVMTargetMachine::initAsmInfo()).
00338   bool UseIntegratedAssembler;
00339 
00340   /// Compress DWARF debug sections. Defaults to false.
00341   bool CompressDebugSections;
00342 
00343 public:
00344   explicit MCAsmInfo();
00345   virtual ~MCAsmInfo();
00346 
00347   /// Get the pointer size in bytes.
00348   unsigned getPointerSize() const { return PointerSize; }
00349 
00350   /// Get the callee-saved register stack slot
00351   /// size in bytes.
00352   unsigned getCalleeSaveStackSlotSize() const {
00353     return CalleeSaveStackSlotSize;
00354   }
00355 
00356   /// True if the target is little endian.
00357   bool isLittleEndian() const { return IsLittleEndian; }
00358 
00359   /// True if target stack grow up.
00360   bool isStackGrowthDirectionUp() const { return StackGrowsUp; }
00361 
00362   bool hasSubsectionsViaSymbols() const { return HasSubsectionsViaSymbols; }
00363 
00364   // Data directive accessors.
00365 
00366   const char *getData8bitsDirective() const { return Data8bitsDirective; }
00367   const char *getData16bitsDirective() const { return Data16bitsDirective; }
00368   const char *getData32bitsDirective() const { return Data32bitsDirective; }
00369   const char *getData64bitsDirective() const { return Data64bitsDirective; }
00370   const char *getGPRel64Directive() const { return GPRel64Directive; }
00371   const char *getGPRel32Directive() const { return GPRel32Directive; }
00372 
00373   /// Targets can implement this method to specify a section to switch to if the
00374   /// translation unit doesn't have any trampolines that require an executable
00375   /// stack.
00376   virtual const MCSection *getNonexecutableStackSection(MCContext &Ctx) const {
00377     return nullptr;
00378   }
00379 
00380   virtual const MCExpr *getExprForPersonalitySymbol(const MCSymbol *Sym,
00381                                                     unsigned Encoding,
00382                                                     MCStreamer &Streamer) const;
00383 
00384   virtual const MCExpr *getExprForFDESymbol(const MCSymbol *Sym,
00385                                             unsigned Encoding,
00386                                             MCStreamer &Streamer) const;
00387 
00388   bool usesSunStyleELFSectionSwitchSyntax() const {
00389     return SunStyleELFSectionSwitchSyntax;
00390   }
00391 
00392   bool usesELFSectionDirectiveForBSS() const {
00393     return UsesELFSectionDirectiveForBSS;
00394   }
00395 
00396   bool needsDwarfSectionOffsetDirective() const {
00397     return NeedsDwarfSectionOffsetDirective;
00398   }
00399 
00400   // Accessors.
00401 
00402   bool hasMachoZeroFillDirective() const { return HasMachoZeroFillDirective; }
00403   bool hasMachoTBSSDirective() const { return HasMachoTBSSDirective; }
00404   bool hasStaticCtorDtorReferenceInStaticMode() const {
00405     return HasStaticCtorDtorReferenceInStaticMode;
00406   }
00407   bool getLinkerRequiresNonEmptyDwarfLines() const {
00408     return LinkerRequiresNonEmptyDwarfLines;
00409   }
00410   unsigned getMaxInstLength() const { return MaxInstLength; }
00411   unsigned getMinInstAlignment() const { return MinInstAlignment; }
00412   bool getDollarIsPC() const { return DollarIsPC; }
00413   const char *getSeparatorString() const { return SeparatorString; }
00414 
00415   /// This indicates the column (zero-based) at which asm comments should be
00416   /// printed.
00417   unsigned getCommentColumn() const { return 40; }
00418 
00419   const char *getCommentString() const { return CommentString; }
00420   const char *getLabelSuffix() const { return LabelSuffix; }
00421 
00422   bool useAssignmentForEHBegin() const { return UseAssignmentForEHBegin; }
00423   const char *getPrivateGlobalPrefix() const { return PrivateGlobalPrefix; }
00424   bool hasLinkerPrivateGlobalPrefix() const {
00425     return LinkerPrivateGlobalPrefix[0] != '\0';
00426   }
00427   const char *getLinkerPrivateGlobalPrefix() const {
00428     if (hasLinkerPrivateGlobalPrefix())
00429       return LinkerPrivateGlobalPrefix;
00430     return getPrivateGlobalPrefix();
00431   }
00432   const char *getInlineAsmStart() const { return InlineAsmStart; }
00433   const char *getInlineAsmEnd() const { return InlineAsmEnd; }
00434   const char *getCode16Directive() const { return Code16Directive; }
00435   const char *getCode32Directive() const { return Code32Directive; }
00436   const char *getCode64Directive() const { return Code64Directive; }
00437   unsigned getAssemblerDialect() const { return AssemblerDialect; }
00438   bool doesAllowAtInName() const { return AllowAtInName; }
00439   bool doesSupportDataRegionDirectives() const {
00440     return UseDataRegionDirectives;
00441   }
00442   const char *getZeroDirective() const { return ZeroDirective; }
00443   const char *getAsciiDirective() const { return AsciiDirective; }
00444   const char *getAscizDirective() const { return AscizDirective; }
00445   bool getAlignmentIsInBytes() const { return AlignmentIsInBytes; }
00446   unsigned getTextAlignFillValue() const { return TextAlignFillValue; }
00447   const char *getGlobalDirective() const { return GlobalDirective; }
00448   bool hasSetDirective() const { return HasSetDirective; }
00449   bool hasAggressiveSymbolFolding() const { return HasAggressiveSymbolFolding; }
00450   bool getCOMMDirectiveAlignmentIsInBytes() const {
00451     return COMMDirectiveAlignmentIsInBytes;
00452   }
00453   LCOMM::LCOMMType getLCOMMDirectiveAlignmentType() const {
00454     return LCOMMDirectiveAlignmentType;
00455   }
00456   bool hasDotTypeDotSizeDirective() const { return HasDotTypeDotSizeDirective; }
00457   bool hasSingleParameterDotFile() const { return HasSingleParameterDotFile; }
00458   bool hasIdentDirective() const { return HasIdentDirective; }
00459   bool hasNoDeadStrip() const { return HasNoDeadStrip; }
00460   const char *getWeakRefDirective() const { return WeakRefDirective; }
00461   bool hasWeakDefDirective() const { return HasWeakDefDirective; }
00462   bool hasWeakDefCanBeHiddenDirective() const {
00463     return HasWeakDefCanBeHiddenDirective;
00464   }
00465   bool hasLinkOnceDirective() const { return HasLinkOnceDirective; }
00466 
00467   MCSymbolAttr getHiddenVisibilityAttr() const { return HiddenVisibilityAttr; }
00468   MCSymbolAttr getHiddenDeclarationVisibilityAttr() const {
00469     return HiddenDeclarationVisibilityAttr;
00470   }
00471   MCSymbolAttr getProtectedVisibilityAttr() const {
00472     return ProtectedVisibilityAttr;
00473   }
00474   bool hasLEB128() const { return HasLEB128; }
00475   bool doesSupportDebugInformation() const { return SupportsDebugInformation; }
00476   bool doesSupportExceptionHandling() const {
00477     return ExceptionsType != ExceptionHandling::None;
00478   }
00479   ExceptionHandling getExceptionHandlingType() const { return ExceptionsType; }
00480   WinEH::EncodingType getWinEHEncodingType() const { return WinEHEncodingType; }
00481   bool isExceptionHandlingDwarf() const {
00482     return (ExceptionsType == ExceptionHandling::DwarfCFI ||
00483             ExceptionsType == ExceptionHandling::ARM ||
00484             // Windows handler data still uses DWARF LSDA encoding.
00485             ExceptionsType == ExceptionHandling::WinEH);
00486   }
00487   bool doesDwarfUseRelocationsAcrossSections() const {
00488     return DwarfUsesRelocationsAcrossSections;
00489   }
00490   bool doDwarfFDESymbolsUseAbsDiff() const { return DwarfFDESymbolsUseAbsDiff; }
00491   bool useDwarfRegNumForCFI() const { return DwarfRegNumForCFI; }
00492   bool useParensForSymbolVariant() const { return UseParensForSymbolVariant; }
00493 
00494   void addInitialFrameState(const MCCFIInstruction &Inst) {
00495     InitialFrameState.push_back(Inst);
00496   }
00497 
00498   const std::vector<MCCFIInstruction> &getInitialFrameState() const {
00499     return InitialFrameState;
00500   }
00501 
00502   /// Return true if assembly (inline or otherwise) should be parsed.
00503   bool useIntegratedAssembler() const { return UseIntegratedAssembler; }
00504 
00505   /// Set whether assembly (inline or otherwise) should be parsed.
00506   virtual void setUseIntegratedAssembler(bool Value) {
00507     UseIntegratedAssembler = Value;
00508   }
00509 
00510   bool compressDebugSections() const { return CompressDebugSections; }
00511 
00512   void setCompressDebugSections(bool CompressDebugSections) {
00513     this->CompressDebugSections = CompressDebugSections;
00514   }
00515 };
00516 }
00517 
00518 #endif