blob: 0f99ecc5484f74ea54446bb66d6f07e15f26c739 [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,
34 MCContext *Ctx = NULL) {
35
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:
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000059 break;
60 case Mips::fixup_Mips_PC16:
61 // So far we are only using this type for branches.
62 // For branches we start 1 instruction after the branch
63 // so the displacement will be one instruction size less.
64 Value -= 4;
65 // The displacement is then divided by 4 to give us an 18 bit
Matheus Almeidae0d75aa2013-12-13 11:11:02 +000066 // address range. Forcing a signed division because Value can be negative.
67 Value = (int64_t)Value / 4;
68 // We now check if Value can be encoded as a 16-bit signed immediate.
Matheus Almeida8cc8b352013-12-17 17:10:00 +000069 if (!isIntN(16, Value) && Ctx)
Matheus Almeidae0d75aa2013-12-13 11:11:02 +000070 Ctx->FatalError(Fixup.getLoc(), "out of range PC16 fixup");
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +000071 break;
72 case Mips::fixup_Mips_26:
73 // So far we are only using this type for jumps.
74 // The displacement is then divided by 4 to give us an 28 bit
75 // address range.
76 Value >>= 2;
77 break;
Akira Hatanakaf5ddf132011-11-23 22:18:04 +000078 case Mips::fixup_Mips_HI16:
Bruno Cardoso Lopes61e6d982011-12-07 00:28:57 +000079 case Mips::fixup_Mips_GOT_Local:
Jack Carterb05cb672012-11-21 23:38:59 +000080 case Mips::fixup_Mips_GOT_HI16:
81 case Mips::fixup_Mips_CALL_HI16:
Zoran Jovanovice7ae8af2013-10-23 16:14:44 +000082 case Mips::fixup_MICROMIPS_HI16:
Jack Carter84491ab2012-08-06 21:26:03 +000083 // Get the 2nd 16-bits. Also add 1 if bit 15 is 1.
Akira Hatanakada728192012-03-27 01:50:08 +000084 Value = ((Value + 0x8000) >> 16) & 0xffff;
Akira Hatanakaf5ddf132011-11-23 22:18:04 +000085 break;
Jack Carter84491ab2012-08-06 21:26:03 +000086 case Mips::fixup_Mips_HIGHER:
87 // Get the 3rd 16-bits.
88 Value = ((Value + 0x80008000LL) >> 32) & 0xffff;
89 break;
90 case Mips::fixup_Mips_HIGHEST:
91 // Get the 4th 16-bits.
92 Value = ((Value + 0x800080008000LL) >> 48) & 0xffff;
93 break;
Zoran Jovanovic507e0842013-10-29 16:38:59 +000094 case Mips::fixup_MICROMIPS_26_S1:
95 Value >>= 1;
96 break;
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +000097 case Mips::fixup_MICROMIPS_PC16_S1:
98 Value -= 4;
Matheus Almeidae0d75aa2013-12-13 11:11:02 +000099 // Forcing a signed division because Value can be negative.
100 Value = (int64_t)Value / 2;
101 // We now check if Value can be encoded as a 16-bit signed immediate.
Matheus Almeida8cc8b352013-12-17 17:10:00 +0000102 if (!isIntN(16, Value) && Ctx)
Matheus Almeidae0d75aa2013-12-13 11:11:02 +0000103 Ctx->FatalError(Fixup.getLoc(), "out of range PC16 fixup");
Zoran Jovanovic8a80aa72013-11-04 14:53:22 +0000104 break;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000105 }
106
107 return Value;
108}
109
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000110MCObjectWriter *MipsAsmBackend::createObjectWriter(raw_ostream &OS) const {
111 return createMipsELFObjectWriter(OS,
112 MCELFObjectTargetWriter::getOSABI(OSType), IsLittle, Is64Bit);
113}
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000114
Zoran Jovanovic842f20e2014-04-03 12:01:01 +0000115// Little-endian fixup data byte ordering:
116// mips32r2: a | b | x | x
117// microMIPS: x | x | a | b
118
119static bool needsMMLEByteOrder(unsigned Kind) {
120 return Kind >= Mips::fixup_MICROMIPS_26_S1 &&
121 Kind < Mips::LastTargetFixupKind;
122}
123
124// Calculate index for microMIPS specific little endian byte order
125static unsigned calculateMMLEIndex(unsigned i) {
126 assert(i <= 3 && "Index out of range!");
127
128 return (1 - i / 2) * 2 + i % 2;
129}
130
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000131/// ApplyFixup - Apply the \p Value for given \p Fixup into the provided
132/// data fragment, at the offset specified by the fixup and following the
133/// fixup kind as appropriate.
134void MipsAsmBackend::applyFixup(const MCFixup &Fixup, char *Data,
Rafael Espindola5904e122014-03-29 06:26:49 +0000135 unsigned DataSize, uint64_t Value,
136 bool IsPCRel) const {
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000137 MCFixupKind Kind = Fixup.getKind();
138 Value = adjustFixupValue(Fixup, Value);
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000139
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000140 if (!Value)
141 return; // Doesn't change encoding.
142
143 // Where do we start in the object
144 unsigned Offset = Fixup.getOffset();
145 // Number of bytes we need to fixup
146 unsigned NumBytes = (getFixupKindInfo(Kind).TargetSize + 7) / 8;
147 // Used to point to big endian bytes
148 unsigned FullSize;
149
150 switch ((unsigned)Kind) {
151 case FK_Data_2:
152 case Mips::fixup_Mips_16:
153 FullSize = 2;
154 break;
155 case FK_Data_8:
156 case Mips::fixup_Mips_64:
157 FullSize = 8;
158 break;
159 case FK_Data_4:
160 default:
161 FullSize = 4;
162 break;
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000163 }
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000164
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000165 // Grab current value, if any, from bits.
166 uint64_t CurVal = 0;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000167
Zoran Jovanovic842f20e2014-04-03 12:01:01 +0000168 bool microMipsLEByteOrder = needsMMLEByteOrder((unsigned) Kind);
169
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000170 for (unsigned i = 0; i != NumBytes; ++i) {
Zoran Jovanovic842f20e2014-04-03 12:01:01 +0000171 unsigned Idx = IsLittle ? (microMipsLEByteOrder ? calculateMMLEIndex(i)
172 : i)
173 : (FullSize - 1 - i);
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000174 CurVal |= (uint64_t)((uint8_t)Data[Offset + Idx]) << (i*8);
Akira Hatanaka0137dfe2012-03-21 00:52:01 +0000175 }
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000176
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000177 uint64_t Mask = ((uint64_t)(-1) >>
178 (64 - getFixupKindInfo(Kind).TargetSize));
179 CurVal |= Value & Mask;
Bruno Cardoso Lopesc85e3ff2011-11-11 22:58:42 +0000180
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000181 // Write out the fixed up bytes back to the code/data bits.
182 for (unsigned i = 0; i != NumBytes; ++i) {
Zoran Jovanovic842f20e2014-04-03 12:01:01 +0000183 unsigned Idx = IsLittle ? (microMipsLEByteOrder ? calculateMMLEIndex(i)
184 : i)
185 : (FullSize - 1 - i);
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000186 Data[Offset + Idx] = (uint8_t)((CurVal >> (i*8)) & 0xff);
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000187 }
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000188}
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000189
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000190const MCFixupKindInfo &MipsAsmBackend::
191getFixupKindInfo(MCFixupKind Kind) const {
192 const static MCFixupKindInfo Infos[Mips::NumTargetFixupKinds] = {
193 // This table *must* be in same the order of fixup_* kinds in
194 // MipsFixupKinds.h.
195 //
196 // name offset bits flags
197 { "fixup_Mips_16", 0, 16, 0 },
198 { "fixup_Mips_32", 0, 32, 0 },
199 { "fixup_Mips_REL32", 0, 32, 0 },
200 { "fixup_Mips_26", 0, 26, 0 },
201 { "fixup_Mips_HI16", 0, 16, 0 },
202 { "fixup_Mips_LO16", 0, 16, 0 },
203 { "fixup_Mips_GPREL16", 0, 16, 0 },
204 { "fixup_Mips_LITERAL", 0, 16, 0 },
205 { "fixup_Mips_GOT_Global", 0, 16, 0 },
206 { "fixup_Mips_GOT_Local", 0, 16, 0 },
207 { "fixup_Mips_PC16", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
208 { "fixup_Mips_CALL16", 0, 16, 0 },
209 { "fixup_Mips_GPREL32", 0, 32, 0 },
210 { "fixup_Mips_SHIFT5", 6, 5, 0 },
211 { "fixup_Mips_SHIFT6", 6, 5, 0 },
212 { "fixup_Mips_64", 0, 64, 0 },
213 { "fixup_Mips_TLSGD", 0, 16, 0 },
214 { "fixup_Mips_GOTTPREL", 0, 16, 0 },
215 { "fixup_Mips_TPREL_HI", 0, 16, 0 },
216 { "fixup_Mips_TPREL_LO", 0, 16, 0 },
217 { "fixup_Mips_TLSLDM", 0, 16, 0 },
218 { "fixup_Mips_DTPREL_HI", 0, 16, 0 },
219 { "fixup_Mips_DTPREL_LO", 0, 16, 0 },
220 { "fixup_Mips_Branch_PCRel", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
221 { "fixup_Mips_GPOFF_HI", 0, 16, 0 },
222 { "fixup_Mips_GPOFF_LO", 0, 16, 0 },
223 { "fixup_Mips_GOT_PAGE", 0, 16, 0 },
224 { "fixup_Mips_GOT_OFST", 0, 16, 0 },
225 { "fixup_Mips_GOT_DISP", 0, 16, 0 },
226 { "fixup_Mips_HIGHER", 0, 16, 0 },
227 { "fixup_Mips_HIGHEST", 0, 16, 0 },
228 { "fixup_Mips_GOT_HI16", 0, 16, 0 },
229 { "fixup_Mips_GOT_LO16", 0, 16, 0 },
230 { "fixup_Mips_CALL_HI16", 0, 16, 0 },
231 { "fixup_Mips_CALL_LO16", 0, 16, 0 },
232 { "fixup_MICROMIPS_26_S1", 0, 26, 0 },
233 { "fixup_MICROMIPS_HI16", 0, 16, 0 },
234 { "fixup_MICROMIPS_LO16", 0, 16, 0 },
235 { "fixup_MICROMIPS_GOT16", 0, 16, 0 },
236 { "fixup_MICROMIPS_PC16_S1", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
237 { "fixup_MICROMIPS_CALL16", 0, 16, 0 },
238 { "fixup_MICROMIPS_GOT_DISP", 0, 16, 0 },
239 { "fixup_MICROMIPS_GOT_PAGE", 0, 16, 0 },
240 { "fixup_MICROMIPS_GOT_OFST", 0, 16, 0 },
241 { "fixup_MICROMIPS_TLS_GD", 0, 16, 0 },
242 { "fixup_MICROMIPS_TLS_LDM", 0, 16, 0 },
243 { "fixup_MICROMIPS_TLS_DTPREL_HI16", 0, 16, 0 },
244 { "fixup_MICROMIPS_TLS_DTPREL_LO16", 0, 16, 0 },
245 { "fixup_MICROMIPS_TLS_TPREL_HI16", 0, 16, 0 },
246 { "fixup_MICROMIPS_TLS_TPREL_LO16", 0, 16, 0 }
247 };
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000248
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000249 if (Kind < FirstTargetFixupKind)
250 return MCAsmBackend::getFixupKindInfo(Kind);
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000251
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000252 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
253 "Invalid kind!");
254 return Infos[Kind - FirstTargetFixupKind];
255}
Jim Grosbach25b63fa2011-12-06 00:47:03 +0000256
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000257/// WriteNopData - Write an (optimal) nop sequence of Count bytes
258/// to the given output. If the target cannot generate such a sequence,
259/// it should return an error.
260///
261/// \return - True on success.
262bool MipsAsmBackend::writeNopData(uint64_t Count, MCObjectWriter *OW) const {
263 // Check for a less than instruction size number of bytes
264 // FIXME: 16 bit instructions are not handled yet here.
265 // We shouldn't be using a hard coded number for instruction size.
266 if (Count % 4) return false;
Jia Liuf54f60f2012-02-28 07:46:26 +0000267
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000268 uint64_t NumNops = Count / 4;
269 for (uint64_t i = 0; i != NumNops; ++i)
270 OW->Write32(0);
271 return true;
272}
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000273
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000274/// processFixupValue - Target hook to process the literal value of a fixup
275/// if necessary.
276void MipsAsmBackend::processFixupValue(const MCAssembler &Asm,
277 const MCAsmLayout &Layout,
278 const MCFixup &Fixup,
279 const MCFragment *DF,
Rafael Espindola3e3de5e2014-03-28 16:06:09 +0000280 const MCValue &Target,
Zoran Jovanovicada38ef2014-03-27 12:38:40 +0000281 uint64_t &Value,
282 bool &IsResolved) {
283 // At this point we'll ignore the value returned by adjustFixupValue as
284 // we are only checking if the fixup can be applied correctly. We have
285 // access to MCContext from here which allows us to report a fatal error
286 // with *possibly* a source code location.
287 (void)adjustFixupValue(Fixup, Value, &Asm.getContext());
288}
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000289
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000290// MCAsmBackend
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000291MCAsmBackend *llvm::createMipsAsmBackendEL32(const Target &T,
292 const MCRegisterInfo &MRI,
293 StringRef TT,
Roman Divacky5dd4ccb2012-09-18 16:08:49 +0000294 StringRef CPU) {
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000295 return new MipsAsmBackend(T, Triple(TT).getOS(),
Akira Hatanakab1f68f92012-04-02 19:25:22 +0000296 /*IsLittle*/true, /*Is64Bit*/false);
Rafael Espindola647841b2012-01-11 04:04:14 +0000297}
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000298
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000299MCAsmBackend *llvm::createMipsAsmBackendEB32(const Target &T,
300 const MCRegisterInfo &MRI,
301 StringRef TT,
Roman Divacky5dd4ccb2012-09-18 16:08:49 +0000302 StringRef CPU) {
Akira Hatanaka1ee768d2012-03-01 01:53:15 +0000303 return new MipsAsmBackend(T, Triple(TT).getOS(),
Akira Hatanakab1f68f92012-04-02 19:25:22 +0000304 /*IsLittle*/false, /*Is64Bit*/false);
Akira Hatanaka44220ca2011-09-30 21:23:45 +0000305}
Akira Hatanakab1f68f92012-04-02 19:25:22 +0000306
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000307MCAsmBackend *llvm::createMipsAsmBackendEL64(const Target &T,
308 const MCRegisterInfo &MRI,
309 StringRef TT,
Roman Divacky5dd4ccb2012-09-18 16:08:49 +0000310 StringRef CPU) {
Akira Hatanakab1f68f92012-04-02 19:25:22 +0000311 return new MipsAsmBackend(T, Triple(TT).getOS(),
312 /*IsLittle*/true, /*Is64Bit*/true);
313}
314
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000315MCAsmBackend *llvm::createMipsAsmBackendEB64(const Target &T,
316 const MCRegisterInfo &MRI,
317 StringRef TT,
Roman Divacky5dd4ccb2012-09-18 16:08:49 +0000318 StringRef CPU) {
Akira Hatanakab1f68f92012-04-02 19:25:22 +0000319 return new MipsAsmBackend(T, Triple(TT).getOS(),
320 /*IsLittle*/false, /*Is64Bit*/true);
321}