MCAsmInfo.h revision 674be02d525d4e24bc6943ed9274958c580bcfbc
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/MachineLocation.h"
21#include <cassert>
22#include <vector>
23
24namespace llvm {
25  class MCExpr;
26  class MCSection;
27  class MCStreamer;
28  class MCSymbol;
29  class MCContext;
30
31  namespace ExceptionHandling {
32    enum ExceptionsType { None, DwarfCFI, SjLj, ARM, Win64 };
33  }
34
35  namespace LCOMM {
36    enum LCOMMType { NoAlignment, ByteAlignment, Log2Alignment };
37  }
38
39  /// MCAsmInfo - This class is intended to be used as a base class for asm
40  /// properties and features specific to the target.
41  class MCAsmInfo {
42  protected:
43    //===------------------------------------------------------------------===//
44    // Properties to be set by the target writer, used to configure asm printer.
45    //
46
47    /// PointerSize - Pointer size in bytes.
48    ///               Default is 4.
49    unsigned PointerSize;
50
51    /// IsLittleEndian - True if target is little endian.
52    ///                  Default is true.
53    bool IsLittleEndian;
54
55    /// StackGrowsUp - True if target stack grow up.
56    ///                Default is false.
57    bool StackGrowsUp;
58
59    /// HasSubsectionsViaSymbols - True if this target has the MachO
60    /// .subsections_via_symbols directive.
61    bool HasSubsectionsViaSymbols;           // Default is false.
62
63    /// HasMachoZeroFillDirective - True if this is a MachO target that supports
64    /// the macho-specific .zerofill directive for emitting BSS Symbols.
65    bool HasMachoZeroFillDirective;               // Default is false.
66
67    /// HasMachoTBSSDirective - True if this is a MachO target that supports
68    /// the macho-specific .tbss directive for emitting thread local BSS Symbols
69    bool HasMachoTBSSDirective;                 // Default is false.
70
71    /// HasStaticCtorDtorReferenceInStaticMode - True if the compiler should
72    /// emit a ".reference .constructors_used" or ".reference .destructors_used"
73    /// directive after the a static ctor/dtor list.  This directive is only
74    /// emitted in Static relocation model.
75    bool HasStaticCtorDtorReferenceInStaticMode;  // Default is false.
76
77    /// LinkerRequiresNonEmptyDwarfLines - True if the linker has a bug and
78    /// requires that the debug_line section be of a minimum size. In practice
79    /// such a linker requires a non empty line sequence if a file is present.
80    bool LinkerRequiresNonEmptyDwarfLines; // Default to false.
81
82    /// MaxInstLength - This is the maximum possible length of an instruction,
83    /// which is needed to compute the size of an inline asm.
84    unsigned MaxInstLength;                  // Defaults to 4.
85
86    /// PCSymbol - The symbol used to represent the current PC.  Used in PC
87    /// relative expressions.
88    const char *PCSymbol;                    // Defaults to "$".
89
90    /// SeparatorString - This string, if specified, is used to separate
91    /// instructions from each other when on the same line.
92    const char *SeparatorString;             // Defaults to ';'
93
94    /// CommentColumn - This indicates the comment num (zero-based) at
95    /// which asm comments should be printed.
96    unsigned CommentColumn;                  // Defaults to 40
97
98    /// CommentString - This indicates the comment character used by the
99    /// assembler.
100    const char *CommentString;               // Defaults to "#"
101
102    /// LabelSuffix - This is appended to emitted labels.
103    const char *LabelSuffix;                 // Defaults to ":"
104
105    /// LabelSuffix - This is appended to emitted labels.
106    const char *DebugLabelSuffix;                 // Defaults to ":"
107
108    /// GlobalPrefix - If this is set to a non-empty string, it is prepended
109    /// onto all global symbols.  This is often used for "_" or ".".
110    const char *GlobalPrefix;                // Defaults to ""
111
112    /// PrivateGlobalPrefix - This prefix is used for globals like constant
113    /// pool entries that are completely private to the .s file and should not
114    /// have names in the .o file.  This is often "." or "L".
115    const char *PrivateGlobalPrefix;         // Defaults to "."
116
117    /// LinkerPrivateGlobalPrefix - This prefix is used for symbols that should
118    /// be passed through the assembler but be removed by the linker.  This
119    /// is "l" on Darwin, currently used for some ObjC metadata.
120    const char *LinkerPrivateGlobalPrefix;   // Defaults to ""
121
122    /// InlineAsmStart/End - If these are nonempty, they contain a directive to
123    /// emit before and after an inline assembly statement.
124    const char *InlineAsmStart;              // Defaults to "#APP\n"
125    const char *InlineAsmEnd;                // Defaults to "#NO_APP\n"
126
127    /// Code16Directive, Code32Directive, Code64Directive - These are assembly
128    /// directives that tells the assembler to interpret the following
129    /// instructions differently.
130    const char *Code16Directive;             // Defaults to ".code16"
131    const char *Code32Directive;             // Defaults to ".code32"
132    const char *Code64Directive;             // Defaults to ".code64"
133
134    /// AssemblerDialect - Which dialect of an assembler variant to use.
135    unsigned AssemblerDialect;               // Defaults to 0
136
137    /// AllowQuotesInName - This is true if the assembler allows for complex
138    /// symbol names to be surrounded in quotes.  This defaults to false.
139    bool AllowQuotesInName;
140
141    /// AllowNameToStartWithDigit - This is true if the assembler allows symbol
142    /// names to start with a digit (e.g., "0x0021").  This defaults to false.
143    bool AllowNameToStartWithDigit;
144
145    /// AllowPeriodsInName - This is true if the assembler allows periods in
146    /// symbol names.  This defaults to true.
147    bool AllowPeriodsInName;
148
149    /// AllowUTF8 - This is true if the assembler accepts UTF-8 input.
150    // FIXME: Make this a more general encoding setting?
151    bool AllowUTF8;
152
153    /// UseDataRegionDirectives - This is true if data region markers should
154    /// be printed as ".data_region/.end_data_region" directives. If false,
155    /// use "$d/$a" labels instead.
156    bool UseDataRegionDirectives;
157
158    //===--- Data Emission Directives -------------------------------------===//
159
160    /// ZeroDirective - this should be set to the directive used to get some
161    /// number of zero bytes emitted to the current section.  Common cases are
162    /// "\t.zero\t" and "\t.space\t".  If this is set to null, the
163    /// Data*bitsDirective's will be used to emit zero bytes.
164    const char *ZeroDirective;               // Defaults to "\t.zero\t"
165
166    /// AsciiDirective - This directive allows emission of an ascii string with
167    /// the standard C escape characters embedded into it.
168    const char *AsciiDirective;              // Defaults to "\t.ascii\t"
169
170    /// AscizDirective - If not null, this allows for special handling of
171    /// zero terminated strings on this target.  This is commonly supported as
172    /// ".asciz".  If a target doesn't support this, it can be set to null.
173    const char *AscizDirective;              // Defaults to "\t.asciz\t"
174
175    /// DataDirectives - These directives are used to output some unit of
176    /// integer data to the current section.  If a data directive is set to
177    /// null, smaller data directives will be used to emit the large sizes.
178    const char *Data8bitsDirective;          // Defaults to "\t.byte\t"
179    const char *Data16bitsDirective;         // Defaults to "\t.short\t"
180    const char *Data32bitsDirective;         // Defaults to "\t.long\t"
181    const char *Data64bitsDirective;         // Defaults to "\t.quad\t"
182
183    /// GPRel64Directive - if non-null, a directive that is used to emit a word
184    /// which should be relocated as a 64-bit GP-relative offset, e.g. .gpdword
185    /// on Mips.
186    const char *GPRel64Directive;            // Defaults to NULL.
187
188    /// GPRel32Directive - if non-null, a directive that is used to emit a word
189    /// which should be relocated as a 32-bit GP-relative offset, e.g. .gpword
190    /// on Mips or .gprel32 on Alpha.
191    const char *GPRel32Directive;            // Defaults to NULL.
192
193    /// getDataASDirective - Return the directive that should be used to emit
194    /// data of the specified size to the specified numeric address space.
195    virtual const char *getDataASDirective(unsigned Size, unsigned AS) const {
196      assert(AS != 0 && "Don't know the directives for default addr space");
197      return 0;
198    }
199
200    /// SunStyleELFSectionSwitchSyntax - This is true if this target uses "Sun
201    /// Style" syntax for section switching ("#alloc,#write" etc) instead of the
202    /// normal ELF syntax (,"a,w") in .section directives.
203    bool SunStyleELFSectionSwitchSyntax;     // Defaults to false.
204
205    /// UsesELFSectionDirectiveForBSS - This is true if this target uses ELF
206    /// '.section' directive before the '.bss' one. It's used for PPC/Linux
207    /// which doesn't support the '.bss' directive only.
208    bool UsesELFSectionDirectiveForBSS;      // Defaults to false.
209
210    /// HasMicrosoftFastStdCallMangling - True if this target uses microsoft
211    /// style mangling for functions with X86_StdCall/X86_FastCall calling
212    /// convention.
213    bool HasMicrosoftFastStdCallMangling;    // Defaults to false.
214
215    //===--- Alignment Information ----------------------------------------===//
216
217    /// AlignDirective - The directive used to emit round up to an alignment
218    /// boundary.
219    ///
220    const char *AlignDirective;              // Defaults to "\t.align\t"
221
222    /// AlignmentIsInBytes - If this is true (the default) then the asmprinter
223    /// emits ".align N" directives, where N is the number of bytes to align to.
224    /// Otherwise, it emits ".align log2(N)", e.g. 3 to align to an 8 byte
225    /// boundary.
226    bool AlignmentIsInBytes;                 // Defaults to true
227
228    /// TextAlignFillValue - If non-zero, this is used to fill the executable
229    /// space created as the result of a alignment directive.
230    unsigned TextAlignFillValue;             // Defaults to 0
231
232    //===--- Global Variable Emission Directives --------------------------===//
233
234    /// GlobalDirective - This is the directive used to declare a global entity.
235    ///
236    const char *GlobalDirective;             // Defaults to NULL.
237
238    /// ExternDirective - This is the directive used to declare external
239    /// globals.
240    ///
241    const char *ExternDirective;             // Defaults to NULL.
242
243    /// HasSetDirective - True if the assembler supports the .set directive.
244    bool HasSetDirective;                    // Defaults to true.
245
246    /// HasAggressiveSymbolFolding - False if the assembler requires that we use
247    /// Lc = a - b
248    /// .long Lc
249    /// instead of
250    /// .long a - b
251    bool HasAggressiveSymbolFolding;           // Defaults to true.
252
253    /// COMMDirectiveAlignmentIsInBytes - True is .comm's and .lcomms optional
254    /// alignment is to be specified in bytes instead of log2(n).
255    bool COMMDirectiveAlignmentIsInBytes;    // Defaults to true;
256
257    /// LCOMMDirectiveAlignment - Describes if the .lcomm directive for the
258    /// target supports an alignment argument and how it is interpreted.
259    LCOMM::LCOMMType LCOMMDirectiveAlignmentType; // Defaults to NoAlignment.
260
261    /// HasDotTypeDotSizeDirective - True if the target has .type and .size
262    /// directives, this is true for most ELF targets.
263    bool HasDotTypeDotSizeDirective;         // Defaults to true.
264
265    /// HasSingleParameterDotFile - True if the target has a single parameter
266    /// .file directive, this is true for ELF targets.
267    bool HasSingleParameterDotFile;          // Defaults to true.
268
269    /// HasNoDeadStrip - True if this target supports the MachO .no_dead_strip
270    /// directive.
271    bool HasNoDeadStrip;                     // Defaults to false.
272
273    /// HasSymbolResolver - True if this target supports the MachO
274    /// .symbol_resolver directive.
275    bool HasSymbolResolver;                     // Defaults to false.
276
277    /// WeakRefDirective - This directive, if non-null, is used to declare a
278    /// global as being a weak undefined symbol.
279    const char *WeakRefDirective;            // Defaults to NULL.
280
281    /// WeakDefDirective - This directive, if non-null, is used to declare a
282    /// global as being a weak defined symbol.
283    const char *WeakDefDirective;            // Defaults to NULL.
284
285    /// LinkOnceDirective - This directive, if non-null is used to declare a
286    /// global as being a weak defined symbol.  This is used on cygwin/mingw.
287    const char *LinkOnceDirective;           // Defaults to NULL.
288
289    /// HiddenVisibilityAttr - This attribute, if not MCSA_Invalid, is used to
290    /// declare a symbol as having hidden visibility.
291    MCSymbolAttr HiddenVisibilityAttr;       // Defaults to MCSA_Hidden.
292
293    /// HiddenDeclarationVisibilityAttr - This attribute, if not MCSA_Invalid,
294    /// is used to declare an undefined symbol as having hidden visibility.
295    MCSymbolAttr HiddenDeclarationVisibilityAttr;   // Defaults to MCSA_Hidden.
296
297
298    /// ProtectedVisibilityAttr - This attribute, if not MCSA_Invalid, is used
299    /// to declare a symbol as having protected visibility.
300    MCSymbolAttr ProtectedVisibilityAttr;    // Defaults to MCSA_Protected
301
302    //===--- Dwarf Emission Directives -----------------------------------===//
303
304    /// HasLEB128 - True if target asm supports leb128 directives.
305    bool HasLEB128;                          // Defaults to false.
306
307    /// SupportsDebugInformation - True if target supports emission of debugging
308    /// information.
309    bool SupportsDebugInformation;           // Defaults to false.
310
311    /// SupportsExceptionHandling - True if target supports exception handling.
312    ExceptionHandling::ExceptionsType ExceptionsType; // Defaults to None
313
314    /// DwarfUsesInlineInfoSection - True if DwarfDebugInlineSection is used to
315    /// encode inline subroutine information.
316    bool DwarfUsesInlineInfoSection;         // Defaults to false.
317
318    /// DwarfSectionOffsetDirective - Special section offset directive.
319    const char* DwarfSectionOffsetDirective; // Defaults to NULL
320
321    /// DwarfUsesRelocationsAcrossSections - True if Dwarf2 output generally
322    /// uses relocations for references to other .debug_* sections.
323    bool DwarfUsesRelocationsAcrossSections;
324
325    /// DwarfRegNumForCFI - True if dwarf register numbers are printed
326    /// instead of symbolic register names in .cfi_* directives.
327    bool DwarfRegNumForCFI;  // Defaults to false;
328
329    //===--- Prologue State ----------------------------------------------===//
330
331    std::vector<MachineMove> InitialFrameState;
332
333  public:
334    explicit MCAsmInfo();
335    virtual ~MCAsmInfo();
336
337    // FIXME: move these methods to DwarfPrinter when the JIT stops using them.
338    static unsigned getSLEB128Size(int Value);
339    static unsigned getULEB128Size(unsigned Value);
340
341    /// getPointerSize - Get the pointer size in bytes.
342    unsigned getPointerSize() const {
343      return PointerSize;
344    }
345
346    /// islittleendian - True if the target is little endian.
347    bool isLittleEndian() const {
348      return IsLittleEndian;
349    }
350
351    /// isStackGrowthDirectionUp - True if target stack grow up.
352    bool isStackGrowthDirectionUp() const {
353      return StackGrowsUp;
354    }
355
356    bool hasSubsectionsViaSymbols() const { return HasSubsectionsViaSymbols; }
357
358    // Data directive accessors.
359    //
360    const char *getData8bitsDirective(unsigned AS = 0) const {
361      return AS == 0 ? Data8bitsDirective : getDataASDirective(8, AS);
362    }
363    const char *getData16bitsDirective(unsigned AS = 0) const {
364      return AS == 0 ? Data16bitsDirective : getDataASDirective(16, AS);
365    }
366    const char *getData32bitsDirective(unsigned AS = 0) const {
367      return AS == 0 ? Data32bitsDirective : getDataASDirective(32, AS);
368    }
369    const char *getData64bitsDirective(unsigned AS = 0) const {
370      return AS == 0 ? Data64bitsDirective : getDataASDirective(64, AS);
371    }
372    const char *getGPRel64Directive() const { return GPRel64Directive; }
373    const char *getGPRel32Directive() const { return GPRel32Directive; }
374
375    /// getNonexecutableStackSection - Targets can implement this method to
376    /// specify a section to switch to if the translation unit doesn't have any
377    /// trampolines that require an executable stack.
378    virtual const MCSection *getNonexecutableStackSection(MCContext &Ctx) const{
379      return 0;
380    }
381
382    virtual const MCExpr *
383    getExprForPersonalitySymbol(const MCSymbol *Sym,
384                                unsigned Encoding,
385                                MCStreamer &Streamer) const;
386
387    const MCExpr *
388    getExprForFDESymbol(const MCSymbol *Sym,
389                        unsigned Encoding,
390                        MCStreamer &Streamer) const;
391
392    bool usesSunStyleELFSectionSwitchSyntax() const {
393      return SunStyleELFSectionSwitchSyntax;
394    }
395
396    bool usesELFSectionDirectiveForBSS() const {
397      return UsesELFSectionDirectiveForBSS;
398    }
399
400    bool hasMicrosoftFastStdCallMangling() const {
401      return HasMicrosoftFastStdCallMangling;
402    }
403
404    // Accessors.
405    //
406    bool hasMachoZeroFillDirective() const { return HasMachoZeroFillDirective; }
407    bool hasMachoTBSSDirective() const { return HasMachoTBSSDirective; }
408    bool hasStaticCtorDtorReferenceInStaticMode() const {
409      return HasStaticCtorDtorReferenceInStaticMode;
410    }
411    bool getLinkerRequiresNonEmptyDwarfLines() const {
412      return LinkerRequiresNonEmptyDwarfLines;
413    }
414    unsigned getMaxInstLength() const {
415      return MaxInstLength;
416    }
417    const char *getPCSymbol() const {
418      return PCSymbol;
419    }
420    const char *getSeparatorString() const {
421      return SeparatorString;
422    }
423    unsigned getCommentColumn() const {
424      return CommentColumn;
425    }
426    const char *getCommentString() const {
427      return CommentString;
428    }
429    const char *getLabelSuffix() const {
430      return LabelSuffix;
431    }
432
433    const char *getDebugLabelSuffix() const {
434      return DebugLabelSuffix;
435    }
436
437    const char *getGlobalPrefix() const {
438      return GlobalPrefix;
439    }
440    const char *getPrivateGlobalPrefix() const {
441      return PrivateGlobalPrefix;
442    }
443    const char *getLinkerPrivateGlobalPrefix() const {
444      return LinkerPrivateGlobalPrefix;
445    }
446    const char *getInlineAsmStart() const {
447      return InlineAsmStart;
448    }
449    const char *getInlineAsmEnd() const {
450      return InlineAsmEnd;
451    }
452    const char *getCode16Directive() const {
453      return Code16Directive;
454    }
455    const char *getCode32Directive() const {
456      return Code32Directive;
457    }
458    const char *getCode64Directive() const {
459      return Code64Directive;
460    }
461    unsigned getAssemblerDialect() const {
462      return AssemblerDialect;
463    }
464    bool doesAllowQuotesInName() const {
465      return AllowQuotesInName;
466    }
467    bool doesAllowNameToStartWithDigit() const {
468      return AllowNameToStartWithDigit;
469    }
470    bool doesAllowPeriodsInName() const {
471      return AllowPeriodsInName;
472    }
473    bool doesAllowUTF8() const {
474      return AllowUTF8;
475    }
476    bool doesSupportDataRegionDirectives() const {
477      return UseDataRegionDirectives;
478    }
479    const char *getZeroDirective() const {
480      return ZeroDirective;
481    }
482    const char *getAsciiDirective() const {
483      return AsciiDirective;
484    }
485    const char *getAscizDirective() const {
486      return AscizDirective;
487    }
488    const char *getAlignDirective() const {
489      return AlignDirective;
490    }
491    bool getAlignmentIsInBytes() const {
492      return AlignmentIsInBytes;
493    }
494    unsigned getTextAlignFillValue() const {
495      return TextAlignFillValue;
496    }
497    const char *getGlobalDirective() const {
498      return GlobalDirective;
499    }
500    const char *getExternDirective() const {
501      return ExternDirective;
502    }
503    bool hasSetDirective() const { return HasSetDirective; }
504    bool hasAggressiveSymbolFolding() const {
505      return HasAggressiveSymbolFolding;
506    }
507    bool getCOMMDirectiveAlignmentIsInBytes() const {
508      return COMMDirectiveAlignmentIsInBytes;
509    }
510    LCOMM::LCOMMType getLCOMMDirectiveAlignmentType() const {
511      return LCOMMDirectiveAlignmentType;
512    }
513    bool hasDotTypeDotSizeDirective() const {return HasDotTypeDotSizeDirective;}
514    bool hasSingleParameterDotFile() const { return HasSingleParameterDotFile; }
515    bool hasNoDeadStrip() const { return HasNoDeadStrip; }
516    bool hasSymbolResolver() const { return HasSymbolResolver; }
517    const char *getWeakRefDirective() const { return WeakRefDirective; }
518    const char *getWeakDefDirective() const { return WeakDefDirective; }
519    const char *getLinkOnceDirective() const { return LinkOnceDirective; }
520
521    MCSymbolAttr getHiddenVisibilityAttr() const { return HiddenVisibilityAttr;}
522    MCSymbolAttr getHiddenDeclarationVisibilityAttr() const {
523      return HiddenDeclarationVisibilityAttr;
524    }
525    MCSymbolAttr getProtectedVisibilityAttr() const {
526      return ProtectedVisibilityAttr;
527    }
528    bool hasLEB128() const {
529      return HasLEB128;
530    }
531    bool doesSupportDebugInformation() const {
532      return SupportsDebugInformation;
533    }
534    bool doesSupportExceptionHandling() const {
535      return ExceptionsType != ExceptionHandling::None;
536    }
537    ExceptionHandling::ExceptionsType getExceptionHandlingType() const {
538      return ExceptionsType;
539    }
540    bool isExceptionHandlingDwarf() const {
541      return
542        (ExceptionsType == ExceptionHandling::DwarfCFI ||
543         ExceptionsType == ExceptionHandling::ARM ||
544         ExceptionsType == ExceptionHandling::Win64);
545    }
546    bool doesDwarfUseInlineInfoSection() const {
547      return DwarfUsesInlineInfoSection;
548    }
549    const char *getDwarfSectionOffsetDirective() const {
550      return DwarfSectionOffsetDirective;
551    }
552    bool doesDwarfUseRelocationsAcrossSections() const {
553      return DwarfUsesRelocationsAcrossSections;
554    }
555    bool useDwarfRegNumForCFI() const {
556      return DwarfRegNumForCFI;
557    }
558
559    void addInitialFrameState(MCSymbol *label, const MachineLocation &D,
560                              const MachineLocation &S) {
561      InitialFrameState.push_back(MachineMove(label, D, S));
562    }
563    const std::vector<MachineMove> &getInitialFrameState() const {
564      return InitialFrameState;
565    }
566  };
567}
568
569#endif
570