28class MCUnwindV2EpilogTargetExpr final :
public MCTargetExpr {
36 MCUnwindV2EpilogTargetExpr(
const WinEH::FrameInfo &FrameInfo,
37 const WinEH::FrameInfo::Epilog &
Epilog,
40 UnwindV2Start(
Epilog.UnwindV2Start), EpilogEnd(
Epilog.End),
41 EpilogSize(EpilogSize_), Loc(
Epilog.Loc) {}
44 static MCUnwindV2EpilogTargetExpr *
45 create(
const WinEH::FrameInfo &FrameInfo,
46 const WinEH::FrameInfo::Epilog &
Epilog, uint8_t EpilogSize_,
48 return new (Ctx) MCUnwindV2EpilogTargetExpr(FrameInfo,
Epilog, EpilogSize_);
51 void printImpl(raw_ostream &OS,
const MCAsmInfo *MAI)
const override {
53 UnwindV2Start->
print(OS, MAI);
56 bool evaluateAsRelocatableImpl(MCValue &Res,
57 const MCAssembler *Asm)
const override;
59 void visitUsedExpr(MCStreamer &Streamer)
const override {
63 MCFragment *findAssociatedFragment()
const override {
73 for (
const auto &
I : Insns) {
92 Count += (
I.Offset > 512 * 1024 - 8) ? 3 : 2;
123 if (inst.
Offset > 512 * 1024 - 8) {
136 b2 |= (((inst.
Offset - 8) >> 3) & 0x0F) << 4;
212static std::optional<int64_t>
223 if (!Diff->evaluateAsAbsolute(value, Assembler))
239 info->Symbol = Label;
242 bool LastEpilogIsAtEnd =
false;
243 bool AddPaddingEpilogCode =
false;
245 bool EnableUnwindV2 = (
info->Version >= 2) && !
info->EpilogMap.empty();
246 if (EnableUnwindV2) {
247 auto &LastEpilog =
info->EpilogMap.back().second;
256 OS->
getAssembler(), LastEpilog.End, LastEpilog.UnwindV2Start);
259 "Failed to evaluate epilog size for Unwind v2 in " +
260 info->Function->getName());
264 if (*MaybeSize >= (int64_t)UINT8_MAX) {
266 "Epilog size is too large for Unwind v2 in " +
267 info->Function->getName());
270 EpilogSize = *MaybeSize + 1;
277 LastEpilogIsAtEnd = (LastEpilogToFuncEnd == EpilogSize);
280 size_t numEpilogCodes =
281 info->EpilogMap.size() + (LastEpilogIsAtEnd ? 0 : 1);
282 if ((numEpilogCodes % 2) != 0) {
283 AddPaddingEpilogCode =
true;
288 if ((
size_t)numCodes + numEpilogCodes > UINT8_MAX) {
290 "Too many unwind codes with Unwind v2 enabled in " +
291 info->Function->getName());
295 numCodes += numEpilogCodes;
300 if (
info->ChainedParent)
303 if (
info->HandlesUnwind)
305 if (
info->HandlesExceptions)
318 if (
info->LastFrameInst >= 0) {
326 if (EnableUnwindV2) {
334 uint8_t Flags = LastEpilogIsAtEnd ? 0x01 : 0;
338 if (LastEpilogIsAtEnd)
345 auto *MCE = MCUnwindV2EpilogTargetExpr::create(*
info,
Epilog.second,
346 EpilogSize, context);
351 if (AddPaddingEpilogCode)
356 for (
uint8_t c = 0; c < numInst; ++c) {
358 info->Instructions.pop_back();
377 else if (numCodes == 0) {
385bool MCUnwindV2EpilogTargetExpr::evaluateAsRelocatableImpl(
391 Asm->getContext().reportError(
392 Loc,
"Failed to evaluate epilog offset for Unwind v2 in " +
397 constexpr uint16_t MaxEpilogOffset = 0x0fff;
398 if (*
Offset > MaxEpilogOffset) {
399 Asm->getContext().reportError(
401 "Epilog offset is too large for Unwind v2 in " +
Function->getName());
407 if (
Size != (EpilogSize - 1)) {
408 Asm->getContext().reportError(
409 Loc,
"Size of this epilog does not match size of last epilog in " +
414 auto HighBits = *
Offset >> 8;
438 bool HandlerData)
const {
468 std::optional<int64_t> MaybeDiff =
481 std::optional<int64_t> MaybeDistance =
487 for (
const auto &
I : Insns) {
503 if (Distance != InstructionBytes) {
505 SMLoc(),
"Incorrect size for " + Name +
" " +
Type +
": " +
507 " bytes of instructions in range, but .seh directives "
508 "corresponding to " +
509 Twine(InstructionBytes) +
" bytes\n");
515 for (
const auto &
I : Insns) {
630 b = (inst.
Offset >> 4) & 0x1F;
647 b = (w & 0x00FF0000) >> 16;
649 b = (w & 0x0000FF00) >> 8;
671 b |= (inst.
Offset >> 3) & 0x1F;
676 b |= ((inst.
Offset >> 3) - 1) & 0x3F;
681 b |= (inst.
Offset >> 3) & 0x3F;
687 b = 0xD0 | ((reg & 0xC) >> 2);
689 b = ((reg & 0x3) << 6) | (inst.
Offset >> 3);
695 b = 0xD4 | ((reg & 0x8) >> 3);
697 b = ((reg & 0x7) << 5) | ((inst.
Offset >> 3) - 1);
703 b = 0xC8 | ((reg & 0xC) >> 2);
705 b = ((reg & 0x3) << 6) | (inst.
Offset >> 3);
711 b = 0xCC | ((reg & 0xC) >> 2);
713 b = ((reg & 0x3) << 6) | ((inst.
Offset >> 3) - 1);
719 assert((reg % 2) == 0 &&
"Saved reg must be 19+2*X");
721 b = 0xD6 | ((reg & 0x7) >> 2);
723 b = ((reg & 0x3) << 6) | (inst.
Offset >> 3);
729 b = 0xDC | ((reg & 0x4) >> 2);
731 b = ((reg & 0x3) << 6) | (inst.
Offset >> 3);
739 b = ((reg & 0x7) << 5) | ((inst.
Offset >> 3) - 1);
745 b = 0xD8 | ((reg & 0x4) >> 2);
747 b = ((reg & 0x3) << 6) | (inst.
Offset >> 3);
753 b = 0xDA | ((reg & 0x4) >> 2);
755 b = ((reg & 0x3) << 6) | ((inst.
Offset >> 3) - 1);
804 int Writeback =
Op / 6;
808 if (Writeback || Paired ||
Mode == 2)
815 b = inst.
Register | (Writeback << 5) | (Paired << 6);
834 b = ((inst.
Offset & 0xC0) >> 1) | reg;
836 b = 0xC0 | (inst.
Offset & 0x3F);
846 b = ((inst.
Offset & 0xC0) >> 1) | 0x10 | reg;
848 b = 0xC0 | (inst.
Offset & 0x3F);
861 const std::vector<MCSymbol *>& Epilogs,
863 for (
auto *EpilogStart : Epilogs) {
864 auto InstrsIter =
info->EpilogMap.find(EpilogStart);
866 "Epilog not found in EpilogMap");
867 const auto &Instrs = InstrsIter->second.Instructions;
869 if (Instrs.size() != EpilogInstrs.size())
873 for (
unsigned i = 0; i < Instrs.size(); ++i)
874 if (Instrs[i] != EpilogInstrs[i]) {
887 unsigned PrevOffset = -1;
888 unsigned PrevRegister = -1;
896 Inst.Register == 29) {
900 Inst.Register == 19 && Inst.Offset <= 248) {
906 Inst.Register == PrevRegister + 2 &&
907 Inst.Offset == PrevOffset + 16) {
922 PrevRegister = Inst.Register;
924 PrevOffset = Inst.Offset;
925 PrevRegister = Inst.Register;
939 for (
auto It = Instructions.rbegin(); It != Instructions.rend(); It++)
940 VisitInstruction(*It);
943 VisitInstruction(Inst);
950 const std::vector<WinEH::Instruction> &
Epilog) {
957 for (
int I =
Epilog.size() - 1;
I >= 0;
I--) {
972 int PrologCodeBytes) {
978 const std::vector<WinEH::Instruction> &
Epilog =
979 info->EpilogMap[Sym].Instructions;
985 if (DistanceFromEnd / 4 !=
Epilog.size())
993 if (PrologCodeBytes <= 31 &&
995 RetVal = PrologCodeBytes;
1004 if (
Offset > 31 || PrologCodeBytes > 124)
1009 info->EpilogMap.erase(Sym);
1014 int PackedEpilogOffset) {
1015 if (PackedEpilogOffset == 0) {
1021 }
else if (PackedEpilogOffset == 1) {
1031 unsigned RegI = 0, RegF = 0;
1032 int Predecrement = 0;
1044 bool StandaloneLR =
false, FPLRPair =
false;
1054 for (
auto It =
info->Instructions.begin(), EndIt =
info->Instructions.end();
1055 It != EndIt; It++) {
1059 if (Location != Start)
1064 if (Location != Start2)
1070 if (Location != Start2 && Location != Start3)
1072 Predecrement = Inst.
Offset;
1077 if (Location != Start2 && Location != Start3)
1079 Predecrement = Inst.
Offset;
1083 StandaloneLR =
true;
1087 Location = FloatRegs;
1114 StandaloneLR =
true;
1118 Location = FloatRegs;
1125 StandaloneLR =
true;
1126 Location = FloatRegs;
1133 if (Location != FloatRegs || RegF == 0 || Inst.
Register != 8 + RegF ||
1134 Inst.
Offset != 8 * (RegI + (StandaloneLR ? 1 : 0) + RegF))
1137 Location = InputArgs;
1140 if ((Location != Start2 && Location != Start3) || Inst.
Register != 8)
1142 Predecrement = Inst.
Offset;
1144 Location = FloatRegs;
1147 if ((Location !=
IntRegs && Location != FloatRegs) ||
1149 Inst.
Offset != 8 * (RegI + (StandaloneLR ? 1 : 0) + RegF))
1152 Location = FloatRegs;
1157 else if (Location == FloatRegs)
1163 if (Location !=
IntRegs && Location != FloatRegs && Location != InputArgs)
1165 Location = InputArgs;
1170 if (Location != Start2 && Location != Start3 && Location !=
IntRegs &&
1171 Location != FloatRegs && Location != InputArgs &&
1172 Location != StackAdjust)
1178 if (Location == Start2) {
1179 auto NextIt = It + 1;
1180 if (NextIt != EndIt) {
1184 assert(Predecrement == 0);
1187 Predecrement = Inst.
Offset;
1189 StandaloneLR =
true;
1190 Location = FloatRegs;
1204 Location = StackAdjust;
1209 if (Location != Start2 && Location != Start3 && Location !=
IntRegs &&
1210 Location != FloatRegs && Location != InputArgs)
1213 Location = FrameRecord;
1218 if (Location != StackAdjust || Inst.
Offset != 0)
1220 Location = FrameRecord;
1224 if (Location != FrameRecord)
1268 if (RegI > 10 || RegF > 8)
1270 if (StandaloneLR && FPLRPair)
1272 if (FPLRPair && Location != End)
1274 if (Nops != 0 && Nops != 4)
1276 if (PAC && !FPLRPair)
1303 if (StandaloneLR && RegI == 1 && RegF > 0)
1305 int IntSZ = 8 * RegI;
1308 int FpSZ = 8 * RegF;
1309 int SavSZ = (IntSZ + FpSZ + 8 * 8 *
H + 0xF) & ~0xF;
1310 if (Predecrement != SavSZ)
1317 if (FrameSize > 0x1FF)
1319 assert(RegF != 1 &&
"One single float reg not allowed");
1322 assert(FuncLength <= 0x7FF &&
"FuncLength should have been checked earlier");
1324 int CR = PAC ? 2 : FPLRPair ? 3 : StandaloneLR ? 1 : 0;
1325 info->PackedInfo |= Flag << 0;
1326 info->PackedInfo |= (FuncLength & 0x7FF) << 2;
1327 info->PackedInfo |= (RegF & 0x7) << 13;
1328 info->PackedInfo |= (RegI & 0xF) << 16;
1329 info->PackedInfo |= (
H & 0x1) << 20;
1330 info->PackedInfo |= (CR & 0x3) << 21;
1331 info->PackedInfo |= (FrameSize & 0x1FF) << 23;
1340 std::vector<MCSymbol *> EpilogStarts;
1342 EpilogStarts.push_back(
I.first);
1345 std::vector<MCSymbol *> AddedEpilogs;
1346 for (
auto *S : EpilogStarts) {
1348 auto &EpilogInstrs =
info->EpilogMap[S].Instructions;
1354 if (MatchingEpilog) {
1356 "Duplicate epilog not found");
1357 EpilogInfo[EpilogStart] = EpilogInfo.
lookup(MatchingEpilog);
1360 EpilogInstrs.clear();
1362 EpilogInstrs)) >= 0) {
1363 EpilogInfo[EpilogStart] = PrologOffset;
1367 EpilogInfo[EpilogStart] += 1;
1370 EpilogInstrs.
clear();
1372 EpilogInfo[EpilogStart] = TotalCodeBytes;
1373 TotalCodeBytes += CodeBytes;
1374 AddedEpilogs.push_back(EpilogStart);
1381 int64_t RawFuncLength) {
1382 if (
info->PrologEnd)
1384 info->PrologEnd,
info->Function->getName(),
1386 struct EpilogStartEnd {
1393 for (
auto &
I :
info->EpilogMap) {
1395 auto &Instrs =
I.second.Instructions;
1398 info->Function->getName(),
"epilogue");
1400 "Epilogs should be monotonically ordered");
1407 int64_t SegLimit = 0xFFFFC;
1408 int64_t SegOffset = 0;
1410 if (RawFuncLength > SegLimit) {
1412 int64_t RemainingLength = RawFuncLength;
1414 while (RemainingLength > SegLimit) {
1418 int64_t SegLength = SegLimit;
1419 int64_t SegEnd = SegOffset + SegLength;
1423 while (
E < Epilogs.
size() && Epilogs[
E].End < SegEnd) {
1425 EpilogsInSegment[Epilogs[
E].Start] = Epilogs[
E].Offset;
1436 if (
E < Epilogs.
size() && Epilogs[
E].Offset <= SegEnd)
1438 SegLength = Epilogs[
E].Offset - SegOffset;
1441 SegOffset, SegLength, !SegOffset);
1442 Seg.Epilogs = std::move(EpilogsInSegment);
1443 info->Segments.push_back(Seg);
1445 SegOffset += SegLength;
1446 RemainingLength -= SegLength;
1455 for (;
E < Epilogs.
size(); ++
E)
1456 LastSeg.Epilogs[Epilogs[
E].Start] = Epilogs[
E].Offset;
1457 info->Segments.push_back(LastSeg);
1463 bool TryPacked =
true) {
1472 info->Symbol = Label;
1475 bool HasEpilogs = (Seg.
Epilogs.size() != 0);
1480 int PackedEpilogOffset = HasEpilogs ?
1487 if (
info->Segments.size() == 1 && PackedEpilogOffset >= 0 &&
1488 uint32_t(PackedEpilogOffset) < PrologCodeBytes &&
1489 !
info->HandlesExceptions && SegLength <= 0x7ff && TryPacked) {
1504 PrologCodeBytes += 1;
1505 if (PackedEpilogOffset >= 0)
1506 PackedEpilogOffset += 1;
1514 PackedEpilogOffset = 0;
1517 uint32_t TotalCodeBytes = PrologCodeBytes;
1525 uint32_t CodeWords = TotalCodeBytes / 4;
1526 uint32_t CodeWordsMod = TotalCodeBytes % 4;
1530 PackedEpilogOffset >= 0 ? PackedEpilogOffset : Seg.
Epilogs.size();
1531 bool ExtensionWord = EpilogCount > 31 || TotalCodeBytes > 124;
1532 if (!ExtensionWord) {
1533 row1 |= (EpilogCount & 0x1F) << 22;
1534 row1 |= (CodeWords & 0x1F) << 27;
1536 if (
info->HandlesExceptions)
1538 if (PackedEpilogOffset >= 0)
1540 row1 |= SegLength & 0x3FFFF;
1544 if (ExtensionWord) {
1546 if (CodeWords > 0xFF || EpilogCount > 0xFFFF)
1548 "SEH unwind data splitting is only implemented for large functions, "
1549 "cases of too many code words or too many epilogs will be done "
1552 row2 |= (CodeWords & 0xFF) << 16;
1553 row2 |= (EpilogCount & 0xFFFF);
1557 if (PackedEpilogOffset < 0) {
1559 for (
auto &
I : EpilogInfo) {
1567 row3 |= (EpilogIndex & 0x3FF) << 22;
1588 auto &EpilogInstrs =
info->EpilogMap[
I.first].Instructions;
1593 int32_t BytesMod = CodeWords * 4 - TotalCodeBytes;
1595 for (
int i = 0; i < BytesMod; i++)
1598 if (
info->HandlesExceptions)
1608 bool TryPacked =
true) {
1616 if (
info->empty()) {
1617 info->EmitAttempted =
true;
1620 if (
info->EmitAttempted) {
1627 SMLoc(),
"Earlier .seh_handlerdata for " +
info->Function->getName() +
1628 " skipped due to no unwind info at the time "
1629 "(.seh_handlerdata too early?), but the function later "
1630 "did get unwind info that can't be emitted");
1635 for (
auto &
I :
info->EpilogMap)
1638 int64_t RawFuncLength;
1639 if (!
info->FuncletOrFuncEnd) {
1678 for (
auto &S :
info->Segments)
1682 info->Instructions.clear();
1687 for (
const auto &
I : Insns) {
1745 for (J = 3; J > 0; J--)
1746 if (
I.Offset & (0xffu << (8 * J)))
1757 bool *HasCustom =
nullptr) {
1759 for (
const auto &
I : Insns) {
1821 std::optional<int64_t> MaybeDistance =
1826 bool HasCustom =
false;
1830 if (Distance != InstructionBytes) {
1832 SMLoc(),
"Incorrect size for " + Name +
" " +
Type +
": " +
1834 " bytes of instructions in range, but .seh directives "
1835 "corresponding to " +
1836 Twine(InstructionBytes) +
" bytes\n");
1868 w = 0x8000 | (inst.
Register & 0x1fff) | (lr << 13);
1869 streamer.
emitInt8((w >> 8) & 0xff);
1870 streamer.
emitInt8((w >> 0) & 0xff);
1893 w = 0xe800 | (inst.
Offset / 4);
1894 streamer.
emitInt8((w >> 8) & 0xff);
1895 streamer.
emitInt8((w >> 0) & 0xff);
1900 w = 0xec00 | (inst.
Register & 0x0ff) | (lr << 8);
1901 streamer.
emitInt8((w >> 8) & 0xff);
1902 streamer.
emitInt8((w >> 0) & 0xff);
1929 streamer.
emitInt8((w >> 8) & 0xff);
1930 streamer.
emitInt8((w >> 0) & 0xff);
1937 streamer.
emitInt8((w >> 16) & 0xff);
1938 streamer.
emitInt8((w >> 8) & 0xff);
1939 streamer.
emitInt8((w >> 0) & 0xff);
1946 streamer.
emitInt8((w >> 8) & 0xff);
1947 streamer.
emitInt8((w >> 0) & 0xff);
1954 streamer.
emitInt8((w >> 16) & 0xff);
1955 streamer.
emitInt8((w >> 8) & 0xff);
1956 streamer.
emitInt8((w >> 0) & 0xff);
1974 for (i = 3; i > 0; i--)
1975 if (inst.
Offset & (0xffu << (8 * i)))
1990 const std::vector<WinEH::Instruction> &
Epilog,
1991 bool CanTweakProlog) {
2000 int EndIdx = CanTweakProlog ? 1 : 0;
2001 for (
int I =
Epilog.size() - 1;
I >= EndIdx;
I--) {
2008 if (CanTweakProlog) {
2026 int PrologCodeBytes) {
2028 if (
info->EpilogMap.size() != 1)
2038 if (
info->Instructions.empty() ||
Epilog.empty())
2044 streamer,
info->FuncletOrFuncEnd,
info->EpilogMap.begin()->first);
2049 if (DistanceFromEnd != InstructionBytes)
2057 if (PrologCodeBytes <= 31 &&
2059 RetVal = PrologCodeBytes;
2069 if (
Offset > 31 || PrologCodeBytes > 63)
2078 info->EpilogMap.clear();
2083 unsigned &Folded,
int &
IntRegs) {
2084 if (Mask & (1 << 14)) {
2088 if (Mask & (1 << 11)) {
2098 while ((Mask & 1) == 0) {
2102 if ((Mask & (Mask + 1)) != 0)
2106 while (Mask & (1 <<
N))
2125 bool Homing =
false;
2126 bool HasR11 =
false;
2127 bool HasChain =
false;
2132 unsigned StackAdjust = 0;
2167 if (Step != 1 && Step != 2)
2182 if (Step == 1 && Inst.
Register == 0x0f) {
2190 if (Step != 1 && Step != 2)
2202 if (Step != 3 || !HasR11 ||
IntRegs >= 0 || PF > 0)
2209 if (Step != 3 || !HasR11 || (
IntRegs < 0 && PF == 0))
2216 if (Step != 1 && Step != 2 && Step != 3 && Step != 4)
2229 if (Step != 1 && Step != 2 && Step != 3 && Step != 4 && Step != 5)
2233 if (Inst.
Offset / 4 >= 0x3f4)
2235 StackAdjust = Inst.
Offset / 4;
2240 if (HasR11 && !HasChain) {
2249 if (HasChain && !HasLR)
2253 if (
info->EpilogMap.size() > 1)
2258 if (
info->EpilogMap.size() == 0) {
2265 info->EpilogMap.begin()->second;
2270 streamer,
info->FuncletOrFuncEnd,
info->EpilogMap.begin()->first);
2275 if (DistanceFromEnd != InstructionBytes)
2278 bool GotStackAdjust =
false;
2279 bool GotFloatRegs =
false;
2280 bool GotIntRegs =
false;
2281 bool GotHomingRestore =
false;
2282 bool GotLRRestore =
false;
2283 bool NeedsReturn =
false;
2284 bool GotReturn =
false;
2306 if (Inst.
Offset / 4 >= 0x3f4)
2309 if (Homing && FloatRegs < 0 &&
IntRegs < 0 && StackAdjust == 0 &&
2310 PF == 0 && Inst.
Offset == 16) {
2311 GotHomingRestore =
true;
2314 if (StackAdjust > 0) {
2316 if (StackAdjust != Inst.
Offset / 4)
2318 GotStackAdjust =
true;
2319 }
else if (PF == Inst.
Offset / 4) {
2321 StackAdjust = Inst.
Offset / 4;
2322 GotStackAdjust =
true;
2329 }
else if (Step == 7 || Step == 8 || Step == 9) {
2330 if (!Homing || Inst.
Offset != 16)
2332 GotHomingRestore =
true;
2339 if (Step != 6 && Step != 7)
2342 if (FloatRegs != (
int)(Inst.
Register - 8))
2344 GotFloatRegs =
true;
2351 if (Step != 6 && Step != 7 && Step != 8)
2355 if (Homing && HasLR) {
2359 GotLRRestore =
true;
2365 if (HasLR != (Inst.
Offset == 1))
2368 GotLRRestore = Inst.
Offset == 1;
2387 if (Step != 6 && Step != 7 && Step != 8)
2392 bool CurHasLR =
false, CurHasR11 =
false;
2397 if (EF != PF && EF != StackAdjust)
2400 if (Homing && HasLR) {
2404 GotLRRestore =
true;
2410 if (CurHasLR != HasLR)
2412 GotLRRestore = CurHasLR;
2439 if (Step != 6 && Step != 7 && Step != 8 && Step != 9)
2441 if (!Homing || Inst.
Offset != 20 || GotLRRestore)
2443 GotLRRestore =
true;
2444 GotHomingRestore =
true;
2454 if (Step != 6 && Step != 7 && Step != 8 && Step != 9 && Step != 10)
2463 if (StackAdjust > 0 && !GotStackAdjust && EF == 0)
2465 if (FloatRegs >= 0 && !GotFloatRegs)
2467 if (
IntRegs >= 0 && !GotIntRegs)
2469 if (Homing && !GotHomingRestore)
2471 if (HasLR && !GotLRRestore)
2473 if (NeedsReturn && !GotReturn)
2477 assert(PF == 0 || EF == 0 ||
2479 if (PF > 0 || EF > 0) {
2480 StackAdjust = PF > 0 ? (PF - 1) : (EF - 1);
2481 assert(StackAdjust <= 3);
2482 StackAdjust |= 0x3f0;
2484 StackAdjust |= 1 << 2;
2486 StackAdjust |= 1 << 3;
2489 assert(FuncLength <= 0x7FF &&
"FuncLength should have been checked earlier");
2490 int Flag =
info->Fragment ? 0x02 : 0x01;
2491 int H = Homing ? 1 : 0;
2492 int L = HasLR ? 1 : 0;
2493 int C = HasChain ? 1 : 0;
2500 }
else if (FloatRegs >= 0) {
2509 info->PackedInfo |= Flag << 0;
2510 info->PackedInfo |= (FuncLength & 0x7FF) << 2;
2511 info->PackedInfo |= (Ret & 0x3) << 13;
2512 info->PackedInfo |=
H << 15;
2513 info->PackedInfo |=
Reg << 16;
2514 info->PackedInfo |= R << 19;
2515 info->PackedInfo |= L << 20;
2516 info->PackedInfo |=
C << 21;
2517 assert(StackAdjust <= 0x3ff);
2518 info->PackedInfo |= StackAdjust << 22;
2525 bool TryPacked =
true) {
2533 if (
info->empty()) {
2534 info->EmitAttempted =
true;
2537 if (
info->EmitAttempted) {
2544 SMLoc(),
"Earlier .seh_handlerdata for " +
info->Function->getName() +
2545 " skipped due to no unwind info at the time "
2546 "(.seh_handlerdata too early?), but the function later "
2547 "did get unwind info that can't be emitted");
2556 info->Symbol = Label;
2558 if (!
info->PrologEnd)
2560 info->Function->getName() +
2561 " not correctly terminated");
2563 if (
info->PrologEnd && !
info->Fragment)
2565 info->PrologEnd,
info->Function->getName(),
2567 for (
auto &
I :
info->EpilogMap) {
2571 info->Function->getName(),
"epilogue");
2572 if (
Epilog.Instructions.empty() ||
2575 SMLoc(),
"Epilogue in " +
info->Function->getName() +
2576 " not correctly terminated");
2579 std::optional<int64_t> RawFuncLength;
2580 const MCExpr *FuncLengthExpr =
nullptr;
2581 if (!
info->FuncletOrFuncEnd) {
2600 FuncLength = (
uint32_t)*RawFuncLength / 2;
2601 if (FuncLength > 0x3FFFF)
2604 uint32_t TotalCodeBytes = PrologCodeBytes;
2606 if (!
info->HandlesExceptions && RawFuncLength && FuncLength <= 0x7ff &&
2620 int PackedEpilogOffset =
2626 std::vector<MCSymbol *> AddedEpilogs;
2628 bool CanTweakProlog =
true;
2629 for (
auto &
I :
info->EpilogMap) {
2631 auto &EpilogInstrs =
I.second.Instructions;
2637 if (MatchingEpilog) {
2639 "Duplicate epilog not found");
2640 EpilogInfo[EpilogStart] = EpilogInfo.
lookup(MatchingEpilog);
2643 EpilogInstrs.clear();
2645 info->Instructions, EpilogInstrs, CanTweakProlog)) >= 0) {
2646 if (CanTweakProlog) {
2649 info->Instructions.front() = EpilogInstrs.back();
2651 CanTweakProlog =
false;
2653 EpilogInfo[EpilogStart] = PrologOffset;
2656 EpilogInstrs.clear();
2658 EpilogInfo[EpilogStart] = TotalCodeBytes;
2659 TotalCodeBytes += CodeBytes;
2660 AddedEpilogs.push_back(EpilogStart);
2666 uint32_t CodeWords = TotalCodeBytes / 4;
2667 uint32_t CodeWordsMod = TotalCodeBytes % 4;
2671 PackedEpilogOffset >= 0 ? PackedEpilogOffset :
info->EpilogMap.size();
2672 bool ExtensionWord = EpilogCount > 31 || CodeWords > 15;
2673 if (!ExtensionWord) {
2674 row1 |= (EpilogCount & 0x1F) << 23;
2675 row1 |= (CodeWords & 0x0F) << 28;
2677 if (
info->HandlesExceptions)
2679 if (PackedEpilogOffset >= 0)
2683 row1 |= FuncLength & 0x3FFFF;
2693 if (ExtensionWord) {
2695 if (CodeWords > 0xFF || EpilogCount > 0xFFFF)
2698 row2 |= (CodeWords & 0xFF) << 16;
2699 row2 |= (EpilogCount & 0xFFFF);
2703 if (PackedEpilogOffset < 0) {
2705 for (
auto &
I : EpilogInfo) {
2709 std::optional<int64_t> MaybeEpilogOffset =
2711 const MCExpr *OffsetExpr =
nullptr;
2713 if (MaybeEpilogOffset)
2714 EpilogOffset = *MaybeEpilogOffset / 2;
2718 assert(
info->EpilogMap.contains(EpilogStart));
2719 unsigned Condition =
info->EpilogMap[EpilogStart].Condition;
2720 assert(Condition <= 0xf);
2723 row3 |= Condition << 20;
2724 row3 |= (EpilogIndex & 0x3FF) << 24;
2725 if (MaybeEpilogOffset)
2737 for (
uint8_t c = 0; c < numInst; ++c) {
2739 info->Instructions.pop_back();
2744 for (
auto &
I :
info->EpilogMap) {
2745 auto &EpilogInstrs =
I.second.Instructions;
2750 int32_t BytesMod = CodeWords * 4 - TotalCodeBytes;
2752 for (
int i = 0; i < BytesMod; i++)
2755 if (
info->HandlesExceptions)
2767 for (
const auto &S :
info->Segments) {
2769 if (
info->PackedInfo)
2786 if (
info->PackedInfo)
2822 bool HandlerData)
const {
2829 if (!
info->FuncletOrFuncEnd) {
2867 bool HandlerData)
const {
2874 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.
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
@ 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