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/ADT/StringRef.h"
20 #include "llvm/MC/MCDirectives.h"
21 #include "llvm/MC/MCDwarf.h"
22 #include "llvm/MC/MCTargetOptions.h"
35 enum class EncodingType {
37 Alpha, /// Windows Alpha
38 Alpha64, /// Windows AXP64
39 ARM, /// Windows NT (Windows on ARM)
40 CE, /// Windows CE ARM, PowerPC, SH3, SH4
41 Itanium, /// Windows x64, Windows Itanium (IA-64)
42 X86, /// Windows x86, uses no CFI, just EH tables
46 } // end namespace WinEH
50 enum LCOMMType { NoAlignment, ByteAlignment, Log2Alignment };
52 } // end namespace LCOMM
54 enum class DebugCompressionType {
55 DCT_None, // no compression
56 DCT_Zlib, // zlib style complession
57 DCT_ZlibGnu // zlib-gnu style compression
60 /// This class is intended to be used as a base class for asm
61 /// properties and features specific to the target.
64 //===------------------------------------------------------------------===//
65 // Properties to be set by the target writer, used to configure asm printer.
68 /// Code pointer size in bytes. Default is 4.
69 unsigned CodePointerSize = 4;
71 /// Size of the stack slot reserved for callee-saved registers, in bytes.
72 /// Default is same as pointer size.
73 unsigned CalleeSaveStackSlotSize = 4;
75 /// True if target is little endian. Default is true.
76 bool IsLittleEndian = true;
78 /// True if target stack grow up. Default is false.
79 bool StackGrowsUp = false;
81 /// True if this target has the MachO .subsections_via_symbols directive.
83 bool HasSubsectionsViaSymbols = false;
85 /// True if this is a MachO target that supports the macho-specific .zerofill
86 /// directive for emitting BSS Symbols. Default is false.
87 bool HasMachoZeroFillDirective = false;
89 /// True if this is a MachO target that supports the macho-specific .tbss
90 /// directive for emitting thread local BSS Symbols. Default is false.
91 bool HasMachoTBSSDirective = false;
93 /// This is the maximum possible length of an instruction, which is needed to
94 /// compute the size of an inline asm. Defaults to 4.
95 unsigned MaxInstLength = 4;
97 /// Every possible instruction length is a multiple of this value. Factored
98 /// out in .debug_frame and .debug_line. Defaults to 1.
99 unsigned MinInstAlignment = 1;
101 /// The '$' token, when not referencing an identifier or constant, refers to
102 /// the current PC. Defaults to false.
103 bool DollarIsPC = false;
105 /// This string, if specified, is used to separate instructions from each
106 /// other when on the same line. Defaults to ';'
107 const char *SeparatorString;
109 /// This indicates the comment character used by the assembler. Defaults to
111 StringRef CommentString;
113 /// This is appended to emitted labels. Defaults to ":"
114 const char *LabelSuffix;
116 // Print the EH begin symbol with an assignment. Defaults to false.
117 bool UseAssignmentForEHBegin = false;
119 // Do we need to create a local symbol for .size?
120 bool NeedsLocalForSize = false;
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 StringRef PrivateGlobalPrefix;
127 /// This prefix is used for labels for basic blocks. Defaults to the same as
128 /// PrivateGlobalPrefix.
129 StringRef PrivateLabelPrefix;
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 StringRef LinkerPrivateGlobalPrefix;
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;
142 /// These are assembly directives that tells the assembler to interpret the
143 /// following instructions differently. Defaults to ".code16", ".code32",
145 const char *Code16Directive;
146 const char *Code32Directive;
147 const char *Code64Directive;
149 /// Which dialect of an assembler variant to use. Defaults to 0
150 unsigned AssemblerDialect = 0;
152 /// This is true if the assembler allows @ characters in symbol names.
153 /// Defaults to false.
154 bool AllowAtInName = false;
156 /// If this is true, symbol names with invalid characters will be printed in
158 bool SupportsQuotedNames = true;
160 /// This is true if data region markers should be printed as
161 /// ".data_region/.end_data_region" directives. If false, use "$d/$a" labels
163 bool UseDataRegionDirectives = false;
165 //===--- Data Emission Directives -------------------------------------===//
167 /// This should be set to the directive used to get some number of zero bytes
168 /// emitted to the current section. Common cases are "\t.zero\t" and
169 /// "\t.space\t". If this is set to null, the Data*bitsDirective's will be
170 /// used to emit zero bytes. Defaults to "\t.zero\t"
171 const char *ZeroDirective;
173 /// This directive allows emission of an ascii string with the standard C
174 /// escape characters embedded into it. Defaults to "\t.ascii\t"
175 const char *AsciiDirective;
177 /// If not null, this allows for special handling of zero terminated strings
178 /// on this target. This is commonly supported as ".asciz". If a target
179 /// doesn't support this, it can be set to null. Defaults to "\t.asciz\t"
180 const char *AscizDirective;
182 /// These directives are used to output some unit of integer data to the
183 /// current section. If a data directive is set to null, smaller data
184 /// directives will be used to emit the large sizes. Defaults to "\t.byte\t",
185 /// "\t.short\t", "\t.long\t", "\t.quad\t"
186 const char *Data8bitsDirective;
187 const char *Data16bitsDirective;
188 const char *Data32bitsDirective;
189 const char *Data64bitsDirective;
191 /// If non-null, a directive that is used to emit a word which should be
192 /// relocated as a 64-bit GP-relative offset, e.g. .gpdword on Mips. Defaults
194 const char *GPRel64Directive = nullptr;
196 /// If non-null, a directive that is used to emit a word which should be
197 /// relocated as a 32-bit GP-relative offset, e.g. .gpword on Mips or .gprel32
198 /// on Alpha. Defaults to nullptr.
199 const char *GPRel32Directive = nullptr;
201 /// If non-null, directives that are used to emit a word/dword which should
202 /// be relocated as a 32/64-bit DTP/TP-relative offset, e.g. .dtprelword/
203 /// .dtpreldword/.tprelword/.tpreldword on Mips.
204 const char *DTPRel32Directive = nullptr;
205 const char *DTPRel64Directive = nullptr;
206 const char *TPRel32Directive = nullptr;
207 const char *TPRel64Directive = nullptr;
209 /// This is true if this target uses "Sun Style" syntax for section switching
210 /// ("#alloc,#write" etc) instead of the normal ELF syntax (,"a,w") in
211 /// .section directives. Defaults to false.
212 bool SunStyleELFSectionSwitchSyntax = false;
214 /// This is true if this target uses ELF '.section' directive before the
215 /// '.bss' one. It's used for PPC/Linux which doesn't support the '.bss'
216 /// directive only. Defaults to false.
217 bool UsesELFSectionDirectiveForBSS = false;
219 bool NeedsDwarfSectionOffsetDirective = false;
221 //===--- Alignment Information ----------------------------------------===//
223 /// If this is true (the default) then the asmprinter emits ".align N"
224 /// directives, where N is the number of bytes to align to. Otherwise, it
225 /// emits ".align log2(N)", e.g. 3 to align to an 8 byte boundary. Defaults
227 bool AlignmentIsInBytes = true;
229 /// If non-zero, this is used to fill the executable space created as the
230 /// result of a alignment directive. Defaults to 0
231 unsigned TextAlignFillValue = 0;
233 //===--- Global Variable Emission Directives --------------------------===//
235 /// This is the directive used to declare a global entity. Defaults to
237 const char *GlobalDirective;
239 /// True if the expression
241 /// uses a relocation but it can be suppressed by writing
244 bool SetDirectiveSuppressesReloc = false;
246 /// False if the assembler requires that we use
258 /// Defaults to true.
259 bool HasAggressiveSymbolFolding = true;
261 /// True is .comm's and .lcomms optional alignment is to be specified in bytes
262 /// instead of log2(n). Defaults to true.
263 bool COMMDirectiveAlignmentIsInBytes = true;
265 /// Describes if the .lcomm directive for the target supports an alignment
266 /// argument and how it is interpreted. Defaults to NoAlignment.
267 LCOMM::LCOMMType LCOMMDirectiveAlignmentType = LCOMM::NoAlignment;
269 // True if the target allows .align directives on functions. This is true for
270 // most targets, so defaults to true.
271 bool HasFunctionAlignment = true;
273 /// True if the target has .type and .size directives, this is true for most
274 /// ELF targets. Defaults to true.
275 bool HasDotTypeDotSizeDirective = true;
277 /// True if the target has a single parameter .file directive, this is true
278 /// for ELF targets. Defaults to true.
279 bool HasSingleParameterDotFile = true;
281 /// True if the target has a .ident directive, this is true for ELF targets.
282 /// Defaults to false.
283 bool HasIdentDirective = false;
285 /// True if this target supports the MachO .no_dead_strip directive. Defaults
287 bool HasNoDeadStrip = false;
289 /// True if this target supports the MachO .alt_entry directive. Defaults to
291 bool HasAltEntry = false;
293 /// Used to declare a global as being a weak symbol. Defaults to ".weak".
294 const char *WeakDirective;
296 /// This directive, if non-null, is used to declare a global as being a weak
297 /// undefined symbol. Defaults to nullptr.
298 const char *WeakRefDirective = nullptr;
300 /// True if we have a directive to declare a global as being a weak defined
301 /// symbol. Defaults to false.
302 bool HasWeakDefDirective = false;
304 /// True if we have a directive to declare a global as being a weak defined
305 /// symbol that can be hidden (unexported). Defaults to false.
306 bool HasWeakDefCanBeHiddenDirective = false;
308 /// True if we have a .linkonce directive. This is used on cygwin/mingw.
309 /// Defaults to false.
310 bool HasLinkOnceDirective = false;
312 /// This attribute, if not MCSA_Invalid, is used to declare a symbol as having
313 /// hidden visibility. Defaults to MCSA_Hidden.
314 MCSymbolAttr HiddenVisibilityAttr = MCSA_Hidden;
316 /// This attribute, if not MCSA_Invalid, is used to declare an undefined
317 /// symbol as having hidden visibility. Defaults to MCSA_Hidden.
318 MCSymbolAttr HiddenDeclarationVisibilityAttr = MCSA_Hidden;
320 /// This attribute, if not MCSA_Invalid, is used to declare a symbol as having
321 /// protected visibility. Defaults to MCSA_Protected
322 MCSymbolAttr ProtectedVisibilityAttr = MCSA_Protected;
324 //===--- Dwarf Emission Directives -----------------------------------===//
326 /// True if target supports emission of debugging information. Defaults to
328 bool SupportsDebugInformation = false;
330 /// Exception handling format for the target. Defaults to None.
331 ExceptionHandling ExceptionsType = ExceptionHandling::None;
333 /// Windows exception handling data (.pdata) encoding. Defaults to Invalid.
334 WinEH::EncodingType WinEHEncodingType = WinEH::EncodingType::Invalid;
336 /// True if Dwarf2 output generally uses relocations for references to other
337 /// .debug_* sections.
338 bool DwarfUsesRelocationsAcrossSections = true;
340 /// True if DWARF FDE symbol reference relocations should be replaced by an
341 /// absolute difference.
342 bool DwarfFDESymbolsUseAbsDiff = false;
344 /// True if dwarf register numbers are printed instead of symbolic register
345 /// names in .cfi_* directives. Defaults to false.
346 bool DwarfRegNumForCFI = false;
348 /// True if target uses parens to indicate the symbol variant instead of @.
349 /// For example, foo(plt) instead of foo@plt. Defaults to false.
350 bool UseParensForSymbolVariant = false;
352 //===--- Prologue State ----------------------------------------------===//
354 std::vector<MCCFIInstruction> InitialFrameState;
356 //===--- Integrated Assembler Information ----------------------------===//
358 /// Should we use the integrated assembler?
359 /// The integrated assembler should be enabled by default (by the
360 /// constructors) when failing to parse a valid piece of assembly (inline
361 /// or otherwise) is considered a bug. It may then be overridden after
362 /// construction (see LLVMTargetMachine::initAsmInfo()).
363 bool UseIntegratedAssembler;
365 /// Preserve Comments in assembly
366 bool PreserveAsmComments;
368 /// Compress DWARF debug sections. Defaults to no compression.
369 DebugCompressionType CompressDebugSections = DebugCompressionType::DCT_None;
371 /// True if the integrated assembler should interpret 'a >> b' constant
372 /// expressions as logical rather than arithmetic.
373 bool UseLogicalShr = true;
375 // If true, emit GOTPCRELX/REX_GOTPCRELX instead of GOTPCREL, on
377 bool RelaxELFRelocations = true;
379 // If true, then the lexer and expression parser will support %neg(),
380 // %hi(), and similar unary operators.
381 bool HasMipsExpressions = false;
384 explicit MCAsmInfo();
385 virtual ~MCAsmInfo();
387 /// Get the code pointer size in bytes.
388 unsigned getCodePointerSize() const { return CodePointerSize; }
390 /// Get the callee-saved register stack slot
392 unsigned getCalleeSaveStackSlotSize() const {
393 return CalleeSaveStackSlotSize;
396 /// True if the target is little endian.
397 bool isLittleEndian() const { return IsLittleEndian; }
399 /// True if target stack grow up.
400 bool isStackGrowthDirectionUp() const { return StackGrowsUp; }
402 bool hasSubsectionsViaSymbols() const { return HasSubsectionsViaSymbols; }
404 // Data directive accessors.
406 const char *getData8bitsDirective() const { return Data8bitsDirective; }
407 const char *getData16bitsDirective() const { return Data16bitsDirective; }
408 const char *getData32bitsDirective() const { return Data32bitsDirective; }
409 const char *getData64bitsDirective() const { return Data64bitsDirective; }
410 const char *getGPRel64Directive() const { return GPRel64Directive; }
411 const char *getGPRel32Directive() const { return GPRel32Directive; }
412 const char *getDTPRel64Directive() const { return DTPRel64Directive; }
413 const char *getDTPRel32Directive() const { return DTPRel32Directive; }
414 const char *getTPRel64Directive() const { return TPRel64Directive; }
415 const char *getTPRel32Directive() const { return TPRel32Directive; }
417 /// Targets can implement this method to specify a section to switch to if the
418 /// translation unit doesn't have any trampolines that require an executable
420 virtual MCSection *getNonexecutableStackSection(MCContext &Ctx) const {
424 /// \brief True if the section is atomized using the symbols in it.
425 /// This is false if the section is not atomized at all (most ELF sections) or
426 /// if it is atomized based on its contents (MachO' __TEXT,__cstring for
428 virtual bool isSectionAtomizableBySymbols(const MCSection &Section) const;
430 virtual const MCExpr *getExprForPersonalitySymbol(const MCSymbol *Sym,
432 MCStreamer &Streamer) const;
434 virtual const MCExpr *getExprForFDESymbol(const MCSymbol *Sym,
436 MCStreamer &Streamer) const;
438 /// Return true if the identifier \p Name does not need quotes to be
439 /// syntactically correct.
440 virtual bool isValidUnquotedName(StringRef Name) const;
442 /// Return true if the .section directive should be omitted when
443 /// emitting \p SectionName. For example:
445 /// shouldOmitSectionDirective(".text")
447 /// returns false => .section .text,#alloc,#execinstr
448 /// returns true => .text
449 virtual bool shouldOmitSectionDirective(StringRef SectionName) const;
451 bool usesSunStyleELFSectionSwitchSyntax() const {
452 return SunStyleELFSectionSwitchSyntax;
455 bool usesELFSectionDirectiveForBSS() const {
456 return UsesELFSectionDirectiveForBSS;
459 bool needsDwarfSectionOffsetDirective() const {
460 return NeedsDwarfSectionOffsetDirective;
465 bool hasMachoZeroFillDirective() const { return HasMachoZeroFillDirective; }
466 bool hasMachoTBSSDirective() const { return HasMachoTBSSDirective; }
467 unsigned getMaxInstLength() const { return MaxInstLength; }
468 unsigned getMinInstAlignment() const { return MinInstAlignment; }
469 bool getDollarIsPC() const { return DollarIsPC; }
470 const char *getSeparatorString() const { return SeparatorString; }
472 /// This indicates the column (zero-based) at which asm comments should be
474 unsigned getCommentColumn() const { return 40; }
476 StringRef getCommentString() const { return CommentString; }
477 const char *getLabelSuffix() const { return LabelSuffix; }
479 bool useAssignmentForEHBegin() const { return UseAssignmentForEHBegin; }
480 bool needsLocalForSize() const { return NeedsLocalForSize; }
481 StringRef getPrivateGlobalPrefix() const { return PrivateGlobalPrefix; }
482 StringRef getPrivateLabelPrefix() const { return PrivateLabelPrefix; }
484 bool hasLinkerPrivateGlobalPrefix() const {
485 return LinkerPrivateGlobalPrefix[0] != '\0';
488 StringRef getLinkerPrivateGlobalPrefix() const {
489 if (hasLinkerPrivateGlobalPrefix())
490 return LinkerPrivateGlobalPrefix;
491 return getPrivateGlobalPrefix();
494 const char *getInlineAsmStart() const { return InlineAsmStart; }
495 const char *getInlineAsmEnd() const { return InlineAsmEnd; }
496 const char *getCode16Directive() const { return Code16Directive; }
497 const char *getCode32Directive() const { return Code32Directive; }
498 const char *getCode64Directive() const { return Code64Directive; }
499 unsigned getAssemblerDialect() const { return AssemblerDialect; }
500 bool doesAllowAtInName() const { return AllowAtInName; }
501 bool supportsNameQuoting() const { return SupportsQuotedNames; }
503 bool doesSupportDataRegionDirectives() const {
504 return UseDataRegionDirectives;
507 const char *getZeroDirective() const { return ZeroDirective; }
508 const char *getAsciiDirective() const { return AsciiDirective; }
509 const char *getAscizDirective() const { return AscizDirective; }
510 bool getAlignmentIsInBytes() const { return AlignmentIsInBytes; }
511 unsigned getTextAlignFillValue() const { return TextAlignFillValue; }
512 const char *getGlobalDirective() const { return GlobalDirective; }
514 bool doesSetDirectiveSuppressReloc() const {
515 return SetDirectiveSuppressesReloc;
518 bool hasAggressiveSymbolFolding() const { return HasAggressiveSymbolFolding; }
520 bool getCOMMDirectiveAlignmentIsInBytes() const {
521 return COMMDirectiveAlignmentIsInBytes;
524 LCOMM::LCOMMType getLCOMMDirectiveAlignmentType() const {
525 return LCOMMDirectiveAlignmentType;
528 bool hasFunctionAlignment() const { return HasFunctionAlignment; }
529 bool hasDotTypeDotSizeDirective() const { return HasDotTypeDotSizeDirective; }
530 bool hasSingleParameterDotFile() const { return HasSingleParameterDotFile; }
531 bool hasIdentDirective() const { return HasIdentDirective; }
532 bool hasNoDeadStrip() const { return HasNoDeadStrip; }
533 bool hasAltEntry() const { return HasAltEntry; }
534 const char *getWeakDirective() const { return WeakDirective; }
535 const char *getWeakRefDirective() const { return WeakRefDirective; }
536 bool hasWeakDefDirective() const { return HasWeakDefDirective; }
538 bool hasWeakDefCanBeHiddenDirective() const {
539 return HasWeakDefCanBeHiddenDirective;
542 bool hasLinkOnceDirective() const { return HasLinkOnceDirective; }
544 MCSymbolAttr getHiddenVisibilityAttr() const { return HiddenVisibilityAttr; }
546 MCSymbolAttr getHiddenDeclarationVisibilityAttr() const {
547 return HiddenDeclarationVisibilityAttr;
550 MCSymbolAttr getProtectedVisibilityAttr() const {
551 return ProtectedVisibilityAttr;
554 bool doesSupportDebugInformation() const { return SupportsDebugInformation; }
556 bool doesSupportExceptionHandling() const {
557 return ExceptionsType != ExceptionHandling::None;
560 ExceptionHandling getExceptionHandlingType() const { return ExceptionsType; }
561 WinEH::EncodingType getWinEHEncodingType() const { return WinEHEncodingType; }
563 void setExceptionsType(ExceptionHandling EH) {
567 /// Returns true if the exception handling method for the platform uses call
568 /// frame information to unwind.
569 bool usesCFIForEH() const {
570 return (ExceptionsType == ExceptionHandling::DwarfCFI ||
571 ExceptionsType == ExceptionHandling::ARM || usesWindowsCFI());
574 bool usesWindowsCFI() const {
575 return ExceptionsType == ExceptionHandling::WinEH &&
576 (WinEHEncodingType != WinEH::EncodingType::Invalid &&
577 WinEHEncodingType != WinEH::EncodingType::X86);
580 bool doesDwarfUseRelocationsAcrossSections() const {
581 return DwarfUsesRelocationsAcrossSections;
584 bool doDwarfFDESymbolsUseAbsDiff() const { return DwarfFDESymbolsUseAbsDiff; }
585 bool useDwarfRegNumForCFI() const { return DwarfRegNumForCFI; }
586 bool useParensForSymbolVariant() const { return UseParensForSymbolVariant; }
588 void addInitialFrameState(const MCCFIInstruction &Inst) {
589 InitialFrameState.push_back(Inst);
592 const std::vector<MCCFIInstruction> &getInitialFrameState() const {
593 return InitialFrameState;
596 /// Return true if assembly (inline or otherwise) should be parsed.
597 bool useIntegratedAssembler() const { return UseIntegratedAssembler; }
599 /// Set whether assembly (inline or otherwise) should be parsed.
600 virtual void setUseIntegratedAssembler(bool Value) {
601 UseIntegratedAssembler = Value;
604 /// Return true if assembly (inline or otherwise) should be parsed.
605 bool preserveAsmComments() const { return PreserveAsmComments; }
607 /// Set whether assembly (inline or otherwise) should be parsed.
608 virtual void setPreserveAsmComments(bool Value) {
609 PreserveAsmComments = Value;
612 DebugCompressionType compressDebugSections() const {
613 return CompressDebugSections;
616 void setCompressDebugSections(DebugCompressionType CompressDebugSections) {
617 this->CompressDebugSections = CompressDebugSections;
620 bool shouldUseLogicalShr() const { return UseLogicalShr; }
622 bool canRelaxRelocations() const { return RelaxELFRelocations; }
623 void setRelaxELFRelocations(bool V) { RelaxELFRelocations = V; }
624 bool hasMipsExpressions() const { return HasMipsExpressions; }
627 } // end namespace llvm
629 #endif // LLVM_MC_MCASMINFO_H