blob: 5c746b2894b2fc6ae2dbdcb5eca616e3fa5292f3 [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"
Akira Hatanaka587fe6c2011-09-30 21:04:02 +000017#include "MCTargetDesc/MipsMCTargetDesc.h"
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000018#include "llvm/MC/MCAsmBackend.h"
Akira Hatanaka587fe6c2011-09-30 21:04:02 +000019#include "llvm/MC/MCAssembler.h"
Matheus Almeidae0d75aa2013-12-13 11:11:02 +000020#include "llvm/MC/MCContext.h"
Akira Hatanaka587fe6c2011-09-30 21:04:02 +000021#include "llvm/MC/MCDirectives.h"
22#include "llvm/MC/MCELFObjectWriter.h"
Craig Topper6e80c282012-03-26 06:58:25 +000023#include "llvm/MC/MCFixupKindInfo.h"
Akira Hatanaka587fe6c2011-09-30 21:04:02 +000024#include "llvm/MC/MCObjectWriter.h"
Akira Hatanaka587fe6c2011-09-30 21:04:02 +000025#include "llvm/MC/MCSubtargetInfo.h"
Akira Hatanaka587fe6c2011-09-30 21:04:02 +000026#include "llvm/Support/ErrorHandling.h"
Matheus Almeidae0d75aa2013-12-13 11:11:02 +000027#include "llvm/Support/MathExtras.h"
Chandler Carruth8a8cd2b2014-01-07 11:48:04 +000028#include "llvm/Support/raw_ostream.h"
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +000029
Akira Hatanaka587fe6c2011-09-30 21:04:02 +000030using namespace llvm;
31
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +000032// Prepare value for the target space for it
Matheus Almeidae0d75aa2013-12-13 11:11:02 +000033static unsigned adjustFixupValue(const MCFixup &Fixup, uint64_t Value,
Craig Topper062a2ba2014-04-25 05:30:21 +000034 MCContext *Ctx = nullptr) {
Matheus Almeidae0d75aa2013-12-13 11:11:02 +000035
36 unsigned Kind = Fixup.getKind();
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000037
38 // Add/subtract and shift
39 switch (Kind) {
40 default:
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +000041 return 0;
Ed Maste2a710d02014-03-03 14:27:49 +000042 case FK_Data_2:
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +000043 case FK_GPRel_4:
44 case FK_Data_4:
Jack Carter4c583812012-08-07 00:01:14 +000045 case FK_Data_8:
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:
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000060 break;
61 case Mips::fixup_Mips_PC16:
62 // So far we are only using this type for branches.
63 // For branches we start 1 instruction after the branch
64 // so the displacement will be one instruction size less.
65 Value -= 4;
66 // The displacement is then divided by 4 to give us an 18 bit
Matheus Almeidae0d75aa2013-12-13 11:11:02 +000067 // address range. Forcing a signed division because Value can be negative.
68 Value = (int64_t)Value / 4;
69 // We now check if Value can be encoded as a 16-bit signed immediate.
Matheus Almeida8cc8b352013-12-17 17:10:00 +000070 if (!isIntN(16, Value) && Ctx)
Jim Grosbach6f482002015-05-18 18:43:14 +000071 Ctx->reportFatalError(Fixup.getLoc(), "out of range PC16 fixup");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000072 break;
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +000073 case Mips::fixup_MIPS_PC19_S2:
74 // 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 19-bit signed immediate.
77 if (!isIntN(19, Value) && Ctx)
Jim Grosbach6f482002015-05-18 18:43:14 +000078 Ctx->reportFatalError(Fixup.getLoc(), "out of range PC19 fixup");
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +000079 break;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000080 case Mips::fixup_Mips_26:
81 // So far we are only using this type for jumps.
82 // The displacement is then divided by 4 to give us an 28 bit
83 // address range.
84 Value >>= 2;
85 break;
Akira Hatanakaf5ddf132011-11-23 22:18:04 +000086 case Mips::fixup_Mips_HI16:
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +000087 case Mips::fixup_Mips_GOT_Local:
Jack Carterb05cb672012-11-21 23:38:59 +000088 case Mips::fixup_Mips_GOT_HI16:
89 case Mips::fixup_Mips_CALL_HI16:
Zoran Jovanovice7ae8af2013-10-23 16:14:44 +000090 case Mips::fixup_MICROMIPS_HI16:
Zoran Jovanovicb355e8f2014-05-27 14:58:51 +000091 case Mips::fixup_MIPS_PCHI16:
Jack Carter84491ab2012-08-06 21:26:03 +000092 // Get the 2nd 16-bits. Also add 1 if bit 15 is 1.
Akira Hatanakada728192012-03-27 01:50:08 +000093 Value = ((Value + 0x8000) >> 16) & 0xffff;
Akira Hatanakaf5ddf132011-11-23 22:18:04 +000094 break;
Jack Carter84491ab2012-08-06 21:26:03 +000095 case Mips::fixup_Mips_HIGHER:
96 // Get the 3rd 16-bits.
97 Value = ((Value + 0x80008000LL) >> 32) & 0xffff;
98 break;
99 case Mips::fixup_Mips_HIGHEST:
100 // Get the 4th 16-bits.
101 Value = ((Value + 0x800080008000LL) >> 48) & 0xffff;
102 break;
Zoran Jovanovic507e0842013-10-29 16:38:59 +0000103 case Mips::fixup_MICROMIPS_26_S1:
104 Value >>= 1;
105 break;
Jozef Kolek9761e962015-01-12 12:03:34 +0000106 case Mips::fixup_MICROMIPS_PC7_S1:
107 Value -= 4;
108 // Forcing a signed division because Value can be negative.
109 Value = (int64_t) Value / 2;
110 // We now check if Value can be encoded as a 7-bit signed immediate.
111 if (!isIntN(7, Value) && Ctx)
Jim Grosbach6f482002015-05-18 18:43:14 +0000112 Ctx->reportFatalError(Fixup.getLoc(), "out of range PC7 fixup");
Jozef Kolek9761e962015-01-12 12:03:34 +0000113 break;
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000114 case Mips::fixup_MICROMIPS_PC10_S1:
115 Value -= 2;
116 // Forcing a signed division because Value can be negative.
117 Value = (int64_t) Value / 2;
118 // We now check if Value can be encoded as a 10-bit signed immediate.
119 if (!isIntN(10, Value) && Ctx)
Jim Grosbach6f482002015-05-18 18:43:14 +0000120 Ctx->reportFatalError(Fixup.getLoc(), "out of range PC10 fixup");
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000121 break;
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000122 case Mips::fixup_MICROMIPS_PC16_S1:
123 Value -= 4;
Matheus Almeidae0d75aa2013-12-13 11:11:02 +0000124 // Forcing a signed division because Value can be negative.
125 Value = (int64_t)Value / 2;
126 // We now check if Value can be encoded as a 16-bit signed immediate.
Matheus Almeida8cc8b352013-12-17 17:10:00 +0000127 if (!isIntN(16, Value) && Ctx)
Jim Grosbach6f482002015-05-18 18:43:14 +0000128 Ctx->reportFatalError(Fixup.getLoc(), "out of range PC16 fixup");
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000129 break;
Zoran Jovanovica5acdcf2014-06-13 14:26:47 +0000130 case Mips::fixup_MIPS_PC18_S3:
131 // Forcing a signed division because Value can be negative.
132 Value = (int64_t)Value / 8;
133 // We now check if Value can be encoded as a 18-bit signed immediate.
134 if (!isIntN(18, Value) && Ctx)
Jim Grosbach6f482002015-05-18 18:43:14 +0000135 Ctx->reportFatalError(Fixup.getLoc(), "out of range PC18 fixup");
Zoran Jovanovica5acdcf2014-06-13 14:26:47 +0000136 break;
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000137 case Mips::fixup_MIPS_PC21_S2:
138 Value -= 4;
139 // Forcing a signed division because Value can be negative.
140 Value = (int64_t) Value / 4;
141 // We now check if Value can be encoded as a 21-bit signed immediate.
142 if (!isIntN(21, Value) && Ctx)
Jim Grosbach6f482002015-05-18 18:43:14 +0000143 Ctx->reportFatalError(Fixup.getLoc(), "out of range PC21 fixup");
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000144 break;
145 case Mips::fixup_MIPS_PC26_S2:
146 Value -= 4;
147 // Forcing a signed division because Value can be negative.
148 Value = (int64_t) Value / 4;
149 // We now check if Value can be encoded as a 26-bit signed immediate.
150 if (!isIntN(26, Value) && Ctx)
Jim Grosbach6f482002015-05-18 18:43:14 +0000151 Ctx->reportFatalError(Fixup.getLoc(), "out of range PC26 fixup");
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000152 break;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000153 }
154
155 return Value;
156}
157
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000158MCObjectWriter *
159MipsAsmBackend::createObjectWriter(raw_pwrite_stream &OS) const {
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000160 return createMipsELFObjectWriter(OS,
161 MCELFObjectTargetWriter::getOSABI(OSType), IsLittle, Is64Bit);
162}
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000163
Zoran Jovanovic842f20e2014-04-03 12:01:01 +0000164// Little-endian fixup data byte ordering:
165// mips32r2: a | b | x | x
166// microMIPS: x | x | a | b
167
168static bool needsMMLEByteOrder(unsigned Kind) {
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000169 return Kind != Mips::fixup_MICROMIPS_PC10_S1 &&
170 Kind >= Mips::fixup_MICROMIPS_26_S1 &&
Zoran Jovanovic842f20e2014-04-03 12:01:01 +0000171 Kind < Mips::LastTargetFixupKind;
172}
173
174// Calculate index for microMIPS specific little endian byte order
175static unsigned calculateMMLEIndex(unsigned i) {
176 assert(i <= 3 && "Index out of range!");
177
178 return (1 - i / 2) * 2 + i % 2;
179}
180
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000181/// ApplyFixup - Apply the \p Value for given \p Fixup into the provided
182/// data fragment, at the offset specified by the fixup and following the
183/// fixup kind as appropriate.
184void MipsAsmBackend::applyFixup(const MCFixup &Fixup, char *Data,
Rafael Espindola5904e122014-03-29 06:26:49 +0000185 unsigned DataSize, uint64_t Value,
186 bool IsPCRel) const {
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000187 MCFixupKind Kind = Fixup.getKind();
188 Value = adjustFixupValue(Fixup, Value);
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000189
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000190 if (!Value)
191 return; // Doesn't change encoding.
192
193 // Where do we start in the object
194 unsigned Offset = Fixup.getOffset();
195 // Number of bytes we need to fixup
196 unsigned NumBytes = (getFixupKindInfo(Kind).TargetSize + 7) / 8;
197 // Used to point to big endian bytes
198 unsigned FullSize;
199
200 switch ((unsigned)Kind) {
201 case FK_Data_2:
202 case Mips::fixup_Mips_16:
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000203 case Mips::fixup_MICROMIPS_PC10_S1:
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000204 FullSize = 2;
205 break;
206 case FK_Data_8:
207 case Mips::fixup_Mips_64:
208 FullSize = 8;
209 break;
210 case FK_Data_4:
211 default:
212 FullSize = 4;
213 break;
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000214 }
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000215
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000216 // Grab current value, if any, from bits.
217 uint64_t CurVal = 0;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000218
Zoran Jovanovic842f20e2014-04-03 12:01:01 +0000219 bool microMipsLEByteOrder = needsMMLEByteOrder((unsigned) Kind);
220
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000221 for (unsigned i = 0; i != NumBytes; ++i) {
Zoran Jovanovic842f20e2014-04-03 12:01:01 +0000222 unsigned Idx = IsLittle ? (microMipsLEByteOrder ? calculateMMLEIndex(i)
223 : i)
224 : (FullSize - 1 - i);
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000225 CurVal |= (uint64_t)((uint8_t)Data[Offset + Idx]) << (i*8);
Akira Hatanaka0137dfe2012-03-21 00:52:01 +0000226 }
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000227
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000228 uint64_t Mask = ((uint64_t)(-1) >>
229 (64 - getFixupKindInfo(Kind).TargetSize));
230 CurVal |= Value & Mask;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000231
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000232 // Write out the fixed up bytes back to the code/data bits.
233 for (unsigned i = 0; i != NumBytes; ++i) {
Zoran Jovanovic842f20e2014-04-03 12:01:01 +0000234 unsigned Idx = IsLittle ? (microMipsLEByteOrder ? calculateMMLEIndex(i)
235 : i)
236 : (FullSize - 1 - i);
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000237 Data[Offset + Idx] = (uint8_t)((CurVal >> (i*8)) & 0xff);
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000238 }
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000239}
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000240
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000241const MCFixupKindInfo &MipsAsmBackend::
242getFixupKindInfo(MCFixupKind Kind) const {
Daniel Sanders683ed962014-05-23 13:35:24 +0000243 const static MCFixupKindInfo LittleEndianInfos[Mips::NumTargetFixupKinds] = {
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000244 // This table *must* be in same the order of fixup_* kinds in
245 // MipsFixupKinds.h.
246 //
247 // name offset bits flags
248 { "fixup_Mips_16", 0, 16, 0 },
249 { "fixup_Mips_32", 0, 32, 0 },
250 { "fixup_Mips_REL32", 0, 32, 0 },
251 { "fixup_Mips_26", 0, 26, 0 },
252 { "fixup_Mips_HI16", 0, 16, 0 },
253 { "fixup_Mips_LO16", 0, 16, 0 },
254 { "fixup_Mips_GPREL16", 0, 16, 0 },
255 { "fixup_Mips_LITERAL", 0, 16, 0 },
256 { "fixup_Mips_GOT_Global", 0, 16, 0 },
257 { "fixup_Mips_GOT_Local", 0, 16, 0 },
258 { "fixup_Mips_PC16", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
259 { "fixup_Mips_CALL16", 0, 16, 0 },
260 { "fixup_Mips_GPREL32", 0, 32, 0 },
261 { "fixup_Mips_SHIFT5", 6, 5, 0 },
262 { "fixup_Mips_SHIFT6", 6, 5, 0 },
263 { "fixup_Mips_64", 0, 64, 0 },
264 { "fixup_Mips_TLSGD", 0, 16, 0 },
265 { "fixup_Mips_GOTTPREL", 0, 16, 0 },
266 { "fixup_Mips_TPREL_HI", 0, 16, 0 },
267 { "fixup_Mips_TPREL_LO", 0, 16, 0 },
268 { "fixup_Mips_TLSLDM", 0, 16, 0 },
269 { "fixup_Mips_DTPREL_HI", 0, 16, 0 },
270 { "fixup_Mips_DTPREL_LO", 0, 16, 0 },
271 { "fixup_Mips_Branch_PCRel", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
272 { "fixup_Mips_GPOFF_HI", 0, 16, 0 },
273 { "fixup_Mips_GPOFF_LO", 0, 16, 0 },
274 { "fixup_Mips_GOT_PAGE", 0, 16, 0 },
275 { "fixup_Mips_GOT_OFST", 0, 16, 0 },
276 { "fixup_Mips_GOT_DISP", 0, 16, 0 },
277 { "fixup_Mips_HIGHER", 0, 16, 0 },
278 { "fixup_Mips_HIGHEST", 0, 16, 0 },
279 { "fixup_Mips_GOT_HI16", 0, 16, 0 },
280 { "fixup_Mips_GOT_LO16", 0, 16, 0 },
281 { "fixup_Mips_CALL_HI16", 0, 16, 0 },
282 { "fixup_Mips_CALL_LO16", 0, 16, 0 },
Zoran Jovanovica5acdcf2014-06-13 14:26:47 +0000283 { "fixup_Mips_PC18_S3", 0, 18, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +0000284 { "fixup_MIPS_PC19_S2", 0, 19, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000285 { "fixup_MIPS_PC21_S2", 0, 21, MCFixupKindInfo::FKF_IsPCRel },
286 { "fixup_MIPS_PC26_S2", 0, 26, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovicb355e8f2014-05-27 14:58:51 +0000287 { "fixup_MIPS_PCHI16", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
288 { "fixup_MIPS_PCLO16", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000289 { "fixup_MICROMIPS_26_S1", 0, 26, 0 },
290 { "fixup_MICROMIPS_HI16", 0, 16, 0 },
291 { "fixup_MICROMIPS_LO16", 0, 16, 0 },
292 { "fixup_MICROMIPS_GOT16", 0, 16, 0 },
Jozef Kolek9761e962015-01-12 12:03:34 +0000293 { "fixup_MICROMIPS_PC7_S1", 0, 7, MCFixupKindInfo::FKF_IsPCRel },
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000294 { "fixup_MICROMIPS_PC10_S1", 0, 10, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000295 { "fixup_MICROMIPS_PC16_S1", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
296 { "fixup_MICROMIPS_CALL16", 0, 16, 0 },
297 { "fixup_MICROMIPS_GOT_DISP", 0, 16, 0 },
298 { "fixup_MICROMIPS_GOT_PAGE", 0, 16, 0 },
299 { "fixup_MICROMIPS_GOT_OFST", 0, 16, 0 },
300 { "fixup_MICROMIPS_TLS_GD", 0, 16, 0 },
301 { "fixup_MICROMIPS_TLS_LDM", 0, 16, 0 },
302 { "fixup_MICROMIPS_TLS_DTPREL_HI16", 0, 16, 0 },
303 { "fixup_MICROMIPS_TLS_DTPREL_LO16", 0, 16, 0 },
304 { "fixup_MICROMIPS_TLS_TPREL_HI16", 0, 16, 0 },
305 { "fixup_MICROMIPS_TLS_TPREL_LO16", 0, 16, 0 }
306 };
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000307
Daniel Sanders683ed962014-05-23 13:35:24 +0000308 const static MCFixupKindInfo BigEndianInfos[Mips::NumTargetFixupKinds] = {
309 // This table *must* be in same the order of fixup_* kinds in
310 // MipsFixupKinds.h.
311 //
312 // name offset bits flags
313 { "fixup_Mips_16", 16, 16, 0 },
314 { "fixup_Mips_32", 0, 32, 0 },
315 { "fixup_Mips_REL32", 0, 32, 0 },
316 { "fixup_Mips_26", 6, 26, 0 },
317 { "fixup_Mips_HI16", 16, 16, 0 },
318 { "fixup_Mips_LO16", 16, 16, 0 },
319 { "fixup_Mips_GPREL16", 16, 16, 0 },
320 { "fixup_Mips_LITERAL", 16, 16, 0 },
321 { "fixup_Mips_GOT_Global", 16, 16, 0 },
322 { "fixup_Mips_GOT_Local", 16, 16, 0 },
323 { "fixup_Mips_PC16", 16, 16, MCFixupKindInfo::FKF_IsPCRel },
324 { "fixup_Mips_CALL16", 16, 16, 0 },
325 { "fixup_Mips_GPREL32", 0, 32, 0 },
326 { "fixup_Mips_SHIFT5", 21, 5, 0 },
327 { "fixup_Mips_SHIFT6", 21, 5, 0 },
328 { "fixup_Mips_64", 0, 64, 0 },
329 { "fixup_Mips_TLSGD", 16, 16, 0 },
330 { "fixup_Mips_GOTTPREL", 16, 16, 0 },
331 { "fixup_Mips_TPREL_HI", 16, 16, 0 },
332 { "fixup_Mips_TPREL_LO", 16, 16, 0 },
333 { "fixup_Mips_TLSLDM", 16, 16, 0 },
334 { "fixup_Mips_DTPREL_HI", 16, 16, 0 },
335 { "fixup_Mips_DTPREL_LO", 16, 16, 0 },
336 { "fixup_Mips_Branch_PCRel",16, 16, MCFixupKindInfo::FKF_IsPCRel },
337 { "fixup_Mips_GPOFF_HI", 16, 16, 0 },
338 { "fixup_Mips_GPOFF_LO", 16, 16, 0 },
339 { "fixup_Mips_GOT_PAGE", 16, 16, 0 },
340 { "fixup_Mips_GOT_OFST", 16, 16, 0 },
341 { "fixup_Mips_GOT_DISP", 16, 16, 0 },
342 { "fixup_Mips_HIGHER", 16, 16, 0 },
343 { "fixup_Mips_HIGHEST", 16, 16, 0 },
344 { "fixup_Mips_GOT_HI16", 16, 16, 0 },
345 { "fixup_Mips_GOT_LO16", 16, 16, 0 },
346 { "fixup_Mips_CALL_HI16", 16, 16, 0 },
347 { "fixup_Mips_CALL_LO16", 16, 16, 0 },
Zoran Jovanovica5acdcf2014-06-13 14:26:47 +0000348 { "fixup_Mips_PC18_S3", 14, 18, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovicb9c07f32014-06-12 12:40:00 +0000349 { "fixup_MIPS_PC19_S2", 13, 19, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovic10e06da2014-05-27 12:55:40 +0000350 { "fixup_MIPS_PC21_S2", 11, 21, MCFixupKindInfo::FKF_IsPCRel },
351 { "fixup_MIPS_PC26_S2", 6, 26, MCFixupKindInfo::FKF_IsPCRel },
Zoran Jovanovicb355e8f2014-05-27 14:58:51 +0000352 { "fixup_MIPS_PCHI16", 16, 16, MCFixupKindInfo::FKF_IsPCRel },
353 { "fixup_MIPS_PCLO16", 16, 16, MCFixupKindInfo::FKF_IsPCRel },
Daniel Sanders683ed962014-05-23 13:35:24 +0000354 { "fixup_MICROMIPS_26_S1", 6, 26, 0 },
355 { "fixup_MICROMIPS_HI16", 16, 16, 0 },
356 { "fixup_MICROMIPS_LO16", 16, 16, 0 },
357 { "fixup_MICROMIPS_GOT16", 16, 16, 0 },
Jozef Kolek9761e962015-01-12 12:03:34 +0000358 { "fixup_MICROMIPS_PC7_S1", 9, 7, MCFixupKindInfo::FKF_IsPCRel },
Jozef Kolek5cfebdd2015-01-21 12:39:30 +0000359 { "fixup_MICROMIPS_PC10_S1", 6, 10, MCFixupKindInfo::FKF_IsPCRel },
Daniel Sanders683ed962014-05-23 13:35:24 +0000360 { "fixup_MICROMIPS_PC16_S1",16, 16, MCFixupKindInfo::FKF_IsPCRel },
361 { "fixup_MICROMIPS_CALL16", 16, 16, 0 },
362 { "fixup_MICROMIPS_GOT_DISP", 16, 16, 0 },
363 { "fixup_MICROMIPS_GOT_PAGE", 16, 16, 0 },
364 { "fixup_MICROMIPS_GOT_OFST", 16, 16, 0 },
365 { "fixup_MICROMIPS_TLS_GD", 16, 16, 0 },
366 { "fixup_MICROMIPS_TLS_LDM", 16, 16, 0 },
367 { "fixup_MICROMIPS_TLS_DTPREL_HI16", 16, 16, 0 },
368 { "fixup_MICROMIPS_TLS_DTPREL_LO16", 16, 16, 0 },
369 { "fixup_MICROMIPS_TLS_TPREL_HI16", 16, 16, 0 },
370 { "fixup_MICROMIPS_TLS_TPREL_LO16", 16, 16, 0 }
371 };
372
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000373 if (Kind < FirstTargetFixupKind)
374 return MCAsmBackend::getFixupKindInfo(Kind);
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000375
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000376 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
377 "Invalid kind!");
Daniel Sanders683ed962014-05-23 13:35:24 +0000378
379 if (IsLittle)
380 return LittleEndianInfos[Kind - FirstTargetFixupKind];
381 return BigEndianInfos[Kind - FirstTargetFixupKind];
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000382}
Jim Grosbach25b63fa2011-12-06 00:47:03 +0000383
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000384/// WriteNopData - Write an (optimal) nop sequence of Count bytes
385/// to the given output. If the target cannot generate such a sequence,
386/// it should return an error.
387///
388/// \return - True on success.
389bool MipsAsmBackend::writeNopData(uint64_t Count, MCObjectWriter *OW) const {
390 // Check for a less than instruction size number of bytes
391 // FIXME: 16 bit instructions are not handled yet here.
392 // We shouldn't be using a hard coded number for instruction size.
Joerg Sonnenbergerf148a6d2014-10-02 13:41:42 +0000393
394 // If the count is not 4-byte aligned, we must be writing data into the text
395 // section (otherwise we have unaligned instructions, and thus have far
396 // bigger problems), so just write zeros instead.
Benjamin Kramer97fbdd52015-04-17 11:12:43 +0000397 OW->WriteZeros(Count);
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000398 return true;
399}
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000400
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000401/// processFixupValue - Target hook to process the literal value of a fixup
402/// if necessary.
403void MipsAsmBackend::processFixupValue(const MCAssembler &Asm,
404 const MCAsmLayout &Layout,
405 const MCFixup &Fixup,
406 const MCFragment *DF,
Rafael Espindola3e3de5e2014-03-28 16:06:09 +0000407 const MCValue &Target,
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000408 uint64_t &Value,
409 bool &IsResolved) {
410 // At this point we'll ignore the value returned by adjustFixupValue as
411 // we are only checking if the fixup can be applied correctly. We have
412 // access to MCContext from here which allows us to report a fatal error
413 // with *possibly* a source code location.
414 (void)adjustFixupValue(Fixup, Value, &Asm.getContext());
415}
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000416
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000417// MCAsmBackend
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000418MCAsmBackend *llvm::createMipsAsmBackendEL32(const Target &T,
419 const MCRegisterInfo &MRI,
Daniel Sanders418caf52015-06-10 10:35:34 +0000420 const Triple &TT, StringRef CPU) {
421 return new MipsAsmBackend(T, TT.getOS(), /*IsLittle*/ true,
422 /*Is64Bit*/ false);
Rafael Espindola647841b2012-01-11 04:04:14 +0000423}
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000424
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000425MCAsmBackend *llvm::createMipsAsmBackendEB32(const Target &T,
426 const MCRegisterInfo &MRI,
Daniel Sanders418caf52015-06-10 10:35:34 +0000427 const Triple &TT, StringRef CPU) {
428 return new MipsAsmBackend(T, TT.getOS(), /*IsLittle*/ false,
429 /*Is64Bit*/ false);
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000430}
Akira Hatanakab1f68f92012-04-02 19:25:22 +0000431
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000432MCAsmBackend *llvm::createMipsAsmBackendEL64(const Target &T,
433 const MCRegisterInfo &MRI,
Daniel Sanders418caf52015-06-10 10:35:34 +0000434 const Triple &TT, StringRef CPU) {
435 return new MipsAsmBackend(T, TT.getOS(), /*IsLittle*/ true, /*Is64Bit*/ true);
Akira Hatanakab1f68f92012-04-02 19:25:22 +0000436}
437
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000438MCAsmBackend *llvm::createMipsAsmBackendEB64(const Target &T,
439 const MCRegisterInfo &MRI,
Daniel Sanders418caf52015-06-10 10:35:34 +0000440 const Triple &TT, StringRef CPU) {
441 return new MipsAsmBackend(T, TT.getOS(), /*IsLittle*/ false,
442 /*Is64Bit*/ true);
Akira Hatanakab1f68f92012-04-02 19:25:22 +0000443}