MC: Move target specific fixup info descriptors to TargetAsmBackend instead of
[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   unsigned getNumFixupKinds() const {
51     return 2;
52   }
53
54   bool MayNeedRelaxation(const MCInst &Inst) const;
55
56   void RelaxInstruction(const MCInst &Inst, MCInst &Res) const;
57
58   bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const;
59
60   unsigned getPointerSize() const {
61     return 4;
62   }
63 };
64
65 static unsigned getRelaxedOpcode(unsigned Op) {
66     switch (Op) {
67     default:            return Op;
68     case MBlaze::ADDIK: return MBlaze::ADDIK32;
69     case MBlaze::ORI:   return MBlaze::ORI32;
70     case MBlaze::BRLID: return MBlaze::BRLID32;
71     }
72 }
73
74 bool MBlazeAsmBackend::MayNeedRelaxation(const MCInst &Inst) const {
75   if (getRelaxedOpcode(Inst.getOpcode()) == Inst.getOpcode())
76     return false;
77
78   bool hasExprOrImm = false;
79   for (unsigned i = 0; i < Inst.getNumOperands(); ++i)
80     hasExprOrImm |= Inst.getOperand(i).isExpr();
81
82   return hasExprOrImm;
83 }
84
85 void MBlazeAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
86   Res = Inst;
87   Res.setOpcode(getRelaxedOpcode(Inst.getOpcode()));
88 }
89
90 bool MBlazeAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
91   if ((Count % 4) != 0)
92     return false;
93
94   for (uint64_t i = 0; i < Count; i += 4)
95       OW->Write32(0x00000000);
96
97   return true;
98 }
99 } // end anonymous namespace
100
101 namespace {
102 class ELFMBlazeAsmBackend : public MBlazeAsmBackend {
103   MCELFObjectFormat Format;
104
105 public:
106   Triple::OSType OSType;
107   ELFMBlazeAsmBackend(const Target &T, Triple::OSType _OSType)
108     : MBlazeAsmBackend(T), OSType(_OSType) {
109     HasScatteredSymbols = true;
110   }
111
112   virtual const MCObjectFormat &getObjectFormat() const {
113     return Format;
114   }
115
116
117   void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
118                   uint64_t Value) const;
119
120   MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
121     return createELFObjectWriter(OS,/*Is64Bit=*/false,
122                                  OSType, ELF::EM_MBLAZE,
123                                  /*IsLittleEndian=*/false,
124                                  /*HasRelocationAddend=*/true);
125   }
126 };
127
128 void ELFMBlazeAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
129                                      unsigned DataSize, uint64_t Value) const {
130   unsigned Size = getFixupKindSize(Fixup.getKind());
131
132   assert(Fixup.getOffset() + Size <= DataSize &&
133          "Invalid fixup offset!");
134
135   char *data = Data + Fixup.getOffset();
136   switch (Size) {
137   default: llvm_unreachable("Cannot fixup unknown value.");
138   case 1:  llvm_unreachable("Cannot fixup 1 byte value.");
139   case 8:  llvm_unreachable("Cannot fixup 8 byte value.");
140
141   case 4:
142     *(data+7) = uint8_t(Value);
143     *(data+6) = uint8_t(Value >> 8);
144     *(data+3) = uint8_t(Value >> 16);
145     *(data+2) = uint8_t(Value >> 24);
146     break;
147
148   case 2:
149     *(data+3) = uint8_t(Value >> 0);
150     *(data+2) = uint8_t(Value >> 8);
151   }
152 }
153 } // end anonymous namespace
154
155 TargetAsmBackend *llvm::createMBlazeAsmBackend(const Target &T,
156                                             const std::string &TT) {
157   switch (Triple(TT).getOS()) {
158   case Triple::Darwin:
159     assert(0 && "Mac not supported on MBlaze");
160   case Triple::MinGW32:
161   case Triple::Cygwin:
162   case Triple::Win32:
163     assert(0 && "Windows not supported on MBlaze");
164   default:
165     return new ELFMBlazeAsmBackend(T, Triple(TT).getOS());
166   }
167 }