1 //===-- SystemZMCAsmBackend.cpp - SystemZ assembler backend ---------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "MCTargetDesc/SystemZMCFixups.h"
10 #include "MCTargetDesc/SystemZMCTargetDesc.h"
11 #include "llvm/ADT/StringSwitch.h"
12 #include "llvm/MC/MCAsmBackend.h"
13 #include "llvm/MC/MCELFObjectWriter.h"
14 #include "llvm/MC/MCFixupKindInfo.h"
15 #include "llvm/MC/MCInst.h"
16 #include "llvm/MC/MCObjectWriter.h"
17 #include "llvm/MC/MCSubtargetInfo.h"
21 // Value is a fully-resolved relocation value: Symbol + Addend [- Pivot].
22 // Return the bits that should be installed in a relocation field for
24 static uint64_t extractBitsForFixup(MCFixupKind Kind
, uint64_t Value
) {
25 if (Kind
< FirstTargetFixupKind
)
28 switch (unsigned(Kind
)) {
29 case SystemZ::FK_390_PC12DBL
:
30 case SystemZ::FK_390_PC16DBL
:
31 case SystemZ::FK_390_PC24DBL
:
32 case SystemZ::FK_390_PC32DBL
:
33 return (int64_t)Value
/ 2;
35 case SystemZ::FK_390_TLS_CALL
:
39 llvm_unreachable("Unknown fixup kind!");
43 class SystemZMCAsmBackend
: public MCAsmBackend
{
46 SystemZMCAsmBackend(uint8_t osABI
)
47 : MCAsmBackend(support::big
), OSABI(osABI
) {}
49 // Override MCAsmBackend
50 unsigned getNumFixupKinds() const override
{
51 return SystemZ::NumTargetFixupKinds
;
53 Optional
<MCFixupKind
> getFixupKind(StringRef Name
) const override
;
54 const MCFixupKindInfo
&getFixupKindInfo(MCFixupKind Kind
) const override
;
55 bool shouldForceRelocation(const MCAssembler
&Asm
, const MCFixup
&Fixup
,
56 const MCValue
&Target
) override
;
57 void applyFixup(const MCAssembler
&Asm
, const MCFixup
&Fixup
,
58 const MCValue
&Target
, MutableArrayRef
<char> Data
,
59 uint64_t Value
, bool IsResolved
,
60 const MCSubtargetInfo
*STI
) const override
;
61 bool fixupNeedsRelaxation(const MCFixup
&Fixup
, uint64_t Value
,
62 const MCRelaxableFragment
*Fragment
,
63 const MCAsmLayout
&Layout
) const override
{
66 bool writeNopData(raw_ostream
&OS
, uint64_t Count
) const override
;
67 std::unique_ptr
<MCObjectTargetWriter
>
68 createObjectTargetWriter() const override
{
69 return createSystemZObjectWriter(OSABI
);
72 } // end anonymous namespace
74 Optional
<MCFixupKind
> SystemZMCAsmBackend::getFixupKind(StringRef Name
) const {
75 unsigned Type
= llvm::StringSwitch
<unsigned>(Name
)
76 #define ELF_RELOC(X, Y) .Case(#X, Y)
77 #include "llvm/BinaryFormat/ELFRelocs/SystemZ.def"
79 .Case("BFD_RELOC_NONE", ELF::R_390_NONE
)
80 .Case("BFD_RELOC_8", ELF::R_390_8
)
81 .Case("BFD_RELOC_16", ELF::R_390_16
)
82 .Case("BFD_RELOC_32", ELF::R_390_32
)
83 .Case("BFD_RELOC_64", ELF::R_390_64
)
86 return static_cast<MCFixupKind
>(FirstLiteralRelocationKind
+ Type
);
90 const MCFixupKindInfo
&
91 SystemZMCAsmBackend::getFixupKindInfo(MCFixupKind Kind
) const {
92 const static MCFixupKindInfo Infos
[SystemZ::NumTargetFixupKinds
] = {
93 { "FK_390_PC12DBL", 4, 12, MCFixupKindInfo::FKF_IsPCRel
},
94 { "FK_390_PC16DBL", 0, 16, MCFixupKindInfo::FKF_IsPCRel
},
95 { "FK_390_PC24DBL", 0, 24, MCFixupKindInfo::FKF_IsPCRel
},
96 { "FK_390_PC32DBL", 0, 32, MCFixupKindInfo::FKF_IsPCRel
},
97 { "FK_390_TLS_CALL", 0, 0, 0 }
100 // Fixup kinds from .reloc directive are like R_390_NONE. They
101 // do not require any extra processing.
102 if (Kind
>= FirstLiteralRelocationKind
)
103 return MCAsmBackend::getFixupKindInfo(FK_NONE
);
105 if (Kind
< FirstTargetFixupKind
)
106 return MCAsmBackend::getFixupKindInfo(Kind
);
108 assert(unsigned(Kind
- FirstTargetFixupKind
) < getNumFixupKinds() &&
110 return Infos
[Kind
- FirstTargetFixupKind
];
113 bool SystemZMCAsmBackend::shouldForceRelocation(const MCAssembler
&,
114 const MCFixup
&Fixup
,
116 return Fixup
.getKind() >= FirstLiteralRelocationKind
;
119 void SystemZMCAsmBackend::applyFixup(const MCAssembler
&Asm
,
120 const MCFixup
&Fixup
,
121 const MCValue
&Target
,
122 MutableArrayRef
<char> Data
, uint64_t Value
,
124 const MCSubtargetInfo
*STI
) const {
125 MCFixupKind Kind
= Fixup
.getKind();
126 if (Kind
>= FirstLiteralRelocationKind
)
128 unsigned Offset
= Fixup
.getOffset();
129 unsigned BitSize
= getFixupKindInfo(Kind
).TargetSize
;
130 unsigned Size
= (BitSize
+ 7) / 8;
132 assert(Offset
+ Size
<= Data
.size() && "Invalid fixup offset!");
134 // Big-endian insertion of Size bytes.
135 Value
= extractBitsForFixup(Kind
, Value
);
137 Value
&= ((uint64_t)1 << BitSize
) - 1;
138 unsigned ShiftValue
= (Size
* 8) - 8;
139 for (unsigned I
= 0; I
!= Size
; ++I
) {
140 Data
[Offset
+ I
] |= uint8_t(Value
>> ShiftValue
);
145 bool SystemZMCAsmBackend::writeNopData(raw_ostream
&OS
, uint64_t Count
) const {
146 for (uint64_t I
= 0; I
!= Count
; ++I
)
151 MCAsmBackend
*llvm::createSystemZMCAsmBackend(const Target
&T
,
152 const MCSubtargetInfo
&STI
,
153 const MCRegisterInfo
&MRI
,
154 const MCTargetOptions
&Options
) {
156 MCELFObjectTargetWriter::getOSABI(STI
.getTargetTriple().getOS());
157 return new SystemZMCAsmBackend(OSABI
);