blob: 38b11f78e36d928bb1cf3abe13720b46e0b34035 [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/MipsFixupKinds.h"
16#include "MCTargetDesc/MipsAsmBackend.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"
Daniel Sandersfe98b2f2016-05-03 13:35:44 +000027#include "llvm/MC/MCValue.h"
Akira Hatanaka587fe6c2011-09-30 21:04:02 +000028#include "llvm/Support/ErrorHandling.h"
Daniel Sandersfe98b2f2016-05-03 13:35:44 +000029#include "llvm/Support/Format.h"
Matheus Almeidae0d75aa2013-12-13 11:11:02 +000030#include "llvm/Support/MathExtras.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000031#include "llvm/Support/raw_ostream.h"
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +000032
Akira Hatanaka587fe6c2011-09-30 21:04:02 +000033using namespace llvm;
34
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +000035// Prepare value for the target space for it
Matheus Almeidae0d75aa2013-12-13 11:11:02 +000036static unsigned adjustFixupValue(const MCFixup &Fixup, uint64_t Value,
Craig Topper062a2ba2014-04-25 05:30:21 +000037 MCContext *Ctx = nullptr) {
Matheus Almeidae0d75aa2013-12-13 11:11:02 +000038
39 unsigned Kind = Fixup.getKind();
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000040
41 // Add/subtract and shift
42 switch (Kind) {
43 default:
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +000044 return 0;
Ed Maste2a710d02014-03-03 14:27:49 +000045 case FK_Data_2:
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +000046 case Mips::fixup_Mips_LO16:
Jack Carterc3dd91c2013-01-08 19:01:28 +000047 case Mips::fixup_Mips_GPREL16:
Jack Carterb9f9de92012-06-27 22:48:25 +000048 case Mips::fixup_Mips_GPOFF_HI:
49 case Mips::fixup_Mips_GPOFF_LO:
50 case Mips::fixup_Mips_GOT_PAGE:
51 case Mips::fixup_Mips_GOT_OFST:
Jack Carter5ddcfda2012-07-13 19:15:47 +000052 case Mips::fixup_Mips_GOT_DISP:
Jack Carterb05cb672012-11-21 23:38:59 +000053 case Mips::fixup_Mips_GOT_LO16:
54 case Mips::fixup_Mips_CALL_LO16:
Zoran Jovanovice7ae8af2013-10-23 16:14:44 +000055 case Mips::fixup_MICROMIPS_LO16:
56 case Mips::fixup_MICROMIPS_GOT_PAGE:
57 case Mips::fixup_MICROMIPS_GOT_OFST:
58 case Mips::fixup_MICROMIPS_GOT_DISP:
Zoran Jovanovicb355e8f2014-05-27 14:58:51 +000059 case Mips::fixup_MIPS_PCLO16:
Daniel Sandersfe98b2f2016-05-03 13:35:44 +000060 Value &= 0xffff;
61 break;
Simon Atanasyaneb9ed612016-08-22 16:18:42 +000062 case FK_DTPRel_4:
63 case FK_DTPRel_8:
64 case FK_TPRel_4:
65 case FK_TPRel_8:
Daniel Sandersfe98b2f2016-05-03 13:35:44 +000066 case FK_GPRel_4:
67 case FK_Data_4:
68 case FK_Data_8:
Daniel Sanders3feeb9c2016-08-08 11:50:25 +000069 case Mips::fixup_Mips_SUB:
70 case Mips::fixup_MICROMIPS_SUB:
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000071 break;
72 case Mips::fixup_Mips_PC16:
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000073 // The displacement is then divided by 4 to give us an 18 bit
Matheus Almeidae0d75aa2013-12-13 11:11:02 +000074 // address range. Forcing a signed division because Value can be negative.
75 Value = (int64_t)Value / 4;
76 // We now check if Value can be encoded as a 16-bit signed immediate.
Oliver Stannard9be59af2015-11-17 10:00:43 +000077 if (!isInt<16>(Value) && Ctx) {
78 Ctx->reportError(Fixup.getLoc(), "out of range PC16 fixup");
79 return 0;
80 }
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000081 break;
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +000082 case Mips::fixup_MIPS_PC19_S2:
Zoran Jovanovic6764fa72016-04-21 14:09:35 +000083 case Mips::fixup_MICROMIPS_PC19_S2:
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +000084 // Forcing a signed division because Value can be negative.
85 Value = (int64_t)Value / 4;
86 // We now check if Value can be encoded as a 19-bit signed immediate.
Oliver Stannard9be59af2015-11-17 10:00:43 +000087 if (!isInt<19>(Value) && Ctx) {
88 Ctx->reportError(Fixup.getLoc(), "out of range PC19 fixup");
89 return 0;
90 }
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +000091 break;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000092 case Mips::fixup_Mips_26:
93 // So far we are only using this type for jumps.
94 // The displacement is then divided by 4 to give us an 28 bit
95 // address range.
96 Value >>= 2;
97 break;
Akira Hatanakaf5ddf132011-11-23 22:18:04 +000098 case Mips::fixup_Mips_HI16:
Daniel Sandersfe98b2f2016-05-03 13:35:44 +000099 case Mips::fixup_Mips_GOT:
Daniel Sandersa2bde882016-05-16 09:33:59 +0000100 case Mips::fixup_MICROMIPS_GOT16:
Jack Carterb05cb672012-11-21 23:38:59 +0000101 case Mips::fixup_Mips_GOT_HI16:
102 case Mips::fixup_Mips_CALL_HI16:
Zoran Jovanovice7ae8af2013-10-23 16:14:44 +0000103 case Mips::fixup_MICROMIPS_HI16:
Zoran Jovanovicb355e8f2014-05-27 14:58:51 +0000104 case Mips::fixup_MIPS_PCHI16:
Jack Carter84491ab2012-08-06 21:26:03 +0000105 // Get the 2nd 16-bits. Also add 1 if bit 15 is 1.
Akira Hatanakada728192012-03-27 01:50:08 +0000106 Value = ((Value + 0x8000) >> 16) & 0xffff;
Akira Hatanakaf5ddf132011-11-23 22:18:04 +0000107 break;
Jack Carter84491ab2012-08-06 21:26:03 +0000108 case Mips::fixup_Mips_HIGHER:
109 // Get the 3rd 16-bits.
110 Value = ((Value + 0x80008000LL) >> 32) & 0xffff;
111 break;
112 case Mips::fixup_Mips_HIGHEST:
113 // Get the 4th 16-bits.
114 Value = ((Value + 0x800080008000LL) >> 48) & 0xffff;
115 break;
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000116 case Mips::fixup_MICROMIPS_26_S1:
117 Value >>= 1;
118 break;
Jozef Kolek9761e962015-01-12 12:03:34 +0000119 case Mips::fixup_MICROMIPS_PC7_S1:
120 Value -= 4;
121 // Forcing a signed division because Value can be negative.
122 Value = (int64_t) Value / 2;
123 // We now check if Value can be encoded as a 7-bit signed immediate.
Oliver Stannard9be59af2015-11-17 10:00:43 +0000124 if (!isInt<7>(Value) && Ctx) {
125 Ctx->reportError(Fixup.getLoc(), "out of range PC7 fixup");
126 return 0;
127 }
Jozef Kolek9761e962015-01-12 12:03:34 +0000128 break;
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000129 case Mips::fixup_MICROMIPS_PC10_S1:
130 Value -= 2;
131 // Forcing a signed division because Value can be negative.
132 Value = (int64_t) Value / 2;
133 // We now check if Value can be encoded as a 10-bit signed immediate.
Oliver Stannard9be59af2015-11-17 10:00:43 +0000134 if (!isInt<10>(Value) && Ctx) {
135 Ctx->reportError(Fixup.getLoc(), "out of range PC10 fixup");
136 return 0;
137 }
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000138 break;
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000139 case Mips::fixup_MICROMIPS_PC16_S1:
140 Value -= 4;
Matheus Almeidae0d75aa2013-12-13 11:11:02 +0000141 // Forcing a signed division because Value can be negative.
142 Value = (int64_t)Value / 2;
143 // We now check if Value can be encoded as a 16-bit signed immediate.
Oliver Stannard9be59af2015-11-17 10:00:43 +0000144 if (!isInt<16>(Value) && Ctx) {
145 Ctx->reportError(Fixup.getLoc(), "out of range PC16 fixup");
146 return 0;
147 }
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000148 break;
Zoran Jovanovica5acdcf2014-06-13 14:26:47 +0000149 case Mips::fixup_MIPS_PC18_S3:
150 // Forcing a signed division because Value can be negative.
151 Value = (int64_t)Value / 8;
152 // We now check if Value can be encoded as a 18-bit signed immediate.
Oliver Stannard9be59af2015-11-17 10:00:43 +0000153 if (!isInt<18>(Value) && Ctx) {
154 Ctx->reportError(Fixup.getLoc(), "out of range PC18 fixup");
155 return 0;
156 }
Zoran Jovanovica5acdcf2014-06-13 14:26:47 +0000157 break;
Zoran Jovanovic8e366822016-04-22 10:15:12 +0000158 case Mips::fixup_MICROMIPS_PC18_S3:
159 // Check alignment.
160 if ((Value & 7) && Ctx) {
161 Ctx->reportError(Fixup.getLoc(), "out of range PC18 fixup");
162 }
163 // Forcing a signed division because Value can be negative.
164 Value = (int64_t)Value / 8;
165 // We now check if Value can be encoded as a 18-bit signed immediate.
166 if (!isInt<18>(Value) && Ctx) {
167 Ctx->reportError(Fixup.getLoc(), "out of range PC18 fixup");
168 return 0;
169 }
170 break;
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000171 case Mips::fixup_MIPS_PC21_S2:
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000172 // Forcing a signed division because Value can be negative.
173 Value = (int64_t) Value / 4;
174 // We now check if Value can be encoded as a 21-bit signed immediate.
Oliver Stannard9be59af2015-11-17 10:00:43 +0000175 if (!isInt<21>(Value) && Ctx) {
176 Ctx->reportError(Fixup.getLoc(), "out of range PC21 fixup");
177 return 0;
178 }
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000179 break;
180 case Mips::fixup_MIPS_PC26_S2:
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000181 // Forcing a signed division because Value can be negative.
182 Value = (int64_t) Value / 4;
183 // We now check if Value can be encoded as a 26-bit signed immediate.
Oliver Stannard9be59af2015-11-17 10:00:43 +0000184 if (!isInt<26>(Value) && Ctx) {
185 Ctx->reportError(Fixup.getLoc(), "out of range PC26 fixup");
186 return 0;
187 }
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000188 break;
Zoran Jovanovic02b70032016-04-21 13:43:26 +0000189 case Mips::fixup_MICROMIPS_PC26_S1:
190 // Forcing a signed division because Value can be negative.
191 Value = (int64_t)Value / 2;
192 // We now check if Value can be encoded as a 26-bit signed immediate.
193 if (!isInt<26>(Value) && Ctx) {
194 Ctx->reportFatalError(Fixup.getLoc(), "out of range PC26 fixup");
195 return 0;
196 }
197 break;
Zoran Jovanovic5f94ced2016-05-19 12:20:40 +0000198 case Mips::fixup_MICROMIPS_PC21_S1:
199 // Forcing a signed division because Value can be negative.
200 Value = (int64_t)Value / 2;
201 // We now check if Value can be encoded as a 21-bit signed immediate.
202 if (!isInt<21>(Value) && Ctx) {
203 Ctx->reportError(Fixup.getLoc(), "out of range PC21 fixup");
204 return 0;
205 }
206 break;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000207 }
208
209 return Value;
210}
211
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000212MCObjectWriter *
213MipsAsmBackend::createObjectWriter(raw_pwrite_stream &OS) const {
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000214 return createMipsELFObjectWriter(OS,
215 MCELFObjectTargetWriter::getOSABI(OSType), IsLittle, Is64Bit);
216}
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.
238void MipsAsmBackend::applyFixup(const MCFixup &Fixup, char *Data,
Rafael Espindola5904e122014-03-29 06:26:49 +0000239 unsigned DataSize, uint64_t Value,
240 bool IsPCRel) const {
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000241 MCFixupKind Kind = Fixup.getKind();
242 Value = adjustFixupValue(Fixup, Value);
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000243
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000244 if (!Value)
245 return; // Doesn't change encoding.
246
247 // Where do we start in the object
248 unsigned Offset = Fixup.getOffset();
249 // Number of bytes we need to fixup
250 unsigned NumBytes = (getFixupKindInfo(Kind).TargetSize + 7) / 8;
251 // Used to point to big endian bytes
252 unsigned FullSize;
253
254 switch ((unsigned)Kind) {
255 case FK_Data_2:
256 case Mips::fixup_Mips_16:
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000257 case Mips::fixup_MICROMIPS_PC10_S1:
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000258 FullSize = 2;
259 break;
260 case FK_Data_8:
261 case Mips::fixup_Mips_64:
262 FullSize = 8;
263 break;
264 case FK_Data_4:
265 default:
266 FullSize = 4;
267 break;
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000268 }
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000269
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000270 // Grab current value, if any, from bits.
271 uint64_t CurVal = 0;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000272
Zoran Jovanovic842f20e2014-04-03 12:01:01 +0000273 bool microMipsLEByteOrder = needsMMLEByteOrder((unsigned) Kind);
274
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000275 for (unsigned i = 0; i != NumBytes; ++i) {
Zoran Jovanovic842f20e2014-04-03 12:01:01 +0000276 unsigned Idx = IsLittle ? (microMipsLEByteOrder ? calculateMMLEIndex(i)
277 : i)
278 : (FullSize - 1 - i);
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000279 CurVal |= (uint64_t)((uint8_t)Data[Offset + Idx]) << (i*8);
Akira Hatanaka0137dfe2012-03-21 00:52:01 +0000280 }
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000281
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000282 uint64_t Mask = ((uint64_t)(-1) >>
283 (64 - getFixupKindInfo(Kind).TargetSize));
284 CurVal |= Value & Mask;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000285
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000286 // Write out the fixed up bytes back to the code/data bits.
287 for (unsigned i = 0; i != NumBytes; ++i) {
Zoran Jovanovic842f20e2014-04-03 12:01:01 +0000288 unsigned Idx = IsLittle ? (microMipsLEByteOrder ? calculateMMLEIndex(i)
289 : i)
290 : (FullSize - 1 - i);
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000291 Data[Offset + Idx] = (uint8_t)((CurVal >> (i*8)) & 0xff);
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000292 }
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000293}
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000294
David Majnemerce108422016-01-19 23:05:27 +0000295Optional<MCFixupKind> MipsAsmBackend::getFixupKind(StringRef Name) const {
296 return StringSwitch<Optional<MCFixupKind>>(Name)
297 .Case("R_MIPS_NONE", (MCFixupKind)Mips::fixup_Mips_NONE)
298 .Case("R_MIPS_32", FK_Data_4)
299 .Default(MCAsmBackend::getFixupKind(Name));
Daniel Sanders9f6ad492015-11-12 13:33:00 +0000300}
301
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000302const MCFixupKindInfo &MipsAsmBackend::
303getFixupKindInfo(MCFixupKind Kind) const {
Daniel Sanders683ed962014-05-23 13:35:24 +0000304 const static MCFixupKindInfo LittleEndianInfos[Mips::NumTargetFixupKinds] = {
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000305 // This table *must* be in same the order of fixup_* kinds in
306 // MipsFixupKinds.h.
307 //
308 // name offset bits flags
Daniel Sanders9f6ad492015-11-12 13:33:00 +0000309 { "fixup_Mips_NONE", 0, 0, 0 },
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000310 { "fixup_Mips_16", 0, 16, 0 },
311 { "fixup_Mips_32", 0, 32, 0 },
312 { "fixup_Mips_REL32", 0, 32, 0 },
313 { "fixup_Mips_26", 0, 26, 0 },
314 { "fixup_Mips_HI16", 0, 16, 0 },
315 { "fixup_Mips_LO16", 0, 16, 0 },
316 { "fixup_Mips_GPREL16", 0, 16, 0 },
317 { "fixup_Mips_LITERAL", 0, 16, 0 },
Daniel Sandersfe98b2f2016-05-03 13:35:44 +0000318 { "fixup_Mips_GOT", 0, 16, 0 },
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000319 { "fixup_Mips_PC16", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
320 { "fixup_Mips_CALL16", 0, 16, 0 },
321 { "fixup_Mips_GPREL32", 0, 32, 0 },
322 { "fixup_Mips_SHIFT5", 6, 5, 0 },
323 { "fixup_Mips_SHIFT6", 6, 5, 0 },
324 { "fixup_Mips_64", 0, 64, 0 },
325 { "fixup_Mips_TLSGD", 0, 16, 0 },
326 { "fixup_Mips_GOTTPREL", 0, 16, 0 },
327 { "fixup_Mips_TPREL_HI", 0, 16, 0 },
328 { "fixup_Mips_TPREL_LO", 0, 16, 0 },
329 { "fixup_Mips_TLSLDM", 0, 16, 0 },
330 { "fixup_Mips_DTPREL_HI", 0, 16, 0 },
331 { "fixup_Mips_DTPREL_LO", 0, 16, 0 },
332 { "fixup_Mips_Branch_PCRel", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
333 { "fixup_Mips_GPOFF_HI", 0, 16, 0 },
334 { "fixup_Mips_GPOFF_LO", 0, 16, 0 },
335 { "fixup_Mips_GOT_PAGE", 0, 16, 0 },
336 { "fixup_Mips_GOT_OFST", 0, 16, 0 },
337 { "fixup_Mips_GOT_DISP", 0, 16, 0 },
338 { "fixup_Mips_HIGHER", 0, 16, 0 },
339 { "fixup_Mips_HIGHEST", 0, 16, 0 },
340 { "fixup_Mips_GOT_HI16", 0, 16, 0 },
341 { "fixup_Mips_GOT_LO16", 0, 16, 0 },
342 { "fixup_Mips_CALL_HI16", 0, 16, 0 },
343 { "fixup_Mips_CALL_LO16", 0, 16, 0 },
Zoran Jovanovica5acdcf2014-06-13 14:26:47 +0000344 { "fixup_Mips_PC18_S3", 0, 18, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +0000345 { "fixup_MIPS_PC19_S2", 0, 19, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000346 { "fixup_MIPS_PC21_S2", 0, 21, MCFixupKindInfo::FKF_IsPCRel },
347 { "fixup_MIPS_PC26_S2", 0, 26, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovicb355e8f2014-05-27 14:58:51 +0000348 { "fixup_MIPS_PCHI16", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
349 { "fixup_MIPS_PCLO16", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000350 { "fixup_MICROMIPS_26_S1", 0, 26, 0 },
351 { "fixup_MICROMIPS_HI16", 0, 16, 0 },
352 { "fixup_MICROMIPS_LO16", 0, 16, 0 },
353 { "fixup_MICROMIPS_GOT16", 0, 16, 0 },
Jozef Kolek9761e962015-01-12 12:03:34 +0000354 { "fixup_MICROMIPS_PC7_S1", 0, 7, MCFixupKindInfo::FKF_IsPCRel },
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000355 { "fixup_MICROMIPS_PC10_S1", 0, 10, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000356 { "fixup_MICROMIPS_PC16_S1", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovic02b70032016-04-21 13:43:26 +0000357 { "fixup_MICROMIPS_PC26_S1", 0, 26, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovic6764fa72016-04-21 14:09:35 +0000358 { "fixup_MICROMIPS_PC19_S2", 0, 19, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovic8e366822016-04-22 10:15:12 +0000359 { "fixup_MICROMIPS_PC18_S3", 0, 18, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovic5f94ced2016-05-19 12:20:40 +0000360 { "fixup_MICROMIPS_PC21_S1", 0, 21, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000361 { "fixup_MICROMIPS_CALL16", 0, 16, 0 },
362 { "fixup_MICROMIPS_GOT_DISP", 0, 16, 0 },
363 { "fixup_MICROMIPS_GOT_PAGE", 0, 16, 0 },
364 { "fixup_MICROMIPS_GOT_OFST", 0, 16, 0 },
365 { "fixup_MICROMIPS_TLS_GD", 0, 16, 0 },
366 { "fixup_MICROMIPS_TLS_LDM", 0, 16, 0 },
367 { "fixup_MICROMIPS_TLS_DTPREL_HI16", 0, 16, 0 },
368 { "fixup_MICROMIPS_TLS_DTPREL_LO16", 0, 16, 0 },
369 { "fixup_MICROMIPS_TLS_TPREL_HI16", 0, 16, 0 },
Daniel Sanders3feeb9c2016-08-08 11:50:25 +0000370 { "fixup_MICROMIPS_TLS_TPREL_LO16", 0, 16, 0 },
371 { "fixup_Mips_SUB", 0, 64, 0 },
372 { "fixup_MICROMIPS_SUB", 0, 64, 0 }
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000373 };
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000374
Daniel Sanders683ed962014-05-23 13:35:24 +0000375 const static MCFixupKindInfo BigEndianInfos[Mips::NumTargetFixupKinds] = {
376 // This table *must* be in same the order of fixup_* kinds in
377 // MipsFixupKinds.h.
378 //
379 // name offset bits flags
Daniel Sanders9f6ad492015-11-12 13:33:00 +0000380 { "fixup_Mips_NONE", 0, 0, 0 },
Daniel Sanders683ed962014-05-23 13:35:24 +0000381 { "fixup_Mips_16", 16, 16, 0 },
382 { "fixup_Mips_32", 0, 32, 0 },
383 { "fixup_Mips_REL32", 0, 32, 0 },
384 { "fixup_Mips_26", 6, 26, 0 },
385 { "fixup_Mips_HI16", 16, 16, 0 },
386 { "fixup_Mips_LO16", 16, 16, 0 },
387 { "fixup_Mips_GPREL16", 16, 16, 0 },
388 { "fixup_Mips_LITERAL", 16, 16, 0 },
Daniel Sandersfe98b2f2016-05-03 13:35:44 +0000389 { "fixup_Mips_GOT", 16, 16, 0 },
Daniel Sanders683ed962014-05-23 13:35:24 +0000390 { "fixup_Mips_PC16", 16, 16, MCFixupKindInfo::FKF_IsPCRel },
391 { "fixup_Mips_CALL16", 16, 16, 0 },
392 { "fixup_Mips_GPREL32", 0, 32, 0 },
393 { "fixup_Mips_SHIFT5", 21, 5, 0 },
394 { "fixup_Mips_SHIFT6", 21, 5, 0 },
395 { "fixup_Mips_64", 0, 64, 0 },
396 { "fixup_Mips_TLSGD", 16, 16, 0 },
397 { "fixup_Mips_GOTTPREL", 16, 16, 0 },
398 { "fixup_Mips_TPREL_HI", 16, 16, 0 },
399 { "fixup_Mips_TPREL_LO", 16, 16, 0 },
400 { "fixup_Mips_TLSLDM", 16, 16, 0 },
401 { "fixup_Mips_DTPREL_HI", 16, 16, 0 },
402 { "fixup_Mips_DTPREL_LO", 16, 16, 0 },
403 { "fixup_Mips_Branch_PCRel",16, 16, MCFixupKindInfo::FKF_IsPCRel },
404 { "fixup_Mips_GPOFF_HI", 16, 16, 0 },
405 { "fixup_Mips_GPOFF_LO", 16, 16, 0 },
406 { "fixup_Mips_GOT_PAGE", 16, 16, 0 },
407 { "fixup_Mips_GOT_OFST", 16, 16, 0 },
408 { "fixup_Mips_GOT_DISP", 16, 16, 0 },
409 { "fixup_Mips_HIGHER", 16, 16, 0 },
410 { "fixup_Mips_HIGHEST", 16, 16, 0 },
411 { "fixup_Mips_GOT_HI16", 16, 16, 0 },
412 { "fixup_Mips_GOT_LO16", 16, 16, 0 },
413 { "fixup_Mips_CALL_HI16", 16, 16, 0 },
414 { "fixup_Mips_CALL_LO16", 16, 16, 0 },
Zoran Jovanovica5acdcf2014-06-13 14:26:47 +0000415 { "fixup_Mips_PC18_S3", 14, 18, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +0000416 { "fixup_MIPS_PC19_S2", 13, 19, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000417 { "fixup_MIPS_PC21_S2", 11, 21, MCFixupKindInfo::FKF_IsPCRel },
418 { "fixup_MIPS_PC26_S2", 6, 26, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovicb355e8f2014-05-27 14:58:51 +0000419 { "fixup_MIPS_PCHI16", 16, 16, MCFixupKindInfo::FKF_IsPCRel },
420 { "fixup_MIPS_PCLO16", 16, 16, MCFixupKindInfo::FKF_IsPCRel },
Daniel Sanders683ed962014-05-23 13:35:24 +0000421 { "fixup_MICROMIPS_26_S1", 6, 26, 0 },
422 { "fixup_MICROMIPS_HI16", 16, 16, 0 },
423 { "fixup_MICROMIPS_LO16", 16, 16, 0 },
424 { "fixup_MICROMIPS_GOT16", 16, 16, 0 },
Jozef Kolek9761e962015-01-12 12:03:34 +0000425 { "fixup_MICROMIPS_PC7_S1", 9, 7, MCFixupKindInfo::FKF_IsPCRel },
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000426 { "fixup_MICROMIPS_PC10_S1", 6, 10, MCFixupKindInfo::FKF_IsPCRel },
Daniel Sanders683ed962014-05-23 13:35:24 +0000427 { "fixup_MICROMIPS_PC16_S1",16, 16, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovic02b70032016-04-21 13:43:26 +0000428 { "fixup_MICROMIPS_PC26_S1", 6, 26, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovic6764fa72016-04-21 14:09:35 +0000429 { "fixup_MICROMIPS_PC19_S2",13, 19, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovic8e366822016-04-22 10:15:12 +0000430 { "fixup_MICROMIPS_PC18_S3",14, 18, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovic5f94ced2016-05-19 12:20:40 +0000431 { "fixup_MICROMIPS_PC21_S1",11, 21, MCFixupKindInfo::FKF_IsPCRel },
Daniel Sanders683ed962014-05-23 13:35:24 +0000432 { "fixup_MICROMIPS_CALL16", 16, 16, 0 },
433 { "fixup_MICROMIPS_GOT_DISP", 16, 16, 0 },
434 { "fixup_MICROMIPS_GOT_PAGE", 16, 16, 0 },
435 { "fixup_MICROMIPS_GOT_OFST", 16, 16, 0 },
436 { "fixup_MICROMIPS_TLS_GD", 16, 16, 0 },
437 { "fixup_MICROMIPS_TLS_LDM", 16, 16, 0 },
438 { "fixup_MICROMIPS_TLS_DTPREL_HI16", 16, 16, 0 },
439 { "fixup_MICROMIPS_TLS_DTPREL_LO16", 16, 16, 0 },
440 { "fixup_MICROMIPS_TLS_TPREL_HI16", 16, 16, 0 },
Daniel Sanders3feeb9c2016-08-08 11:50:25 +0000441 { "fixup_MICROMIPS_TLS_TPREL_LO16", 16, 16, 0 },
442 { "fixup_Mips_SUB", 0, 64, 0 },
443 { "fixup_MICROMIPS_SUB", 0, 64, 0 }
Daniel Sanders683ed962014-05-23 13:35:24 +0000444 };
445
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000446 if (Kind < FirstTargetFixupKind)
447 return MCAsmBackend::getFixupKindInfo(Kind);
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000448
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000449 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
450 "Invalid kind!");
Daniel Sanders683ed962014-05-23 13:35:24 +0000451
452 if (IsLittle)
453 return LittleEndianInfos[Kind - FirstTargetFixupKind];
454 return BigEndianInfos[Kind - FirstTargetFixupKind];
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000455}
Jim Grosbach25b63fa2011-12-06 00:47:03 +0000456
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000457/// WriteNopData - Write an (optimal) nop sequence of Count bytes
458/// to the given output. If the target cannot generate such a sequence,
459/// it should return an error.
460///
461/// \return - True on success.
462bool MipsAsmBackend::writeNopData(uint64_t Count, MCObjectWriter *OW) const {
463 // Check for a less than instruction size number of bytes
464 // FIXME: 16 bit instructions are not handled yet here.
465 // We shouldn't be using a hard coded number for instruction size.
Joerg Sonnenbergerf148a6d2014-10-02 13:41:42 +0000466
467 // If the count is not 4-byte aligned, we must be writing data into the text
468 // section (otherwise we have unaligned instructions, and thus have far
469 // bigger problems), so just write zeros instead.
Benjamin Kramer97fbdd52015-04-17 11:12:43 +0000470 OW->WriteZeros(Count);
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000471 return true;
472}
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000473
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000474/// processFixupValue - Target hook to process the literal value of a fixup
475/// if necessary.
476void MipsAsmBackend::processFixupValue(const MCAssembler &Asm,
477 const MCAsmLayout &Layout,
478 const MCFixup &Fixup,
479 const MCFragment *DF,
Rafael Espindola3e3de5e2014-03-28 16:06:09 +0000480 const MCValue &Target,
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000481 uint64_t &Value,
482 bool &IsResolved) {
483 // At this point we'll ignore the value returned by adjustFixupValue as
484 // we are only checking if the fixup can be applied correctly. We have
485 // access to MCContext from here which allows us to report a fatal error
486 // with *possibly* a source code location.
Daniel Sandersfe98b2f2016-05-03 13:35:44 +0000487 // The caller will also ignore any changes we make to Value
488 // (recordRelocation() overwrites it with it's own calculation).
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000489 (void)adjustFixupValue(Fixup, Value, &Asm.getContext());
490}
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000491
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000492// MCAsmBackend
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000493MCAsmBackend *llvm::createMipsAsmBackendEL32(const Target &T,
494 const MCRegisterInfo &MRI,
Joel Jones373d7d32016-07-25 17:18:28 +0000495 const Triple &TT, StringRef CPU,
496 const MCTargetOptions &Options) {
Daniel Sanders418caf52015-06-10 10:35:34 +0000497 return new MipsAsmBackend(T, TT.getOS(), /*IsLittle*/ true,
498 /*Is64Bit*/ false);
Rafael Espindola647841b2012-01-11 04:04:14 +0000499}
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000500
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000501MCAsmBackend *llvm::createMipsAsmBackendEB32(const Target &T,
502 const MCRegisterInfo &MRI,
Joel Jones373d7d32016-07-25 17:18:28 +0000503 const Triple &TT, StringRef CPU,
504 const MCTargetOptions &Options) {
Daniel Sanders418caf52015-06-10 10:35:34 +0000505 return new MipsAsmBackend(T, TT.getOS(), /*IsLittle*/ false,
506 /*Is64Bit*/ false);
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000507}
Akira Hatanakab1f68f92012-04-02 19:25:22 +0000508
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000509MCAsmBackend *llvm::createMipsAsmBackendEL64(const Target &T,
510 const MCRegisterInfo &MRI,
Joel Jones373d7d32016-07-25 17:18:28 +0000511 const Triple &TT, StringRef CPU,
512 const MCTargetOptions &Options) {
Daniel Sanders418caf52015-06-10 10:35:34 +0000513 return new MipsAsmBackend(T, TT.getOS(), /*IsLittle*/ true, /*Is64Bit*/ true);
Akira Hatanakab1f68f92012-04-02 19:25:22 +0000514}
515
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000516MCAsmBackend *llvm::createMipsAsmBackendEB64(const Target &T,
517 const MCRegisterInfo &MRI,
Joel Jones373d7d32016-07-25 17:18:28 +0000518 const Triple &TT, StringRef CPU,
519 const MCTargetOptions &Options) {
Daniel Sanders418caf52015-06-10 10:35:34 +0000520 return new MipsAsmBackend(T, TT.getOS(), /*IsLittle*/ false,
521 /*Is64Bit*/ true);
Akira Hatanakab1f68f92012-04-02 19:25:22 +0000522}