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