]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/llvm/lib/Target/MBlaze/MBlazeAsmBackend.cpp
Copy head to stable/9 as part of 9.0-RELEASE release cycle.
[FreeBSD/stable/9.git] / contrib / llvm / 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/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 MBlazeELFObjectWriter : public MCELFObjectTargetWriter {
45 public:
46   MBlazeELFObjectWriter(Triple::OSType OSType)
47     : MCELFObjectTargetWriter(/*is64Bit*/ false, OSType, ELF::EM_MBLAZE,
48                               /*HasRelocationAddend*/ true) {}
49 };
50
51 class MBlazeAsmBackend : public TargetAsmBackend {
52 public:
53   MBlazeAsmBackend(const Target &T)
54     : TargetAsmBackend() {
55   }
56
57   unsigned getNumFixupKinds() const {
58     return 2;
59   }
60
61   bool MayNeedRelaxation(const MCInst &Inst) const;
62
63   void RelaxInstruction(const MCInst &Inst, MCInst &Res) const;
64
65   bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const;
66
67   unsigned getPointerSize() const {
68     return 4;
69   }
70 };
71
72 static unsigned getRelaxedOpcode(unsigned Op) {
73     switch (Op) {
74     default:            return Op;
75     case MBlaze::ADDIK: return MBlaze::ADDIK32;
76     case MBlaze::ORI:   return MBlaze::ORI32;
77     case MBlaze::BRLID: return MBlaze::BRLID32;
78     }
79 }
80
81 bool MBlazeAsmBackend::MayNeedRelaxation(const MCInst &Inst) const {
82   if (getRelaxedOpcode(Inst.getOpcode()) == Inst.getOpcode())
83     return false;
84
85   bool hasExprOrImm = false;
86   for (unsigned i = 0; i < Inst.getNumOperands(); ++i)
87     hasExprOrImm |= Inst.getOperand(i).isExpr();
88
89   return hasExprOrImm;
90 }
91
92 void MBlazeAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
93   Res = Inst;
94   Res.setOpcode(getRelaxedOpcode(Inst.getOpcode()));
95 }
96
97 bool MBlazeAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
98   if ((Count % 4) != 0)
99     return false;
100
101   for (uint64_t i = 0; i < Count; i += 4)
102       OW->Write32(0x00000000);
103
104   return true;
105 }
106 } // end anonymous namespace
107
108 namespace {
109 class ELFMBlazeAsmBackend : public MBlazeAsmBackend {
110 public:
111   Triple::OSType OSType;
112   ELFMBlazeAsmBackend(const Target &T, Triple::OSType _OSType)
113     : MBlazeAsmBackend(T), OSType(_OSType) { }
114
115   void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
116                   uint64_t Value) const;
117
118   MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
119     return createELFObjectWriter(new MBlazeELFObjectWriter(OSType), OS,
120                                  /*IsLittleEndian*/ false);
121   }
122 };
123
124 void ELFMBlazeAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data,
125                                      unsigned DataSize, uint64_t Value) const {
126   unsigned Size = getFixupKindSize(Fixup.getKind());
127
128   assert(Fixup.getOffset() + Size <= DataSize &&
129          "Invalid fixup offset!");
130
131   char *data = 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   Triple TheTriple(TT);
154
155   if (TheTriple.isOSDarwin())
156     assert(0 && "Mac not supported on MBlaze");
157
158   if (TheTriple.isOSWindows())
159     assert(0 && "Windows not supported on MBlaze");
160
161   return new ELFMBlazeAsmBackend(T, TheTriple.getOS());
162 }