1 //===-- SystemZMCAsmBackend.cpp - SystemZ assembler backend ---------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "MCTargetDesc/SystemZMCFixups.h" 11 #include "MCTargetDesc/SystemZMCTargetDesc.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 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 : 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) const override; 56 bool mayNeedRelaxation(const MCInst &Inst) const override { 57 return false; 58 } 59 bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value, 60 const MCRelaxableFragment *Fragment, 61 const MCAsmLayout &Layout) const override { 62 return false; 63 } 64 void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI, 65 MCInst &Res) const override { 66 llvm_unreachable("SystemZ does do not have assembler relaxation"); 67 } 68 bool writeNopData(uint64_t Count, MCObjectWriter *OW) const override; 69 std::unique_ptr<MCObjectWriter> 70 createObjectWriter(raw_pwrite_stream &OS) const override { 71 return createSystemZObjectWriter(OS, OSABI); 72 } 73 }; 74 } // end anonymous namespace 75 76 const MCFixupKindInfo & 77 SystemZMCAsmBackend::getFixupKindInfo(MCFixupKind Kind) const { 78 const static MCFixupKindInfo Infos[SystemZ::NumTargetFixupKinds] = { 79 { "FK_390_PC12DBL", 4, 12, MCFixupKindInfo::FKF_IsPCRel }, 80 { "FK_390_PC16DBL", 0, 16, MCFixupKindInfo::FKF_IsPCRel }, 81 { "FK_390_PC24DBL", 0, 24, MCFixupKindInfo::FKF_IsPCRel }, 82 { "FK_390_PC32DBL", 0, 32, MCFixupKindInfo::FKF_IsPCRel }, 83 { "FK_390_TLS_CALL", 0, 0, 0 } 84 }; 85 86 if (Kind < FirstTargetFixupKind) 87 return MCAsmBackend::getFixupKindInfo(Kind); 88 89 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() && 90 "Invalid kind!"); 91 return Infos[Kind - FirstTargetFixupKind]; 92 } 93 94 void SystemZMCAsmBackend::applyFixup(const MCAssembler &Asm, 95 const MCFixup &Fixup, 96 const MCValue &Target, 97 MutableArrayRef<char> Data, uint64_t Value, 98 bool IsResolved) const { 99 MCFixupKind Kind = Fixup.getKind(); 100 unsigned Offset = Fixup.getOffset(); 101 unsigned BitSize = getFixupKindInfo(Kind).TargetSize; 102 unsigned Size = (BitSize + 7) / 8; 103 104 assert(Offset + Size <= Data.size() && "Invalid fixup offset!"); 105 106 // Big-endian insertion of Size bytes. 107 Value = extractBitsForFixup(Kind, Value); 108 if (BitSize < 64) 109 Value &= ((uint64_t)1 << BitSize) - 1; 110 unsigned ShiftValue = (Size * 8) - 8; 111 for (unsigned I = 0; I != Size; ++I) { 112 Data[Offset + I] |= uint8_t(Value >> ShiftValue); 113 ShiftValue -= 8; 114 } 115 } 116 117 bool SystemZMCAsmBackend::writeNopData(uint64_t Count, 118 MCObjectWriter *OW) const { 119 for (uint64_t I = 0; I != Count; ++I) 120 OW->write8(7); 121 return true; 122 } 123 124 MCAsmBackend *llvm::createSystemZMCAsmBackend(const Target &T, 125 const MCRegisterInfo &MRI, 126 const Triple &TT, StringRef CPU, 127 const MCTargetOptions &Options) { 128 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TT.getOS()); 129 return new SystemZMCAsmBackend(OSABI); 130 } 131