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