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/SystemZMCTargetDesc.h"
11 #include "MCTargetDesc/SystemZMCFixups.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.
extractBitsForFixup(MCFixupKind Kind,uint64_t Value)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_PC16DBL:
29 case SystemZ::FK_390_PC32DBL:
30 return (int64_t)Value / 2;
31
32 case SystemZ::FK_390_TLS_CALL:
33 return 0;
34 }
35
36 llvm_unreachable("Unknown fixup kind!");
37 }
38
39 namespace {
40 class SystemZMCAsmBackend : public MCAsmBackend {
41 uint8_t OSABI;
42 public:
SystemZMCAsmBackend(uint8_t osABI)43 SystemZMCAsmBackend(uint8_t osABI)
44 : OSABI(osABI) {}
45
46 // Override MCAsmBackend
getNumFixupKinds() const47 unsigned getNumFixupKinds() const override {
48 return SystemZ::NumTargetFixupKinds;
49 }
50 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override;
51 void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
52 uint64_t Value, bool IsPCRel) const override;
mayNeedRelaxation(const MCInst & Inst) const53 bool mayNeedRelaxation(const MCInst &Inst) const override {
54 return false;
55 }
fixupNeedsRelaxation(const MCFixup & Fixup,uint64_t Value,const MCRelaxableFragment * Fragment,const MCAsmLayout & Layout) const56 bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
57 const MCRelaxableFragment *Fragment,
58 const MCAsmLayout &Layout) const override {
59 return false;
60 }
relaxInstruction(const MCInst & Inst,const MCSubtargetInfo & STI,MCInst & Res) const61 void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI,
62 MCInst &Res) const override {
63 llvm_unreachable("SystemZ does do not have assembler relaxation");
64 }
65 bool writeNopData(uint64_t Count, MCObjectWriter *OW) const override;
createObjectWriter(raw_pwrite_stream & OS) const66 MCObjectWriter *createObjectWriter(raw_pwrite_stream &OS) const override {
67 return createSystemZObjectWriter(OS, OSABI);
68 }
69 };
70 } // end anonymous namespace
71
72 const MCFixupKindInfo &
getFixupKindInfo(MCFixupKind Kind) const73 SystemZMCAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
74 const static MCFixupKindInfo Infos[SystemZ::NumTargetFixupKinds] = {
75 { "FK_390_PC16DBL", 0, 16, 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
applyFixup(const MCFixup & Fixup,char * Data,unsigned DataSize,uint64_t Value,bool IsPCRel) const88 void SystemZMCAsmBackend::applyFixup(const MCFixup &Fixup, char *Data,
89 unsigned DataSize, uint64_t Value,
90 bool IsPCRel) const {
91 MCFixupKind Kind = Fixup.getKind();
92 unsigned Offset = Fixup.getOffset();
93 unsigned Size = (getFixupKindInfo(Kind).TargetSize + 7) / 8;
94
95 assert(Offset + Size <= DataSize && "Invalid fixup offset!");
96
97 // Big-endian insertion of Size bytes.
98 Value = extractBitsForFixup(Kind, Value);
99 unsigned ShiftValue = (Size * 8) - 8;
100 for (unsigned I = 0; I != Size; ++I) {
101 Data[Offset + I] |= uint8_t(Value >> ShiftValue);
102 ShiftValue -= 8;
103 }
104 }
105
writeNopData(uint64_t Count,MCObjectWriter * OW) const106 bool SystemZMCAsmBackend::writeNopData(uint64_t Count,
107 MCObjectWriter *OW) const {
108 for (uint64_t I = 0; I != Count; ++I)
109 OW->write8(7);
110 return true;
111 }
112
createSystemZMCAsmBackend(const Target & T,const MCRegisterInfo & MRI,const Triple & TT,StringRef CPU)113 MCAsmBackend *llvm::createSystemZMCAsmBackend(const Target &T,
114 const MCRegisterInfo &MRI,
115 const Triple &TT, StringRef CPU) {
116 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TT.getOS());
117 return new SystemZMCAsmBackend(OSABI);
118 }
119