Just returning false is the default.
[oota-llvm.git] / lib / Target / SystemZ / MCTargetDesc / SystemZMCAsmBackend.cpp
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.
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   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
38 namespace {
39 class SystemZMCAsmBackend : public MCAsmBackend {
40   uint8_t OSABI;
41 public:
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 };
74 } // end anonymous namespace
75
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 }
83   };
84
85   if (Kind < FirstTargetFixupKind)
86     return MCAsmBackend::getFixupKindInfo(Kind);
87
88   assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
89          "Invalid kind!");
90   return Infos[Kind - FirstTargetFixupKind];
91 }
92
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;
98
99   assert(Offset + Size <= DataSize && "Invalid fixup offset!");
100
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);
106     ShiftValue -= 8;
107   }
108 }
109
110 bool SystemZMCAsmBackend::writeNopData(uint64_t Count,
111                                        MCObjectWriter *OW) const {
112   for (uint64_t I = 0; I != Count; ++I)
113     OW->Write8(7);
114   return true;
115 }
116
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);
122 }