blob: 550c2894695ccbc55a142c5f6095363a2b50037c [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:
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000065 break;
66 case Mips::fixup_Mips_PC16:
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000067 // The displacement is then divided by 4 to give us an 18 bit
Matheus Almeidae0d75aa2013-12-13 11:11:02 +000068 // address range. Forcing a signed division because Value can be negative.
69 Value = (int64_t)Value / 4;
70 // We now check if Value can be encoded as a 16-bit signed immediate.
Oliver Stannard9be59af2015-11-17 10:00:43 +000071 if (!isInt<16>(Value) && Ctx) {
72 Ctx->reportError(Fixup.getLoc(), "out of range PC16 fixup");
73 return 0;
74 }
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000075 break;
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +000076 case Mips::fixup_MIPS_PC19_S2:
Zoran Jovanovic6764fa72016-04-21 14:09:35 +000077 case Mips::fixup_MICROMIPS_PC19_S2:
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +000078 // Forcing a signed division because Value can be negative.
79 Value = (int64_t)Value / 4;
80 // We now check if Value can be encoded as a 19-bit signed immediate.
Oliver Stannard9be59af2015-11-17 10:00:43 +000081 if (!isInt<19>(Value) && Ctx) {
82 Ctx->reportError(Fixup.getLoc(), "out of range PC19 fixup");
83 return 0;
84 }
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +000085 break;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000086 case Mips::fixup_Mips_26:
87 // So far we are only using this type for jumps.
88 // The displacement is then divided by 4 to give us an 28 bit
89 // address range.
90 Value >>= 2;
91 break;
Akira Hatanakaf5ddf132011-11-23 22:18:04 +000092 case Mips::fixup_Mips_HI16:
Daniel Sandersfe98b2f2016-05-03 13:35:44 +000093 case Mips::fixup_Mips_GOT:
Daniel Sandersa2bde882016-05-16 09:33:59 +000094 case Mips::fixup_MICROMIPS_GOT16:
Jack Carterb05cb672012-11-21 23:38:59 +000095 case Mips::fixup_Mips_GOT_HI16:
96 case Mips::fixup_Mips_CALL_HI16:
Zoran Jovanovice7ae8af2013-10-23 16:14:44 +000097 case Mips::fixup_MICROMIPS_HI16:
Zoran Jovanovicb355e8f2014-05-27 14:58:51 +000098 case Mips::fixup_MIPS_PCHI16:
Jack Carter84491ab2012-08-06 21:26:03 +000099 // Get the 2nd 16-bits. Also add 1 if bit 15 is 1.
Akira Hatanakada728192012-03-27 01:50:08 +0000100 Value = ((Value + 0x8000) >> 16) & 0xffff;
Akira Hatanakaf5ddf132011-11-23 22:18:04 +0000101 break;
Jack Carter84491ab2012-08-06 21:26:03 +0000102 case Mips::fixup_Mips_HIGHER:
103 // Get the 3rd 16-bits.
104 Value = ((Value + 0x80008000LL) >> 32) & 0xffff;
105 break;
106 case Mips::fixup_Mips_HIGHEST:
107 // Get the 4th 16-bits.
108 Value = ((Value + 0x800080008000LL) >> 48) & 0xffff;
109 break;
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000110 case Mips::fixup_MICROMIPS_26_S1:
111 Value >>= 1;
112 break;
Jozef Kolek9761e962015-01-12 12:03:34 +0000113 case Mips::fixup_MICROMIPS_PC7_S1:
114 Value -= 4;
115 // Forcing a signed division because Value can be negative.
116 Value = (int64_t) Value / 2;
117 // We now check if Value can be encoded as a 7-bit signed immediate.
Oliver Stannard9be59af2015-11-17 10:00:43 +0000118 if (!isInt<7>(Value) && Ctx) {
119 Ctx->reportError(Fixup.getLoc(), "out of range PC7 fixup");
120 return 0;
121 }
Jozef Kolek9761e962015-01-12 12:03:34 +0000122 break;
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000123 case Mips::fixup_MICROMIPS_PC10_S1:
124 Value -= 2;
125 // Forcing a signed division because Value can be negative.
126 Value = (int64_t) Value / 2;
127 // We now check if Value can be encoded as a 10-bit signed immediate.
Oliver Stannard9be59af2015-11-17 10:00:43 +0000128 if (!isInt<10>(Value) && Ctx) {
129 Ctx->reportError(Fixup.getLoc(), "out of range PC10 fixup");
130 return 0;
131 }
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000132 break;
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000133 case Mips::fixup_MICROMIPS_PC16_S1:
134 Value -= 4;
Matheus Almeidae0d75aa2013-12-13 11:11:02 +0000135 // Forcing a signed division because Value can be negative.
136 Value = (int64_t)Value / 2;
137 // We now check if Value can be encoded as a 16-bit signed immediate.
Oliver Stannard9be59af2015-11-17 10:00:43 +0000138 if (!isInt<16>(Value) && Ctx) {
139 Ctx->reportError(Fixup.getLoc(), "out of range PC16 fixup");
140 return 0;
141 }
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000142 break;
Zoran Jovanovica5acdcf2014-06-13 14:26:47 +0000143 case Mips::fixup_MIPS_PC18_S3:
144 // Forcing a signed division because Value can be negative.
145 Value = (int64_t)Value / 8;
146 // We now check if Value can be encoded as a 18-bit signed immediate.
Oliver Stannard9be59af2015-11-17 10:00:43 +0000147 if (!isInt<18>(Value) && Ctx) {
148 Ctx->reportError(Fixup.getLoc(), "out of range PC18 fixup");
149 return 0;
150 }
Zoran Jovanovica5acdcf2014-06-13 14:26:47 +0000151 break;
Zoran Jovanovic8e366822016-04-22 10:15:12 +0000152 case Mips::fixup_MICROMIPS_PC18_S3:
153 // Check alignment.
154 if ((Value & 7) && Ctx) {
155 Ctx->reportError(Fixup.getLoc(), "out of range PC18 fixup");
156 }
157 // Forcing a signed division because Value can be negative.
158 Value = (int64_t)Value / 8;
159 // We now check if Value can be encoded as a 18-bit signed immediate.
160 if (!isInt<18>(Value) && Ctx) {
161 Ctx->reportError(Fixup.getLoc(), "out of range PC18 fixup");
162 return 0;
163 }
164 break;
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000165 case Mips::fixup_MIPS_PC21_S2:
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000166 // Forcing a signed division because Value can be negative.
167 Value = (int64_t) Value / 4;
168 // We now check if Value can be encoded as a 21-bit signed immediate.
Oliver Stannard9be59af2015-11-17 10:00:43 +0000169 if (!isInt<21>(Value) && Ctx) {
170 Ctx->reportError(Fixup.getLoc(), "out of range PC21 fixup");
171 return 0;
172 }
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000173 break;
174 case Mips::fixup_MIPS_PC26_S2:
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000175 // Forcing a signed division because Value can be negative.
176 Value = (int64_t) Value / 4;
177 // We now check if Value can be encoded as a 26-bit signed immediate.
Oliver Stannard9be59af2015-11-17 10:00:43 +0000178 if (!isInt<26>(Value) && Ctx) {
179 Ctx->reportError(Fixup.getLoc(), "out of range PC26 fixup");
180 return 0;
181 }
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000182 break;
Zoran Jovanovic02b70032016-04-21 13:43:26 +0000183 case Mips::fixup_MICROMIPS_PC26_S1:
184 // Forcing a signed division because Value can be negative.
185 Value = (int64_t)Value / 2;
186 // We now check if Value can be encoded as a 26-bit signed immediate.
187 if (!isInt<26>(Value) && Ctx) {
188 Ctx->reportFatalError(Fixup.getLoc(), "out of range PC26 fixup");
189 return 0;
190 }
191 break;
Zoran Jovanovic5f94ced2016-05-19 12:20:40 +0000192 case Mips::fixup_MICROMIPS_PC21_S1:
193 // Forcing a signed division because Value can be negative.
194 Value = (int64_t)Value / 2;
195 // We now check if Value can be encoded as a 21-bit signed immediate.
196 if (!isInt<21>(Value) && Ctx) {
197 Ctx->reportError(Fixup.getLoc(), "out of range PC21 fixup");
198 return 0;
199 }
200 break;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000201 }
202
203 return Value;
204}
205
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000206MCObjectWriter *
207MipsAsmBackend::createObjectWriter(raw_pwrite_stream &OS) const {
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000208 return createMipsELFObjectWriter(OS,
209 MCELFObjectTargetWriter::getOSABI(OSType), IsLittle, Is64Bit);
210}
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000211
Zoran Jovanovic842f20e2014-04-03 12:01:01 +0000212// Little-endian fixup data byte ordering:
213// mips32r2: a | b | x | x
214// microMIPS: x | x | a | b
215
216static bool needsMMLEByteOrder(unsigned Kind) {
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000217 return Kind != Mips::fixup_MICROMIPS_PC10_S1 &&
218 Kind >= Mips::fixup_MICROMIPS_26_S1 &&
Zoran Jovanovic842f20e2014-04-03 12:01:01 +0000219 Kind < Mips::LastTargetFixupKind;
220}
221
222// Calculate index for microMIPS specific little endian byte order
223static unsigned calculateMMLEIndex(unsigned i) {
224 assert(i <= 3 && "Index out of range!");
225
226 return (1 - i / 2) * 2 + i % 2;
227}
228
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000229/// ApplyFixup - Apply the \p Value for given \p Fixup into the provided
230/// data fragment, at the offset specified by the fixup and following the
231/// fixup kind as appropriate.
232void MipsAsmBackend::applyFixup(const MCFixup &Fixup, char *Data,
Rafael Espindola5904e122014-03-29 06:26:49 +0000233 unsigned DataSize, uint64_t Value,
234 bool IsPCRel) const {
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000235 MCFixupKind Kind = Fixup.getKind();
236 Value = adjustFixupValue(Fixup, Value);
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000237
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000238 if (!Value)
239 return; // Doesn't change encoding.
240
241 // Where do we start in the object
242 unsigned Offset = Fixup.getOffset();
243 // Number of bytes we need to fixup
244 unsigned NumBytes = (getFixupKindInfo(Kind).TargetSize + 7) / 8;
245 // Used to point to big endian bytes
246 unsigned FullSize;
247
248 switch ((unsigned)Kind) {
249 case FK_Data_2:
250 case Mips::fixup_Mips_16:
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000251 case Mips::fixup_MICROMIPS_PC10_S1:
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000252 FullSize = 2;
253 break;
254 case FK_Data_8:
255 case Mips::fixup_Mips_64:
256 FullSize = 8;
257 break;
258 case FK_Data_4:
259 default:
260 FullSize = 4;
261 break;
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000262 }
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000263
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000264 // Grab current value, if any, from bits.
265 uint64_t CurVal = 0;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000266
Zoran Jovanovic842f20e2014-04-03 12:01:01 +0000267 bool microMipsLEByteOrder = needsMMLEByteOrder((unsigned) Kind);
268
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000269 for (unsigned i = 0; i != NumBytes; ++i) {
Zoran Jovanovic842f20e2014-04-03 12:01:01 +0000270 unsigned Idx = IsLittle ? (microMipsLEByteOrder ? calculateMMLEIndex(i)
271 : i)
272 : (FullSize - 1 - i);
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000273 CurVal |= (uint64_t)((uint8_t)Data[Offset + Idx]) << (i*8);
Akira Hatanaka0137dfe2012-03-21 00:52:01 +0000274 }
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000275
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000276 uint64_t Mask = ((uint64_t)(-1) >>
277 (64 - getFixupKindInfo(Kind).TargetSize));
278 CurVal |= Value & Mask;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000279
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000280 // Write out the fixed up bytes back to the code/data bits.
281 for (unsigned i = 0; i != NumBytes; ++i) {
Zoran Jovanovic842f20e2014-04-03 12:01:01 +0000282 unsigned Idx = IsLittle ? (microMipsLEByteOrder ? calculateMMLEIndex(i)
283 : i)
284 : (FullSize - 1 - i);
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000285 Data[Offset + Idx] = (uint8_t)((CurVal >> (i*8)) & 0xff);
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000286 }
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000287}
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000288
David Majnemerce108422016-01-19 23:05:27 +0000289Optional<MCFixupKind> MipsAsmBackend::getFixupKind(StringRef Name) const {
290 return StringSwitch<Optional<MCFixupKind>>(Name)
291 .Case("R_MIPS_NONE", (MCFixupKind)Mips::fixup_Mips_NONE)
292 .Case("R_MIPS_32", FK_Data_4)
293 .Default(MCAsmBackend::getFixupKind(Name));
Daniel Sanders9f6ad492015-11-12 13:33:00 +0000294}
295
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000296const MCFixupKindInfo &MipsAsmBackend::
297getFixupKindInfo(MCFixupKind Kind) const {
Daniel Sanders683ed962014-05-23 13:35:24 +0000298 const static MCFixupKindInfo LittleEndianInfos[Mips::NumTargetFixupKinds] = {
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000299 // This table *must* be in same the order of fixup_* kinds in
300 // MipsFixupKinds.h.
301 //
302 // name offset bits flags
Daniel Sanders9f6ad492015-11-12 13:33:00 +0000303 { "fixup_Mips_NONE", 0, 0, 0 },
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000304 { "fixup_Mips_16", 0, 16, 0 },
305 { "fixup_Mips_32", 0, 32, 0 },
306 { "fixup_Mips_REL32", 0, 32, 0 },
307 { "fixup_Mips_26", 0, 26, 0 },
308 { "fixup_Mips_HI16", 0, 16, 0 },
309 { "fixup_Mips_LO16", 0, 16, 0 },
310 { "fixup_Mips_GPREL16", 0, 16, 0 },
311 { "fixup_Mips_LITERAL", 0, 16, 0 },
Daniel Sandersfe98b2f2016-05-03 13:35:44 +0000312 { "fixup_Mips_GOT", 0, 16, 0 },
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000313 { "fixup_Mips_PC16", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
314 { "fixup_Mips_CALL16", 0, 16, 0 },
315 { "fixup_Mips_GPREL32", 0, 32, 0 },
316 { "fixup_Mips_SHIFT5", 6, 5, 0 },
317 { "fixup_Mips_SHIFT6", 6, 5, 0 },
318 { "fixup_Mips_64", 0, 64, 0 },
319 { "fixup_Mips_TLSGD", 0, 16, 0 },
320 { "fixup_Mips_GOTTPREL", 0, 16, 0 },
321 { "fixup_Mips_TPREL_HI", 0, 16, 0 },
322 { "fixup_Mips_TPREL_LO", 0, 16, 0 },
323 { "fixup_Mips_TLSLDM", 0, 16, 0 },
324 { "fixup_Mips_DTPREL_HI", 0, 16, 0 },
325 { "fixup_Mips_DTPREL_LO", 0, 16, 0 },
326 { "fixup_Mips_Branch_PCRel", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
327 { "fixup_Mips_GPOFF_HI", 0, 16, 0 },
328 { "fixup_Mips_GPOFF_LO", 0, 16, 0 },
329 { "fixup_Mips_GOT_PAGE", 0, 16, 0 },
330 { "fixup_Mips_GOT_OFST", 0, 16, 0 },
331 { "fixup_Mips_GOT_DISP", 0, 16, 0 },
332 { "fixup_Mips_HIGHER", 0, 16, 0 },
333 { "fixup_Mips_HIGHEST", 0, 16, 0 },
334 { "fixup_Mips_GOT_HI16", 0, 16, 0 },
335 { "fixup_Mips_GOT_LO16", 0, 16, 0 },
336 { "fixup_Mips_CALL_HI16", 0, 16, 0 },
337 { "fixup_Mips_CALL_LO16", 0, 16, 0 },
Zoran Jovanovica5acdcf2014-06-13 14:26:47 +0000338 { "fixup_Mips_PC18_S3", 0, 18, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +0000339 { "fixup_MIPS_PC19_S2", 0, 19, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000340 { "fixup_MIPS_PC21_S2", 0, 21, MCFixupKindInfo::FKF_IsPCRel },
341 { "fixup_MIPS_PC26_S2", 0, 26, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovicb355e8f2014-05-27 14:58:51 +0000342 { "fixup_MIPS_PCHI16", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
343 { "fixup_MIPS_PCLO16", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000344 { "fixup_MICROMIPS_26_S1", 0, 26, 0 },
345 { "fixup_MICROMIPS_HI16", 0, 16, 0 },
346 { "fixup_MICROMIPS_LO16", 0, 16, 0 },
347 { "fixup_MICROMIPS_GOT16", 0, 16, 0 },
Jozef Kolek9761e962015-01-12 12:03:34 +0000348 { "fixup_MICROMIPS_PC7_S1", 0, 7, MCFixupKindInfo::FKF_IsPCRel },
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000349 { "fixup_MICROMIPS_PC10_S1", 0, 10, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000350 { "fixup_MICROMIPS_PC16_S1", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovic02b70032016-04-21 13:43:26 +0000351 { "fixup_MICROMIPS_PC26_S1", 0, 26, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovic6764fa72016-04-21 14:09:35 +0000352 { "fixup_MICROMIPS_PC19_S2", 0, 19, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovic8e366822016-04-22 10:15:12 +0000353 { "fixup_MICROMIPS_PC18_S3", 0, 18, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovic5f94ced2016-05-19 12:20:40 +0000354 { "fixup_MICROMIPS_PC21_S1", 0, 21, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000355 { "fixup_MICROMIPS_CALL16", 0, 16, 0 },
356 { "fixup_MICROMIPS_GOT_DISP", 0, 16, 0 },
357 { "fixup_MICROMIPS_GOT_PAGE", 0, 16, 0 },
358 { "fixup_MICROMIPS_GOT_OFST", 0, 16, 0 },
359 { "fixup_MICROMIPS_TLS_GD", 0, 16, 0 },
360 { "fixup_MICROMIPS_TLS_LDM", 0, 16, 0 },
361 { "fixup_MICROMIPS_TLS_DTPREL_HI16", 0, 16, 0 },
362 { "fixup_MICROMIPS_TLS_DTPREL_LO16", 0, 16, 0 },
363 { "fixup_MICROMIPS_TLS_TPREL_HI16", 0, 16, 0 },
Daniel Sanderscae9aee2016-08-08 09:33:14 +0000364 { "fixup_MICROMIPS_TLS_TPREL_LO16", 0, 16, 0 }
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000365 };
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000366
Daniel Sanders683ed962014-05-23 13:35:24 +0000367 const static MCFixupKindInfo BigEndianInfos[Mips::NumTargetFixupKinds] = {
368 // This table *must* be in same the order of fixup_* kinds in
369 // MipsFixupKinds.h.
370 //
371 // name offset bits flags
Daniel Sanders9f6ad492015-11-12 13:33:00 +0000372 { "fixup_Mips_NONE", 0, 0, 0 },
Daniel Sanders683ed962014-05-23 13:35:24 +0000373 { "fixup_Mips_16", 16, 16, 0 },
374 { "fixup_Mips_32", 0, 32, 0 },
375 { "fixup_Mips_REL32", 0, 32, 0 },
376 { "fixup_Mips_26", 6, 26, 0 },
377 { "fixup_Mips_HI16", 16, 16, 0 },
378 { "fixup_Mips_LO16", 16, 16, 0 },
379 { "fixup_Mips_GPREL16", 16, 16, 0 },
380 { "fixup_Mips_LITERAL", 16, 16, 0 },
Daniel Sandersfe98b2f2016-05-03 13:35:44 +0000381 { "fixup_Mips_GOT", 16, 16, 0 },
Daniel Sanders683ed962014-05-23 13:35:24 +0000382 { "fixup_Mips_PC16", 16, 16, MCFixupKindInfo::FKF_IsPCRel },
383 { "fixup_Mips_CALL16", 16, 16, 0 },
384 { "fixup_Mips_GPREL32", 0, 32, 0 },
385 { "fixup_Mips_SHIFT5", 21, 5, 0 },
386 { "fixup_Mips_SHIFT6", 21, 5, 0 },
387 { "fixup_Mips_64", 0, 64, 0 },
388 { "fixup_Mips_TLSGD", 16, 16, 0 },
389 { "fixup_Mips_GOTTPREL", 16, 16, 0 },
390 { "fixup_Mips_TPREL_HI", 16, 16, 0 },
391 { "fixup_Mips_TPREL_LO", 16, 16, 0 },
392 { "fixup_Mips_TLSLDM", 16, 16, 0 },
393 { "fixup_Mips_DTPREL_HI", 16, 16, 0 },
394 { "fixup_Mips_DTPREL_LO", 16, 16, 0 },
395 { "fixup_Mips_Branch_PCRel",16, 16, MCFixupKindInfo::FKF_IsPCRel },
396 { "fixup_Mips_GPOFF_HI", 16, 16, 0 },
397 { "fixup_Mips_GPOFF_LO", 16, 16, 0 },
398 { "fixup_Mips_GOT_PAGE", 16, 16, 0 },
399 { "fixup_Mips_GOT_OFST", 16, 16, 0 },
400 { "fixup_Mips_GOT_DISP", 16, 16, 0 },
401 { "fixup_Mips_HIGHER", 16, 16, 0 },
402 { "fixup_Mips_HIGHEST", 16, 16, 0 },
403 { "fixup_Mips_GOT_HI16", 16, 16, 0 },
404 { "fixup_Mips_GOT_LO16", 16, 16, 0 },
405 { "fixup_Mips_CALL_HI16", 16, 16, 0 },
406 { "fixup_Mips_CALL_LO16", 16, 16, 0 },
Zoran Jovanovica5acdcf2014-06-13 14:26:47 +0000407 { "fixup_Mips_PC18_S3", 14, 18, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +0000408 { "fixup_MIPS_PC19_S2", 13, 19, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000409 { "fixup_MIPS_PC21_S2", 11, 21, MCFixupKindInfo::FKF_IsPCRel },
410 { "fixup_MIPS_PC26_S2", 6, 26, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovicb355e8f2014-05-27 14:58:51 +0000411 { "fixup_MIPS_PCHI16", 16, 16, MCFixupKindInfo::FKF_IsPCRel },
412 { "fixup_MIPS_PCLO16", 16, 16, MCFixupKindInfo::FKF_IsPCRel },
Daniel Sanders683ed962014-05-23 13:35:24 +0000413 { "fixup_MICROMIPS_26_S1", 6, 26, 0 },
414 { "fixup_MICROMIPS_HI16", 16, 16, 0 },
415 { "fixup_MICROMIPS_LO16", 16, 16, 0 },
416 { "fixup_MICROMIPS_GOT16", 16, 16, 0 },
Jozef Kolek9761e962015-01-12 12:03:34 +0000417 { "fixup_MICROMIPS_PC7_S1", 9, 7, MCFixupKindInfo::FKF_IsPCRel },
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000418 { "fixup_MICROMIPS_PC10_S1", 6, 10, MCFixupKindInfo::FKF_IsPCRel },
Daniel Sanders683ed962014-05-23 13:35:24 +0000419 { "fixup_MICROMIPS_PC16_S1",16, 16, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovic02b70032016-04-21 13:43:26 +0000420 { "fixup_MICROMIPS_PC26_S1", 6, 26, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovic6764fa72016-04-21 14:09:35 +0000421 { "fixup_MICROMIPS_PC19_S2",13, 19, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovic8e366822016-04-22 10:15:12 +0000422 { "fixup_MICROMIPS_PC18_S3",14, 18, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovic5f94ced2016-05-19 12:20:40 +0000423 { "fixup_MICROMIPS_PC21_S1",11, 21, MCFixupKindInfo::FKF_IsPCRel },
Daniel Sanders683ed962014-05-23 13:35:24 +0000424 { "fixup_MICROMIPS_CALL16", 16, 16, 0 },
425 { "fixup_MICROMIPS_GOT_DISP", 16, 16, 0 },
426 { "fixup_MICROMIPS_GOT_PAGE", 16, 16, 0 },
427 { "fixup_MICROMIPS_GOT_OFST", 16, 16, 0 },
428 { "fixup_MICROMIPS_TLS_GD", 16, 16, 0 },
429 { "fixup_MICROMIPS_TLS_LDM", 16, 16, 0 },
430 { "fixup_MICROMIPS_TLS_DTPREL_HI16", 16, 16, 0 },
431 { "fixup_MICROMIPS_TLS_DTPREL_LO16", 16, 16, 0 },
432 { "fixup_MICROMIPS_TLS_TPREL_HI16", 16, 16, 0 },
Daniel Sanderscae9aee2016-08-08 09:33:14 +0000433 { "fixup_MICROMIPS_TLS_TPREL_LO16", 16, 16, 0 }
Daniel Sanders683ed962014-05-23 13:35:24 +0000434 };
435
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000436 if (Kind < FirstTargetFixupKind)
437 return MCAsmBackend::getFixupKindInfo(Kind);
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000438
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000439 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
440 "Invalid kind!");
Daniel Sanders683ed962014-05-23 13:35:24 +0000441
442 if (IsLittle)
443 return LittleEndianInfos[Kind - FirstTargetFixupKind];
444 return BigEndianInfos[Kind - FirstTargetFixupKind];
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000445}
Jim Grosbach25b63fa2011-12-06 00:47:03 +0000446
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000447/// WriteNopData - Write an (optimal) nop sequence of Count bytes
448/// to the given output. If the target cannot generate such a sequence,
449/// it should return an error.
450///
451/// \return - True on success.
452bool MipsAsmBackend::writeNopData(uint64_t Count, MCObjectWriter *OW) const {
453 // Check for a less than instruction size number of bytes
454 // FIXME: 16 bit instructions are not handled yet here.
455 // We shouldn't be using a hard coded number for instruction size.
Joerg Sonnenbergerf148a6d2014-10-02 13:41:42 +0000456
457 // If the count is not 4-byte aligned, we must be writing data into the text
458 // section (otherwise we have unaligned instructions, and thus have far
459 // bigger problems), so just write zeros instead.
Benjamin Kramer97fbdd52015-04-17 11:12:43 +0000460 OW->WriteZeros(Count);
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000461 return true;
462}
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000463
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000464/// processFixupValue - Target hook to process the literal value of a fixup
465/// if necessary.
466void MipsAsmBackend::processFixupValue(const MCAssembler &Asm,
467 const MCAsmLayout &Layout,
468 const MCFixup &Fixup,
469 const MCFragment *DF,
Rafael Espindola3e3de5e2014-03-28 16:06:09 +0000470 const MCValue &Target,
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000471 uint64_t &Value,
472 bool &IsResolved) {
473 // At this point we'll ignore the value returned by adjustFixupValue as
474 // we are only checking if the fixup can be applied correctly. We have
475 // access to MCContext from here which allows us to report a fatal error
476 // with *possibly* a source code location.
Daniel Sandersfe98b2f2016-05-03 13:35:44 +0000477 // The caller will also ignore any changes we make to Value
478 // (recordRelocation() overwrites it with it's own calculation).
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000479 (void)adjustFixupValue(Fixup, Value, &Asm.getContext());
480}
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000481
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000482// MCAsmBackend
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000483MCAsmBackend *llvm::createMipsAsmBackendEL32(const Target &T,
484 const MCRegisterInfo &MRI,
Joel Jones373d7d32016-07-25 17:18:28 +0000485 const Triple &TT, StringRef CPU,
486 const MCTargetOptions &Options) {
Daniel Sanders418caf52015-06-10 10:35:34 +0000487 return new MipsAsmBackend(T, TT.getOS(), /*IsLittle*/ true,
488 /*Is64Bit*/ false);
Rafael Espindola647841b2012-01-11 04:04:14 +0000489}
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000490
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000491MCAsmBackend *llvm::createMipsAsmBackendEB32(const Target &T,
492 const MCRegisterInfo &MRI,
Joel Jones373d7d32016-07-25 17:18:28 +0000493 const Triple &TT, StringRef CPU,
494 const MCTargetOptions &Options) {
Daniel Sanders418caf52015-06-10 10:35:34 +0000495 return new MipsAsmBackend(T, TT.getOS(), /*IsLittle*/ false,
496 /*Is64Bit*/ false);
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000497}
Akira Hatanakab1f68f92012-04-02 19:25:22 +0000498
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000499MCAsmBackend *llvm::createMipsAsmBackendEL64(const Target &T,
500 const MCRegisterInfo &MRI,
Joel Jones373d7d32016-07-25 17:18:28 +0000501 const Triple &TT, StringRef CPU,
502 const MCTargetOptions &Options) {
Daniel Sanders418caf52015-06-10 10:35:34 +0000503 return new MipsAsmBackend(T, TT.getOS(), /*IsLittle*/ true, /*Is64Bit*/ true);
Akira Hatanakab1f68f92012-04-02 19:25:22 +0000504}
505
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000506MCAsmBackend *llvm::createMipsAsmBackendEB64(const Target &T,
507 const MCRegisterInfo &MRI,
Joel Jones373d7d32016-07-25 17:18:28 +0000508 const Triple &TT, StringRef CPU,
509 const MCTargetOptions &Options) {
Daniel Sanders418caf52015-06-10 10:35:34 +0000510 return new MipsAsmBackend(T, TT.getOS(), /*IsLittle*/ false,
511 /*Is64Bit*/ true);
Akira Hatanakab1f68f92012-04-02 19:25:22 +0000512}