blob: acbc6d37e24bedfd85282f3f20cf0eacdc17fb1e [file] [log] [blame]
Zoran Jovanovicada38ef2014-03-27 12:38:40 +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//
Zoran Jovanovicada38ef2014-03-27 12:38:40 +000010// This file implements the MipsAsmBackend class.
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000011//
12//===----------------------------------------------------------------------===//
13//
14
Zoran Jovanovicada38ef2014-03-27 12:38:40 +000015#include "MCTargetDesc/MipsAsmBackend.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include "MCTargetDesc/MipsFixupKinds.h"
Daniel Sandersfe98b2f2016-05-03 13:35:44 +000017#include "MCTargetDesc/MipsMCExpr.h"
Akira Hatanaka587fe6c2011-09-30 21:04:02 +000018#include "MCTargetDesc/MipsMCTargetDesc.h"
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000019#include "llvm/MC/MCAsmBackend.h"
Akira Hatanaka587fe6c2011-09-30 21:04:02 +000020#include "llvm/MC/MCAssembler.h"
Matheus Almeidae0d75aa2013-12-13 11:11:02 +000021#include "llvm/MC/MCContext.h"
Akira Hatanaka587fe6c2011-09-30 21:04:02 +000022#include "llvm/MC/MCDirectives.h"
23#include "llvm/MC/MCELFObjectWriter.h"
Craig Topper6e80c282012-03-26 06:58:25 +000024#include "llvm/MC/MCFixupKindInfo.h"
Akira Hatanaka587fe6c2011-09-30 21:04:02 +000025#include "llvm/MC/MCObjectWriter.h"
Akira Hatanaka587fe6c2011-09-30 21:04:02 +000026#include "llvm/MC/MCSubtargetInfo.h"
Simon Atanasyan117665582017-09-21 10:44:26 +000027#include "llvm/MC/MCTargetOptions.h"
Daniel Sandersfe98b2f2016-05-03 13:35:44 +000028#include "llvm/MC/MCValue.h"
Akira Hatanaka587fe6c2011-09-30 21:04:02 +000029#include "llvm/Support/ErrorHandling.h"
Daniel Sandersfe98b2f2016-05-03 13:35:44 +000030#include "llvm/Support/Format.h"
Matheus Almeidae0d75aa2013-12-13 11:11:02 +000031#include "llvm/Support/MathExtras.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000032#include "llvm/Support/raw_ostream.h"
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +000033
Akira Hatanaka587fe6c2011-09-30 21:04:02 +000034using namespace llvm;
35
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +000036// Prepare value for the target space for it
Matheus Almeidae0d75aa2013-12-13 11:11:02 +000037static unsigned adjustFixupValue(const MCFixup &Fixup, uint64_t Value,
Alex Bradbury866113c2017-04-05 10:16:14 +000038 MCContext &Ctx) {
Matheus Almeidae0d75aa2013-12-13 11:11:02 +000039
40 unsigned Kind = Fixup.getKind();
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000041
42 // Add/subtract and shift
43 switch (Kind) {
44 default:
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +000045 return 0;
Ed Maste2a710d02014-03-03 14:27:49 +000046 case FK_Data_2:
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +000047 case Mips::fixup_Mips_LO16:
Jack Carterc3dd91c2013-01-08 19:01:28 +000048 case Mips::fixup_Mips_GPREL16:
Jack Carterb9f9de92012-06-27 22:48:25 +000049 case Mips::fixup_Mips_GPOFF_HI:
50 case Mips::fixup_Mips_GPOFF_LO:
51 case Mips::fixup_Mips_GOT_PAGE:
52 case Mips::fixup_Mips_GOT_OFST:
Jack Carter5ddcfda2012-07-13 19:15:47 +000053 case Mips::fixup_Mips_GOT_DISP:
Jack Carterb05cb672012-11-21 23:38:59 +000054 case Mips::fixup_Mips_GOT_LO16:
55 case Mips::fixup_Mips_CALL_LO16:
Zoran Jovanovice7ae8af2013-10-23 16:14:44 +000056 case Mips::fixup_MICROMIPS_LO16:
57 case Mips::fixup_MICROMIPS_GOT_PAGE:
58 case Mips::fixup_MICROMIPS_GOT_OFST:
59 case Mips::fixup_MICROMIPS_GOT_DISP:
Zoran Jovanovicb355e8f2014-05-27 14:58:51 +000060 case Mips::fixup_MIPS_PCLO16:
Daniel Sandersfe98b2f2016-05-03 13:35:44 +000061 Value &= 0xffff;
62 break;
Simon Atanasyaneb9ed612016-08-22 16:18:42 +000063 case FK_DTPRel_4:
64 case FK_DTPRel_8:
65 case FK_TPRel_4:
66 case FK_TPRel_8:
Daniel Sandersfe98b2f2016-05-03 13:35:44 +000067 case FK_GPRel_4:
68 case FK_Data_4:
69 case FK_Data_8:
Daniel Sanders3feeb9c2016-08-08 11:50:25 +000070 case Mips::fixup_Mips_SUB:
71 case Mips::fixup_MICROMIPS_SUB:
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000072 break;
73 case Mips::fixup_Mips_PC16:
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000074 // The displacement is then divided by 4 to give us an 18 bit
Matheus Almeidae0d75aa2013-12-13 11:11:02 +000075 // address range. Forcing a signed division because Value can be negative.
76 Value = (int64_t)Value / 4;
77 // We now check if Value can be encoded as a 16-bit signed immediate.
Alex Bradbury866113c2017-04-05 10:16:14 +000078 if (!isInt<16>(Value)) {
79 Ctx.reportError(Fixup.getLoc(), "out of range PC16 fixup");
Oliver Stannard9be59af2015-11-17 10:00:43 +000080 return 0;
81 }
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000082 break;
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +000083 case Mips::fixup_MIPS_PC19_S2:
Zoran Jovanovic6764fa72016-04-21 14:09:35 +000084 case Mips::fixup_MICROMIPS_PC19_S2:
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +000085 // Forcing a signed division because Value can be negative.
86 Value = (int64_t)Value / 4;
87 // We now check if Value can be encoded as a 19-bit signed immediate.
Alex Bradbury866113c2017-04-05 10:16:14 +000088 if (!isInt<19>(Value)) {
89 Ctx.reportError(Fixup.getLoc(), "out of range PC19 fixup");
Oliver Stannard9be59af2015-11-17 10:00:43 +000090 return 0;
91 }
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +000092 break;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000093 case Mips::fixup_Mips_26:
94 // So far we are only using this type for jumps.
95 // The displacement is then divided by 4 to give us an 28 bit
96 // address range.
97 Value >>= 2;
98 break;
Akira Hatanakaf5ddf132011-11-23 22:18:04 +000099 case Mips::fixup_Mips_HI16:
Daniel Sandersfe98b2f2016-05-03 13:35:44 +0000100 case Mips::fixup_Mips_GOT:
Daniel Sandersa2bde882016-05-16 09:33:59 +0000101 case Mips::fixup_MICROMIPS_GOT16:
Jack Carterb05cb672012-11-21 23:38:59 +0000102 case Mips::fixup_Mips_GOT_HI16:
103 case Mips::fixup_Mips_CALL_HI16:
Zoran Jovanovice7ae8af2013-10-23 16:14:44 +0000104 case Mips::fixup_MICROMIPS_HI16:
Zoran Jovanovicb355e8f2014-05-27 14:58:51 +0000105 case Mips::fixup_MIPS_PCHI16:
Jack Carter84491ab2012-08-06 21:26:03 +0000106 // Get the 2nd 16-bits. Also add 1 if bit 15 is 1.
Akira Hatanakada728192012-03-27 01:50:08 +0000107 Value = ((Value + 0x8000) >> 16) & 0xffff;
Akira Hatanakaf5ddf132011-11-23 22:18:04 +0000108 break;
Jack Carter84491ab2012-08-06 21:26:03 +0000109 case Mips::fixup_Mips_HIGHER:
110 // Get the 3rd 16-bits.
111 Value = ((Value + 0x80008000LL) >> 32) & 0xffff;
112 break;
113 case Mips::fixup_Mips_HIGHEST:
114 // Get the 4th 16-bits.
115 Value = ((Value + 0x800080008000LL) >> 48) & 0xffff;
116 break;
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000117 case Mips::fixup_MICROMIPS_26_S1:
118 Value >>= 1;
119 break;
Jozef Kolek9761e962015-01-12 12:03:34 +0000120 case Mips::fixup_MICROMIPS_PC7_S1:
121 Value -= 4;
122 // Forcing a signed division because Value can be negative.
123 Value = (int64_t) Value / 2;
124 // We now check if Value can be encoded as a 7-bit signed immediate.
Alex Bradbury866113c2017-04-05 10:16:14 +0000125 if (!isInt<7>(Value)) {
126 Ctx.reportError(Fixup.getLoc(), "out of range PC7 fixup");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000127 return 0;
128 }
Jozef Kolek9761e962015-01-12 12:03:34 +0000129 break;
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000130 case Mips::fixup_MICROMIPS_PC10_S1:
131 Value -= 2;
132 // Forcing a signed division because Value can be negative.
133 Value = (int64_t) Value / 2;
134 // We now check if Value can be encoded as a 10-bit signed immediate.
Alex Bradbury866113c2017-04-05 10:16:14 +0000135 if (!isInt<10>(Value)) {
136 Ctx.reportError(Fixup.getLoc(), "out of range PC10 fixup");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000137 return 0;
138 }
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000139 break;
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000140 case Mips::fixup_MICROMIPS_PC16_S1:
141 Value -= 4;
Matheus Almeidae0d75aa2013-12-13 11:11:02 +0000142 // Forcing a signed division because Value can be negative.
143 Value = (int64_t)Value / 2;
144 // We now check if Value can be encoded as a 16-bit signed immediate.
Alex Bradbury866113c2017-04-05 10:16:14 +0000145 if (!isInt<16>(Value)) {
146 Ctx.reportError(Fixup.getLoc(), "out of range PC16 fixup");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000147 return 0;
148 }
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000149 break;
Zoran Jovanovica5acdcf2014-06-13 14:26:47 +0000150 case Mips::fixup_MIPS_PC18_S3:
151 // Forcing a signed division because Value can be negative.
152 Value = (int64_t)Value / 8;
153 // We now check if Value can be encoded as a 18-bit signed immediate.
Alex Bradbury866113c2017-04-05 10:16:14 +0000154 if (!isInt<18>(Value)) {
155 Ctx.reportError(Fixup.getLoc(), "out of range PC18 fixup");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000156 return 0;
157 }
Zoran Jovanovica5acdcf2014-06-13 14:26:47 +0000158 break;
Zoran Jovanovic8e366822016-04-22 10:15:12 +0000159 case Mips::fixup_MICROMIPS_PC18_S3:
160 // Check alignment.
Alex Bradbury866113c2017-04-05 10:16:14 +0000161 if ((Value & 7)) {
162 Ctx.reportError(Fixup.getLoc(), "out of range PC18 fixup");
Zoran Jovanovic8e366822016-04-22 10:15:12 +0000163 }
164 // Forcing a signed division because Value can be negative.
165 Value = (int64_t)Value / 8;
166 // We now check if Value can be encoded as a 18-bit signed immediate.
Alex Bradbury866113c2017-04-05 10:16:14 +0000167 if (!isInt<18>(Value)) {
168 Ctx.reportError(Fixup.getLoc(), "out of range PC18 fixup");
Zoran Jovanovic8e366822016-04-22 10:15:12 +0000169 return 0;
170 }
171 break;
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000172 case Mips::fixup_MIPS_PC21_S2:
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000173 // Forcing a signed division because Value can be negative.
174 Value = (int64_t) Value / 4;
175 // We now check if Value can be encoded as a 21-bit signed immediate.
Alex Bradbury866113c2017-04-05 10:16:14 +0000176 if (!isInt<21>(Value)) {
177 Ctx.reportError(Fixup.getLoc(), "out of range PC21 fixup");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000178 return 0;
179 }
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000180 break;
181 case Mips::fixup_MIPS_PC26_S2:
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000182 // Forcing a signed division because Value can be negative.
183 Value = (int64_t) Value / 4;
184 // We now check if Value can be encoded as a 26-bit signed immediate.
Alex Bradbury866113c2017-04-05 10:16:14 +0000185 if (!isInt<26>(Value)) {
186 Ctx.reportError(Fixup.getLoc(), "out of range PC26 fixup");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000187 return 0;
188 }
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000189 break;
Zoran Jovanovic02b70032016-04-21 13:43:26 +0000190 case Mips::fixup_MICROMIPS_PC26_S1:
191 // Forcing a signed division because Value can be negative.
192 Value = (int64_t)Value / 2;
193 // We now check if Value can be encoded as a 26-bit signed immediate.
Alex Bradbury866113c2017-04-05 10:16:14 +0000194 if (!isInt<26>(Value)) {
195 Ctx.reportFatalError(Fixup.getLoc(), "out of range PC26 fixup");
Zoran Jovanovic02b70032016-04-21 13:43:26 +0000196 return 0;
197 }
198 break;
Zoran Jovanovic5f94ced2016-05-19 12:20:40 +0000199 case Mips::fixup_MICROMIPS_PC21_S1:
200 // Forcing a signed division because Value can be negative.
201 Value = (int64_t)Value / 2;
202 // We now check if Value can be encoded as a 21-bit signed immediate.
Alex Bradbury866113c2017-04-05 10:16:14 +0000203 if (!isInt<21>(Value)) {
204 Ctx.reportError(Fixup.getLoc(), "out of range PC21 fixup");
Zoran Jovanovic5f94ced2016-05-19 12:20:40 +0000205 return 0;
206 }
207 break;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000208 }
209
210 return Value;
211}
212
Lang Hames60fbc7c2017-10-10 16:28:07 +0000213std::unique_ptr<MCObjectWriter>
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000214MipsAsmBackend::createObjectWriter(raw_pwrite_stream &OS) const {
Simon Atanasyan117665582017-09-21 10:44:26 +0000215 return createMipsELFObjectWriter(OS, TheTriple, IsN32);
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000216}
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000217
Zoran Jovanovic842f20e2014-04-03 12:01:01 +0000218// Little-endian fixup data byte ordering:
219// mips32r2: a | b | x | x
220// microMIPS: x | x | a | b
221
222static bool needsMMLEByteOrder(unsigned Kind) {
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000223 return Kind != Mips::fixup_MICROMIPS_PC10_S1 &&
224 Kind >= Mips::fixup_MICROMIPS_26_S1 &&
Zoran Jovanovic842f20e2014-04-03 12:01:01 +0000225 Kind < Mips::LastTargetFixupKind;
226}
227
228// Calculate index for microMIPS specific little endian byte order
229static unsigned calculateMMLEIndex(unsigned i) {
230 assert(i <= 3 && "Index out of range!");
231
232 return (1 - i / 2) * 2 + i % 2;
233}
234
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000235/// ApplyFixup - Apply the \p Value for given \p Fixup into the provided
236/// data fragment, at the offset specified by the fixup and following the
237/// fixup kind as appropriate.
Rafael Espindola801b42d2017-06-23 22:52:36 +0000238void MipsAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
239 const MCValue &Target,
Rafael Espindola88d9e372017-06-21 23:06:53 +0000240 MutableArrayRef<char> Data, uint64_t Value,
Rafael Espindola1beb7022017-07-11 23:18:25 +0000241 bool IsResolved) const {
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000242 MCFixupKind Kind = Fixup.getKind();
Rafael Espindolaf3512922017-06-24 00:26:57 +0000243 MCContext &Ctx = Asm.getContext();
Alex Bradbury866113c2017-04-05 10:16:14 +0000244 Value = adjustFixupValue(Fixup, Value, Ctx);
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000245
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000246 if (!Value)
247 return; // Doesn't change encoding.
248
249 // Where do we start in the object
250 unsigned Offset = Fixup.getOffset();
251 // Number of bytes we need to fixup
252 unsigned NumBytes = (getFixupKindInfo(Kind).TargetSize + 7) / 8;
253 // Used to point to big endian bytes
254 unsigned FullSize;
255
256 switch ((unsigned)Kind) {
257 case FK_Data_2:
258 case Mips::fixup_Mips_16:
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000259 case Mips::fixup_MICROMIPS_PC10_S1:
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000260 FullSize = 2;
261 break;
262 case FK_Data_8:
263 case Mips::fixup_Mips_64:
264 FullSize = 8;
265 break;
266 case FK_Data_4:
267 default:
268 FullSize = 4;
269 break;
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000270 }
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000271
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000272 // Grab current value, if any, from bits.
273 uint64_t CurVal = 0;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000274
Zoran Jovanovic842f20e2014-04-03 12:01:01 +0000275 bool microMipsLEByteOrder = needsMMLEByteOrder((unsigned) Kind);
276
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000277 for (unsigned i = 0; i != NumBytes; ++i) {
Zoran Jovanovic842f20e2014-04-03 12:01:01 +0000278 unsigned Idx = IsLittle ? (microMipsLEByteOrder ? calculateMMLEIndex(i)
279 : i)
280 : (FullSize - 1 - i);
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000281 CurVal |= (uint64_t)((uint8_t)Data[Offset + Idx]) << (i*8);
Akira Hatanaka0137dfe2012-03-21 00:52:01 +0000282 }
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000283
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000284 uint64_t Mask = ((uint64_t)(-1) >>
285 (64 - getFixupKindInfo(Kind).TargetSize));
286 CurVal |= Value & Mask;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000287
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000288 // Write out the fixed up bytes back to the code/data bits.
289 for (unsigned i = 0; i != NumBytes; ++i) {
Zoran Jovanovic842f20e2014-04-03 12:01:01 +0000290 unsigned Idx = IsLittle ? (microMipsLEByteOrder ? calculateMMLEIndex(i)
291 : i)
292 : (FullSize - 1 - i);
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000293 Data[Offset + Idx] = (uint8_t)((CurVal >> (i*8)) & 0xff);
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000294 }
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000295}
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000296
David Majnemerce108422016-01-19 23:05:27 +0000297Optional<MCFixupKind> MipsAsmBackend::getFixupKind(StringRef Name) const {
298 return StringSwitch<Optional<MCFixupKind>>(Name)
299 .Case("R_MIPS_NONE", (MCFixupKind)Mips::fixup_Mips_NONE)
300 .Case("R_MIPS_32", FK_Data_4)
301 .Default(MCAsmBackend::getFixupKind(Name));
Daniel Sanders9f6ad492015-11-12 13:33:00 +0000302}
303
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000304const MCFixupKindInfo &MipsAsmBackend::
305getFixupKindInfo(MCFixupKind Kind) const {
Daniel Sanders683ed962014-05-23 13:35:24 +0000306 const static MCFixupKindInfo LittleEndianInfos[Mips::NumTargetFixupKinds] = {
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000307 // This table *must* be in same the order of fixup_* kinds in
308 // MipsFixupKinds.h.
309 //
310 // name offset bits flags
Daniel Sanders9f6ad492015-11-12 13:33:00 +0000311 { "fixup_Mips_NONE", 0, 0, 0 },
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000312 { "fixup_Mips_16", 0, 16, 0 },
313 { "fixup_Mips_32", 0, 32, 0 },
314 { "fixup_Mips_REL32", 0, 32, 0 },
315 { "fixup_Mips_26", 0, 26, 0 },
316 { "fixup_Mips_HI16", 0, 16, 0 },
317 { "fixup_Mips_LO16", 0, 16, 0 },
318 { "fixup_Mips_GPREL16", 0, 16, 0 },
319 { "fixup_Mips_LITERAL", 0, 16, 0 },
Daniel Sandersfe98b2f2016-05-03 13:35:44 +0000320 { "fixup_Mips_GOT", 0, 16, 0 },
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000321 { "fixup_Mips_PC16", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
322 { "fixup_Mips_CALL16", 0, 16, 0 },
323 { "fixup_Mips_GPREL32", 0, 32, 0 },
324 { "fixup_Mips_SHIFT5", 6, 5, 0 },
325 { "fixup_Mips_SHIFT6", 6, 5, 0 },
326 { "fixup_Mips_64", 0, 64, 0 },
327 { "fixup_Mips_TLSGD", 0, 16, 0 },
328 { "fixup_Mips_GOTTPREL", 0, 16, 0 },
329 { "fixup_Mips_TPREL_HI", 0, 16, 0 },
330 { "fixup_Mips_TPREL_LO", 0, 16, 0 },
331 { "fixup_Mips_TLSLDM", 0, 16, 0 },
332 { "fixup_Mips_DTPREL_HI", 0, 16, 0 },
333 { "fixup_Mips_DTPREL_LO", 0, 16, 0 },
334 { "fixup_Mips_Branch_PCRel", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
335 { "fixup_Mips_GPOFF_HI", 0, 16, 0 },
336 { "fixup_Mips_GPOFF_LO", 0, 16, 0 },
337 { "fixup_Mips_GOT_PAGE", 0, 16, 0 },
338 { "fixup_Mips_GOT_OFST", 0, 16, 0 },
339 { "fixup_Mips_GOT_DISP", 0, 16, 0 },
340 { "fixup_Mips_HIGHER", 0, 16, 0 },
341 { "fixup_Mips_HIGHEST", 0, 16, 0 },
342 { "fixup_Mips_GOT_HI16", 0, 16, 0 },
343 { "fixup_Mips_GOT_LO16", 0, 16, 0 },
344 { "fixup_Mips_CALL_HI16", 0, 16, 0 },
345 { "fixup_Mips_CALL_LO16", 0, 16, 0 },
Zoran Jovanovica5acdcf2014-06-13 14:26:47 +0000346 { "fixup_Mips_PC18_S3", 0, 18, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +0000347 { "fixup_MIPS_PC19_S2", 0, 19, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000348 { "fixup_MIPS_PC21_S2", 0, 21, MCFixupKindInfo::FKF_IsPCRel },
349 { "fixup_MIPS_PC26_S2", 0, 26, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovicb355e8f2014-05-27 14:58:51 +0000350 { "fixup_MIPS_PCHI16", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
351 { "fixup_MIPS_PCLO16", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000352 { "fixup_MICROMIPS_26_S1", 0, 26, 0 },
353 { "fixup_MICROMIPS_HI16", 0, 16, 0 },
354 { "fixup_MICROMIPS_LO16", 0, 16, 0 },
355 { "fixup_MICROMIPS_GOT16", 0, 16, 0 },
Jozef Kolek9761e962015-01-12 12:03:34 +0000356 { "fixup_MICROMIPS_PC7_S1", 0, 7, MCFixupKindInfo::FKF_IsPCRel },
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000357 { "fixup_MICROMIPS_PC10_S1", 0, 10, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000358 { "fixup_MICROMIPS_PC16_S1", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovic02b70032016-04-21 13:43:26 +0000359 { "fixup_MICROMIPS_PC26_S1", 0, 26, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovic6764fa72016-04-21 14:09:35 +0000360 { "fixup_MICROMIPS_PC19_S2", 0, 19, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovic8e366822016-04-22 10:15:12 +0000361 { "fixup_MICROMIPS_PC18_S3", 0, 18, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovic5f94ced2016-05-19 12:20:40 +0000362 { "fixup_MICROMIPS_PC21_S1", 0, 21, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000363 { "fixup_MICROMIPS_CALL16", 0, 16, 0 },
364 { "fixup_MICROMIPS_GOT_DISP", 0, 16, 0 },
365 { "fixup_MICROMIPS_GOT_PAGE", 0, 16, 0 },
366 { "fixup_MICROMIPS_GOT_OFST", 0, 16, 0 },
367 { "fixup_MICROMIPS_TLS_GD", 0, 16, 0 },
368 { "fixup_MICROMIPS_TLS_LDM", 0, 16, 0 },
369 { "fixup_MICROMIPS_TLS_DTPREL_HI16", 0, 16, 0 },
370 { "fixup_MICROMIPS_TLS_DTPREL_LO16", 0, 16, 0 },
Simon Atanasyan3979f432017-04-30 04:27:23 +0000371 { "fixup_MICROMIPS_GOTTPREL", 0, 16, 0 },
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000372 { "fixup_MICROMIPS_TLS_TPREL_HI16", 0, 16, 0 },
Daniel Sanders3feeb9c2016-08-08 11:50:25 +0000373 { "fixup_MICROMIPS_TLS_TPREL_LO16", 0, 16, 0 },
374 { "fixup_Mips_SUB", 0, 64, 0 },
375 { "fixup_MICROMIPS_SUB", 0, 64, 0 }
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000376 };
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000377
Daniel Sanders683ed962014-05-23 13:35:24 +0000378 const static MCFixupKindInfo BigEndianInfos[Mips::NumTargetFixupKinds] = {
379 // This table *must* be in same the order of fixup_* kinds in
380 // MipsFixupKinds.h.
381 //
382 // name offset bits flags
Daniel Sanders9f6ad492015-11-12 13:33:00 +0000383 { "fixup_Mips_NONE", 0, 0, 0 },
Daniel Sanders683ed962014-05-23 13:35:24 +0000384 { "fixup_Mips_16", 16, 16, 0 },
385 { "fixup_Mips_32", 0, 32, 0 },
386 { "fixup_Mips_REL32", 0, 32, 0 },
387 { "fixup_Mips_26", 6, 26, 0 },
388 { "fixup_Mips_HI16", 16, 16, 0 },
389 { "fixup_Mips_LO16", 16, 16, 0 },
390 { "fixup_Mips_GPREL16", 16, 16, 0 },
391 { "fixup_Mips_LITERAL", 16, 16, 0 },
Daniel Sandersfe98b2f2016-05-03 13:35:44 +0000392 { "fixup_Mips_GOT", 16, 16, 0 },
Daniel Sanders683ed962014-05-23 13:35:24 +0000393 { "fixup_Mips_PC16", 16, 16, MCFixupKindInfo::FKF_IsPCRel },
394 { "fixup_Mips_CALL16", 16, 16, 0 },
395 { "fixup_Mips_GPREL32", 0, 32, 0 },
396 { "fixup_Mips_SHIFT5", 21, 5, 0 },
397 { "fixup_Mips_SHIFT6", 21, 5, 0 },
398 { "fixup_Mips_64", 0, 64, 0 },
399 { "fixup_Mips_TLSGD", 16, 16, 0 },
400 { "fixup_Mips_GOTTPREL", 16, 16, 0 },
401 { "fixup_Mips_TPREL_HI", 16, 16, 0 },
402 { "fixup_Mips_TPREL_LO", 16, 16, 0 },
403 { "fixup_Mips_TLSLDM", 16, 16, 0 },
404 { "fixup_Mips_DTPREL_HI", 16, 16, 0 },
405 { "fixup_Mips_DTPREL_LO", 16, 16, 0 },
406 { "fixup_Mips_Branch_PCRel",16, 16, MCFixupKindInfo::FKF_IsPCRel },
407 { "fixup_Mips_GPOFF_HI", 16, 16, 0 },
408 { "fixup_Mips_GPOFF_LO", 16, 16, 0 },
409 { "fixup_Mips_GOT_PAGE", 16, 16, 0 },
410 { "fixup_Mips_GOT_OFST", 16, 16, 0 },
411 { "fixup_Mips_GOT_DISP", 16, 16, 0 },
412 { "fixup_Mips_HIGHER", 16, 16, 0 },
413 { "fixup_Mips_HIGHEST", 16, 16, 0 },
414 { "fixup_Mips_GOT_HI16", 16, 16, 0 },
415 { "fixup_Mips_GOT_LO16", 16, 16, 0 },
416 { "fixup_Mips_CALL_HI16", 16, 16, 0 },
417 { "fixup_Mips_CALL_LO16", 16, 16, 0 },
Zoran Jovanovica5acdcf2014-06-13 14:26:47 +0000418 { "fixup_Mips_PC18_S3", 14, 18, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +0000419 { "fixup_MIPS_PC19_S2", 13, 19, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000420 { "fixup_MIPS_PC21_S2", 11, 21, MCFixupKindInfo::FKF_IsPCRel },
421 { "fixup_MIPS_PC26_S2", 6, 26, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovicb355e8f2014-05-27 14:58:51 +0000422 { "fixup_MIPS_PCHI16", 16, 16, MCFixupKindInfo::FKF_IsPCRel },
423 { "fixup_MIPS_PCLO16", 16, 16, MCFixupKindInfo::FKF_IsPCRel },
Daniel Sanders683ed962014-05-23 13:35:24 +0000424 { "fixup_MICROMIPS_26_S1", 6, 26, 0 },
425 { "fixup_MICROMIPS_HI16", 16, 16, 0 },
426 { "fixup_MICROMIPS_LO16", 16, 16, 0 },
427 { "fixup_MICROMIPS_GOT16", 16, 16, 0 },
Jozef Kolek9761e962015-01-12 12:03:34 +0000428 { "fixup_MICROMIPS_PC7_S1", 9, 7, MCFixupKindInfo::FKF_IsPCRel },
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000429 { "fixup_MICROMIPS_PC10_S1", 6, 10, MCFixupKindInfo::FKF_IsPCRel },
Daniel Sanders683ed962014-05-23 13:35:24 +0000430 { "fixup_MICROMIPS_PC16_S1",16, 16, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovic02b70032016-04-21 13:43:26 +0000431 { "fixup_MICROMIPS_PC26_S1", 6, 26, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovic6764fa72016-04-21 14:09:35 +0000432 { "fixup_MICROMIPS_PC19_S2",13, 19, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovic8e366822016-04-22 10:15:12 +0000433 { "fixup_MICROMIPS_PC18_S3",14, 18, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovic5f94ced2016-05-19 12:20:40 +0000434 { "fixup_MICROMIPS_PC21_S1",11, 21, MCFixupKindInfo::FKF_IsPCRel },
Daniel Sanders683ed962014-05-23 13:35:24 +0000435 { "fixup_MICROMIPS_CALL16", 16, 16, 0 },
436 { "fixup_MICROMIPS_GOT_DISP", 16, 16, 0 },
437 { "fixup_MICROMIPS_GOT_PAGE", 16, 16, 0 },
438 { "fixup_MICROMIPS_GOT_OFST", 16, 16, 0 },
439 { "fixup_MICROMIPS_TLS_GD", 16, 16, 0 },
440 { "fixup_MICROMIPS_TLS_LDM", 16, 16, 0 },
441 { "fixup_MICROMIPS_TLS_DTPREL_HI16", 16, 16, 0 },
442 { "fixup_MICROMIPS_TLS_DTPREL_LO16", 16, 16, 0 },
Simon Atanasyan3979f432017-04-30 04:27:23 +0000443 { "fixup_MICROMIPS_GOTTPREL", 16, 16, 0 },
Daniel Sanders683ed962014-05-23 13:35:24 +0000444 { "fixup_MICROMIPS_TLS_TPREL_HI16", 16, 16, 0 },
Daniel Sanders3feeb9c2016-08-08 11:50:25 +0000445 { "fixup_MICROMIPS_TLS_TPREL_LO16", 16, 16, 0 },
446 { "fixup_Mips_SUB", 0, 64, 0 },
447 { "fixup_MICROMIPS_SUB", 0, 64, 0 }
Daniel Sanders683ed962014-05-23 13:35:24 +0000448 };
449
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000450 if (Kind < FirstTargetFixupKind)
451 return MCAsmBackend::getFixupKindInfo(Kind);
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000452
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000453 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
454 "Invalid kind!");
Daniel Sanders683ed962014-05-23 13:35:24 +0000455
456 if (IsLittle)
457 return LittleEndianInfos[Kind - FirstTargetFixupKind];
458 return BigEndianInfos[Kind - FirstTargetFixupKind];
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000459}
Jim Grosbach25b63fa2011-12-06 00:47:03 +0000460
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000461/// WriteNopData - Write an (optimal) nop sequence of Count bytes
462/// to the given output. If the target cannot generate such a sequence,
463/// it should return an error.
464///
465/// \return - True on success.
466bool MipsAsmBackend::writeNopData(uint64_t Count, MCObjectWriter *OW) const {
467 // Check for a less than instruction size number of bytes
468 // FIXME: 16 bit instructions are not handled yet here.
469 // We shouldn't be using a hard coded number for instruction size.
Joerg Sonnenbergerf148a6d2014-10-02 13:41:42 +0000470
471 // If the count is not 4-byte aligned, we must be writing data into the text
472 // section (otherwise we have unaligned instructions, and thus have far
473 // bigger problems), so just write zeros instead.
Benjamin Kramer97fbdd52015-04-17 11:12:43 +0000474 OW->WriteZeros(Count);
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000475 return true;
476}
Simon Atanasyan117665582017-09-21 10:44:26 +0000477
478MCAsmBackend *llvm::createMipsAsmBackend(const Target &T,
Alex Bradburyb22f7512018-01-03 08:53:05 +0000479 const MCSubtargetInfo &STI,
Simon Atanasyan117665582017-09-21 10:44:26 +0000480 const MCRegisterInfo &MRI,
Simon Atanasyan117665582017-09-21 10:44:26 +0000481 const MCTargetOptions &Options) {
Alex Bradburyb22f7512018-01-03 08:53:05 +0000482 return new MipsAsmBackend(T, MRI, STI.getTargetTriple(), STI.getCPU(),
483 Options.ABIName == "n32");
Simon Atanasyan117665582017-09-21 10:44:26 +0000484}