1 //===- lib/MC/MCAsmStreamer.cpp - Text Assembly Output ----------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/SmallString.h"
10 #include "llvm/ADT/StringExtras.h"
11 #include "llvm/ADT/Twine.h"
12 #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
13 #include "llvm/MC/MCAsmBackend.h"
14 #include "llvm/MC/MCAsmInfo.h"
15 #include "llvm/MC/MCAssembler.h"
16 #include "llvm/MC/MCCodeEmitter.h"
17 #include "llvm/MC/MCCodeView.h"
18 #include "llvm/MC/MCContext.h"
19 #include "llvm/MC/MCExpr.h"
20 #include "llvm/MC/MCFixupKindInfo.h"
21 #include "llvm/MC/MCInst.h"
22 #include "llvm/MC/MCInstPrinter.h"
23 #include "llvm/MC/MCObjectFileInfo.h"
24 #include "llvm/MC/MCObjectWriter.h"
25 #include "llvm/MC/MCPseudoProbe.h"
26 #include "llvm/MC/MCRegister.h"
27 #include "llvm/MC/MCRegisterInfo.h"
28 #include "llvm/MC/MCSectionMachO.h"
29 #include "llvm/MC/MCStreamer.h"
30 #include "llvm/MC/MCSymbolXCOFF.h"
31 #include "llvm/MC/TargetRegistry.h"
32 #include "llvm/Support/Casting.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/Format.h"
35 #include "llvm/Support/FormattedStream.h"
36 #include "llvm/Support/LEB128.h"
37 #include "llvm/Support/MathExtras.h"
38 #include "llvm/Support/Path.h"
46 class MCAsmStreamer final
: public MCStreamer
{
47 std::unique_ptr
<formatted_raw_ostream
> OSOwner
;
48 formatted_raw_ostream
&OS
;
50 std::unique_ptr
<MCInstPrinter
> InstPrinter
;
51 std::unique_ptr
<MCAssembler
> Assembler
;
53 SmallString
<128> ExplicitCommentToEmit
;
54 SmallString
<128> CommentToEmit
;
55 raw_svector_ostream CommentStream
;
56 raw_null_ostream NullStream
;
58 unsigned IsVerboseAsm
: 1;
59 unsigned ShowInst
: 1;
60 unsigned UseDwarfDirectory
: 1;
62 void EmitRegisterName(int64_t Register
);
63 void PrintQuotedString(StringRef Data
, raw_ostream
&OS
) const;
64 void printDwarfFileDirective(unsigned FileNo
, StringRef Directory
,
66 std::optional
<MD5::MD5Result
> Checksum
,
67 std::optional
<StringRef
> Source
,
68 bool UseDwarfDirectory
,
69 raw_svector_ostream
&OS
) const;
70 void emitCFIStartProcImpl(MCDwarfFrameInfo
&Frame
) override
;
71 void emitCFIEndProcImpl(MCDwarfFrameInfo
&Frame
) override
;
74 MCAsmStreamer(MCContext
&Context
, std::unique_ptr
<formatted_raw_ostream
> os
,
75 bool isVerboseAsm
, bool useDwarfDirectory
,
76 MCInstPrinter
*printer
, std::unique_ptr
<MCCodeEmitter
> emitter
,
77 std::unique_ptr
<MCAsmBackend
> asmbackend
, bool showInst
)
78 : MCStreamer(Context
), OSOwner(std::move(os
)), OS(*OSOwner
),
79 MAI(Context
.getAsmInfo()), InstPrinter(printer
),
80 Assembler(std::make_unique
<MCAssembler
>(
81 Context
, std::move(asmbackend
), std::move(emitter
),
82 (asmbackend
) ? asmbackend
->createObjectWriter(NullStream
)
84 CommentStream(CommentToEmit
), IsVerboseAsm(isVerboseAsm
),
85 ShowInst(showInst
), UseDwarfDirectory(useDwarfDirectory
) {
88 InstPrinter
->setCommentStream(CommentStream
);
89 if (Assembler
->getBackendPtr())
90 setAllowAutoPadding(Assembler
->getBackend().allowAutoPadding());
92 Context
.setUseNamesOnTempLabels(true);
95 MCAssembler
&getAssembler() { return *Assembler
; }
96 MCAssembler
*getAssemblerPtr() override
{ return nullptr; }
98 inline void EmitEOL() {
99 // Dump Explicit Comments here.
100 emitExplicitComments();
101 // If we don't have any comments, just emit a \n.
106 EmitCommentsAndEOL();
109 void emitSyntaxDirective() override
;
111 void EmitCommentsAndEOL();
113 /// Return true if this streamer supports verbose assembly at all.
114 bool isVerboseAsm() const override
{ return IsVerboseAsm
; }
116 /// Do we support EmitRawText?
117 bool hasRawTextSupport() const override
{ return true; }
119 /// Add a comment that can be emitted to the generated .s file to make the
120 /// output of the compiler more readable. This only affects the MCAsmStreamer
121 /// and only when verbose assembly output is enabled.
122 void AddComment(const Twine
&T
, bool EOL
= true) override
;
124 /// Add a comment showing the encoding of an instruction.
125 void AddEncodingComment(const MCInst
&Inst
, const MCSubtargetInfo
&);
127 /// Return a raw_ostream that comments can be written to.
128 /// Unlike AddComment, you are required to terminate comments with \n if you
130 raw_ostream
&getCommentOS() override
{
132 return nulls(); // Discard comments unless in verbose asm mode.
133 return CommentStream
;
136 void emitRawComment(const Twine
&T
, bool TabPrefix
= true) override
;
138 void addExplicitComment(const Twine
&T
) override
;
139 void emitExplicitComments() override
;
141 /// Emit a blank line to a .s file to pretty it up.
142 void addBlankLine() override
{ EmitEOL(); }
144 /// @name MCStreamer Interface
147 void changeSection(MCSection
*Section
, const MCExpr
*Subsection
) override
;
149 void emitELFSymverDirective(const MCSymbol
*OriginalSym
, StringRef Name
,
150 bool KeepOriginalSym
) override
;
152 void emitLOHDirective(MCLOHType Kind
, const MCLOHArgs
&Args
) override
;
154 void emitGNUAttribute(unsigned Tag
, unsigned Value
) override
;
156 StringRef
getMnemonic(MCInst
&MI
) override
{
157 auto [Ptr
, Bits
] = InstPrinter
->getMnemonic(&MI
);
158 assert((Bits
!= 0 || Ptr
== nullptr) &&
159 "Invalid char pointer for instruction with no mnemonic");
163 void emitLabel(MCSymbol
*Symbol
, SMLoc Loc
= SMLoc()) override
;
165 void emitAssemblerFlag(MCAssemblerFlag Flag
) override
;
166 void emitLinkerOptions(ArrayRef
<std::string
> Options
) override
;
167 void emitDataRegion(MCDataRegionType Kind
) override
;
168 void emitVersionMin(MCVersionMinType Kind
, unsigned Major
, unsigned Minor
,
169 unsigned Update
, VersionTuple SDKVersion
) override
;
170 void emitBuildVersion(unsigned Platform
, unsigned Major
, unsigned Minor
,
171 unsigned Update
, VersionTuple SDKVersion
) override
;
172 void emitDarwinTargetVariantBuildVersion(unsigned Platform
, unsigned Major
,
173 unsigned Minor
, unsigned Update
,
174 VersionTuple SDKVersion
) override
;
175 void emitThumbFunc(MCSymbol
*Func
) override
;
177 void emitAssignment(MCSymbol
*Symbol
, const MCExpr
*Value
) override
;
178 void emitConditionalAssignment(MCSymbol
*Symbol
,
179 const MCExpr
*Value
) override
;
180 void emitWeakReference(MCSymbol
*Alias
, const MCSymbol
*Symbol
) override
;
181 bool emitSymbolAttribute(MCSymbol
*Symbol
, MCSymbolAttr Attribute
) override
;
183 void emitSymbolDesc(MCSymbol
*Symbol
, unsigned DescValue
) override
;
184 void beginCOFFSymbolDef(const MCSymbol
*Symbol
) override
;
185 void emitCOFFSymbolStorageClass(int StorageClass
) override
;
186 void emitCOFFSymbolType(int Type
) override
;
187 void endCOFFSymbolDef() override
;
188 void emitCOFFSafeSEH(MCSymbol
const *Symbol
) override
;
189 void emitCOFFSymbolIndex(MCSymbol
const *Symbol
) override
;
190 void emitCOFFSectionIndex(MCSymbol
const *Symbol
) override
;
191 void emitCOFFSecRel32(MCSymbol
const *Symbol
, uint64_t Offset
) override
;
192 void emitCOFFImgRel32(MCSymbol
const *Symbol
, int64_t Offset
) override
;
193 void emitXCOFFLocalCommonSymbol(MCSymbol
*LabelSym
, uint64_t Size
,
194 MCSymbol
*CsectSym
, Align Alignment
) override
;
195 void emitXCOFFSymbolLinkageWithVisibility(MCSymbol
*Symbol
,
196 MCSymbolAttr Linkage
,
197 MCSymbolAttr Visibility
) override
;
198 void emitXCOFFRenameDirective(const MCSymbol
*Name
,
199 StringRef Rename
) override
;
201 void emitXCOFFRefDirective(const MCSymbol
*Symbol
) override
;
203 void emitXCOFFExceptDirective(const MCSymbol
*Symbol
,
204 const MCSymbol
*Trap
,
205 unsigned Lang
, unsigned Reason
,
206 unsigned FunctionSize
, bool hasDebug
) override
;
207 void emitXCOFFCInfoSym(StringRef Name
, StringRef Metadata
) override
;
209 void emitELFSize(MCSymbol
*Symbol
, const MCExpr
*Value
) override
;
210 void emitCommonSymbol(MCSymbol
*Symbol
, uint64_t Size
,
211 Align ByteAlignment
) override
;
213 /// Emit a local common (.lcomm) symbol.
215 /// @param Symbol - The common symbol to emit.
216 /// @param Size - The size of the common symbol.
217 /// @param ByteAlignment - The alignment of the common symbol in bytes.
218 void emitLocalCommonSymbol(MCSymbol
*Symbol
, uint64_t Size
,
219 Align ByteAlignment
) override
;
221 void emitZerofill(MCSection
*Section
, MCSymbol
*Symbol
= nullptr,
222 uint64_t Size
= 0, Align ByteAlignment
= Align(1),
223 SMLoc Loc
= SMLoc()) override
;
225 void emitTBSSSymbol(MCSection
*Section
, MCSymbol
*Symbol
, uint64_t Size
,
226 Align ByteAlignment
= Align(1)) override
;
228 void emitBinaryData(StringRef Data
) override
;
230 void emitBytes(StringRef Data
) override
;
232 void emitValueImpl(const MCExpr
*Value
, unsigned Size
,
233 SMLoc Loc
= SMLoc()) override
;
234 void emitIntValue(uint64_t Value
, unsigned Size
) override
;
235 void emitIntValueInHex(uint64_t Value
, unsigned Size
) override
;
236 void emitIntValueInHexWithPadding(uint64_t Value
, unsigned Size
) override
;
238 void emitULEB128Value(const MCExpr
*Value
) override
;
240 void emitSLEB128Value(const MCExpr
*Value
) override
;
242 void emitDTPRel32Value(const MCExpr
*Value
) override
;
243 void emitDTPRel64Value(const MCExpr
*Value
) override
;
244 void emitTPRel32Value(const MCExpr
*Value
) override
;
245 void emitTPRel64Value(const MCExpr
*Value
) override
;
247 void emitGPRel64Value(const MCExpr
*Value
) override
;
249 void emitGPRel32Value(const MCExpr
*Value
) override
;
251 void emitFill(const MCExpr
&NumBytes
, uint64_t FillValue
,
252 SMLoc Loc
= SMLoc()) override
;
254 void emitFill(const MCExpr
&NumValues
, int64_t Size
, int64_t Expr
,
255 SMLoc Loc
= SMLoc()) override
;
257 void emitAlignmentDirective(unsigned ByteAlignment
,
258 std::optional
<int64_t> Value
, unsigned ValueSize
,
259 unsigned MaxBytesToEmit
);
261 void emitValueToAlignment(Align Alignment
, int64_t Value
= 0,
262 unsigned ValueSize
= 1,
263 unsigned MaxBytesToEmit
= 0) override
;
265 void emitCodeAlignment(Align Alignment
, const MCSubtargetInfo
*STI
,
266 unsigned MaxBytesToEmit
= 0) override
;
268 void emitValueToOffset(const MCExpr
*Offset
,
272 void emitFileDirective(StringRef Filename
) override
;
273 void emitFileDirective(StringRef Filename
, StringRef CompilerVerion
,
274 StringRef TimeStamp
, StringRef Description
) override
;
275 Expected
<unsigned> tryEmitDwarfFileDirective(
276 unsigned FileNo
, StringRef Directory
, StringRef Filename
,
277 std::optional
<MD5::MD5Result
> Checksum
= std::nullopt
,
278 std::optional
<StringRef
> Source
= std::nullopt
,
279 unsigned CUID
= 0) override
;
280 void emitDwarfFile0Directive(StringRef Directory
, StringRef Filename
,
281 std::optional
<MD5::MD5Result
> Checksum
,
282 std::optional
<StringRef
> Source
,
283 unsigned CUID
= 0) override
;
284 void emitDwarfLocDirective(unsigned FileNo
, unsigned Line
, unsigned Column
,
285 unsigned Flags
, unsigned Isa
,
286 unsigned Discriminator
,
287 StringRef FileName
) override
;
288 MCSymbol
*getDwarfLineTableSymbol(unsigned CUID
) override
;
290 bool emitCVFileDirective(unsigned FileNo
, StringRef Filename
,
291 ArrayRef
<uint8_t> Checksum
,
292 unsigned ChecksumKind
) override
;
293 bool emitCVFuncIdDirective(unsigned FuncId
) override
;
294 bool emitCVInlineSiteIdDirective(unsigned FunctionId
, unsigned IAFunc
,
295 unsigned IAFile
, unsigned IALine
,
296 unsigned IACol
, SMLoc Loc
) override
;
297 void emitCVLocDirective(unsigned FunctionId
, unsigned FileNo
, unsigned Line
,
298 unsigned Column
, bool PrologueEnd
, bool IsStmt
,
299 StringRef FileName
, SMLoc Loc
) override
;
300 void emitCVLinetableDirective(unsigned FunctionId
, const MCSymbol
*FnStart
,
301 const MCSymbol
*FnEnd
) override
;
302 void emitCVInlineLinetableDirective(unsigned PrimaryFunctionId
,
303 unsigned SourceFileId
,
304 unsigned SourceLineNum
,
305 const MCSymbol
*FnStartSym
,
306 const MCSymbol
*FnEndSym
) override
;
308 void PrintCVDefRangePrefix(
309 ArrayRef
<std::pair
<const MCSymbol
*, const MCSymbol
*>> Ranges
);
311 void emitCVDefRangeDirective(
312 ArrayRef
<std::pair
<const MCSymbol
*, const MCSymbol
*>> Ranges
,
313 codeview::DefRangeRegisterRelHeader DRHdr
) override
;
315 void emitCVDefRangeDirective(
316 ArrayRef
<std::pair
<const MCSymbol
*, const MCSymbol
*>> Ranges
,
317 codeview::DefRangeSubfieldRegisterHeader DRHdr
) override
;
319 void emitCVDefRangeDirective(
320 ArrayRef
<std::pair
<const MCSymbol
*, const MCSymbol
*>> Ranges
,
321 codeview::DefRangeRegisterHeader DRHdr
) override
;
323 void emitCVDefRangeDirective(
324 ArrayRef
<std::pair
<const MCSymbol
*, const MCSymbol
*>> Ranges
,
325 codeview::DefRangeFramePointerRelHeader DRHdr
) override
;
327 void emitCVStringTableDirective() override
;
328 void emitCVFileChecksumsDirective() override
;
329 void emitCVFileChecksumOffsetDirective(unsigned FileNo
) override
;
330 void emitCVFPOData(const MCSymbol
*ProcSym
, SMLoc L
) override
;
332 void emitIdent(StringRef IdentString
) override
;
333 void emitCFIBKeyFrame() override
;
334 void emitCFIMTETaggedFrame() override
;
335 void emitCFISections(bool EH
, bool Debug
) override
;
336 void emitCFIDefCfa(int64_t Register
, int64_t Offset
, SMLoc Loc
) override
;
337 void emitCFIDefCfaOffset(int64_t Offset
, SMLoc Loc
) override
;
338 void emitCFIDefCfaRegister(int64_t Register
, SMLoc Loc
) override
;
339 void emitCFILLVMDefAspaceCfa(int64_t Register
, int64_t Offset
,
340 int64_t AddressSpace
, SMLoc Loc
) override
;
341 void emitCFIOffset(int64_t Register
, int64_t Offset
, SMLoc Loc
) override
;
342 void emitCFIPersonality(const MCSymbol
*Sym
, unsigned Encoding
) override
;
343 void emitCFILsda(const MCSymbol
*Sym
, unsigned Encoding
) override
;
344 void emitCFIRememberState(SMLoc Loc
) override
;
345 void emitCFIRestoreState(SMLoc Loc
) override
;
346 void emitCFIRestore(int64_t Register
, SMLoc Loc
) override
;
347 void emitCFISameValue(int64_t Register
, SMLoc Loc
) override
;
348 void emitCFIRelOffset(int64_t Register
, int64_t Offset
, SMLoc Loc
) override
;
349 void emitCFIAdjustCfaOffset(int64_t Adjustment
, SMLoc Loc
) override
;
350 void emitCFIEscape(StringRef Values
, SMLoc Loc
) override
;
351 void emitCFIGnuArgsSize(int64_t Size
, SMLoc Loc
) override
;
352 void emitCFISignalFrame() override
;
353 void emitCFIUndefined(int64_t Register
, SMLoc Loc
) override
;
354 void emitCFIRegister(int64_t Register1
, int64_t Register2
,
356 void emitCFIWindowSave(SMLoc Loc
) override
;
357 void emitCFINegateRAState(SMLoc Loc
) override
;
358 void emitCFIReturnColumn(int64_t Register
) override
;
360 void emitWinCFIStartProc(const MCSymbol
*Symbol
, SMLoc Loc
) override
;
361 void emitWinCFIEndProc(SMLoc Loc
) override
;
362 void emitWinCFIFuncletOrFuncEnd(SMLoc Loc
) override
;
363 void emitWinCFIStartChained(SMLoc Loc
) override
;
364 void emitWinCFIEndChained(SMLoc Loc
) override
;
365 void emitWinCFIPushReg(MCRegister Register
, SMLoc Loc
) override
;
366 void emitWinCFISetFrame(MCRegister Register
, unsigned Offset
,
368 void emitWinCFIAllocStack(unsigned Size
, SMLoc Loc
) override
;
369 void emitWinCFISaveReg(MCRegister Register
, unsigned Offset
,
371 void emitWinCFISaveXMM(MCRegister Register
, unsigned Offset
,
373 void emitWinCFIPushFrame(bool Code
, SMLoc Loc
) override
;
374 void emitWinCFIEndProlog(SMLoc Loc
) override
;
376 void emitWinEHHandler(const MCSymbol
*Sym
, bool Unwind
, bool Except
,
378 void emitWinEHHandlerData(SMLoc Loc
) override
;
380 void emitCGProfileEntry(const MCSymbolRefExpr
*From
,
381 const MCSymbolRefExpr
*To
, uint64_t Count
) override
;
383 void emitInstruction(const MCInst
&Inst
, const MCSubtargetInfo
&STI
) override
;
385 void emitPseudoProbe(uint64_t Guid
, uint64_t Index
, uint64_t Type
,
386 uint64_t Attr
, uint64_t Discriminator
,
387 const MCPseudoProbeInlineStack
&InlineStack
,
388 MCSymbol
*FnSym
) override
;
390 void emitBundleAlignMode(Align Alignment
) override
;
391 void emitBundleLock(bool AlignToEnd
) override
;
392 void emitBundleUnlock() override
;
394 std::optional
<std::pair
<bool, std::string
>>
395 emitRelocDirective(const MCExpr
&Offset
, StringRef Name
, const MCExpr
*Expr
,
396 SMLoc Loc
, const MCSubtargetInfo
&STI
) override
;
398 void emitAddrsig() override
;
399 void emitAddrsigSym(const MCSymbol
*Sym
) override
;
401 /// If this file is backed by an assembly streamer, this dumps the specified
402 /// string in the output .s file. This capability is indicated by the
403 /// hasRawTextSupport() predicate.
404 void emitRawTextImpl(StringRef String
) override
;
406 void finishImpl() override
;
408 void emitDwarfUnitLength(uint64_t Length
, const Twine
&Comment
) override
;
410 MCSymbol
*emitDwarfUnitLength(const Twine
&Prefix
,
411 const Twine
&Comment
) override
;
413 void emitDwarfLineStartLabel(MCSymbol
*StartSym
) override
;
415 void emitDwarfLineEndEntry(MCSection
*Section
, MCSymbol
*LastLabel
) override
;
417 void emitDwarfAdvanceLineAddr(int64_t LineDelta
, const MCSymbol
*LastLabel
,
418 const MCSymbol
*Label
,
419 unsigned PointerSize
) override
;
421 void doFinalizationAtSectionEnd(MCSection
*Section
) override
;
424 } // end anonymous namespace.
426 void MCAsmStreamer::AddComment(const Twine
&T
, bool EOL
) {
427 if (!IsVerboseAsm
) return;
429 T
.toVector(CommentToEmit
);
432 CommentToEmit
.push_back('\n'); // Place comment in a new line.
435 void MCAsmStreamer::EmitCommentsAndEOL() {
436 if (CommentToEmit
.empty() && CommentStream
.GetNumBytesInBuffer() == 0) {
441 StringRef Comments
= CommentToEmit
;
443 assert(Comments
.back() == '\n' &&
444 "Comment array not newline terminated");
446 // Emit a line of comments.
447 OS
.PadToColumn(MAI
->getCommentColumn());
448 size_t Position
= Comments
.find('\n');
449 OS
<< MAI
->getCommentString() << ' ' << Comments
.substr(0, Position
) <<'\n';
451 Comments
= Comments
.substr(Position
+1);
452 } while (!Comments
.empty());
454 CommentToEmit
.clear();
457 static inline int64_t truncateToSize(int64_t Value
, unsigned Bytes
) {
458 assert(Bytes
> 0 && Bytes
<= 8 && "Invalid size!");
459 return Value
& ((uint64_t) (int64_t) -1 >> (64 - Bytes
* 8));
462 void MCAsmStreamer::emitRawComment(const Twine
&T
, bool TabPrefix
) {
465 OS
<< MAI
->getCommentString() << T
;
469 void MCAsmStreamer::addExplicitComment(const Twine
&T
) {
470 StringRef c
= T
.getSingleStringRef();
471 if (c
.equals(StringRef(MAI
->getSeparatorString())))
473 if (c
.starts_with(StringRef("//"))) {
474 ExplicitCommentToEmit
.append("\t");
475 ExplicitCommentToEmit
.append(MAI
->getCommentString());
477 ExplicitCommentToEmit
.append(c
.slice(2, c
.size()).str());
478 } else if (c
.starts_with(StringRef("/*"))) {
479 size_t p
= 2, len
= c
.size() - 2;
480 // emit each line in comment as separate newline.
482 size_t newp
= std::min(len
, c
.find_first_of("\r\n", p
));
483 ExplicitCommentToEmit
.append("\t");
484 ExplicitCommentToEmit
.append(MAI
->getCommentString());
485 ExplicitCommentToEmit
.append(c
.slice(p
, newp
).str());
486 // If we have another line in this comment add line
488 ExplicitCommentToEmit
.append("\n");
491 } else if (c
.starts_with(StringRef(MAI
->getCommentString()))) {
492 ExplicitCommentToEmit
.append("\t");
493 ExplicitCommentToEmit
.append(c
.str());
494 } else if (c
.front() == '#') {
496 ExplicitCommentToEmit
.append("\t");
497 ExplicitCommentToEmit
.append(MAI
->getCommentString());
498 ExplicitCommentToEmit
.append(c
.slice(1, c
.size()).str());
500 assert(false && "Unexpected Assembly Comment");
501 // full line comments immediately output
502 if (c
.back() == '\n')
503 emitExplicitComments();
506 void MCAsmStreamer::emitExplicitComments() {
507 StringRef Comments
= ExplicitCommentToEmit
;
508 if (!Comments
.empty())
510 ExplicitCommentToEmit
.clear();
513 void MCAsmStreamer::changeSection(MCSection
*Section
,
514 const MCExpr
*Subsection
) {
515 assert(Section
&& "Cannot switch to a null section!");
516 if (MCTargetStreamer
*TS
= getTargetStreamer()) {
517 TS
->changeSection(getCurrentSectionOnly(), Section
, Subsection
, OS
);
519 Section
->printSwitchToSection(*MAI
, getContext().getTargetTriple(), OS
,
524 void MCAsmStreamer::emitELFSymverDirective(const MCSymbol
*OriginalSym
,
526 bool KeepOriginalSym
) {
528 OriginalSym
->print(OS
, MAI
);
530 if (!KeepOriginalSym
&& !Name
.contains("@@@"))
535 void MCAsmStreamer::emitLabel(MCSymbol
*Symbol
, SMLoc Loc
) {
536 MCStreamer::emitLabel(Symbol
, Loc
);
538 Symbol
->print(OS
, MAI
);
539 OS
<< MAI
->getLabelSuffix();
544 void MCAsmStreamer::emitLOHDirective(MCLOHType Kind
, const MCLOHArgs
&Args
) {
545 StringRef str
= MCLOHIdToName(Kind
);
548 int NbArgs
= MCLOHIdToNbArgs(Kind
);
549 assert(NbArgs
!= -1 && ((size_t)NbArgs
) == Args
.size() && "Malformed LOH!");
550 assert(str
!= "" && "Invalid LOH name");
553 OS
<< "\t" << MCLOHDirectiveName() << " " << str
<< "\t";
555 for (const MCSymbol
*Arg
: Args
) {
564 void MCAsmStreamer::emitGNUAttribute(unsigned Tag
, unsigned Value
) {
565 OS
<< "\t.gnu_attribute " << Tag
<< ", " << Value
<< "\n";
568 void MCAsmStreamer::emitAssemblerFlag(MCAssemblerFlag Flag
) {
570 case MCAF_SyntaxUnified
: OS
<< "\t.syntax unified"; break;
571 case MCAF_SubsectionsViaSymbols
: OS
<< ".subsections_via_symbols"; break;
572 case MCAF_Code16
: OS
<< '\t'<< MAI
->getCode16Directive();break;
573 case MCAF_Code32
: OS
<< '\t'<< MAI
->getCode32Directive();break;
574 case MCAF_Code64
: OS
<< '\t'<< MAI
->getCode64Directive();break;
579 void MCAsmStreamer::emitLinkerOptions(ArrayRef
<std::string
> Options
) {
580 assert(!Options
.empty() && "At least one option is required!");
581 OS
<< "\t.linker_option \"" << Options
[0] << '"';
582 for (const std::string
&Opt
: llvm::drop_begin(Options
))
583 OS
<< ", " << '"' << Opt
<< '"';
587 void MCAsmStreamer::emitDataRegion(MCDataRegionType Kind
) {
588 if (!MAI
->doesSupportDataRegionDirectives())
591 case MCDR_DataRegion
: OS
<< "\t.data_region"; break;
592 case MCDR_DataRegionJT8
: OS
<< "\t.data_region jt8"; break;
593 case MCDR_DataRegionJT16
: OS
<< "\t.data_region jt16"; break;
594 case MCDR_DataRegionJT32
: OS
<< "\t.data_region jt32"; break;
595 case MCDR_DataRegionEnd
: OS
<< "\t.end_data_region"; break;
600 static const char *getVersionMinDirective(MCVersionMinType Type
) {
602 case MCVM_WatchOSVersionMin
: return ".watchos_version_min";
603 case MCVM_TvOSVersionMin
: return ".tvos_version_min";
604 case MCVM_IOSVersionMin
: return ".ios_version_min";
605 case MCVM_OSXVersionMin
: return ".macosx_version_min";
607 llvm_unreachable("Invalid MC version min type");
610 static void EmitSDKVersionSuffix(raw_ostream
&OS
,
611 const VersionTuple
&SDKVersion
) {
612 if (SDKVersion
.empty())
614 OS
<< '\t' << "sdk_version " << SDKVersion
.getMajor();
615 if (auto Minor
= SDKVersion
.getMinor()) {
616 OS
<< ", " << *Minor
;
617 if (auto Subminor
= SDKVersion
.getSubminor()) {
618 OS
<< ", " << *Subminor
;
623 void MCAsmStreamer::emitVersionMin(MCVersionMinType Type
, unsigned Major
,
624 unsigned Minor
, unsigned Update
,
625 VersionTuple SDKVersion
) {
626 OS
<< '\t' << getVersionMinDirective(Type
) << ' ' << Major
<< ", " << Minor
;
628 OS
<< ", " << Update
;
629 EmitSDKVersionSuffix(OS
, SDKVersion
);
633 static const char *getPlatformName(MachO::PlatformType Type
) {
635 #define PLATFORM(platform, id, name, build_name, target, tapi_target, \
637 case MachO::PLATFORM_##platform: \
639 #include "llvm/BinaryFormat/MachO.def"
641 llvm_unreachable("Invalid Mach-O platform type");
644 void MCAsmStreamer::emitBuildVersion(unsigned Platform
, unsigned Major
,
645 unsigned Minor
, unsigned Update
,
646 VersionTuple SDKVersion
) {
647 const char *PlatformName
= getPlatformName((MachO::PlatformType
)Platform
);
648 OS
<< "\t.build_version " << PlatformName
<< ", " << Major
<< ", " << Minor
;
650 OS
<< ", " << Update
;
651 EmitSDKVersionSuffix(OS
, SDKVersion
);
655 void MCAsmStreamer::emitDarwinTargetVariantBuildVersion(
656 unsigned Platform
, unsigned Major
, unsigned Minor
, unsigned Update
,
657 VersionTuple SDKVersion
) {
658 emitBuildVersion(Platform
, Major
, Minor
, Update
, SDKVersion
);
661 void MCAsmStreamer::emitThumbFunc(MCSymbol
*Func
) {
662 // This needs to emit to a temporary string to get properly quoted
663 // MCSymbols when they have spaces in them.
664 OS
<< "\t.thumb_func";
665 // Only Mach-O hasSubsectionsViaSymbols()
666 if (MAI
->hasSubsectionsViaSymbols()) {
668 Func
->print(OS
, MAI
);
673 void MCAsmStreamer::emitAssignment(MCSymbol
*Symbol
, const MCExpr
*Value
) {
674 // Do not emit a .set on inlined target assignments.
676 if (auto *E
= dyn_cast
<MCTargetExpr
>(Value
))
677 if (E
->inlineAssignedExpr())
681 Symbol
->print(OS
, MAI
);
683 Value
->print(OS
, MAI
);
688 MCStreamer::emitAssignment(Symbol
, Value
);
691 void MCAsmStreamer::emitConditionalAssignment(MCSymbol
*Symbol
,
692 const MCExpr
*Value
) {
693 OS
<< ".lto_set_conditional ";
694 Symbol
->print(OS
, MAI
);
696 Value
->print(OS
, MAI
);
700 void MCAsmStreamer::emitWeakReference(MCSymbol
*Alias
, const MCSymbol
*Symbol
) {
702 Alias
->print(OS
, MAI
);
704 Symbol
->print(OS
, MAI
);
708 bool MCAsmStreamer::emitSymbolAttribute(MCSymbol
*Symbol
,
709 MCSymbolAttr Attribute
) {
711 case MCSA_Invalid
: llvm_unreachable("Invalid symbol attribute");
712 case MCSA_ELF_TypeFunction
: /// .type _foo, STT_FUNC # aka @function
713 case MCSA_ELF_TypeIndFunction
: /// .type _foo, STT_GNU_IFUNC
714 case MCSA_ELF_TypeObject
: /// .type _foo, STT_OBJECT # aka @object
715 case MCSA_ELF_TypeTLS
: /// .type _foo, STT_TLS # aka @tls_object
716 case MCSA_ELF_TypeCommon
: /// .type _foo, STT_COMMON # aka @common
717 case MCSA_ELF_TypeNoType
: /// .type _foo, STT_NOTYPE # aka @notype
718 case MCSA_ELF_TypeGnuUniqueObject
: /// .type _foo, @gnu_unique_object
719 if (!MAI
->hasDotTypeDotSizeDirective())
720 return false; // Symbol attribute not supported
722 Symbol
->print(OS
, MAI
);
723 OS
<< ',' << ((MAI
->getCommentString()[0] != '@') ? '@' : '%');
725 default: return false;
726 case MCSA_ELF_TypeFunction
: OS
<< "function"; break;
727 case MCSA_ELF_TypeIndFunction
: OS
<< "gnu_indirect_function"; break;
728 case MCSA_ELF_TypeObject
: OS
<< "object"; break;
729 case MCSA_ELF_TypeTLS
: OS
<< "tls_object"; break;
730 case MCSA_ELF_TypeCommon
: OS
<< "common"; break;
731 case MCSA_ELF_TypeNoType
: OS
<< "notype"; break;
732 case MCSA_ELF_TypeGnuUniqueObject
: OS
<< "gnu_unique_object"; break;
736 case MCSA_Global
: // .globl/.global
737 OS
<< MAI
->getGlobalDirective();
739 case MCSA_LGlobal
: OS
<< "\t.lglobl\t"; break;
740 case MCSA_Hidden
: OS
<< "\t.hidden\t"; break;
741 case MCSA_IndirectSymbol
: OS
<< "\t.indirect_symbol\t"; break;
742 case MCSA_Internal
: OS
<< "\t.internal\t"; break;
743 case MCSA_LazyReference
: OS
<< "\t.lazy_reference\t"; break;
744 case MCSA_Local
: OS
<< "\t.local\t"; break;
745 case MCSA_NoDeadStrip
:
746 if (!MAI
->hasNoDeadStrip())
748 OS
<< "\t.no_dead_strip\t";
750 case MCSA_SymbolResolver
: OS
<< "\t.symbol_resolver\t"; break;
751 case MCSA_AltEntry
: OS
<< "\t.alt_entry\t"; break;
752 case MCSA_PrivateExtern
:
753 OS
<< "\t.private_extern\t";
755 case MCSA_Protected
: OS
<< "\t.protected\t"; break;
756 case MCSA_Reference
: OS
<< "\t.reference\t"; break;
760 case MCSA_Weak
: OS
<< MAI
->getWeakDirective(); break;
761 case MCSA_WeakDefinition
:
762 OS
<< "\t.weak_definition\t";
765 case MCSA_WeakReference
: OS
<< MAI
->getWeakRefDirective(); break;
766 case MCSA_WeakDefAutoPrivate
: OS
<< "\t.weak_def_can_be_hidden\t"; break;
768 // Assemblers currently do not support a .cold directive.
770 // Non-AIX assemblers currently do not support exported visibility.
775 case MCSA_WeakAntiDep
:
776 OS
<< "\t.weak_anti_dep\t";
780 Symbol
->print(OS
, MAI
);
786 void MCAsmStreamer::emitSymbolDesc(MCSymbol
*Symbol
, unsigned DescValue
) {
787 OS
<< ".desc" << ' ';
788 Symbol
->print(OS
, MAI
);
789 OS
<< ',' << DescValue
;
793 void MCAsmStreamer::emitSyntaxDirective() {
794 if (MAI
->getAssemblerDialect() == 1) {
795 OS
<< "\t.intel_syntax noprefix";
798 // FIXME: Currently emit unprefix'ed registers.
799 // The intel_syntax directive has one optional argument
800 // with may have a value of prefix or noprefix.
803 void MCAsmStreamer::beginCOFFSymbolDef(const MCSymbol
*Symbol
) {
805 Symbol
->print(OS
, MAI
);
810 void MCAsmStreamer::emitCOFFSymbolStorageClass(int StorageClass
) {
811 OS
<< "\t.scl\t" << StorageClass
<< ';';
815 void MCAsmStreamer::emitCOFFSymbolType(int Type
) {
816 OS
<< "\t.type\t" << Type
<< ';';
820 void MCAsmStreamer::endCOFFSymbolDef() {
825 void MCAsmStreamer::emitCOFFSafeSEH(MCSymbol
const *Symbol
) {
826 OS
<< "\t.safeseh\t";
827 Symbol
->print(OS
, MAI
);
831 void MCAsmStreamer::emitCOFFSymbolIndex(MCSymbol
const *Symbol
) {
833 Symbol
->print(OS
, MAI
);
837 void MCAsmStreamer::emitCOFFSectionIndex(MCSymbol
const *Symbol
) {
839 Symbol
->print(OS
, MAI
);
843 void MCAsmStreamer::emitCOFFSecRel32(MCSymbol
const *Symbol
, uint64_t Offset
) {
844 OS
<< "\t.secrel32\t";
845 Symbol
->print(OS
, MAI
);
851 void MCAsmStreamer::emitCOFFImgRel32(MCSymbol
const *Symbol
, int64_t Offset
) {
853 Symbol
->print(OS
, MAI
);
857 OS
<< '-' << -Offset
;
861 // We need an XCOFF-specific version of this directive as the AIX syntax
862 // requires a QualName argument identifying the csect name and storage mapping
863 // class to appear before the alignment if we are specifying it.
864 void MCAsmStreamer::emitXCOFFLocalCommonSymbol(MCSymbol
*LabelSym
,
868 assert(MAI
->getLCOMMDirectiveAlignmentType() == LCOMM::Log2Alignment
&&
869 "We only support writing log base-2 alignment format with XCOFF.");
872 LabelSym
->print(OS
, MAI
);
873 OS
<< ',' << Size
<< ',';
874 CsectSym
->print(OS
, MAI
);
875 OS
<< ',' << Log2(Alignment
);
879 // Print symbol's rename (original name contains invalid character(s)) if
881 MCSymbolXCOFF
*XSym
= cast
<MCSymbolXCOFF
>(CsectSym
);
882 if (XSym
->hasRename())
883 emitXCOFFRenameDirective(XSym
, XSym
->getSymbolTableName());
886 void MCAsmStreamer::emitXCOFFSymbolLinkageWithVisibility(
887 MCSymbol
*Symbol
, MCSymbolAttr Linkage
, MCSymbolAttr Visibility
) {
891 OS
<< MAI
->getGlobalDirective();
894 OS
<< MAI
->getWeakDirective();
903 report_fatal_error("unhandled linkage type");
906 Symbol
->print(OS
, MAI
);
908 switch (Visibility
) {
922 report_fatal_error("unexpected value for Visibility type");
926 // Print symbol's rename (original name contains invalid character(s)) if
928 if (cast
<MCSymbolXCOFF
>(Symbol
)->hasRename())
929 emitXCOFFRenameDirective(Symbol
,
930 cast
<MCSymbolXCOFF
>(Symbol
)->getSymbolTableName());
933 void MCAsmStreamer::emitXCOFFRenameDirective(const MCSymbol
*Name
,
936 Name
->print(OS
, MAI
);
939 for (char C
: Rename
) {
940 // To escape a double quote character, the character should be doubled.
949 void MCAsmStreamer::emitXCOFFRefDirective(const MCSymbol
*Symbol
) {
951 Symbol
->print(OS
, MAI
);
955 void MCAsmStreamer::emitXCOFFExceptDirective(const MCSymbol
*Symbol
,
956 const MCSymbol
*Trap
,
959 unsigned FunctionSize
,
962 Symbol
->print(OS
, MAI
);
963 OS
<< ", " << Lang
<< ", " << Reason
;
967 void MCAsmStreamer::emitXCOFFCInfoSym(StringRef Name
, StringRef Metadata
) {
968 const char InfoDirective
[] = "\t.info ";
969 const char *Separator
= ", ";
970 constexpr int WordSize
= sizeof(uint32_t);
972 // Start by emitting the .info pseudo-op and C_INFO symbol name.
974 PrintQuotedString(Name
, OS
);
977 size_t MetadataSize
= Metadata
.size();
979 // Emit the 4-byte length of the metadata.
980 OS
<< format_hex(MetadataSize
, 10) << Separator
;
982 // Nothing left to do if there's no metadata.
983 if (MetadataSize
== 0) {
988 // Metadata needs to be padded out to an even word size when generating
989 // assembly because the .info pseudo-op can only generate words of data. We
990 // apply the same restriction to the object case for consistency, however the
991 // linker doesn't require padding, so it will only save bytes specified by the
992 // length and discard any padding.
993 uint32_t PaddedSize
= alignTo(MetadataSize
, WordSize
);
994 uint32_t PaddingSize
= PaddedSize
- MetadataSize
;
996 // Write out the payload a word at a time.
998 // The assembler has a limit on the number of operands in an expression,
999 // so we need multiple .info pseudo-ops. We choose a small number of words
1000 // per pseudo-op to keep the assembly readable.
1001 constexpr int WordsPerDirective
= 5;
1002 // Force emitting a new directive to keep the first directive purely about the
1003 // name and size of the note.
1004 int WordsBeforeNextDirective
= 0;
1005 auto PrintWord
= [&](const uint8_t *WordPtr
) {
1006 if (WordsBeforeNextDirective
-- == 0) {
1008 OS
<< InfoDirective
;
1009 WordsBeforeNextDirective
= WordsPerDirective
;
1012 uint32_t Word
= llvm::support::endian::read32be(WordPtr
);
1013 OS
<< format_hex(Word
, 10);
1017 for (; Index
+ WordSize
<= MetadataSize
; Index
+= WordSize
)
1018 PrintWord(reinterpret_cast<const uint8_t *>(Metadata
.data()) + Index
);
1020 // If there is padding, then we have at least one byte of payload left
1023 assert(PaddedSize
- Index
== WordSize
);
1024 std::array
<uint8_t, WordSize
> LastWord
= {0};
1025 ::memcpy(LastWord
.data(), Metadata
.data() + Index
, MetadataSize
- Index
);
1026 PrintWord(LastWord
.data());
1031 void MCAsmStreamer::emitELFSize(MCSymbol
*Symbol
, const MCExpr
*Value
) {
1032 assert(MAI
->hasDotTypeDotSizeDirective());
1034 Symbol
->print(OS
, MAI
);
1036 Value
->print(OS
, MAI
);
1040 void MCAsmStreamer::emitCommonSymbol(MCSymbol
*Symbol
, uint64_t Size
,
1041 Align ByteAlignment
) {
1043 Symbol
->print(OS
, MAI
);
1046 if (MAI
->getCOMMDirectiveAlignmentIsInBytes())
1047 OS
<< ',' << ByteAlignment
.value();
1049 OS
<< ',' << Log2(ByteAlignment
);
1052 // Print symbol's rename (original name contains invalid character(s)) if
1054 MCSymbolXCOFF
*XSym
= dyn_cast
<MCSymbolXCOFF
>(Symbol
);
1055 if (XSym
&& XSym
->hasRename())
1056 emitXCOFFRenameDirective(XSym
, XSym
->getSymbolTableName());
1059 void MCAsmStreamer::emitLocalCommonSymbol(MCSymbol
*Symbol
, uint64_t Size
,
1062 Symbol
->print(OS
, MAI
);
1065 if (ByteAlign
> 1) {
1066 switch (MAI
->getLCOMMDirectiveAlignmentType()) {
1067 case LCOMM::NoAlignment
:
1068 llvm_unreachable("alignment not supported on .lcomm!");
1069 case LCOMM::ByteAlignment
:
1070 OS
<< ',' << ByteAlign
.value();
1072 case LCOMM::Log2Alignment
:
1073 OS
<< ',' << Log2(ByteAlign
);
1080 void MCAsmStreamer::emitZerofill(MCSection
*Section
, MCSymbol
*Symbol
,
1081 uint64_t Size
, Align ByteAlignment
,
1084 assignFragment(Symbol
, &Section
->getDummyFragment());
1086 // Note: a .zerofill directive does not switch sections.
1089 assert(Section
->getVariant() == MCSection::SV_MachO
&&
1090 ".zerofill is a Mach-O specific directive");
1091 // This is a mach-o specific directive.
1093 const MCSectionMachO
*MOSection
= ((const MCSectionMachO
*)Section
);
1094 OS
<< MOSection
->getSegmentName() << "," << MOSection
->getName();
1098 Symbol
->print(OS
, MAI
);
1100 OS
<< ',' << Log2(ByteAlignment
);
1105 // .tbss sym, size, align
1106 // This depends that the symbol has already been mangled from the original,
1108 void MCAsmStreamer::emitTBSSSymbol(MCSection
*Section
, MCSymbol
*Symbol
,
1109 uint64_t Size
, Align ByteAlignment
) {
1110 assignFragment(Symbol
, &Section
->getDummyFragment());
1112 assert(Symbol
&& "Symbol shouldn't be NULL!");
1113 // Instead of using the Section we'll just use the shortcut.
1115 assert(Section
->getVariant() == MCSection::SV_MachO
&&
1116 ".zerofill is a Mach-O specific directive");
1117 // This is a mach-o specific directive and section.
1120 Symbol
->print(OS
, MAI
);
1123 // Output align if we have it. We default to 1 so don't bother printing
1125 if (ByteAlignment
> 1)
1126 OS
<< ", " << Log2(ByteAlignment
);
1131 static inline bool isPrintableString(StringRef Data
) {
1132 const auto BeginPtr
= Data
.begin(), EndPtr
= Data
.end();
1133 for (const unsigned char C
: make_range(BeginPtr
, EndPtr
- 1)) {
1137 return isPrint(Data
.back()) || Data
.back() == 0;
1140 static inline char toOctal(int X
) { return (X
&7)+'0'; }
1142 static void PrintByteList(StringRef Data
, raw_ostream
&OS
,
1143 MCAsmInfo::AsmCharLiteralSyntax ACLS
) {
1144 assert(!Data
.empty() && "Cannot generate an empty list.");
1145 const auto printCharacterInOctal
= [&OS
](unsigned char C
) {
1147 OS
<< toOctal(C
>> 6);
1148 OS
<< toOctal(C
>> 3);
1149 OS
<< toOctal(C
>> 0);
1151 const auto printOneCharacterFor
= [printCharacterInOctal
](
1152 auto printOnePrintingCharacter
) {
1153 return [printCharacterInOctal
, printOnePrintingCharacter
](unsigned char C
) {
1155 printOnePrintingCharacter(static_cast<char>(C
));
1158 printCharacterInOctal(C
);
1161 const auto printCharacterList
= [Data
, &OS
](const auto &printOneCharacter
) {
1162 const auto BeginPtr
= Data
.begin(), EndPtr
= Data
.end();
1163 for (const unsigned char C
: make_range(BeginPtr
, EndPtr
- 1)) {
1164 printOneCharacter(C
);
1167 printOneCharacter(*(EndPtr
- 1));
1170 case MCAsmInfo::ACLS_Unknown
:
1171 printCharacterList(printCharacterInOctal
);
1173 case MCAsmInfo::ACLS_SingleQuotePrefix
:
1174 printCharacterList(printOneCharacterFor([&OS
](char C
) {
1175 const char AsmCharLitBuf
[2] = {'\'', C
};
1176 OS
<< StringRef(AsmCharLitBuf
, sizeof(AsmCharLitBuf
));
1180 llvm_unreachable("Invalid AsmCharLiteralSyntax value!");
1183 void MCAsmStreamer::PrintQuotedString(StringRef Data
, raw_ostream
&OS
) const {
1186 if (MAI
->hasPairedDoubleQuoteStringConstants()) {
1187 for (unsigned char C
: Data
) {
1194 for (unsigned char C
: Data
) {
1195 if (C
== '"' || C
== '\\') {
1196 OS
<< '\\' << (char)C
;
1200 if (isPrint((unsigned char)C
)) {
1223 OS
<< toOctal(C
>> 6);
1224 OS
<< toOctal(C
>> 3);
1225 OS
<< toOctal(C
>> 0);
1234 void MCAsmStreamer::emitBytes(StringRef Data
) {
1235 assert(getCurrentSectionOnly() &&
1236 "Cannot emit contents before setting section!");
1237 if (Data
.empty()) return;
1239 const auto emitAsString
= [this](StringRef Data
) {
1240 // If the data ends with 0 and the target supports .asciz, use it, otherwise
1241 // use .ascii or a byte-list directive
1242 if (MAI
->getAscizDirective() && Data
.back() == 0) {
1243 OS
<< MAI
->getAscizDirective();
1244 Data
= Data
.substr(0, Data
.size() - 1);
1245 } else if (LLVM_LIKELY(MAI
->getAsciiDirective())) {
1246 OS
<< MAI
->getAsciiDirective();
1247 } else if (MAI
->hasPairedDoubleQuoteStringConstants() &&
1248 isPrintableString(Data
)) {
1249 // For target with DoubleQuoteString constants, .string and .byte are used
1250 // as replacement of .asciz and .ascii.
1251 assert(MAI
->getPlainStringDirective() &&
1252 "hasPairedDoubleQuoteStringConstants target must support "
1253 "PlainString Directive");
1254 assert(MAI
->getByteListDirective() &&
1255 "hasPairedDoubleQuoteStringConstants target must support ByteList "
1257 if (Data
.back() == 0) {
1258 OS
<< MAI
->getPlainStringDirective();
1259 Data
= Data
.substr(0, Data
.size() - 1);
1261 OS
<< MAI
->getByteListDirective();
1263 } else if (MAI
->getByteListDirective()) {
1264 OS
<< MAI
->getByteListDirective();
1265 PrintByteList(Data
, OS
, MAI
->characterLiteralSyntax());
1272 PrintQuotedString(Data
, OS
);
1277 if (Data
.size() != 1 && emitAsString(Data
))
1280 // Only single byte is provided or no ascii, asciz, or byte-list directives
1281 // are applicable. Emit as vector of individual 8bits data elements.
1282 if (MCTargetStreamer
*TS
= getTargetStreamer()) {
1283 TS
->emitRawBytes(Data
);
1286 const char *Directive
= MAI
->getData8bitsDirective();
1287 for (const unsigned char C
: Data
.bytes()) {
1288 OS
<< Directive
<< (unsigned)C
;
1293 void MCAsmStreamer::emitBinaryData(StringRef Data
) {
1294 // This is binary data. Print it in a grid of hex bytes for readability.
1295 const size_t Cols
= 4;
1296 for (size_t I
= 0, EI
= alignTo(Data
.size(), Cols
); I
< EI
; I
+= Cols
) {
1297 size_t J
= I
, EJ
= std::min(I
+ Cols
, Data
.size());
1299 OS
<< MAI
->getData8bitsDirective();
1300 for (; J
< EJ
- 1; ++J
)
1301 OS
<< format("0x%02x", uint8_t(Data
[J
])) << ", ";
1302 OS
<< format("0x%02x", uint8_t(Data
[J
]));
1307 void MCAsmStreamer::emitIntValue(uint64_t Value
, unsigned Size
) {
1308 emitValue(MCConstantExpr::create(Value
, getContext()), Size
);
1311 void MCAsmStreamer::emitIntValueInHex(uint64_t Value
, unsigned Size
) {
1312 emitValue(MCConstantExpr::create(Value
, getContext(), true), Size
);
1315 void MCAsmStreamer::emitIntValueInHexWithPadding(uint64_t Value
,
1317 emitValue(MCConstantExpr::create(Value
, getContext(), true, Size
), Size
);
1320 void MCAsmStreamer::emitValueImpl(const MCExpr
*Value
, unsigned Size
,
1322 assert(Size
<= 8 && "Invalid size");
1323 assert(getCurrentSectionOnly() &&
1324 "Cannot emit contents before setting section!");
1325 const char *Directive
= nullptr;
1328 case 1: Directive
= MAI
->getData8bitsDirective(); break;
1329 case 2: Directive
= MAI
->getData16bitsDirective(); break;
1330 case 4: Directive
= MAI
->getData32bitsDirective(); break;
1331 case 8: Directive
= MAI
->getData64bitsDirective(); break;
1336 if (!Value
->evaluateAsAbsolute(IntValue
))
1337 report_fatal_error("Don't know how to emit this value.");
1339 // We couldn't handle the requested integer size so we fallback by breaking
1340 // the request down into several, smaller, integers.
1341 // Since sizes greater or equal to "Size" are invalid, we use the greatest
1342 // power of 2 that is less than "Size" as our largest piece of granularity.
1343 bool IsLittleEndian
= MAI
->isLittleEndian();
1344 for (unsigned Emitted
= 0; Emitted
!= Size
;) {
1345 unsigned Remaining
= Size
- Emitted
;
1346 // The size of our partial emission must be a power of two less than
1348 unsigned EmissionSize
= llvm::bit_floor(std::min(Remaining
, Size
- 1));
1349 // Calculate the byte offset of our partial emission taking into account
1350 // the endianness of the target.
1351 unsigned ByteOffset
=
1352 IsLittleEndian
? Emitted
: (Remaining
- EmissionSize
);
1353 uint64_t ValueToEmit
= IntValue
>> (ByteOffset
* 8);
1354 // We truncate our partial emission to fit within the bounds of the
1355 // emission domain. This produces nicer output and silences potential
1356 // truncation warnings when round tripping through another assembler.
1357 uint64_t Shift
= 64 - EmissionSize
* 8;
1358 assert(Shift
< static_cast<uint64_t>(
1359 std::numeric_limits
<unsigned long long>::digits
) &&
1360 "undefined behavior");
1361 ValueToEmit
&= ~0ULL >> Shift
;
1362 emitIntValue(ValueToEmit
, EmissionSize
);
1363 Emitted
+= EmissionSize
;
1368 assert(Directive
&& "Invalid size for machine code value!");
1370 if (MCTargetStreamer
*TS
= getTargetStreamer()) {
1371 TS
->emitValue(Value
);
1373 Value
->print(OS
, MAI
);
1378 void MCAsmStreamer::emitULEB128Value(const MCExpr
*Value
) {
1380 if (Value
->evaluateAsAbsolute(IntValue
)) {
1381 emitULEB128IntValue(IntValue
);
1384 OS
<< "\t.uleb128 ";
1385 Value
->print(OS
, MAI
);
1389 void MCAsmStreamer::emitSLEB128Value(const MCExpr
*Value
) {
1391 if (Value
->evaluateAsAbsolute(IntValue
)) {
1392 emitSLEB128IntValue(IntValue
);
1395 OS
<< "\t.sleb128 ";
1396 Value
->print(OS
, MAI
);
1400 void MCAsmStreamer::emitDTPRel64Value(const MCExpr
*Value
) {
1401 assert(MAI
->getDTPRel64Directive() != nullptr);
1402 OS
<< MAI
->getDTPRel64Directive();
1403 Value
->print(OS
, MAI
);
1407 void MCAsmStreamer::emitDTPRel32Value(const MCExpr
*Value
) {
1408 assert(MAI
->getDTPRel32Directive() != nullptr);
1409 OS
<< MAI
->getDTPRel32Directive();
1410 Value
->print(OS
, MAI
);
1414 void MCAsmStreamer::emitTPRel64Value(const MCExpr
*Value
) {
1415 assert(MAI
->getTPRel64Directive() != nullptr);
1416 OS
<< MAI
->getTPRel64Directive();
1417 Value
->print(OS
, MAI
);
1421 void MCAsmStreamer::emitTPRel32Value(const MCExpr
*Value
) {
1422 assert(MAI
->getTPRel32Directive() != nullptr);
1423 OS
<< MAI
->getTPRel32Directive();
1424 Value
->print(OS
, MAI
);
1428 void MCAsmStreamer::emitGPRel64Value(const MCExpr
*Value
) {
1429 assert(MAI
->getGPRel64Directive() != nullptr);
1430 OS
<< MAI
->getGPRel64Directive();
1431 Value
->print(OS
, MAI
);
1435 void MCAsmStreamer::emitGPRel32Value(const MCExpr
*Value
) {
1436 assert(MAI
->getGPRel32Directive() != nullptr);
1437 OS
<< MAI
->getGPRel32Directive();
1438 Value
->print(OS
, MAI
);
1442 void MCAsmStreamer::emitFill(const MCExpr
&NumBytes
, uint64_t FillValue
,
1444 int64_t IntNumBytes
;
1445 const bool IsAbsolute
= NumBytes
.evaluateAsAbsolute(IntNumBytes
);
1446 if (IsAbsolute
&& IntNumBytes
== 0)
1449 if (const char *ZeroDirective
= MAI
->getZeroDirective()) {
1450 if (MAI
->doesZeroDirectiveSupportNonZeroValue() || FillValue
== 0) {
1451 // FIXME: Emit location directives
1452 OS
<< ZeroDirective
;
1453 NumBytes
.print(OS
, MAI
);
1455 OS
<< ',' << (int)FillValue
;
1460 "Cannot emit non-absolute expression lengths of fill.");
1461 for (int i
= 0; i
< IntNumBytes
; ++i
) {
1462 OS
<< MAI
->getData8bitsDirective() << (int)FillValue
;
1469 MCStreamer::emitFill(NumBytes
, FillValue
);
1472 void MCAsmStreamer::emitFill(const MCExpr
&NumValues
, int64_t Size
,
1473 int64_t Expr
, SMLoc Loc
) {
1474 // FIXME: Emit location directives
1476 NumValues
.print(OS
, MAI
);
1477 OS
<< ", " << Size
<< ", 0x";
1478 OS
.write_hex(truncateToSize(Expr
, 4));
1482 void MCAsmStreamer::emitAlignmentDirective(unsigned ByteAlignment
,
1483 std::optional
<int64_t> Value
,
1485 unsigned MaxBytesToEmit
) {
1486 if (MAI
->useDotAlignForAlignment()) {
1487 if (!isPowerOf2_32(ByteAlignment
))
1488 report_fatal_error("Only power-of-two alignments are supported "
1491 OS
<< Log2_32(ByteAlignment
);
1496 // Some assemblers don't support non-power of two alignments, so we always
1497 // emit alignments as a power of two if possible.
1498 if (isPowerOf2_32(ByteAlignment
)) {
1499 switch (ValueSize
) {
1501 llvm_unreachable("Invalid size for machine code value!");
1503 OS
<< "\t.p2align\t";
1512 llvm_unreachable("Unsupported alignment size!");
1515 OS
<< Log2_32(ByteAlignment
);
1517 if (Value
.has_value() || MaxBytesToEmit
) {
1518 if (Value
.has_value()) {
1520 OS
.write_hex(truncateToSize(*Value
, ValueSize
));
1526 OS
<< ", " << MaxBytesToEmit
;
1532 // Non-power of two alignment. This is not widely supported by assemblers.
1533 // FIXME: Parameterize this based on MAI.
1534 switch (ValueSize
) {
1535 default: llvm_unreachable("Invalid size for machine code value!");
1536 case 1: OS
<< ".balign"; break;
1537 case 2: OS
<< ".balignw"; break;
1538 case 4: OS
<< ".balignl"; break;
1539 case 8: llvm_unreachable("Unsupported alignment size!");
1542 OS
<< ' ' << ByteAlignment
;
1543 if (Value
.has_value())
1544 OS
<< ", " << truncateToSize(*Value
, ValueSize
);
1545 else if (MaxBytesToEmit
)
1548 OS
<< ", " << MaxBytesToEmit
;
1552 void MCAsmStreamer::emitValueToAlignment(Align Alignment
, int64_t Value
,
1554 unsigned MaxBytesToEmit
) {
1555 emitAlignmentDirective(Alignment
.value(), Value
, ValueSize
, MaxBytesToEmit
);
1558 void MCAsmStreamer::emitCodeAlignment(Align Alignment
,
1559 const MCSubtargetInfo
*STI
,
1560 unsigned MaxBytesToEmit
) {
1561 // Emit with a text fill value.
1562 if (MAI
->getTextAlignFillValue())
1563 emitAlignmentDirective(Alignment
.value(), MAI
->getTextAlignFillValue(), 1,
1566 emitAlignmentDirective(Alignment
.value(), std::nullopt
, 1, MaxBytesToEmit
);
1569 void MCAsmStreamer::emitValueToOffset(const MCExpr
*Offset
,
1570 unsigned char Value
,
1572 // FIXME: Verify that Offset is associated with the current section.
1574 Offset
->print(OS
, MAI
);
1575 OS
<< ", " << (unsigned)Value
;
1579 void MCAsmStreamer::emitFileDirective(StringRef Filename
) {
1580 assert(MAI
->hasSingleParameterDotFile());
1582 PrintQuotedString(Filename
, OS
);
1586 void MCAsmStreamer::emitFileDirective(StringRef Filename
,
1587 StringRef CompilerVerion
,
1588 StringRef TimeStamp
,
1589 StringRef Description
) {
1590 assert(MAI
->hasFourStringsDotFile());
1592 PrintQuotedString(Filename
, OS
);
1594 if (!CompilerVerion
.empty()) {
1595 PrintQuotedString(CompilerVerion
, OS
);
1597 if (!TimeStamp
.empty()) {
1599 PrintQuotedString(TimeStamp
, OS
);
1601 if (!Description
.empty()) {
1603 PrintQuotedString(Description
, OS
);
1608 void MCAsmStreamer::printDwarfFileDirective(
1609 unsigned FileNo
, StringRef Directory
, StringRef Filename
,
1610 std::optional
<MD5::MD5Result
> Checksum
, std::optional
<StringRef
> Source
,
1611 bool UseDwarfDirectory
, raw_svector_ostream
&OS
) const {
1612 SmallString
<128> FullPathName
;
1614 if (!UseDwarfDirectory
&& !Directory
.empty()) {
1615 if (sys::path::is_absolute(Filename
))
1618 FullPathName
= Directory
;
1619 sys::path::append(FullPathName
, Filename
);
1621 Filename
= FullPathName
;
1625 OS
<< "\t.file\t" << FileNo
<< ' ';
1626 if (!Directory
.empty()) {
1627 PrintQuotedString(Directory
, OS
);
1630 PrintQuotedString(Filename
, OS
);
1632 OS
<< " md5 0x" << Checksum
->digest();
1635 PrintQuotedString(*Source
, OS
);
1639 Expected
<unsigned> MCAsmStreamer::tryEmitDwarfFileDirective(
1640 unsigned FileNo
, StringRef Directory
, StringRef Filename
,
1641 std::optional
<MD5::MD5Result
> Checksum
, std::optional
<StringRef
> Source
,
1643 assert(CUID
== 0 && "multiple CUs not supported by MCAsmStreamer");
1645 MCDwarfLineTable
&Table
= getContext().getMCDwarfLineTable(CUID
);
1646 unsigned NumFiles
= Table
.getMCDwarfFiles().size();
1647 Expected
<unsigned> FileNoOrErr
=
1648 Table
.tryGetFile(Directory
, Filename
, Checksum
, Source
,
1649 getContext().getDwarfVersion(), FileNo
);
1651 return FileNoOrErr
.takeError();
1652 FileNo
= FileNoOrErr
.get();
1654 // Return early if this file is already emitted before or if target doesn't
1655 // support .file directive.
1656 if (NumFiles
== Table
.getMCDwarfFiles().size() ||
1657 !MAI
->usesDwarfFileAndLocDirectives())
1660 SmallString
<128> Str
;
1661 raw_svector_ostream
OS1(Str
);
1662 printDwarfFileDirective(FileNo
, Directory
, Filename
, Checksum
, Source
,
1663 UseDwarfDirectory
, OS1
);
1665 if (MCTargetStreamer
*TS
= getTargetStreamer())
1666 TS
->emitDwarfFileDirective(OS1
.str());
1668 emitRawText(OS1
.str());
1673 void MCAsmStreamer::emitDwarfFile0Directive(
1674 StringRef Directory
, StringRef Filename
,
1675 std::optional
<MD5::MD5Result
> Checksum
, std::optional
<StringRef
> Source
,
1678 // .file 0 is new for DWARF v5.
1679 if (getContext().getDwarfVersion() < 5)
1681 // Inform MCDwarf about the root file.
1682 getContext().setMCLineTableRootFile(CUID
, Directory
, Filename
, Checksum
,
1685 // Target doesn't support .loc/.file directives, return early.
1686 if (!MAI
->usesDwarfFileAndLocDirectives())
1689 SmallString
<128> Str
;
1690 raw_svector_ostream
OS1(Str
);
1691 printDwarfFileDirective(0, Directory
, Filename
, Checksum
, Source
,
1692 UseDwarfDirectory
, OS1
);
1694 if (MCTargetStreamer
*TS
= getTargetStreamer())
1695 TS
->emitDwarfFileDirective(OS1
.str());
1697 emitRawText(OS1
.str());
1700 void MCAsmStreamer::emitDwarfLocDirective(unsigned FileNo
, unsigned Line
,
1701 unsigned Column
, unsigned Flags
,
1702 unsigned Isa
, unsigned Discriminator
,
1703 StringRef FileName
) {
1704 // If target doesn't support .loc/.file directive, we need to record the lines
1705 // same way like we do in object mode.
1706 if (!MAI
->usesDwarfFileAndLocDirectives()) {
1707 // In case we see two .loc directives in a row, make sure the
1708 // first one gets a line entry.
1709 MCDwarfLineEntry::make(this, getCurrentSectionOnly());
1710 this->MCStreamer::emitDwarfLocDirective(FileNo
, Line
, Column
, Flags
, Isa
,
1711 Discriminator
, FileName
);
1715 OS
<< "\t.loc\t" << FileNo
<< " " << Line
<< " " << Column
;
1716 if (MAI
->supportsExtendedDwarfLocDirective()) {
1717 if (Flags
& DWARF2_FLAG_BASIC_BLOCK
)
1718 OS
<< " basic_block";
1719 if (Flags
& DWARF2_FLAG_PROLOGUE_END
)
1720 OS
<< " prologue_end";
1721 if (Flags
& DWARF2_FLAG_EPILOGUE_BEGIN
)
1722 OS
<< " epilogue_begin";
1724 unsigned OldFlags
= getContext().getCurrentDwarfLoc().getFlags();
1725 if ((Flags
& DWARF2_FLAG_IS_STMT
) != (OldFlags
& DWARF2_FLAG_IS_STMT
)) {
1728 if (Flags
& DWARF2_FLAG_IS_STMT
)
1735 OS
<< " isa " << Isa
;
1737 OS
<< " discriminator " << Discriminator
;
1741 OS
.PadToColumn(MAI
->getCommentColumn());
1742 OS
<< MAI
->getCommentString() << ' ' << FileName
<< ':'
1743 << Line
<< ':' << Column
;
1746 this->MCStreamer::emitDwarfLocDirective(FileNo
, Line
, Column
, Flags
, Isa
,
1747 Discriminator
, FileName
);
1750 MCSymbol
*MCAsmStreamer::getDwarfLineTableSymbol(unsigned CUID
) {
1751 // Always use the zeroth line table, since asm syntax only supports one line
1753 return MCStreamer::getDwarfLineTableSymbol(0);
1756 bool MCAsmStreamer::emitCVFileDirective(unsigned FileNo
, StringRef Filename
,
1757 ArrayRef
<uint8_t> Checksum
,
1758 unsigned ChecksumKind
) {
1759 if (!getContext().getCVContext().addFile(*this, FileNo
, Filename
, Checksum
,
1763 OS
<< "\t.cv_file\t" << FileNo
<< ' ';
1764 PrintQuotedString(Filename
, OS
);
1766 if (!ChecksumKind
) {
1772 PrintQuotedString(toHex(Checksum
), OS
);
1773 OS
<< ' ' << ChecksumKind
;
1779 bool MCAsmStreamer::emitCVFuncIdDirective(unsigned FuncId
) {
1780 OS
<< "\t.cv_func_id " << FuncId
<< '\n';
1781 return MCStreamer::emitCVFuncIdDirective(FuncId
);
1784 bool MCAsmStreamer::emitCVInlineSiteIdDirective(unsigned FunctionId
,
1787 unsigned IALine
, unsigned IACol
,
1789 OS
<< "\t.cv_inline_site_id " << FunctionId
<< " within " << IAFunc
1790 << " inlined_at " << IAFile
<< ' ' << IALine
<< ' ' << IACol
<< '\n';
1791 return MCStreamer::emitCVInlineSiteIdDirective(FunctionId
, IAFunc
, IAFile
,
1792 IALine
, IACol
, Loc
);
1795 void MCAsmStreamer::emitCVLocDirective(unsigned FunctionId
, unsigned FileNo
,
1796 unsigned Line
, unsigned Column
,
1797 bool PrologueEnd
, bool IsStmt
,
1798 StringRef FileName
, SMLoc Loc
) {
1799 // Validate the directive.
1800 if (!checkCVLocSection(FunctionId
, FileNo
, Loc
))
1803 OS
<< "\t.cv_loc\t" << FunctionId
<< " " << FileNo
<< " " << Line
<< " "
1806 OS
<< " prologue_end";
1812 OS
.PadToColumn(MAI
->getCommentColumn());
1813 OS
<< MAI
->getCommentString() << ' ' << FileName
<< ':' << Line
<< ':'
1819 void MCAsmStreamer::emitCVLinetableDirective(unsigned FunctionId
,
1820 const MCSymbol
*FnStart
,
1821 const MCSymbol
*FnEnd
) {
1822 OS
<< "\t.cv_linetable\t" << FunctionId
<< ", ";
1823 FnStart
->print(OS
, MAI
);
1825 FnEnd
->print(OS
, MAI
);
1827 this->MCStreamer::emitCVLinetableDirective(FunctionId
, FnStart
, FnEnd
);
1830 void MCAsmStreamer::emitCVInlineLinetableDirective(unsigned PrimaryFunctionId
,
1831 unsigned SourceFileId
,
1832 unsigned SourceLineNum
,
1833 const MCSymbol
*FnStartSym
,
1834 const MCSymbol
*FnEndSym
) {
1835 OS
<< "\t.cv_inline_linetable\t" << PrimaryFunctionId
<< ' ' << SourceFileId
1836 << ' ' << SourceLineNum
<< ' ';
1837 FnStartSym
->print(OS
, MAI
);
1839 FnEndSym
->print(OS
, MAI
);
1841 this->MCStreamer::emitCVInlineLinetableDirective(
1842 PrimaryFunctionId
, SourceFileId
, SourceLineNum
, FnStartSym
, FnEndSym
);
1845 void MCAsmStreamer::PrintCVDefRangePrefix(
1846 ArrayRef
<std::pair
<const MCSymbol
*, const MCSymbol
*>> Ranges
) {
1847 OS
<< "\t.cv_def_range\t";
1848 for (std::pair
<const MCSymbol
*, const MCSymbol
*> Range
: Ranges
) {
1850 Range
.first
->print(OS
, MAI
);
1852 Range
.second
->print(OS
, MAI
);
1856 void MCAsmStreamer::emitCVDefRangeDirective(
1857 ArrayRef
<std::pair
<const MCSymbol
*, const MCSymbol
*>> Ranges
,
1858 codeview::DefRangeRegisterRelHeader DRHdr
) {
1859 PrintCVDefRangePrefix(Ranges
);
1860 OS
<< ", reg_rel, ";
1861 OS
<< DRHdr
.Register
<< ", " << DRHdr
.Flags
<< ", "
1862 << DRHdr
.BasePointerOffset
;
1866 void MCAsmStreamer::emitCVDefRangeDirective(
1867 ArrayRef
<std::pair
<const MCSymbol
*, const MCSymbol
*>> Ranges
,
1868 codeview::DefRangeSubfieldRegisterHeader DRHdr
) {
1869 PrintCVDefRangePrefix(Ranges
);
1870 OS
<< ", subfield_reg, ";
1871 OS
<< DRHdr
.Register
<< ", " << DRHdr
.OffsetInParent
;
1875 void MCAsmStreamer::emitCVDefRangeDirective(
1876 ArrayRef
<std::pair
<const MCSymbol
*, const MCSymbol
*>> Ranges
,
1877 codeview::DefRangeRegisterHeader DRHdr
) {
1878 PrintCVDefRangePrefix(Ranges
);
1880 OS
<< DRHdr
.Register
;
1884 void MCAsmStreamer::emitCVDefRangeDirective(
1885 ArrayRef
<std::pair
<const MCSymbol
*, const MCSymbol
*>> Ranges
,
1886 codeview::DefRangeFramePointerRelHeader DRHdr
) {
1887 PrintCVDefRangePrefix(Ranges
);
1888 OS
<< ", frame_ptr_rel, ";
1893 void MCAsmStreamer::emitCVStringTableDirective() {
1894 OS
<< "\t.cv_stringtable";
1898 void MCAsmStreamer::emitCVFileChecksumsDirective() {
1899 OS
<< "\t.cv_filechecksums";
1903 void MCAsmStreamer::emitCVFileChecksumOffsetDirective(unsigned FileNo
) {
1904 OS
<< "\t.cv_filechecksumoffset\t" << FileNo
;
1908 void MCAsmStreamer::emitCVFPOData(const MCSymbol
*ProcSym
, SMLoc L
) {
1909 OS
<< "\t.cv_fpo_data\t";
1910 ProcSym
->print(OS
, MAI
);
1914 void MCAsmStreamer::emitIdent(StringRef IdentString
) {
1915 assert(MAI
->hasIdentDirective() && ".ident directive not supported");
1917 PrintQuotedString(IdentString
, OS
);
1921 void MCAsmStreamer::emitCFISections(bool EH
, bool Debug
) {
1922 MCStreamer::emitCFISections(EH
, Debug
);
1923 OS
<< "\t.cfi_sections ";
1927 OS
<< ", .debug_frame";
1929 OS
<< ".debug_frame";
1935 void MCAsmStreamer::emitCFIStartProcImpl(MCDwarfFrameInfo
&Frame
) {
1936 OS
<< "\t.cfi_startproc";
1942 void MCAsmStreamer::emitCFIEndProcImpl(MCDwarfFrameInfo
&Frame
) {
1943 MCStreamer::emitCFIEndProcImpl(Frame
);
1944 OS
<< "\t.cfi_endproc";
1948 void MCAsmStreamer::EmitRegisterName(int64_t Register
) {
1949 if (!MAI
->useDwarfRegNumForCFI()) {
1950 // User .cfi_* directives can use arbitrary DWARF register numbers, not
1951 // just ones that map to LLVM register numbers and have known names.
1952 // Fall back to using the original number directly if no name is known.
1953 const MCRegisterInfo
*MRI
= getContext().getRegisterInfo();
1954 if (std::optional
<unsigned> LLVMRegister
=
1955 MRI
->getLLVMRegNum(Register
, true)) {
1956 InstPrinter
->printRegName(OS
, *LLVMRegister
);
1963 void MCAsmStreamer::emitCFIDefCfa(int64_t Register
, int64_t Offset
, SMLoc Loc
) {
1964 MCStreamer::emitCFIDefCfa(Register
, Offset
, Loc
);
1965 OS
<< "\t.cfi_def_cfa ";
1966 EmitRegisterName(Register
);
1967 OS
<< ", " << Offset
;
1971 void MCAsmStreamer::emitCFIDefCfaOffset(int64_t Offset
, SMLoc Loc
) {
1972 MCStreamer::emitCFIDefCfaOffset(Offset
, Loc
);
1973 OS
<< "\t.cfi_def_cfa_offset " << Offset
;
1977 void MCAsmStreamer::emitCFILLVMDefAspaceCfa(int64_t Register
, int64_t Offset
,
1978 int64_t AddressSpace
, SMLoc Loc
) {
1979 MCStreamer::emitCFILLVMDefAspaceCfa(Register
, Offset
, AddressSpace
, Loc
);
1980 OS
<< "\t.cfi_llvm_def_aspace_cfa ";
1981 EmitRegisterName(Register
);
1982 OS
<< ", " << Offset
;
1983 OS
<< ", " << AddressSpace
;
1987 static void PrintCFIEscape(llvm::formatted_raw_ostream
&OS
, StringRef Values
) {
1988 OS
<< "\t.cfi_escape ";
1989 if (!Values
.empty()) {
1990 size_t e
= Values
.size() - 1;
1991 for (size_t i
= 0; i
< e
; ++i
)
1992 OS
<< format("0x%02x", uint8_t(Values
[i
])) << ", ";
1993 OS
<< format("0x%02x", uint8_t(Values
[e
]));
1997 void MCAsmStreamer::emitCFIEscape(StringRef Values
, SMLoc Loc
) {
1998 MCStreamer::emitCFIEscape(Values
, Loc
);
1999 PrintCFIEscape(OS
, Values
);
2003 void MCAsmStreamer::emitCFIGnuArgsSize(int64_t Size
, SMLoc Loc
) {
2004 MCStreamer::emitCFIGnuArgsSize(Size
, Loc
);
2006 uint8_t Buffer
[16] = { dwarf::DW_CFA_GNU_args_size
};
2007 unsigned Len
= encodeULEB128(Size
, Buffer
+ 1) + 1;
2009 PrintCFIEscape(OS
, StringRef((const char *)&Buffer
[0], Len
));
2013 void MCAsmStreamer::emitCFIDefCfaRegister(int64_t Register
, SMLoc Loc
) {
2014 MCStreamer::emitCFIDefCfaRegister(Register
, Loc
);
2015 OS
<< "\t.cfi_def_cfa_register ";
2016 EmitRegisterName(Register
);
2020 void MCAsmStreamer::emitCFIOffset(int64_t Register
, int64_t Offset
, SMLoc Loc
) {
2021 MCStreamer::emitCFIOffset(Register
, Offset
, Loc
);
2022 OS
<< "\t.cfi_offset ";
2023 EmitRegisterName(Register
);
2024 OS
<< ", " << Offset
;
2028 void MCAsmStreamer::emitCFIPersonality(const MCSymbol
*Sym
,
2029 unsigned Encoding
) {
2030 MCStreamer::emitCFIPersonality(Sym
, Encoding
);
2031 OS
<< "\t.cfi_personality " << Encoding
<< ", ";
2032 Sym
->print(OS
, MAI
);
2036 void MCAsmStreamer::emitCFILsda(const MCSymbol
*Sym
, unsigned Encoding
) {
2037 MCStreamer::emitCFILsda(Sym
, Encoding
);
2038 OS
<< "\t.cfi_lsda " << Encoding
<< ", ";
2039 Sym
->print(OS
, MAI
);
2043 void MCAsmStreamer::emitCFIRememberState(SMLoc Loc
) {
2044 MCStreamer::emitCFIRememberState(Loc
);
2045 OS
<< "\t.cfi_remember_state";
2049 void MCAsmStreamer::emitCFIRestoreState(SMLoc Loc
) {
2050 MCStreamer::emitCFIRestoreState(Loc
);
2051 OS
<< "\t.cfi_restore_state";
2055 void MCAsmStreamer::emitCFIRestore(int64_t Register
, SMLoc Loc
) {
2056 MCStreamer::emitCFIRestore(Register
, Loc
);
2057 OS
<< "\t.cfi_restore ";
2058 EmitRegisterName(Register
);
2062 void MCAsmStreamer::emitCFISameValue(int64_t Register
, SMLoc Loc
) {
2063 MCStreamer::emitCFISameValue(Register
, Loc
);
2064 OS
<< "\t.cfi_same_value ";
2065 EmitRegisterName(Register
);
2069 void MCAsmStreamer::emitCFIRelOffset(int64_t Register
, int64_t Offset
,
2071 MCStreamer::emitCFIRelOffset(Register
, Offset
, Loc
);
2072 OS
<< "\t.cfi_rel_offset ";
2073 EmitRegisterName(Register
);
2074 OS
<< ", " << Offset
;
2078 void MCAsmStreamer::emitCFIAdjustCfaOffset(int64_t Adjustment
, SMLoc Loc
) {
2079 MCStreamer::emitCFIAdjustCfaOffset(Adjustment
, Loc
);
2080 OS
<< "\t.cfi_adjust_cfa_offset " << Adjustment
;
2084 void MCAsmStreamer::emitCFISignalFrame() {
2085 MCStreamer::emitCFISignalFrame();
2086 OS
<< "\t.cfi_signal_frame";
2090 void MCAsmStreamer::emitCFIUndefined(int64_t Register
, SMLoc Loc
) {
2091 MCStreamer::emitCFIUndefined(Register
, Loc
);
2092 OS
<< "\t.cfi_undefined ";
2093 EmitRegisterName(Register
);
2097 void MCAsmStreamer::emitCFIRegister(int64_t Register1
, int64_t Register2
,
2099 MCStreamer::emitCFIRegister(Register1
, Register2
, Loc
);
2100 OS
<< "\t.cfi_register ";
2101 EmitRegisterName(Register1
);
2103 EmitRegisterName(Register2
);
2107 void MCAsmStreamer::emitCFIWindowSave(SMLoc Loc
) {
2108 MCStreamer::emitCFIWindowSave(Loc
);
2109 OS
<< "\t.cfi_window_save";
2113 void MCAsmStreamer::emitCFINegateRAState(SMLoc Loc
) {
2114 MCStreamer::emitCFINegateRAState(Loc
);
2115 OS
<< "\t.cfi_negate_ra_state";
2119 void MCAsmStreamer::emitCFIReturnColumn(int64_t Register
) {
2120 MCStreamer::emitCFIReturnColumn(Register
);
2121 OS
<< "\t.cfi_return_column ";
2122 EmitRegisterName(Register
);
2126 void MCAsmStreamer::emitCFIBKeyFrame() {
2127 MCStreamer::emitCFIBKeyFrame();
2128 OS
<< "\t.cfi_b_key_frame";
2132 void MCAsmStreamer::emitCFIMTETaggedFrame() {
2133 MCStreamer::emitCFIMTETaggedFrame();
2134 OS
<< "\t.cfi_mte_tagged_frame";
2138 void MCAsmStreamer::emitWinCFIStartProc(const MCSymbol
*Symbol
, SMLoc Loc
) {
2139 MCStreamer::emitWinCFIStartProc(Symbol
, Loc
);
2142 Symbol
->print(OS
, MAI
);
2146 void MCAsmStreamer::emitWinCFIEndProc(SMLoc Loc
) {
2147 MCStreamer::emitWinCFIEndProc(Loc
);
2149 OS
<< "\t.seh_endproc";
2153 void MCAsmStreamer::emitWinCFIFuncletOrFuncEnd(SMLoc Loc
) {
2154 MCStreamer::emitWinCFIFuncletOrFuncEnd(Loc
);
2156 OS
<< "\t.seh_endfunclet";
2160 void MCAsmStreamer::emitWinCFIStartChained(SMLoc Loc
) {
2161 MCStreamer::emitWinCFIStartChained(Loc
);
2163 OS
<< "\t.seh_startchained";
2167 void MCAsmStreamer::emitWinCFIEndChained(SMLoc Loc
) {
2168 MCStreamer::emitWinCFIEndChained(Loc
);
2170 OS
<< "\t.seh_endchained";
2174 void MCAsmStreamer::emitWinEHHandler(const MCSymbol
*Sym
, bool Unwind
,
2175 bool Except
, SMLoc Loc
) {
2176 MCStreamer::emitWinEHHandler(Sym
, Unwind
, Except
, Loc
);
2178 OS
<< "\t.seh_handler ";
2179 Sym
->print(OS
, MAI
);
2181 const Triple
&T
= getContext().getTargetTriple();
2182 if (T
.getArch() == Triple::arm
|| T
.getArch() == Triple::thumb
)
2185 OS
<< ", " << Marker
<< "unwind";
2187 OS
<< ", " << Marker
<< "except";
2191 void MCAsmStreamer::emitWinEHHandlerData(SMLoc Loc
) {
2192 MCStreamer::emitWinEHHandlerData(Loc
);
2194 // Switch sections. Don't call switchSection directly, because that will
2195 // cause the section switch to be visible in the emitted assembly.
2196 // We only do this so the section switch that terminates the handler
2197 // data block is visible.
2198 WinEH::FrameInfo
*CurFrame
= getCurrentWinFrameInfo();
2200 // Do nothing if no frame is open. MCStreamer should've already reported an
2205 MCSection
*TextSec
= &CurFrame
->Function
->getSection();
2206 MCSection
*XData
= getAssociatedXDataSection(TextSec
);
2207 switchSectionNoChange(XData
);
2209 OS
<< "\t.seh_handlerdata";
2213 void MCAsmStreamer::emitWinCFIPushReg(MCRegister Register
, SMLoc Loc
) {
2214 MCStreamer::emitWinCFIPushReg(Register
, Loc
);
2216 OS
<< "\t.seh_pushreg ";
2217 InstPrinter
->printRegName(OS
, Register
);
2221 void MCAsmStreamer::emitWinCFISetFrame(MCRegister Register
, unsigned Offset
,
2223 MCStreamer::emitWinCFISetFrame(Register
, Offset
, Loc
);
2225 OS
<< "\t.seh_setframe ";
2226 InstPrinter
->printRegName(OS
, Register
);
2227 OS
<< ", " << Offset
;
2231 void MCAsmStreamer::emitWinCFIAllocStack(unsigned Size
, SMLoc Loc
) {
2232 MCStreamer::emitWinCFIAllocStack(Size
, Loc
);
2234 OS
<< "\t.seh_stackalloc " << Size
;
2238 void MCAsmStreamer::emitWinCFISaveReg(MCRegister Register
, unsigned Offset
,
2240 MCStreamer::emitWinCFISaveReg(Register
, Offset
, Loc
);
2242 OS
<< "\t.seh_savereg ";
2243 InstPrinter
->printRegName(OS
, Register
);
2244 OS
<< ", " << Offset
;
2248 void MCAsmStreamer::emitWinCFISaveXMM(MCRegister Register
, unsigned Offset
,
2250 MCStreamer::emitWinCFISaveXMM(Register
, Offset
, Loc
);
2252 OS
<< "\t.seh_savexmm ";
2253 InstPrinter
->printRegName(OS
, Register
);
2254 OS
<< ", " << Offset
;
2258 void MCAsmStreamer::emitWinCFIPushFrame(bool Code
, SMLoc Loc
) {
2259 MCStreamer::emitWinCFIPushFrame(Code
, Loc
);
2261 OS
<< "\t.seh_pushframe";
2267 void MCAsmStreamer::emitWinCFIEndProlog(SMLoc Loc
) {
2268 MCStreamer::emitWinCFIEndProlog(Loc
);
2270 OS
<< "\t.seh_endprologue";
2274 void MCAsmStreamer::emitCGProfileEntry(const MCSymbolRefExpr
*From
,
2275 const MCSymbolRefExpr
*To
,
2277 OS
<< "\t.cg_profile ";
2278 From
->getSymbol().print(OS
, MAI
);
2280 To
->getSymbol().print(OS
, MAI
);
2281 OS
<< ", " << Count
;
2285 void MCAsmStreamer::AddEncodingComment(const MCInst
&Inst
,
2286 const MCSubtargetInfo
&STI
) {
2287 raw_ostream
&OS
= getCommentOS();
2288 SmallString
<256> Code
;
2289 SmallVector
<MCFixup
, 4> Fixups
;
2291 // If we have no code emitter, don't emit code.
2292 if (!getAssembler().getEmitterPtr())
2295 getAssembler().getEmitter().encodeInstruction(Inst
, Code
, Fixups
, STI
);
2297 // If we are showing fixups, create symbolic markers in the encoded
2298 // representation. We do this by making a per-bit map to the fixup item index,
2299 // then trying to display it as nicely as possible.
2300 SmallVector
<uint8_t, 64> FixupMap
;
2301 FixupMap
.resize(Code
.size() * 8);
2302 for (unsigned i
= 0, e
= Code
.size() * 8; i
!= e
; ++i
)
2305 for (unsigned i
= 0, e
= Fixups
.size(); i
!= e
; ++i
) {
2306 MCFixup
&F
= Fixups
[i
];
2307 const MCFixupKindInfo
&Info
=
2308 getAssembler().getBackend().getFixupKindInfo(F
.getKind());
2309 for (unsigned j
= 0; j
!= Info
.TargetSize
; ++j
) {
2310 unsigned Index
= F
.getOffset() * 8 + Info
.TargetOffset
+ j
;
2311 assert(Index
< Code
.size() * 8 && "Invalid offset in fixup!");
2312 FixupMap
[Index
] = 1 + i
;
2316 // FIXME: Note the fixup comments for Thumb2 are completely bogus since the
2317 // high order halfword of a 32-bit Thumb2 instruction is emitted first.
2318 OS
<< "encoding: [";
2319 for (unsigned i
= 0, e
= Code
.size(); i
!= e
; ++i
) {
2323 // See if all bits are the same map entry.
2324 uint8_t MapEntry
= FixupMap
[i
* 8 + 0];
2325 for (unsigned j
= 1; j
!= 8; ++j
) {
2326 if (FixupMap
[i
* 8 + j
] == MapEntry
)
2329 MapEntry
= uint8_t(~0U);
2333 if (MapEntry
!= uint8_t(~0U)) {
2334 if (MapEntry
== 0) {
2335 OS
<< format("0x%02x", uint8_t(Code
[i
]));
2338 // FIXME: Some of the 8 bits require fix up.
2339 OS
<< format("0x%02x", uint8_t(Code
[i
])) << '\''
2340 << char('A' + MapEntry
- 1) << '\'';
2342 OS
<< char('A' + MapEntry
- 1);
2345 // Otherwise, write out in binary.
2347 for (unsigned j
= 8; j
--;) {
2348 unsigned Bit
= (Code
[i
] >> j
) & 1;
2351 if (MAI
->isLittleEndian())
2352 FixupBit
= i
* 8 + j
;
2354 FixupBit
= i
* 8 + (7-j
);
2356 if (uint8_t MapEntry
= FixupMap
[FixupBit
]) {
2357 assert(Bit
== 0 && "Encoder wrote into fixed up bit!");
2358 OS
<< char('A' + MapEntry
- 1);
2366 for (unsigned i
= 0, e
= Fixups
.size(); i
!= e
; ++i
) {
2367 MCFixup
&F
= Fixups
[i
];
2368 const MCFixupKindInfo
&Info
=
2369 getAssembler().getBackend().getFixupKindInfo(F
.getKind());
2370 OS
<< " fixup " << char('A' + i
) << " - "
2371 << "offset: " << F
.getOffset() << ", value: ";
2372 F
.getValue()->print(OS
, MAI
);
2373 OS
<< ", kind: " << Info
.Name
<< "\n";
2377 void MCAsmStreamer::emitInstruction(const MCInst
&Inst
,
2378 const MCSubtargetInfo
&STI
) {
2379 assert(getCurrentSectionOnly() &&
2380 "Cannot emit contents before setting section!");
2382 if (!MAI
->usesDwarfFileAndLocDirectives())
2383 // Now that a machine instruction has been assembled into this section, make
2384 // a line entry for any .loc directive that has been seen.
2385 MCDwarfLineEntry::make(this, getCurrentSectionOnly());
2387 // Show the encoding in a comment if we have a code emitter.
2388 AddEncodingComment(Inst
, STI
);
2390 // Show the MCInst if enabled.
2392 Inst
.dump_pretty(getCommentOS(), InstPrinter
.get(), "\n ");
2393 getCommentOS() << "\n";
2396 if(getTargetStreamer())
2397 getTargetStreamer()->prettyPrintAsm(*InstPrinter
, 0, Inst
, STI
, OS
);
2399 InstPrinter
->printInst(&Inst
, 0, "", STI
, OS
);
2401 StringRef Comments
= CommentToEmit
;
2402 if (Comments
.size() && Comments
.back() != '\n')
2403 getCommentOS() << "\n";
2408 void MCAsmStreamer::emitPseudoProbe(uint64_t Guid
, uint64_t Index
,
2409 uint64_t Type
, uint64_t Attr
,
2410 uint64_t Discriminator
,
2411 const MCPseudoProbeInlineStack
&InlineStack
,
2413 OS
<< "\t.pseudoprobe\t" << Guid
<< " " << Index
<< " " << Type
<< " " << Attr
;
2415 OS
<< " " << Discriminator
;
2416 // Emit inline stack like
2417 // @ GUIDmain:3 @ GUIDCaller:1 @ GUIDDirectCaller:11
2418 for (const auto &Site
: InlineStack
)
2419 OS
<< " @ " << std::get
<0>(Site
) << ":" << std::get
<1>(Site
);
2421 OS
<< " " << FnSym
->getName();
2426 void MCAsmStreamer::emitBundleAlignMode(Align Alignment
) {
2427 OS
<< "\t.bundle_align_mode " << Log2(Alignment
);
2431 void MCAsmStreamer::emitBundleLock(bool AlignToEnd
) {
2432 OS
<< "\t.bundle_lock";
2434 OS
<< " align_to_end";
2438 void MCAsmStreamer::emitBundleUnlock() {
2439 OS
<< "\t.bundle_unlock";
2443 std::optional
<std::pair
<bool, std::string
>>
2444 MCAsmStreamer::emitRelocDirective(const MCExpr
&Offset
, StringRef Name
,
2445 const MCExpr
*Expr
, SMLoc
,
2446 const MCSubtargetInfo
&STI
) {
2448 Offset
.print(OS
, MAI
);
2452 Expr
->print(OS
, MAI
);
2455 return std::nullopt
;
2458 void MCAsmStreamer::emitAddrsig() {
2463 void MCAsmStreamer::emitAddrsigSym(const MCSymbol
*Sym
) {
2464 OS
<< "\t.addrsig_sym ";
2465 Sym
->print(OS
, MAI
);
2469 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
2470 /// the specified string in the output .s file. This capability is
2471 /// indicated by the hasRawTextSupport() predicate.
2472 void MCAsmStreamer::emitRawTextImpl(StringRef String
) {
2473 if (!String
.empty() && String
.back() == '\n')
2474 String
= String
.substr(0, String
.size()-1);
2479 void MCAsmStreamer::finishImpl() {
2480 // If we are generating dwarf for assembly source files dump out the sections.
2481 if (getContext().getGenDwarfForAssembly())
2482 MCGenDwarfInfo::Emit(this);
2484 // Now it is time to emit debug line sections if target doesn't support .loc
2485 // and .line directives.
2486 if (!MAI
->usesDwarfFileAndLocDirectives()) {
2487 MCDwarfLineTable::emit(this, getAssembler().getDWARFLinetableParams());
2491 // Emit the label for the line table, if requested - since the rest of the
2492 // line table will be defined by .loc/.file directives, and not emitted
2493 // directly, the label is the only work required here.
2494 const auto &Tables
= getContext().getMCDwarfLineTables();
2495 if (!Tables
.empty()) {
2496 assert(Tables
.size() == 1 && "asm output only supports one line table");
2497 if (auto *Label
= Tables
.begin()->second
.getLabel()) {
2498 switchSection(getContext().getObjectFileInfo()->getDwarfLineSection());
2504 void MCAsmStreamer::emitDwarfUnitLength(uint64_t Length
, const Twine
&Comment
) {
2505 // If the assembler on some target fills in the DWARF unit length, we
2506 // don't want to emit the length in the compiler. For example, the AIX
2507 // assembler requires the assembly file with the unit length omitted from
2508 // the debug section headers. In such cases, any label we placed occurs
2509 // after the implied length field. We need to adjust the reference here
2510 // to account for the offset introduced by the inserted length field.
2511 if (!MAI
->needsDwarfSectionSizeInHeader())
2513 MCStreamer::emitDwarfUnitLength(Length
, Comment
);
2516 MCSymbol
*MCAsmStreamer::emitDwarfUnitLength(const Twine
&Prefix
,
2517 const Twine
&Comment
) {
2518 // If the assembler on some target fills in the DWARF unit length, we
2519 // don't want to emit the length in the compiler. For example, the AIX
2520 // assembler requires the assembly file with the unit length omitted from
2521 // the debug section headers. In such cases, any label we placed occurs
2522 // after the implied length field. We need to adjust the reference here
2523 // to account for the offset introduced by the inserted length field.
2524 if (!MAI
->needsDwarfSectionSizeInHeader())
2525 return getContext().createTempSymbol(Prefix
+ "_end");
2526 return MCStreamer::emitDwarfUnitLength(Prefix
, Comment
);
2529 void MCAsmStreamer::emitDwarfLineStartLabel(MCSymbol
*StartSym
) {
2530 // If the assembler on some target fills in the DWARF unit length, we
2531 // don't want to emit the length in the compiler. For example, the AIX
2532 // assembler requires the assembly file with the unit length omitted from
2533 // the debug section headers. In such cases, any label we placed occurs
2534 // after the implied length field. We need to adjust the reference here
2535 // to account for the offset introduced by the inserted length field.
2536 MCContext
&Ctx
= getContext();
2537 if (!MAI
->needsDwarfSectionSizeInHeader()) {
2538 MCSymbol
*DebugLineSymTmp
= Ctx
.createTempSymbol("debug_line_");
2539 // Emit the symbol which does not contain the unit length field.
2540 emitLabel(DebugLineSymTmp
);
2542 // Adjust the outer reference to account for the offset introduced by the
2543 // inserted length field.
2544 unsigned LengthFieldSize
=
2545 dwarf::getUnitLengthFieldByteSize(Ctx
.getDwarfFormat());
2546 const MCExpr
*EntrySize
= MCConstantExpr::create(LengthFieldSize
, Ctx
);
2547 const MCExpr
*OuterSym
= MCBinaryExpr::createSub(
2548 MCSymbolRefExpr::create(DebugLineSymTmp
, Ctx
), EntrySize
, Ctx
);
2550 emitAssignment(StartSym
, OuterSym
);
2553 MCStreamer::emitDwarfLineStartLabel(StartSym
);
2556 void MCAsmStreamer::emitDwarfLineEndEntry(MCSection
*Section
,
2557 MCSymbol
*LastLabel
) {
2558 // If the targets write the raw debug line data for assembly output (We can
2559 // not switch to Section and add the end symbol there for assembly output)
2560 // we currently use the .text end label as any section end. This will not
2561 // impact the debugability as we will jump to the caller of the last function
2562 // in the section before we come into the .text end address.
2563 assert(!MAI
->usesDwarfFileAndLocDirectives() &&
2564 ".loc should not be generated together with raw data!");
2566 MCContext
&Ctx
= getContext();
2568 // FIXME: use section end symbol as end of the Section. We need to consider
2569 // the explicit sections and -ffunction-sections when we try to generate or
2570 // find section end symbol for the Section.
2571 MCSection
*TextSection
= Ctx
.getObjectFileInfo()->getTextSection();
2572 assert(TextSection
->hasEnded() && ".text section is not end!");
2574 MCSymbol
*SectionEnd
= TextSection
->getEndSymbol(Ctx
);
2575 const MCAsmInfo
*AsmInfo
= Ctx
.getAsmInfo();
2576 emitDwarfAdvanceLineAddr(INT64_MAX
, LastLabel
, SectionEnd
,
2577 AsmInfo
->getCodePointerSize());
2580 // Generate DWARF line sections for assembly mode without .loc/.file
2581 void MCAsmStreamer::emitDwarfAdvanceLineAddr(int64_t LineDelta
,
2582 const MCSymbol
*LastLabel
,
2583 const MCSymbol
*Label
,
2584 unsigned PointerSize
) {
2585 assert(!MAI
->usesDwarfFileAndLocDirectives() &&
2586 ".loc/.file don't need raw data in debug line section!");
2588 // Set to new address.
2589 AddComment("Set address to " + Label
->getName());
2590 emitIntValue(dwarf::DW_LNS_extended_op
, 1);
2591 emitULEB128IntValue(PointerSize
+ 1);
2592 emitIntValue(dwarf::DW_LNE_set_address
, 1);
2593 emitSymbolValue(Label
, PointerSize
);
2596 // Emit the sequence for the LineDelta (from 1) and a zero address delta.
2597 AddComment("Start sequence");
2598 MCDwarfLineAddr::Emit(this, MCDwarfLineTableParams(), LineDelta
, 0);
2602 // INT64_MAX is a signal of the end of the section. Emit DW_LNE_end_sequence
2603 // for the end of the section.
2604 if (LineDelta
== INT64_MAX
) {
2605 AddComment("End sequence");
2606 emitIntValue(dwarf::DW_LNS_extended_op
, 1);
2607 emitULEB128IntValue(1);
2608 emitIntValue(dwarf::DW_LNE_end_sequence
, 1);
2613 AddComment("Advance line " + Twine(LineDelta
));
2614 emitIntValue(dwarf::DW_LNS_advance_line
, 1);
2615 emitSLEB128IntValue(LineDelta
);
2616 emitIntValue(dwarf::DW_LNS_copy
, 1);
2619 void MCAsmStreamer::doFinalizationAtSectionEnd(MCSection
*Section
) {
2620 // Emit section end. This is used to tell the debug line section where the end
2621 // is for a text section if we don't use .loc to represent the debug line.
2622 if (MAI
->usesDwarfFileAndLocDirectives())
2625 switchSectionNoChange(Section
);
2627 MCSymbol
*Sym
= getCurrentSectionOnly()->getEndSymbol(getContext());
2629 if (!Sym
->isInSection())
2633 MCStreamer
*llvm::createAsmStreamer(MCContext
&Context
,
2634 std::unique_ptr
<formatted_raw_ostream
> OS
,
2635 bool isVerboseAsm
, bool useDwarfDirectory
,
2637 std::unique_ptr
<MCCodeEmitter
> &&CE
,
2638 std::unique_ptr
<MCAsmBackend
> &&MAB
,
2640 return new MCAsmStreamer(Context
, std::move(OS
), isVerboseAsm
,
2641 useDwarfDirectory
, IP
, std::move(CE
), std::move(MAB
),