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