blob: 3ee0cd9c7ec60790058fcc1dd27b20b08a7cb91c [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;
62 case FK_GPRel_4:
63 case FK_Data_4:
64 case FK_Data_8:
Daniel Sanders2ab623b2016-08-08 09:20:52 +000065 case Mips::fixup_Mips_SUB:
66 case Mips::fixup_MICROMIPS_SUB:
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000067 break;
68 case Mips::fixup_Mips_PC16:
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000069 // The displacement is then divided by 4 to give us an 18 bit
Matheus Almeidae0d75aa2013-12-13 11:11:02 +000070 // address range. Forcing a signed division because Value can be negative.
71 Value = (int64_t)Value / 4;
72 // We now check if Value can be encoded as a 16-bit signed immediate.
Oliver Stannard9be59af2015-11-17 10:00:43 +000073 if (!isInt<16>(Value) && Ctx) {
74 Ctx->reportError(Fixup.getLoc(), "out of range PC16 fixup");
75 return 0;
76 }
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000077 break;
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +000078 case Mips::fixup_MIPS_PC19_S2:
Zoran Jovanovic6764fa72016-04-21 14:09:35 +000079 case Mips::fixup_MICROMIPS_PC19_S2:
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +000080 // Forcing a signed division because Value can be negative.
81 Value = (int64_t)Value / 4;
82 // We now check if Value can be encoded as a 19-bit signed immediate.
Oliver Stannard9be59af2015-11-17 10:00:43 +000083 if (!isInt<19>(Value) && Ctx) {
84 Ctx->reportError(Fixup.getLoc(), "out of range PC19 fixup");
85 return 0;
86 }
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +000087 break;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000088 case Mips::fixup_Mips_26:
89 // So far we are only using this type for jumps.
90 // The displacement is then divided by 4 to give us an 28 bit
91 // address range.
92 Value >>= 2;
93 break;
Akira Hatanakaf5ddf132011-11-23 22:18:04 +000094 case Mips::fixup_Mips_HI16:
Daniel Sandersfe98b2f2016-05-03 13:35:44 +000095 case Mips::fixup_Mips_GOT:
Daniel Sandersa2bde882016-05-16 09:33:59 +000096 case Mips::fixup_MICROMIPS_GOT16:
Jack Carterb05cb672012-11-21 23:38:59 +000097 case Mips::fixup_Mips_GOT_HI16:
98 case Mips::fixup_Mips_CALL_HI16:
Zoran Jovanovice7ae8af2013-10-23 16:14:44 +000099 case Mips::fixup_MICROMIPS_HI16:
Zoran Jovanovicb355e8f2014-05-27 14:58:51 +0000100 case Mips::fixup_MIPS_PCHI16:
Jack Carter84491ab2012-08-06 21:26:03 +0000101 // Get the 2nd 16-bits. Also add 1 if bit 15 is 1.
Akira Hatanakada728192012-03-27 01:50:08 +0000102 Value = ((Value + 0x8000) >> 16) & 0xffff;
Akira Hatanakaf5ddf132011-11-23 22:18:04 +0000103 break;
Jack Carter84491ab2012-08-06 21:26:03 +0000104 case Mips::fixup_Mips_HIGHER:
105 // Get the 3rd 16-bits.
106 Value = ((Value + 0x80008000LL) >> 32) & 0xffff;
107 break;
108 case Mips::fixup_Mips_HIGHEST:
109 // Get the 4th 16-bits.
110 Value = ((Value + 0x800080008000LL) >> 48) & 0xffff;
111 break;
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000112 case Mips::fixup_MICROMIPS_26_S1:
113 Value >>= 1;
114 break;
Jozef Kolek9761e962015-01-12 12:03:34 +0000115 case Mips::fixup_MICROMIPS_PC7_S1:
116 Value -= 4;
117 // Forcing a signed division because Value can be negative.
118 Value = (int64_t) Value / 2;
119 // We now check if Value can be encoded as a 7-bit signed immediate.
Oliver Stannard9be59af2015-11-17 10:00:43 +0000120 if (!isInt<7>(Value) && Ctx) {
121 Ctx->reportError(Fixup.getLoc(), "out of range PC7 fixup");
122 return 0;
123 }
Jozef Kolek9761e962015-01-12 12:03:34 +0000124 break;
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000125 case Mips::fixup_MICROMIPS_PC10_S1:
126 Value -= 2;
127 // Forcing a signed division because Value can be negative.
128 Value = (int64_t) Value / 2;
129 // We now check if Value can be encoded as a 10-bit signed immediate.
Oliver Stannard9be59af2015-11-17 10:00:43 +0000130 if (!isInt<10>(Value) && Ctx) {
131 Ctx->reportError(Fixup.getLoc(), "out of range PC10 fixup");
132 return 0;
133 }
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000134 break;
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000135 case Mips::fixup_MICROMIPS_PC16_S1:
136 Value -= 4;
Matheus Almeidae0d75aa2013-12-13 11:11:02 +0000137 // Forcing a signed division because Value can be negative.
138 Value = (int64_t)Value / 2;
139 // We now check if Value can be encoded as a 16-bit signed immediate.
Oliver Stannard9be59af2015-11-17 10:00:43 +0000140 if (!isInt<16>(Value) && Ctx) {
141 Ctx->reportError(Fixup.getLoc(), "out of range PC16 fixup");
142 return 0;
143 }
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000144 break;
Zoran Jovanovica5acdcf2014-06-13 14:26:47 +0000145 case Mips::fixup_MIPS_PC18_S3:
146 // Forcing a signed division because Value can be negative.
147 Value = (int64_t)Value / 8;
148 // We now check if Value can be encoded as a 18-bit signed immediate.
Oliver Stannard9be59af2015-11-17 10:00:43 +0000149 if (!isInt<18>(Value) && Ctx) {
150 Ctx->reportError(Fixup.getLoc(), "out of range PC18 fixup");
151 return 0;
152 }
Zoran Jovanovica5acdcf2014-06-13 14:26:47 +0000153 break;
Zoran Jovanovic8e366822016-04-22 10:15:12 +0000154 case Mips::fixup_MICROMIPS_PC18_S3:
155 // Check alignment.
156 if ((Value & 7) && Ctx) {
157 Ctx->reportError(Fixup.getLoc(), "out of range PC18 fixup");
158 }
159 // Forcing a signed division because Value can be negative.
160 Value = (int64_t)Value / 8;
161 // We now check if Value can be encoded as a 18-bit signed immediate.
162 if (!isInt<18>(Value) && Ctx) {
163 Ctx->reportError(Fixup.getLoc(), "out of range PC18 fixup");
164 return 0;
165 }
166 break;
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000167 case Mips::fixup_MIPS_PC21_S2:
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000168 // Forcing a signed division because Value can be negative.
169 Value = (int64_t) Value / 4;
170 // We now check if Value can be encoded as a 21-bit signed immediate.
Oliver Stannard9be59af2015-11-17 10:00:43 +0000171 if (!isInt<21>(Value) && Ctx) {
172 Ctx->reportError(Fixup.getLoc(), "out of range PC21 fixup");
173 return 0;
174 }
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000175 break;
176 case Mips::fixup_MIPS_PC26_S2:
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000177 // Forcing a signed division because Value can be negative.
178 Value = (int64_t) Value / 4;
179 // We now check if Value can be encoded as a 26-bit signed immediate.
Oliver Stannard9be59af2015-11-17 10:00:43 +0000180 if (!isInt<26>(Value) && Ctx) {
181 Ctx->reportError(Fixup.getLoc(), "out of range PC26 fixup");
182 return 0;
183 }
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000184 break;
Zoran Jovanovic02b70032016-04-21 13:43:26 +0000185 case Mips::fixup_MICROMIPS_PC26_S1:
186 // Forcing a signed division because Value can be negative.
187 Value = (int64_t)Value / 2;
188 // We now check if Value can be encoded as a 26-bit signed immediate.
189 if (!isInt<26>(Value) && Ctx) {
190 Ctx->reportFatalError(Fixup.getLoc(), "out of range PC26 fixup");
191 return 0;
192 }
193 break;
Zoran Jovanovic5f94ced2016-05-19 12:20:40 +0000194 case Mips::fixup_MICROMIPS_PC21_S1:
195 // Forcing a signed division because Value can be negative.
196 Value = (int64_t)Value / 2;
197 // We now check if Value can be encoded as a 21-bit signed immediate.
198 if (!isInt<21>(Value) && Ctx) {
199 Ctx->reportError(Fixup.getLoc(), "out of range PC21 fixup");
200 return 0;
201 }
202 break;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000203 }
204
205 return Value;
206}
207
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000208MCObjectWriter *
209MipsAsmBackend::createObjectWriter(raw_pwrite_stream &OS) const {
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000210 return createMipsELFObjectWriter(OS,
211 MCELFObjectTargetWriter::getOSABI(OSType), IsLittle, Is64Bit);
212}
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000213
Zoran Jovanovic842f20e2014-04-03 12:01:01 +0000214// Little-endian fixup data byte ordering:
215// mips32r2: a | b | x | x
216// microMIPS: x | x | a | b
217
218static bool needsMMLEByteOrder(unsigned Kind) {
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000219 return Kind != Mips::fixup_MICROMIPS_PC10_S1 &&
220 Kind >= Mips::fixup_MICROMIPS_26_S1 &&
Zoran Jovanovic842f20e2014-04-03 12:01:01 +0000221 Kind < Mips::LastTargetFixupKind;
222}
223
224// Calculate index for microMIPS specific little endian byte order
225static unsigned calculateMMLEIndex(unsigned i) {
226 assert(i <= 3 && "Index out of range!");
227
228 return (1 - i / 2) * 2 + i % 2;
229}
230
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000231/// ApplyFixup - Apply the \p Value for given \p Fixup into the provided
232/// data fragment, at the offset specified by the fixup and following the
233/// fixup kind as appropriate.
234void MipsAsmBackend::applyFixup(const MCFixup &Fixup, char *Data,
Rafael Espindola5904e122014-03-29 06:26:49 +0000235 unsigned DataSize, uint64_t Value,
236 bool IsPCRel) const {
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000237 MCFixupKind Kind = Fixup.getKind();
238 Value = adjustFixupValue(Fixup, Value);
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000239
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000240 if (!Value)
241 return; // Doesn't change encoding.
242
243 // Where do we start in the object
244 unsigned Offset = Fixup.getOffset();
245 // Number of bytes we need to fixup
246 unsigned NumBytes = (getFixupKindInfo(Kind).TargetSize + 7) / 8;
247 // Used to point to big endian bytes
248 unsigned FullSize;
249
250 switch ((unsigned)Kind) {
251 case FK_Data_2:
252 case Mips::fixup_Mips_16:
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000253 case Mips::fixup_MICROMIPS_PC10_S1:
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000254 FullSize = 2;
255 break;
256 case FK_Data_8:
257 case Mips::fixup_Mips_64:
258 FullSize = 8;
259 break;
260 case FK_Data_4:
261 default:
262 FullSize = 4;
263 break;
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000264 }
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000265
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000266 // Grab current value, if any, from bits.
267 uint64_t CurVal = 0;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000268
Zoran Jovanovic842f20e2014-04-03 12:01:01 +0000269 bool microMipsLEByteOrder = needsMMLEByteOrder((unsigned) Kind);
270
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000271 for (unsigned i = 0; i != NumBytes; ++i) {
Zoran Jovanovic842f20e2014-04-03 12:01:01 +0000272 unsigned Idx = IsLittle ? (microMipsLEByteOrder ? calculateMMLEIndex(i)
273 : i)
274 : (FullSize - 1 - i);
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000275 CurVal |= (uint64_t)((uint8_t)Data[Offset + Idx]) << (i*8);
Akira Hatanaka0137dfe2012-03-21 00:52:01 +0000276 }
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000277
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000278 uint64_t Mask = ((uint64_t)(-1) >>
279 (64 - getFixupKindInfo(Kind).TargetSize));
280 CurVal |= Value & Mask;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000281
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000282 // Write out the fixed up bytes back to the code/data bits.
283 for (unsigned i = 0; i != NumBytes; ++i) {
Zoran Jovanovic842f20e2014-04-03 12:01:01 +0000284 unsigned Idx = IsLittle ? (microMipsLEByteOrder ? calculateMMLEIndex(i)
285 : i)
286 : (FullSize - 1 - i);
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000287 Data[Offset + Idx] = (uint8_t)((CurVal >> (i*8)) & 0xff);
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000288 }
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000289}
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000290
David Majnemerce108422016-01-19 23:05:27 +0000291Optional<MCFixupKind> MipsAsmBackend::getFixupKind(StringRef Name) const {
292 return StringSwitch<Optional<MCFixupKind>>(Name)
293 .Case("R_MIPS_NONE", (MCFixupKind)Mips::fixup_Mips_NONE)
294 .Case("R_MIPS_32", FK_Data_4)
295 .Default(MCAsmBackend::getFixupKind(Name));
Daniel Sanders9f6ad492015-11-12 13:33:00 +0000296}
297
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000298const MCFixupKindInfo &MipsAsmBackend::
299getFixupKindInfo(MCFixupKind Kind) const {
Daniel Sanders683ed962014-05-23 13:35:24 +0000300 const static MCFixupKindInfo LittleEndianInfos[Mips::NumTargetFixupKinds] = {
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000301 // This table *must* be in same the order of fixup_* kinds in
302 // MipsFixupKinds.h.
303 //
304 // name offset bits flags
Daniel Sanders9f6ad492015-11-12 13:33:00 +0000305 { "fixup_Mips_NONE", 0, 0, 0 },
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000306 { "fixup_Mips_16", 0, 16, 0 },
307 { "fixup_Mips_32", 0, 32, 0 },
308 { "fixup_Mips_REL32", 0, 32, 0 },
309 { "fixup_Mips_26", 0, 26, 0 },
310 { "fixup_Mips_HI16", 0, 16, 0 },
311 { "fixup_Mips_LO16", 0, 16, 0 },
312 { "fixup_Mips_GPREL16", 0, 16, 0 },
313 { "fixup_Mips_LITERAL", 0, 16, 0 },
Daniel Sandersfe98b2f2016-05-03 13:35:44 +0000314 { "fixup_Mips_GOT", 0, 16, 0 },
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000315 { "fixup_Mips_PC16", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
316 { "fixup_Mips_CALL16", 0, 16, 0 },
317 { "fixup_Mips_GPREL32", 0, 32, 0 },
318 { "fixup_Mips_SHIFT5", 6, 5, 0 },
319 { "fixup_Mips_SHIFT6", 6, 5, 0 },
320 { "fixup_Mips_64", 0, 64, 0 },
321 { "fixup_Mips_TLSGD", 0, 16, 0 },
322 { "fixup_Mips_GOTTPREL", 0, 16, 0 },
323 { "fixup_Mips_TPREL_HI", 0, 16, 0 },
324 { "fixup_Mips_TPREL_LO", 0, 16, 0 },
325 { "fixup_Mips_TLSLDM", 0, 16, 0 },
326 { "fixup_Mips_DTPREL_HI", 0, 16, 0 },
327 { "fixup_Mips_DTPREL_LO", 0, 16, 0 },
328 { "fixup_Mips_Branch_PCRel", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
329 { "fixup_Mips_GPOFF_HI", 0, 16, 0 },
330 { "fixup_Mips_GPOFF_LO", 0, 16, 0 },
331 { "fixup_Mips_GOT_PAGE", 0, 16, 0 },
332 { "fixup_Mips_GOT_OFST", 0, 16, 0 },
333 { "fixup_Mips_GOT_DISP", 0, 16, 0 },
334 { "fixup_Mips_HIGHER", 0, 16, 0 },
335 { "fixup_Mips_HIGHEST", 0, 16, 0 },
336 { "fixup_Mips_GOT_HI16", 0, 16, 0 },
337 { "fixup_Mips_GOT_LO16", 0, 16, 0 },
338 { "fixup_Mips_CALL_HI16", 0, 16, 0 },
339 { "fixup_Mips_CALL_LO16", 0, 16, 0 },
Zoran Jovanovica5acdcf2014-06-13 14:26:47 +0000340 { "fixup_Mips_PC18_S3", 0, 18, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +0000341 { "fixup_MIPS_PC19_S2", 0, 19, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000342 { "fixup_MIPS_PC21_S2", 0, 21, MCFixupKindInfo::FKF_IsPCRel },
343 { "fixup_MIPS_PC26_S2", 0, 26, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovicb355e8f2014-05-27 14:58:51 +0000344 { "fixup_MIPS_PCHI16", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
345 { "fixup_MIPS_PCLO16", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000346 { "fixup_MICROMIPS_26_S1", 0, 26, 0 },
347 { "fixup_MICROMIPS_HI16", 0, 16, 0 },
348 { "fixup_MICROMIPS_LO16", 0, 16, 0 },
349 { "fixup_MICROMIPS_GOT16", 0, 16, 0 },
Jozef Kolek9761e962015-01-12 12:03:34 +0000350 { "fixup_MICROMIPS_PC7_S1", 0, 7, MCFixupKindInfo::FKF_IsPCRel },
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000351 { "fixup_MICROMIPS_PC10_S1", 0, 10, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000352 { "fixup_MICROMIPS_PC16_S1", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovic02b70032016-04-21 13:43:26 +0000353 { "fixup_MICROMIPS_PC26_S1", 0, 26, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovic6764fa72016-04-21 14:09:35 +0000354 { "fixup_MICROMIPS_PC19_S2", 0, 19, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovic8e366822016-04-22 10:15:12 +0000355 { "fixup_MICROMIPS_PC18_S3", 0, 18, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovic5f94ced2016-05-19 12:20:40 +0000356 { "fixup_MICROMIPS_PC21_S1", 0, 21, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000357 { "fixup_MICROMIPS_CALL16", 0, 16, 0 },
358 { "fixup_MICROMIPS_GOT_DISP", 0, 16, 0 },
359 { "fixup_MICROMIPS_GOT_PAGE", 0, 16, 0 },
360 { "fixup_MICROMIPS_GOT_OFST", 0, 16, 0 },
361 { "fixup_MICROMIPS_TLS_GD", 0, 16, 0 },
362 { "fixup_MICROMIPS_TLS_LDM", 0, 16, 0 },
363 { "fixup_MICROMIPS_TLS_DTPREL_HI16", 0, 16, 0 },
364 { "fixup_MICROMIPS_TLS_DTPREL_LO16", 0, 16, 0 },
365 { "fixup_MICROMIPS_TLS_TPREL_HI16", 0, 16, 0 },
Daniel Sanders2ab623b2016-08-08 09:20:52 +0000366 { "fixup_MICROMIPS_TLS_TPREL_LO16", 0, 16, 0 },
367 { "fixup_Mips_SUB", 0, 64, 0 },
368 { "fixup_MICROMIPS_SUB", 0, 64, 0 }
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000369 };
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000370
Daniel Sanders683ed962014-05-23 13:35:24 +0000371 const static MCFixupKindInfo BigEndianInfos[Mips::NumTargetFixupKinds] = {
372 // This table *must* be in same the order of fixup_* kinds in
373 // MipsFixupKinds.h.
374 //
375 // name offset bits flags
Daniel Sanders9f6ad492015-11-12 13:33:00 +0000376 { "fixup_Mips_NONE", 0, 0, 0 },
Daniel Sanders683ed962014-05-23 13:35:24 +0000377 { "fixup_Mips_16", 16, 16, 0 },
378 { "fixup_Mips_32", 0, 32, 0 },
379 { "fixup_Mips_REL32", 0, 32, 0 },
380 { "fixup_Mips_26", 6, 26, 0 },
381 { "fixup_Mips_HI16", 16, 16, 0 },
382 { "fixup_Mips_LO16", 16, 16, 0 },
383 { "fixup_Mips_GPREL16", 16, 16, 0 },
384 { "fixup_Mips_LITERAL", 16, 16, 0 },
Daniel Sandersfe98b2f2016-05-03 13:35:44 +0000385 { "fixup_Mips_GOT", 16, 16, 0 },
Daniel Sanders683ed962014-05-23 13:35:24 +0000386 { "fixup_Mips_PC16", 16, 16, MCFixupKindInfo::FKF_IsPCRel },
387 { "fixup_Mips_CALL16", 16, 16, 0 },
388 { "fixup_Mips_GPREL32", 0, 32, 0 },
389 { "fixup_Mips_SHIFT5", 21, 5, 0 },
390 { "fixup_Mips_SHIFT6", 21, 5, 0 },
391 { "fixup_Mips_64", 0, 64, 0 },
392 { "fixup_Mips_TLSGD", 16, 16, 0 },
393 { "fixup_Mips_GOTTPREL", 16, 16, 0 },
394 { "fixup_Mips_TPREL_HI", 16, 16, 0 },
395 { "fixup_Mips_TPREL_LO", 16, 16, 0 },
396 { "fixup_Mips_TLSLDM", 16, 16, 0 },
397 { "fixup_Mips_DTPREL_HI", 16, 16, 0 },
398 { "fixup_Mips_DTPREL_LO", 16, 16, 0 },
399 { "fixup_Mips_Branch_PCRel",16, 16, MCFixupKindInfo::FKF_IsPCRel },
400 { "fixup_Mips_GPOFF_HI", 16, 16, 0 },
401 { "fixup_Mips_GPOFF_LO", 16, 16, 0 },
402 { "fixup_Mips_GOT_PAGE", 16, 16, 0 },
403 { "fixup_Mips_GOT_OFST", 16, 16, 0 },
404 { "fixup_Mips_GOT_DISP", 16, 16, 0 },
405 { "fixup_Mips_HIGHER", 16, 16, 0 },
406 { "fixup_Mips_HIGHEST", 16, 16, 0 },
407 { "fixup_Mips_GOT_HI16", 16, 16, 0 },
408 { "fixup_Mips_GOT_LO16", 16, 16, 0 },
409 { "fixup_Mips_CALL_HI16", 16, 16, 0 },
410 { "fixup_Mips_CALL_LO16", 16, 16, 0 },
Zoran Jovanovica5acdcf2014-06-13 14:26:47 +0000411 { "fixup_Mips_PC18_S3", 14, 18, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +0000412 { "fixup_MIPS_PC19_S2", 13, 19, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000413 { "fixup_MIPS_PC21_S2", 11, 21, MCFixupKindInfo::FKF_IsPCRel },
414 { "fixup_MIPS_PC26_S2", 6, 26, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovicb355e8f2014-05-27 14:58:51 +0000415 { "fixup_MIPS_PCHI16", 16, 16, MCFixupKindInfo::FKF_IsPCRel },
416 { "fixup_MIPS_PCLO16", 16, 16, MCFixupKindInfo::FKF_IsPCRel },
Daniel Sanders683ed962014-05-23 13:35:24 +0000417 { "fixup_MICROMIPS_26_S1", 6, 26, 0 },
418 { "fixup_MICROMIPS_HI16", 16, 16, 0 },
419 { "fixup_MICROMIPS_LO16", 16, 16, 0 },
420 { "fixup_MICROMIPS_GOT16", 16, 16, 0 },
Jozef Kolek9761e962015-01-12 12:03:34 +0000421 { "fixup_MICROMIPS_PC7_S1", 9, 7, MCFixupKindInfo::FKF_IsPCRel },
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000422 { "fixup_MICROMIPS_PC10_S1", 6, 10, MCFixupKindInfo::FKF_IsPCRel },
Daniel Sanders683ed962014-05-23 13:35:24 +0000423 { "fixup_MICROMIPS_PC16_S1",16, 16, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovic02b70032016-04-21 13:43:26 +0000424 { "fixup_MICROMIPS_PC26_S1", 6, 26, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovic6764fa72016-04-21 14:09:35 +0000425 { "fixup_MICROMIPS_PC19_S2",13, 19, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovic8e366822016-04-22 10:15:12 +0000426 { "fixup_MICROMIPS_PC18_S3",14, 18, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovic5f94ced2016-05-19 12:20:40 +0000427 { "fixup_MICROMIPS_PC21_S1",11, 21, MCFixupKindInfo::FKF_IsPCRel },
Daniel Sanders683ed962014-05-23 13:35:24 +0000428 { "fixup_MICROMIPS_CALL16", 16, 16, 0 },
429 { "fixup_MICROMIPS_GOT_DISP", 16, 16, 0 },
430 { "fixup_MICROMIPS_GOT_PAGE", 16, 16, 0 },
431 { "fixup_MICROMIPS_GOT_OFST", 16, 16, 0 },
432 { "fixup_MICROMIPS_TLS_GD", 16, 16, 0 },
433 { "fixup_MICROMIPS_TLS_LDM", 16, 16, 0 },
434 { "fixup_MICROMIPS_TLS_DTPREL_HI16", 16, 16, 0 },
435 { "fixup_MICROMIPS_TLS_DTPREL_LO16", 16, 16, 0 },
436 { "fixup_MICROMIPS_TLS_TPREL_HI16", 16, 16, 0 },
Daniel Sanders2ab623b2016-08-08 09:20:52 +0000437 { "fixup_MICROMIPS_TLS_TPREL_LO16", 16, 16, 0 },
438 { "fixup_Mips_SUB", 0, 64, 0 },
439 { "fixup_MICROMIPS_SUB", 0, 64, 0 }
Daniel Sanders683ed962014-05-23 13:35:24 +0000440 };
441
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000442 if (Kind < FirstTargetFixupKind)
443 return MCAsmBackend::getFixupKindInfo(Kind);
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000444
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000445 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
446 "Invalid kind!");
Daniel Sanders683ed962014-05-23 13:35:24 +0000447
448 if (IsLittle)
449 return LittleEndianInfos[Kind - FirstTargetFixupKind];
450 return BigEndianInfos[Kind - FirstTargetFixupKind];
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000451}
Jim Grosbach25b63fa2011-12-06 00:47:03 +0000452
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000453/// WriteNopData - Write an (optimal) nop sequence of Count bytes
454/// to the given output. If the target cannot generate such a sequence,
455/// it should return an error.
456///
457/// \return - True on success.
458bool MipsAsmBackend::writeNopData(uint64_t Count, MCObjectWriter *OW) const {
459 // Check for a less than instruction size number of bytes
460 // FIXME: 16 bit instructions are not handled yet here.
461 // We shouldn't be using a hard coded number for instruction size.
Joerg Sonnenbergerf148a6d2014-10-02 13:41:42 +0000462
463 // If the count is not 4-byte aligned, we must be writing data into the text
464 // section (otherwise we have unaligned instructions, and thus have far
465 // bigger problems), so just write zeros instead.
Benjamin Kramer97fbdd52015-04-17 11:12:43 +0000466 OW->WriteZeros(Count);
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000467 return true;
468}
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000469
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000470/// processFixupValue - Target hook to process the literal value of a fixup
471/// if necessary.
472void MipsAsmBackend::processFixupValue(const MCAssembler &Asm,
473 const MCAsmLayout &Layout,
474 const MCFixup &Fixup,
475 const MCFragment *DF,
Rafael Espindola3e3de5e2014-03-28 16:06:09 +0000476 const MCValue &Target,
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000477 uint64_t &Value,
478 bool &IsResolved) {
479 // At this point we'll ignore the value returned by adjustFixupValue as
480 // we are only checking if the fixup can be applied correctly. We have
481 // access to MCContext from here which allows us to report a fatal error
482 // with *possibly* a source code location.
Daniel Sandersfe98b2f2016-05-03 13:35:44 +0000483 // The caller will also ignore any changes we make to Value
484 // (recordRelocation() overwrites it with it's own calculation).
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000485 (void)adjustFixupValue(Fixup, Value, &Asm.getContext());
486}
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000487
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000488// MCAsmBackend
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000489MCAsmBackend *llvm::createMipsAsmBackendEL32(const Target &T,
490 const MCRegisterInfo &MRI,
Joel Jones373d7d32016-07-25 17:18:28 +0000491 const Triple &TT, StringRef CPU,
492 const MCTargetOptions &Options) {
Daniel Sanders418caf52015-06-10 10:35:34 +0000493 return new MipsAsmBackend(T, TT.getOS(), /*IsLittle*/ true,
494 /*Is64Bit*/ false);
Rafael Espindola647841b2012-01-11 04:04:14 +0000495}
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000496
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000497MCAsmBackend *llvm::createMipsAsmBackendEB32(const Target &T,
498 const MCRegisterInfo &MRI,
Joel Jones373d7d32016-07-25 17:18:28 +0000499 const Triple &TT, StringRef CPU,
500 const MCTargetOptions &Options) {
Daniel Sanders418caf52015-06-10 10:35:34 +0000501 return new MipsAsmBackend(T, TT.getOS(), /*IsLittle*/ false,
502 /*Is64Bit*/ false);
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000503}
Akira Hatanakab1f68f92012-04-02 19:25:22 +0000504
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000505MCAsmBackend *llvm::createMipsAsmBackendEL64(const Target &T,
506 const MCRegisterInfo &MRI,
Joel Jones373d7d32016-07-25 17:18:28 +0000507 const Triple &TT, StringRef CPU,
508 const MCTargetOptions &Options) {
Daniel Sanders418caf52015-06-10 10:35:34 +0000509 return new MipsAsmBackend(T, TT.getOS(), /*IsLittle*/ true, /*Is64Bit*/ true);
Akira Hatanakab1f68f92012-04-02 19:25:22 +0000510}
511
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000512MCAsmBackend *llvm::createMipsAsmBackendEB64(const Target &T,
513 const MCRegisterInfo &MRI,
Joel Jones373d7d32016-07-25 17:18:28 +0000514 const Triple &TT, StringRef CPU,
515 const MCTargetOptions &Options) {
Daniel Sanders418caf52015-06-10 10:35:34 +0000516 return new MipsAsmBackend(T, TT.getOS(), /*IsLittle*/ false,
517 /*Is64Bit*/ true);
Akira Hatanakab1f68f92012-04-02 19:25:22 +0000518}