blob: 26df966d773f6e4ef0ee76beeeca7257e841eef2 [file] [log] [blame]
Jia Liu9f610112012-02-17 08:55:11 +00001//===-- MipsASMBackend.cpp - Mips Asm Backend ----------------------------===//
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +00002//
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 Hatanaka587fe6c2011-09-30 21:04:02 +000016#include "MCTargetDesc/MipsMCTargetDesc.h"
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000017#include "llvm/MC/MCAsmBackend.h"
Akira Hatanaka587fe6c2011-09-30 21:04:02 +000018#include "llvm/MC/MCAssembler.h"
19#include "llvm/MC/MCDirectives.h"
20#include "llvm/MC/MCELFObjectWriter.h"
Craig Topper6e80c282012-03-26 06:58:25 +000021#include "llvm/MC/MCFixupKindInfo.h"
Akira Hatanaka587fe6c2011-09-30 21:04:02 +000022#include "llvm/MC/MCObjectWriter.h"
Akira Hatanaka587fe6c2011-09-30 21:04:02 +000023#include "llvm/MC/MCSubtargetInfo.h"
Akira Hatanaka587fe6c2011-09-30 21:04:02 +000024#include "llvm/Support/ErrorHandling.h"
25#include "llvm/Support/raw_ostream.h"
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +000026
Akira Hatanaka587fe6c2011-09-30 21:04:02 +000027using namespace llvm;
28
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +000029// Prepare value for the target space for it
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000030static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) {
31
32 // Add/subtract and shift
33 switch (Kind) {
34 default:
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +000035 return 0;
36 case FK_GPRel_4:
37 case FK_Data_4:
Jack Carter4c583812012-08-07 00:01:14 +000038 case FK_Data_8:
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +000039 case Mips::fixup_Mips_LO16:
Jack Carterb9f9de92012-06-27 22:48:25 +000040 case Mips::fixup_Mips_GPOFF_HI:
41 case Mips::fixup_Mips_GPOFF_LO:
42 case Mips::fixup_Mips_GOT_PAGE:
43 case Mips::fixup_Mips_GOT_OFST:
Jack Carter5ddcfda2012-07-13 19:15:47 +000044 case Mips::fixup_Mips_GOT_DISP:
Jack Carterb05cb672012-11-21 23:38:59 +000045 case Mips::fixup_Mips_GOT_LO16:
46 case Mips::fixup_Mips_CALL_LO16:
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000047 break;
48 case Mips::fixup_Mips_PC16:
49 // So far we are only using this type for branches.
50 // For branches we start 1 instruction after the branch
51 // so the displacement will be one instruction size less.
52 Value -= 4;
53 // The displacement is then divided by 4 to give us an 18 bit
54 // address range.
55 Value >>= 2;
56 break;
57 case Mips::fixup_Mips_26:
58 // So far we are only using this type for jumps.
59 // The displacement is then divided by 4 to give us an 28 bit
60 // address range.
61 Value >>= 2;
62 break;
Akira Hatanakaf5ddf132011-11-23 22:18:04 +000063 case Mips::fixup_Mips_HI16:
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +000064 case Mips::fixup_Mips_GOT_Local:
Jack Carterb05cb672012-11-21 23:38:59 +000065 case Mips::fixup_Mips_GOT_HI16:
66 case Mips::fixup_Mips_CALL_HI16:
Jack Carter84491ab2012-08-06 21:26:03 +000067 // Get the 2nd 16-bits. Also add 1 if bit 15 is 1.
Akira Hatanakada728192012-03-27 01:50:08 +000068 Value = ((Value + 0x8000) >> 16) & 0xffff;
Akira Hatanakaf5ddf132011-11-23 22:18:04 +000069 break;
Jack Carter84491ab2012-08-06 21:26:03 +000070 case Mips::fixup_Mips_HIGHER:
71 // Get the 3rd 16-bits.
72 Value = ((Value + 0x80008000LL) >> 32) & 0xffff;
73 break;
74 case Mips::fixup_Mips_HIGHEST:
75 // Get the 4th 16-bits.
76 Value = ((Value + 0x800080008000LL) >> 48) & 0xffff;
77 break;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000078 }
79
80 return Value;
81}
82
Akira Hatanaka587fe6c2011-09-30 21:04:02 +000083namespace {
Akira Hatanaka587fe6c2011-09-30 21:04:02 +000084class MipsAsmBackend : public MCAsmBackend {
Akira Hatanaka1ee768d2012-03-01 01:53:15 +000085 Triple::OSType OSType;
86 bool IsLittle; // Big or little endian
Akira Hatanakab1f68f92012-04-02 19:25:22 +000087 bool Is64Bit; // 32 or 64 bit words
Akira Hatanaka1ee768d2012-03-01 01:53:15 +000088
Akira Hatanaka587fe6c2011-09-30 21:04:02 +000089public:
Akira Hatanakab1f68f92012-04-02 19:25:22 +000090 MipsAsmBackend(const Target &T, Triple::OSType _OSType,
91 bool _isLittle, bool _is64Bit)
92 :MCAsmBackend(), OSType(_OSType), IsLittle(_isLittle), Is64Bit(_is64Bit) {}
Akira Hatanaka1ee768d2012-03-01 01:53:15 +000093
94 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Jack Carter06de0fb2012-07-02 20:04:43 +000095 return createMipsELFObjectWriter(OS,
96 MCELFObjectTargetWriter::getOSABI(OSType), IsLittle, Is64Bit);
Akira Hatanaka1ee768d2012-03-01 01:53:15 +000097 }
Akira Hatanaka44220ca2011-09-30 21:23:45 +000098
Dmitri Gribenko5485acd2012-09-14 14:57:36 +000099 /// ApplyFixup - Apply the \p Value for given \p Fixup into the provided
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000100 /// data fragment, at the offset specified by the fixup and following the
101 /// fixup kind as appropriate.
Jim Grosbachaba3de92012-01-18 18:52:16 +0000102 void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000103 uint64_t Value) const {
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +0000104 MCFixupKind Kind = Fixup.getKind();
105 Value = adjustFixupValue((unsigned)Kind, Value);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000106
Akira Hatanaka3e9d81f2012-04-16 18:00:19 +0000107 if (!Value)
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +0000108 return; // Doesn't change encoding.
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000109
Akira Hatanaka0137dfe2012-03-21 00:52:01 +0000110 // Where do we start in the object
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000111 unsigned Offset = Fixup.getOffset();
Akira Hatanaka0137dfe2012-03-21 00:52:01 +0000112 // Number of bytes we need to fixup
113 unsigned NumBytes = (getFixupKindInfo(Kind).TargetSize + 7) / 8;
114 // Used to point to big endian bytes
115 unsigned FullSize;
116
Craig Topper344e0122012-03-21 02:28:53 +0000117 switch ((unsigned)Kind) {
Akira Hatanaka0137dfe2012-03-21 00:52:01 +0000118 case Mips::fixup_Mips_16:
119 FullSize = 2;
120 break;
121 case Mips::fixup_Mips_64:
122 FullSize = 8;
123 break;
124 default:
125 FullSize = 4;
126 break;
127 }
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +0000128
129 // Grab current value, if any, from bits.
130 uint64_t CurVal = 0;
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +0000131
Akira Hatanaka0137dfe2012-03-21 00:52:01 +0000132 for (unsigned i = 0; i != NumBytes; ++i) {
133 unsigned Idx = IsLittle ? i : (FullSize - 1 - i);
134 CurVal |= (uint64_t)((uint8_t)Data[Offset + Idx]) << (i*8);
135 }
136
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000137 uint64_t Mask = ((uint64_t)(-1) >>
138 (64 - getFixupKindInfo(Kind).TargetSize));
Akira Hatanaka3e9d81f2012-04-16 18:00:19 +0000139 CurVal |= Value & Mask;
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +0000140
Akira Hatanaka0137dfe2012-03-21 00:52:01 +0000141 // Write out the fixed up bytes back to the code/data bits.
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +0000142 for (unsigned i = 0; i != NumBytes; ++i) {
Akira Hatanaka0137dfe2012-03-21 00:52:01 +0000143 unsigned Idx = IsLittle ? i : (FullSize - 1 - i);
144 Data[Offset + Idx] = (uint8_t)((CurVal >> (i*8)) & 0xff);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000145 }
Akira Hatanaka0137dfe2012-03-21 00:52:01 +0000146 }
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000147
148 unsigned getNumFixupKinds() const { return Mips::NumTargetFixupKinds; }
149
150 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
151 const static MCFixupKindInfo Infos[Mips::NumTargetFixupKinds] = {
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +0000152 // This table *must* be in same the order of fixup_* kinds in
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000153 // MipsFixupKinds.h.
154 //
155 // name offset bits flags
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000156 { "fixup_Mips_16", 0, 16, 0 },
157 { "fixup_Mips_32", 0, 32, 0 },
158 { "fixup_Mips_REL32", 0, 32, 0 },
159 { "fixup_Mips_26", 0, 26, 0 },
160 { "fixup_Mips_HI16", 0, 16, 0 },
161 { "fixup_Mips_LO16", 0, 16, 0 },
162 { "fixup_Mips_GPREL16", 0, 16, 0 },
163 { "fixup_Mips_LITERAL", 0, 16, 0 },
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +0000164 { "fixup_Mips_GOT_Global", 0, 16, 0 },
165 { "fixup_Mips_GOT_Local", 0, 16, 0 },
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000166 { "fixup_Mips_PC16", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
167 { "fixup_Mips_CALL16", 0, 16, 0 },
168 { "fixup_Mips_GPREL32", 0, 32, 0 },
169 { "fixup_Mips_SHIFT5", 6, 5, 0 },
170 { "fixup_Mips_SHIFT6", 6, 5, 0 },
171 { "fixup_Mips_64", 0, 64, 0 },
172 { "fixup_Mips_TLSGD", 0, 16, 0 },
173 { "fixup_Mips_GOTTPREL", 0, 16, 0 },
174 { "fixup_Mips_TPREL_HI", 0, 16, 0 },
175 { "fixup_Mips_TPREL_LO", 0, 16, 0 },
Akira Hatanakae2eed962011-12-22 01:05:17 +0000176 { "fixup_Mips_TLSLDM", 0, 16, 0 },
177 { "fixup_Mips_DTPREL_HI", 0, 16, 0 },
178 { "fixup_Mips_DTPREL_LO", 0, 16, 0 },
Jack Carterb9f9de92012-06-27 22:48:25 +0000179 { "fixup_Mips_Branch_PCRel", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
180 { "fixup_Mips_GPOFF_HI", 0, 16, 0 },
181 { "fixup_Mips_GPOFF_LO", 0, 16, 0 },
182 { "fixup_Mips_GOT_PAGE", 0, 16, 0 },
Jack Carter5ddcfda2012-07-13 19:15:47 +0000183 { "fixup_Mips_GOT_OFST", 0, 16, 0 },
Jack Carter84491ab2012-08-06 21:26:03 +0000184 { "fixup_Mips_GOT_DISP", 0, 16, 0 },
185 { "fixup_Mips_HIGHER", 0, 16, 0 },
Jack Carterb05cb672012-11-21 23:38:59 +0000186 { "fixup_Mips_HIGHEST", 0, 16, 0 },
187 { "fixup_Mips_GOT_HI16", 0, 16, 0 },
188 { "fixup_Mips_GOT_LO16", 0, 16, 0 },
189 { "fixup_Mips_CALL_HI16", 0, 16, 0 },
190 { "fixup_Mips_CALL_LO16", 0, 16, 0 }
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000191 };
192
193 if (Kind < FirstTargetFixupKind)
194 return MCAsmBackend::getFixupKindInfo(Kind);
195
196 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
197 "Invalid kind!");
198 return Infos[Kind - FirstTargetFixupKind];
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000199 }
200
201 /// @name Target Relaxation Interfaces
202 /// @{
203
204 /// MayNeedRelaxation - Check whether the given instruction may need
205 /// relaxation.
206 ///
207 /// \param Inst - The instruction to test.
Jim Grosbachaba3de92012-01-18 18:52:16 +0000208 bool mayNeedRelaxation(const MCInst &Inst) const {
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000209 return false;
210 }
211
Jim Grosbach25b63fa2011-12-06 00:47:03 +0000212 /// fixupNeedsRelaxation - Target specific predicate for whether a given
213 /// fixup requires the associated instruction to be relaxed.
214 bool fixupNeedsRelaxation(const MCFixup &Fixup,
215 uint64_t Value,
Eli Bendersky4d9ada02013-01-08 00:22:56 +0000216 const MCRelaxableFragment *DF,
Jim Grosbach25b63fa2011-12-06 00:47:03 +0000217 const MCAsmLayout &Layout) const {
218 // FIXME.
219 assert(0 && "RelaxInstruction() unimplemented");
NAKAMURA Takumid3002492011-12-06 01:48:32 +0000220 return false;
Jim Grosbach25b63fa2011-12-06 00:47:03 +0000221 }
222
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000223 /// RelaxInstruction - Relax the instruction in the given fragment
224 /// to the next wider instruction.
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000225 ///
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000226 /// \param Inst - The instruction to relax, which may be the same
227 /// as the output.
Dmitri Gribenko881929c2012-09-12 16:59:47 +0000228 /// \param [out] Res On return, the relaxed instruction.
Jim Grosbachaba3de92012-01-18 18:52:16 +0000229 void relaxInstruction(const MCInst &Inst, MCInst &Res) const {
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000230 }
Jia Liuf54f60f2012-02-28 07:46:26 +0000231
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000232 /// @}
233
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000234 /// WriteNopData - Write an (optimal) nop sequence of Count bytes
235 /// to the given output. If the target cannot generate such a sequence,
236 /// it should return an error.
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000237 ///
238 /// \return - True on success.
Jim Grosbachaba3de92012-01-18 18:52:16 +0000239 bool writeNopData(uint64_t Count, MCObjectWriter *OW) const {
Jack Carter570ae0b2012-07-11 22:17:39 +0000240 // Check for a less than instruction size number of bytes
241 // FIXME: 16 bit instructions are not handled yet here.
242 // We shouldn't be using a hard coded number for instruction size.
243 if (Count % 4) return false;
244
245 uint64_t NumNops = Count / 4;
246 for (uint64_t i = 0; i != NumNops; ++i)
247 OW->Write32(0);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000248 return true;
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000249 }
Akira Hatanakab1f68f92012-04-02 19:25:22 +0000250}; // class MipsAsmBackend
Akira Hatanaka587fe6c2011-09-30 21:04:02 +0000251
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000252} // namespace
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000253
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000254// MCAsmBackend
Roman Divacky5dd4ccb2012-09-18 16:08:49 +0000255MCAsmBackend *llvm::createMipsAsmBackendEL32(const Target &T, StringRef TT,
256 StringRef CPU) {
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000257 return new MipsAsmBackend(T, Triple(TT).getOS(),
Akira Hatanakab1f68f92012-04-02 19:25:22 +0000258 /*IsLittle*/true, /*Is64Bit*/false);
Rafael Espindola647841b2012-01-11 04:04:14 +0000259}
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000260
Roman Divacky5dd4ccb2012-09-18 16:08:49 +0000261MCAsmBackend *llvm::createMipsAsmBackendEB32(const Target &T, StringRef TT,
262 StringRef CPU) {
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000263 return new MipsAsmBackend(T, Triple(TT).getOS(),
Akira Hatanakab1f68f92012-04-02 19:25:22 +0000264 /*IsLittle*/false, /*Is64Bit*/false);
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000265}
Akira Hatanakab1f68f92012-04-02 19:25:22 +0000266
Roman Divacky5dd4ccb2012-09-18 16:08:49 +0000267MCAsmBackend *llvm::createMipsAsmBackendEL64(const Target &T, StringRef TT,
268 StringRef CPU) {
Akira Hatanakab1f68f92012-04-02 19:25:22 +0000269 return new MipsAsmBackend(T, Triple(TT).getOS(),
270 /*IsLittle*/true, /*Is64Bit*/true);
271}
272
Roman Divacky5dd4ccb2012-09-18 16:08:49 +0000273MCAsmBackend *llvm::createMipsAsmBackendEB64(const Target &T, StringRef TT,
274 StringRef CPU) {
Akira Hatanakab1f68f92012-04-02 19:25:22 +0000275 return new MipsAsmBackend(T, Triple(TT).getOS(),
276 /*IsLittle*/false, /*Is64Bit*/true);
277}
278