28class MCUnwindV2EpilogTargetExpr final :
public MCTargetExpr {
29 const WinEH::FrameInfo &FrameInfo;
35 MCUnwindV2EpilogTargetExpr(
const WinEH::FrameInfo &FrameInfo,
36 const WinEH::FrameInfo::Epilog &
Epilog,
38 : FrameInfo(FrameInfo), UnwindV2Start(
Epilog.UnwindV2Start),
39 EpilogEnd(
Epilog.End), EpilogSize(EpilogSize_), Loc(
Epilog.Loc) {
40 assert(UnwindV2Start &&
"Epilog must have a start");
41 assert(EpilogEnd &&
"Epilog must have an end");
45 static MCUnwindV2EpilogTargetExpr *
46 create(
const WinEH::FrameInfo &FrameInfo,
47 const WinEH::FrameInfo::Epilog &
Epilog, uint8_t EpilogSize_,
49 return new (Ctx) MCUnwindV2EpilogTargetExpr(FrameInfo,
Epilog, EpilogSize_);
52 void printImpl(raw_ostream &OS,
const MCAsmInfo *MAI)
const override {
54 UnwindV2Start->
print(OS, MAI);
57 bool evaluateAsRelocatableImpl(MCValue &Res,
58 const MCAssembler *Asm)
const override;
60 void visitUsedExpr(MCStreamer &Streamer)
const override {
64 MCFragment *findAssociatedFragment()
const override {
74 for (
const auto &
I : Insns) {
93 Count += (
I.Offset > 512 * 1024 - 8) ? 3 : 2;
124 if (inst.
Offset > 512 * 1024 - 8) {
137 b2 |= (((inst.
Offset - 8) >> 3) & 0x0F) << 4;
213static std::optional<int64_t>
224 if (!Diff->evaluateAsAbsolute(value, Assembler))
240 info->Symbol = Label;
243 bool LastEpilogIsAtEnd =
false;
244 bool AddPaddingEpilogCode =
false;
246 bool EnableUnwindV2 = (
info->Version >= 2) && !
info->EpilogMap.empty();
247 if (EnableUnwindV2) {
248 auto &LastEpilog =
info->EpilogMap.back().second;
257 LastEpilog.UnwindV2Start &&
258 "If unwind v2 is enabled, epilog must have a unwind v2 start marker");
259 assert(LastEpilog.End &&
"Epilog must have an end");
261 OS->
getAssembler(), LastEpilog.End, LastEpilog.UnwindV2Start);
264 "Failed to evaluate epilog size for Unwind v2 in " +
265 info->Function->getName());
269 if (*MaybeSize >= (int64_t)UINT8_MAX) {
271 "Epilog size is too large for Unwind v2 in " +
272 info->Function->getName());
275 EpilogSize = *MaybeSize + 1;
286 LastEpilogIsAtEnd = (LastEpilogToFuncEnd == EpilogSize);
290 size_t numEpilogCodes =
291 info->EpilogMap.size() + (LastEpilogIsAtEnd ? 0 : 1);
292 if ((numEpilogCodes % 2) != 0) {
293 AddPaddingEpilogCode =
true;
298 if ((
size_t)numCodes + numEpilogCodes > UINT8_MAX) {
300 "Too many unwind codes with Unwind v2 enabled in " +
301 info->Function->getName());
305 numCodes += numEpilogCodes;
310 if (
info->ChainedParent)
313 if (
info->HandlesUnwind)
315 if (
info->HandlesExceptions)
328 if (
info->LastFrameInst >= 0) {
336 if (EnableUnwindV2) {
344 uint8_t Flags = LastEpilogIsAtEnd ? 0x01 : 0;
348 if (LastEpilogIsAtEnd)
355 auto *MCE = MCUnwindV2EpilogTargetExpr::create(*
info,
Epilog.second,
356 EpilogSize, context);
361 if (AddPaddingEpilogCode)
366 for (
uint8_t c = 0; c < numInst; ++c) {
368 info->Instructions.pop_back();
387 else if (numCodes == 0) {
395bool MCUnwindV2EpilogTargetExpr::evaluateAsRelocatableImpl(
401 Asm->getContext().reportError(
402 Loc,
"Failed to evaluate epilog offset for Unwind v2 in " +
407 constexpr uint16_t MaxEpilogOffset = 0x0fff;
408 if (*
Offset > MaxEpilogOffset) {
409 Asm->getContext().reportError(
410 Loc,
"Epilog offset is too large for Unwind v2 in " +
417 if (
Size != (EpilogSize - 1)) {
418 Asm->getContext().reportError(
419 Loc,
"Size of this epilog does not match size of last epilog in " +
424 auto HighBits = *
Offset >> 8;
448 bool HandlerData)
const {
478 std::optional<int64_t> MaybeDiff =
491 std::optional<int64_t> MaybeDistance =
497 for (
const auto &
I : Insns) {
513 if (Distance != InstructionBytes) {
515 SMLoc(),
"Incorrect size for " + Name +
" " +
Type +
": " +
517 " bytes of instructions in range, but .seh directives "
518 "corresponding to " +
519 Twine(InstructionBytes) +
" bytes\n");
525 for (
const auto &
I : Insns) {
640 b = (inst.
Offset >> 4) & 0x1F;
657 b = (w & 0x00FF0000) >> 16;
659 b = (w & 0x0000FF00) >> 8;
681 b |= (inst.
Offset >> 3) & 0x1F;
686 b |= ((inst.
Offset >> 3) - 1) & 0x3F;
691 b |= (inst.
Offset >> 3) & 0x3F;
697 b = 0xD0 | ((reg & 0xC) >> 2);
699 b = ((reg & 0x3) << 6) | (inst.
Offset >> 3);
705 b = 0xD4 | ((reg & 0x8) >> 3);
707 b = ((reg & 0x7) << 5) | ((inst.
Offset >> 3) - 1);
713 b = 0xC8 | ((reg & 0xC) >> 2);
715 b = ((reg & 0x3) << 6) | (inst.
Offset >> 3);
721 b = 0xCC | ((reg & 0xC) >> 2);
723 b = ((reg & 0x3) << 6) | ((inst.
Offset >> 3) - 1);
729 assert((reg % 2) == 0 &&
"Saved reg must be 19+2*X");
731 b = 0xD6 | ((reg & 0x7) >> 2);
733 b = ((reg & 0x3) << 6) | (inst.
Offset >> 3);
739 b = 0xDC | ((reg & 0x4) >> 2);
741 b = ((reg & 0x3) << 6) | (inst.
Offset >> 3);
749 b = ((reg & 0x7) << 5) | ((inst.
Offset >> 3) - 1);
755 b = 0xD8 | ((reg & 0x4) >> 2);
757 b = ((reg & 0x3) << 6) | (inst.
Offset >> 3);
763 b = 0xDA | ((reg & 0x4) >> 2);
765 b = ((reg & 0x3) << 6) | ((inst.
Offset >> 3) - 1);
814 int Writeback =
Op / 6;
818 if (Writeback || Paired ||
Mode == 2)
825 b = inst.
Register | (Writeback << 5) | (Paired << 6);
844 b = ((inst.
Offset & 0xC0) >> 1) | reg;
846 b = 0xC0 | (inst.
Offset & 0x3F);
856 b = ((inst.
Offset & 0xC0) >> 1) | 0x10 | reg;
858 b = 0xC0 | (inst.
Offset & 0x3F);
871 const std::vector<MCSymbol *>& Epilogs,
873 for (
auto *EpilogStart : Epilogs) {
874 auto InstrsIter =
info->EpilogMap.find(EpilogStart);
876 "Epilog not found in EpilogMap");
877 const auto &Instrs = InstrsIter->second.Instructions;
879 if (Instrs.size() != EpilogInstrs.size())
883 for (
unsigned i = 0; i < Instrs.size(); ++i)
884 if (Instrs[i] != EpilogInstrs[i]) {
897 unsigned PrevOffset = -1;
898 unsigned PrevRegister = -1;
910 Inst.Register == 29) {
914 Inst.Register == 19 && Inst.Offset <= 248) {
920 Inst.Register == PrevRegister + 2 &&
921 Inst.Offset == PrevOffset + 16) {
936 PrevRegister = Inst.Register;
938 PrevOffset = Inst.Offset;
939 PrevRegister = Inst.Register;
953 const std::vector<WinEH::Instruction> &
Epilog) {
960 for (
int I =
Epilog.size() - 1;
I >= 0;
I--) {
975 int PrologCodeBytes) {
981 const std::vector<WinEH::Instruction> &
Epilog =
982 info->EpilogMap[Sym].Instructions;
988 if (DistanceFromEnd / 4 !=
Epilog.size())
996 if (PrologCodeBytes <= 31 &&
998 RetVal = PrologCodeBytes;
1007 if (
Offset > 31 || PrologCodeBytes > 124)
1012 info->EpilogMap.erase(Sym);
1017 int PackedEpilogOffset) {
1018 if (PackedEpilogOffset == 0) {
1024 }
else if (PackedEpilogOffset == 1) {
1034 unsigned RegI = 0, RegF = 0;
1035 int Predecrement = 0;
1047 bool StandaloneLR =
false, FPLRPair =
false;
1057 for (
auto It =
info->Instructions.begin(), EndIt =
info->Instructions.end();
1058 It != EndIt; It++) {
1062 if (Location != Start)
1067 if (Location != Start2)
1073 if (Location != Start2 && Location != Start3)
1075 Predecrement = Inst.
Offset;
1080 if (Location != Start2 && Location != Start3)
1082 Predecrement = Inst.
Offset;
1086 StandaloneLR =
true;
1090 Location = FloatRegs;
1117 StandaloneLR =
true;
1121 Location = FloatRegs;
1128 StandaloneLR =
true;
1129 Location = FloatRegs;
1136 if (Location != FloatRegs || RegF == 0 || Inst.
Register != 8 + RegF ||
1137 Inst.
Offset != 8 * (RegI + (StandaloneLR ? 1 : 0) + RegF))
1140 Location = InputArgs;
1143 if ((Location != Start2 && Location != Start3) || Inst.
Register != 8)
1145 Predecrement = Inst.
Offset;
1147 Location = FloatRegs;
1150 if ((Location !=
IntRegs && Location != FloatRegs) ||
1152 Inst.
Offset != 8 * (RegI + (StandaloneLR ? 1 : 0) + RegF))
1155 Location = FloatRegs;
1160 else if (Location == FloatRegs)
1166 if (Location !=
IntRegs && Location != FloatRegs && Location != InputArgs)
1168 Location = InputArgs;
1173 if (Location != Start2 && Location != Start3 && Location !=
IntRegs &&
1174 Location != FloatRegs && Location != InputArgs &&
1175 Location != StackAdjust)
1181 if (Location == Start2) {
1182 auto NextIt = It + 1;
1183 if (NextIt != EndIt) {
1187 assert(Predecrement == 0);
1190 Predecrement = Inst.
Offset;
1192 StandaloneLR =
true;
1193 Location = FloatRegs;
1207 Location = StackAdjust;
1212 if (Location != Start2 && Location != Start3 && Location !=
IntRegs &&
1213 Location != FloatRegs && Location != InputArgs)
1216 Location = FrameRecord;
1221 if (Location != StackAdjust || Inst.
Offset != 0)
1223 Location = FrameRecord;
1227 if (Location != FrameRecord)
1271 if (RegI > 10 || RegF > 8)
1273 if (StandaloneLR && FPLRPair)
1275 if (FPLRPair && Location != End)
1277 if (Nops != 0 && Nops != 4)
1279 if (PAC && !FPLRPair)
1306 if (StandaloneLR && RegI == 1 && RegF > 0)
1308 int IntSZ = 8 * RegI;
1311 int FpSZ = 8 * RegF;
1312 int SavSZ = (IntSZ + FpSZ + 8 * 8 *
H + 0xF) & ~0xF;
1313 if (Predecrement != SavSZ)
1320 if (FrameSize > 0x1FF)
1322 assert(RegF != 1 &&
"One single float reg not allowed");
1325 assert(FuncLength <= 0x7FF &&
"FuncLength should have been checked earlier");
1327 int CR = PAC ? 2 : FPLRPair ? 3 : StandaloneLR ? 1 : 0;
1328 info->PackedInfo |= Flag << 0;
1329 info->PackedInfo |= (FuncLength & 0x7FF) << 2;
1330 info->PackedInfo |= (RegF & 0x7) << 13;
1331 info->PackedInfo |= (RegI & 0xF) << 16;
1332 info->PackedInfo |= (
H & 0x1) << 20;
1333 info->PackedInfo |= (CR & 0x3) << 21;
1334 info->PackedInfo |= (FrameSize & 0x1FF) << 23;
1343 std::vector<MCSymbol *> EpilogStarts;
1345 EpilogStarts.push_back(
I.first);
1348 std::vector<MCSymbol *> AddedEpilogs;
1349 for (
auto *S : EpilogStarts) {
1351 auto &EpilogInstrs =
info->EpilogMap[S].Instructions;
1357 if (MatchingEpilog) {
1359 "Duplicate epilog not found");
1360 EpilogInfo[EpilogStart] = EpilogInfo.
lookup(MatchingEpilog);
1363 EpilogInstrs.clear();
1365 EpilogInstrs)) >= 0) {
1366 EpilogInfo[EpilogStart] = PrologOffset;
1370 EpilogInfo[EpilogStart] += 1;
1373 EpilogInstrs.
clear();
1375 EpilogInfo[EpilogStart] = TotalCodeBytes;
1376 TotalCodeBytes += CodeBytes;
1377 AddedEpilogs.push_back(EpilogStart);
1384 int64_t RawFuncLength) {
1385 if (
info->PrologEnd)
1387 info->PrologEnd,
info->Function->getName(),
1389 struct EpilogStartEnd {
1396 for (
auto &
I :
info->EpilogMap) {
1398 auto &Instrs =
I.second.Instructions;
1401 info->Function->getName(),
"epilogue");
1403 "Epilogs should be monotonically ordered");
1410 int64_t SegLimit = 0xFFFFC;
1411 int64_t SegOffset = 0;
1413 if (RawFuncLength > SegLimit) {
1415 int64_t RemainingLength = RawFuncLength;
1417 while (RemainingLength > SegLimit) {
1421 int64_t SegLength = SegLimit;
1422 int64_t SegEnd = SegOffset + SegLength;
1426 while (
E < Epilogs.
size() && Epilogs[
E].End < SegEnd) {
1428 EpilogsInSegment[Epilogs[
E].Start] = Epilogs[
E].Offset;
1439 if (
E < Epilogs.
size() && Epilogs[
E].Offset <= SegEnd)
1441 SegLength = Epilogs[
E].Offset - SegOffset;
1444 SegOffset, SegLength, !SegOffset);
1445 Seg.Epilogs = std::move(EpilogsInSegment);
1446 info->Segments.push_back(Seg);
1448 SegOffset += SegLength;
1449 RemainingLength -= SegLength;
1458 for (;
E < Epilogs.
size(); ++
E)
1459 LastSeg.Epilogs[Epilogs[
E].Start] = Epilogs[
E].Offset;
1460 info->Segments.push_back(LastSeg);
1466 bool TryPacked =
true) {
1475 info->Symbol = Label;
1478 bool HasEpilogs = (Seg.
Epilogs.size() != 0);
1483 int PackedEpilogOffset = HasEpilogs ?
1490 if (
info->Segments.size() == 1 && PackedEpilogOffset >= 0 &&
1491 uint32_t(PackedEpilogOffset) < PrologCodeBytes &&
1492 !
info->HandlesExceptions && SegLength <= 0x7ff && TryPacked) {
1507 PrologCodeBytes += 1;
1508 if (PackedEpilogOffset >= 0)
1509 PackedEpilogOffset += 1;
1517 PackedEpilogOffset = 0;
1520 uint32_t TotalCodeBytes = PrologCodeBytes;
1528 uint32_t CodeWords = TotalCodeBytes / 4;
1529 uint32_t CodeWordsMod = TotalCodeBytes % 4;
1533 PackedEpilogOffset >= 0 ? PackedEpilogOffset : Seg.
Epilogs.size();
1534 bool ExtensionWord = EpilogCount > 31 || TotalCodeBytes > 124;
1535 if (!ExtensionWord) {
1536 row1 |= (EpilogCount & 0x1F) << 22;
1537 row1 |= (CodeWords & 0x1F) << 27;
1539 if (
info->HandlesExceptions)
1541 if (PackedEpilogOffset >= 0)
1543 row1 |= SegLength & 0x3FFFF;
1547 if (ExtensionWord) {
1549 if (CodeWords > 0xFF || EpilogCount > 0xFFFF)
1551 "SEH unwind data splitting is only implemented for large functions, "
1552 "cases of too many code words or too many epilogs will be done "
1555 row2 |= (CodeWords & 0xFF) << 16;
1556 row2 |= (EpilogCount & 0xFFFF);
1560 if (PackedEpilogOffset < 0) {
1562 for (
auto &
I : EpilogInfo) {
1570 row3 |= (EpilogIndex & 0x3FF) << 22;
1591 auto &EpilogInstrs =
info->EpilogMap[
I.first].Instructions;
1596 int32_t BytesMod = CodeWords * 4 - TotalCodeBytes;
1598 for (
int i = 0; i < BytesMod; i++)
1601 if (
info->HandlesExceptions)
1611 bool TryPacked =
true) {
1619 if (
info->empty()) {
1620 info->EmitAttempted =
true;
1623 if (
info->EmitAttempted) {
1630 SMLoc(),
"Earlier .seh_handlerdata for " +
info->Function->getName() +
1631 " skipped due to no unwind info at the time "
1632 "(.seh_handlerdata too early?), but the function later "
1633 "did get unwind info that can't be emitted");
1638 for (
auto &
I :
info->EpilogMap)
1641 int64_t RawFuncLength;
1642 if (!
info->FuncletOrFuncEnd) {
1681 for (
auto &S :
info->Segments)
1685 info->Instructions.clear();
1690 for (
const auto &
I : Insns) {
1748 for (J = 3; J > 0; J--)
1749 if (
I.Offset & (0xffu << (8 * J)))
1760 bool *HasCustom =
nullptr) {
1762 for (
const auto &
I : Insns) {
1824 std::optional<int64_t> MaybeDistance =
1829 bool HasCustom =
false;
1833 if (Distance != InstructionBytes) {
1835 SMLoc(),
"Incorrect size for " + Name +
" " +
Type +
": " +
1837 " bytes of instructions in range, but .seh directives "
1838 "corresponding to " +
1839 Twine(InstructionBytes) +
" bytes\n");
1871 w = 0x8000 | (inst.
Register & 0x1fff) | (lr << 13);
1872 streamer.
emitInt8((w >> 8) & 0xff);
1873 streamer.
emitInt8((w >> 0) & 0xff);
1896 w = 0xe800 | (inst.
Offset / 4);
1897 streamer.
emitInt8((w >> 8) & 0xff);
1898 streamer.
emitInt8((w >> 0) & 0xff);
1903 w = 0xec00 | (inst.
Register & 0x0ff) | (lr << 8);
1904 streamer.
emitInt8((w >> 8) & 0xff);
1905 streamer.
emitInt8((w >> 0) & 0xff);
1932 streamer.
emitInt8((w >> 8) & 0xff);
1933 streamer.
emitInt8((w >> 0) & 0xff);
1940 streamer.
emitInt8((w >> 16) & 0xff);
1941 streamer.
emitInt8((w >> 8) & 0xff);
1942 streamer.
emitInt8((w >> 0) & 0xff);
1949 streamer.
emitInt8((w >> 8) & 0xff);
1950 streamer.
emitInt8((w >> 0) & 0xff);
1957 streamer.
emitInt8((w >> 16) & 0xff);
1958 streamer.
emitInt8((w >> 8) & 0xff);
1959 streamer.
emitInt8((w >> 0) & 0xff);
1977 for (i = 3; i > 0; i--)
1978 if (inst.
Offset & (0xffu << (8 * i)))
1993 const std::vector<WinEH::Instruction> &
Epilog,
1994 bool CanTweakProlog) {
2003 int EndIdx = CanTweakProlog ? 1 : 0;
2004 for (
int I =
Epilog.size() - 1;
I >= EndIdx;
I--) {
2011 if (CanTweakProlog) {
2029 int PrologCodeBytes) {
2031 if (
info->EpilogMap.size() != 1)
2041 if (
info->Instructions.empty() ||
Epilog.empty())
2047 streamer,
info->FuncletOrFuncEnd,
info->EpilogMap.begin()->first);
2052 if (DistanceFromEnd != InstructionBytes)
2060 if (PrologCodeBytes <= 31 &&
2062 RetVal = PrologCodeBytes;
2072 if (
Offset > 31 || PrologCodeBytes > 63)
2081 info->EpilogMap.clear();
2086 unsigned &Folded,
int &
IntRegs) {
2087 if (Mask & (1 << 14)) {
2091 if (Mask & (1 << 11)) {
2101 while ((Mask & 1) == 0) {
2105 if ((Mask & (Mask + 1)) != 0)
2109 while (Mask & (1 <<
N))
2128 bool Homing =
false;
2129 bool HasR11 =
false;
2130 bool HasChain =
false;
2135 unsigned StackAdjust = 0;
2170 if (Step != 1 && Step != 2)
2185 if (Step == 1 && Inst.
Register == 0x0f) {
2193 if (Step != 1 && Step != 2)
2205 if (Step != 3 || !HasR11 ||
IntRegs >= 0 || PF > 0)
2212 if (Step != 3 || !HasR11 || (
IntRegs < 0 && PF == 0))
2219 if (Step != 1 && Step != 2 && Step != 3 && Step != 4)
2232 if (Step != 1 && Step != 2 && Step != 3 && Step != 4 && Step != 5)
2236 if (Inst.
Offset / 4 >= 0x3f4)
2238 StackAdjust = Inst.
Offset / 4;
2243 if (HasR11 && !HasChain) {
2252 if (HasChain && !HasLR)
2256 if (
info->EpilogMap.size() > 1)
2261 if (
info->EpilogMap.size() == 0) {
2268 info->EpilogMap.begin()->second;
2273 streamer,
info->FuncletOrFuncEnd,
info->EpilogMap.begin()->first);
2278 if (DistanceFromEnd != InstructionBytes)
2281 bool GotStackAdjust =
false;
2282 bool GotFloatRegs =
false;
2283 bool GotIntRegs =
false;
2284 bool GotHomingRestore =
false;
2285 bool GotLRRestore =
false;
2286 bool NeedsReturn =
false;
2287 bool GotReturn =
false;
2309 if (Inst.
Offset / 4 >= 0x3f4)
2312 if (Homing && FloatRegs < 0 &&
IntRegs < 0 && StackAdjust == 0 &&
2313 PF == 0 && Inst.
Offset == 16) {
2314 GotHomingRestore =
true;
2317 if (StackAdjust > 0) {
2319 if (StackAdjust != Inst.
Offset / 4)
2321 GotStackAdjust =
true;
2322 }
else if (PF == Inst.
Offset / 4) {
2324 StackAdjust = Inst.
Offset / 4;
2325 GotStackAdjust =
true;
2332 }
else if (Step == 7 || Step == 8 || Step == 9) {
2333 if (!Homing || Inst.
Offset != 16)
2335 GotHomingRestore =
true;
2342 if (Step != 6 && Step != 7)
2345 if (FloatRegs != (
int)(Inst.
Register - 8))
2347 GotFloatRegs =
true;
2354 if (Step != 6 && Step != 7 && Step != 8)
2358 if (Homing && HasLR) {
2362 GotLRRestore =
true;
2368 if (HasLR != (Inst.
Offset == 1))
2371 GotLRRestore = Inst.
Offset == 1;
2390 if (Step != 6 && Step != 7 && Step != 8)
2395 bool CurHasLR =
false, CurHasR11 =
false;
2400 if (EF != PF && EF != StackAdjust)
2403 if (Homing && HasLR) {
2407 GotLRRestore =
true;
2413 if (CurHasLR != HasLR)
2415 GotLRRestore = CurHasLR;
2442 if (Step != 6 && Step != 7 && Step != 8 && Step != 9)
2444 if (!Homing || Inst.
Offset != 20 || GotLRRestore)
2446 GotLRRestore =
true;
2447 GotHomingRestore =
true;
2457 if (Step != 6 && Step != 7 && Step != 8 && Step != 9 && Step != 10)
2466 if (StackAdjust > 0 && !GotStackAdjust && EF == 0)
2468 if (FloatRegs >= 0 && !GotFloatRegs)
2470 if (
IntRegs >= 0 && !GotIntRegs)
2472 if (Homing && !GotHomingRestore)
2474 if (HasLR && !GotLRRestore)
2476 if (NeedsReturn && !GotReturn)
2480 assert(PF == 0 || EF == 0 ||
2482 if (PF > 0 || EF > 0) {
2483 StackAdjust = PF > 0 ? (PF - 1) : (EF - 1);
2484 assert(StackAdjust <= 3);
2485 StackAdjust |= 0x3f0;
2487 StackAdjust |= 1 << 2;
2489 StackAdjust |= 1 << 3;
2492 assert(FuncLength <= 0x7FF &&
"FuncLength should have been checked earlier");
2493 int Flag =
info->Fragment ? 0x02 : 0x01;
2494 int H = Homing ? 1 : 0;
2495 int L = HasLR ? 1 : 0;
2496 int C = HasChain ? 1 : 0;
2503 }
else if (FloatRegs >= 0) {
2512 info->PackedInfo |= Flag << 0;
2513 info->PackedInfo |= (FuncLength & 0x7FF) << 2;
2514 info->PackedInfo |= (Ret & 0x3) << 13;
2515 info->PackedInfo |=
H << 15;
2516 info->PackedInfo |=
Reg << 16;
2517 info->PackedInfo |= R << 19;
2518 info->PackedInfo |= L << 20;
2519 info->PackedInfo |=
C << 21;
2520 assert(StackAdjust <= 0x3ff);
2521 info->PackedInfo |= StackAdjust << 22;
2528 bool TryPacked =
true) {
2536 if (
info->empty()) {
2537 info->EmitAttempted =
true;
2540 if (
info->EmitAttempted) {
2547 SMLoc(),
"Earlier .seh_handlerdata for " +
info->Function->getName() +
2548 " skipped due to no unwind info at the time "
2549 "(.seh_handlerdata too early?), but the function later "
2550 "did get unwind info that can't be emitted");
2559 info->Symbol = Label;
2561 if (!
info->PrologEnd)
2563 info->Function->getName() +
2564 " not correctly terminated");
2566 if (
info->PrologEnd && !
info->Fragment)
2568 info->PrologEnd,
info->Function->getName(),
2570 for (
auto &
I :
info->EpilogMap) {
2574 info->Function->getName(),
"epilogue");
2575 if (
Epilog.Instructions.empty() ||
2578 SMLoc(),
"Epilogue in " +
info->Function->getName() +
2579 " not correctly terminated");
2582 std::optional<int64_t> RawFuncLength;
2583 const MCExpr *FuncLengthExpr =
nullptr;
2584 if (!
info->FuncletOrFuncEnd) {
2603 FuncLength = (
uint32_t)*RawFuncLength / 2;
2604 if (FuncLength > 0x3FFFF)
2607 uint32_t TotalCodeBytes = PrologCodeBytes;
2609 if (!
info->HandlesExceptions && RawFuncLength && FuncLength <= 0x7ff &&
2623 int PackedEpilogOffset =
2629 std::vector<MCSymbol *> AddedEpilogs;
2631 bool CanTweakProlog =
true;
2632 for (
auto &
I :
info->EpilogMap) {
2634 auto &EpilogInstrs =
I.second.Instructions;
2640 if (MatchingEpilog) {
2642 "Duplicate epilog not found");
2643 EpilogInfo[EpilogStart] = EpilogInfo.
lookup(MatchingEpilog);
2646 EpilogInstrs.clear();
2648 info->Instructions, EpilogInstrs, CanTweakProlog)) >= 0) {
2649 if (CanTweakProlog) {
2652 info->Instructions.front() = EpilogInstrs.back();
2654 CanTweakProlog =
false;
2656 EpilogInfo[EpilogStart] = PrologOffset;
2659 EpilogInstrs.clear();
2661 EpilogInfo[EpilogStart] = TotalCodeBytes;
2662 TotalCodeBytes += CodeBytes;
2663 AddedEpilogs.push_back(EpilogStart);
2669 uint32_t CodeWords = TotalCodeBytes / 4;
2670 uint32_t CodeWordsMod = TotalCodeBytes % 4;
2674 PackedEpilogOffset >= 0 ? PackedEpilogOffset :
info->EpilogMap.size();
2675 bool ExtensionWord = EpilogCount > 31 || CodeWords > 15;
2676 if (!ExtensionWord) {
2677 row1 |= (EpilogCount & 0x1F) << 23;
2678 row1 |= (CodeWords & 0x0F) << 28;
2680 if (
info->HandlesExceptions)
2682 if (PackedEpilogOffset >= 0)
2686 row1 |= FuncLength & 0x3FFFF;
2696 if (ExtensionWord) {
2698 if (CodeWords > 0xFF || EpilogCount > 0xFFFF)
2701 row2 |= (CodeWords & 0xFF) << 16;
2702 row2 |= (EpilogCount & 0xFFFF);
2706 if (PackedEpilogOffset < 0) {
2708 for (
auto &
I : EpilogInfo) {
2712 std::optional<int64_t> MaybeEpilogOffset =
2714 const MCExpr *OffsetExpr =
nullptr;
2716 if (MaybeEpilogOffset)
2717 EpilogOffset = *MaybeEpilogOffset / 2;
2721 assert(
info->EpilogMap.contains(EpilogStart));
2722 unsigned Condition =
info->EpilogMap[EpilogStart].Condition;
2723 assert(Condition <= 0xf);
2726 row3 |= Condition << 20;
2727 row3 |= (EpilogIndex & 0x3FF) << 24;
2728 if (MaybeEpilogOffset)
2740 for (
uint8_t c = 0; c < numInst; ++c) {
2742 info->Instructions.pop_back();
2747 for (
auto &
I :
info->EpilogMap) {
2748 auto &EpilogInstrs =
I.second.Instructions;
2753 int32_t BytesMod = CodeWords * 4 - TotalCodeBytes;
2755 for (
int i = 0; i < BytesMod; i++)
2758 if (
info->HandlesExceptions)
2770 for (
const auto &S :
info->Segments) {
2772 if (
info->PackedInfo)
2789 if (
info->PackedInfo)
2825 bool HandlerData)
const {
2832 if (!
info->FuncletOrFuncEnd) {
2870 bool HandlerData)
const {
2877 if (!
info->FuncletOrFuncEnd) {
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static int checkARM64PackedEpilog(MCStreamer &streamer, WinEH::FrameInfo *info, WinEH::FrameInfo::Segment *Seg, int PrologCodeBytes)
static void ARM64EmitUnwindInfoForSegment(MCStreamer &streamer, WinEH::FrameInfo *info, WinEH::FrameInfo::Segment &Seg, bool TryPacked=true)
static uint32_t ARMCountOfUnwindCodes(ArrayRef< WinEH::Instruction > Insns)
static uint32_t ARM64CountOfUnwindCodes(ArrayRef< WinEH::Instruction > Insns)
static void checkARMInstructions(MCStreamer &Streamer, ArrayRef< WinEH::Instruction > Insns, const MCSymbol *Begin, const MCSymbol *End, StringRef Name, StringRef Type)
static bool isARMTerminator(const WinEH::Instruction &inst)
static void ARM64EmitUnwindCode(MCStreamer &streamer, const WinEH::Instruction &inst)
static void simplifyARM64Opcodes(std::vector< WinEH::Instruction > &Instructions, bool Reverse)
static void ARMEmitUnwindCode(MCStreamer &streamer, const WinEH::Instruction &inst)
static std::optional< int64_t > GetOptionalAbsDifference(const MCAssembler &Assembler, const MCSymbol *LHS, const MCSymbol *RHS)
static int getARM64OffsetInProlog(const std::vector< WinEH::Instruction > &Prolog, const std::vector< WinEH::Instruction > &Epilog)
static void ARMEmitRuntimeFunction(MCStreamer &streamer, const WinEH::FrameInfo *info)
static bool tryARM64PackedUnwind(WinEH::FrameInfo *info, uint32_t FuncLength, int PackedEpilogOffset)
static void EmitUnwindInfo(MCStreamer &streamer, WinEH::FrameInfo *info)
static void EmitUnwindCode(MCStreamer &streamer, const MCSymbol *begin, WinEH::Instruction &inst)
static void ARM64EmitUnwindInfo(MCStreamer &streamer, WinEH::FrameInfo *info, bool TryPacked=true)
static int getARMOffsetInProlog(const std::vector< WinEH::Instruction > &Prolog, const std::vector< WinEH::Instruction > &Epilog, bool CanTweakProlog)
static void ARM64EmitRuntimeFunction(MCStreamer &streamer, const WinEH::FrameInfo *info)
static void EmitSymbolRefWithOfs(MCStreamer &streamer, const MCSymbol *Base, int64_t Offset)
static bool parseRegMask(unsigned Mask, bool &HasLR, bool &HasR11, unsigned &Folded, int &IntRegs)
static int checkARMPackedEpilog(MCStreamer &streamer, WinEH::FrameInfo *info, int PrologCodeBytes)
static int64_t GetAbsDifference(MCStreamer &Streamer, const MCSymbol *LHS, const MCSymbol *RHS)
static void ARM64FindSegmentsInFunction(MCStreamer &streamer, WinEH::FrameInfo *info, int64_t RawFuncLength)
static bool tryARMPackedUnwind(MCStreamer &streamer, WinEH::FrameInfo *info, uint32_t FuncLength)
static MCSymbol * FindMatchingEpilog(const std::vector< WinEH::Instruction > &EpilogInstrs, const std::vector< MCSymbol * > &Epilogs, const WinEH::FrameInfo *info)
static void EmitRuntimeFunction(MCStreamer &streamer, const WinEH::FrameInfo *info)
static void checkARM64Instructions(MCStreamer &Streamer, ArrayRef< WinEH::Instruction > Insns, const MCSymbol *Begin, const MCSymbol *End, StringRef Name, StringRef Type)
static const MCExpr * GetSubDivExpr(MCStreamer &Streamer, const MCSymbol *LHS, const MCSymbol *RHS, int Div)
static void EmitAbsDifference(MCStreamer &Streamer, const MCSymbol *LHS, const MCSymbol *RHS)
static void ARMEmitUnwindInfo(MCStreamer &streamer, WinEH::FrameInfo *info, bool TryPacked=true)
static uint32_t ARMCountOfInstructionBytes(ArrayRef< WinEH::Instruction > Insns, bool *HasCustom=nullptr)
static void ARM64ProcessEpilogs(WinEH::FrameInfo *info, WinEH::FrameInfo::Segment *Seg, uint32_t &TotalCodeBytes, MapVector< MCSymbol *, uint32_t > &EpilogInfo)
static uint8_t CountOfUnwindCodes(std::vector< WinEH::Instruction > &Insns)
static void printImpl(const MCAsmInfo &MAI, raw_ostream &OS, const MCSpecifierExpr &Expr)
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
static const MCPhysReg IntRegs[32]
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
size - Get the array size.
Tagged union holding either a T or a Error.
MCContext & getContext() const
static const MCBinaryExpr * createAdd(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx, SMLoc Loc=SMLoc())
static const MCBinaryExpr * createOr(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
static const MCBinaryExpr * createDiv(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
static LLVM_ABI const MCConstantExpr * create(int64_t Value, MCContext &Ctx, bool PrintInHex=false, unsigned SizeInBytes=0)
Context object for machine code objects.
LLVM_ABI MCSymbol * createTempSymbol()
Create a temporary symbol with a unique name.
LLVM_ABI void reportError(SMLoc L, const Twine &Msg)
Base class for the full range of assembler expressions which are needed for parsing.
Streaming object file generation interface.
MCAssembler & getAssembler()
void appendContents(ArrayRef< char > Contents)
void addFixup(const MCExpr *Value, MCFixupKind Kind)
void ensureHeadroom(size_t Headroom)
Instances of this class represent a uniqued identifier for a section in the current translation unit.
Streaming machine code generation interface.
virtual MCSymbol * emitCFILabel()
When emitting an object file, create and emit a real label.
MCSection * getAssociatedPDataSection(const MCSection *TextSec)
Get the .pdata section used for the given section.
MCContext & getContext() const
MCSection * getAssociatedXDataSection(const MCSection *TextSec)
Get the .xdata section used for the given section.
void emitValue(const MCExpr *Value, unsigned Size, SMLoc Loc=SMLoc())
virtual void emitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc())
Emit a label for Symbol into the current section.
virtual void emitValueToAlignment(Align Alignment, int64_t Fill=0, uint8_t FillLen=1, unsigned MaxBytesToEmit=0)
Emit some number of copies of Value until the byte alignment ByteAlignment is reached.
void emitInt16(uint64_t Value)
virtual void switchSection(MCSection *Section, uint32_t Subsec=0)
Set the current section where code is being emitted to Section.
void emitInt32(uint64_t Value)
ArrayRef< std::unique_ptr< WinEH::FrameInfo > > getWinFrameInfos() const
void emitInt8(uint64_t Value)
Represent a reference to a symbol from inside an expression.
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx, SMLoc Loc=SMLoc())
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
LLVM_ABI void print(raw_ostream &OS, const MCAsmInfo *MAI) const
print - Print the value to the stream OS.
StringRef getName() const
getName - Get the symbol name.
MCFragment * getFragment() const
static MCValue get(const MCSymbol *SymA, const MCSymbol *SymB=nullptr, int64_t Val=0, uint32_t Specifier=0)
This class implements a map that also provides access to all stored values in a deterministic order.
bool contains(const KeyT &Key) const
ValueT lookup(const KeyT &Key) const
Represents a location in source code.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StackOffset holds a fixed and a scalable offset in bytes.
StringRef - Represent a constant reference to a string, i.e.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
The instances of the Type class are immutable: once they are created, they are never changed.
void EmitUnwindInfo(MCStreamer &Streamer, WinEH::FrameInfo *FI, bool HandlerData) const override
void Emit(MCStreamer &Streamer) const override
This emits the unwind info sections (.pdata and .xdata in PE/COFF).
void Emit(MCStreamer &Streamer) const override
This emits the unwind info sections (.pdata and .xdata in PE/COFF).
void EmitUnwindInfo(MCStreamer &Streamer, WinEH::FrameInfo *FI, bool HandlerData) const override
void Emit(MCStreamer &Streamer) const override
This emits the unwind info sections (.pdata and .xdata in PE/COFF).
void EmitUnwindInfo(MCStreamer &Streamer, WinEH::FrameInfo *FI, bool HandlerData) const override
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
@ UNW_TerminateHandler
UNW_TerminateHandler - Specifies that this function has a termination handler.
@ UNW_ExceptionHandler
UNW_ExceptionHandler - Specifies that this function has an exception handler.
@ UNW_ChainInfo
UNW_ChainInfo - Specifies that this UnwindInfo structure is chained to another one.
UnwindOpcodes
UnwindOpcodes - Enumeration whose values specify a single operation in the prolog of a function.
@ UOP_WideSaveRegsR4R11LR
This is an optimization pass for GlobalISel generic memory operations.
auto reverse(ContainerTy &&C)
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
FunctionAddr VTableAddr Count
auto reverse_conditionally(ContainerTy &&C, bool ShouldReverse)
Return a range that conditionally reverses C.
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
@ FK_Data_2
A two-byte fixup.
DWARFExpression::Operation Op
This struct is a compact representation of a valid (non-zero power of two) alignment.
std::vector< Instruction > Instructions
MapVector< MCSymbol *, int64_t > Epilogs
const MCSymbol * Function