Skip to content

Commit 31e85f9

Browse files
committed
MCAsmBackend: Make some target overrides out-of-line
To align with the majority of targets where these overrides are out-of-line. The consistency helps the pending change that merges addReloc and applyFixup.
1 parent 283f53a commit 31e85f9

File tree

4 files changed

+242
-203
lines changed

4 files changed

+242
-203
lines changed

llvm/lib/Target/Hexagon/MCTargetDesc/HexagonAsmBackend.cpp

Lines changed: 127 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -404,131 +404,9 @@ class HexagonAsmBackend : public MCAsmBackend {
404404
llvm_unreachable(errStr.str().c_str());
405405
}
406406

407-
/// ApplyFixup - Apply the \arg Value for given \arg Fixup into the provided
408-
/// data fragment, at the offset specified by the fixup and following the
409-
/// fixup kind as appropriate.
410-
void applyFixup(const MCFragment &, const MCFixup &Fixup,
411-
const MCValue &Target, MutableArrayRef<char> Data,
412-
uint64_t FixupValue, bool IsResolved) override {
413-
414-
// When FixupValue is 0 the relocation is external and there
415-
// is nothing for us to do.
416-
if (!FixupValue) return;
417-
418-
MCFixupKind Kind = Fixup.getKind();
419-
uint64_t Value;
420-
uint32_t InstMask;
421-
uint32_t Reloc;
422-
423-
// LLVM gives us an encoded value, we have to convert it back
424-
// to a real offset before we can use it.
425-
uint32_t Offset = Fixup.getOffset();
426-
unsigned NumBytes = getFixupKindNumBytes(Kind);
427-
assert(Offset + NumBytes <= Data.size() && "Invalid fixup offset!");
428-
char *InstAddr = Data.data() + Offset;
429-
430-
Value = adjustFixupValue(Kind, FixupValue);
431-
if(!Value)
432-
return;
433-
int sValue = (int)Value;
434-
435-
switch((unsigned)Kind) {
436-
default:
437-
return;
438-
439-
case fixup_Hexagon_B7_PCREL:
440-
if (!(isIntN(7, sValue)))
441-
HandleFixupError(7, 2, (int64_t)FixupValue, "B7_PCREL");
442-
[[fallthrough]];
443-
case fixup_Hexagon_B7_PCREL_X:
444-
InstMask = 0x00001f18; // Word32_B7
445-
Reloc = (((Value >> 2) & 0x1f) << 8) | // Value 6-2 = Target 12-8
446-
((Value & 0x3) << 3); // Value 1-0 = Target 4-3
447-
break;
448-
449-
case fixup_Hexagon_B9_PCREL:
450-
if (!(isIntN(9, sValue)))
451-
HandleFixupError(9, 2, (int64_t)FixupValue, "B9_PCREL");
452-
[[fallthrough]];
453-
case fixup_Hexagon_B9_PCREL_X:
454-
InstMask = 0x003000fe; // Word32_B9
455-
Reloc = (((Value >> 7) & 0x3) << 20) | // Value 8-7 = Target 21-20
456-
((Value & 0x7f) << 1); // Value 6-0 = Target 7-1
457-
break;
458-
459-
// Since the existing branches that use this relocation cannot be
460-
// extended, they should only be fixed up if the target is within range.
461-
case fixup_Hexagon_B13_PCREL:
462-
if (!(isIntN(13, sValue)))
463-
HandleFixupError(13, 2, (int64_t)FixupValue, "B13_PCREL");
464-
[[fallthrough]];
465-
case fixup_Hexagon_B13_PCREL_X:
466-
InstMask = 0x00202ffe; // Word32_B13
467-
Reloc = (((Value >> 12) & 0x1) << 21) | // Value 12 = Target 21
468-
(((Value >> 11) & 0x1) << 13) | // Value 11 = Target 13
469-
((Value & 0x7ff) << 1); // Value 10-0 = Target 11-1
470-
break;
471-
472-
case fixup_Hexagon_B15_PCREL:
473-
if (!(isIntN(15, sValue)))
474-
HandleFixupError(15, 2, (int64_t)FixupValue, "B15_PCREL");
475-
[[fallthrough]];
476-
case fixup_Hexagon_B15_PCREL_X:
477-
InstMask = 0x00df20fe; // Word32_B15
478-
Reloc = (((Value >> 13) & 0x3) << 22) | // Value 14-13 = Target 23-22
479-
(((Value >> 8) & 0x1f) << 16) | // Value 12-8 = Target 20-16
480-
(((Value >> 7) & 0x1) << 13) | // Value 7 = Target 13
481-
((Value & 0x7f) << 1); // Value 6-0 = Target 7-1
482-
break;
483-
484-
case fixup_Hexagon_B22_PCREL:
485-
if (!(isIntN(22, sValue)))
486-
HandleFixupError(22, 2, (int64_t)FixupValue, "B22_PCREL");
487-
[[fallthrough]];
488-
case fixup_Hexagon_B22_PCREL_X:
489-
InstMask = 0x01ff3ffe; // Word32_B22
490-
Reloc = (((Value >> 13) & 0x1ff) << 16) | // Value 21-13 = Target 24-16
491-
((Value & 0x1fff) << 1); // Value 12-0 = Target 13-1
492-
break;
493-
494-
case fixup_Hexagon_B32_PCREL_X:
495-
InstMask = 0x0fff3fff; // Word32_X26
496-
Reloc = (((Value >> 14) & 0xfff) << 16) | // Value 25-14 = Target 27-16
497-
(Value & 0x3fff); // Value 13-0 = Target 13-0
498-
break;
499-
500-
case FK_Data_1:
501-
case FK_Data_2:
502-
case FK_Data_4:
503-
case fixup_Hexagon_32:
504-
InstMask = 0xffffffff; // Word32
505-
Reloc = Value;
506-
break;
507-
}
508-
509-
LLVM_DEBUG(dbgs() << "Name=" << getFixupKindInfo(Kind).Name << "("
510-
<< (unsigned)Kind << ")\n");
511-
LLVM_DEBUG(
512-
uint32_t OldData = 0; for (unsigned i = 0; i < NumBytes; i++) OldData |=
513-
(InstAddr[i] << (i * 8)) & (0xff << (i * 8));
514-
dbgs() << "\tBValue=0x"; dbgs().write_hex(Value) << ": AValue=0x";
515-
dbgs().write_hex(FixupValue)
516-
<< ": Offset=" << Offset << ": Size=" << Data.size() << ": OInst=0x";
517-
dbgs().write_hex(OldData) << ": Reloc=0x"; dbgs().write_hex(Reloc););
518-
519-
// For each byte of the fragment that the fixup touches, mask in the
520-
// bits from the fixup value. The Value has been "split up" into the
521-
// appropriate bitfields above.
522-
for (unsigned i = 0; i < NumBytes; i++){
523-
InstAddr[i] &= uint8_t(~InstMask >> (i * 8)) & 0xff; // Clear reloc bits
524-
InstAddr[i] |= uint8_t(Reloc >> (i * 8)) & 0xff; // Apply new reloc
525-
}
526-
527-
LLVM_DEBUG(uint32_t NewData = 0;
528-
for (unsigned i = 0; i < NumBytes; i++) NewData |=
529-
(InstAddr[i] << (i * 8)) & (0xff << (i * 8));
530-
dbgs() << ": NInst=0x"; dbgs().write_hex(NewData) << "\n";);
531-
}
407+
void applyFixup(const MCFragment &, const MCFixup &, const MCValue &,
408+
MutableArrayRef<char> Data, uint64_t FixupValue,
409+
bool IsResolved) override;
532410

