1 //===-- SystemZMCAsmBackend.cpp - SystemZ assembler backend ---------------===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 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" 17 18 using namespace llvm; 19 20 // Value is a fully-resolved relocation value: Symbol + Addend [- Pivot]. 21 // Return the bits that should be installed in a relocation field for 22 // fixup kind Kind. 23 static uint64_t extractBitsForFixup(MCFixupKind Kind, uint64_t Value) { 24 if (Kind < FirstTargetFixupKind) 25 return Value; 26 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; 33 34 case SystemZ::FK_390_TLS_CALL: 35 return 0; 36 } 37 38 llvm_unreachable("Unknown fixup kind!"); 39 } 40 41 namespace { 42 class SystemZMCAsmBackend : public MCAsmBackend { 43 uint8_t OSABI; 44 public: 45 SystemZMCAsmBackend(uint8_t osABI) 46 : MCAsmBackend(support::big), OSABI(osABI) {} 47 48 // Override MCAsmBackend 49 unsigned getNumFixupKinds() const override { 50 return SystemZ::NumTargetFixupKinds; 51 } 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 fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value, 58 const MCRelaxableFragment *Fragment, 59 const MCAsmLayout &Layout) const override { 60 return false; 61 } 62 bool writeNopData(raw_ostream &OS, uint64_t Count) const override; 63 std::unique_ptr<MCObjectTargetWriter> 64 createObjectTargetWriter() const override { 65 return createSystemZObjectWriter(OSABI); 66 } 67 }; 68 } // end anonymous namespace 69 70 const MCFixupKindInfo & 71 SystemZMCAsmBackend::getFixupKindInfo(MCFixupKind Kind) const { 72 const static MCFixupKindInfo Infos[SystemZ::NumTargetFixupKinds] = { 73 { "FK_390_PC12DBL", 4, 12, MCFixupKindInfo::FKF_IsPCRel }, 74 { "FK_390_PC16DBL", 0, 16, MCFixupKindInfo::FKF_IsPCRel }, 75 { "FK_390_PC24DBL", 0, 24, MCFixupKindInfo::FKF_IsPCRel }, 76 { "FK_390_PC32DBL", 0, 32, MCFixupKindInfo::FKF_IsPCRel }, 77 { "FK_390_TLS_CALL", 0, 0, 0 } 78 }; 79 80 if (Kind < FirstTargetFixupKind) 81 return MCAsmBackend::getFixupKindInfo(Kind); 82 83 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() && 84 "Invalid kind!"); 85 return Infos[Kind - FirstTargetFixupKind]; 86 } 87 88 void SystemZMCAsmBackend::applyFixup(const MCAssembler &Asm, 89 const MCFixup &Fixup, 90 const MCValue &Target, 91 MutableArrayRef<char> Data, uint64_t Value, 92 bool IsResolved, 93 const MCSubtargetInfo *STI) const { 94 MCFixupKind Kind = Fixup.getKind(); 95 unsigned Offset = Fixup.getOffset(); 96 unsigned BitSize = getFixupKindInfo(Kind).TargetSize; 97 unsigned Size = (BitSize + 7) / 8; 98 99 assert(Offset + Size <= Data.size() && "Invalid fixup offset!"); 100 101 // Big-endian insertion of Size bytes. 102 Value = extractBitsForFixup(Kind, Value); 103 if (BitSize < 64) 104 Value &= ((uint64_t)1 << BitSize) - 1; 105 unsigned ShiftValue = (Size * 8) - 8; 106 for (unsigned I = 0; I != Size; ++I) { 107 Data[Offset + I] |= uint8_t(Value >> ShiftValue); 108 ShiftValue -= 8; 109 } 110 } 111 112 bool SystemZMCAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count) const { 113 for (uint64_t I = 0; I != Count; ++I) 114 OS << '\x7'; 115 return true; 116 } 117 118 MCAsmBackend *llvm::createSystemZMCAsmBackend(const Target &T, 119 const MCSubtargetInfo &STI, 120 const MCRegisterInfo &MRI, 121 const MCTargetOptions &Options) { 122 uint8_t OSABI = 123 MCELFObjectTargetWriter::getOSABI(STI.getTargetTriple().getOS()); 124 return new SystemZMCAsmBackend(OSABI); 125 } 126