Define generic 1, 2 and 4 byte pc relative relocations. They are common
[oota-llvm.git] / lib / Target / MBlaze / MBlazeAsmBackend.cpp
1 //===-- MBlazeAsmBackend.cpp - MBlaze 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 "llvm/Target/TargetAsmBackend.h"
11 #include "MBlaze.h"
12 #include "MBlazeELFWriterInfo.h"
13 #include "llvm/ADT/Twine.h"
14 #include "llvm/MC/MCAssembler.h"
15 #include "llvm/MC/MCAsmLayout.h"
16 #include "llvm/MC/MCELFSymbolFlags.h"
17 #include "llvm/MC/MCExpr.h"
18 #include "llvm/MC/MCObjectFormat.h"
19 #include "llvm/MC/MCObjectWriter.h"
20 #include "llvm/MC/MCSectionELF.h"
21 #include "llvm/MC/MCSectionMachO.h"
22 #include "llvm/MC/MCValue.h"
23 #include "llvm/Support/ELF.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Target/TargetRegistry.h"
27 #include "llvm/Target/TargetAsmBackend.h"
28 using namespace llvm;
29
30 static unsigned getFixupKindSize(unsigned Kind) {
31   switch (Kind) {
32   default: assert(0 && "invalid fixup kind!");
33   case FK_Data_1: return 1;
34   case FK_PCRel_2:
35   case FK_Data_2: return 2;
36   case FK_PCRel_4:
37   case FK_Data_4: return 4;
38   case FK_Data_8: return 8;
39   }
40 }
41
42
43 namespace {
44 class MBlazeAsmBackend : public TargetAsmBackend {
45 public:
46   MBlazeAsmBackend(const Target &T)
47     : TargetAsmBackend() {
48   }
49
50   bool MayNeedRelaxation(const MCInst &Inst) const;
51
52   void RelaxInstruction(const MCInst &Inst, MCInst &Res) const;
53
54   bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const;
55
56   unsigned getPointerSize() const {
57     return 4;
58   }
59 };
60
61 static unsigned getRelaxedOpcode(unsigned Op) {
62     switch (Op) {
63     default:            return Op;
64     case MBlaze::ADDI:  return MBlaze::ADDI32;
65     case MBlaze::ORI:   return MBlaze::ORI32;
66     case MBlaze::BRLID: return MBlaze::BRLID32;
67     }
68 }
69
70 bool MBlazeAsmBackend::MayNeedRelaxation(const MCInst &Inst) const {
71   if (getRelaxedOpcode(Inst.getOpcode()) == Inst.getOpcode())
72     return false;
73
74   bool hasExprOrImm = false;
75   for (unsigned i = 0; i < Inst.getNumOperands(); ++i)
76     hasExprOrImm |= Inst.getOperand(i).isExpr();
77
78   return hasExprOrImm;
79 }
80
81 void MBlazeAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
82   Res = Inst;
83   Res.setOpcode(getRelaxedOpcode(Inst.getOpcode()));
84 }
85
86 bool MBlazeAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
87   if ((Count % 4) != 0)
88     return false;
89
90   for (uint64_t i = 0; i < Count; i += 4)
91       OW->Write32(0x00000000);
92
93   return true;
94 }
95 } // end anonymous namespace
96
97 namespace {
98 class ELFMBlazeAsmBackend : public MBlazeAsmBackend {
99   MCELFObjectFormat Format;
100
101 public:
102   Triple::OSType OSType;
103   ELFMBlazeAsmBackend(const Target &T, Triple::OSType _OSType)
104     : MBlazeAsmBackend(T), OSType(_OSType) {
105     HasScatteredSymbols = true;
106   }
107
108   virtual const MCObjectFormat &getObjectFormat() const {
109     return Format;
110   }
111
112
113   void ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
114                   uint64_t Value) const;
115
116   MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
117     return createELFObjectWriter(OS,/*Is64Bit=*/false,
118                                  OSType, ELF::EM_MBLAZE,
119                                  /*IsLittleEndian=*/false,
120                                  /*HasRelocationAddend=*/true);
121   }
122 };
123
124 void ELFMBlazeAsmBackend::ApplyFixup(const MCFixup &Fixup, MCDataFragment &DF,
125                                      uint64_t Value) const {
126   unsigned Size = getFixupKindSize(Fixup.getKind());
127
128   assert(Fixup.getOffset() + Size <= DF.getContents().size() &&
129          "Invalid fixup offset!");
130
131   char *data = DF.getContents().data() + Fixup.getOffset();
132   switch (Size) {
133   default: llvm_unreachable("Cannot fixup unknown value.");
134   case 1:  llvm_unreachable("Cannot fixup 1 byte value.");
135   case 8:  llvm_unreachable("Cannot fixup 8 byte value.");
136
137   case 4:
138     *(data+7) = uint8_t(Value);
139     *(data+6) = uint8_t(Value >> 8);
140     *(data+3) = uint8_t(Value >> 16);
141     *(data+2) = uint8_t(Value >> 24);
142     break;
143
144   case 2:
145     *(data+3) = uint8_t(Value >> 0);
146     *(data+2) = uint8_t(Value >> 8);
147   }
148 }
149 } // end anonymous namespace
150
151 TargetAsmBackend *llvm::createMBlazeAsmBackend(const Target &T,
152                                             const std::string &TT) {
153   switch (Triple(TT).getOS()) {
154   case Triple::Darwin:
155     assert(0 && "Mac not supported on MBlaze");
156   case Triple::MinGW32:
157   case Triple::Cygwin:
158   case Triple::Win32:
159     assert(0 && "Windows not supported on MBlaze");
160   default:
161     return new ELFMBlazeAsmBackend(T, Triple(TT).getOS());
162   }
163 }