blob: ae70e25043c5a98c999359b5887de3a17c2dbb82 [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;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000084 }
85
86 return Value;
87}
88
Akira Hatanaka587fe6c2011-09-30 21:04:02 +000089namespace {
Akira Hatanaka587fe6c2011-09-30 21:04:02 +000090class MipsAsmBackend : public MCAsmBackend {
Akira Hatanaka1ee768d2012-03-01 01:53:15 +000091 Triple::OSType OSType;
92 bool IsLittle; // Big or little endian
Akira Hatanakab1f68f92012-04-02 19:25:22 +000093 bool Is64Bit; // 32 or 64 bit words
Akira Hatanaka1ee768d2012-03-01 01:53:15 +000094
Akira Hatanaka587fe6c2011-09-30 21:04:02 +000095public:
Akira Hatanakab1f68f92012-04-02 19:25:22 +000096 MipsAsmBackend(const Target &T, Triple::OSType _OSType,
97 bool _isLittle, bool _is64Bit)
98 :MCAsmBackend(), OSType(_OSType), IsLittle(_isLittle), Is64Bit(_is64Bit) {}
Akira Hatanaka1ee768d2012-03-01 01:53:15 +000099
100 MCObjectWriter *createObjectWriter(raw_ostream &OS) const {
Jack Carter06de0fb2012-07-02 20:04:43 +0000101 return createMipsELFObjectWriter(OS,
102 MCELFObjectTargetWriter::getOSABI(OSType), IsLittle, Is64Bit);
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000103 }
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000104
Dmitri Gribenko5485acd2012-09-14 14:57:36 +0000105 /// ApplyFixup - Apply the \p Value for given \p Fixup into the provided
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000106 /// data fragment, at the offset specified by the fixup and following the
107 /// fixup kind as appropriate.
Jim Grosbachaba3de92012-01-18 18:52:16 +0000108 void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000109 uint64_t Value) const {
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +0000110 MCFixupKind Kind = Fixup.getKind();
111 Value = adjustFixupValue((unsigned)Kind, Value);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000112
Akira Hatanaka3e9d81f2012-04-16 18:00:19 +0000113 if (!Value)
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +0000114 return; // Doesn't change encoding.
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000115
Akira Hatanaka0137dfe2012-03-21 00:52:01 +0000116 // Where do we start in the object
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000117 unsigned Offset = Fixup.getOffset();
Akira Hatanaka0137dfe2012-03-21 00:52:01 +0000118 // Number of bytes we need to fixup
119 unsigned NumBytes = (getFixupKindInfo(Kind).TargetSize + 7) / 8;
120 // Used to point to big endian bytes
121 unsigned FullSize;
122
Craig Topper344e0122012-03-21 02:28:53 +0000123 switch ((unsigned)Kind) {
Akira Hatanaka0137dfe2012-03-21 00:52:01 +0000124 case Mips::fixup_Mips_16:
125 FullSize = 2;
126 break;
127 case Mips::fixup_Mips_64:
128 FullSize = 8;
129 break;
130 default:
131 FullSize = 4;
132 break;
133 }
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +0000134
135 // Grab current value, if any, from bits.
136 uint64_t CurVal = 0;
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +0000137
Akira Hatanaka0137dfe2012-03-21 00:52:01 +0000138 for (unsigned i = 0; i != NumBytes; ++i) {
139 unsigned Idx = IsLittle ? i : (FullSize - 1 - i);
140 CurVal |= (uint64_t)((uint8_t)Data[Offset + Idx]) << (i*8);
141 }
142
Akira Hatanaka5fd22482012-06-14 21:10:56 +0000143 uint64_t Mask = ((uint64_t)(-1) >>
144 (64 - getFixupKindInfo(Kind).TargetSize));
Akira Hatanaka3e9d81f2012-04-16 18:00:19 +0000145 CurVal |= Value & Mask;
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +0000146
Akira Hatanaka0137dfe2012-03-21 00:52:01 +0000147 // Write out the fixed up bytes back to the code/data bits.
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +0000148 for (unsigned i = 0; i != NumBytes; ++i) {
Akira Hatanaka0137dfe2012-03-21 00:52:01 +0000149 unsigned Idx = IsLittle ? i : (FullSize - 1 - i);
150 Data[Offset + Idx] = (uint8_t)((CurVal >> (i*8)) & 0xff);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000151 }
Akira Hatanaka0137dfe2012-03-21 00:52:01 +0000152 }
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000153
154 unsigned getNumFixupKinds() const { return Mips::NumTargetFixupKinds; }
155
156 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
157 const static MCFixupKindInfo Infos[Mips::NumTargetFixupKinds] = {
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +0000158 // This table *must* be in same the order of fixup_* kinds in
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000159 // MipsFixupKinds.h.
160 //
161 // name offset bits flags
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000162 { "fixup_Mips_16", 0, 16, 0 },
163 { "fixup_Mips_32", 0, 32, 0 },
164 { "fixup_Mips_REL32", 0, 32, 0 },
165 { "fixup_Mips_26", 0, 26, 0 },
166 { "fixup_Mips_HI16", 0, 16, 0 },
167 { "fixup_Mips_LO16", 0, 16, 0 },
168 { "fixup_Mips_GPREL16", 0, 16, 0 },
169 { "fixup_Mips_LITERAL", 0, 16, 0 },
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +0000170 { "fixup_Mips_GOT_Global", 0, 16, 0 },
171 { "fixup_Mips_GOT_Local", 0, 16, 0 },
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000172 { "fixup_Mips_PC16", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
173 { "fixup_Mips_CALL16", 0, 16, 0 },
174 { "fixup_Mips_GPREL32", 0, 32, 0 },
175 { "fixup_Mips_SHIFT5", 6, 5, 0 },
176 { "fixup_Mips_SHIFT6", 6, 5, 0 },
177 { "fixup_Mips_64", 0, 64, 0 },
178 { "fixup_Mips_TLSGD", 0, 16, 0 },
179 { "fixup_Mips_GOTTPREL", 0, 16, 0 },
180 { "fixup_Mips_TPREL_HI", 0, 16, 0 },
181 { "fixup_Mips_TPREL_LO", 0, 16, 0 },
Akira Hatanakae2eed962011-12-22 01:05:17 +0000182 { "fixup_Mips_TLSLDM", 0, 16, 0 },
183 { "fixup_Mips_DTPREL_HI", 0, 16, 0 },
184 { "fixup_Mips_DTPREL_LO", 0, 16, 0 },
Jack Carterb9f9de92012-06-27 22:48:25 +0000185 { "fixup_Mips_Branch_PCRel", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
186 { "fixup_Mips_GPOFF_HI", 0, 16, 0 },
187 { "fixup_Mips_GPOFF_LO", 0, 16, 0 },
188 { "fixup_Mips_GOT_PAGE", 0, 16, 0 },
Jack Carter5ddcfda2012-07-13 19:15:47 +0000189 { "fixup_Mips_GOT_OFST", 0, 16, 0 },
Jack Carter84491ab2012-08-06 21:26:03 +0000190 { "fixup_Mips_GOT_DISP", 0, 16, 0 },
191 { "fixup_Mips_HIGHER", 0, 16, 0 },
Jack Carterb05cb672012-11-21 23:38:59 +0000192 { "fixup_Mips_HIGHEST", 0, 16, 0 },
193 { "fixup_Mips_GOT_HI16", 0, 16, 0 },
194 { "fixup_Mips_GOT_LO16", 0, 16, 0 },
195 { "fixup_Mips_CALL_HI16", 0, 16, 0 },
Zoran Jovanovice7ae8af2013-10-23 16:14:44 +0000196 { "fixup_Mips_CALL_LO16", 0, 16, 0 },
197 { "fixup_MICROMIPS_HI16", 0, 16, 0 },
198 { "fixup_MICROMIPS_LO16", 0, 16, 0 },
199 { "fixup_MICROMIPS_GOT16", 0, 16, 0 },
200 { "fixup_MICROMIPS_CALL16", 0, 16, 0 },
201 { "fixup_MICROMIPS_GOT_DISP", 0, 16, 0 },
202 { "fixup_MICROMIPS_GOT_PAGE", 0, 16, 0 },
203 { "fixup_MICROMIPS_GOT_OFST", 0, 16, 0 },
204 { "fixup_MICROMIPS_TLS_DTPREL_HI16", 0, 16, 0 },
205 { "fixup_MICROMIPS_TLS_DTPREL_LO16", 0, 16, 0 },
206 { "fixup_MICROMIPS_TLS_TPREL_HI16", 0, 16, 0 },
207 { "fixup_MICROMIPS_TLS_TPREL_LO16", 0, 16, 0 }
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000208 };
209
210 if (Kind < FirstTargetFixupKind)
211 return MCAsmBackend::getFixupKindInfo(Kind);
212
213 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
214 "Invalid kind!");
215 return Infos[Kind - FirstTargetFixupKind];
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000216 }
217
218 /// @name Target Relaxation Interfaces
219 /// @{
220
221 /// MayNeedRelaxation - Check whether the given instruction may need
222 /// relaxation.
223 ///
224 /// \param Inst - The instruction to test.
Jim Grosbachaba3de92012-01-18 18:52:16 +0000225 bool mayNeedRelaxation(const MCInst &Inst) const {
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000226 return false;
227 }
228
Jim Grosbach25b63fa2011-12-06 00:47:03 +0000229 /// fixupNeedsRelaxation - Target specific predicate for whether a given
230 /// fixup requires the associated instruction to be relaxed.
231 bool fixupNeedsRelaxation(const MCFixup &Fixup,
232 uint64_t Value,
Eli Bendersky4d9ada02013-01-08 00:22:56 +0000233 const MCRelaxableFragment *DF,
Jim Grosbach25b63fa2011-12-06 00:47:03 +0000234 const MCAsmLayout &Layout) const {
235 // FIXME.
236 assert(0 && "RelaxInstruction() unimplemented");
NAKAMURA Takumid3002492011-12-06 01:48:32 +0000237 return false;
Jim Grosbach25b63fa2011-12-06 00:47:03 +0000238 }
239
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000240 /// RelaxInstruction - Relax the instruction in the given fragment
241 /// to the next wider instruction.
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000242 ///
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000243 /// \param Inst - The instruction to relax, which may be the same
244 /// as the output.
Dmitri Gribenko881929c2012-09-12 16:59:47 +0000245 /// \param [out] Res On return, the relaxed instruction.
Jim Grosbachaba3de92012-01-18 18:52:16 +0000246 void relaxInstruction(const MCInst &Inst, MCInst &Res) const {
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000247 }
Jia Liuf54f60f2012-02-28 07:46:26 +0000248
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000249 /// @}
250
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000251 /// WriteNopData - Write an (optimal) nop sequence of Count bytes
252 /// to the given output. If the target cannot generate such a sequence,
253 /// it should return an error.
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000254 ///
255 /// \return - True on success.
Jim Grosbachaba3de92012-01-18 18:52:16 +0000256 bool writeNopData(uint64_t Count, MCObjectWriter *OW) const {
Jack Carter570ae0b2012-07-11 22:17:39 +0000257 // Check for a less than instruction size number of bytes
258 // FIXME: 16 bit instructions are not handled yet here.
259 // We shouldn't be using a hard coded number for instruction size.
260 if (Count % 4) return false;
261
262 uint64_t NumNops = Count / 4;
263 for (uint64_t i = 0; i != NumNops; ++i)
264 OW->Write32(0);
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000265 return true;
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000266 }
Akira Hatanakab1f68f92012-04-02 19:25:22 +0000267}; // class MipsAsmBackend
Akira Hatanaka587fe6c2011-09-30 21:04:02 +0000268
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000269} // namespace
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000270
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000271// MCAsmBackend
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000272MCAsmBackend *llvm::createMipsAsmBackendEL32(const Target &T,
273 const MCRegisterInfo &MRI,
274 StringRef TT,
Roman Divacky5dd4ccb2012-09-18 16:08:49 +0000275 StringRef CPU) {
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000276 return new MipsAsmBackend(T, Triple(TT).getOS(),
Akira Hatanakab1f68f92012-04-02 19:25:22 +0000277 /*IsLittle*/true, /*Is64Bit*/false);
Rafael Espindola647841b2012-01-11 04:04:14 +0000278}
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000279
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000280MCAsmBackend *llvm::createMipsAsmBackendEB32(const Target &T,
281 const MCRegisterInfo &MRI,
282 StringRef TT,
Roman Divacky5dd4ccb2012-09-18 16:08:49 +0000283 StringRef CPU) {
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000284 return new MipsAsmBackend(T, Triple(TT).getOS(),
Akira Hatanakab1f68f92012-04-02 19:25:22 +0000285 /*IsLittle*/false, /*Is64Bit*/false);
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000286}
Akira Hatanakab1f68f92012-04-02 19:25:22 +0000287
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000288MCAsmBackend *llvm::createMipsAsmBackendEL64(const Target &T,
289 const MCRegisterInfo &MRI,
290 StringRef TT,
Roman Divacky5dd4ccb2012-09-18 16:08:49 +0000291 StringRef CPU) {
Akira Hatanakab1f68f92012-04-02 19:25:22 +0000292 return new MipsAsmBackend(T, Triple(TT).getOS(),
293 /*IsLittle*/true, /*Is64Bit*/true);
294}
295
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000296MCAsmBackend *llvm::createMipsAsmBackendEB64(const Target &T,
297 const MCRegisterInfo &MRI,
298 StringRef TT,
Roman Divacky5dd4ccb2012-09-18 16:08:49 +0000299 StringRef CPU) {
Akira Hatanakab1f68f92012-04-02 19:25:22 +0000300 return new MipsAsmBackend(T, Triple(TT).getOS(),
301 /*IsLittle*/false, /*Is64Bit*/true);
302}
303