Switch all uses of LLVM_OVERRIDE to just use 'override' directly.
[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 override {
47     return SystemZ::NumTargetFixupKinds;
48   }
49   virtual const MCFixupKindInfo &
50   getFixupKindInfo(MCFixupKind Kind) const override;
51   virtual void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
52                           uint64_t Value) const override;
53   virtual bool mayNeedRelaxation(const MCInst &Inst) const 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 override {
60     return false;
61   }
62   virtual void relaxInstruction(const MCInst &Inst,
63                                 MCInst &Res) const override {
64     llvm_unreachable("SystemZ does do not have assembler relaxation");
65   }
66   virtual bool writeNopData(uint64_t Count, MCObjectWriter *OW) const override;
67   virtual MCObjectWriter *createObjectWriter(raw_ostream &OS) const override {
68     return createSystemZObjectWriter(OS, OSABI);
69   }
70 };
71 } // end anonymous namespace
72
73 const MCFixupKindInfo &
74 SystemZMCAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
75   const static MCFixupKindInfo Infos[SystemZ::NumTargetFixupKinds] = {
76     { "FK_390_PC16DBL",  0, 16, MCFixupKindInfo::FKF_IsPCRel },
77     { "FK_390_PC32DBL",  0, 32, MCFixupKindInfo::FKF_IsPCRel },
78     { "FK_390_PLT16DBL", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
79     { "FK_390_PLT32DBL", 0, 32, MCFixupKindInfo::FKF_IsPCRel }
80   };
81
82   if (Kind < FirstTargetFixupKind)
83     return MCAsmBackend::getFixupKindInfo(Kind);
84
85   assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
86          "Invalid kind!");
87   return Infos[Kind - FirstTargetFixupKind];
88 }
89
90 void SystemZMCAsmBackend::applyFixup(const MCFixup &Fixup, char *Data,
91                                      unsigned DataSize, uint64_t Value) const {
92   MCFixupKind Kind = Fixup.getKind();
93   unsigned Offset = Fixup.getOffset();
94   unsigned Size = (getFixupKindInfo(Kind).TargetSize + 7) / 8;
95
96   assert(Offset + Size <= DataSize && "Invalid fixup offset!");
97
98   // Big-endian insertion of Size bytes.
99   Value = extractBitsForFixup(Kind, Value);
100   unsigned ShiftValue = (Size * 8) - 8;
101   for (unsigned I = 0; I != Size; ++I) {
102     Data[Offset + I] |= uint8_t(Value >> ShiftValue);
103     ShiftValue -= 8;
104   }
105 }
106
107 bool SystemZMCAsmBackend::writeNopData(uint64_t Count,
108                                        MCObjectWriter *OW) const {
109   for (uint64_t I = 0; I != Count; ++I)
110     OW->Write8(7);
111   return true;
112 }
113
114 MCAsmBackend *llvm::createSystemZMCAsmBackend(const Target &T,
115                                               const MCRegisterInfo &MRI,
116                                               StringRef TT, StringRef CPU) {
117   uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(Triple(TT).getOS());
118   return new SystemZMCAsmBackend(OSABI);
119 }