1 //===- lib/MC/MCAsmStreamer.cpp - Text Assembly Output --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/MC/MCStreamer.h"
11 #include "llvm/MC/MCAsmInfo.h"
12 #include "llvm/MC/MCCodeEmitter.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCExpr.h"
15 #include "llvm/MC/MCFixupKindInfo.h"
16 #include "llvm/MC/MCInst.h"
17 #include "llvm/MC/MCInstPrinter.h"
18 #include "llvm/MC/MCSectionMachO.h"
19 #include "llvm/MC/MCSymbol.h"
20 #include "llvm/ADT/OwningPtr.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/ADT/StringExtras.h"
23 #include "llvm/ADT/Twine.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/MathExtras.h"
26 #include "llvm/Support/Format.h"
27 #include "llvm/Support/FormattedStream.h"
28 #include "llvm/Target/TargetAsmBackend.h"
29 #include "llvm/Target/TargetAsmInfo.h"
30 #include "llvm/Target/TargetLoweringObjectFile.h"
36 class MCAsmStreamer
: public MCStreamer
{
38 formatted_raw_ostream
&OS
;
41 OwningPtr
<MCInstPrinter
> InstPrinter
;
42 OwningPtr
<MCCodeEmitter
> Emitter
;
43 OwningPtr
<TargetAsmBackend
> AsmBackend
;
45 SmallString
<128> CommentToEmit
;
46 raw_svector_ostream CommentStream
;
48 unsigned IsVerboseAsm
: 1;
49 unsigned ShowInst
: 1;
53 enum EHSymbolFlags
{ EHGlobal
= 1,
54 EHWeakDefinition
= 1 << 1,
55 EHPrivateExtern
= 1 << 2 };
56 DenseMap
<const MCSymbol
*, unsigned> FlagMap
;
58 bool needsSet(const MCExpr
*Value
);
60 void EmitRegisterName(int64_t Register
);
63 MCAsmStreamer(MCContext
&Context
, formatted_raw_ostream
&os
,
64 bool isVerboseAsm
, bool useLoc
, bool useCFI
,
65 MCInstPrinter
*printer
, MCCodeEmitter
*emitter
,
66 TargetAsmBackend
*asmbackend
,
68 : MCStreamer(Context
), OS(os
), MAI(Context
.getAsmInfo()),
69 InstPrinter(printer
), Emitter(emitter
), AsmBackend(asmbackend
),
70 CommentStream(CommentToEmit
), IsVerboseAsm(isVerboseAsm
),
71 ShowInst(showInst
), UseLoc(useLoc
), UseCFI(useCFI
) {
72 if (InstPrinter
&& IsVerboseAsm
)
73 InstPrinter
->setCommentStream(CommentStream
);
77 inline void EmitEOL() {
78 // If we don't have any comments, just emit a \n.
85 void EmitCommentsAndEOL();
87 /// isVerboseAsm - Return true if this streamer supports verbose assembly at
89 virtual bool isVerboseAsm() const { return IsVerboseAsm
; }
91 /// hasRawTextSupport - We support EmitRawText.
92 virtual bool hasRawTextSupport() const { return true; }
94 /// AddComment - Add a comment that can be emitted to the generated .s
95 /// file if applicable as a QoI issue to make the output of the compiler
96 /// more readable. This only affects the MCAsmStreamer, and only when
97 /// verbose assembly output is enabled.
98 virtual void AddComment(const Twine
&T
);
100 /// AddEncodingComment - Add a comment showing the encoding of an instruction.
101 virtual void AddEncodingComment(const MCInst
&Inst
);
103 /// GetCommentOS - Return a raw_ostream that comments can be written to.
104 /// Unlike AddComment, you are required to terminate comments with \n if you
106 virtual raw_ostream
&GetCommentOS() {
108 return nulls(); // Discard comments unless in verbose asm mode.
109 return CommentStream
;
112 /// AddBlankLine - Emit a blank line to a .s file to pretty it up.
113 virtual void AddBlankLine() {
117 /// @name MCStreamer Interface
120 virtual void ChangeSection(const MCSection
*Section
);
122 virtual void InitSections() {
123 // FIXME, this is MachO specific, but the testsuite
125 SwitchSection(getContext().getMachOSection("__TEXT", "__text",
126 MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS
,
127 0, SectionKind::getText()));
130 virtual void EmitLabel(MCSymbol
*Symbol
);
131 virtual void EmitEHSymAttributes(const MCSymbol
*Symbol
,
133 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag
);
134 virtual void EmitThumbFunc(MCSymbol
*Func
);
136 virtual void EmitAssignment(MCSymbol
*Symbol
, const MCExpr
*Value
);
137 virtual void EmitWeakReference(MCSymbol
*Alias
, const MCSymbol
*Symbol
);
138 virtual void EmitDwarfAdvanceLineAddr(int64_t LineDelta
,
139 const MCSymbol
*LastLabel
,
140 const MCSymbol
*Label
);
141 virtual void EmitDwarfAdvanceFrameAddr(const MCSymbol
*LastLabel
,
142 const MCSymbol
*Label
);
144 virtual void EmitSymbolAttribute(MCSymbol
*Symbol
, MCSymbolAttr Attribute
);
146 virtual void EmitSymbolDesc(MCSymbol
*Symbol
, unsigned DescValue
);
147 virtual void BeginCOFFSymbolDef(const MCSymbol
*Symbol
);
148 virtual void EmitCOFFSymbolStorageClass(int StorageClass
);
149 virtual void EmitCOFFSymbolType(int Type
);
150 virtual void EndCOFFSymbolDef();
151 virtual void EmitELFSize(MCSymbol
*Symbol
, const MCExpr
*Value
);
152 virtual void EmitCommonSymbol(MCSymbol
*Symbol
, uint64_t Size
,
153 unsigned ByteAlignment
);
155 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
157 /// @param Symbol - The common symbol to emit.
158 /// @param Size - The size of the common symbol.
159 virtual void EmitLocalCommonSymbol(MCSymbol
*Symbol
, uint64_t Size
);
161 virtual void EmitZerofill(const MCSection
*Section
, MCSymbol
*Symbol
= 0,
162 unsigned Size
= 0, unsigned ByteAlignment
= 0);
164 virtual void EmitTBSSSymbol (const MCSection
*Section
, MCSymbol
*Symbol
,
165 uint64_t Size
, unsigned ByteAlignment
= 0);
167 virtual void EmitBytes(StringRef Data
, unsigned AddrSpace
);
169 virtual void EmitValueImpl(const MCExpr
*Value
, unsigned Size
,
171 virtual void EmitIntValue(uint64_t Value
, unsigned Size
,
172 unsigned AddrSpace
= 0);
174 virtual void EmitULEB128Value(const MCExpr
*Value
);
176 virtual void EmitSLEB128Value(const MCExpr
*Value
);
178 virtual void EmitGPRel32Value(const MCExpr
*Value
);
181 virtual void EmitFill(uint64_t NumBytes
, uint8_t FillValue
,
184 virtual void EmitValueToAlignment(unsigned ByteAlignment
, int64_t Value
= 0,
185 unsigned ValueSize
= 1,
186 unsigned MaxBytesToEmit
= 0);
188 virtual void EmitCodeAlignment(unsigned ByteAlignment
,
189 unsigned MaxBytesToEmit
= 0);
191 virtual void EmitValueToOffset(const MCExpr
*Offset
,
192 unsigned char Value
= 0);
194 virtual void EmitFileDirective(StringRef Filename
);
195 virtual bool EmitDwarfFileDirective(unsigned FileNo
, StringRef Filename
);
196 virtual void EmitDwarfLocDirective(unsigned FileNo
, unsigned Line
,
197 unsigned Column
, unsigned Flags
,
198 unsigned Isa
, unsigned Discriminator
,
201 virtual void EmitCFISections(bool EH
, bool Debug
);
202 virtual void EmitCFIStartProc();
203 virtual void EmitCFIEndProc();
204 virtual void EmitCFIDefCfa(int64_t Register
, int64_t Offset
);
205 virtual void EmitCFIDefCfaOffset(int64_t Offset
);
206 virtual void EmitCFIDefCfaRegister(int64_t Register
);
207 virtual void EmitCFIOffset(int64_t Register
, int64_t Offset
);
208 virtual void EmitCFIPersonality(const MCSymbol
*Sym
, unsigned Encoding
);
209 virtual void EmitCFILsda(const MCSymbol
*Sym
, unsigned Encoding
);
210 virtual void EmitCFIRememberState();
211 virtual void EmitCFIRestoreState();
212 virtual void EmitCFISameValue(int64_t Register
);
213 virtual void EmitCFIRelOffset(int64_t Register
, int64_t Offset
);
214 virtual void EmitCFIAdjustCfaOffset(int64_t Adjustment
);
216 virtual void EmitWin64EHStartProc(const MCSymbol
*Symbol
);
217 virtual void EmitWin64EHEndProc();
218 virtual void EmitWin64EHStartChained();
219 virtual void EmitWin64EHEndChained();
220 virtual void EmitWin64EHHandler(const MCSymbol
*Sym
, bool Unwind
,
222 virtual void EmitWin64EHHandlerData();
223 virtual void EmitWin64EHPushReg(unsigned Register
);
224 virtual void EmitWin64EHSetFrame(unsigned Register
, unsigned Offset
);
225 virtual void EmitWin64EHAllocStack(unsigned Size
);
226 virtual void EmitWin64EHSaveReg(unsigned Register
, unsigned Offset
);
227 virtual void EmitWin64EHSaveXMM(unsigned Register
, unsigned Offset
);
228 virtual void EmitWin64EHPushFrame(bool Code
);
229 virtual void EmitWin64EHEndProlog();
231 virtual void EmitFnStart();
232 virtual void EmitFnEnd();
233 virtual void EmitCantUnwind();
234 virtual void EmitPersonality(const MCSymbol
*Personality
);
235 virtual void EmitHandlerData();
236 virtual void EmitSetFP(unsigned FpReg
, unsigned SpReg
, int64_t Offset
= 0);
237 virtual void EmitPad(int64_t Offset
);
238 virtual void EmitRegSave(const SmallVectorImpl
<unsigned> &RegList
, bool);
241 virtual void EmitInstruction(const MCInst
&Inst
);
243 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
244 /// the specified string in the output .s file. This capability is
245 /// indicated by the hasRawTextSupport() predicate.
246 virtual void EmitRawText(StringRef String
);
248 virtual void Finish();
253 } // end anonymous namespace.
255 /// AddComment - Add a comment that can be emitted to the generated .s
256 /// file if applicable as a QoI issue to make the output of the compiler
257 /// more readable. This only affects the MCAsmStreamer, and only when
258 /// verbose assembly output is enabled.
259 void MCAsmStreamer::AddComment(const Twine
&T
) {
260 if (!IsVerboseAsm
) return;
262 // Make sure that CommentStream is flushed.
263 CommentStream
.flush();
265 T
.toVector(CommentToEmit
);
266 // Each comment goes on its own line.
267 CommentToEmit
.push_back('\n');
269 // Tell the comment stream that the vector changed underneath it.
270 CommentStream
.resync();
273 void MCAsmStreamer::EmitCommentsAndEOL() {
274 if (CommentToEmit
.empty() && CommentStream
.GetNumBytesInBuffer() == 0) {
279 CommentStream
.flush();
280 StringRef Comments
= CommentToEmit
.str();
282 assert(Comments
.back() == '\n' &&
283 "Comment array not newline terminated");
285 // Emit a line of comments.
286 OS
.PadToColumn(MAI
.getCommentColumn());
287 size_t Position
= Comments
.find('\n');
288 OS
<< MAI
.getCommentString() << ' ' << Comments
.substr(0, Position
) << '\n';
290 Comments
= Comments
.substr(Position
+1);
291 } while (!Comments
.empty());
293 CommentToEmit
.clear();
294 // Tell the comment stream that the vector changed underneath it.
295 CommentStream
.resync();
298 static inline int64_t truncateToSize(int64_t Value
, unsigned Bytes
) {
299 assert(Bytes
&& "Invalid size!");
300 return Value
& ((uint64_t) (int64_t) -1 >> (64 - Bytes
* 8));
303 void MCAsmStreamer::ChangeSection(const MCSection
*Section
) {
304 assert(Section
&& "Cannot switch to a null section!");
305 Section
->PrintSwitchToSection(MAI
, OS
);
308 void MCAsmStreamer::EmitEHSymAttributes(const MCSymbol
*Symbol
,
309 MCSymbol
*EHSymbol
) {
313 unsigned Flags
= FlagMap
.lookup(Symbol
);
315 if (Flags
& EHGlobal
)
316 EmitSymbolAttribute(EHSymbol
, MCSA_Global
);
317 if (Flags
& EHWeakDefinition
)
318 EmitSymbolAttribute(EHSymbol
, MCSA_WeakDefinition
);
319 if (Flags
& EHPrivateExtern
)
320 EmitSymbolAttribute(EHSymbol
, MCSA_PrivateExtern
);
323 void MCAsmStreamer::EmitLabel(MCSymbol
*Symbol
) {
324 assert(Symbol
->isUndefined() && "Cannot define a symbol twice!");
325 MCStreamer::EmitLabel(Symbol
);
327 OS
<< *Symbol
<< MAI
.getLabelSuffix();
331 void MCAsmStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag
) {
333 default: assert(0 && "Invalid flag!");
334 case MCAF_SyntaxUnified
: OS
<< "\t.syntax unified"; break;
335 case MCAF_SubsectionsViaSymbols
: OS
<< ".subsections_via_symbols"; break;
336 case MCAF_Code16
: OS
<< "\t.code\t16"; break;
337 case MCAF_Code32
: OS
<< "\t.code\t32"; break;
342 void MCAsmStreamer::EmitThumbFunc(MCSymbol
*Func
) {
343 // This needs to emit to a temporary string to get properly quoted
344 // MCSymbols when they have spaces in them.
345 OS
<< "\t.thumb_func";
346 // Only Mach-O hasSubsectionsViaSymbols()
347 if (MAI
.hasSubsectionsViaSymbols())
352 void MCAsmStreamer::EmitAssignment(MCSymbol
*Symbol
, const MCExpr
*Value
) {
353 OS
<< *Symbol
<< " = " << *Value
;
356 // FIXME: Lift context changes into super class.
357 Symbol
->setVariableValue(Value
);
360 void MCAsmStreamer::EmitWeakReference(MCSymbol
*Alias
, const MCSymbol
*Symbol
) {
361 OS
<< ".weakref " << *Alias
<< ", " << *Symbol
;
365 void MCAsmStreamer::EmitDwarfAdvanceLineAddr(int64_t LineDelta
,
366 const MCSymbol
*LastLabel
,
367 const MCSymbol
*Label
) {
368 EmitDwarfSetLineAddr(LineDelta
, Label
,
369 getContext().getTargetAsmInfo().getPointerSize());
372 void MCAsmStreamer::EmitDwarfAdvanceFrameAddr(const MCSymbol
*LastLabel
,
373 const MCSymbol
*Label
) {
374 EmitIntValue(dwarf::DW_CFA_advance_loc4
, 1);
375 const MCExpr
*AddrDelta
= BuildSymbolDiff(getContext(), Label
, LastLabel
);
376 AddrDelta
= ForceExpAbs(AddrDelta
);
377 EmitValue(AddrDelta
, 4);
381 void MCAsmStreamer::EmitSymbolAttribute(MCSymbol
*Symbol
,
382 MCSymbolAttr Attribute
) {
384 case MCSA_Invalid
: assert(0 && "Invalid symbol attribute");
385 case MCSA_ELF_TypeFunction
: /// .type _foo, STT_FUNC # aka @function
386 case MCSA_ELF_TypeIndFunction
: /// .type _foo, STT_GNU_IFUNC
387 case MCSA_ELF_TypeObject
: /// .type _foo, STT_OBJECT # aka @object
388 case MCSA_ELF_TypeTLS
: /// .type _foo, STT_TLS # aka @tls_object
389 case MCSA_ELF_TypeCommon
: /// .type _foo, STT_COMMON # aka @common
390 case MCSA_ELF_TypeNoType
: /// .type _foo, STT_NOTYPE # aka @notype
391 case MCSA_ELF_TypeGnuUniqueObject
: /// .type _foo, @gnu_unique_object
392 assert(MAI
.hasDotTypeDotSizeDirective() && "Symbol Attr not supported");
393 OS
<< "\t.type\t" << *Symbol
<< ','
394 << ((MAI
.getCommentString()[0] != '@') ? '@' : '%');
396 default: assert(0 && "Unknown ELF .type");
397 case MCSA_ELF_TypeFunction
: OS
<< "function"; break;
398 case MCSA_ELF_TypeIndFunction
: OS
<< "gnu_indirect_function"; break;
399 case MCSA_ELF_TypeObject
: OS
<< "object"; break;
400 case MCSA_ELF_TypeTLS
: OS
<< "tls_object"; break;
401 case MCSA_ELF_TypeCommon
: OS
<< "common"; break;
402 case MCSA_ELF_TypeNoType
: OS
<< "no_type"; break;
403 case MCSA_ELF_TypeGnuUniqueObject
: OS
<< "gnu_unique_object"; break;
407 case MCSA_Global
: // .globl/.global
408 OS
<< MAI
.getGlobalDirective();
409 FlagMap
[Symbol
] |= EHGlobal
;
411 case MCSA_Hidden
: OS
<< "\t.hidden\t"; break;
412 case MCSA_IndirectSymbol
: OS
<< "\t.indirect_symbol\t"; break;
413 case MCSA_Internal
: OS
<< "\t.internal\t"; break;
414 case MCSA_LazyReference
: OS
<< "\t.lazy_reference\t"; break;
415 case MCSA_Local
: OS
<< "\t.local\t"; break;
416 case MCSA_NoDeadStrip
: OS
<< "\t.no_dead_strip\t"; break;
417 case MCSA_SymbolResolver
: OS
<< "\t.symbol_resolver\t"; break;
418 case MCSA_PrivateExtern
:
419 OS
<< "\t.private_extern\t";
420 FlagMap
[Symbol
] |= EHPrivateExtern
;
422 case MCSA_Protected
: OS
<< "\t.protected\t"; break;
423 case MCSA_Reference
: OS
<< "\t.reference\t"; break;
424 case MCSA_Weak
: OS
<< "\t.weak\t"; break;
425 case MCSA_WeakDefinition
:
426 OS
<< "\t.weak_definition\t";
427 FlagMap
[Symbol
] |= EHWeakDefinition
;
430 case MCSA_WeakReference
: OS
<< MAI
.getWeakRefDirective(); break;
431 case MCSA_WeakDefAutoPrivate
: OS
<< "\t.weak_def_can_be_hidden\t"; break;
438 void MCAsmStreamer::EmitSymbolDesc(MCSymbol
*Symbol
, unsigned DescValue
) {
439 OS
<< ".desc" << ' ' << *Symbol
<< ',' << DescValue
;
443 void MCAsmStreamer::BeginCOFFSymbolDef(const MCSymbol
*Symbol
) {
444 OS
<< "\t.def\t " << *Symbol
<< ';';
448 void MCAsmStreamer::EmitCOFFSymbolStorageClass (int StorageClass
) {
449 OS
<< "\t.scl\t" << StorageClass
<< ';';
453 void MCAsmStreamer::EmitCOFFSymbolType (int Type
) {
454 OS
<< "\t.type\t" << Type
<< ';';
458 void MCAsmStreamer::EndCOFFSymbolDef() {
463 void MCAsmStreamer::EmitELFSize(MCSymbol
*Symbol
, const MCExpr
*Value
) {
464 assert(MAI
.hasDotTypeDotSizeDirective());
465 OS
<< "\t.size\t" << *Symbol
<< ", " << *Value
<< '\n';
468 void MCAsmStreamer::EmitCommonSymbol(MCSymbol
*Symbol
, uint64_t Size
,
469 unsigned ByteAlignment
) {
470 OS
<< "\t.comm\t" << *Symbol
<< ',' << Size
;
471 if (ByteAlignment
!= 0) {
472 if (MAI
.getCOMMDirectiveAlignmentIsInBytes())
473 OS
<< ',' << ByteAlignment
;
475 OS
<< ',' << Log2_32(ByteAlignment
);
480 /// EmitLocalCommonSymbol - Emit a local common (.lcomm) symbol.
482 /// @param Symbol - The common symbol to emit.
483 /// @param Size - The size of the common symbol.
484 void MCAsmStreamer::EmitLocalCommonSymbol(MCSymbol
*Symbol
, uint64_t Size
) {
485 assert(MAI
.hasLCOMMDirective() && "Doesn't have .lcomm, can't emit it!");
486 OS
<< "\t.lcomm\t" << *Symbol
<< ',' << Size
;
490 void MCAsmStreamer::EmitZerofill(const MCSection
*Section
, MCSymbol
*Symbol
,
491 unsigned Size
, unsigned ByteAlignment
) {
492 // Note: a .zerofill directive does not switch sections.
495 // This is a mach-o specific directive.
496 const MCSectionMachO
*MOSection
= ((const MCSectionMachO
*)Section
);
497 OS
<< MOSection
->getSegmentName() << "," << MOSection
->getSectionName();
499 if (Symbol
!= NULL
) {
500 OS
<< ',' << *Symbol
<< ',' << Size
;
501 if (ByteAlignment
!= 0)
502 OS
<< ',' << Log2_32(ByteAlignment
);
507 // .tbss sym, size, align
508 // This depends that the symbol has already been mangled from the original,
510 void MCAsmStreamer::EmitTBSSSymbol(const MCSection
*Section
, MCSymbol
*Symbol
,
511 uint64_t Size
, unsigned ByteAlignment
) {
512 assert(Symbol
!= NULL
&& "Symbol shouldn't be NULL!");
513 // Instead of using the Section we'll just use the shortcut.
514 // This is a mach-o specific directive and section.
515 OS
<< ".tbss " << *Symbol
<< ", " << Size
;
517 // Output align if we have it. We default to 1 so don't bother printing
519 if (ByteAlignment
> 1) OS
<< ", " << Log2_32(ByteAlignment
);
524 static inline char toOctal(int X
) { return (X
&7)+'0'; }
526 static void PrintQuotedString(StringRef Data
, raw_ostream
&OS
) {
529 for (unsigned i
= 0, e
= Data
.size(); i
!= e
; ++i
) {
530 unsigned char C
= Data
[i
];
531 if (C
== '"' || C
== '\\') {
532 OS
<< '\\' << (char)C
;
536 if (isprint((unsigned char)C
)) {
542 case '\b': OS
<< "\\b"; break;
543 case '\f': OS
<< "\\f"; break;
544 case '\n': OS
<< "\\n"; break;
545 case '\r': OS
<< "\\r"; break;
546 case '\t': OS
<< "\\t"; break;
549 OS
<< toOctal(C
>> 6);
550 OS
<< toOctal(C
>> 3);
551 OS
<< toOctal(C
>> 0);
560 void MCAsmStreamer::EmitBytes(StringRef Data
, unsigned AddrSpace
) {
561 assert(getCurrentSection() && "Cannot emit contents before setting section!");
562 if (Data
.empty()) return;
564 if (Data
.size() == 1) {
565 OS
<< MAI
.getData8bitsDirective(AddrSpace
);
566 OS
<< (unsigned)(unsigned char)Data
[0];
571 // If the data ends with 0 and the target supports .asciz, use it, otherwise
573 if (MAI
.getAscizDirective() && Data
.back() == 0) {
574 OS
<< MAI
.getAscizDirective();
575 Data
= Data
.substr(0, Data
.size()-1);
577 OS
<< MAI
.getAsciiDirective();
581 PrintQuotedString(Data
, OS
);
585 void MCAsmStreamer::EmitIntValue(uint64_t Value
, unsigned Size
,
586 unsigned AddrSpace
) {
587 EmitValue(MCConstantExpr::Create(Value
, getContext()), Size
, AddrSpace
);
590 void MCAsmStreamer::EmitValueImpl(const MCExpr
*Value
, unsigned Size
,
591 unsigned AddrSpace
) {
592 assert(getCurrentSection() && "Cannot emit contents before setting section!");
593 const char *Directive
= 0;
596 case 1: Directive
= MAI
.getData8bitsDirective(AddrSpace
); break;
597 case 2: Directive
= MAI
.getData16bitsDirective(AddrSpace
); break;
598 case 4: Directive
= MAI
.getData32bitsDirective(AddrSpace
); break;
600 Directive
= MAI
.getData64bitsDirective(AddrSpace
);
601 // If the target doesn't support 64-bit data, emit as two 32-bit halves.
602 if (Directive
) break;
604 if (!Value
->EvaluateAsAbsolute(IntValue
))
605 report_fatal_error("Don't know how to emit this value.");
606 if (getContext().getTargetAsmInfo().isLittleEndian()) {
607 EmitIntValue((uint32_t)(IntValue
>> 0 ), 4, AddrSpace
);
608 EmitIntValue((uint32_t)(IntValue
>> 32), 4, AddrSpace
);
610 EmitIntValue((uint32_t)(IntValue
>> 32), 4, AddrSpace
);
611 EmitIntValue((uint32_t)(IntValue
>> 0 ), 4, AddrSpace
);
616 assert(Directive
&& "Invalid size for machine code value!");
617 OS
<< Directive
<< *Value
;
621 void MCAsmStreamer::EmitULEB128Value(const MCExpr
*Value
) {
623 if (Value
->EvaluateAsAbsolute(IntValue
)) {
624 EmitULEB128IntValue(IntValue
);
627 assert(MAI
.hasLEB128() && "Cannot print a .uleb");
628 OS
<< ".uleb128 " << *Value
;
632 void MCAsmStreamer::EmitSLEB128Value(const MCExpr
*Value
) {
634 if (Value
->EvaluateAsAbsolute(IntValue
)) {
635 EmitSLEB128IntValue(IntValue
);
638 assert(MAI
.hasLEB128() && "Cannot print a .sleb");
639 OS
<< ".sleb128 " << *Value
;
643 void MCAsmStreamer::EmitGPRel32Value(const MCExpr
*Value
) {
644 assert(MAI
.getGPRel32Directive() != 0);
645 OS
<< MAI
.getGPRel32Directive() << *Value
;
650 /// EmitFill - Emit NumBytes bytes worth of the value specified by
651 /// FillValue. This implements directives such as '.space'.
652 void MCAsmStreamer::EmitFill(uint64_t NumBytes
, uint8_t FillValue
,
653 unsigned AddrSpace
) {
654 if (NumBytes
== 0) return;
657 if (const char *ZeroDirective
= MAI
.getZeroDirective()) {
658 OS
<< ZeroDirective
<< NumBytes
;
660 OS
<< ',' << (int)FillValue
;
665 // Emit a byte at a time.
666 MCStreamer::EmitFill(NumBytes
, FillValue
, AddrSpace
);
669 void MCAsmStreamer::EmitValueToAlignment(unsigned ByteAlignment
, int64_t Value
,
671 unsigned MaxBytesToEmit
) {
672 // Some assemblers don't support non-power of two alignments, so we always
673 // emit alignments as a power of two if possible.
674 if (isPowerOf2_32(ByteAlignment
)) {
676 default: llvm_unreachable("Invalid size for machine code value!");
677 case 1: OS
<< MAI
.getAlignDirective(); break;
678 // FIXME: use MAI for this!
679 case 2: OS
<< ".p2alignw "; break;
680 case 4: OS
<< ".p2alignl "; break;
681 case 8: llvm_unreachable("Unsupported alignment size!");
684 if (MAI
.getAlignmentIsInBytes())
687 OS
<< Log2_32(ByteAlignment
);
689 if (Value
|| MaxBytesToEmit
) {
691 OS
.write_hex(truncateToSize(Value
, ValueSize
));
694 OS
<< ", " << MaxBytesToEmit
;
700 // Non-power of two alignment. This is not widely supported by assemblers.
701 // FIXME: Parameterize this based on MAI.
703 default: llvm_unreachable("Invalid size for machine code value!");
704 case 1: OS
<< ".balign"; break;
705 case 2: OS
<< ".balignw"; break;
706 case 4: OS
<< ".balignl"; break;
707 case 8: llvm_unreachable("Unsupported alignment size!");
710 OS
<< ' ' << ByteAlignment
;
711 OS
<< ", " << truncateToSize(Value
, ValueSize
);
713 OS
<< ", " << MaxBytesToEmit
;
717 void MCAsmStreamer::EmitCodeAlignment(unsigned ByteAlignment
,
718 unsigned MaxBytesToEmit
) {
719 // Emit with a text fill value.
720 EmitValueToAlignment(ByteAlignment
, MAI
.getTextAlignFillValue(),
724 void MCAsmStreamer::EmitValueToOffset(const MCExpr
*Offset
,
725 unsigned char Value
) {
726 // FIXME: Verify that Offset is associated with the current section.
727 OS
<< ".org " << *Offset
<< ", " << (unsigned) Value
;
732 void MCAsmStreamer::EmitFileDirective(StringRef Filename
) {
733 assert(MAI
.hasSingleParameterDotFile());
735 PrintQuotedString(Filename
, OS
);
739 bool MCAsmStreamer::EmitDwarfFileDirective(unsigned FileNo
, StringRef Filename
){
741 OS
<< "\t.file\t" << FileNo
<< ' ';
742 PrintQuotedString(Filename
, OS
);
745 return this->MCStreamer::EmitDwarfFileDirective(FileNo
, Filename
);
748 void MCAsmStreamer::EmitDwarfLocDirective(unsigned FileNo
, unsigned Line
,
749 unsigned Column
, unsigned Flags
,
751 unsigned Discriminator
,
752 StringRef FileName
) {
753 this->MCStreamer::EmitDwarfLocDirective(FileNo
, Line
, Column
, Flags
,
754 Isa
, Discriminator
, FileName
);
758 OS
<< "\t.loc\t" << FileNo
<< " " << Line
<< " " << Column
;
759 if (Flags
& DWARF2_FLAG_BASIC_BLOCK
)
760 OS
<< " basic_block";
761 if (Flags
& DWARF2_FLAG_PROLOGUE_END
)
762 OS
<< " prologue_end";
763 if (Flags
& DWARF2_FLAG_EPILOGUE_BEGIN
)
764 OS
<< " epilogue_begin";
766 unsigned OldFlags
= getContext().getCurrentDwarfLoc().getFlags();
767 if ((Flags
& DWARF2_FLAG_IS_STMT
) != (OldFlags
& DWARF2_FLAG_IS_STMT
)) {
770 if (Flags
& DWARF2_FLAG_IS_STMT
)
779 OS
<< "discriminator " << Discriminator
;
782 OS
.PadToColumn(MAI
.getCommentColumn());
783 OS
<< MAI
.getCommentString() << ' ' << FileName
<< ':'
784 << Line
<< ':' << Column
;
789 void MCAsmStreamer::EmitCFISections(bool EH
, bool Debug
) {
790 MCStreamer::EmitCFISections(EH
, Debug
);
795 OS
<< "\t.cfi_sections ";
799 OS
<< ", .debug_frame";
801 OS
<< ".debug_frame";
807 void MCAsmStreamer::EmitCFIStartProc() {
808 MCStreamer::EmitCFIStartProc();
813 OS
<< "\t.cfi_startproc";
817 void MCAsmStreamer::EmitCFIEndProc() {
818 MCStreamer::EmitCFIEndProc();
823 OS
<< "\t.cfi_endproc";
827 void MCAsmStreamer::EmitRegisterName(int64_t Register
) {
828 if (InstPrinter
&& !MAI
.useDwarfRegNumForCFI()) {
829 const TargetAsmInfo
&asmInfo
= getContext().getTargetAsmInfo();
830 unsigned LLVMRegister
= asmInfo
.getLLVMRegNum(Register
, true);
831 InstPrinter
->printRegName(OS
, LLVMRegister
);
837 void MCAsmStreamer::EmitCFIDefCfa(int64_t Register
, int64_t Offset
) {
838 MCStreamer::EmitCFIDefCfa(Register
, Offset
);
843 OS
<< "\t.cfi_def_cfa ";
844 EmitRegisterName(Register
);
845 OS
<< ", " << Offset
;
849 void MCAsmStreamer::EmitCFIDefCfaOffset(int64_t Offset
) {
850 MCStreamer::EmitCFIDefCfaOffset(Offset
);
855 OS
<< "\t.cfi_def_cfa_offset " << Offset
;
859 void MCAsmStreamer::EmitCFIDefCfaRegister(int64_t Register
) {
860 MCStreamer::EmitCFIDefCfaRegister(Register
);
865 OS
<< "\t.cfi_def_cfa_register ";
866 EmitRegisterName(Register
);
870 void MCAsmStreamer::EmitCFIOffset(int64_t Register
, int64_t Offset
) {
871 this->MCStreamer::EmitCFIOffset(Register
, Offset
);
876 OS
<< "\t.cfi_offset ";
877 EmitRegisterName(Register
);
878 OS
<< ", " << Offset
;
882 void MCAsmStreamer::EmitCFIPersonality(const MCSymbol
*Sym
,
884 MCStreamer::EmitCFIPersonality(Sym
, Encoding
);
889 OS
<< "\t.cfi_personality " << Encoding
<< ", " << *Sym
;
893 void MCAsmStreamer::EmitCFILsda(const MCSymbol
*Sym
, unsigned Encoding
) {
894 MCStreamer::EmitCFILsda(Sym
, Encoding
);
899 OS
<< "\t.cfi_lsda " << Encoding
<< ", " << *Sym
;
903 void MCAsmStreamer::EmitCFIRememberState() {
904 MCStreamer::EmitCFIRememberState();
909 OS
<< "\t.cfi_remember_state";
913 void MCAsmStreamer::EmitCFIRestoreState() {
914 MCStreamer::EmitCFIRestoreState();
919 OS
<< "\t.cfi_restore_state";
923 void MCAsmStreamer::EmitCFISameValue(int64_t Register
) {
924 MCStreamer::EmitCFISameValue(Register
);
929 OS
<< "\t.cfi_same_value ";
930 EmitRegisterName(Register
);
934 void MCAsmStreamer::EmitCFIRelOffset(int64_t Register
, int64_t Offset
) {
935 MCStreamer::EmitCFIRelOffset(Register
, Offset
);
940 OS
<< "\t.cfi_rel_offset ";
941 EmitRegisterName(Register
);
942 OS
<< ", " << Offset
;
946 void MCAsmStreamer::EmitCFIAdjustCfaOffset(int64_t Adjustment
) {
947 MCStreamer::EmitCFIAdjustCfaOffset(Adjustment
);
952 OS
<< "\t.cfi_adjust_cfa_offset " << Adjustment
;
956 void MCAsmStreamer::EmitWin64EHStartProc(const MCSymbol
*Symbol
) {
957 MCStreamer::EmitWin64EHStartProc(Symbol
);
959 OS
<< ".seh_proc " << *Symbol
;
963 void MCAsmStreamer::EmitWin64EHEndProc() {
964 MCStreamer::EmitWin64EHEndProc();
966 OS
<< "\t.seh_endproc";
970 void MCAsmStreamer::EmitWin64EHStartChained() {
971 MCStreamer::EmitWin64EHStartChained();
973 OS
<< "\t.seh_startchained";
977 void MCAsmStreamer::EmitWin64EHEndChained() {
978 MCStreamer::EmitWin64EHEndChained();
980 OS
<< "\t.seh_endchained";
984 void MCAsmStreamer::EmitWin64EHHandler(const MCSymbol
*Sym
, bool Unwind
,
986 MCStreamer::EmitWin64EHHandler(Sym
, Unwind
, Except
);
988 OS
<< "\t.seh_handler " << *Sym
;
996 void MCAsmStreamer::EmitWin64EHHandlerData() {
997 MCStreamer::EmitWin64EHHandlerData();
999 // Switch sections. Don't call SwitchSection directly, because that will
1000 // cause the section switch to be visible in the emitted assembly.
1001 // We only do this so the section switch that terminates the handler
1002 // data block is visible.
1003 MCWin64EHUnwindInfo
*CurFrame
= getCurrentW64UnwindInfo();
1004 StringRef suffix
=MCWin64EHUnwindEmitter::GetSectionSuffix(CurFrame
->Function
);
1005 const MCSection
*xdataSect
=
1006 getContext().getTargetAsmInfo().getWin64EHTableSection(suffix
);
1008 SwitchSectionNoChange(xdataSect
);
1010 OS
<< "\t.seh_handlerdata";
1014 void MCAsmStreamer::EmitWin64EHPushReg(unsigned Register
) {
1015 MCStreamer::EmitWin64EHPushReg(Register
);
1017 OS
<< "\t.seh_pushreg " << Register
;
1021 void MCAsmStreamer::EmitWin64EHSetFrame(unsigned Register
, unsigned Offset
) {
1022 MCStreamer::EmitWin64EHSetFrame(Register
, Offset
);
1024 OS
<< "\t.seh_setframe " << Register
<< ", " << Offset
;
1028 void MCAsmStreamer::EmitWin64EHAllocStack(unsigned Size
) {
1029 MCStreamer::EmitWin64EHAllocStack(Size
);
1031 OS
<< "\t.seh_stackalloc " << Size
;
1035 void MCAsmStreamer::EmitWin64EHSaveReg(unsigned Register
, unsigned Offset
) {
1036 MCStreamer::EmitWin64EHSaveReg(Register
, Offset
);
1038 OS
<< "\t.seh_savereg " << Register
<< ", " << Offset
;
1042 void MCAsmStreamer::EmitWin64EHSaveXMM(unsigned Register
, unsigned Offset
) {
1043 MCStreamer::EmitWin64EHSaveXMM(Register
, Offset
);
1045 OS
<< "\t.seh_savexmm " << Register
<< ", " << Offset
;
1049 void MCAsmStreamer::EmitWin64EHPushFrame(bool Code
) {
1050 MCStreamer::EmitWin64EHPushFrame(Code
);
1052 OS
<< "\t.seh_pushframe";
1058 void MCAsmStreamer::EmitWin64EHEndProlog(void) {
1059 MCStreamer::EmitWin64EHEndProlog();
1061 OS
<< "\t.seh_endprologue";
1065 void MCAsmStreamer::AddEncodingComment(const MCInst
&Inst
) {
1066 raw_ostream
&OS
= GetCommentOS();
1067 SmallString
<256> Code
;
1068 SmallVector
<MCFixup
, 4> Fixups
;
1069 raw_svector_ostream
VecOS(Code
);
1070 Emitter
->EncodeInstruction(Inst
, VecOS
, Fixups
);
1073 // If we are showing fixups, create symbolic markers in the encoded
1074 // representation. We do this by making a per-bit map to the fixup item index,
1075 // then trying to display it as nicely as possible.
1076 SmallVector
<uint8_t, 64> FixupMap
;
1077 FixupMap
.resize(Code
.size() * 8);
1078 for (unsigned i
= 0, e
= Code
.size() * 8; i
!= e
; ++i
)
1081 for (unsigned i
= 0, e
= Fixups
.size(); i
!= e
; ++i
) {
1082 MCFixup
&F
= Fixups
[i
];
1083 const MCFixupKindInfo
&Info
= AsmBackend
->getFixupKindInfo(F
.getKind());
1084 for (unsigned j
= 0; j
!= Info
.TargetSize
; ++j
) {
1085 unsigned Index
= F
.getOffset() * 8 + Info
.TargetOffset
+ j
;
1086 assert(Index
< Code
.size() * 8 && "Invalid offset in fixup!");
1087 FixupMap
[Index
] = 1 + i
;
1091 // FIXME: Note the fixup comments for Thumb2 are completely bogus since the
1092 // high order halfword of a 32-bit Thumb2 instruction is emitted first.
1093 OS
<< "encoding: [";
1094 for (unsigned i
= 0, e
= Code
.size(); i
!= e
; ++i
) {
1098 // See if all bits are the same map entry.
1099 uint8_t MapEntry
= FixupMap
[i
* 8 + 0];
1100 for (unsigned j
= 1; j
!= 8; ++j
) {
1101 if (FixupMap
[i
* 8 + j
] == MapEntry
)
1104 MapEntry
= uint8_t(~0U);
1108 if (MapEntry
!= uint8_t(~0U)) {
1109 if (MapEntry
== 0) {
1110 OS
<< format("0x%02x", uint8_t(Code
[i
]));
1113 // FIXME: Some of the 8 bits require fix up.
1114 OS
<< format("0x%02x", uint8_t(Code
[i
])) << '\''
1115 << char('A' + MapEntry
- 1) << '\'';
1117 OS
<< char('A' + MapEntry
- 1);
1120 // Otherwise, write out in binary.
1122 for (unsigned j
= 8; j
--;) {
1123 unsigned Bit
= (Code
[i
] >> j
) & 1;
1126 if (getContext().getTargetAsmInfo().isLittleEndian())
1127 FixupBit
= i
* 8 + j
;
1129 FixupBit
= i
* 8 + (7-j
);
1131 if (uint8_t MapEntry
= FixupMap
[FixupBit
]) {
1132 assert(Bit
== 0 && "Encoder wrote into fixed up bit!");
1133 OS
<< char('A' + MapEntry
- 1);
1141 for (unsigned i
= 0, e
= Fixups
.size(); i
!= e
; ++i
) {
1142 MCFixup
&F
= Fixups
[i
];
1143 const MCFixupKindInfo
&Info
= AsmBackend
->getFixupKindInfo(F
.getKind());
1144 OS
<< " fixup " << char('A' + i
) << " - " << "offset: " << F
.getOffset()
1145 << ", value: " << *F
.getValue() << ", kind: " << Info
.Name
<< "\n";
1149 void MCAsmStreamer::EmitFnStart() {
1154 void MCAsmStreamer::EmitFnEnd() {
1159 void MCAsmStreamer::EmitCantUnwind() {
1160 OS
<< "\t.cantunwind";
1164 void MCAsmStreamer::EmitHandlerData() {
1165 OS
<< "\t.handlerdata";
1169 void MCAsmStreamer::EmitPersonality(const MCSymbol
*Personality
) {
1170 OS
<< "\t.personality " << Personality
->getName();
1174 void MCAsmStreamer::EmitSetFP(unsigned FpReg
, unsigned SpReg
, int64_t Offset
) {
1176 InstPrinter
->printRegName(OS
, FpReg
);
1178 InstPrinter
->printRegName(OS
, SpReg
);
1180 OS
<< ", #" << Offset
;
1184 void MCAsmStreamer::EmitPad(int64_t Offset
) {
1185 OS
<< "\t.pad\t#" << Offset
;
1189 void MCAsmStreamer::EmitRegSave(const SmallVectorImpl
<unsigned> &RegList
,
1191 assert(RegList
.size() && "RegList should not be empty");
1193 OS
<< "\t.vsave\t{";
1197 InstPrinter
->printRegName(OS
, RegList
[0]);
1199 for (unsigned i
= 1, e
= RegList
.size(); i
!= e
; ++i
) {
1201 InstPrinter
->printRegName(OS
, RegList
[i
]);
1208 void MCAsmStreamer::EmitInstruction(const MCInst
&Inst
) {
1209 assert(getCurrentSection() && "Cannot emit contents before setting section!");
1211 // Show the encoding in a comment if we have a code emitter.
1213 AddEncodingComment(Inst
);
1215 // Show the MCInst if enabled.
1217 Inst
.dump_pretty(GetCommentOS(), &MAI
, InstPrinter
.get(), "\n ");
1218 GetCommentOS() << "\n";
1221 // If we have an AsmPrinter, use that to print, otherwise print the MCInst.
1223 InstPrinter
->printInst(&Inst
, OS
);
1225 Inst
.print(OS
, &MAI
);
1229 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
1230 /// the specified string in the output .s file. This capability is
1231 /// indicated by the hasRawTextSupport() predicate.
1232 void MCAsmStreamer::EmitRawText(StringRef String
) {
1233 if (!String
.empty() && String
.back() == '\n')
1234 String
= String
.substr(0, String
.size()-1);
1239 void MCAsmStreamer::Finish() {
1240 // Dump out the dwarf file & directory tables and line tables.
1241 if (getContext().hasDwarfFiles() && !UseLoc
)
1242 MCDwarfFileTable::Emit(this);
1247 MCStreamer
*llvm::createAsmStreamer(MCContext
&Context
,
1248 formatted_raw_ostream
&OS
,
1249 bool isVerboseAsm
, bool useLoc
,
1250 bool useCFI
, MCInstPrinter
*IP
,
1251 MCCodeEmitter
*CE
, TargetAsmBackend
*TAB
,
1253 return new MCAsmStreamer(Context
, OS
, isVerboseAsm
, useLoc
, useCFI
,
1254 IP
, CE
, TAB
, ShowInst
);