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