blob: b47bff66f1111ab050e70d4322ac93d8a7734254 [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 Carterc3dd91c2013-01-08 19:01:28 +000040 case Mips::fixup_Mips_GPREL16:
Jack Carterb9f9de92012-06-27 22:48:25 +000041 case Mips::fixup_Mips_GPOFF_HI:
42 case Mips::fixup_Mips_GPOFF_LO:
43 case Mips::fixup_Mips_GOT_PAGE:
44 case Mips::fixup_Mips_GOT_OFST:
Jack Carter5ddcfda2012-07-13 19:15:47 +000045 case Mips::fixup_Mips_GOT_DISP:
Jack Carterb05cb672012-11-21 23:38:59 +000046 case Mips::fixup_Mips_GOT_LO16:
47 case Mips::fixup_Mips_CALL_LO16:
Zoran Jovanovice7ae8af2013-10-23 16:14:44 +000048 case Mips::fixup_MICROMIPS_LO16:
49 case Mips::fixup_MICROMIPS_GOT_PAGE:
50 case Mips::fixup_MICROMIPS_GOT_OFST:
51 case Mips::fixup_MICROMIPS_GOT_DISP:
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000052 break;
53 case Mips::fixup_Mips_PC16:
54 // So far we are only using this type for branches.
55 // For branches we start 1 instruction after the branch
56 // so the displacement will be one instruction size less.
57 Value -= 4;
58 // The displacement is then divided by 4 to give us an 18 bit
59 // address range.
60 Value >>= 2;
61 break;
62 case Mips::fixup_Mips_26:
63 // So far we are only using this type for jumps.
64 // The displacement is then divided by 4 to give us an 28 bit
65 // address range.
66 Value >>= 2;
67 break;
Akira Hatanakaf5ddf132011-11-23 22:18:04 +000068 case Mips::fixup_Mips_HI16:
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +000069 case Mips::fixup_Mips_GOT_Local:
Jack Carterb05cb672012-11-21 23:38:59 +000070 case Mips::fixup_Mips_GOT_HI16:
71 case Mips::fixup_Mips_CALL_HI16:
Zoran Jovanovice7ae8af2013-10-23 16:14:44 +000072 case Mips::fixup_MICROMIPS_HI16:
Jack Carter84491ab2012-08-06 21:26:03 +000073 // Get the 2nd 16-bits. Also add 1 if bit 15 is 1.
Akira Hatanakada728192012-03-27 01:50:08 +000074 Value = ((Value + 0x8000) >> 16) & 0xffff;
Akira Hatanakaf5ddf132011-11-23 22:18:04 +000075 break;
Jack Carter84491ab2012-08-06 21:26:03 +000076 case Mips::fixup_Mips_HIGHER:
77 // Get the 3rd 16-bits.
78 Value = ((Value + 0x80008000LL) >> 32) & 0xffff;
79 break;
80 case Mips::fixup_Mips_HIGHEST:
81 // Get the 4th 16-bits.
82 Value = ((Value + 0x800080008000LL) >> 48) & 0xffff;
83 break;
Zoran Jovanovic507e0842013-10-29 16:38:59 +000084 case Mips::fixup_MICROMIPS_26_S1:
85 Value >>= 1;
86 break;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000087 }
88
89 return Value;
90}
91
Akira Hatanaka587fe6c2011-09-30 21:04:02 +000092namespace {
Akira Hatanaka587fe6c2011-09-30 21:04:02 +000093class MipsAsmBackend : public MCAsmBackend {
Akira Hatanaka1ee768d2012-03-01 01:53:15 +000094 Triple::OSType OSType;
95 bool IsLittle; // Big or little endian
Akira Hatanakab1f68f92012-04-02 19:25:22 +000096 bool Is64Bit; // 32 or 64 bit words
Akira Hatanaka1ee768d2012-03-01 01:53:15 +000097
Akira Hatanaka587fe6c2011-09-30 21:04:02 +000098public:
Akira Hatanakab1f68f92012-04-02 19:25:22 +000099 MipsAsmBackend(const Target &T, Triple::OSType _OSType,
100 bool _isLittle, bool _is64Bit)
101 :MCAsmBackend(), OSType(_OSType), IsLittle(_isLittle), Is64Bit(_is64Bit) {}
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000102
103 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Jack Carter06de0fb2012-07-02 20:04:43 +0000104 return createMipsELFObjectWriter(OS,
105 MCELFObjectTargetWriter::getOSABI(OSType), IsLittle, Is64Bit);
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000106 }
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000107
Dmitri Gribenko5485acd2012-09-14 14:57:36 +0000108 /// ApplyFixup - Apply the \p Value for given \p Fixup into the provided
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000109 /// data fragment, at the offset specified by the fixup and following the
110 /// fixup kind as appropriate.
Jim Grosbachaba3de92012-01-18 18:52:16 +0000111 void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000112 uint64_t Value) const {
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +0000113 MCFixupKind Kind = Fixup.getKind();
114 Value = adjustFixupValue((unsigned)Kind, Value);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000115
Akira Hatanaka3e9d81f2012-04-16 18:00:19 +0000116 if (!Value)
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +0000117 return; // Doesn't change encoding.
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000118
Akira Hatanaka0137dfe2012-03-21 00:52:01 +0000119 // Where do we start in the object
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000120 unsigned Offset = Fixup.getOffset();
Akira Hatanaka0137dfe2012-03-21 00:52:01 +0000121 // Number of bytes we need to fixup
122 unsigned NumBytes = (getFixupKindInfo(Kind).TargetSize + 7) / 8;
123 // Used to point to big endian bytes
124 unsigned FullSize;
125
Craig Topper344e0122012-03-21 02:28:53 +0000126 switch ((unsigned)Kind) {
Akira Hatanaka0137dfe2012-03-21 00:52:01 +0000127 case Mips::fixup_Mips_16:
128 FullSize = 2;
129 break;
130 case Mips::fixup_Mips_64:
131 FullSize = 8;
132 break;
133 default:
134 FullSize = 4;
135 break;
136 }
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +0000137
138 // Grab current value, if any, from bits.
139 uint64_t CurVal = 0;
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +0000140
Akira Hatanaka0137dfe2012-03-21 00:52:01 +0000141 for (unsigned i = 0; i != NumBytes; ++i) {
142 unsigned Idx = IsLittle ? i : (FullSize - 1 - i);
143 CurVal |= (uint64_t)((uint8_t)Data[Offset + Idx]) << (i*8);
144 }
145
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000146 uint64_t Mask = ((uint64_t)(-1) >>
147 (64 - getFixupKindInfo(Kind).TargetSize));
Akira Hatanaka3e9d81f2012-04-16 18:00:19 +0000148 CurVal |= Value & Mask;
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +0000149
Akira Hatanaka0137dfe2012-03-21 00:52:01 +0000150 // Write out the fixed up bytes back to the code/data bits.
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +0000151 for (unsigned i = 0; i != NumBytes; ++i) {
Akira Hatanaka0137dfe2012-03-21 00:52:01 +0000152 unsigned Idx = IsLittle ? i : (FullSize - 1 - i);
153 Data[Offset + Idx] = (uint8_t)((CurVal >> (i*8)) & 0xff);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000154 }
Akira Hatanaka0137dfe2012-03-21 00:52:01 +0000155 }
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000156
157 unsigned getNumFixupKinds() const { return Mips::NumTargetFixupKinds; }
158
159 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
160 const static MCFixupKindInfo Infos[Mips::NumTargetFixupKinds] = {
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +0000161 // This table *must* be in same the order of fixup_* kinds in
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000162 // MipsFixupKinds.h.
163 //
164 // name offset bits flags
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000165 { "fixup_Mips_16", 0, 16, 0 },
166 { "fixup_Mips_32", 0, 32, 0 },
167 { "fixup_Mips_REL32", 0, 32, 0 },
168 { "fixup_Mips_26", 0, 26, 0 },
169 { "fixup_Mips_HI16", 0, 16, 0 },
170 { "fixup_Mips_LO16", 0, 16, 0 },
171 { "fixup_Mips_GPREL16", 0, 16, 0 },
172 { "fixup_Mips_LITERAL", 0, 16, 0 },
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +0000173 { "fixup_Mips_GOT_Global", 0, 16, 0 },
174 { "fixup_Mips_GOT_Local", 0, 16, 0 },
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000175 { "fixup_Mips_PC16", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
176 { "fixup_Mips_CALL16", 0, 16, 0 },
177 { "fixup_Mips_GPREL32", 0, 32, 0 },
178 { "fixup_Mips_SHIFT5", 6, 5, 0 },
179 { "fixup_Mips_SHIFT6", 6, 5, 0 },
180 { "fixup_Mips_64", 0, 64, 0 },
181 { "fixup_Mips_TLSGD", 0, 16, 0 },
182 { "fixup_Mips_GOTTPREL", 0, 16, 0 },
183 { "fixup_Mips_TPREL_HI", 0, 16, 0 },
184 { "fixup_Mips_TPREL_LO", 0, 16, 0 },
Akira Hatanakae2eed962011-12-22 01:05:17 +0000185 { "fixup_Mips_TLSLDM", 0, 16, 0 },
186 { "fixup_Mips_DTPREL_HI", 0, 16, 0 },
187 { "fixup_Mips_DTPREL_LO", 0, 16, 0 },
Jack Carterb9f9de92012-06-27 22:48:25 +0000188 { "fixup_Mips_Branch_PCRel", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
189 { "fixup_Mips_GPOFF_HI", 0, 16, 0 },
190 { "fixup_Mips_GPOFF_LO", 0, 16, 0 },
191 { "fixup_Mips_GOT_PAGE", 0, 16, 0 },
Jack Carter5ddcfda2012-07-13 19:15:47 +0000192 { "fixup_Mips_GOT_OFST", 0, 16, 0 },
Jack Carter84491ab2012-08-06 21:26:03 +0000193 { "fixup_Mips_GOT_DISP", 0, 16, 0 },
194 { "fixup_Mips_HIGHER", 0, 16, 0 },
Jack Carterb05cb672012-11-21 23:38:59 +0000195 { "fixup_Mips_HIGHEST", 0, 16, 0 },
196 { "fixup_Mips_GOT_HI16", 0, 16, 0 },
197 { "fixup_Mips_GOT_LO16", 0, 16, 0 },
198 { "fixup_Mips_CALL_HI16", 0, 16, 0 },
Zoran Jovanovice7ae8af2013-10-23 16:14:44 +0000199 { "fixup_Mips_CALL_LO16", 0, 16, 0 },
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000200 { "fixup_MICROMIPS_26_S1", 0, 26, 0 },
Zoran Jovanovice7ae8af2013-10-23 16:14:44 +0000201 { "fixup_MICROMIPS_HI16", 0, 16, 0 },
202 { "fixup_MICROMIPS_LO16", 0, 16, 0 },
203 { "fixup_MICROMIPS_GOT16", 0, 16, 0 },
204 { "fixup_MICROMIPS_CALL16", 0, 16, 0 },
205 { "fixup_MICROMIPS_GOT_DISP", 0, 16, 0 },
206 { "fixup_MICROMIPS_GOT_PAGE", 0, 16, 0 },
207 { "fixup_MICROMIPS_GOT_OFST", 0, 16, 0 },
208 { "fixup_MICROMIPS_TLS_DTPREL_HI16", 0, 16, 0 },
209 { "fixup_MICROMIPS_TLS_DTPREL_LO16", 0, 16, 0 },
210 { "fixup_MICROMIPS_TLS_TPREL_HI16", 0, 16, 0 },
211 { "fixup_MICROMIPS_TLS_TPREL_LO16", 0, 16, 0 }
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000212 };
213
214 if (Kind < FirstTargetFixupKind)
215 return MCAsmBackend::getFixupKindInfo(Kind);
216
217 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
218 "Invalid kind!");
219 return Infos[Kind - FirstTargetFixupKind];
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000220 }
221
222 /// @name Target Relaxation Interfaces
223 /// @{
224
225 /// MayNeedRelaxation - Check whether the given instruction may need
226 /// relaxation.
227 ///
228 /// \param Inst - The instruction to test.
Jim Grosbachaba3de92012-01-18 18:52:16 +0000229 bool mayNeedRelaxation(const MCInst &Inst) const {
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000230 return false;
231 }
232
Jim Grosbach25b63fa2011-12-06 00:47:03 +0000233 /// fixupNeedsRelaxation - Target specific predicate for whether a given
234 /// fixup requires the associated instruction to be relaxed.
235 bool fixupNeedsRelaxation(const MCFixup &Fixup,
236 uint64_t Value,
Eli Bendersky4d9ada02013-01-08 00:22:56 +0000237 const MCRelaxableFragment *DF,
Jim Grosbach25b63fa2011-12-06 00:47:03 +0000238 const MCAsmLayout &Layout) const {
239 // FIXME.
240 assert(0 && "RelaxInstruction() unimplemented");
NAKAMURA Takumid3002492011-12-06 01:48:32 +0000241 return false;
Jim Grosbach25b63fa2011-12-06 00:47:03 +0000242 }
243
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000244 /// RelaxInstruction - Relax the instruction in the given fragment
245 /// to the next wider instruction.
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000246 ///
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000247 /// \param Inst - The instruction to relax, which may be the same
248 /// as the output.
Dmitri Gribenko881929c2012-09-12 16:59:47 +0000249 /// \param [out] Res On return, the relaxed instruction.
Jim Grosbachaba3de92012-01-18 18:52:16 +0000250 void relaxInstruction(const MCInst &Inst, MCInst &Res) const {
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000251 }
Jia Liuf54f60f2012-02-28 07:46:26 +0000252
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000253 /// @}
254
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000255 /// WriteNopData - Write an (optimal) nop sequence of Count bytes
256 /// to the given output. If the target cannot generate such a sequence,
257 /// it should return an error.
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000258 ///
259 /// \return - True on success.
Jim Grosbachaba3de92012-01-18 18:52:16 +0000260 bool writeNopData(uint64_t Count, MCObjectWriter *OW) const {
Jack Carter570ae0b2012-07-11 22:17:39 +0000261 // Check for a less than instruction size number of bytes
262 // FIXME: 16 bit instructions are not handled yet here.
263 // We shouldn't be using a hard coded number for instruction size.
264 if (Count % 4) return false;
265
266 uint64_t NumNops = Count / 4;
267 for (uint64_t i = 0; i != NumNops; ++i)
268 OW->Write32(0);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000269 return true;
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000270 }
Akira Hatanakab1f68f92012-04-02 19:25:22 +0000271}; // class MipsAsmBackend
Akira Hatanaka587fe6c2011-09-30 21:04:02 +0000272
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000273} // namespace
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000274
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000275// MCAsmBackend
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000276MCAsmBackend *llvm::createMipsAsmBackendEL32(const Target &T,
277 const MCRegisterInfo &MRI,
278 StringRef TT,
Roman Divacky5dd4ccb2012-09-18 16:08:49 +0000279 StringRef CPU) {
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000280 return new MipsAsmBackend(T, Triple(TT).getOS(),
Akira Hatanakab1f68f92012-04-02 19:25:22 +0000281 /*IsLittle*/true, /*Is64Bit*/false);
Rafael Espindola647841b2012-01-11 04:04:14 +0000282}
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000283
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000284MCAsmBackend *llvm::createMipsAsmBackendEB32(const Target &T,
285 const MCRegisterInfo &MRI,
286 StringRef TT,
Roman Divacky5dd4ccb2012-09-18 16:08:49 +0000287 StringRef CPU) {
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000288 return new MipsAsmBackend(T, Triple(TT).getOS(),
Akira Hatanakab1f68f92012-04-02 19:25:22 +0000289 /*IsLittle*/false, /*Is64Bit*/false);
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000290}
Akira Hatanakab1f68f92012-04-02 19:25:22 +0000291
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000292MCAsmBackend *llvm::createMipsAsmBackendEL64(const Target &T,
293 const MCRegisterInfo &MRI,
294 StringRef TT,
Roman Divacky5dd4ccb2012-09-18 16:08:49 +0000295 StringRef CPU) {
Akira Hatanakab1f68f92012-04-02 19:25:22 +0000296 return new MipsAsmBackend(T, Triple(TT).getOS(),
297 /*IsLittle*/true, /*Is64Bit*/true);
298}
299
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000300MCAsmBackend *llvm::createMipsAsmBackendEB64(const Target &T,
301 const MCRegisterInfo &MRI,
302 StringRef TT,
Roman Divacky5dd4ccb2012-09-18 16:08:49 +0000303 StringRef CPU) {
Akira Hatanakab1f68f92012-04-02 19:25:22 +0000304 return new MipsAsmBackend(T, Triple(TT).getOS(),
305 /*IsLittle*/false, /*Is64Bit*/true);
306}
307