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