[RISCV] Refactor predicates for rvv intrinsic patterns.
[llvm-project.git] / llvm / lib / MC / MCStreamer.cpp
blob0cb04594522b2bc1b2d12b583fbc0f5caf7eefa8
1 //===- lib/MC/MCStreamer.cpp - Streaming Machine Code Output --------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/MC/MCStreamer.h"
10 #include "llvm/ADT/SmallString.h"
11 #include "llvm/ADT/StringRef.h"
12 #include "llvm/ADT/Twine.h"
13 #include "llvm/BinaryFormat/COFF.h"
14 #include "llvm/BinaryFormat/MachO.h"
15 #include "llvm/DebugInfo/CodeView/SymbolRecord.h"
16 #include "llvm/MC/MCAsmBackend.h"
17 #include "llvm/MC/MCAsmInfo.h"
18 #include "llvm/MC/MCCodeView.h"
19 #include "llvm/MC/MCContext.h"
20 #include "llvm/MC/MCDwarf.h"
21 #include "llvm/MC/MCExpr.h"
22 #include "llvm/MC/MCInst.h"
23 #include "llvm/MC/MCInstPrinter.h"
24 #include "llvm/MC/MCObjectFileInfo.h"
25 #include "llvm/MC/MCPseudoProbe.h"
26 #include "llvm/MC/MCRegister.h"
27 #include "llvm/MC/MCRegisterInfo.h"
28 #include "llvm/MC/MCSection.h"
29 #include "llvm/MC/MCSectionCOFF.h"
30 #include "llvm/MC/MCSymbol.h"
31 #include "llvm/MC/MCWin64EH.h"
32 #include "llvm/MC/MCWinEH.h"
33 #include "llvm/Support/Casting.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/LEB128.h"
36 #include "llvm/Support/MathExtras.h"
37 #include "llvm/Support/raw_ostream.h"
38 #include <cassert>
39 #include <cstdint>
40 #include <cstdlib>
41 #include <optional>
42 #include <utility>
44 using namespace llvm;
46 MCTargetStreamer::MCTargetStreamer(MCStreamer &S) : Streamer(S) {
47 S.setTargetStreamer(this);
50 // Pin the vtables to this file.
51 MCTargetStreamer::~MCTargetStreamer() = default;
53 void MCTargetStreamer::emitLabel(MCSymbol *Symbol) {}
55 void MCTargetStreamer::finish() {}
57 void MCTargetStreamer::emitConstantPools() {}
59 void MCTargetStreamer::changeSection(const MCSection *CurSection,
60 MCSection *Section,
61 const MCExpr *Subsection,
62 raw_ostream &OS) {
63 Section->printSwitchToSection(*Streamer.getContext().getAsmInfo(),
64 Streamer.getContext().getTargetTriple(), OS,
65 Subsection);
68 void MCTargetStreamer::emitDwarfFileDirective(StringRef Directive) {
69 Streamer.emitRawText(Directive);
72 void MCTargetStreamer::emitValue(const MCExpr *Value) {
73 SmallString<128> Str;
74 raw_svector_ostream OS(Str);
76 Value->print(OS, Streamer.getContext().getAsmInfo());
77 Streamer.emitRawText(OS.str());
80 void MCTargetStreamer::emitRawBytes(StringRef Data) {
81 const MCAsmInfo *MAI = Streamer.getContext().getAsmInfo();
82 const char *Directive = MAI->getData8bitsDirective();
83 for (const unsigned char C : Data.bytes()) {
84 SmallString<128> Str;
85 raw_svector_ostream OS(Str);
87 OS << Directive << (unsigned)C;
88 Streamer.emitRawText(OS.str());
92 void MCTargetStreamer::emitAssignment(MCSymbol *Symbol, const MCExpr *Value) {}
94 MCStreamer::MCStreamer(MCContext &Ctx)
95 : Context(Ctx), CurrentWinFrameInfo(nullptr),
96 CurrentProcWinFrameInfoStartIndex(0), UseAssemblerInfoForParsing(false) {
97 SectionStack.push_back(std::pair<MCSectionSubPair, MCSectionSubPair>());
100 MCStreamer::~MCStreamer() = default;
102 void MCStreamer::reset() {
103 DwarfFrameInfos.clear();
104 CurrentWinFrameInfo = nullptr;
105 WinFrameInfos.clear();
106 SymbolOrdering.clear();
107 SectionStack.clear();
108 SectionStack.push_back(std::pair<MCSectionSubPair, MCSectionSubPair>());
111 raw_ostream &MCStreamer::getCommentOS() {
112 // By default, discard comments.
113 return nulls();
116 unsigned MCStreamer::getNumFrameInfos() { return DwarfFrameInfos.size(); }
117 ArrayRef<MCDwarfFrameInfo> MCStreamer::getDwarfFrameInfos() const {
118 return DwarfFrameInfos;
121 void MCStreamer::emitRawComment(const Twine &T, bool TabPrefix) {}
123 void MCStreamer::addExplicitComment(const Twine &T) {}
124 void MCStreamer::emitExplicitComments() {}
126 void MCStreamer::generateCompactUnwindEncodings(MCAsmBackend *MAB) {
127 for (auto &FI : DwarfFrameInfos)
128 FI.CompactUnwindEncoding =
129 (MAB ? MAB->generateCompactUnwindEncoding(FI.Instructions) : 0);
132 /// EmitIntValue - Special case of EmitValue that avoids the client having to
133 /// pass in a MCExpr for constant integers.
134 void MCStreamer::emitIntValue(uint64_t Value, unsigned Size) {
135 assert(1 <= Size && Size <= 8 && "Invalid size");
136 assert((isUIntN(8 * Size, Value) || isIntN(8 * Size, Value)) &&
137 "Invalid size");
138 const bool IsLittleEndian = Context.getAsmInfo()->isLittleEndian();
139 uint64_t Swapped = support::endian::byte_swap(
140 Value, IsLittleEndian ? support::little : support::big);
141 unsigned Index = IsLittleEndian ? 0 : 8 - Size;
142 emitBytes(StringRef(reinterpret_cast<char *>(&Swapped) + Index, Size));
144 void MCStreamer::emitIntValue(APInt Value) {
145 if (Value.getNumWords() == 1) {
146 emitIntValue(Value.getLimitedValue(), Value.getBitWidth() / 8);
147 return;
150 const bool IsLittleEndianTarget = Context.getAsmInfo()->isLittleEndian();
151 const bool ShouldSwap = sys::IsLittleEndianHost != IsLittleEndianTarget;
152 const APInt Swapped = ShouldSwap ? Value.byteSwap() : Value;
153 const unsigned Size = Value.getBitWidth() / 8;
154 SmallString<10> Tmp;
155 Tmp.resize(Size);
156 StoreIntToMemory(Swapped, reinterpret_cast<uint8_t *>(Tmp.data()), Size);
157 emitBytes(Tmp.str());
160 /// EmitULEB128IntValue - Special case of EmitULEB128Value that avoids the
161 /// client having to pass in a MCExpr for constant integers.
162 unsigned MCStreamer::emitULEB128IntValue(uint64_t Value, unsigned PadTo) {
163 SmallString<128> Tmp;
164 raw_svector_ostream OSE(Tmp);
165 encodeULEB128(Value, OSE, PadTo);
166 emitBytes(OSE.str());
167 return Tmp.size();
170 /// EmitSLEB128IntValue - Special case of EmitSLEB128Value that avoids the
171 /// client having to pass in a MCExpr for constant integers.
172 unsigned MCStreamer::emitSLEB128IntValue(int64_t Value) {
173 SmallString<128> Tmp;
174 raw_svector_ostream OSE(Tmp);
175 encodeSLEB128(Value, OSE);
176 emitBytes(OSE.str());
177 return Tmp.size();
180 void MCStreamer::emitValue(const MCExpr *Value, unsigned Size, SMLoc Loc) {
181 emitValueImpl(Value, Size, Loc);
184 void MCStreamer::emitSymbolValue(const MCSymbol *Sym, unsigned Size,
185 bool IsSectionRelative) {
186 assert((!IsSectionRelative || Size == 4) &&
187 "SectionRelative value requires 4-bytes");
189 if (!IsSectionRelative)
190 emitValueImpl(MCSymbolRefExpr::create(Sym, getContext()), Size);
191 else
192 emitCOFFSecRel32(Sym, /*Offset=*/0);
195 void MCStreamer::emitDTPRel64Value(const MCExpr *Value) {
196 report_fatal_error("unsupported directive in streamer");
199 void MCStreamer::emitDTPRel32Value(const MCExpr *Value) {
200 report_fatal_error("unsupported directive in streamer");
203 void MCStreamer::emitTPRel64Value(const MCExpr *Value) {
204 report_fatal_error("unsupported directive in streamer");
207 void MCStreamer::emitTPRel32Value(const MCExpr *Value) {
208 report_fatal_error("unsupported directive in streamer");
211 void MCStreamer::emitGPRel64Value(const MCExpr *Value) {
212 report_fatal_error("unsupported directive in streamer");
215 void MCStreamer::emitGPRel32Value(const MCExpr *Value) {
216 report_fatal_error("unsupported directive in streamer");
219 /// Emit NumBytes bytes worth of the value specified by FillValue.
220 /// This implements directives such as '.space'.
221 void MCStreamer::emitFill(uint64_t NumBytes, uint8_t FillValue) {
222 if (NumBytes)
223 emitFill(*MCConstantExpr::create(NumBytes, getContext()), FillValue);
226 void llvm::MCStreamer::emitNops(int64_t NumBytes, int64_t ControlledNopLen,
227 llvm::SMLoc, const MCSubtargetInfo& STI) {}
229 /// The implementation in this class just redirects to emitFill.
230 void MCStreamer::emitZeros(uint64_t NumBytes) { emitFill(NumBytes, 0); }
232 Expected<unsigned> MCStreamer::tryEmitDwarfFileDirective(
233 unsigned FileNo, StringRef Directory, StringRef Filename,
234 std::optional<MD5::MD5Result> Checksum, std::optional<StringRef> Source,
235 unsigned CUID) {
236 return getContext().getDwarfFile(Directory, Filename, FileNo, Checksum,
237 Source, CUID);
240 void MCStreamer::emitDwarfFile0Directive(StringRef Directory,
241 StringRef Filename,
242 std::optional<MD5::MD5Result> Checksum,
243 std::optional<StringRef> Source,
244 unsigned CUID) {
245 getContext().setMCLineTableRootFile(CUID, Directory, Filename, Checksum,
246 Source);
249 void MCStreamer::emitCFIBKeyFrame() {
250 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
251 if (!CurFrame)
252 return;
253 CurFrame->IsBKeyFrame = true;
256 void MCStreamer::emitCFIMTETaggedFrame() {
257 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
258 if (!CurFrame)
259 return;
260 CurFrame->IsMTETaggedFrame = true;
263 void MCStreamer::emitDwarfLocDirective(unsigned FileNo, unsigned Line,
264 unsigned Column, unsigned Flags,
265 unsigned Isa, unsigned Discriminator,
266 StringRef FileName) {
267 getContext().setCurrentDwarfLoc(FileNo, Line, Column, Flags, Isa,
268 Discriminator);
271 MCSymbol *MCStreamer::getDwarfLineTableSymbol(unsigned CUID) {
272 MCDwarfLineTable &Table = getContext().getMCDwarfLineTable(CUID);
273 if (!Table.getLabel()) {
274 StringRef Prefix = Context.getAsmInfo()->getPrivateGlobalPrefix();
275 Table.setLabel(
276 Context.getOrCreateSymbol(Prefix + "line_table_start" + Twine(CUID)));
278 return Table.getLabel();
281 bool MCStreamer::hasUnfinishedDwarfFrameInfo() {
282 return !FrameInfoStack.empty();
285 MCDwarfFrameInfo *MCStreamer::getCurrentDwarfFrameInfo() {
286 if (!hasUnfinishedDwarfFrameInfo()) {
287 getContext().reportError(getStartTokLoc(),
288 "this directive must appear between "
289 ".cfi_startproc and .cfi_endproc directives");
290 return nullptr;
292 return &DwarfFrameInfos[FrameInfoStack.back().first];
295 bool MCStreamer::emitCVFileDirective(unsigned FileNo, StringRef Filename,
296 ArrayRef<uint8_t> Checksum,
297 unsigned ChecksumKind) {
298 return getContext().getCVContext().addFile(*this, FileNo, Filename, Checksum,
299 ChecksumKind);
302 bool MCStreamer::emitCVFuncIdDirective(unsigned FunctionId) {
303 return getContext().getCVContext().recordFunctionId(FunctionId);
306 bool MCStreamer::emitCVInlineSiteIdDirective(unsigned FunctionId,
307 unsigned IAFunc, unsigned IAFile,
308 unsigned IALine, unsigned IACol,
309 SMLoc Loc) {
310 if (getContext().getCVContext().getCVFunctionInfo(IAFunc) == nullptr) {
311 getContext().reportError(Loc, "parent function id not introduced by "
312 ".cv_func_id or .cv_inline_site_id");
313 return true;
316 return getContext().getCVContext().recordInlinedCallSiteId(
317 FunctionId, IAFunc, IAFile, IALine, IACol);
320 void MCStreamer::emitCVLocDirective(unsigned FunctionId, unsigned FileNo,
321 unsigned Line, unsigned Column,
322 bool PrologueEnd, bool IsStmt,
323 StringRef FileName, SMLoc Loc) {}
325 bool MCStreamer::checkCVLocSection(unsigned FuncId, unsigned FileNo,
326 SMLoc Loc) {
327 CodeViewContext &CVC = getContext().getCVContext();
328 MCCVFunctionInfo *FI = CVC.getCVFunctionInfo(FuncId);
329 if (!FI) {
330 getContext().reportError(
331 Loc, "function id not introduced by .cv_func_id or .cv_inline_site_id");
332 return false;
335 // Track the section
336 if (FI->Section == nullptr)
337 FI->Section = getCurrentSectionOnly();
338 else if (FI->Section != getCurrentSectionOnly()) {
339 getContext().reportError(
340 Loc,
341 "all .cv_loc directives for a function must be in the same section");
342 return false;
344 return true;
347 void MCStreamer::emitCVLinetableDirective(unsigned FunctionId,
348 const MCSymbol *Begin,
349 const MCSymbol *End) {}
351 void MCStreamer::emitCVInlineLinetableDirective(unsigned PrimaryFunctionId,
352 unsigned SourceFileId,
353 unsigned SourceLineNum,
354 const MCSymbol *FnStartSym,
355 const MCSymbol *FnEndSym) {}
357 /// Only call this on endian-specific types like ulittle16_t and little32_t, or
358 /// structs composed of them.
359 template <typename T>
360 static void copyBytesForDefRange(SmallString<20> &BytePrefix,
361 codeview::SymbolKind SymKind,
362 const T &DefRangeHeader) {
363 BytePrefix.resize(2 + sizeof(T));
364 codeview::ulittle16_t SymKindLE = codeview::ulittle16_t(SymKind);
365 memcpy(&BytePrefix[0], &SymKindLE, 2);
366 memcpy(&BytePrefix[2], &DefRangeHeader, sizeof(T));
369 void MCStreamer::emitCVDefRangeDirective(
370 ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
371 StringRef FixedSizePortion) {}
373 void MCStreamer::emitCVDefRangeDirective(
374 ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
375 codeview::DefRangeRegisterRelHeader DRHdr) {
376 SmallString<20> BytePrefix;
377 copyBytesForDefRange(BytePrefix, codeview::S_DEFRANGE_REGISTER_REL, DRHdr);
378 emitCVDefRangeDirective(Ranges, BytePrefix);
381 void MCStreamer::emitCVDefRangeDirective(
382 ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
383 codeview::DefRangeSubfieldRegisterHeader DRHdr) {
384 SmallString<20> BytePrefix;
385 copyBytesForDefRange(BytePrefix, codeview::S_DEFRANGE_SUBFIELD_REGISTER,
386 DRHdr);
387 emitCVDefRangeDirective(Ranges, BytePrefix);
390 void MCStreamer::emitCVDefRangeDirective(
391 ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
392 codeview::DefRangeRegisterHeader DRHdr) {
393 SmallString<20> BytePrefix;
394 copyBytesForDefRange(BytePrefix, codeview::S_DEFRANGE_REGISTER, DRHdr);
395 emitCVDefRangeDirective(Ranges, BytePrefix);
398 void MCStreamer::emitCVDefRangeDirective(
399 ArrayRef<std::pair<const MCSymbol *, const MCSymbol *>> Ranges,
400 codeview::DefRangeFramePointerRelHeader DRHdr) {
401 SmallString<20> BytePrefix;
402 copyBytesForDefRange(BytePrefix, codeview::S_DEFRANGE_FRAMEPOINTER_REL,
403 DRHdr);
404 emitCVDefRangeDirective(Ranges, BytePrefix);
407 void MCStreamer::emitEHSymAttributes(const MCSymbol *Symbol,
408 MCSymbol *EHSymbol) {
411 void MCStreamer::initSections(bool NoExecStack, const MCSubtargetInfo &STI) {
412 switchSection(getContext().getObjectFileInfo()->getTextSection());
415 void MCStreamer::assignFragment(MCSymbol *Symbol, MCFragment *Fragment) {
416 assert(Fragment);
417 Symbol->setFragment(Fragment);
419 // As we emit symbols into a section, track the order so that they can
420 // be sorted upon later. Zero is reserved to mean 'unemitted'.
421 SymbolOrdering[Symbol] = 1 + SymbolOrdering.size();
424 void MCStreamer::emitLabel(MCSymbol *Symbol, SMLoc Loc) {
425 Symbol->redefineIfPossible();
427 if (!Symbol->isUndefined() || Symbol->isVariable())
428 return getContext().reportError(Loc, "symbol '" + Twine(Symbol->getName()) +
429 "' is already defined");
431 assert(!Symbol->isVariable() && "Cannot emit a variable symbol!");
432 assert(getCurrentSectionOnly() && "Cannot emit before setting section!");
433 assert(!Symbol->getFragment() && "Unexpected fragment on symbol data!");
434 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
436 Symbol->setFragment(&getCurrentSectionOnly()->getDummyFragment());
438 MCTargetStreamer *TS = getTargetStreamer();
439 if (TS)
440 TS->emitLabel(Symbol);
443 void MCStreamer::emitConditionalAssignment(MCSymbol *Symbol,
444 const MCExpr *Value) {}
446 void MCStreamer::emitCFISections(bool EH, bool Debug) {}
448 void MCStreamer::emitCFIStartProc(bool IsSimple, SMLoc Loc) {
449 if (!FrameInfoStack.empty() &&
450 getCurrentSectionOnly() == FrameInfoStack.back().second)
451 return getContext().reportError(
452 Loc, "starting new .cfi frame before finishing the previous one");
454 MCDwarfFrameInfo Frame;
455 Frame.IsSimple = IsSimple;
456 emitCFIStartProcImpl(Frame);
458 const MCAsmInfo* MAI = Context.getAsmInfo();
459 if (MAI) {
460 for (const MCCFIInstruction& Inst : MAI->getInitialFrameState()) {
461 if (Inst.getOperation() == MCCFIInstruction::OpDefCfa ||
462 Inst.getOperation() == MCCFIInstruction::OpDefCfaRegister ||
463 Inst.getOperation() == MCCFIInstruction::OpLLVMDefAspaceCfa) {
464 Frame.CurrentCfaRegister = Inst.getRegister();
469 FrameInfoStack.emplace_back(DwarfFrameInfos.size(), getCurrentSectionOnly());
470 DwarfFrameInfos.push_back(Frame);
473 void MCStreamer::emitCFIStartProcImpl(MCDwarfFrameInfo &Frame) {
476 void MCStreamer::emitCFIEndProc() {
477 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
478 if (!CurFrame)
479 return;
480 emitCFIEndProcImpl(*CurFrame);
481 FrameInfoStack.pop_back();
484 void MCStreamer::emitCFIEndProcImpl(MCDwarfFrameInfo &Frame) {
485 // Put a dummy non-null value in Frame.End to mark that this frame has been
486 // closed.
487 Frame.End = (MCSymbol *)1;
490 MCSymbol *MCStreamer::emitCFILabel() {
491 // Return a dummy non-null value so that label fields appear filled in when
492 // generating textual assembly.
493 return (MCSymbol *)1;
496 void MCStreamer::emitCFIDefCfa(int64_t Register, int64_t Offset) {
497 MCSymbol *Label = emitCFILabel();
498 MCCFIInstruction Instruction =
499 MCCFIInstruction::cfiDefCfa(Label, Register, Offset);
500 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
501 if (!CurFrame)
502 return;
503 CurFrame->Instructions.push_back(Instruction);
504 CurFrame->CurrentCfaRegister = static_cast<unsigned>(Register);
507 void MCStreamer::emitCFIDefCfaOffset(int64_t Offset) {
508 MCSymbol *Label = emitCFILabel();
509 MCCFIInstruction Instruction =
510 MCCFIInstruction::cfiDefCfaOffset(Label, Offset);
511 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
512 if (!CurFrame)
513 return;
514 CurFrame->Instructions.push_back(Instruction);
517 void MCStreamer::emitCFIAdjustCfaOffset(int64_t Adjustment) {
518 MCSymbol *Label = emitCFILabel();
519 MCCFIInstruction Instruction =
520 MCCFIInstruction::createAdjustCfaOffset(Label, Adjustment);
521 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
522 if (!CurFrame)
523 return;
524 CurFrame->Instructions.push_back(Instruction);
527 void MCStreamer::emitCFIDefCfaRegister(int64_t Register) {
528 MCSymbol *Label = emitCFILabel();
529 MCCFIInstruction Instruction =
530 MCCFIInstruction::createDefCfaRegister(Label, Register);
531 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
532 if (!CurFrame)
533 return;
534 CurFrame->Instructions.push_back(Instruction);
535 CurFrame->CurrentCfaRegister = static_cast<unsigned>(Register);
538 void MCStreamer::emitCFILLVMDefAspaceCfa(int64_t Register, int64_t Offset,
539 int64_t AddressSpace) {
540 MCSymbol *Label = emitCFILabel();
541 MCCFIInstruction Instruction = MCCFIInstruction::createLLVMDefAspaceCfa(
542 Label, Register, Offset, AddressSpace);
543 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
544 if (!CurFrame)
545 return;
546 CurFrame->Instructions.push_back(Instruction);
547 CurFrame->CurrentCfaRegister = static_cast<unsigned>(Register);
550 void MCStreamer::emitCFIOffset(int64_t Register, int64_t Offset) {
551 MCSymbol *Label = emitCFILabel();
552 MCCFIInstruction Instruction =
553 MCCFIInstruction::createOffset(Label, Register, Offset);
554 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
555 if (!CurFrame)
556 return;
557 CurFrame->Instructions.push_back(Instruction);
560 void MCStreamer::emitCFIRelOffset(int64_t Register, int64_t Offset) {
561 MCSymbol *Label = emitCFILabel();
562 MCCFIInstruction Instruction =
563 MCCFIInstruction::createRelOffset(Label, Register, Offset);
564 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
565 if (!CurFrame)
566 return;
567 CurFrame->Instructions.push_back(Instruction);
570 void MCStreamer::emitCFIPersonality(const MCSymbol *Sym,
571 unsigned Encoding) {
572 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
573 if (!CurFrame)
574 return;
575 CurFrame->Personality = Sym;
576 CurFrame->PersonalityEncoding = Encoding;
579 void MCStreamer::emitCFILsda(const MCSymbol *Sym, unsigned Encoding) {
580 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
581 if (!CurFrame)
582 return;
583 CurFrame->Lsda = Sym;
584 CurFrame->LsdaEncoding = Encoding;
587 void MCStreamer::emitCFIRememberState() {
588 MCSymbol *Label = emitCFILabel();
589 MCCFIInstruction Instruction = MCCFIInstruction::createRememberState(Label);
590 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
591 if (!CurFrame)
592 return;
593 CurFrame->Instructions.push_back(Instruction);
596 void MCStreamer::emitCFIRestoreState() {
597 // FIXME: Error if there is no matching cfi_remember_state.
598 MCSymbol *Label = emitCFILabel();
599 MCCFIInstruction Instruction = MCCFIInstruction::createRestoreState(Label);
600 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
601 if (!CurFrame)
602 return;
603 CurFrame->Instructions.push_back(Instruction);
606 void MCStreamer::emitCFISameValue(int64_t Register) {
607 MCSymbol *Label = emitCFILabel();
608 MCCFIInstruction Instruction =
609 MCCFIInstruction::createSameValue(Label, Register);
610 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
611 if (!CurFrame)
612 return;
613 CurFrame->Instructions.push_back(Instruction);
616 void MCStreamer::emitCFIRestore(int64_t Register) {
617 MCSymbol *Label = emitCFILabel();
618 MCCFIInstruction Instruction =
619 MCCFIInstruction::createRestore(Label, Register);
620 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
621 if (!CurFrame)
622 return;
623 CurFrame->Instructions.push_back(Instruction);
626 void MCStreamer::emitCFIEscape(StringRef Values) {
627 MCSymbol *Label = emitCFILabel();
628 MCCFIInstruction Instruction = MCCFIInstruction::createEscape(Label, Values);
629 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
630 if (!CurFrame)
631 return;
632 CurFrame->Instructions.push_back(Instruction);
635 void MCStreamer::emitCFIGnuArgsSize(int64_t Size) {
636 MCSymbol *Label = emitCFILabel();
637 MCCFIInstruction Instruction =
638 MCCFIInstruction::createGnuArgsSize(Label, Size);
639 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
640 if (!CurFrame)
641 return;
642 CurFrame->Instructions.push_back(Instruction);
645 void MCStreamer::emitCFISignalFrame() {
646 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
647 if (!CurFrame)
648 return;
649 CurFrame->IsSignalFrame = true;
652 void MCStreamer::emitCFIUndefined(int64_t Register) {
653 MCSymbol *Label = emitCFILabel();
654 MCCFIInstruction Instruction =
655 MCCFIInstruction::createUndefined(Label, Register);
656 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
657 if (!CurFrame)
658 return;
659 CurFrame->Instructions.push_back(Instruction);
662 void MCStreamer::emitCFIRegister(int64_t Register1, int64_t Register2) {
663 MCSymbol *Label = emitCFILabel();
664 MCCFIInstruction Instruction =
665 MCCFIInstruction::createRegister(Label, Register1, Register2);
666 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
667 if (!CurFrame)
668 return;
669 CurFrame->Instructions.push_back(Instruction);
672 void MCStreamer::emitCFIWindowSave() {
673 MCSymbol *Label = emitCFILabel();
674 MCCFIInstruction Instruction =
675 MCCFIInstruction::createWindowSave(Label);
676 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
677 if (!CurFrame)
678 return;
679 CurFrame->Instructions.push_back(Instruction);
682 void MCStreamer::emitCFINegateRAState() {
683 MCSymbol *Label = emitCFILabel();
684 MCCFIInstruction Instruction = MCCFIInstruction::createNegateRAState(Label);
685 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
686 if (!CurFrame)
687 return;
688 CurFrame->Instructions.push_back(Instruction);
691 void MCStreamer::emitCFIReturnColumn(int64_t Register) {
692 MCDwarfFrameInfo *CurFrame = getCurrentDwarfFrameInfo();
693 if (!CurFrame)
694 return;
695 CurFrame->RAReg = Register;
698 WinEH::FrameInfo *MCStreamer::EnsureValidWinFrameInfo(SMLoc Loc) {
699 const MCAsmInfo *MAI = Context.getAsmInfo();
700 if (!MAI->usesWindowsCFI()) {
701 getContext().reportError(
702 Loc, ".seh_* directives are not supported on this target");
703 return nullptr;
705 if (!CurrentWinFrameInfo || CurrentWinFrameInfo->End) {
706 getContext().reportError(
707 Loc, ".seh_ directive must appear within an active frame");
708 return nullptr;
710 return CurrentWinFrameInfo;
713 void MCStreamer::emitWinCFIStartProc(const MCSymbol *Symbol, SMLoc Loc) {
714 const MCAsmInfo *MAI = Context.getAsmInfo();
715 if (!MAI->usesWindowsCFI())
716 return getContext().reportError(
717 Loc, ".seh_* directives are not supported on this target");
718 if (CurrentWinFrameInfo && !CurrentWinFrameInfo->End)
719 getContext().reportError(
720 Loc, "Starting a function before ending the previous one!");
722 MCSymbol *StartProc = emitCFILabel();
724 CurrentProcWinFrameInfoStartIndex = WinFrameInfos.size();
725 WinFrameInfos.emplace_back(
726 std::make_unique<WinEH::FrameInfo>(Symbol, StartProc));
727 CurrentWinFrameInfo = WinFrameInfos.back().get();
728 CurrentWinFrameInfo->TextSection = getCurrentSectionOnly();
731 void MCStreamer::emitWinCFIEndProc(SMLoc Loc) {
732 WinEH::FrameInfo *CurFrame = EnsureValidWinFrameInfo(Loc);
733 if (!CurFrame)
734 return;
735 if (CurFrame->ChainedParent)
736 getContext().reportError(Loc, "Not all chained regions terminated!");
738 MCSymbol *Label = emitCFILabel();
739 CurFrame->End = Label;
740 if (!CurFrame->FuncletOrFuncEnd)
741 CurFrame->FuncletOrFuncEnd = CurFrame->End;
743 for (size_t I = CurrentProcWinFrameInfoStartIndex, E = WinFrameInfos.size();
744 I != E; ++I)
745 emitWindowsUnwindTables(WinFrameInfos[I].get());
746 switchSection(CurFrame->TextSection);
749 void MCStreamer::emitWinCFIFuncletOrFuncEnd(SMLoc Loc) {
750 WinEH::FrameInfo *CurFrame = EnsureValidWinFrameInfo(Loc);
751 if (!CurFrame)
752 return;
753 if (CurFrame->ChainedParent)
754 getContext().reportError(Loc, "Not all chained regions terminated!");
756 MCSymbol *Label = emitCFILabel();
757 CurFrame->FuncletOrFuncEnd = Label;
760 void MCStreamer::emitWinCFIStartChained(SMLoc Loc) {
761 WinEH::FrameInfo *CurFrame = EnsureValidWinFrameInfo(Loc);
762 if (!CurFrame)
763 return;
765 MCSymbol *StartProc = emitCFILabel();
767 WinFrameInfos.emplace_back(std::make_unique<WinEH::FrameInfo>(
768 CurFrame->Function, StartProc, CurFrame));
769 CurrentWinFrameInfo = WinFrameInfos.back().get();
770 CurrentWinFrameInfo->TextSection = getCurrentSectionOnly();
773 void MCStreamer::emitWinCFIEndChained(SMLoc Loc) {
774 WinEH::FrameInfo *CurFrame = EnsureValidWinFrameInfo(Loc);
775 if (!CurFrame)
776 return;
777 if (!CurFrame->ChainedParent)
778 return getContext().reportError(
779 Loc, "End of a chained region outside a chained region!");
781 MCSymbol *Label = emitCFILabel();
783 CurFrame->End = Label;
784 CurrentWinFrameInfo = const_cast<WinEH::FrameInfo *>(CurFrame->ChainedParent);
787 void MCStreamer::emitWinEHHandler(const MCSymbol *Sym, bool Unwind, bool Except,
788 SMLoc Loc) {
789 WinEH::FrameInfo *CurFrame = EnsureValidWinFrameInfo(Loc);
790 if (!CurFrame)
791 return;
792 if (CurFrame->ChainedParent)
793 return getContext().reportError(
794 Loc, "Chained unwind areas can't have handlers!");
795 CurFrame->ExceptionHandler = Sym;
796 if (!Except && !Unwind)
797 getContext().reportError(Loc, "Don't know what kind of handler this is!");
798 if (Unwind)
799 CurFrame->HandlesUnwind = true;
800 if (Except)
801 CurFrame->HandlesExceptions = true;
804 void MCStreamer::emitWinEHHandlerData(SMLoc Loc) {
805 WinEH::FrameInfo *CurFrame = EnsureValidWinFrameInfo(Loc);
806 if (!CurFrame)
807 return;
808 if (CurFrame->ChainedParent)
809 getContext().reportError(Loc, "Chained unwind areas can't have handlers!");
812 void MCStreamer::emitCGProfileEntry(const MCSymbolRefExpr *From,
813 const MCSymbolRefExpr *To, uint64_t Count) {
816 static MCSection *getWinCFISection(MCContext &Context, unsigned *NextWinCFIID,
817 MCSection *MainCFISec,
818 const MCSection *TextSec) {
819 // If this is the main .text section, use the main unwind info section.
820 if (TextSec == Context.getObjectFileInfo()->getTextSection())
821 return MainCFISec;
823 const auto *TextSecCOFF = cast<MCSectionCOFF>(TextSec);
824 auto *MainCFISecCOFF = cast<MCSectionCOFF>(MainCFISec);
825 unsigned UniqueID = TextSecCOFF->getOrAssignWinCFISectionID(NextWinCFIID);
827 // If this section is COMDAT, this unwind section should be COMDAT associative
828 // with its group.
829 const MCSymbol *KeySym = nullptr;
830 if (TextSecCOFF->getCharacteristics() & COFF::IMAGE_SCN_LNK_COMDAT) {
831 KeySym = TextSecCOFF->getCOMDATSymbol();
833 // In a GNU environment, we can't use associative comdats. Instead, do what
834 // GCC does, which is to make plain comdat selectany section named like
835 // ".[px]data$_Z3foov".
836 if (!Context.getAsmInfo()->hasCOFFAssociativeComdats()) {
837 std::string SectionName = (MainCFISecCOFF->getName() + "$" +
838 TextSecCOFF->getName().split('$').second)
839 .str();
840 return Context.getCOFFSection(
841 SectionName,
842 MainCFISecCOFF->getCharacteristics() | COFF::IMAGE_SCN_LNK_COMDAT,
843 MainCFISecCOFF->getKind(), "", COFF::IMAGE_COMDAT_SELECT_ANY);
847 return Context.getAssociativeCOFFSection(MainCFISecCOFF, KeySym, UniqueID);
850 MCSection *MCStreamer::getAssociatedPDataSection(const MCSection *TextSec) {
851 return getWinCFISection(getContext(), &NextWinCFIID,
852 getContext().getObjectFileInfo()->getPDataSection(),
853 TextSec);
856 MCSection *MCStreamer::getAssociatedXDataSection(const MCSection *TextSec) {
857 return getWinCFISection(getContext(), &NextWinCFIID,
858 getContext().getObjectFileInfo()->getXDataSection(),
859 TextSec);
862 void MCStreamer::emitSyntaxDirective() {}
864 static unsigned encodeSEHRegNum(MCContext &Ctx, MCRegister Reg) {
865 return Ctx.getRegisterInfo()->getSEHRegNum(Reg);
868 void MCStreamer::emitWinCFIPushReg(MCRegister Register, SMLoc Loc) {
869 WinEH::FrameInfo *CurFrame = EnsureValidWinFrameInfo(Loc);
870 if (!CurFrame)
871 return;
873 MCSymbol *Label = emitCFILabel();
875 WinEH::Instruction Inst = Win64EH::Instruction::PushNonVol(
876 Label, encodeSEHRegNum(Context, Register));
877 CurFrame->Instructions.push_back(Inst);
880 void MCStreamer::emitWinCFISetFrame(MCRegister Register, unsigned Offset,
881 SMLoc Loc) {
882 WinEH::FrameInfo *CurFrame = EnsureValidWinFrameInfo(Loc);
883 if (!CurFrame)
884 return;
885 if (CurFrame->LastFrameInst >= 0)
886 return getContext().reportError(
887 Loc, "frame register and offset can be set at most once");
888 if (Offset & 0x0F)
889 return getContext().reportError(Loc, "offset is not a multiple of 16");
890 if (Offset > 240)
891 return getContext().reportError(
892 Loc, "frame offset must be less than or equal to 240");
894 MCSymbol *Label = emitCFILabel();
896 WinEH::Instruction Inst = Win64EH::Instruction::SetFPReg(
897 Label, encodeSEHRegNum(getContext(), Register), Offset);
898 CurFrame->LastFrameInst = CurFrame->Instructions.size();
899 CurFrame->Instructions.push_back(Inst);
902 void MCStreamer::emitWinCFIAllocStack(unsigned Size, SMLoc Loc) {
903 WinEH::FrameInfo *CurFrame = EnsureValidWinFrameInfo(Loc);
904 if (!CurFrame)
905 return;
906 if (Size == 0)
907 return getContext().reportError(Loc,
908 "stack allocation size must be non-zero");
909 if (Size & 7)
910 return getContext().reportError(
911 Loc, "stack allocation size is not a multiple of 8");
913 MCSymbol *Label = emitCFILabel();
915 WinEH::Instruction Inst = Win64EH::Instruction::Alloc(Label, Size);
916 CurFrame->Instructions.push_back(Inst);
919 void MCStreamer::emitWinCFISaveReg(MCRegister Register, unsigned Offset,
920 SMLoc Loc) {
921 WinEH::FrameInfo *CurFrame = EnsureValidWinFrameInfo(Loc);
922 if (!CurFrame)
923 return;
925 if (Offset & 7)
926 return getContext().reportError(
927 Loc, "register save offset is not 8 byte aligned");
929 MCSymbol *Label = emitCFILabel();
931 WinEH::Instruction Inst = Win64EH::Instruction::SaveNonVol(
932 Label, encodeSEHRegNum(Context, Register), Offset);
933 CurFrame->Instructions.push_back(Inst);
936 void MCStreamer::emitWinCFISaveXMM(MCRegister Register, unsigned Offset,
937 SMLoc Loc) {
938 WinEH::FrameInfo *CurFrame = EnsureValidWinFrameInfo(Loc);
939 if (!CurFrame)
940 return;
941 if (Offset & 0x0F)
942 return getContext().reportError(Loc, "offset is not a multiple of 16");
944 MCSymbol *Label = emitCFILabel();
946 WinEH::Instruction Inst = Win64EH::Instruction::SaveXMM(
947 Label, encodeSEHRegNum(Context, Register), Offset);
948 CurFrame->Instructions.push_back(Inst);
951 void MCStreamer::emitWinCFIPushFrame(bool Code, SMLoc Loc) {
952 WinEH::FrameInfo *CurFrame = EnsureValidWinFrameInfo(Loc);
953 if (!CurFrame)
954 return;
955 if (!CurFrame->Instructions.empty())
956 return getContext().reportError(
957 Loc, "If present, PushMachFrame must be the first UOP");
959 MCSymbol *Label = emitCFILabel();
961 WinEH::Instruction Inst = Win64EH::Instruction::PushMachFrame(Label, Code);
962 CurFrame->Instructions.push_back(Inst);
965 void MCStreamer::emitWinCFIEndProlog(SMLoc Loc) {
966 WinEH::FrameInfo *CurFrame = EnsureValidWinFrameInfo(Loc);
967 if (!CurFrame)
968 return;
970 MCSymbol *Label = emitCFILabel();
972 CurFrame->PrologEnd = Label;
975 void MCStreamer::emitCOFFSafeSEH(MCSymbol const *Symbol) {}
977 void MCStreamer::emitCOFFSymbolIndex(MCSymbol const *Symbol) {}
979 void MCStreamer::emitCOFFSectionIndex(MCSymbol const *Symbol) {}
981 void MCStreamer::emitCOFFSecRel32(MCSymbol const *Symbol, uint64_t Offset) {}
983 void MCStreamer::emitCOFFImgRel32(MCSymbol const *Symbol, int64_t Offset) {}
985 /// EmitRawText - If this file is backed by an assembly streamer, this dumps
986 /// the specified string in the output .s file. This capability is
987 /// indicated by the hasRawTextSupport() predicate.
988 void MCStreamer::emitRawTextImpl(StringRef String) {
989 // This is not llvm_unreachable for the sake of out of tree backend
990 // developers who may not have assembly streamers and should serve as a
991 // reminder to not accidentally call EmitRawText in the absence of such.
992 report_fatal_error("EmitRawText called on an MCStreamer that doesn't support "
993 "it (target backend is likely missing an AsmStreamer "
994 "implementation)");
997 void MCStreamer::emitRawText(const Twine &T) {
998 SmallString<128> Str;
999 emitRawTextImpl(T.toStringRef(Str));
1002 void MCStreamer::emitWindowsUnwindTables() {}
1004 void MCStreamer::emitWindowsUnwindTables(WinEH::FrameInfo *Frame) {}
1006 void MCStreamer::finish(SMLoc EndLoc) {
1007 if ((!DwarfFrameInfos.empty() && !DwarfFrameInfos.back().End) ||
1008 (!WinFrameInfos.empty() && !WinFrameInfos.back()->End)) {
1009 getContext().reportError(EndLoc, "Unfinished frame!");
1010 return;
1013 MCTargetStreamer *TS = getTargetStreamer();
1014 if (TS)
1015 TS->finish();
1017 finishImpl();
1020 void MCStreamer::maybeEmitDwarf64Mark() {
1021 if (Context.getDwarfFormat() != dwarf::DWARF64)
1022 return;
1023 AddComment("DWARF64 Mark");
1024 emitInt32(dwarf::DW_LENGTH_DWARF64);
1027 void MCStreamer::emitDwarfUnitLength(uint64_t Length, const Twine &Comment) {
1028 assert(Context.getDwarfFormat() == dwarf::DWARF64 ||
1029 Length <= dwarf::DW_LENGTH_lo_reserved);
1030 maybeEmitDwarf64Mark();
1031 AddComment(Comment);
1032 emitIntValue(Length, dwarf::getDwarfOffsetByteSize(Context.getDwarfFormat()));
1035 MCSymbol *MCStreamer::emitDwarfUnitLength(const Twine &Prefix,
1036 const Twine &Comment) {
1037 maybeEmitDwarf64Mark();
1038 AddComment(Comment);
1039 MCSymbol *Lo = Context.createTempSymbol(Prefix + "_start");
1040 MCSymbol *Hi = Context.createTempSymbol(Prefix + "_end");
1042 emitAbsoluteSymbolDiff(
1043 Hi, Lo, dwarf::getDwarfOffsetByteSize(Context.getDwarfFormat()));
1044 // emit the begin symbol after we generate the length field.
1045 emitLabel(Lo);
1046 // Return the Hi symbol to the caller.
1047 return Hi;
1050 void MCStreamer::emitDwarfLineStartLabel(MCSymbol *StartSym) {
1051 // Set the value of the symbol, as we are at the start of the line table.
1052 emitLabel(StartSym);
1055 void MCStreamer::emitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
1056 visitUsedExpr(*Value);
1057 Symbol->setVariableValue(Value);
1059 MCTargetStreamer *TS = getTargetStreamer();
1060 if (TS)
1061 TS->emitAssignment(Symbol, Value);
1064 void MCTargetStreamer::prettyPrintAsm(MCInstPrinter &InstPrinter,
1065 uint64_t Address, const MCInst &Inst,
1066 const MCSubtargetInfo &STI,
1067 raw_ostream &OS) {
1068 InstPrinter.printInst(&Inst, Address, "", STI, OS);
1071 void MCStreamer::visitUsedSymbol(const MCSymbol &Sym) {
1074 void MCStreamer::visitUsedExpr(const MCExpr &Expr) {
1075 switch (Expr.getKind()) {
1076 case MCExpr::Target:
1077 cast<MCTargetExpr>(Expr).visitUsedExpr(*this);
1078 break;
1080 case MCExpr::Constant:
1081 break;
1083 case MCExpr::Binary: {
1084 const MCBinaryExpr &BE = cast<MCBinaryExpr>(Expr);
1085 visitUsedExpr(*BE.getLHS());
1086 visitUsedExpr(*BE.getRHS());
1087 break;
1090 case MCExpr::SymbolRef:
1091 visitUsedSymbol(cast<MCSymbolRefExpr>(Expr).getSymbol());
1092 break;
1094 case MCExpr::Unary:
1095 visitUsedExpr(*cast<MCUnaryExpr>(Expr).getSubExpr());
1096 break;
1100 void MCStreamer::emitInstruction(const MCInst &Inst, const MCSubtargetInfo &) {
1101 // Scan for values.
1102 for (unsigned i = Inst.getNumOperands(); i--;)
1103 if (Inst.getOperand(i).isExpr())
1104 visitUsedExpr(*Inst.getOperand(i).getExpr());
1107 void MCStreamer::emitPseudoProbe(uint64_t Guid, uint64_t Index, uint64_t Type,
1108 uint64_t Attr, uint64_t Discriminator,
1109 const MCPseudoProbeInlineStack &InlineStack,
1110 MCSymbol *FnSym) {
1111 auto &Context = getContext();
1113 // Create a symbol at in the current section for use in the probe.
1114 MCSymbol *ProbeSym = Context.createTempSymbol();
1116 // Set the value of the symbol to use for the MCPseudoProbe.
1117 emitLabel(ProbeSym);
1119 // Create a (local) probe entry with the symbol.
1120 MCPseudoProbe Probe(ProbeSym, Guid, Index, Type, Attr, Discriminator);
1122 // Add the probe entry to this section's entries.
1123 Context.getMCPseudoProbeTable().getProbeSections().addPseudoProbe(
1124 FnSym, Probe, InlineStack);
1127 void MCStreamer::emitAbsoluteSymbolDiff(const MCSymbol *Hi, const MCSymbol *Lo,
1128 unsigned Size) {
1129 // Get the Hi-Lo expression.
1130 const MCExpr *Diff =
1131 MCBinaryExpr::createSub(MCSymbolRefExpr::create(Hi, Context),
1132 MCSymbolRefExpr::create(Lo, Context), Context);
1134 const MCAsmInfo *MAI = Context.getAsmInfo();
1135 if (!MAI->doesSetDirectiveSuppressReloc()) {
1136 emitValue(Diff, Size);
1137 return;
1140 // Otherwise, emit with .set (aka assignment).
1141 MCSymbol *SetLabel = Context.createTempSymbol("set");
1142 emitAssignment(SetLabel, Diff);
1143 emitSymbolValue(SetLabel, Size);
1146 void MCStreamer::emitAbsoluteSymbolDiffAsULEB128(const MCSymbol *Hi,
1147 const MCSymbol *Lo) {
1148 // Get the Hi-Lo expression.
1149 const MCExpr *Diff =
1150 MCBinaryExpr::createSub(MCSymbolRefExpr::create(Hi, Context),
1151 MCSymbolRefExpr::create(Lo, Context), Context);
1153 emitULEB128Value(Diff);
1156 void MCStreamer::emitAssemblerFlag(MCAssemblerFlag Flag) {}
1157 void MCStreamer::emitThumbFunc(MCSymbol *Func) {}
1158 void MCStreamer::emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {}
1159 void MCStreamer::beginCOFFSymbolDef(const MCSymbol *Symbol) {
1160 llvm_unreachable("this directive only supported on COFF targets");
1162 void MCStreamer::endCOFFSymbolDef() {
1163 llvm_unreachable("this directive only supported on COFF targets");
1165 void MCStreamer::emitFileDirective(StringRef Filename) {}
1166 void MCStreamer::emitFileDirective(StringRef Filename, StringRef CompilerVerion,
1167 StringRef TimeStamp, StringRef Description) {
1169 void MCStreamer::emitCOFFSymbolStorageClass(int StorageClass) {
1170 llvm_unreachable("this directive only supported on COFF targets");
1172 void MCStreamer::emitCOFFSymbolType(int Type) {
1173 llvm_unreachable("this directive only supported on COFF targets");
1175 void MCStreamer::emitXCOFFLocalCommonSymbol(MCSymbol *LabelSym, uint64_t Size,
1176 MCSymbol *CsectSym,
1177 Align Alignment) {
1178 llvm_unreachable("this directive only supported on XCOFF targets");
1181 void MCStreamer::emitXCOFFSymbolLinkageWithVisibility(MCSymbol *Symbol,
1182 MCSymbolAttr Linkage,
1183 MCSymbolAttr Visibility) {
1184 llvm_unreachable("emitXCOFFSymbolLinkageWithVisibility is only supported on "
1185 "XCOFF targets");
1188 void MCStreamer::emitXCOFFRenameDirective(const MCSymbol *Name,
1189 StringRef Rename) {
1190 llvm_unreachable("emitXCOFFRenameDirective is only supported on "
1191 "XCOFF targets");
1194 void MCStreamer::emitXCOFFRefDirective(const MCSymbol *Symbol) {
1195 llvm_unreachable("emitXCOFFRefDirective is only supported on XCOFF targets");
1198 void MCStreamer::emitXCOFFExceptDirective(const MCSymbol *Symbol,
1199 const MCSymbol *Trap,
1200 unsigned Lang, unsigned Reason,
1201 unsigned FunctionSize,
1202 bool hasDebug) {
1203 report_fatal_error("emitXCOFFExceptDirective is only supported on "
1204 "XCOFF targets");
1207 void MCStreamer::emitELFSize(MCSymbol *Symbol, const MCExpr *Value) {}
1208 void MCStreamer::emitELFSymverDirective(const MCSymbol *OriginalSym,
1209 StringRef Name, bool KeepOriginalSym) {}
1210 void MCStreamer::emitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
1211 Align ByteAlignment) {}
1212 void MCStreamer::emitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
1213 uint64_t Size, Align ByteAlignment) {}
1214 void MCStreamer::changeSection(MCSection *, const MCExpr *) {}
1215 void MCStreamer::emitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {}
1216 void MCStreamer::emitBytes(StringRef Data) {}
1217 void MCStreamer::emitBinaryData(StringRef Data) { emitBytes(Data); }
1218 void MCStreamer::emitValueImpl(const MCExpr *Value, unsigned Size, SMLoc Loc) {
1219 visitUsedExpr(*Value);
1221 void MCStreamer::emitULEB128Value(const MCExpr *Value) {}
1222 void MCStreamer::emitSLEB128Value(const MCExpr *Value) {}
1223 void MCStreamer::emitFill(const MCExpr &NumBytes, uint64_t Value, SMLoc Loc) {}
1224 void MCStreamer::emitFill(const MCExpr &NumValues, int64_t Size, int64_t Expr,
1225 SMLoc Loc) {}
1226 void MCStreamer::emitValueToAlignment(Align Alignment, int64_t Value,
1227 unsigned ValueSize,
1228 unsigned MaxBytesToEmit) {}
1229 void MCStreamer::emitCodeAlignment(Align Alignment, const MCSubtargetInfo *STI,
1230 unsigned MaxBytesToEmit) {}
1231 void MCStreamer::emitValueToOffset(const MCExpr *Offset, unsigned char Value,
1232 SMLoc Loc) {}
1233 void MCStreamer::emitBundleAlignMode(Align Alignment) {}
1234 void MCStreamer::emitBundleLock(bool AlignToEnd) {}
1235 void MCStreamer::finishImpl() {}
1236 void MCStreamer::emitBundleUnlock() {}
1238 void MCStreamer::switchSection(MCSection *Section, const MCExpr *Subsection) {
1239 assert(Section && "Cannot switch to a null section!");
1240 MCSectionSubPair curSection = SectionStack.back().first;
1241 SectionStack.back().second = curSection;
1242 if (MCSectionSubPair(Section, Subsection) != curSection) {
1243 changeSection(Section, Subsection);
1244 SectionStack.back().first = MCSectionSubPair(Section, Subsection);
1245 assert(!Section->hasEnded() && "Section already ended");
1246 MCSymbol *Sym = Section->getBeginSymbol();
1247 if (Sym && !Sym->isInSection())
1248 emitLabel(Sym);
1252 MCSymbol *MCStreamer::endSection(MCSection *Section) {
1253 // TODO: keep track of the last subsection so that this symbol appears in the
1254 // correct place.
1255 MCSymbol *Sym = Section->getEndSymbol(Context);
1256 if (Sym->isInSection())
1257 return Sym;
1259 switchSection(Section);
1260 emitLabel(Sym);
1261 return Sym;
1264 static VersionTuple
1265 targetVersionOrMinimumSupportedOSVersion(const Triple &Target,
1266 VersionTuple TargetVersion) {
1267 VersionTuple Min = Target.getMinimumSupportedOSVersion();
1268 return !Min.empty() && Min > TargetVersion ? Min : TargetVersion;
1271 static MCVersionMinType
1272 getMachoVersionMinLoadCommandType(const Triple &Target) {
1273 assert(Target.isOSDarwin() && "expected a darwin OS");
1274 switch (Target.getOS()) {
1275 case Triple::MacOSX:
1276 case Triple::Darwin:
1277 return MCVM_OSXVersionMin;
1278 case Triple::IOS:
1279 assert(!Target.isMacCatalystEnvironment() &&
1280 "mac Catalyst should use LC_BUILD_VERSION");
1281 return MCVM_IOSVersionMin;
1282 case Triple::TvOS:
1283 return MCVM_TvOSVersionMin;
1284 case Triple::WatchOS:
1285 return MCVM_WatchOSVersionMin;
1286 default:
1287 break;
1289 llvm_unreachable("unexpected OS type");
1292 static VersionTuple getMachoBuildVersionSupportedOS(const Triple &Target) {
1293 assert(Target.isOSDarwin() && "expected a darwin OS");
1294 switch (Target.getOS()) {
1295 case Triple::MacOSX:
1296 case Triple::Darwin:
1297 return VersionTuple(10, 14);
1298 case Triple::IOS:
1299 // Mac Catalyst always uses the build version load command.
1300 if (Target.isMacCatalystEnvironment())
1301 return VersionTuple();
1302 [[fallthrough]];
1303 case Triple::TvOS:
1304 return VersionTuple(12);
1305 case Triple::WatchOS:
1306 return VersionTuple(5);
1307 case Triple::DriverKit:
1308 // DriverKit always uses the build version load command.
1309 return VersionTuple();
1310 default:
1311 break;
1313 llvm_unreachable("unexpected OS type");
1316 static MachO::PlatformType
1317 getMachoBuildVersionPlatformType(const Triple &Target) {
1318 assert(Target.isOSDarwin() && "expected a darwin OS");
1319 switch (Target.getOS()) {
1320 case Triple::MacOSX:
1321 case Triple::Darwin:
1322 return MachO::PLATFORM_MACOS;
1323 case Triple::IOS:
1324 if (Target.isMacCatalystEnvironment())
1325 return MachO::PLATFORM_MACCATALYST;
1326 return Target.isSimulatorEnvironment() ? MachO::PLATFORM_IOSSIMULATOR
1327 : MachO::PLATFORM_IOS;
1328 case Triple::TvOS:
1329 return Target.isSimulatorEnvironment() ? MachO::PLATFORM_TVOSSIMULATOR
1330 : MachO::PLATFORM_TVOS;
1331 case Triple::WatchOS:
1332 return Target.isSimulatorEnvironment() ? MachO::PLATFORM_WATCHOSSIMULATOR
1333 : MachO::PLATFORM_WATCHOS;
1334 case Triple::DriverKit:
1335 return MachO::PLATFORM_DRIVERKIT;
1336 default:
1337 break;
1339 llvm_unreachable("unexpected OS type");
1342 void MCStreamer::emitVersionForTarget(
1343 const Triple &Target, const VersionTuple &SDKVersion,
1344 const Triple *DarwinTargetVariantTriple,
1345 const VersionTuple &DarwinTargetVariantSDKVersion) {
1346 if (!Target.isOSBinFormatMachO() || !Target.isOSDarwin())
1347 return;
1348 // Do we even know the version?
1349 if (Target.getOSMajorVersion() == 0)
1350 return;
1352 VersionTuple Version;
1353 switch (Target.getOS()) {
1354 case Triple::MacOSX:
1355 case Triple::Darwin:
1356 Target.getMacOSXVersion(Version);
1357 break;
1358 case Triple::IOS:
1359 case Triple::TvOS:
1360 Version = Target.getiOSVersion();
1361 break;
1362 case Triple::WatchOS:
1363 Version = Target.getWatchOSVersion();
1364 break;
1365 case Triple::DriverKit:
1366 Version = Target.getDriverKitVersion();
1367 break;
1368 default:
1369 llvm_unreachable("unexpected OS type");
1371 assert(Version.getMajor() != 0 && "A non-zero major version is expected");
1372 auto LinkedTargetVersion =
1373 targetVersionOrMinimumSupportedOSVersion(Target, Version);
1374 auto BuildVersionOSVersion = getMachoBuildVersionSupportedOS(Target);
1375 bool ShouldEmitBuildVersion = false;
1376 if (BuildVersionOSVersion.empty() ||
1377 LinkedTargetVersion >= BuildVersionOSVersion) {
1378 if (Target.isMacCatalystEnvironment() && DarwinTargetVariantTriple &&
1379 DarwinTargetVariantTriple->isMacOSX()) {
1380 emitVersionForTarget(*DarwinTargetVariantTriple,
1381 DarwinTargetVariantSDKVersion,
1382 /*DarwinTargetVariantTriple=*/nullptr,
1383 /*DarwinTargetVariantSDKVersion=*/VersionTuple());
1384 emitDarwinTargetVariantBuildVersion(
1385 getMachoBuildVersionPlatformType(Target),
1386 LinkedTargetVersion.getMajor(),
1387 LinkedTargetVersion.getMinor().value_or(0),
1388 LinkedTargetVersion.getSubminor().value_or(0), SDKVersion);
1389 return;
1391 emitBuildVersion(getMachoBuildVersionPlatformType(Target),
1392 LinkedTargetVersion.getMajor(),
1393 LinkedTargetVersion.getMinor().value_or(0),
1394 LinkedTargetVersion.getSubminor().value_or(0), SDKVersion);
1395 ShouldEmitBuildVersion = true;
1398 if (const Triple *TVT = DarwinTargetVariantTriple) {
1399 if (Target.isMacOSX() && TVT->isMacCatalystEnvironment()) {
1400 auto TVLinkedTargetVersion =
1401 targetVersionOrMinimumSupportedOSVersion(*TVT, TVT->getiOSVersion());
1402 emitDarwinTargetVariantBuildVersion(
1403 getMachoBuildVersionPlatformType(*TVT),
1404 TVLinkedTargetVersion.getMajor(),
1405 TVLinkedTargetVersion.getMinor().value_or(0),
1406 TVLinkedTargetVersion.getSubminor().value_or(0),
1407 DarwinTargetVariantSDKVersion);
1411 if (ShouldEmitBuildVersion)
1412 return;
1414 emitVersionMin(getMachoVersionMinLoadCommandType(Target),
1415 LinkedTargetVersion.getMajor(),
1416 LinkedTargetVersion.getMinor().value_or(0),
1417 LinkedTargetVersion.getSubminor().value_or(0), SDKVersion);