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/MC/MCAsmBackend.h"
12 #include "llvm/MC/MCELFObjectWriter.h"
13 #include "llvm/MC/MCFixupKindInfo.h"
14 #include "llvm/MC/MCInst.h"
15 #include "llvm/MC/MCObjectWriter.h"
16 #include "llvm/MC/MCSubtargetInfo.h"
20 // Value is a fully-resolved relocation value: Symbol + Addend [- Pivot].
21 // Return the bits that should be installed in a relocation field for
23 static uint64_t extractBitsForFixup(MCFixupKind Kind
, uint64_t Value
) {
24 if (Kind
< FirstTargetFixupKind
)
27 switch (unsigned(Kind
)) {
28 case SystemZ::FK_390_PC12DBL
:
29 case SystemZ::FK_390_PC16DBL
:
30 case SystemZ::FK_390_PC24DBL
:
31 case SystemZ::FK_390_PC32DBL
:
32 return (int64_t)Value
/ 2;
34 case SystemZ::FK_390_TLS_CALL
:
38 llvm_unreachable("Unknown fixup kind!");
42 class SystemZMCAsmBackend
: public MCAsmBackend
{
45 SystemZMCAsmBackend(uint8_t osABI
)
46 : MCAsmBackend(support::big
), OSABI(osABI
) {}
48 // Override MCAsmBackend
49 unsigned getNumFixupKinds() const override
{
50 return SystemZ::NumTargetFixupKinds
;
52 const MCFixupKindInfo
&getFixupKindInfo(MCFixupKind Kind
) const override
;
53 void applyFixup(const MCAssembler
&Asm
, const MCFixup
&Fixup
,
54 const MCValue
&Target
, MutableArrayRef
<char> Data
,
55 uint64_t Value
, bool IsResolved
,
56 const MCSubtargetInfo
*STI
) const override
;
57 bool mayNeedRelaxation(const MCInst
&Inst
,
58 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 void relaxInstruction(const MCInst
&Inst
, const MCSubtargetInfo
&STI
,
67 MCInst
&Res
) const override
{
68 llvm_unreachable("SystemZ does do not have assembler relaxation");
70 bool writeNopData(raw_ostream
&OS
, uint64_t Count
) const override
;
71 std::unique_ptr
<MCObjectTargetWriter
>
72 createObjectTargetWriter() const override
{
73 return createSystemZObjectWriter(OSABI
);
76 } // end anonymous namespace
78 const MCFixupKindInfo
&
79 SystemZMCAsmBackend::getFixupKindInfo(MCFixupKind Kind
) const {
80 const static MCFixupKindInfo Infos
[SystemZ::NumTargetFixupKinds
] = {
81 { "FK_390_PC12DBL", 4, 12, MCFixupKindInfo::FKF_IsPCRel
},
82 { "FK_390_PC16DBL", 0, 16, MCFixupKindInfo::FKF_IsPCRel
},
83 { "FK_390_PC24DBL", 0, 24, MCFixupKindInfo::FKF_IsPCRel
},
84 { "FK_390_PC32DBL", 0, 32, MCFixupKindInfo::FKF_IsPCRel
},
85 { "FK_390_TLS_CALL", 0, 0, 0 }
88 if (Kind
< FirstTargetFixupKind
)
89 return MCAsmBackend::getFixupKindInfo(Kind
);
91 assert(unsigned(Kind
- FirstTargetFixupKind
) < getNumFixupKinds() &&
93 return Infos
[Kind
- FirstTargetFixupKind
];
96 void SystemZMCAsmBackend::applyFixup(const MCAssembler
&Asm
,
98 const MCValue
&Target
,
99 MutableArrayRef
<char> Data
, uint64_t Value
,
101 const MCSubtargetInfo
*STI
) const {
102 MCFixupKind Kind
= Fixup
.getKind();
103 unsigned Offset
= Fixup
.getOffset();
104 unsigned BitSize
= getFixupKindInfo(Kind
).TargetSize
;
105 unsigned Size
= (BitSize
+ 7) / 8;
107 assert(Offset
+ Size
<= Data
.size() && "Invalid fixup offset!");
109 // Big-endian insertion of Size bytes.
110 Value
= extractBitsForFixup(Kind
, Value
);
112 Value
&= ((uint64_t)1 << BitSize
) - 1;
113 unsigned ShiftValue
= (Size
* 8) - 8;
114 for (unsigned I
= 0; I
!= Size
; ++I
) {
115 Data
[Offset
+ I
] |= uint8_t(Value
>> ShiftValue
);
120 bool SystemZMCAsmBackend::writeNopData(raw_ostream
&OS
, uint64_t Count
) const {
121 for (uint64_t I
= 0; I
!= Count
; ++I
)
126 MCAsmBackend
*llvm::createSystemZMCAsmBackend(const Target
&T
,
127 const MCSubtargetInfo
&STI
,
128 const MCRegisterInfo
&MRI
,
129 const MCTargetOptions
&Options
) {
131 MCELFObjectTargetWriter::getOSABI(STI
.getTargetTriple().getOS());
132 return new SystemZMCAsmBackend(OSABI
);