533411
bool isInstRelaxable(MCInst const &HMI) const {
534412
const MCInstrDesc &MCID = HexagonMCInstrInfo::getDesc(*MCII, HMI);
@@ -775,6 +653,130 @@ class HexagonAsmBackend : public MCAsmBackend {
775653

776654
} // namespace
777655

656+
void HexagonAsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
657+
const MCValue &Target,
658+
MutableArrayRef<char> Data,
659+
uint64_t FixupValue, bool IsResolved) {
660+
// When FixupValue is 0 the relocation is external and there
661+
// is nothing for us to do.
662+
if (!FixupValue)
663+
return;
664+
665+
MCFixupKind Kind = Fixup.getKind();
666+
uint64_t Value;
667+
uint32_t InstMask;
668+
uint32_t Reloc;
669+
670+
// LLVM gives us an encoded value, we have to convert it back
671+
// to a real offset before we can use it.
672+
uint32_t Offset = Fixup.getOffset();
673+
unsigned NumBytes = getFixupKindNumBytes(Kind);
674+
assert(Offset + NumBytes <= Data.size() && "Invalid fixup offset!");
675+
char *InstAddr = Data.data() + Offset;
676+
677+
Value = adjustFixupValue(Kind, FixupValue);
678+
if (!Value)
679+
return;
680+
int sValue = (int)Value;
681+
682+
switch ((unsigned)Kind) {
683+
default:
684+
return;
685+
686+
case fixup_Hexagon_B7_PCREL:
687+
if (!(isIntN(7, sValue)))
688+
HandleFixupError(7, 2, (int64_t)FixupValue, "B7_PCREL");
689+
[[fallthrough]];
690+
case fixup_Hexagon_B7_PCREL_X:
691+
InstMask = 0x00001f18; // Word32_B7
692+
Reloc = (((Value >> 2) & 0x1f) << 8) | // Value 6-2 = Target 12-8
693+
((Value & 0x3) << 3); // Value 1-0 = Target 4-3
694+
break;
695+
696+
case fixup_Hexagon_B9_PCREL:
697+
if (!(isIntN(9, sValue)))
698+
HandleFixupError(9, 2, (int64_t)FixupValue, "B9_PCREL");
699+
[[fallthrough]];
700+
case fixup_Hexagon_B9_PCREL_X:
701+
InstMask = 0x003000fe; // Word32_B9
702+
Reloc = (((Value >> 7) & 0x3) << 20) | // Value 8-7 = Target 21-20
703+
((Value & 0x7f) << 1); // Value 6-0 = Target 7-1
704+
break;
705+
706+
// Since the existing branches that use this relocation cannot be
707+
// extended, they should only be fixed up if the target is within range.
708+
case fixup_Hexagon_B13_PCREL:
709+
if (!(isIntN(13, sValue)))
710+
HandleFixupError(13, 2, (int64_t)FixupValue, "B13_PCREL");
711+
[[fallthrough]];
712+
case fixup_Hexagon_B13_PCREL_X:
713+
InstMask = 0x00202ffe; // Word32_B13
714+
Reloc = (((Value >> 12) & 0x1) << 21) | // Value 12 = Target 21
715+
(((Value >> 11) & 0x1) << 13) | // Value 11 = Target 13
716+
((Value & 0x7ff) << 1); // Value 10-0 = Target 11-1
717+
break;
718+
719+
case fixup_Hexagon_B15_PCREL:
720+
if (!(isIntN(15, sValue)))
721+
HandleFixupError(15, 2, (int64_t)FixupValue, "B15_PCREL");
722+
[[fallthrough]];
723+
case fixup_Hexagon_B15_PCREL_X:
724+
InstMask = 0x00df20fe; // Word32_B15
725+
Reloc = (((Value >> 13) & 0x3) << 22) | // Value 14-13 = Target 23-22
726+
(((Value >> 8) & 0x1f) << 16) | // Value 12-8 = Target 20-16
727+
(((Value >> 7) & 0x1) << 13) | // Value 7 = Target 13
728+
((Value & 0x7f) << 1); // Value 6-0 = Target 7-1
729+
break;
730+
731+
case fixup_Hexagon_B22_PCREL:
732+
if (!(isIntN(22, sValue)))
733+
HandleFixupError(22, 2, (int64_t)FixupValue, "B22_PCREL");
734+
[[fallthrough]];
735+
case fixup_Hexagon_B22_PCREL_X:
736+
InstMask = 0x01ff3ffe; // Word32_B22
737+
Reloc = (((Value >> 13) & 0x1ff) << 16) | // Value 21-13 = Target 24-16
738+
((Value & 0x1fff) << 1); // Value 12-0 = Target 13-1
739+
break;
740+
741+
case fixup_Hexagon_B32_PCREL_X:
742+
InstMask = 0x0fff3fff; // Word32_X26
743+
Reloc = (((Value >> 14) & 0xfff) << 16) | // Value 25-14 = Target 27-16
744+
(Value & 0x3fff); // Value 13-0 = Target 13-0
745+
break;
746+
747+
case FK_Data_1:
748+
case FK_Data_2:
749+
case FK_Data_4:
750+
case fixup_Hexagon_32:
751+
InstMask = 0xffffffff; // Word32
752+
Reloc = Value;
753+
break;
754+
}
755+
756+
LLVM_DEBUG(dbgs() << "Name=" << getFixupKindInfo(Kind).Name << "("
757+
<< (unsigned)Kind << ")\n");
758+
LLVM_DEBUG(
759+
uint32_t OldData = 0; for (unsigned i = 0; i < NumBytes; i++) OldData |=
760+
(InstAddr[i] << (i * 8)) & (0xff << (i * 8));
761+
dbgs() << "\tBValue=0x"; dbgs().write_hex(Value) << ": AValue=0x";
762+
dbgs().write_hex(FixupValue)
763+
<< ": Offset=" << Offset << ": Size=" << Data.size() << ": OInst=0x";
764+
dbgs().write_hex(OldData) << ": Reloc=0x"; dbgs().write_hex(Reloc););
765+
766+
// For each byte of the fragment that the fixup touches, mask in the
767+
// bits from the fixup value. The Value has been "split up" into the
768+
// appropriate bitfields above.
769+
for (unsigned i = 0; i < NumBytes; i++) {
770+
InstAddr[i] &= uint8_t(~InstMask >> (i * 8)) & 0xff; // Clear reloc bits
771+
InstAddr[i] |= uint8_t(Reloc >> (i * 8)) & 0xff; // Apply new reloc
772+
}
773+
774+
LLVM_DEBUG(uint32_t NewData = 0;
775+
for (unsigned i = 0; i < NumBytes; i++) NewData |=
776+
(InstAddr[i] << (i * 8)) & (0xff << (i * 8));
777+
dbgs() << ": NInst=0x"; dbgs().write_hex(NewData) << "\n";);
778+
}
779+
778780
// MCAsmBackend
779781
MCAsmBackend *llvm::createHexagonAsmBackend(Target const &T,
780782
const MCSubtargetInfo &STI,

llvm/lib/Target/M68k/MCTargetDesc/M68kAsmBackend.cpp

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,9 @@ class M68kAsmBackend : public MCAsmBackend {
5252
.CasesLower("m68020", "m68030", "m68040", true)
5353
.Default(false)) {}
5454

55-
void applyFixup(const MCFragment &, const MCFixup &Fixup, const MCValue &,
56-
MutableArrayRef<char> Data, uint64_t Value, bool) override {
57-
unsigned Size = 1 << getFixupKindLog2Size(Fixup.getKind());
58-
59-
assert(Fixup.getOffset() + Size <= Data.size() && "Invalid fixup offset!");
60-
// Check that uppper bits are either all zeros or all ones.
61-
// Specifically ignore overflow/underflow as long as the leakage is
62-
// limited to the lower bits. This is to remain compatible with
63-
// other assemblers.
64-
assert(isIntN(Size * 8 + 1, static_cast<int64_t>(Value)) &&
65-
"Value does not fit in the Fixup field");
66-
67-
// Write in Big Endian
68-
for (unsigned i = 0; i != Size; ++i)
69-
Data[Fixup.getOffset() + i] =
70-
uint8_t(static_cast<int64_t>(Value) >> ((Size - i - 1) * 8));
71-
}
55+
void applyFixup(const MCFragment &, const MCFixup &, const MCValue &,
56+
MutableArrayRef<char> Data, uint64_t Value,
57+
bool IsResolved) override;
7258

7359
bool mayNeedRelaxation(const MCInst &Inst,
7460
const MCSubtargetInfo &STI) const override;
@@ -91,6 +77,25 @@ class M68kAsmBackend : public MCAsmBackend {
9177
};
9278
} // end anonymous namespace
9379

80+
void M68kAsmBackend::applyFixup(const MCFragment &, const MCFixup &Fixup,
81+
const MCValue &, MutableArrayRef<char> Data,
82+
uint64_t Value, bool) {
83+
unsigned Size = 1 << getFixupKindLog2Size(Fixup.getKind());
84+
85+
assert(Fixup.getOffset() + Size <= Data.size() && "Invalid fixup offset!");
86+
// Check that uppper bits are either all zeros or all ones.
87+
// Specifically ignore overflow/underflow as long as the leakage is
88+
// limited to the lower bits. This is to remain compatible with
89+
// other assemblers.
90+
assert(isIntN(Size * 8 + 1, static_cast<int64_t>(Value)) &&
91+
"Value does not fit in the Fixup field");
92+
93+
// Write in Big Endian
94+
for (unsigned i = 0; i != Size; ++i)
95+
Data[Fixup.getOffset() + i] =
96+
uint8_t(static_cast<int64_t>(Value) >> ((Size - i - 1) * 8));
97+
}
98+
9499
/// cc—Carry clear GE—Greater than or equal
95100
/// LS—Lower or same PL—Plus
96101
/// CS—Carry set GT—Greater than

0 commit comments

Comments
 (0)