1 //===-- llvm/MC/MCAsmInfo.h - Asm info --------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_MC_MCASMINFO_H
17 #define LLVM_MC_MCASMINFO_H
19 #include "llvm/MC/MCDirectives.h"
20 #include "llvm/MC/MCDwarf.h"
21 #include "llvm/MC/MCTargetOptions.h"
33 enum class EncodingType {
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 X86, /// Windows x86, uses no CFI, just EH tables
46 enum LCOMMType { NoAlignment, ByteAlignment, Log2Alignment };
49 enum class DebugCompressionType {
50 DCT_None, // no compression
51 DCT_Zlib, // zlib style complession
52 DCT_ZlibGnu // zlib-gnu style compression
55 /// This class is intended to be used as a base class for asm
56 /// properties and features specific to the target.
59 //===------------------------------------------------------------------===//
60 // Properties to be set by the target writer, used to configure asm printer.
63 /// Pointer size in bytes. Default is 4.
66 /// Size of the stack slot reserved for callee-saved registers, in bytes.
67 /// Default is same as pointer size.
68 unsigned CalleeSaveStackSlotSize;
70 /// True if target is little endian. Default is true.
73 /// True if target stack grow up. Default is false.
76 /// True if this target has the MachO .subsections_via_symbols directive.
78 bool HasSubsectionsViaSymbols;
80 /// True if this is a MachO target that supports the macho-specific .zerofill
81 /// directive for emitting BSS Symbols. Default is false.
82 bool HasMachoZeroFillDirective;
84 /// True if this is a MachO target that supports the macho-specific .tbss
85 /// directive for emitting thread local BSS Symbols. Default is false.
86 bool HasMachoTBSSDirective;
88 /// This is the maximum possible length of an instruction, which is needed to
89 /// compute the size of an inline asm. Defaults to 4.
90 unsigned MaxInstLength;
92 /// Every possible instruction length is a multiple of this value. Factored
93 /// out in .debug_frame and .debug_line. Defaults to 1.
94 unsigned MinInstAlignment;
96 /// The '$' token, when not referencing an identifier or constant, refers to
97 /// the current PC. Defaults to false.
100 /// This string, if specified, is used to separate instructions from each
101 /// other when on the same line. Defaults to ';'
102 const char *SeparatorString;
104 /// This indicates the comment character used by the assembler. Defaults to
106 StringRef CommentString;
108 /// This is appended to emitted labels. Defaults to ":"
109 const char *LabelSuffix;
111 // Print the EH begin symbol with an assignment. Defaults to false.
112 bool UseAssignmentForEHBegin;
114 // Do we need to create a local symbol for .size?
115 bool NeedsLocalForSize;
117 /// This prefix is used for globals like constant pool entries that are
118 /// completely private to the .s file and should not have names in the .o
119 /// file. Defaults to "L"
120 StringRef PrivateGlobalPrefix;
122 /// This prefix is used for labels for basic blocks. Defaults to the same as
123 /// PrivateGlobalPrefix.
124 StringRef PrivateLabelPrefix;
126 /// This prefix is used for symbols that should be passed through the
127 /// assembler but be removed by the linker. This is 'l' on Darwin, currently
128 /// used for some ObjC metadata. The default of "" meast that for this system
129 /// a plain private symbol should be used. Defaults to "".
130 StringRef LinkerPrivateGlobalPrefix;
132 /// If these are nonempty, they contain a directive to emit before and after
133 /// an inline assembly statement. Defaults to "#APP\n", "#NO_APP\n"
134 const char *InlineAsmStart;
135 const char *InlineAsmEnd;
137 /// These are assembly directives that tells the assembler to interpret the
138 /// following instructions differently. Defaults to ".code16", ".code32",
140 const char *Code16Directive;
141 const char *Code32Directive;
142 const char *Code64Directive;
144 /// Which dialect of an assembler variant to use. Defaults to 0
145 unsigned AssemblerDialect;
147 /// This is true if the assembler allows @ characters in symbol names.
148 /// Defaults to false.
151 /// If this is true, symbol names with invalid characters will be printed in
153 bool SupportsQuotedNames;
155 /// This is true if data region markers should be printed as
156 /// ".data_region/.end_data_region" directives. If false, use "$d/$a" labels
158 bool UseDataRegionDirectives;
160 //===--- Data Emission Directives -------------------------------------===//
162 /// This should be set to the directive used to get some number of zero bytes
163 /// emitted to the current section. Common cases are "\t.zero\t" and
164 /// "\t.space\t". If this is set to null, the Data*bitsDirective's will be
165 /// used to emit zero bytes. Defaults to "\t.zero\t"
166 const char *ZeroDirective;
168 /// This directive allows emission of an ascii string with the standard C
169 /// escape characters embedded into it. Defaults to "\t.ascii\t"
170 const char *AsciiDirective;
172 /// If not null, this allows for special handling of zero terminated strings
173 /// on this target. This is commonly supported as ".asciz". If a target
174 /// doesn't support this, it can be set to null. Defaults to "\t.asciz\t"
175 const char *AscizDirective;
177 /// These directives are used to output some unit of integer data to the
178 /// current section. If a data directive is set to null, smaller data
179 /// directives will be used to emit the large sizes. Defaults to "\t.byte\t",
180 /// "\t.short\t", "\t.long\t", "\t.quad\t"
181 const char *Data8bitsDirective;
182 const char *Data16bitsDirective;
183 const char *Data32bitsDirective;
184 const char *Data64bitsDirective;
186 /// If non-null, a directive that is used to emit a word which should be
187 /// relocated as a 64-bit GP-relative offset, e.g. .gpdword on Mips. Defaults
189 const char *GPRel64Directive;
191 /// If non-null, a directive that is used to emit a word which should be
192 /// relocated as a 32-bit GP-relative offset, e.g. .gpword on Mips or .gprel32
193 /// on Alpha. Defaults to NULL.
194 const char *GPRel32Directive;
196 /// If non-null, directives that are used to emit a word/dword which should
197 /// be relocated as a 32/64-bit DTP/TP-relative offset, e.g. .dtprelword/
198 /// .dtpreldword/.tprelword/.tpreldword on Mips.
199 const char *DTPRel32Directive = nullptr;
200 const char *DTPRel64Directive = nullptr;
201 const char *TPRel32Directive = nullptr;
202 const char *TPRel64Directive = nullptr;
204 /// This is true if this target uses "Sun Style" syntax for section switching
205 /// ("#alloc,#write" etc) instead of the normal ELF syntax (,"a,w") in
206 /// .section directives. Defaults to false.
207 bool SunStyleELFSectionSwitchSyntax;
209 /// This is true if this target uses ELF '.section' directive before the
210 /// '.bss' one. It's used for PPC/Linux which doesn't support the '.bss'
211 /// directive only. Defaults to false.
212 bool UsesELFSectionDirectiveForBSS;
214 bool NeedsDwarfSectionOffsetDirective;
216 //===--- Alignment Information ----------------------------------------===//
218 /// If this is true (the default) then the asmprinter emits ".align N"
219 /// directives, where N is the number of bytes to align to. Otherwise, it
220 /// emits ".align log2(N)", e.g. 3 to align to an 8 byte boundary. Defaults
222 bool AlignmentIsInBytes;
224 /// If non-zero, this is used to fill the executable space created as the
225 /// result of a alignment directive. Defaults to 0
226 unsigned TextAlignFillValue;
228 //===--- Global Variable Emission Directives --------------------------===//
230 /// This is the directive used to declare a global entity. Defaults to
232 const char *GlobalDirective;
234 /// True if the expression
236 /// uses a relocation but it can be suppressed by writing
239 bool SetDirectiveSuppressesReloc;
241 /// False if the assembler requires that we use
253 /// Defaults to true.
254 bool HasAggressiveSymbolFolding;
256 /// True is .comm's and .lcomms optional alignment is to be specified in bytes
257 /// instead of log2(n). Defaults to true.
258 bool COMMDirectiveAlignmentIsInBytes;
260 /// Describes if the .lcomm directive for the target supports an alignment
261 /// argument and how it is interpreted. Defaults to NoAlignment.
262 LCOMM::LCOMMType LCOMMDirectiveAlignmentType;
264 // True if the target allows .align directives on functions. This is true for
265 // most targets, so defaults to true.
266 bool HasFunctionAlignment;
268 /// True if the target has .type and .size directives, this is true for most
269 /// ELF targets. Defaults to true.
270 bool HasDotTypeDotSizeDirective;
272 /// True if the target has a single parameter .file directive, this is true
273 /// for ELF targets. Defaults to true.
274 bool HasSingleParameterDotFile;
276 /// True if the target has a .ident directive, this is true for ELF targets.
277 /// Defaults to false.
278 bool HasIdentDirective;
280 /// True if this target supports the MachO .no_dead_strip directive. Defaults
284 /// True if this target supports the MachO .alt_entry directive. Defaults to
288 /// Used to declare a global as being a weak symbol. Defaults to ".weak".
289 const char *WeakDirective;
291 /// This directive, if non-null, is used to declare a global as being a weak
292 /// undefined symbol. Defaults to NULL.
293 const char *WeakRefDirective;
295 /// True if we have a directive to declare a global as being a weak defined
296 /// symbol. Defaults to false.
297 bool HasWeakDefDirective;
299 /// True if we have a directive to declare a global as being a weak defined
300 /// symbol that can be hidden (unexported). Defaults to false.
301 bool HasWeakDefCanBeHiddenDirective;
303 /// True if we have a .linkonce directive. This is used on cygwin/mingw.
304 /// Defaults to false.
305 bool HasLinkOnceDirective;
307 /// This attribute, if not MCSA_Invalid, is used to declare a symbol as having
308 /// hidden visibility. Defaults to MCSA_Hidden.
309 MCSymbolAttr HiddenVisibilityAttr;
311 /// This attribute, if not MCSA_Invalid, is used to declare an undefined
312 /// symbol as having hidden visibility. Defaults to MCSA_Hidden.
313 MCSymbolAttr HiddenDeclarationVisibilityAttr;
315 /// This attribute, if not MCSA_Invalid, is used to declare a symbol as having
316 /// protected visibility. Defaults to MCSA_Protected
317 MCSymbolAttr ProtectedVisibilityAttr;
319 //===--- Dwarf Emission Directives -----------------------------------===//
321 /// True if target supports emission of debugging information. Defaults to
323 bool SupportsDebugInformation;
325 /// Exception handling format for the target. Defaults to None.
326 ExceptionHandling ExceptionsType;
328 /// Windows exception handling data (.pdata) encoding. Defaults to Invalid.
329 WinEH::EncodingType WinEHEncodingType;
331 /// True if Dwarf2 output generally uses relocations for references to other
332 /// .debug_* sections.
333 bool DwarfUsesRelocationsAcrossSections;
335 /// True if DWARF FDE symbol reference relocations should be replaced by an
336 /// absolute difference.
337 bool DwarfFDESymbolsUseAbsDiff;
339 /// True if dwarf register numbers are printed instead of symbolic register
340 /// names in .cfi_* directives. Defaults to false.
341 bool DwarfRegNumForCFI;
343 /// True if target uses parens to indicate the symbol variant instead of @.
344 /// For example, foo(plt) instead of foo@plt. Defaults to false.
345 bool UseParensForSymbolVariant;
347 //===--- Prologue State ----------------------------------------------===//
349 std::vector<MCCFIInstruction> InitialFrameState;
351 //===--- Integrated Assembler Information ----------------------------===//
353 /// Should we use the integrated assembler?
354 /// The integrated assembler should be enabled by default (by the
355 /// constructors) when failing to parse a valid piece of assembly (inline
356 /// or otherwise) is considered a bug. It may then be overridden after
357 /// construction (see LLVMTargetMachine::initAsmInfo()).
358 bool UseIntegratedAssembler;
360 /// Preserve Comments in assembly
361 bool PreserveAsmComments;
363 /// Compress DWARF debug sections. Defaults to no compression.
364 DebugCompressionType CompressDebugSections;
366 /// True if the integrated assembler should interpret 'a >> b' constant
367 /// expressions as logical rather than arithmetic.
370 // If true, emit GOTPCRELX/REX_GOTPCRELX instead of GOTPCREL, on
372 bool RelaxELFRelocations = true;
374 // If true, then the lexer and expression parser will support %neg(),
375 // %hi(), and similar unary operators.
376 bool HasMipsExpressions = false;
379 explicit MCAsmInfo();
380 virtual ~MCAsmInfo();
382 /// Get the pointer size in bytes.
383 unsigned getPointerSize() const { return PointerSize; }
385 /// Get the callee-saved register stack slot
387 unsigned getCalleeSaveStackSlotSize() const {
388 return CalleeSaveStackSlotSize;
391 /// True if the target is little endian.
392 bool isLittleEndian() const { return IsLittleEndian; }
394 /// True if target stack grow up.
395 bool isStackGrowthDirectionUp() const { return StackGrowsUp; }
397 bool hasSubsectionsViaSymbols() const { return HasSubsectionsViaSymbols; }
399 // Data directive accessors.
401 const char *getData8bitsDirective() const { return Data8bitsDirective; }
402 const char *getData16bitsDirective() const { return Data16bitsDirective; }
403 const char *getData32bitsDirective() const { return Data32bitsDirective; }
404 const char *getData64bitsDirective() const { return Data64bitsDirective; }
405 const char *getGPRel64Directive() const { return GPRel64Directive; }
406 const char *getGPRel32Directive() const { return GPRel32Directive; }
407 const char *getDTPRel64Directive() const { return DTPRel64Directive; }
408 const char *getDTPRel32Directive() const { return DTPRel32Directive; }
409 const char *getTPRel64Directive() const { return TPRel64Directive; }
410 const char *getTPRel32Directive() const { return TPRel32Directive; }
412 /// Targets can implement this method to specify a section to switch to if the
413 /// translation unit doesn't have any trampolines that require an executable
415 virtual MCSection *getNonexecutableStackSection(MCContext &Ctx) const {
419 /// \brief True if the section is atomized using the symbols in it.
420 /// This is false if the section is not atomized at all (most ELF sections) or
421 /// if it is atomized based on its contents (MachO' __TEXT,__cstring for
423 virtual bool isSectionAtomizableBySymbols(const MCSection &Section) const;
425 virtual const MCExpr *getExprForPersonalitySymbol(const MCSymbol *Sym,
427 MCStreamer &Streamer) const;
429 virtual const MCExpr *getExprForFDESymbol(const MCSymbol *Sym,
431 MCStreamer &Streamer) const;
433 /// Return true if the identifier \p Name does not need quotes to be
434 /// syntactically correct.
435 virtual bool isValidUnquotedName(StringRef Name) const;
437 /// Return true if the .section directive should be omitted when
438 /// emitting \p SectionName. For example:
440 /// shouldOmitSectionDirective(".text")
442 /// returns false => .section .text,#alloc,#execinstr
443 /// returns true => .text
444 virtual bool shouldOmitSectionDirective(StringRef SectionName) const;
446 bool usesSunStyleELFSectionSwitchSyntax() const {
447 return SunStyleELFSectionSwitchSyntax;
450 bool usesELFSectionDirectiveForBSS() const {
451 return UsesELFSectionDirectiveForBSS;
454 bool needsDwarfSectionOffsetDirective() const {
455 return NeedsDwarfSectionOffsetDirective;
460 bool hasMachoZeroFillDirective() const { return HasMachoZeroFillDirective; }
461 bool hasMachoTBSSDirective() const { return HasMachoTBSSDirective; }
462 unsigned getMaxInstLength() const { return MaxInstLength; }
463 unsigned getMinInstAlignment() const { return MinInstAlignment; }
464 bool getDollarIsPC() const { return DollarIsPC; }
465 const char *getSeparatorString() const { return SeparatorString; }
467 /// This indicates the column (zero-based) at which asm comments should be
469 unsigned getCommentColumn() const { return 40; }
471 StringRef getCommentString() const { return CommentString; }
472 const char *getLabelSuffix() const { return LabelSuffix; }
474 bool useAssignmentForEHBegin() const { return UseAssignmentForEHBegin; }
475 bool needsLocalForSize() const { return NeedsLocalForSize; }
476 StringRef getPrivateGlobalPrefix() const { return PrivateGlobalPrefix; }
477 StringRef getPrivateLabelPrefix() const { return PrivateLabelPrefix; }
478 bool hasLinkerPrivateGlobalPrefix() const {
479 return LinkerPrivateGlobalPrefix[0] != '\0';
481 StringRef getLinkerPrivateGlobalPrefix() const {
482 if (hasLinkerPrivateGlobalPrefix())
483 return LinkerPrivateGlobalPrefix;
484 return getPrivateGlobalPrefix();
486 const char *getInlineAsmStart() const { return InlineAsmStart; }
487 const char *getInlineAsmEnd() const { return InlineAsmEnd; }
488 const char *getCode16Directive() const { return Code16Directive; }
489 const char *getCode32Directive() const { return Code32Directive; }
490 const char *getCode64Directive() const { return Code64Directive; }
491 unsigned getAssemblerDialect() const { return AssemblerDialect; }
492 bool doesAllowAtInName() const { return AllowAtInName; }
493 bool supportsNameQuoting() const { return SupportsQuotedNames; }
494 bool doesSupportDataRegionDirectives() const {
495 return UseDataRegionDirectives;
497 const char *getZeroDirective() const { return ZeroDirective; }
498 const char *getAsciiDirective() const { return AsciiDirective; }
499 const char *getAscizDirective() const { return AscizDirective; }
500 bool getAlignmentIsInBytes() const { return AlignmentIsInBytes; }
501 unsigned getTextAlignFillValue() const { return TextAlignFillValue; }
502 const char *getGlobalDirective() const { return GlobalDirective; }
503 bool doesSetDirectiveSuppressReloc() const {
504 return SetDirectiveSuppressesReloc;
506 bool hasAggressiveSymbolFolding() const { return HasAggressiveSymbolFolding; }
507 bool getCOMMDirectiveAlignmentIsInBytes() const {
508 return COMMDirectiveAlignmentIsInBytes;
510 LCOMM::LCOMMType getLCOMMDirectiveAlignmentType() const {
511 return LCOMMDirectiveAlignmentType;
513 bool hasFunctionAlignment() const { return HasFunctionAlignment; }
514 bool hasDotTypeDotSizeDirective() const { return HasDotTypeDotSizeDirective; }
515 bool hasSingleParameterDotFile() const { return HasSingleParameterDotFile; }
516 bool hasIdentDirective() const { return HasIdentDirective; }
517 bool hasNoDeadStrip() const { return HasNoDeadStrip; }
518 bool hasAltEntry() const { return HasAltEntry; }
519 const char *getWeakDirective() const { return WeakDirective; }
520 const char *getWeakRefDirective() const { return WeakRefDirective; }
521 bool hasWeakDefDirective() const { return HasWeakDefDirective; }
522 bool hasWeakDefCanBeHiddenDirective() const {
523 return HasWeakDefCanBeHiddenDirective;
525 bool hasLinkOnceDirective() const { return HasLinkOnceDirective; }
527 MCSymbolAttr getHiddenVisibilityAttr() const { return HiddenVisibilityAttr; }
528 MCSymbolAttr getHiddenDeclarationVisibilityAttr() const {
529 return HiddenDeclarationVisibilityAttr;
531 MCSymbolAttr getProtectedVisibilityAttr() const {
532 return ProtectedVisibilityAttr;
534 bool doesSupportDebugInformation() const { return SupportsDebugInformation; }
535 bool doesSupportExceptionHandling() const {
536 return ExceptionsType != ExceptionHandling::None;
538 ExceptionHandling getExceptionHandlingType() const { return ExceptionsType; }
539 WinEH::EncodingType getWinEHEncodingType() const { return WinEHEncodingType; }
541 void setExceptionsType(ExceptionHandling EH) {
545 /// Returns true if the exception handling method for the platform uses call
546 /// frame information to unwind.
547 bool usesCFIForEH() const {
548 return (ExceptionsType == ExceptionHandling::DwarfCFI ||
549 ExceptionsType == ExceptionHandling::ARM || usesWindowsCFI());
552 bool usesWindowsCFI() const {
553 return ExceptionsType == ExceptionHandling::WinEH &&
554 (WinEHEncodingType != WinEH::EncodingType::Invalid &&
555 WinEHEncodingType != WinEH::EncodingType::X86);
558 bool doesDwarfUseRelocationsAcrossSections() const {
559 return DwarfUsesRelocationsAcrossSections;
561 bool doDwarfFDESymbolsUseAbsDiff() const { return DwarfFDESymbolsUseAbsDiff; }
562 bool useDwarfRegNumForCFI() const { return DwarfRegNumForCFI; }
563 bool useParensForSymbolVariant() const { return UseParensForSymbolVariant; }
565 void addInitialFrameState(const MCCFIInstruction &Inst) {
566 InitialFrameState.push_back(Inst);
569 const std::vector<MCCFIInstruction> &getInitialFrameState() const {
570 return InitialFrameState;
573 /// Return true if assembly (inline or otherwise) should be parsed.
574 bool useIntegratedAssembler() const { return UseIntegratedAssembler; }
576 /// Set whether assembly (inline or otherwise) should be parsed.
577 virtual void setUseIntegratedAssembler(bool Value) {
578 UseIntegratedAssembler = Value;
581 /// Return true if assembly (inline or otherwise) should be parsed.
582 bool preserveAsmComments() const { return PreserveAsmComments; }
584 /// Set whether assembly (inline or otherwise) should be parsed.
585 virtual void setPreserveAsmComments(bool Value) {
586 PreserveAsmComments = Value;
589 DebugCompressionType compressDebugSections() const {
590 return CompressDebugSections;
593 void setCompressDebugSections(DebugCompressionType CompressDebugSections) {
594 this->CompressDebugSections = CompressDebugSections;
597 bool shouldUseLogicalShr() const { return UseLogicalShr; }
599 bool canRelaxRelocations() const { return RelaxELFRelocations; }
600 void setRelaxELFRelocations(bool V) { RelaxELFRelocations = V; }
601 bool hasMipsExpressions() const { return HasMipsExpressions; }