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