SystemZMCAsmBackend.cpp revision 76f8ae87b4705f5c08c3995948223531715a2d58
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
18using 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.
23static 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  case SystemZ::FK_390_PLT16DBL:
31  case SystemZ::FK_390_PLT32DBL:
32    return (int64_t)Value / 2;
33  }
34
35  llvm_unreachable("Unknown fixup kind!");
36}
37
38namespace {
39class SystemZMCAsmBackend : public MCAsmBackend {
40  uint8_t OSABI;
41public:
42  SystemZMCAsmBackend(uint8_t osABI)
43    : OSABI(osABI) {}
44
45  // Override MCAsmBackend
46  virtual unsigned getNumFixupKinds() const LLVM_OVERRIDE {
47    return SystemZ::NumTargetFixupKinds;
48  }
49  virtual const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const
50    LLVM_OVERRIDE;
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 {
54    return false;
55  }
56  virtual bool fixupNeedsRelaxation(const MCFixup &Fixup,
57                                    uint64_t Value,
58                                    const MCRelaxableFragment *Fragment,
59                                    const MCAsmLayout &Layout) const
60    LLVM_OVERRIDE {
61    return false;
62  }
63  virtual void relaxInstruction(const MCInst &Inst,
64                                MCInst &Res) const LLVM_OVERRIDE {
65    llvm_unreachable("SystemZ does do not have assembler relaxation");
66  }
67  virtual bool writeNopData(uint64_t Count,
68                            MCObjectWriter *OW) const LLVM_OVERRIDE;
69  virtual MCObjectWriter *createObjectWriter(raw_ostream &OS) const
70    LLVM_OVERRIDE {
71    return createSystemZObjectWriter(OS, OSABI);
72  }
73  virtual bool doesSectionRequireSymbols(const MCSection &Section) const
74    LLVM_OVERRIDE {
75    return false;
76  }
77};
78} // end anonymous namespace
79
80const MCFixupKindInfo &
81SystemZMCAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
82  const static MCFixupKindInfo Infos[SystemZ::NumTargetFixupKinds] = {
83    { "FK_390_PC16DBL",  0, 16, MCFixupKindInfo::FKF_IsPCRel },
84    { "FK_390_PC32DBL",  0, 32, MCFixupKindInfo::FKF_IsPCRel },
85    { "FK_390_PLT16DBL", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
86    { "FK_390_PLT32DBL", 0, 32, MCFixupKindInfo::FKF_IsPCRel }
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
97void SystemZMCAsmBackend::applyFixup(const MCFixup &Fixup, char *Data,
98                                     unsigned DataSize, uint64_t Value) const {
99  MCFixupKind Kind = Fixup.getKind();
100  unsigned Offset = Fixup.getOffset();
101  unsigned Size = (getFixupKindInfo(Kind).TargetSize + 7) / 8;
102
103  assert(Offset + Size <= DataSize && "Invalid fixup offset!");
104
105  // Big-endian insertion of Size bytes.
106  Value = extractBitsForFixup(Kind, Value);
107  unsigned ShiftValue = (Size * 8) - 8;
108  for (unsigned I = 0; I != Size; ++I) {
109    Data[Offset + I] |= uint8_t(Value >> ShiftValue);
110    ShiftValue -= 8;
111  }
112}
113
114bool SystemZMCAsmBackend::writeNopData(uint64_t Count,
115                                       MCObjectWriter *OW) const {
116  for (uint64_t I = 0; I != Count; ++I)
117    OW->Write8(7);
118  return true;
119}
120
121MCAsmBackend *llvm::createSystemZMCAsmBackend(const Target &T,
122                                              const MCRegisterInfo &MRI,
123                                              StringRef TT, StringRef CPU) {
124  uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(Triple(TT).getOS());
125  return new SystemZMCAsmBackend(OSABI);
126}
127