1 //===-- SystemZMCAsmBackend.cpp - SystemZ assembler backend ---------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
20 // Value is a fully-resolved relocation value: Symbol + Addend [- Pivot].
21 // Return the bits that should be installed in a relocation field for
23 static uint64_t extractBitsForFixup(MCFixupKind Kind, uint64_t Value) {
24 if (Kind < FirstTargetFixupKind)
27 switch (unsigned(Kind)) {
28 case SystemZ::FK_390_PC16DBL:
29 case SystemZ::FK_390_PC32DBL:
30 case SystemZ::FK_390_PLT16DBL:
31 case SystemZ::FK_390_PLT32DBL:
32 return (int64_t)Value / 2;
35 llvm_unreachable("Unknown fixup kind!");
39 class SystemZMCAsmBackend : public MCAsmBackend {
42 SystemZMCAsmBackend(uint8_t osABI)
45 // Override MCAsmBackend
46 virtual unsigned getNumFixupKinds() const LLVM_OVERRIDE {
47 return SystemZ::NumTargetFixupKinds;
49 virtual const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const
51 virtual void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
52 uint64_t Value) const LLVM_OVERRIDE;
53 virtual bool mayNeedRelaxation(const MCInst &Inst) const LLVM_OVERRIDE {
56 virtual bool fixupNeedsRelaxation(const MCFixup &Fixup,
58 const MCRelaxableFragment *Fragment,
59 const MCAsmLayout &Layout) const
63 virtual void relaxInstruction(const MCInst &Inst,
64 MCInst &Res) const LLVM_OVERRIDE {
65 llvm_unreachable("SystemZ does do not have assembler relaxation");
67 virtual bool writeNopData(uint64_t Count,
68 MCObjectWriter *OW) const LLVM_OVERRIDE;
69 virtual MCObjectWriter *createObjectWriter(raw_ostream &OS) const
71 return createSystemZObjectWriter(OS, OSABI);
74 } // end anonymous namespace
76 const MCFixupKindInfo &
77 SystemZMCAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
78 const static MCFixupKindInfo Infos[SystemZ::NumTargetFixupKinds] = {
79 { "FK_390_PC16DBL", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
80 { "FK_390_PC32DBL", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
81 { "FK_390_PLT16DBL", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
82 { "FK_390_PLT32DBL", 0, 32, MCFixupKindInfo::FKF_IsPCRel }
85 if (Kind < FirstTargetFixupKind)
86 return MCAsmBackend::getFixupKindInfo(Kind);
88 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
90 return Infos[Kind - FirstTargetFixupKind];
93 void SystemZMCAsmBackend::applyFixup(const MCFixup &Fixup, char *Data,
94 unsigned DataSize, uint64_t Value) const {
95 MCFixupKind Kind = Fixup.getKind();
96 unsigned Offset = Fixup.getOffset();
97 unsigned Size = (getFixupKindInfo(Kind).TargetSize + 7) / 8;
99 assert(Offset + Size <= DataSize && "Invalid fixup offset!");
101 // Big-endian insertion of Size bytes.
102 Value = extractBitsForFixup(Kind, Value);
103 unsigned ShiftValue = (Size * 8) - 8;
104 for (unsigned I = 0; I != Size; ++I) {
105 Data[Offset + I] |= uint8_t(Value >> ShiftValue);
110 bool SystemZMCAsmBackend::writeNopData(uint64_t Count,
111 MCObjectWriter *OW) const {
112 for (uint64_t I = 0; I != Count; ++I)
117 MCAsmBackend *llvm::createSystemZMCAsmBackend(const Target &T,
118 const MCRegisterInfo &MRI,
119 StringRef TT, StringRef CPU) {
120 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(Triple(TT).getOS());
121 return new SystemZMCAsmBackend(OSABI);