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