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