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