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