blob: 20e16368f87f6ab6bfcc8bb9055a8171ca07a6bc [file] [log] [blame]
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +00001//===-- MipsASMBackend.cpp - ---------===//
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// This file implements the MipsAsmBackend and MipsELFObjectWriter classes.
11//
12//===----------------------------------------------------------------------===//
13//
14
15#include "MipsFixupKinds.h"
Akira Hatanaka82ea7312011-09-30 21:04:02 +000016#include "MCTargetDesc/MipsMCTargetDesc.h"
17#include "llvm/ADT/Twine.h"
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +000018#include "llvm/MC/MCAsmBackend.h"
Akira Hatanaka82ea7312011-09-30 21:04:02 +000019#include "llvm/MC/MCAssembler.h"
20#include "llvm/MC/MCDirectives.h"
21#include "llvm/MC/MCELFObjectWriter.h"
22#include "llvm/MC/MCExpr.h"
23#include "llvm/MC/MCMachObjectWriter.h"
24#include "llvm/MC/MCObjectWriter.h"
25#include "llvm/MC/MCSectionELF.h"
26#include "llvm/MC/MCSectionMachO.h"
Akira Hatanaka82ea7312011-09-30 21:04:02 +000027#include "llvm/MC/MCSubtargetInfo.h"
28#include "llvm/Object/MachOFormat.h"
29#include "llvm/Support/ELF.h"
30#include "llvm/Support/ErrorHandling.h"
31#include "llvm/Support/raw_ostream.h"
32using namespace llvm;
33
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +000034static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
35
36 // Add/subtract and shift
37 switch (Kind) {
38 default:
39 break;
40 case Mips::fixup_Mips_PC16:
41 // So far we are only using this type for branches.
42 // For branches we start 1 instruction after the branch
43 // so the displacement will be one instruction size less.
44 Value -= 4;
45 // The displacement is then divided by 4 to give us an 18 bit
46 // address range.
47 Value >>= 2;
48 break;
49 case Mips::fixup_Mips_26:
50 // So far we are only using this type for jumps.
51 // The displacement is then divided by 4 to give us an 28 bit
52 // address range.
53 Value >>= 2;
54 break;
55 }
56
57 // Mask off value for placement as an operand
58 switch (Kind) {
59 default:
60 break;
Akira Hatanaka84bfc2f2011-11-23 22:18:04 +000061 case FK_GPRel_4:
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +000062 case FK_Data_4:
63 Value &= 0xffffffff;
64 break;
65 case Mips::fixup_Mips_26:
66 Value &= 0x03ffffff;
67 break;
68 case Mips::fixup_Mips_LO16:
69 case Mips::fixup_Mips_PC16:
70 Value &= 0x0000ffff;
71 break;
Akira Hatanaka84bfc2f2011-11-23 22:18:04 +000072 case Mips::fixup_Mips_HI16:
73 Value >>= 16;
74 break;
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +000075 }
76
77 return Value;
78}
79
Akira Hatanaka82ea7312011-09-30 21:04:02 +000080namespace {
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +000081
Akira Hatanaka82ea7312011-09-30 21:04:02 +000082class MipsELFObjectWriter : public MCELFObjectTargetWriter {
83public:
84 MipsELFObjectWriter(bool is64Bit, Triple::OSType OSType, uint16_t EMachine,
85 bool HasRelocationAddend)
86 : MCELFObjectTargetWriter(is64Bit, OSType, EMachine,
87 HasRelocationAddend) {}
88};
89
90class MipsAsmBackend : public MCAsmBackend {
91public:
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +000092 MipsAsmBackend(const Target &T) : MCAsmBackend() {}
Akira Hatanaka4b6ee7a2011-09-30 21:23:45 +000093
94 /// ApplyFixup - Apply the \arg Value for given \arg Fixup into the provided
95 /// data fragment, at the offset specified by the fixup and following the
96 /// fixup kind as appropriate.
97 void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
98 uint64_t Value) const {
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +000099 unsigned Kind = (unsigned)Fixup.getKind();
100 Value = adjustFixupValue(Kind, Value);
101
102 if (!Value)
103 return; // Doesn't change encoding.
104
105 unsigned Offset = Fixup.getOffset();
106 switch (Kind) {
107 default:
108 llvm_unreachable("Unknown fixup kind!");
109 case Mips::fixup_Mips_GOT16: // This will be fixed up at link time
110 break;
Akira Hatanaka84bfc2f2011-11-23 22:18:04 +0000111 case FK_GPRel_4:
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +0000112 case FK_Data_4:
113 case Mips::fixup_Mips_26:
114 case Mips::fixup_Mips_LO16:
115 case Mips::fixup_Mips_PC16:
Akira Hatanaka84bfc2f2011-11-23 22:18:04 +0000116 case Mips::fixup_Mips_HI16:
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +0000117 // For each byte of the fragment that the fixup touches, mask i
118 // the fixup value. The Value has been "split up" into the appr
119 // bitfields above.
120 for (unsigned i = 0; i != 4; ++i) // FIXME - Need to support 2 and 8 bytes
Akira Hatanaka84bfc2f2011-11-23 22:18:04 +0000121 Data[Offset + i] += uint8_t((Value >> (i * 8)) & 0xff);
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +0000122 break;
123 }
124 }
125
126 unsigned getNumFixupKinds() const { return Mips::NumTargetFixupKinds; }
127
128 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
129 const static MCFixupKindInfo Infos[Mips::NumTargetFixupKinds] = {
130 // This table *must* be in the order that the fixup_* kinds a
131 // MipsFixupKinds.h.
132 //
133 // name offset bits flags
134 { "fixup_Mips_NONE", 0, 0, 0 },
135 { "fixup_Mips_16", 0, 16, 0 },
136 { "fixup_Mips_32", 0, 32, 0 },
137 { "fixup_Mips_REL32", 0, 32, 0 },
138 { "fixup_Mips_26", 0, 26, 0 },
139 { "fixup_Mips_HI16", 0, 16, 0 },
140 { "fixup_Mips_LO16", 0, 16, 0 },
141 { "fixup_Mips_GPREL16", 0, 16, 0 },
142 { "fixup_Mips_LITERAL", 0, 16, 0 },
143 { "fixup_Mips_GOT16", 0, 16, 0 },
144 { "fixup_Mips_PC16", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
145 { "fixup_Mips_CALL16", 0, 16, 0 },
146 { "fixup_Mips_GPREL32", 0, 32, 0 },
147 { "fixup_Mips_SHIFT5", 6, 5, 0 },
148 { "fixup_Mips_SHIFT6", 6, 5, 0 },
149 { "fixup_Mips_64", 0, 64, 0 },
150 { "fixup_Mips_TLSGD", 0, 16, 0 },
151 { "fixup_Mips_GOTTPREL", 0, 16, 0 },
152 { "fixup_Mips_TPREL_HI", 0, 16, 0 },
153 { "fixup_Mips_TPREL_LO", 0, 16, 0 },
154 { "fixup_Mips_Branch_PCRel", 0, 16, MCFixupKindInfo::FKF_IsPCRel }
155 };
156
157 if (Kind < FirstTargetFixupKind)
158 return MCAsmBackend::getFixupKindInfo(Kind);
159
160 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
161 "Invalid kind!");
162 return Infos[Kind - FirstTargetFixupKind];
Akira Hatanaka4b6ee7a2011-09-30 21:23:45 +0000163 }
164
165 /// @name Target Relaxation Interfaces
166 /// @{
167
168 /// MayNeedRelaxation - Check whether the given instruction may need
169 /// relaxation.
170 ///
171 /// \param Inst - The instruction to test.
172 bool MayNeedRelaxation(const MCInst &Inst) const {
173 return false;
174 }
175
Jim Grosbach370b78d2011-12-06 00:47:03 +0000176 /// fixupNeedsRelaxation - Target specific predicate for whether a given
177 /// fixup requires the associated instruction to be relaxed.
178 bool fixupNeedsRelaxation(const MCFixup &Fixup,
179 uint64_t Value,
180 const MCInstFragment *DF,
181 const MCAsmLayout &Layout) const {
182 // FIXME.
183 assert(0 && "RelaxInstruction() unimplemented");
184 }
185
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +0000186 /// RelaxInstruction - Relax the instruction in the given fragment
187 /// to the next wider instruction.
Akira Hatanaka4b6ee7a2011-09-30 21:23:45 +0000188 ///
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +0000189 /// \param Inst - The instruction to relax, which may be the same
190 /// as the output.
Akira Hatanaka4b6ee7a2011-09-30 21:23:45 +0000191 /// \parm Res [output] - On return, the relaxed instruction.
192 void RelaxInstruction(const MCInst &Inst, MCInst &Res) const {
193 }
194
195 /// @}
196
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +0000197 /// WriteNopData - Write an (optimal) nop sequence of Count bytes
198 /// to the given output. If the target cannot generate such a sequence,
199 /// it should return an error.
Akira Hatanaka4b6ee7a2011-09-30 21:23:45 +0000200 ///
201 /// \return - True on success.
202 bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const {
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +0000203 return true;
Akira Hatanaka4b6ee7a2011-09-30 21:23:45 +0000204 }
Akira Hatanaka82ea7312011-09-30 21:04:02 +0000205};
206
207class MipsEB_AsmBackend : public MipsAsmBackend {
208public:
209 Triple::OSType OSType;
210
211 MipsEB_AsmBackend(const Target &T, Triple::OSType _OSType)
212 : MipsAsmBackend(T), OSType(_OSType) {}
213
214 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
215 return createELFObjectWriter(createELFObjectTargetWriter(),
216 OS, /*IsLittleEndian*/ false);
217 }
218
219 MCELFObjectTargetWriter *createELFObjectTargetWriter() const {
220 return new MipsELFObjectWriter(false, OSType, ELF::EM_MIPS, false);
221 }
222};
223
224class MipsEL_AsmBackend : public MipsAsmBackend {
225public:
226 Triple::OSType OSType;
227
228 MipsEL_AsmBackend(const Target &T, Triple::OSType _OSType)
229 : MipsAsmBackend(T), OSType(_OSType) {}
230
231 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
232 return createELFObjectWriter(createELFObjectTargetWriter(),
233 OS, /*IsLittleEndian*/ true);
234 }
235
236 MCELFObjectTargetWriter *createELFObjectTargetWriter() const {
237 return new MipsELFObjectWriter(false, OSType, ELF::EM_MIPS, false);
238 }
239};
Bruno Cardoso Lopes47b92f32011-11-11 22:58:42 +0000240} // namespace
Akira Hatanaka4b6ee7a2011-09-30 21:23:45 +0000241
242MCAsmBackend *llvm::createMipsAsmBackend(const Target &T, StringRef TT) {
243 Triple TheTriple(TT);
244
245 // just return little endian for now
246 //
247 return new MipsEL_AsmBackend(T, Triple(TT).getOS());
248}