Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 1 | //===-- MipsAsmBackend.cpp - Mips Asm Backend ----------------------------===// |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 2 | // |
| 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 Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 10 | // This file implements the MipsAsmBackend class. |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | // |
| 14 | |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 15 | #include "MCTargetDesc/MipsFixupKinds.h" |
| 16 | #include "MCTargetDesc/MipsAsmBackend.h" |
Akira Hatanaka | 587fe6c | 2011-09-30 21:04:02 +0000 | [diff] [blame] | 17 | #include "MCTargetDesc/MipsMCTargetDesc.h" |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCAsmBackend.h" |
Akira Hatanaka | 587fe6c | 2011-09-30 21:04:02 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCAssembler.h" |
Matheus Almeida | e0d75aa | 2013-12-13 11:11:02 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCContext.h" |
Akira Hatanaka | 587fe6c | 2011-09-30 21:04:02 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCDirectives.h" |
| 22 | #include "llvm/MC/MCELFObjectWriter.h" |
Craig Topper | 6e80c28 | 2012-03-26 06:58:25 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCFixupKindInfo.h" |
Akira Hatanaka | 587fe6c | 2011-09-30 21:04:02 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCObjectWriter.h" |
Akira Hatanaka | 587fe6c | 2011-09-30 21:04:02 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCSubtargetInfo.h" |
Akira Hatanaka | 587fe6c | 2011-09-30 21:04:02 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ErrorHandling.h" |
Matheus Almeida | e0d75aa | 2013-12-13 11:11:02 +0000 | [diff] [blame] | 27 | #include "llvm/Support/MathExtras.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
Bruno Cardoso Lopes | 61e6d98 | 2011-12-07 00:28:57 +0000 | [diff] [blame] | 29 | |
Akira Hatanaka | 587fe6c | 2011-09-30 21:04:02 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | |
Bruno Cardoso Lopes | 61e6d98 | 2011-12-07 00:28:57 +0000 | [diff] [blame] | 32 | // Prepare value for the target space for it |
Matheus Almeida | e0d75aa | 2013-12-13 11:11:02 +0000 | [diff] [blame] | 33 | static unsigned adjustFixupValue(const MCFixup &Fixup, uint64_t Value, |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 34 | MCContext *Ctx = nullptr) { |
Matheus Almeida | e0d75aa | 2013-12-13 11:11:02 +0000 | [diff] [blame] | 35 | |
| 36 | unsigned Kind = Fixup.getKind(); |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 37 | |
| 38 | // Add/subtract and shift |
| 39 | switch (Kind) { |
| 40 | default: |
Bruno Cardoso Lopes | 61e6d98 | 2011-12-07 00:28:57 +0000 | [diff] [blame] | 41 | return 0; |
Ed Maste | 2a710d0 | 2014-03-03 14:27:49 +0000 | [diff] [blame] | 42 | case FK_Data_2: |
Bruno Cardoso Lopes | 61e6d98 | 2011-12-07 00:28:57 +0000 | [diff] [blame] | 43 | case FK_GPRel_4: |
| 44 | case FK_Data_4: |
Jack Carter | 4c58381 | 2012-08-07 00:01:14 +0000 | [diff] [blame] | 45 | case FK_Data_8: |
Bruno Cardoso Lopes | 61e6d98 | 2011-12-07 00:28:57 +0000 | [diff] [blame] | 46 | case Mips::fixup_Mips_LO16: |
Jack Carter | c3dd91c | 2013-01-08 19:01:28 +0000 | [diff] [blame] | 47 | case Mips::fixup_Mips_GPREL16: |
Jack Carter | b9f9de9 | 2012-06-27 22:48:25 +0000 | [diff] [blame] | 48 | 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 Carter | 5ddcfda | 2012-07-13 19:15:47 +0000 | [diff] [blame] | 52 | case Mips::fixup_Mips_GOT_DISP: |
Jack Carter | b05cb67 | 2012-11-21 23:38:59 +0000 | [diff] [blame] | 53 | case Mips::fixup_Mips_GOT_LO16: |
| 54 | case Mips::fixup_Mips_CALL_LO16: |
Zoran Jovanovic | e7ae8af | 2013-10-23 16:14:44 +0000 | [diff] [blame] | 55 | 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 Jovanovic | b355e8f | 2014-05-27 14:58:51 +0000 | [diff] [blame] | 59 | case Mips::fixup_MIPS_PCLO16: |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 60 | break; |
| 61 | case Mips::fixup_Mips_PC16: |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 62 | // The displacement is then divided by 4 to give us an 18 bit |
Matheus Almeida | e0d75aa | 2013-12-13 11:11:02 +0000 | [diff] [blame] | 63 | // address range. Forcing a signed division because Value can be negative. |
| 64 | Value = (int64_t)Value / 4; |
| 65 | // We now check if Value can be encoded as a 16-bit signed immediate. |
Craig Topper | 55b1f29 | 2015-10-10 20:17:07 +0000 | [diff] [blame] | 66 | if (!isInt<16>(Value) && Ctx) |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 67 | Ctx->reportFatalError(Fixup.getLoc(), "out of range PC16 fixup"); |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 68 | break; |
Zoran Jovanovic | b9c07f3 | 2014-06-12 12:40:00 +0000 | [diff] [blame] | 69 | case Mips::fixup_MIPS_PC19_S2: |
| 70 | // 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 19-bit signed immediate. |
Craig Topper | 55b1f29 | 2015-10-10 20:17:07 +0000 | [diff] [blame] | 73 | if (!isInt<19>(Value) && Ctx) |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 74 | Ctx->reportFatalError(Fixup.getLoc(), "out of range PC19 fixup"); |
Zoran Jovanovic | b9c07f3 | 2014-06-12 12:40:00 +0000 | [diff] [blame] | 75 | break; |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 76 | case Mips::fixup_Mips_26: |
| 77 | // So far we are only using this type for jumps. |
| 78 | // The displacement is then divided by 4 to give us an 28 bit |
| 79 | // address range. |
| 80 | Value >>= 2; |
| 81 | break; |
Akira Hatanaka | f5ddf13 | 2011-11-23 22:18:04 +0000 | [diff] [blame] | 82 | case Mips::fixup_Mips_HI16: |
Bruno Cardoso Lopes | 61e6d98 | 2011-12-07 00:28:57 +0000 | [diff] [blame] | 83 | case Mips::fixup_Mips_GOT_Local: |
Jack Carter | b05cb67 | 2012-11-21 23:38:59 +0000 | [diff] [blame] | 84 | case Mips::fixup_Mips_GOT_HI16: |
| 85 | case Mips::fixup_Mips_CALL_HI16: |
Zoran Jovanovic | e7ae8af | 2013-10-23 16:14:44 +0000 | [diff] [blame] | 86 | case Mips::fixup_MICROMIPS_HI16: |
Zoran Jovanovic | b355e8f | 2014-05-27 14:58:51 +0000 | [diff] [blame] | 87 | case Mips::fixup_MIPS_PCHI16: |
Jack Carter | 84491ab | 2012-08-06 21:26:03 +0000 | [diff] [blame] | 88 | // Get the 2nd 16-bits. Also add 1 if bit 15 is 1. |
Akira Hatanaka | da72819 | 2012-03-27 01:50:08 +0000 | [diff] [blame] | 89 | Value = ((Value + 0x8000) >> 16) & 0xffff; |
Akira Hatanaka | f5ddf13 | 2011-11-23 22:18:04 +0000 | [diff] [blame] | 90 | break; |
Jack Carter | 84491ab | 2012-08-06 21:26:03 +0000 | [diff] [blame] | 91 | case Mips::fixup_Mips_HIGHER: |
| 92 | // Get the 3rd 16-bits. |
| 93 | Value = ((Value + 0x80008000LL) >> 32) & 0xffff; |
| 94 | break; |
| 95 | case Mips::fixup_Mips_HIGHEST: |
| 96 | // Get the 4th 16-bits. |
| 97 | Value = ((Value + 0x800080008000LL) >> 48) & 0xffff; |
| 98 | break; |
Zoran Jovanovic | 507e084 | 2013-10-29 16:38:59 +0000 | [diff] [blame] | 99 | case Mips::fixup_MICROMIPS_26_S1: |
| 100 | Value >>= 1; |
| 101 | break; |
Jozef Kolek | 9761e96 | 2015-01-12 12:03:34 +0000 | [diff] [blame] | 102 | case Mips::fixup_MICROMIPS_PC7_S1: |
| 103 | Value -= 4; |
| 104 | // Forcing a signed division because Value can be negative. |
| 105 | Value = (int64_t) Value / 2; |
| 106 | // We now check if Value can be encoded as a 7-bit signed immediate. |
Craig Topper | 55b1f29 | 2015-10-10 20:17:07 +0000 | [diff] [blame] | 107 | if (!isInt<7>(Value) && Ctx) |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 108 | Ctx->reportFatalError(Fixup.getLoc(), "out of range PC7 fixup"); |
Jozef Kolek | 9761e96 | 2015-01-12 12:03:34 +0000 | [diff] [blame] | 109 | break; |
Jozef Kolek | 5cfebdd | 2015-01-21 12:39:30 +0000 | [diff] [blame] | 110 | case Mips::fixup_MICROMIPS_PC10_S1: |
| 111 | Value -= 2; |
| 112 | // Forcing a signed division because Value can be negative. |
| 113 | Value = (int64_t) Value / 2; |
| 114 | // We now check if Value can be encoded as a 10-bit signed immediate. |
Craig Topper | 55b1f29 | 2015-10-10 20:17:07 +0000 | [diff] [blame] | 115 | if (!isInt<10>(Value) && Ctx) |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 116 | Ctx->reportFatalError(Fixup.getLoc(), "out of range PC10 fixup"); |
Jozef Kolek | 5cfebdd | 2015-01-21 12:39:30 +0000 | [diff] [blame] | 117 | break; |
Zoran Jovanovic | 8a80aa7 | 2013-11-04 14:53:22 +0000 | [diff] [blame] | 118 | case Mips::fixup_MICROMIPS_PC16_S1: |
| 119 | Value -= 4; |
Matheus Almeida | e0d75aa | 2013-12-13 11:11:02 +0000 | [diff] [blame] | 120 | // Forcing a signed division because Value can be negative. |
| 121 | Value = (int64_t)Value / 2; |
| 122 | // We now check if Value can be encoded as a 16-bit signed immediate. |
Craig Topper | 55b1f29 | 2015-10-10 20:17:07 +0000 | [diff] [blame] | 123 | if (!isInt<16>(Value) && Ctx) |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 124 | Ctx->reportFatalError(Fixup.getLoc(), "out of range PC16 fixup"); |
Zoran Jovanovic | 8a80aa7 | 2013-11-04 14:53:22 +0000 | [diff] [blame] | 125 | break; |
Zoran Jovanovic | a5acdcf | 2014-06-13 14:26:47 +0000 | [diff] [blame] | 126 | case Mips::fixup_MIPS_PC18_S3: |
| 127 | // Forcing a signed division because Value can be negative. |
| 128 | Value = (int64_t)Value / 8; |
| 129 | // We now check if Value can be encoded as a 18-bit signed immediate. |
Craig Topper | 55b1f29 | 2015-10-10 20:17:07 +0000 | [diff] [blame] | 130 | if (!isInt<18>(Value) && Ctx) |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 131 | Ctx->reportFatalError(Fixup.getLoc(), "out of range PC18 fixup"); |
Zoran Jovanovic | a5acdcf | 2014-06-13 14:26:47 +0000 | [diff] [blame] | 132 | break; |
Zoran Jovanovic | 10e06da | 2014-05-27 12:55:40 +0000 | [diff] [blame] | 133 | case Mips::fixup_MIPS_PC21_S2: |
Zoran Jovanovic | 10e06da | 2014-05-27 12:55:40 +0000 | [diff] [blame] | 134 | // Forcing a signed division because Value can be negative. |
| 135 | Value = (int64_t) Value / 4; |
| 136 | // We now check if Value can be encoded as a 21-bit signed immediate. |
Craig Topper | 55b1f29 | 2015-10-10 20:17:07 +0000 | [diff] [blame] | 137 | if (!isInt<21>(Value) && Ctx) |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 138 | Ctx->reportFatalError(Fixup.getLoc(), "out of range PC21 fixup"); |
Zoran Jovanovic | 10e06da | 2014-05-27 12:55:40 +0000 | [diff] [blame] | 139 | break; |
| 140 | case Mips::fixup_MIPS_PC26_S2: |
Zoran Jovanovic | 10e06da | 2014-05-27 12:55:40 +0000 | [diff] [blame] | 141 | // Forcing a signed division because Value can be negative. |
| 142 | Value = (int64_t) Value / 4; |
| 143 | // We now check if Value can be encoded as a 26-bit signed immediate. |
Craig Topper | 55b1f29 | 2015-10-10 20:17:07 +0000 | [diff] [blame] | 144 | if (!isInt<26>(Value) && Ctx) |
Jim Grosbach | 6f48200 | 2015-05-18 18:43:14 +0000 | [diff] [blame] | 145 | Ctx->reportFatalError(Fixup.getLoc(), "out of range PC26 fixup"); |
Zoran Jovanovic | 10e06da | 2014-05-27 12:55:40 +0000 | [diff] [blame] | 146 | break; |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | return Value; |
| 150 | } |
| 151 | |
Rafael Espindola | 5560a4c | 2015-04-14 22:14:34 +0000 | [diff] [blame] | 152 | MCObjectWriter * |
| 153 | MipsAsmBackend::createObjectWriter(raw_pwrite_stream &OS) const { |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 154 | return createMipsELFObjectWriter(OS, |
| 155 | MCELFObjectTargetWriter::getOSABI(OSType), IsLittle, Is64Bit); |
| 156 | } |
Akira Hatanaka | 1ee768d | 2012-03-01 01:53:15 +0000 | [diff] [blame] | 157 | |
Zoran Jovanovic | 842f20e | 2014-04-03 12:01:01 +0000 | [diff] [blame] | 158 | // Little-endian fixup data byte ordering: |
| 159 | // mips32r2: a | b | x | x |
| 160 | // microMIPS: x | x | a | b |
| 161 | |
| 162 | static bool needsMMLEByteOrder(unsigned Kind) { |
Jozef Kolek | 5cfebdd | 2015-01-21 12:39:30 +0000 | [diff] [blame] | 163 | return Kind != Mips::fixup_MICROMIPS_PC10_S1 && |
| 164 | Kind >= Mips::fixup_MICROMIPS_26_S1 && |
Zoran Jovanovic | 842f20e | 2014-04-03 12:01:01 +0000 | [diff] [blame] | 165 | Kind < Mips::LastTargetFixupKind; |
| 166 | } |
| 167 | |
| 168 | // Calculate index for microMIPS specific little endian byte order |
| 169 | static unsigned calculateMMLEIndex(unsigned i) { |
| 170 | assert(i <= 3 && "Index out of range!"); |
| 171 | |
| 172 | return (1 - i / 2) * 2 + i % 2; |
| 173 | } |
| 174 | |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 175 | /// ApplyFixup - Apply the \p Value for given \p Fixup into the provided |
| 176 | /// data fragment, at the offset specified by the fixup and following the |
| 177 | /// fixup kind as appropriate. |
| 178 | void MipsAsmBackend::applyFixup(const MCFixup &Fixup, char *Data, |
Rafael Espindola | 5904e12 | 2014-03-29 06:26:49 +0000 | [diff] [blame] | 179 | unsigned DataSize, uint64_t Value, |
| 180 | bool IsPCRel) const { |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 181 | MCFixupKind Kind = Fixup.getKind(); |
| 182 | Value = adjustFixupValue(Fixup, Value); |
Akira Hatanaka | 1ee768d | 2012-03-01 01:53:15 +0000 | [diff] [blame] | 183 | |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 184 | if (!Value) |
| 185 | return; // Doesn't change encoding. |
| 186 | |
| 187 | // Where do we start in the object |
| 188 | unsigned Offset = Fixup.getOffset(); |
| 189 | // Number of bytes we need to fixup |
| 190 | unsigned NumBytes = (getFixupKindInfo(Kind).TargetSize + 7) / 8; |
| 191 | // Used to point to big endian bytes |
| 192 | unsigned FullSize; |
| 193 | |
| 194 | switch ((unsigned)Kind) { |
| 195 | case FK_Data_2: |
| 196 | case Mips::fixup_Mips_16: |
Jozef Kolek | 5cfebdd | 2015-01-21 12:39:30 +0000 | [diff] [blame] | 197 | case Mips::fixup_MICROMIPS_PC10_S1: |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 198 | FullSize = 2; |
| 199 | break; |
| 200 | case FK_Data_8: |
| 201 | case Mips::fixup_Mips_64: |
| 202 | FullSize = 8; |
| 203 | break; |
| 204 | case FK_Data_4: |
| 205 | default: |
| 206 | FullSize = 4; |
| 207 | break; |
Akira Hatanaka | 1ee768d | 2012-03-01 01:53:15 +0000 | [diff] [blame] | 208 | } |
Akira Hatanaka | 44220ca | 2011-09-30 21:23:45 +0000 | [diff] [blame] | 209 | |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 210 | // Grab current value, if any, from bits. |
| 211 | uint64_t CurVal = 0; |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 212 | |
Zoran Jovanovic | 842f20e | 2014-04-03 12:01:01 +0000 | [diff] [blame] | 213 | bool microMipsLEByteOrder = needsMMLEByteOrder((unsigned) Kind); |
| 214 | |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 215 | for (unsigned i = 0; i != NumBytes; ++i) { |
Zoran Jovanovic | 842f20e | 2014-04-03 12:01:01 +0000 | [diff] [blame] | 216 | unsigned Idx = IsLittle ? (microMipsLEByteOrder ? calculateMMLEIndex(i) |
| 217 | : i) |
| 218 | : (FullSize - 1 - i); |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 219 | CurVal |= (uint64_t)((uint8_t)Data[Offset + Idx]) << (i*8); |
Akira Hatanaka | 0137dfe | 2012-03-21 00:52:01 +0000 | [diff] [blame] | 220 | } |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 221 | |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 222 | uint64_t Mask = ((uint64_t)(-1) >> |
| 223 | (64 - getFixupKindInfo(Kind).TargetSize)); |
| 224 | CurVal |= Value & Mask; |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 225 | |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 226 | // Write out the fixed up bytes back to the code/data bits. |
| 227 | for (unsigned i = 0; i != NumBytes; ++i) { |
Zoran Jovanovic | 842f20e | 2014-04-03 12:01:01 +0000 | [diff] [blame] | 228 | unsigned Idx = IsLittle ? (microMipsLEByteOrder ? calculateMMLEIndex(i) |
| 229 | : i) |
| 230 | : (FullSize - 1 - i); |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 231 | Data[Offset + Idx] = (uint8_t)((CurVal >> (i*8)) & 0xff); |
Akira Hatanaka | 44220ca | 2011-09-30 21:23:45 +0000 | [diff] [blame] | 232 | } |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 233 | } |
Akira Hatanaka | 44220ca | 2011-09-30 21:23:45 +0000 | [diff] [blame] | 234 | |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame^] | 235 | bool MipsAsmBackend::getFixupKind(StringRef Name, MCFixupKind &MappedKind) const { |
| 236 | if (Name == "R_MIPS_NONE") { |
| 237 | MappedKind = (MCFixupKind)Mips::fixup_Mips_NONE; |
| 238 | return true; |
| 239 | } |
| 240 | if (Name == "R_MIPS_32") { |
| 241 | MappedKind = FK_Data_4; |
| 242 | return true; |
| 243 | } |
| 244 | return MCAsmBackend::getFixupKind(Name, MappedKind); |
| 245 | } |
| 246 | |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 247 | const MCFixupKindInfo &MipsAsmBackend:: |
| 248 | getFixupKindInfo(MCFixupKind Kind) const { |
Daniel Sanders | 683ed96 | 2014-05-23 13:35:24 +0000 | [diff] [blame] | 249 | const static MCFixupKindInfo LittleEndianInfos[Mips::NumTargetFixupKinds] = { |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 250 | // This table *must* be in same the order of fixup_* kinds in |
| 251 | // MipsFixupKinds.h. |
| 252 | // |
| 253 | // name offset bits flags |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame^] | 254 | { "fixup_Mips_NONE", 0, 0, 0 }, |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 255 | { "fixup_Mips_16", 0, 16, 0 }, |
| 256 | { "fixup_Mips_32", 0, 32, 0 }, |
| 257 | { "fixup_Mips_REL32", 0, 32, 0 }, |
| 258 | { "fixup_Mips_26", 0, 26, 0 }, |
| 259 | { "fixup_Mips_HI16", 0, 16, 0 }, |
| 260 | { "fixup_Mips_LO16", 0, 16, 0 }, |
| 261 | { "fixup_Mips_GPREL16", 0, 16, 0 }, |
| 262 | { "fixup_Mips_LITERAL", 0, 16, 0 }, |
| 263 | { "fixup_Mips_GOT_Global", 0, 16, 0 }, |
| 264 | { "fixup_Mips_GOT_Local", 0, 16, 0 }, |
| 265 | { "fixup_Mips_PC16", 0, 16, MCFixupKindInfo::FKF_IsPCRel }, |
| 266 | { "fixup_Mips_CALL16", 0, 16, 0 }, |
| 267 | { "fixup_Mips_GPREL32", 0, 32, 0 }, |
| 268 | { "fixup_Mips_SHIFT5", 6, 5, 0 }, |
| 269 | { "fixup_Mips_SHIFT6", 6, 5, 0 }, |
| 270 | { "fixup_Mips_64", 0, 64, 0 }, |
| 271 | { "fixup_Mips_TLSGD", 0, 16, 0 }, |
| 272 | { "fixup_Mips_GOTTPREL", 0, 16, 0 }, |
| 273 | { "fixup_Mips_TPREL_HI", 0, 16, 0 }, |
| 274 | { "fixup_Mips_TPREL_LO", 0, 16, 0 }, |
| 275 | { "fixup_Mips_TLSLDM", 0, 16, 0 }, |
| 276 | { "fixup_Mips_DTPREL_HI", 0, 16, 0 }, |
| 277 | { "fixup_Mips_DTPREL_LO", 0, 16, 0 }, |
| 278 | { "fixup_Mips_Branch_PCRel", 0, 16, MCFixupKindInfo::FKF_IsPCRel }, |
| 279 | { "fixup_Mips_GPOFF_HI", 0, 16, 0 }, |
| 280 | { "fixup_Mips_GPOFF_LO", 0, 16, 0 }, |
| 281 | { "fixup_Mips_GOT_PAGE", 0, 16, 0 }, |
| 282 | { "fixup_Mips_GOT_OFST", 0, 16, 0 }, |
| 283 | { "fixup_Mips_GOT_DISP", 0, 16, 0 }, |
| 284 | { "fixup_Mips_HIGHER", 0, 16, 0 }, |
| 285 | { "fixup_Mips_HIGHEST", 0, 16, 0 }, |
| 286 | { "fixup_Mips_GOT_HI16", 0, 16, 0 }, |
| 287 | { "fixup_Mips_GOT_LO16", 0, 16, 0 }, |
| 288 | { "fixup_Mips_CALL_HI16", 0, 16, 0 }, |
| 289 | { "fixup_Mips_CALL_LO16", 0, 16, 0 }, |
Zoran Jovanovic | a5acdcf | 2014-06-13 14:26:47 +0000 | [diff] [blame] | 290 | { "fixup_Mips_PC18_S3", 0, 18, MCFixupKindInfo::FKF_IsPCRel }, |
Zoran Jovanovic | b9c07f3 | 2014-06-12 12:40:00 +0000 | [diff] [blame] | 291 | { "fixup_MIPS_PC19_S2", 0, 19, MCFixupKindInfo::FKF_IsPCRel }, |
Zoran Jovanovic | 10e06da | 2014-05-27 12:55:40 +0000 | [diff] [blame] | 292 | { "fixup_MIPS_PC21_S2", 0, 21, MCFixupKindInfo::FKF_IsPCRel }, |
| 293 | { "fixup_MIPS_PC26_S2", 0, 26, MCFixupKindInfo::FKF_IsPCRel }, |
Zoran Jovanovic | b355e8f | 2014-05-27 14:58:51 +0000 | [diff] [blame] | 294 | { "fixup_MIPS_PCHI16", 0, 16, MCFixupKindInfo::FKF_IsPCRel }, |
| 295 | { "fixup_MIPS_PCLO16", 0, 16, MCFixupKindInfo::FKF_IsPCRel }, |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 296 | { "fixup_MICROMIPS_26_S1", 0, 26, 0 }, |
| 297 | { "fixup_MICROMIPS_HI16", 0, 16, 0 }, |
| 298 | { "fixup_MICROMIPS_LO16", 0, 16, 0 }, |
| 299 | { "fixup_MICROMIPS_GOT16", 0, 16, 0 }, |
Jozef Kolek | 9761e96 | 2015-01-12 12:03:34 +0000 | [diff] [blame] | 300 | { "fixup_MICROMIPS_PC7_S1", 0, 7, MCFixupKindInfo::FKF_IsPCRel }, |
Jozef Kolek | 5cfebdd | 2015-01-21 12:39:30 +0000 | [diff] [blame] | 301 | { "fixup_MICROMIPS_PC10_S1", 0, 10, MCFixupKindInfo::FKF_IsPCRel }, |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 302 | { "fixup_MICROMIPS_PC16_S1", 0, 16, MCFixupKindInfo::FKF_IsPCRel }, |
| 303 | { "fixup_MICROMIPS_CALL16", 0, 16, 0 }, |
| 304 | { "fixup_MICROMIPS_GOT_DISP", 0, 16, 0 }, |
| 305 | { "fixup_MICROMIPS_GOT_PAGE", 0, 16, 0 }, |
| 306 | { "fixup_MICROMIPS_GOT_OFST", 0, 16, 0 }, |
| 307 | { "fixup_MICROMIPS_TLS_GD", 0, 16, 0 }, |
| 308 | { "fixup_MICROMIPS_TLS_LDM", 0, 16, 0 }, |
| 309 | { "fixup_MICROMIPS_TLS_DTPREL_HI16", 0, 16, 0 }, |
| 310 | { "fixup_MICROMIPS_TLS_DTPREL_LO16", 0, 16, 0 }, |
| 311 | { "fixup_MICROMIPS_TLS_TPREL_HI16", 0, 16, 0 }, |
| 312 | { "fixup_MICROMIPS_TLS_TPREL_LO16", 0, 16, 0 } |
| 313 | }; |
Akira Hatanaka | 44220ca | 2011-09-30 21:23:45 +0000 | [diff] [blame] | 314 | |
Daniel Sanders | 683ed96 | 2014-05-23 13:35:24 +0000 | [diff] [blame] | 315 | const static MCFixupKindInfo BigEndianInfos[Mips::NumTargetFixupKinds] = { |
| 316 | // This table *must* be in same the order of fixup_* kinds in |
| 317 | // MipsFixupKinds.h. |
| 318 | // |
| 319 | // name offset bits flags |
Daniel Sanders | 9f6ad49 | 2015-11-12 13:33:00 +0000 | [diff] [blame^] | 320 | { "fixup_Mips_NONE", 0, 0, 0 }, |
Daniel Sanders | 683ed96 | 2014-05-23 13:35:24 +0000 | [diff] [blame] | 321 | { "fixup_Mips_16", 16, 16, 0 }, |
| 322 | { "fixup_Mips_32", 0, 32, 0 }, |
| 323 | { "fixup_Mips_REL32", 0, 32, 0 }, |
| 324 | { "fixup_Mips_26", 6, 26, 0 }, |
| 325 | { "fixup_Mips_HI16", 16, 16, 0 }, |
| 326 | { "fixup_Mips_LO16", 16, 16, 0 }, |
| 327 | { "fixup_Mips_GPREL16", 16, 16, 0 }, |
| 328 | { "fixup_Mips_LITERAL", 16, 16, 0 }, |
| 329 | { "fixup_Mips_GOT_Global", 16, 16, 0 }, |
| 330 | { "fixup_Mips_GOT_Local", 16, 16, 0 }, |
| 331 | { "fixup_Mips_PC16", 16, 16, MCFixupKindInfo::FKF_IsPCRel }, |
| 332 | { "fixup_Mips_CALL16", 16, 16, 0 }, |
| 333 | { "fixup_Mips_GPREL32", 0, 32, 0 }, |
| 334 | { "fixup_Mips_SHIFT5", 21, 5, 0 }, |
| 335 | { "fixup_Mips_SHIFT6", 21, 5, 0 }, |
| 336 | { "fixup_Mips_64", 0, 64, 0 }, |
| 337 | { "fixup_Mips_TLSGD", 16, 16, 0 }, |
| 338 | { "fixup_Mips_GOTTPREL", 16, 16, 0 }, |
| 339 | { "fixup_Mips_TPREL_HI", 16, 16, 0 }, |
| 340 | { "fixup_Mips_TPREL_LO", 16, 16, 0 }, |
| 341 | { "fixup_Mips_TLSLDM", 16, 16, 0 }, |
| 342 | { "fixup_Mips_DTPREL_HI", 16, 16, 0 }, |
| 343 | { "fixup_Mips_DTPREL_LO", 16, 16, 0 }, |
| 344 | { "fixup_Mips_Branch_PCRel",16, 16, MCFixupKindInfo::FKF_IsPCRel }, |
| 345 | { "fixup_Mips_GPOFF_HI", 16, 16, 0 }, |
| 346 | { "fixup_Mips_GPOFF_LO", 16, 16, 0 }, |
| 347 | { "fixup_Mips_GOT_PAGE", 16, 16, 0 }, |
| 348 | { "fixup_Mips_GOT_OFST", 16, 16, 0 }, |
| 349 | { "fixup_Mips_GOT_DISP", 16, 16, 0 }, |
| 350 | { "fixup_Mips_HIGHER", 16, 16, 0 }, |
| 351 | { "fixup_Mips_HIGHEST", 16, 16, 0 }, |
| 352 | { "fixup_Mips_GOT_HI16", 16, 16, 0 }, |
| 353 | { "fixup_Mips_GOT_LO16", 16, 16, 0 }, |
| 354 | { "fixup_Mips_CALL_HI16", 16, 16, 0 }, |
| 355 | { "fixup_Mips_CALL_LO16", 16, 16, 0 }, |
Zoran Jovanovic | a5acdcf | 2014-06-13 14:26:47 +0000 | [diff] [blame] | 356 | { "fixup_Mips_PC18_S3", 14, 18, MCFixupKindInfo::FKF_IsPCRel }, |
Zoran Jovanovic | b9c07f3 | 2014-06-12 12:40:00 +0000 | [diff] [blame] | 357 | { "fixup_MIPS_PC19_S2", 13, 19, MCFixupKindInfo::FKF_IsPCRel }, |
Zoran Jovanovic | 10e06da | 2014-05-27 12:55:40 +0000 | [diff] [blame] | 358 | { "fixup_MIPS_PC21_S2", 11, 21, MCFixupKindInfo::FKF_IsPCRel }, |
| 359 | { "fixup_MIPS_PC26_S2", 6, 26, MCFixupKindInfo::FKF_IsPCRel }, |
Zoran Jovanovic | b355e8f | 2014-05-27 14:58:51 +0000 | [diff] [blame] | 360 | { "fixup_MIPS_PCHI16", 16, 16, MCFixupKindInfo::FKF_IsPCRel }, |
| 361 | { "fixup_MIPS_PCLO16", 16, 16, MCFixupKindInfo::FKF_IsPCRel }, |
Daniel Sanders | 683ed96 | 2014-05-23 13:35:24 +0000 | [diff] [blame] | 362 | { "fixup_MICROMIPS_26_S1", 6, 26, 0 }, |
| 363 | { "fixup_MICROMIPS_HI16", 16, 16, 0 }, |
| 364 | { "fixup_MICROMIPS_LO16", 16, 16, 0 }, |
| 365 | { "fixup_MICROMIPS_GOT16", 16, 16, 0 }, |
Jozef Kolek | 9761e96 | 2015-01-12 12:03:34 +0000 | [diff] [blame] | 366 | { "fixup_MICROMIPS_PC7_S1", 9, 7, MCFixupKindInfo::FKF_IsPCRel }, |
Jozef Kolek | 5cfebdd | 2015-01-21 12:39:30 +0000 | [diff] [blame] | 367 | { "fixup_MICROMIPS_PC10_S1", 6, 10, MCFixupKindInfo::FKF_IsPCRel }, |
Daniel Sanders | 683ed96 | 2014-05-23 13:35:24 +0000 | [diff] [blame] | 368 | { "fixup_MICROMIPS_PC16_S1",16, 16, MCFixupKindInfo::FKF_IsPCRel }, |
| 369 | { "fixup_MICROMIPS_CALL16", 16, 16, 0 }, |
| 370 | { "fixup_MICROMIPS_GOT_DISP", 16, 16, 0 }, |
| 371 | { "fixup_MICROMIPS_GOT_PAGE", 16, 16, 0 }, |
| 372 | { "fixup_MICROMIPS_GOT_OFST", 16, 16, 0 }, |
| 373 | { "fixup_MICROMIPS_TLS_GD", 16, 16, 0 }, |
| 374 | { "fixup_MICROMIPS_TLS_LDM", 16, 16, 0 }, |
| 375 | { "fixup_MICROMIPS_TLS_DTPREL_HI16", 16, 16, 0 }, |
| 376 | { "fixup_MICROMIPS_TLS_DTPREL_LO16", 16, 16, 0 }, |
| 377 | { "fixup_MICROMIPS_TLS_TPREL_HI16", 16, 16, 0 }, |
| 378 | { "fixup_MICROMIPS_TLS_TPREL_LO16", 16, 16, 0 } |
| 379 | }; |
| 380 | |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 381 | if (Kind < FirstTargetFixupKind) |
| 382 | return MCAsmBackend::getFixupKindInfo(Kind); |
Akira Hatanaka | 44220ca | 2011-09-30 21:23:45 +0000 | [diff] [blame] | 383 | |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 384 | assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() && |
| 385 | "Invalid kind!"); |
Daniel Sanders | 683ed96 | 2014-05-23 13:35:24 +0000 | [diff] [blame] | 386 | |
| 387 | if (IsLittle) |
| 388 | return LittleEndianInfos[Kind - FirstTargetFixupKind]; |
| 389 | return BigEndianInfos[Kind - FirstTargetFixupKind]; |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 390 | } |
Jim Grosbach | 25b63fa | 2011-12-06 00:47:03 +0000 | [diff] [blame] | 391 | |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 392 | /// WriteNopData - Write an (optimal) nop sequence of Count bytes |
| 393 | /// to the given output. If the target cannot generate such a sequence, |
| 394 | /// it should return an error. |
| 395 | /// |
| 396 | /// \return - True on success. |
| 397 | bool MipsAsmBackend::writeNopData(uint64_t Count, MCObjectWriter *OW) const { |
| 398 | // Check for a less than instruction size number of bytes |
| 399 | // FIXME: 16 bit instructions are not handled yet here. |
| 400 | // We shouldn't be using a hard coded number for instruction size. |
Joerg Sonnenberger | f148a6d | 2014-10-02 13:41:42 +0000 | [diff] [blame] | 401 | |
| 402 | // If the count is not 4-byte aligned, we must be writing data into the text |
| 403 | // section (otherwise we have unaligned instructions, and thus have far |
| 404 | // bigger problems), so just write zeros instead. |
Benjamin Kramer | 97fbdd5 | 2015-04-17 11:12:43 +0000 | [diff] [blame] | 405 | OW->WriteZeros(Count); |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 406 | return true; |
| 407 | } |
Akira Hatanaka | 44220ca | 2011-09-30 21:23:45 +0000 | [diff] [blame] | 408 | |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 409 | /// processFixupValue - Target hook to process the literal value of a fixup |
| 410 | /// if necessary. |
| 411 | void MipsAsmBackend::processFixupValue(const MCAssembler &Asm, |
| 412 | const MCAsmLayout &Layout, |
| 413 | const MCFixup &Fixup, |
| 414 | const MCFragment *DF, |
Rafael Espindola | 3e3de5e | 2014-03-28 16:06:09 +0000 | [diff] [blame] | 415 | const MCValue &Target, |
Zoran Jovanovic | ada38ef | 2014-03-27 12:38:40 +0000 | [diff] [blame] | 416 | uint64_t &Value, |
| 417 | bool &IsResolved) { |
| 418 | // At this point we'll ignore the value returned by adjustFixupValue as |
| 419 | // we are only checking if the fixup can be applied correctly. We have |
| 420 | // access to MCContext from here which allows us to report a fatal error |
| 421 | // with *possibly* a source code location. |
| 422 | (void)adjustFixupValue(Fixup, Value, &Asm.getContext()); |
| 423 | } |
Akira Hatanaka | 44220ca | 2011-09-30 21:23:45 +0000 | [diff] [blame] | 424 | |
Akira Hatanaka | 1ee768d | 2012-03-01 01:53:15 +0000 | [diff] [blame] | 425 | // MCAsmBackend |
Bill Wendling | 58e2d3d | 2013-09-09 02:37:14 +0000 | [diff] [blame] | 426 | MCAsmBackend *llvm::createMipsAsmBackendEL32(const Target &T, |
| 427 | const MCRegisterInfo &MRI, |
Daniel Sanders | 50f1723 | 2015-09-15 16:17:27 +0000 | [diff] [blame] | 428 | const Triple &TT, StringRef CPU) { |
Daniel Sanders | 418caf5 | 2015-06-10 10:35:34 +0000 | [diff] [blame] | 429 | return new MipsAsmBackend(T, TT.getOS(), /*IsLittle*/ true, |
| 430 | /*Is64Bit*/ false); |
Rafael Espindola | 647841b | 2012-01-11 04:04:14 +0000 | [diff] [blame] | 431 | } |
Akira Hatanaka | 44220ca | 2011-09-30 21:23:45 +0000 | [diff] [blame] | 432 | |
Bill Wendling | 58e2d3d | 2013-09-09 02:37:14 +0000 | [diff] [blame] | 433 | MCAsmBackend *llvm::createMipsAsmBackendEB32(const Target &T, |
| 434 | const MCRegisterInfo &MRI, |
Daniel Sanders | 50f1723 | 2015-09-15 16:17:27 +0000 | [diff] [blame] | 435 | const Triple &TT, StringRef CPU) { |
Daniel Sanders | 418caf5 | 2015-06-10 10:35:34 +0000 | [diff] [blame] | 436 | return new MipsAsmBackend(T, TT.getOS(), /*IsLittle*/ false, |
| 437 | /*Is64Bit*/ false); |
Akira Hatanaka | 44220ca | 2011-09-30 21:23:45 +0000 | [diff] [blame] | 438 | } |
Akira Hatanaka | b1f68f9 | 2012-04-02 19:25:22 +0000 | [diff] [blame] | 439 | |
Bill Wendling | 58e2d3d | 2013-09-09 02:37:14 +0000 | [diff] [blame] | 440 | MCAsmBackend *llvm::createMipsAsmBackendEL64(const Target &T, |
| 441 | const MCRegisterInfo &MRI, |
Daniel Sanders | 50f1723 | 2015-09-15 16:17:27 +0000 | [diff] [blame] | 442 | const Triple &TT, StringRef CPU) { |
Daniel Sanders | 418caf5 | 2015-06-10 10:35:34 +0000 | [diff] [blame] | 443 | return new MipsAsmBackend(T, TT.getOS(), /*IsLittle*/ true, /*Is64Bit*/ true); |
Akira Hatanaka | b1f68f9 | 2012-04-02 19:25:22 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Bill Wendling | 58e2d3d | 2013-09-09 02:37:14 +0000 | [diff] [blame] | 446 | MCAsmBackend *llvm::createMipsAsmBackendEB64(const Target &T, |
| 447 | const MCRegisterInfo &MRI, |
Daniel Sanders | 50f1723 | 2015-09-15 16:17:27 +0000 | [diff] [blame] | 448 | const Triple &TT, StringRef CPU) { |
Daniel Sanders | 418caf5 | 2015-06-10 10:35:34 +0000 | [diff] [blame] | 449 | return new MipsAsmBackend(T, TT.getOS(), /*IsLittle*/ false, |
| 450 | /*Is64Bit*/ true); |
Akira Hatanaka | b1f68f9 | 2012-04-02 19:25:22 +0000 | [diff] [blame] | 451 | } |