Jia Liu | 9f61011 | 2012-02-17 08:55:11 +0000 | [diff] [blame] | 1 | //===-- MipsMCCodeEmitter.cpp - Convert Mips Code to Machine Code ---------===// |
Akira Hatanaka | 750ecec | 2011-09-30 20:40:03 +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 | // |
| 10 | // This file implements the MipsMCCodeEmitter class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | // |
Matheus Almeida | 9e1450b | 2014-03-20 09:29:54 +0000 | [diff] [blame] | 14 | |
Matheus Almeida | 9e1450b | 2014-03-20 09:29:54 +0000 | [diff] [blame] | 15 | #include "MipsMCCodeEmitter.h" |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 16 | #include "MCTargetDesc/MipsFixupKinds.h" |
Petar Jovanovic | a5da588 | 2014-02-04 18:41:57 +0000 | [diff] [blame] | 17 | #include "MCTargetDesc/MipsMCExpr.h" |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 18 | #include "MCTargetDesc/MipsMCTargetDesc.h" |
| 19 | #include "llvm/ADT/APFloat.h" |
Matheus Almeida | 9e1450b | 2014-03-20 09:29:54 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/SmallVector.h" |
Akira Hatanaka | 5d6faed | 2012-12-10 20:04:40 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCContext.h" |
Akira Hatanaka | 750ecec | 2011-09-30 20:40:03 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCExpr.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCFixup.h" |
Akira Hatanaka | 750ecec | 2011-09-30 20:40:03 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCInst.h" |
| 25 | #include "llvm/MC/MCInstrInfo.h" |
Akira Hatanaka | 750ecec | 2011-09-30 20:40:03 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCSubtargetInfo.h" |
Akira Hatanaka | 750ecec | 2011-09-30 20:40:03 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Akira Hatanaka | 750ecec | 2011-09-30 20:40:03 +0000 | [diff] [blame] | 28 | |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 29 | #define DEBUG_TYPE "mccodeemitter" |
| 30 | |
Akira Hatanaka | be6a818 | 2013-04-19 19:03:11 +0000 | [diff] [blame] | 31 | #define GET_INSTRMAP_INFO |
| 32 | #include "MipsGenInstrInfo.inc" |
Matheus Almeida | 9e1450b | 2014-03-20 09:29:54 +0000 | [diff] [blame] | 33 | #undef GET_INSTRMAP_INFO |
Akira Hatanaka | be6a818 | 2013-04-19 19:03:11 +0000 | [diff] [blame] | 34 | |
Matheus Almeida | 9e1450b | 2014-03-20 09:29:54 +0000 | [diff] [blame] | 35 | namespace llvm { |
| 36 | MCCodeEmitter *createMipsMCCodeEmitterEB(const MCInstrInfo &MCII, |
| 37 | const MCRegisterInfo &MRI, |
Matheus Almeida | 9e1450b | 2014-03-20 09:29:54 +0000 | [diff] [blame] | 38 | MCContext &Ctx) { |
David Woodhouse | d2cca11 | 2014-01-28 23:13:25 +0000 | [diff] [blame] | 39 | return new MipsMCCodeEmitter(MCII, Ctx, false); |
Akira Hatanaka | 1ee768d | 2012-03-01 01:53:15 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Matheus Almeida | 9e1450b | 2014-03-20 09:29:54 +0000 | [diff] [blame] | 42 | MCCodeEmitter *createMipsMCCodeEmitterEL(const MCInstrInfo &MCII, |
| 43 | const MCRegisterInfo &MRI, |
Matheus Almeida | 9e1450b | 2014-03-20 09:29:54 +0000 | [diff] [blame] | 44 | MCContext &Ctx) { |
David Woodhouse | d2cca11 | 2014-01-28 23:13:25 +0000 | [diff] [blame] | 45 | return new MipsMCCodeEmitter(MCII, Ctx, true); |
Akira Hatanaka | 750ecec | 2011-09-30 20:40:03 +0000 | [diff] [blame] | 46 | } |
Matheus Almeida | 9e1450b | 2014-03-20 09:29:54 +0000 | [diff] [blame] | 47 | } // End of namespace llvm. |
Rafael Espindola | f30f2cc | 2013-05-27 22:34:59 +0000 | [diff] [blame] | 48 | |
| 49 | // If the D<shift> instruction has a shift amount that is greater |
| 50 | // than 31 (checked in calling routine), lower it to a D<shift>32 instruction |
| 51 | static void LowerLargeShift(MCInst& Inst) { |
| 52 | |
| 53 | assert(Inst.getNumOperands() == 3 && "Invalid no. of operands for shift!"); |
| 54 | assert(Inst.getOperand(2).isImm()); |
| 55 | |
| 56 | int64_t Shift = Inst.getOperand(2).getImm(); |
| 57 | if (Shift <= 31) |
| 58 | return; // Do nothing |
| 59 | Shift -= 32; |
| 60 | |
| 61 | // saminus32 |
| 62 | Inst.getOperand(2).setImm(Shift); |
| 63 | |
| 64 | switch (Inst.getOpcode()) { |
| 65 | default: |
| 66 | // Calling function is not synchronized |
| 67 | llvm_unreachable("Unexpected shift instruction"); |
| 68 | case Mips::DSLL: |
| 69 | Inst.setOpcode(Mips::DSLL32); |
| 70 | return; |
| 71 | case Mips::DSRL: |
| 72 | Inst.setOpcode(Mips::DSRL32); |
| 73 | return; |
| 74 | case Mips::DSRA: |
| 75 | Inst.setOpcode(Mips::DSRA32); |
| 76 | return; |
Akira Hatanaka | 6a3fe57 | 2013-09-07 00:18:01 +0000 | [diff] [blame] | 77 | case Mips::DROTR: |
| 78 | Inst.setOpcode(Mips::DROTR32); |
| 79 | return; |
Rafael Espindola | f30f2cc | 2013-05-27 22:34:59 +0000 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
| 83 | // Pick a DEXT or DINS instruction variant based on the pos and size operands |
| 84 | static void LowerDextDins(MCInst& InstIn) { |
| 85 | int Opcode = InstIn.getOpcode(); |
| 86 | |
| 87 | if (Opcode == Mips::DEXT) |
| 88 | assert(InstIn.getNumOperands() == 4 && |
| 89 | "Invalid no. of machine operands for DEXT!"); |
| 90 | else // Only DEXT and DINS are possible |
| 91 | assert(InstIn.getNumOperands() == 5 && |
| 92 | "Invalid no. of machine operands for DINS!"); |
| 93 | |
| 94 | assert(InstIn.getOperand(2).isImm()); |
| 95 | int64_t pos = InstIn.getOperand(2).getImm(); |
| 96 | assert(InstIn.getOperand(3).isImm()); |
| 97 | int64_t size = InstIn.getOperand(3).getImm(); |
| 98 | |
| 99 | if (size <= 32) { |
| 100 | if (pos < 32) // DEXT/DINS, do nothing |
| 101 | return; |
| 102 | // DEXTU/DINSU |
| 103 | InstIn.getOperand(2).setImm(pos - 32); |
| 104 | InstIn.setOpcode((Opcode == Mips::DEXT) ? Mips::DEXTU : Mips::DINSU); |
| 105 | return; |
| 106 | } |
| 107 | // DEXTM/DINSM |
| 108 | assert(pos < 32 && "DEXT/DINS cannot have both size and pos > 32"); |
| 109 | InstIn.getOperand(3).setImm(size - 32); |
| 110 | InstIn.setOpcode((Opcode == Mips::DEXT) ? Mips::DEXTM : Mips::DINSM); |
| 111 | return; |
| 112 | } |
| 113 | |
Matheus Almeida | 9e1450b | 2014-03-20 09:29:54 +0000 | [diff] [blame] | 114 | bool MipsMCCodeEmitter::isMicroMips(const MCSubtargetInfo &STI) const { |
Michael Kuperstein | c3434b3 | 2015-05-13 10:28:46 +0000 | [diff] [blame] | 115 | return STI.getFeatureBits() & Mips::FeatureMicroMips; |
Matheus Almeida | 9e1450b | 2014-03-20 09:29:54 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Jozef Kolek | c22555d | 2015-04-20 12:23:06 +0000 | [diff] [blame] | 118 | bool MipsMCCodeEmitter::isMips32r6(const MCSubtargetInfo &STI) const { |
Michael Kuperstein | c3434b3 | 2015-05-13 10:28:46 +0000 | [diff] [blame] | 119 | return STI.getFeatureBits() & Mips::FeatureMips32r6; |
Jozef Kolek | c22555d | 2015-04-20 12:23:06 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Matheus Almeida | 9e1450b | 2014-03-20 09:29:54 +0000 | [diff] [blame] | 122 | void MipsMCCodeEmitter::EmitByte(unsigned char C, raw_ostream &OS) const { |
| 123 | OS << (char)C; |
| 124 | } |
| 125 | |
| 126 | void MipsMCCodeEmitter::EmitInstruction(uint64_t Val, unsigned Size, |
| 127 | const MCSubtargetInfo &STI, |
| 128 | raw_ostream &OS) const { |
| 129 | // Output the instruction encoding in little endian byte order. |
| 130 | // Little-endian byte ordering: |
| 131 | // mips32r2: 4 | 3 | 2 | 1 |
| 132 | // microMIPS: 2 | 1 | 4 | 3 |
| 133 | if (IsLittleEndian && Size == 4 && isMicroMips(STI)) { |
| 134 | EmitInstruction(Val >> 16, 2, STI, OS); |
| 135 | EmitInstruction(Val, 2, STI, OS); |
| 136 | } else { |
| 137 | for (unsigned i = 0; i < Size; ++i) { |
| 138 | unsigned Shift = IsLittleEndian ? i * 8 : (Size - 1 - i) * 8; |
| 139 | EmitByte((Val >> Shift) & 0xff, OS); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
Jim Grosbach | 91df21f | 2015-05-15 19:13:16 +0000 | [diff] [blame^] | 144 | /// encodeInstruction - Emit the instruction. |
Jack Carter | 4e07b95d | 2013-08-27 19:45:28 +0000 | [diff] [blame] | 145 | /// Size the instruction with Desc.getSize(). |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 146 | void MipsMCCodeEmitter:: |
Jim Grosbach | 91df21f | 2015-05-15 19:13:16 +0000 | [diff] [blame^] | 147 | encodeInstruction(const MCInst &MI, raw_ostream &OS, |
David Woodhouse | 9784cef | 2014-01-28 23:13:07 +0000 | [diff] [blame] | 148 | SmallVectorImpl<MCFixup> &Fixups, |
| 149 | const MCSubtargetInfo &STI) const |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 150 | { |
Jack Carter | aa7aeaa | 2012-10-02 23:09:40 +0000 | [diff] [blame] | 151 | |
| 152 | // Non-pseudo instructions that get changed for direct object |
| 153 | // only based on operand values. |
| 154 | // If this list of instructions get much longer we will move |
| 155 | // the check to a function call. Until then, this is more efficient. |
| 156 | MCInst TmpInst = MI; |
| 157 | switch (MI.getOpcode()) { |
| 158 | // If shift amount is >= 32 it the inst needs to be lowered further |
| 159 | case Mips::DSLL: |
| 160 | case Mips::DSRL: |
| 161 | case Mips::DSRA: |
Akira Hatanaka | 6a3fe57 | 2013-09-07 00:18:01 +0000 | [diff] [blame] | 162 | case Mips::DROTR: |
Rafael Espindola | f30f2cc | 2013-05-27 22:34:59 +0000 | [diff] [blame] | 163 | LowerLargeShift(TmpInst); |
Jack Carter | aa7aeaa | 2012-10-02 23:09:40 +0000 | [diff] [blame] | 164 | break; |
| 165 | // Double extract instruction is chosen by pos and size operands |
| 166 | case Mips::DEXT: |
| 167 | case Mips::DINS: |
Rafael Espindola | f30f2cc | 2013-05-27 22:34:59 +0000 | [diff] [blame] | 168 | LowerDextDins(TmpInst); |
Jack Carter | aa7aeaa | 2012-10-02 23:09:40 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Jack Carter | 9770097 | 2013-08-13 20:19:16 +0000 | [diff] [blame] | 171 | unsigned long N = Fixups.size(); |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 172 | uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI); |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 173 | |
| 174 | // Check for unimplemented opcodes. |
Jack Carter | aa7aeaa | 2012-10-02 23:09:40 +0000 | [diff] [blame] | 175 | // Unfortunately in MIPS both NOP and SLL will come in with Binary == 0 |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 176 | // so we have to special check for them. |
Jack Carter | aa7aeaa | 2012-10-02 23:09:40 +0000 | [diff] [blame] | 177 | unsigned Opcode = TmpInst.getOpcode(); |
Jozef Kolek | c7e220f | 2014-11-29 13:29:24 +0000 | [diff] [blame] | 178 | if ((Opcode != Mips::NOP) && (Opcode != Mips::SLL) && |
| 179 | (Opcode != Mips::SLL_MM) && !Binary) |
Jim Grosbach | 91df21f | 2015-05-15 19:13:16 +0000 | [diff] [blame^] | 180 | llvm_unreachable("unimplemented opcode in encodeInstruction()"); |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 181 | |
Zoran Jovanovic | b59a541 | 2015-04-22 13:27:34 +0000 | [diff] [blame] | 182 | int NewOpcode = -1; |
Jozef Kolek | 6ca13ea | 2015-04-20 12:42:08 +0000 | [diff] [blame] | 183 | if (isMicroMips(STI)) { |
Zoran Jovanovic | b59a541 | 2015-04-22 13:27:34 +0000 | [diff] [blame] | 184 | if (isMips32r6(STI)) { |
| 185 | NewOpcode = Mips::MipsR62MicroMipsR6(Opcode, Mips::Arch_micromipsr6); |
| 186 | if (NewOpcode == -1) |
| 187 | NewOpcode = Mips::Std2MicroMipsR6(Opcode, Mips::Arch_micromipsr6); |
| 188 | } |
| 189 | else |
| 190 | NewOpcode = Mips::Std2MicroMips(Opcode, Mips::Arch_micromips); |
| 191 | |
Akira Hatanaka | be6a818 | 2013-04-19 19:03:11 +0000 | [diff] [blame] | 192 | if (NewOpcode != -1) { |
Jack Carter | 9770097 | 2013-08-13 20:19:16 +0000 | [diff] [blame] | 193 | if (Fixups.size() > N) |
| 194 | Fixups.pop_back(); |
Zoran Jovanovic | b59a541 | 2015-04-22 13:27:34 +0000 | [diff] [blame] | 195 | |
Akira Hatanaka | be6a818 | 2013-04-19 19:03:11 +0000 | [diff] [blame] | 196 | Opcode = NewOpcode; |
| 197 | TmpInst.setOpcode (NewOpcode); |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 198 | Binary = getBinaryCodeForInstr(TmpInst, Fixups, STI); |
Akira Hatanaka | be6a818 | 2013-04-19 19:03:11 +0000 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
Jack Carter | aa7aeaa | 2012-10-02 23:09:40 +0000 | [diff] [blame] | 202 | const MCInstrDesc &Desc = MCII.get(TmpInst.getOpcode()); |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 203 | |
Jack Carter | 5b5559d | 2012-10-03 21:58:54 +0000 | [diff] [blame] | 204 | // Get byte count of instruction |
| 205 | unsigned Size = Desc.getSize(); |
| 206 | if (!Size) |
| 207 | llvm_unreachable("Desc.getSize() returns 0"); |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 208 | |
David Woodhouse | d2cca11 | 2014-01-28 23:13:25 +0000 | [diff] [blame] | 209 | EmitInstruction(Binary, Size, STI, OS); |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /// getBranchTargetOpValue - Return binary encoding of the branch |
| 213 | /// target operand. If the machine operand requires relocation, |
| 214 | /// record the relocation and return zero. |
| 215 | unsigned MipsMCCodeEmitter:: |
| 216 | getBranchTargetOpValue(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 217 | SmallVectorImpl<MCFixup> &Fixups, |
| 218 | const MCSubtargetInfo &STI) const { |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 219 | |
| 220 | const MCOperand &MO = MI.getOperand(OpNo); |
Jack Carter | 71e6a74 | 2012-09-06 00:43:26 +0000 | [diff] [blame] | 221 | |
Jack Carter | 4f69a0f | 2013-03-22 00:29:10 +0000 | [diff] [blame] | 222 | // If the destination is an immediate, divide by 4. |
| 223 | if (MO.isImm()) return MO.getImm() >> 2; |
| 224 | |
Jack Carter | 71e6a74 | 2012-09-06 00:43:26 +0000 | [diff] [blame] | 225 | assert(MO.isExpr() && |
| 226 | "getBranchTargetOpValue expects only expressions or immediates"); |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 227 | |
| 228 | const MCExpr *Expr = MO.getExpr(); |
Jim Grosbach | 63661f8 | 2015-05-15 19:13:05 +0000 | [diff] [blame] | 229 | Fixups.push_back(MCFixup::create(0, Expr, |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 230 | MCFixupKind(Mips::fixup_Mips_PC16))); |
| 231 | return 0; |
| 232 | } |
| 233 | |
Jozef Kolek | 9761e96 | 2015-01-12 12:03:34 +0000 | [diff] [blame] | 234 | /// getBranchTarget7OpValueMM - Return binary encoding of the microMIPS branch |
| 235 | /// target operand. If the machine operand requires relocation, |
| 236 | /// record the relocation and return zero. |
| 237 | unsigned MipsMCCodeEmitter:: |
| 238 | getBranchTarget7OpValueMM(const MCInst &MI, unsigned OpNo, |
| 239 | SmallVectorImpl<MCFixup> &Fixups, |
| 240 | const MCSubtargetInfo &STI) const { |
| 241 | |
| 242 | const MCOperand &MO = MI.getOperand(OpNo); |
| 243 | |
| 244 | // If the destination is an immediate, divide by 2. |
| 245 | if (MO.isImm()) return MO.getImm() >> 1; |
| 246 | |
| 247 | assert(MO.isExpr() && |
| 248 | "getBranchTargetOpValueMM expects only expressions or immediates"); |
| 249 | |
| 250 | const MCExpr *Expr = MO.getExpr(); |
Jim Grosbach | 63661f8 | 2015-05-15 19:13:05 +0000 | [diff] [blame] | 251 | Fixups.push_back(MCFixup::create(0, Expr, |
Jozef Kolek | 9761e96 | 2015-01-12 12:03:34 +0000 | [diff] [blame] | 252 | MCFixupKind(Mips::fixup_MICROMIPS_PC7_S1))); |
| 253 | return 0; |
| 254 | } |
| 255 | |
Jozef Kolek | 5cfebdd | 2015-01-21 12:39:30 +0000 | [diff] [blame] | 256 | /// getBranchTargetOpValueMMPC10 - Return binary encoding of the microMIPS |
| 257 | /// 10-bit branch target operand. If the machine operand requires relocation, |
| 258 | /// record the relocation and return zero. |
| 259 | unsigned MipsMCCodeEmitter:: |
| 260 | getBranchTargetOpValueMMPC10(const MCInst &MI, unsigned OpNo, |
| 261 | SmallVectorImpl<MCFixup> &Fixups, |
| 262 | const MCSubtargetInfo &STI) const { |
| 263 | |
| 264 | const MCOperand &MO = MI.getOperand(OpNo); |
| 265 | |
| 266 | // If the destination is an immediate, divide by 2. |
| 267 | if (MO.isImm()) return MO.getImm() >> 1; |
| 268 | |
| 269 | assert(MO.isExpr() && |
| 270 | "getBranchTargetOpValuePC10 expects only expressions or immediates"); |
| 271 | |
| 272 | const MCExpr *Expr = MO.getExpr(); |
Jim Grosbach | 63661f8 | 2015-05-15 19:13:05 +0000 | [diff] [blame] | 273 | Fixups.push_back(MCFixup::create(0, Expr, |
Jozef Kolek | 5cfebdd | 2015-01-21 12:39:30 +0000 | [diff] [blame] | 274 | MCFixupKind(Mips::fixup_MICROMIPS_PC10_S1))); |
| 275 | return 0; |
| 276 | } |
| 277 | |
Zoran Jovanovic | 8a80aa7 | 2013-11-04 14:53:22 +0000 | [diff] [blame] | 278 | /// getBranchTargetOpValue - Return binary encoding of the microMIPS branch |
| 279 | /// target operand. If the machine operand requires relocation, |
| 280 | /// record the relocation and return zero. |
| 281 | unsigned MipsMCCodeEmitter:: |
| 282 | getBranchTargetOpValueMM(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 283 | SmallVectorImpl<MCFixup> &Fixups, |
| 284 | const MCSubtargetInfo &STI) const { |
Zoran Jovanovic | 8a80aa7 | 2013-11-04 14:53:22 +0000 | [diff] [blame] | 285 | |
| 286 | const MCOperand &MO = MI.getOperand(OpNo); |
| 287 | |
| 288 | // If the destination is an immediate, divide by 2. |
| 289 | if (MO.isImm()) return MO.getImm() >> 1; |
| 290 | |
| 291 | assert(MO.isExpr() && |
| 292 | "getBranchTargetOpValueMM expects only expressions or immediates"); |
| 293 | |
| 294 | const MCExpr *Expr = MO.getExpr(); |
Jim Grosbach | 63661f8 | 2015-05-15 19:13:05 +0000 | [diff] [blame] | 295 | Fixups.push_back(MCFixup::create(0, Expr, |
Zoran Jovanovic | 8a80aa7 | 2013-11-04 14:53:22 +0000 | [diff] [blame] | 296 | MCFixupKind(Mips:: |
| 297 | fixup_MICROMIPS_PC16_S1))); |
| 298 | return 0; |
| 299 | } |
| 300 | |
Zoran Jovanovic | 3c8869d | 2014-05-16 11:03:45 +0000 | [diff] [blame] | 301 | /// getBranchTarget21OpValue - Return binary encoding of the branch |
| 302 | /// target operand. If the machine operand requires relocation, |
| 303 | /// record the relocation and return zero. |
| 304 | unsigned MipsMCCodeEmitter:: |
| 305 | getBranchTarget21OpValue(const MCInst &MI, unsigned OpNo, |
| 306 | SmallVectorImpl<MCFixup> &Fixups, |
| 307 | const MCSubtargetInfo &STI) const { |
| 308 | |
| 309 | const MCOperand &MO = MI.getOperand(OpNo); |
| 310 | |
| 311 | // If the destination is an immediate, divide by 4. |
| 312 | if (MO.isImm()) return MO.getImm() >> 2; |
| 313 | |
| 314 | assert(MO.isExpr() && |
| 315 | "getBranchTarget21OpValue expects only expressions or immediates"); |
| 316 | |
Zoran Jovanovic | 10e06da | 2014-05-27 12:55:40 +0000 | [diff] [blame] | 317 | const MCExpr *Expr = MO.getExpr(); |
Jim Grosbach | 63661f8 | 2015-05-15 19:13:05 +0000 | [diff] [blame] | 318 | Fixups.push_back(MCFixup::create(0, Expr, |
Zoran Jovanovic | 10e06da | 2014-05-27 12:55:40 +0000 | [diff] [blame] | 319 | MCFixupKind(Mips::fixup_MIPS_PC21_S2))); |
Zoran Jovanovic | 3c8869d | 2014-05-16 11:03:45 +0000 | [diff] [blame] | 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | /// getBranchTarget26OpValue - Return binary encoding of the branch |
| 324 | /// target operand. If the machine operand requires relocation, |
| 325 | /// record the relocation and return zero. |
| 326 | unsigned MipsMCCodeEmitter:: |
| 327 | getBranchTarget26OpValue(const MCInst &MI, unsigned OpNo, |
| 328 | SmallVectorImpl<MCFixup> &Fixups, |
| 329 | const MCSubtargetInfo &STI) const { |
| 330 | |
| 331 | const MCOperand &MO = MI.getOperand(OpNo); |
| 332 | |
| 333 | // If the destination is an immediate, divide by 4. |
| 334 | if (MO.isImm()) return MO.getImm() >> 2; |
| 335 | |
| 336 | assert(MO.isExpr() && |
| 337 | "getBranchTarget26OpValue expects only expressions or immediates"); |
| 338 | |
Zoran Jovanovic | 10e06da | 2014-05-27 12:55:40 +0000 | [diff] [blame] | 339 | const MCExpr *Expr = MO.getExpr(); |
Jim Grosbach | 63661f8 | 2015-05-15 19:13:05 +0000 | [diff] [blame] | 340 | Fixups.push_back(MCFixup::create(0, Expr, |
Zoran Jovanovic | 10e06da | 2014-05-27 12:55:40 +0000 | [diff] [blame] | 341 | MCFixupKind(Mips::fixup_MIPS_PC26_S2))); |
Zoran Jovanovic | 3c8869d | 2014-05-16 11:03:45 +0000 | [diff] [blame] | 342 | return 0; |
| 343 | } |
| 344 | |
Zoran Jovanovic | 52c56b9 | 2014-05-16 13:19:46 +0000 | [diff] [blame] | 345 | /// getJumpOffset16OpValue - Return binary encoding of the jump |
| 346 | /// target operand. If the machine operand requires relocation, |
| 347 | /// record the relocation and return zero. |
| 348 | unsigned MipsMCCodeEmitter:: |
| 349 | getJumpOffset16OpValue(const MCInst &MI, unsigned OpNo, |
| 350 | SmallVectorImpl<MCFixup> &Fixups, |
| 351 | const MCSubtargetInfo &STI) const { |
| 352 | |
| 353 | const MCOperand &MO = MI.getOperand(OpNo); |
| 354 | |
| 355 | if (MO.isImm()) return MO.getImm(); |
| 356 | |
| 357 | assert(MO.isExpr() && |
| 358 | "getJumpOffset16OpValue expects only expressions or an immediate"); |
| 359 | |
| 360 | // TODO: Push fixup. |
| 361 | return 0; |
| 362 | } |
| 363 | |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 364 | /// getJumpTargetOpValue - Return binary encoding of the jump |
| 365 | /// target operand. If the machine operand requires relocation, |
| 366 | /// record the relocation and return zero. |
| 367 | unsigned MipsMCCodeEmitter:: |
| 368 | getJumpTargetOpValue(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 369 | SmallVectorImpl<MCFixup> &Fixups, |
| 370 | const MCSubtargetInfo &STI) const { |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 371 | |
| 372 | const MCOperand &MO = MI.getOperand(OpNo); |
Jack Carter | 4f69a0f | 2013-03-22 00:29:10 +0000 | [diff] [blame] | 373 | // If the destination is an immediate, divide by 4. |
| 374 | if (MO.isImm()) return MO.getImm()>>2; |
| 375 | |
Jack Carter | 71e6a74 | 2012-09-06 00:43:26 +0000 | [diff] [blame] | 376 | assert(MO.isExpr() && |
| 377 | "getJumpTargetOpValue expects only expressions or an immediate"); |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 378 | |
| 379 | const MCExpr *Expr = MO.getExpr(); |
Jim Grosbach | 63661f8 | 2015-05-15 19:13:05 +0000 | [diff] [blame] | 380 | Fixups.push_back(MCFixup::create(0, Expr, |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 381 | MCFixupKind(Mips::fixup_Mips_26))); |
| 382 | return 0; |
| 383 | } |
| 384 | |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 385 | unsigned MipsMCCodeEmitter:: |
Zoran Jovanovic | 507e084 | 2013-10-29 16:38:59 +0000 | [diff] [blame] | 386 | getJumpTargetOpValueMM(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 387 | SmallVectorImpl<MCFixup> &Fixups, |
| 388 | const MCSubtargetInfo &STI) const { |
Zoran Jovanovic | 507e084 | 2013-10-29 16:38:59 +0000 | [diff] [blame] | 389 | |
| 390 | const MCOperand &MO = MI.getOperand(OpNo); |
| 391 | // If the destination is an immediate, divide by 2. |
| 392 | if (MO.isImm()) return MO.getImm() >> 1; |
| 393 | |
| 394 | assert(MO.isExpr() && |
| 395 | "getJumpTargetOpValueMM expects only expressions or an immediate"); |
| 396 | |
| 397 | const MCExpr *Expr = MO.getExpr(); |
Jim Grosbach | 63661f8 | 2015-05-15 19:13:05 +0000 | [diff] [blame] | 398 | Fixups.push_back(MCFixup::create(0, Expr, |
Zoran Jovanovic | 507e084 | 2013-10-29 16:38:59 +0000 | [diff] [blame] | 399 | MCFixupKind(Mips::fixup_MICROMIPS_26_S1))); |
| 400 | return 0; |
| 401 | } |
| 402 | |
| 403 | unsigned MipsMCCodeEmitter:: |
Zoran Jovanovic | c74e3eb9 | 2014-09-12 14:29:54 +0000 | [diff] [blame] | 404 | getUImm5Lsl2Encoding(const MCInst &MI, unsigned OpNo, |
| 405 | SmallVectorImpl<MCFixup> &Fixups, |
| 406 | const MCSubtargetInfo &STI) const { |
| 407 | |
| 408 | const MCOperand &MO = MI.getOperand(OpNo); |
| 409 | if (MO.isImm()) { |
| 410 | // The immediate is encoded as 'immediate << 2'. |
| 411 | unsigned Res = getMachineOpValue(MI, MO, Fixups, STI); |
| 412 | assert((Res & 3) == 0); |
| 413 | return Res >> 2; |
| 414 | } |
| 415 | |
| 416 | assert(MO.isExpr() && |
| 417 | "getUImm5Lsl2Encoding expects only expressions or an immediate"); |
| 418 | |
| 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | unsigned MipsMCCodeEmitter:: |
Zoran Jovanovic | bac3619 | 2014-10-23 11:06:34 +0000 | [diff] [blame] | 423 | getSImm3Lsa2Value(const MCInst &MI, unsigned OpNo, |
| 424 | SmallVectorImpl<MCFixup> &Fixups, |
| 425 | const MCSubtargetInfo &STI) const { |
| 426 | |
| 427 | const MCOperand &MO = MI.getOperand(OpNo); |
| 428 | if (MO.isImm()) { |
| 429 | int Value = MO.getImm(); |
| 430 | return Value >> 2; |
| 431 | } |
| 432 | |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | unsigned MipsMCCodeEmitter:: |
Zoran Jovanovic | 42b8444 | 2014-10-23 11:13:59 +0000 | [diff] [blame] | 437 | getUImm6Lsl2Encoding(const MCInst &MI, unsigned OpNo, |
| 438 | SmallVectorImpl<MCFixup> &Fixups, |
| 439 | const MCSubtargetInfo &STI) const { |
| 440 | |
| 441 | const MCOperand &MO = MI.getOperand(OpNo); |
| 442 | if (MO.isImm()) { |
| 443 | unsigned Value = MO.getImm(); |
| 444 | return Value >> 2; |
| 445 | } |
| 446 | |
| 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | unsigned MipsMCCodeEmitter:: |
Zoran Jovanovic | 98bd58c | 2014-10-10 14:37:30 +0000 | [diff] [blame] | 451 | getSImm9AddiuspValue(const MCInst &MI, unsigned OpNo, |
| 452 | SmallVectorImpl<MCFixup> &Fixups, |
| 453 | const MCSubtargetInfo &STI) const { |
| 454 | |
| 455 | const MCOperand &MO = MI.getOperand(OpNo); |
| 456 | if (MO.isImm()) { |
| 457 | unsigned Binary = (MO.getImm() >> 2) & 0x0000ffff; |
| 458 | return (((Binary & 0x8000) >> 7) | (Binary & 0x00ff)); |
| 459 | } |
| 460 | |
| 461 | return 0; |
| 462 | } |
| 463 | |
| 464 | unsigned MipsMCCodeEmitter:: |
Daniel Sanders | 60f1db0 | 2015-03-13 12:45:09 +0000 | [diff] [blame] | 465 | getExprOpValue(const MCExpr *Expr, SmallVectorImpl<MCFixup> &Fixups, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 466 | const MCSubtargetInfo &STI) const { |
Jack Carter | b5cf590 | 2013-04-17 00:18:04 +0000 | [diff] [blame] | 467 | int64_t Res; |
Akira Hatanaka | 049e9e4 | 2011-11-23 22:19:28 +0000 | [diff] [blame] | 468 | |
Jack Carter | b5cf590 | 2013-04-17 00:18:04 +0000 | [diff] [blame] | 469 | if (Expr->EvaluateAsAbsolute(Res)) |
| 470 | return Res; |
Akira Hatanaka | 049e9e4 | 2011-11-23 22:19:28 +0000 | [diff] [blame] | 471 | |
Akira Hatanaka | fe384a2 | 2012-03-27 02:33:05 +0000 | [diff] [blame] | 472 | MCExpr::ExprKind Kind = Expr->getKind(); |
Jack Carter | b5cf590 | 2013-04-17 00:18:04 +0000 | [diff] [blame] | 473 | if (Kind == MCExpr::Constant) { |
| 474 | return cast<MCConstantExpr>(Expr)->getValue(); |
| 475 | } |
Akira Hatanaka | e2eed96 | 2011-12-22 01:05:17 +0000 | [diff] [blame] | 476 | |
Akira Hatanaka | fe384a2 | 2012-03-27 02:33:05 +0000 | [diff] [blame] | 477 | if (Kind == MCExpr::Binary) { |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 478 | unsigned Res = getExprOpValue(cast<MCBinaryExpr>(Expr)->getLHS(), Fixups, STI); |
| 479 | Res += getExprOpValue(cast<MCBinaryExpr>(Expr)->getRHS(), Fixups, STI); |
Jack Carter | b5cf590 | 2013-04-17 00:18:04 +0000 | [diff] [blame] | 480 | return Res; |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 481 | } |
Petar Jovanovic | a5da588 | 2014-02-04 18:41:57 +0000 | [diff] [blame] | 482 | |
| 483 | if (Kind == MCExpr::Target) { |
| 484 | const MipsMCExpr *MipsExpr = cast<MipsMCExpr>(Expr); |
| 485 | |
| 486 | Mips::Fixups FixupKind = Mips::Fixups(0); |
| 487 | switch (MipsExpr->getKind()) { |
| 488 | default: llvm_unreachable("Unsupported fixup kind for target expression!"); |
Sasa Stankovic | 06c4780 | 2014-04-03 10:37:45 +0000 | [diff] [blame] | 489 | case MipsMCExpr::VK_Mips_HIGHEST: |
| 490 | FixupKind = Mips::fixup_Mips_HIGHEST; |
| 491 | break; |
| 492 | case MipsMCExpr::VK_Mips_HIGHER: |
| 493 | FixupKind = Mips::fixup_Mips_HIGHER; |
| 494 | break; |
| 495 | case MipsMCExpr::VK_Mips_HI: |
Petar Jovanovic | a5da588 | 2014-02-04 18:41:57 +0000 | [diff] [blame] | 496 | FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HI16 |
| 497 | : Mips::fixup_Mips_HI16; |
| 498 | break; |
Sasa Stankovic | 06c4780 | 2014-04-03 10:37:45 +0000 | [diff] [blame] | 499 | case MipsMCExpr::VK_Mips_LO: |
Petar Jovanovic | a5da588 | 2014-02-04 18:41:57 +0000 | [diff] [blame] | 500 | FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_LO16 |
| 501 | : Mips::fixup_Mips_LO16; |
| 502 | break; |
| 503 | } |
Jim Grosbach | 63661f8 | 2015-05-15 19:13:05 +0000 | [diff] [blame] | 504 | Fixups.push_back(MCFixup::create(0, MipsExpr, MCFixupKind(FixupKind))); |
Petar Jovanovic | a5da588 | 2014-02-04 18:41:57 +0000 | [diff] [blame] | 505 | return 0; |
| 506 | } |
| 507 | |
Jack Carter | b5cf590 | 2013-04-17 00:18:04 +0000 | [diff] [blame] | 508 | if (Kind == MCExpr::SymbolRef) { |
Mark Seaborn | c3bd177 | 2013-12-31 13:05:15 +0000 | [diff] [blame] | 509 | Mips::Fixups FixupKind = Mips::Fixups(0); |
Akira Hatanaka | fe384a2 | 2012-03-27 02:33:05 +0000 | [diff] [blame] | 510 | |
Mark Seaborn | c3bd177 | 2013-12-31 13:05:15 +0000 | [diff] [blame] | 511 | switch(cast<MCSymbolRefExpr>(Expr)->getKind()) { |
| 512 | default: llvm_unreachable("Unknown fixup kind!"); |
| 513 | break; |
Daniel Sanders | 60f1db0 | 2015-03-13 12:45:09 +0000 | [diff] [blame] | 514 | case MCSymbolRefExpr::VK_None: |
| 515 | FixupKind = Mips::fixup_Mips_32; // FIXME: This is ok for O32/N32 but not N64. |
| 516 | break; |
Mark Seaborn | c3bd177 | 2013-12-31 13:05:15 +0000 | [diff] [blame] | 517 | case MCSymbolRefExpr::VK_Mips_GPOFF_HI : |
| 518 | FixupKind = Mips::fixup_Mips_GPOFF_HI; |
| 519 | break; |
| 520 | case MCSymbolRefExpr::VK_Mips_GPOFF_LO : |
| 521 | FixupKind = Mips::fixup_Mips_GPOFF_LO; |
| 522 | break; |
| 523 | case MCSymbolRefExpr::VK_Mips_GOT_PAGE : |
David Woodhouse | d2cca11 | 2014-01-28 23:13:25 +0000 | [diff] [blame] | 524 | FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_PAGE |
Mark Seaborn | c3bd177 | 2013-12-31 13:05:15 +0000 | [diff] [blame] | 525 | : Mips::fixup_Mips_GOT_PAGE; |
| 526 | break; |
| 527 | case MCSymbolRefExpr::VK_Mips_GOT_OFST : |
David Woodhouse | d2cca11 | 2014-01-28 23:13:25 +0000 | [diff] [blame] | 528 | FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_OFST |
Mark Seaborn | c3bd177 | 2013-12-31 13:05:15 +0000 | [diff] [blame] | 529 | : Mips::fixup_Mips_GOT_OFST; |
| 530 | break; |
| 531 | case MCSymbolRefExpr::VK_Mips_GOT_DISP : |
David Woodhouse | d2cca11 | 2014-01-28 23:13:25 +0000 | [diff] [blame] | 532 | FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT_DISP |
Mark Seaborn | c3bd177 | 2013-12-31 13:05:15 +0000 | [diff] [blame] | 533 | : Mips::fixup_Mips_GOT_DISP; |
| 534 | break; |
| 535 | case MCSymbolRefExpr::VK_Mips_GPREL: |
| 536 | FixupKind = Mips::fixup_Mips_GPREL16; |
| 537 | break; |
| 538 | case MCSymbolRefExpr::VK_Mips_GOT_CALL: |
David Woodhouse | d2cca11 | 2014-01-28 23:13:25 +0000 | [diff] [blame] | 539 | FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_CALL16 |
Mark Seaborn | c3bd177 | 2013-12-31 13:05:15 +0000 | [diff] [blame] | 540 | : Mips::fixup_Mips_CALL16; |
| 541 | break; |
| 542 | case MCSymbolRefExpr::VK_Mips_GOT16: |
David Woodhouse | d2cca11 | 2014-01-28 23:13:25 +0000 | [diff] [blame] | 543 | FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT16 |
Mark Seaborn | c3bd177 | 2013-12-31 13:05:15 +0000 | [diff] [blame] | 544 | : Mips::fixup_Mips_GOT_Global; |
| 545 | break; |
| 546 | case MCSymbolRefExpr::VK_Mips_GOT: |
David Woodhouse | d2cca11 | 2014-01-28 23:13:25 +0000 | [diff] [blame] | 547 | FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_GOT16 |
Mark Seaborn | c3bd177 | 2013-12-31 13:05:15 +0000 | [diff] [blame] | 548 | : Mips::fixup_Mips_GOT_Local; |
| 549 | break; |
| 550 | case MCSymbolRefExpr::VK_Mips_ABS_HI: |
David Woodhouse | d2cca11 | 2014-01-28 23:13:25 +0000 | [diff] [blame] | 551 | FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_HI16 |
Mark Seaborn | c3bd177 | 2013-12-31 13:05:15 +0000 | [diff] [blame] | 552 | : Mips::fixup_Mips_HI16; |
| 553 | break; |
| 554 | case MCSymbolRefExpr::VK_Mips_ABS_LO: |
David Woodhouse | d2cca11 | 2014-01-28 23:13:25 +0000 | [diff] [blame] | 555 | FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_LO16 |
Mark Seaborn | c3bd177 | 2013-12-31 13:05:15 +0000 | [diff] [blame] | 556 | : Mips::fixup_Mips_LO16; |
| 557 | break; |
| 558 | case MCSymbolRefExpr::VK_Mips_TLSGD: |
David Woodhouse | d2cca11 | 2014-01-28 23:13:25 +0000 | [diff] [blame] | 559 | FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_GD |
Mark Seaborn | c3bd177 | 2013-12-31 13:05:15 +0000 | [diff] [blame] | 560 | : Mips::fixup_Mips_TLSGD; |
| 561 | break; |
| 562 | case MCSymbolRefExpr::VK_Mips_TLSLDM: |
David Woodhouse | d2cca11 | 2014-01-28 23:13:25 +0000 | [diff] [blame] | 563 | FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_LDM |
Mark Seaborn | c3bd177 | 2013-12-31 13:05:15 +0000 | [diff] [blame] | 564 | : Mips::fixup_Mips_TLSLDM; |
| 565 | break; |
| 566 | case MCSymbolRefExpr::VK_Mips_DTPREL_HI: |
David Woodhouse | d2cca11 | 2014-01-28 23:13:25 +0000 | [diff] [blame] | 567 | FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_HI16 |
Mark Seaborn | c3bd177 | 2013-12-31 13:05:15 +0000 | [diff] [blame] | 568 | : Mips::fixup_Mips_DTPREL_HI; |
| 569 | break; |
| 570 | case MCSymbolRefExpr::VK_Mips_DTPREL_LO: |
David Woodhouse | d2cca11 | 2014-01-28 23:13:25 +0000 | [diff] [blame] | 571 | FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_DTPREL_LO16 |
Mark Seaborn | c3bd177 | 2013-12-31 13:05:15 +0000 | [diff] [blame] | 572 | : Mips::fixup_Mips_DTPREL_LO; |
| 573 | break; |
| 574 | case MCSymbolRefExpr::VK_Mips_GOTTPREL: |
| 575 | FixupKind = Mips::fixup_Mips_GOTTPREL; |
| 576 | break; |
| 577 | case MCSymbolRefExpr::VK_Mips_TPREL_HI: |
David Woodhouse | d2cca11 | 2014-01-28 23:13:25 +0000 | [diff] [blame] | 578 | FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_HI16 |
Mark Seaborn | c3bd177 | 2013-12-31 13:05:15 +0000 | [diff] [blame] | 579 | : Mips::fixup_Mips_TPREL_HI; |
| 580 | break; |
| 581 | case MCSymbolRefExpr::VK_Mips_TPREL_LO: |
David Woodhouse | d2cca11 | 2014-01-28 23:13:25 +0000 | [diff] [blame] | 582 | FixupKind = isMicroMips(STI) ? Mips::fixup_MICROMIPS_TLS_TPREL_LO16 |
Mark Seaborn | c3bd177 | 2013-12-31 13:05:15 +0000 | [diff] [blame] | 583 | : Mips::fixup_Mips_TPREL_LO; |
| 584 | break; |
| 585 | case MCSymbolRefExpr::VK_Mips_HIGHER: |
| 586 | FixupKind = Mips::fixup_Mips_HIGHER; |
| 587 | break; |
| 588 | case MCSymbolRefExpr::VK_Mips_HIGHEST: |
| 589 | FixupKind = Mips::fixup_Mips_HIGHEST; |
| 590 | break; |
| 591 | case MCSymbolRefExpr::VK_Mips_GOT_HI16: |
| 592 | FixupKind = Mips::fixup_Mips_GOT_HI16; |
| 593 | break; |
| 594 | case MCSymbolRefExpr::VK_Mips_GOT_LO16: |
| 595 | FixupKind = Mips::fixup_Mips_GOT_LO16; |
| 596 | break; |
| 597 | case MCSymbolRefExpr::VK_Mips_CALL_HI16: |
| 598 | FixupKind = Mips::fixup_Mips_CALL_HI16; |
| 599 | break; |
| 600 | case MCSymbolRefExpr::VK_Mips_CALL_LO16: |
| 601 | FixupKind = Mips::fixup_Mips_CALL_LO16; |
| 602 | break; |
Zoran Jovanovic | b355e8f | 2014-05-27 14:58:51 +0000 | [diff] [blame] | 603 | case MCSymbolRefExpr::VK_Mips_PCREL_HI16: |
| 604 | FixupKind = Mips::fixup_MIPS_PCHI16; |
| 605 | break; |
| 606 | case MCSymbolRefExpr::VK_Mips_PCREL_LO16: |
| 607 | FixupKind = Mips::fixup_MIPS_PCLO16; |
| 608 | break; |
Mark Seaborn | c3bd177 | 2013-12-31 13:05:15 +0000 | [diff] [blame] | 609 | } // switch |
Akira Hatanaka | fe384a2 | 2012-03-27 02:33:05 +0000 | [diff] [blame] | 610 | |
Jim Grosbach | 63661f8 | 2015-05-15 19:13:05 +0000 | [diff] [blame] | 611 | Fixups.push_back(MCFixup::create(0, Expr, MCFixupKind(FixupKind))); |
Jack Carter | b5cf590 | 2013-04-17 00:18:04 +0000 | [diff] [blame] | 612 | return 0; |
| 613 | } |
Akira Hatanaka | fe384a2 | 2012-03-27 02:33:05 +0000 | [diff] [blame] | 614 | return 0; |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Jack Carter | b5cf590 | 2013-04-17 00:18:04 +0000 | [diff] [blame] | 617 | /// getMachineOpValue - Return binary encoding of operand. If the machine |
| 618 | /// operand requires relocation, record the relocation and return zero. |
| 619 | unsigned MipsMCCodeEmitter:: |
| 620 | getMachineOpValue(const MCInst &MI, const MCOperand &MO, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 621 | SmallVectorImpl<MCFixup> &Fixups, |
| 622 | const MCSubtargetInfo &STI) const { |
Jack Carter | b5cf590 | 2013-04-17 00:18:04 +0000 | [diff] [blame] | 623 | if (MO.isReg()) { |
| 624 | unsigned Reg = MO.getReg(); |
Bill Wendling | bc07a89 | 2013-06-18 07:20:20 +0000 | [diff] [blame] | 625 | unsigned RegNo = Ctx.getRegisterInfo()->getEncodingValue(Reg); |
Jack Carter | b5cf590 | 2013-04-17 00:18:04 +0000 | [diff] [blame] | 626 | return RegNo; |
| 627 | } else if (MO.isImm()) { |
| 628 | return static_cast<unsigned>(MO.getImm()); |
| 629 | } else if (MO.isFPImm()) { |
| 630 | return static_cast<unsigned>(APFloat(MO.getFPImm()) |
| 631 | .bitcastToAPInt().getHiBits(32).getLimitedValue()); |
| 632 | } |
| 633 | // MO must be an Expr. |
| 634 | assert(MO.isExpr()); |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 635 | return getExprOpValue(MO.getExpr(),Fixups, STI); |
Jack Carter | b5cf590 | 2013-04-17 00:18:04 +0000 | [diff] [blame] | 636 | } |
| 637 | |
Matheus Almeida | 6b59c44 | 2013-12-05 11:06:22 +0000 | [diff] [blame] | 638 | /// getMSAMemEncoding - Return binary encoding of memory operand for LD/ST |
| 639 | /// instructions. |
| 640 | unsigned |
| 641 | MipsMCCodeEmitter::getMSAMemEncoding(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 642 | SmallVectorImpl<MCFixup> &Fixups, |
| 643 | const MCSubtargetInfo &STI) const { |
Matheus Almeida | 6b59c44 | 2013-12-05 11:06:22 +0000 | [diff] [blame] | 644 | // Base register is encoded in bits 20-16, offset is encoded in bits 15-0. |
| 645 | assert(MI.getOperand(OpNo).isReg()); |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 646 | unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups, STI) << 16; |
| 647 | unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI); |
Matheus Almeida | 6b59c44 | 2013-12-05 11:06:22 +0000 | [diff] [blame] | 648 | |
| 649 | // The immediate field of an LD/ST instruction is scaled which means it must |
| 650 | // be divided (when encoding) by the size (in bytes) of the instructions' |
| 651 | // data format. |
| 652 | // .b - 1 byte |
| 653 | // .h - 2 bytes |
| 654 | // .w - 4 bytes |
| 655 | // .d - 8 bytes |
| 656 | switch(MI.getOpcode()) |
| 657 | { |
| 658 | default: |
| 659 | assert (0 && "Unexpected instruction"); |
| 660 | break; |
| 661 | case Mips::LD_B: |
| 662 | case Mips::ST_B: |
| 663 | // We don't need to scale the offset in this case |
| 664 | break; |
| 665 | case Mips::LD_H: |
| 666 | case Mips::ST_H: |
| 667 | OffBits >>= 1; |
| 668 | break; |
| 669 | case Mips::LD_W: |
| 670 | case Mips::ST_W: |
| 671 | OffBits >>= 2; |
| 672 | break; |
| 673 | case Mips::LD_D: |
| 674 | case Mips::ST_D: |
| 675 | OffBits >>= 3; |
| 676 | break; |
| 677 | } |
| 678 | |
| 679 | return (OffBits & 0xFFFF) | RegBits; |
| 680 | } |
| 681 | |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 682 | /// getMemEncoding - Return binary encoding of memory related operand. |
| 683 | /// If the offset operand requires relocation, record the relocation. |
| 684 | unsigned |
| 685 | MipsMCCodeEmitter::getMemEncoding(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 686 | SmallVectorImpl<MCFixup> &Fixups, |
| 687 | const MCSubtargetInfo &STI) const { |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 688 | // Base register is encoded in bits 20-16, offset is encoded in bits 15-0. |
| 689 | assert(MI.getOperand(OpNo).isReg()); |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 690 | unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups, STI) << 16; |
| 691 | unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI); |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 692 | |
| 693 | return (OffBits & 0xFFFF) | RegBits; |
| 694 | } |
| 695 | |
Jack Carter | 9770097 | 2013-08-13 20:19:16 +0000 | [diff] [blame] | 696 | unsigned MipsMCCodeEmitter:: |
Jozef Kolek | e8c9d1e | 2014-11-24 14:39:13 +0000 | [diff] [blame] | 697 | getMemEncodingMMImm4(const MCInst &MI, unsigned OpNo, |
| 698 | SmallVectorImpl<MCFixup> &Fixups, |
| 699 | const MCSubtargetInfo &STI) const { |
| 700 | // Base register is encoded in bits 6-4, offset is encoded in bits 3-0. |
| 701 | assert(MI.getOperand(OpNo).isReg()); |
| 702 | unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), |
| 703 | Fixups, STI) << 4; |
| 704 | unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), |
| 705 | Fixups, STI); |
| 706 | |
| 707 | return (OffBits & 0xF) | RegBits; |
| 708 | } |
| 709 | |
| 710 | unsigned MipsMCCodeEmitter:: |
| 711 | getMemEncodingMMImm4Lsl1(const MCInst &MI, unsigned OpNo, |
| 712 | SmallVectorImpl<MCFixup> &Fixups, |
| 713 | const MCSubtargetInfo &STI) const { |
| 714 | // Base register is encoded in bits 6-4, offset is encoded in bits 3-0. |
| 715 | assert(MI.getOperand(OpNo).isReg()); |
| 716 | unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), |
| 717 | Fixups, STI) << 4; |
| 718 | unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), |
| 719 | Fixups, STI) >> 1; |
| 720 | |
| 721 | return (OffBits & 0xF) | RegBits; |
| 722 | } |
| 723 | |
| 724 | unsigned MipsMCCodeEmitter:: |
| 725 | getMemEncodingMMImm4Lsl2(const MCInst &MI, unsigned OpNo, |
| 726 | SmallVectorImpl<MCFixup> &Fixups, |
| 727 | const MCSubtargetInfo &STI) const { |
| 728 | // Base register is encoded in bits 6-4, offset is encoded in bits 3-0. |
| 729 | assert(MI.getOperand(OpNo).isReg()); |
| 730 | unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), |
| 731 | Fixups, STI) << 4; |
| 732 | unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), |
| 733 | Fixups, STI) >> 2; |
| 734 | |
| 735 | return (OffBits & 0xF) | RegBits; |
| 736 | } |
| 737 | |
| 738 | unsigned MipsMCCodeEmitter:: |
Jozef Kolek | 12c6982 | 2014-12-23 16:16:33 +0000 | [diff] [blame] | 739 | getMemEncodingMMSPImm5Lsl2(const MCInst &MI, unsigned OpNo, |
| 740 | SmallVectorImpl<MCFixup> &Fixups, |
| 741 | const MCSubtargetInfo &STI) const { |
| 742 | // Register is encoded in bits 9-5, offset is encoded in bits 4-0. |
| 743 | assert(MI.getOperand(OpNo).isReg() && |
| 744 | MI.getOperand(OpNo).getReg() == Mips::SP && |
| 745 | "Unexpected base register!"); |
| 746 | unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), |
| 747 | Fixups, STI) >> 2; |
| 748 | |
| 749 | return OffBits & 0x1F; |
| 750 | } |
| 751 | |
| 752 | unsigned MipsMCCodeEmitter:: |
Jozef Kolek | e10a02e | 2015-01-28 17:27:26 +0000 | [diff] [blame] | 753 | getMemEncodingMMGPImm7Lsl2(const MCInst &MI, unsigned OpNo, |
| 754 | SmallVectorImpl<MCFixup> &Fixups, |
| 755 | const MCSubtargetInfo &STI) const { |
| 756 | // Register is encoded in bits 9-7, offset is encoded in bits 6-0. |
| 757 | assert(MI.getOperand(OpNo).isReg() && |
| 758 | MI.getOperand(OpNo).getReg() == Mips::GP && |
| 759 | "Unexpected base register!"); |
| 760 | |
| 761 | unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), |
| 762 | Fixups, STI) >> 2; |
| 763 | |
| 764 | return OffBits & 0x7F; |
| 765 | } |
| 766 | |
| 767 | unsigned MipsMCCodeEmitter:: |
Jack Carter | 9770097 | 2013-08-13 20:19:16 +0000 | [diff] [blame] | 768 | getMemEncodingMMImm12(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 769 | SmallVectorImpl<MCFixup> &Fixups, |
| 770 | const MCSubtargetInfo &STI) const { |
Zoran Jovanovic | a4c4b5f | 2014-11-19 16:44:02 +0000 | [diff] [blame] | 771 | // opNum can be invalid if instruction had reglist as operand. |
| 772 | // MemOperand is always last operand of instruction (base + offset). |
| 773 | switch (MI.getOpcode()) { |
| 774 | default: |
| 775 | break; |
| 776 | case Mips::SWM32_MM: |
| 777 | case Mips::LWM32_MM: |
| 778 | OpNo = MI.getNumOperands() - 2; |
| 779 | break; |
| 780 | } |
| 781 | |
Jack Carter | 9770097 | 2013-08-13 20:19:16 +0000 | [diff] [blame] | 782 | // Base register is encoded in bits 20-16, offset is encoded in bits 11-0. |
| 783 | assert(MI.getOperand(OpNo).isReg()); |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 784 | unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI) << 16; |
| 785 | unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI); |
Jack Carter | 9770097 | 2013-08-13 20:19:16 +0000 | [diff] [blame] | 786 | |
| 787 | return (OffBits & 0x0FFF) | RegBits; |
| 788 | } |
| 789 | |
Zoran Jovanovic | f9a0250 | 2014-11-27 18:28:59 +0000 | [diff] [blame] | 790 | unsigned MipsMCCodeEmitter:: |
| 791 | getMemEncodingMMImm4sp(const MCInst &MI, unsigned OpNo, |
| 792 | SmallVectorImpl<MCFixup> &Fixups, |
| 793 | const MCSubtargetInfo &STI) const { |
| 794 | // opNum can be invalid if instruction had reglist as operand |
| 795 | // MemOperand is always last operand of instruction (base + offset) |
| 796 | switch (MI.getOpcode()) { |
| 797 | default: |
| 798 | break; |
| 799 | case Mips::SWM16_MM: |
| 800 | case Mips::LWM16_MM: |
| 801 | OpNo = MI.getNumOperands() - 2; |
| 802 | break; |
| 803 | } |
| 804 | |
| 805 | // Offset is encoded in bits 4-0. |
| 806 | assert(MI.getOperand(OpNo).isReg()); |
| 807 | // Base register is always SP - thus it is not encoded. |
| 808 | assert(MI.getOperand(OpNo+1).isImm()); |
| 809 | unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI); |
| 810 | |
| 811 | return ((OffBits >> 2) & 0x0F); |
| 812 | } |
| 813 | |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 814 | unsigned |
| 815 | MipsMCCodeEmitter::getSizeExtEncoding(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 816 | SmallVectorImpl<MCFixup> &Fixups, |
| 817 | const MCSubtargetInfo &STI) const { |
Akira Hatanaka | 049e9e4 | 2011-11-23 22:19:28 +0000 | [diff] [blame] | 818 | assert(MI.getOperand(OpNo).isImm()); |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 819 | unsigned SizeEncoding = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI); |
Bruno Cardoso Lopes | 56b70de | 2011-12-07 22:35:30 +0000 | [diff] [blame] | 820 | return SizeEncoding - 1; |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 821 | } |
| 822 | |
Akira Hatanaka | 049e9e4 | 2011-11-23 22:19:28 +0000 | [diff] [blame] | 823 | // FIXME: should be called getMSBEncoding |
| 824 | // |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 825 | unsigned |
| 826 | MipsMCCodeEmitter::getSizeInsEncoding(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 827 | SmallVectorImpl<MCFixup> &Fixups, |
| 828 | const MCSubtargetInfo &STI) const { |
Akira Hatanaka | 049e9e4 | 2011-11-23 22:19:28 +0000 | [diff] [blame] | 829 | assert(MI.getOperand(OpNo-1).isImm()); |
| 830 | assert(MI.getOperand(OpNo).isImm()); |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 831 | unsigned Position = getMachineOpValue(MI, MI.getOperand(OpNo-1), Fixups, STI); |
| 832 | unsigned Size = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI); |
Akira Hatanaka | 049e9e4 | 2011-11-23 22:19:28 +0000 | [diff] [blame] | 833 | |
Bruno Cardoso Lopes | 56b70de | 2011-12-07 22:35:30 +0000 | [diff] [blame] | 834 | return Position + Size - 1; |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 835 | } |
| 836 | |
Matheus Almeida | 779c593 | 2013-11-18 12:32:49 +0000 | [diff] [blame] | 837 | unsigned |
| 838 | MipsMCCodeEmitter::getLSAImmEncoding(const MCInst &MI, unsigned OpNo, |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 839 | SmallVectorImpl<MCFixup> &Fixups, |
| 840 | const MCSubtargetInfo &STI) const { |
Matheus Almeida | 779c593 | 2013-11-18 12:32:49 +0000 | [diff] [blame] | 841 | assert(MI.getOperand(OpNo).isImm()); |
| 842 | // The immediate is encoded as 'immediate - 1'. |
David Woodhouse | 3fa98a6 | 2014-01-28 23:13:18 +0000 | [diff] [blame] | 843 | return getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI) - 1; |
Matheus Almeida | 779c593 | 2013-11-18 12:32:49 +0000 | [diff] [blame] | 844 | } |
| 845 | |
Daniel Sanders | b59e1a4 | 2014-05-15 10:45:58 +0000 | [diff] [blame] | 846 | unsigned |
| 847 | MipsMCCodeEmitter::getSimm19Lsl2Encoding(const MCInst &MI, unsigned OpNo, |
| 848 | SmallVectorImpl<MCFixup> &Fixups, |
| 849 | const MCSubtargetInfo &STI) const { |
Zoran Jovanovic | b9c07f3 | 2014-06-12 12:40:00 +0000 | [diff] [blame] | 850 | const MCOperand &MO = MI.getOperand(OpNo); |
| 851 | if (MO.isImm()) { |
| 852 | // The immediate is encoded as 'immediate << 2'. |
| 853 | unsigned Res = getMachineOpValue(MI, MO, Fixups, STI); |
| 854 | assert((Res & 3) == 0); |
| 855 | return Res >> 2; |
| 856 | } |
| 857 | |
| 858 | assert(MO.isExpr() && |
| 859 | "getSimm19Lsl2Encoding expects only expressions or an immediate"); |
| 860 | |
| 861 | const MCExpr *Expr = MO.getExpr(); |
Jim Grosbach | 63661f8 | 2015-05-15 19:13:05 +0000 | [diff] [blame] | 862 | Fixups.push_back(MCFixup::create(0, Expr, |
Zoran Jovanovic | b9c07f3 | 2014-06-12 12:40:00 +0000 | [diff] [blame] | 863 | MCFixupKind(Mips::fixup_MIPS_PC19_S2))); |
| 864 | return 0; |
Daniel Sanders | b59e1a4 | 2014-05-15 10:45:58 +0000 | [diff] [blame] | 865 | } |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 866 | |
Zoran Jovanovic | 2855142 | 2014-06-09 09:49:51 +0000 | [diff] [blame] | 867 | unsigned |
| 868 | MipsMCCodeEmitter::getSimm18Lsl3Encoding(const MCInst &MI, unsigned OpNo, |
| 869 | SmallVectorImpl<MCFixup> &Fixups, |
| 870 | const MCSubtargetInfo &STI) const { |
Zoran Jovanovic | a5acdcf | 2014-06-13 14:26:47 +0000 | [diff] [blame] | 871 | const MCOperand &MO = MI.getOperand(OpNo); |
| 872 | if (MO.isImm()) { |
| 873 | // The immediate is encoded as 'immediate << 3'. |
| 874 | unsigned Res = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI); |
| 875 | assert((Res & 7) == 0); |
| 876 | return Res >> 3; |
| 877 | } |
| 878 | |
| 879 | assert(MO.isExpr() && |
| 880 | "getSimm18Lsl2Encoding expects only expressions or an immediate"); |
| 881 | |
| 882 | const MCExpr *Expr = MO.getExpr(); |
Jim Grosbach | 63661f8 | 2015-05-15 19:13:05 +0000 | [diff] [blame] | 883 | Fixups.push_back(MCFixup::create(0, Expr, |
Zoran Jovanovic | a5acdcf | 2014-06-13 14:26:47 +0000 | [diff] [blame] | 884 | MCFixupKind(Mips::fixup_MIPS_PC18_S3))); |
| 885 | return 0; |
Zoran Jovanovic | 2855142 | 2014-06-09 09:49:51 +0000 | [diff] [blame] | 886 | } |
| 887 | |
Zoran Jovanovic | 4a00fdc | 2014-10-23 10:42:01 +0000 | [diff] [blame] | 888 | unsigned |
| 889 | MipsMCCodeEmitter::getUImm3Mod8Encoding(const MCInst &MI, unsigned OpNo, |
| 890 | SmallVectorImpl<MCFixup> &Fixups, |
| 891 | const MCSubtargetInfo &STI) const { |
| 892 | assert(MI.getOperand(OpNo).isImm()); |
| 893 | const MCOperand &MO = MI.getOperand(OpNo); |
| 894 | return MO.getImm() % 8; |
| 895 | } |
| 896 | |
Zoran Jovanovic | 8853171 | 2014-11-05 17:31:00 +0000 | [diff] [blame] | 897 | unsigned |
| 898 | MipsMCCodeEmitter::getUImm4AndValue(const MCInst &MI, unsigned OpNo, |
| 899 | SmallVectorImpl<MCFixup> &Fixups, |
| 900 | const MCSubtargetInfo &STI) const { |
| 901 | assert(MI.getOperand(OpNo).isImm()); |
| 902 | const MCOperand &MO = MI.getOperand(OpNo); |
| 903 | unsigned Value = MO.getImm(); |
| 904 | switch (Value) { |
| 905 | case 128: return 0x0; |
| 906 | case 1: return 0x1; |
| 907 | case 2: return 0x2; |
| 908 | case 3: return 0x3; |
| 909 | case 4: return 0x4; |
| 910 | case 7: return 0x5; |
| 911 | case 8: return 0x6; |
| 912 | case 15: return 0x7; |
| 913 | case 16: return 0x8; |
| 914 | case 31: return 0x9; |
| 915 | case 32: return 0xa; |
| 916 | case 63: return 0xb; |
| 917 | case 64: return 0xc; |
| 918 | case 255: return 0xd; |
| 919 | case 32768: return 0xe; |
| 920 | case 65535: return 0xf; |
| 921 | } |
| 922 | llvm_unreachable("Unexpected value"); |
| 923 | } |
| 924 | |
Zoran Jovanovic | a4c4b5f | 2014-11-19 16:44:02 +0000 | [diff] [blame] | 925 | unsigned |
| 926 | MipsMCCodeEmitter::getRegisterListOpValue(const MCInst &MI, unsigned OpNo, |
| 927 | SmallVectorImpl<MCFixup> &Fixups, |
| 928 | const MCSubtargetInfo &STI) const { |
| 929 | unsigned res = 0; |
| 930 | |
| 931 | // Register list operand is always first operand of instruction and it is |
| 932 | // placed before memory operand (register + imm). |
| 933 | |
| 934 | for (unsigned I = OpNo, E = MI.getNumOperands() - 2; I < E; ++I) { |
| 935 | unsigned Reg = MI.getOperand(I).getReg(); |
| 936 | unsigned RegNo = Ctx.getRegisterInfo()->getEncodingValue(Reg); |
| 937 | if (RegNo != 31) |
| 938 | res++; |
| 939 | else |
| 940 | res |= 0x10; |
| 941 | } |
| 942 | return res; |
| 943 | } |
| 944 | |
Zoran Jovanovic | f9a0250 | 2014-11-27 18:28:59 +0000 | [diff] [blame] | 945 | unsigned |
| 946 | MipsMCCodeEmitter::getRegisterListOpValue16(const MCInst &MI, unsigned OpNo, |
| 947 | SmallVectorImpl<MCFixup> &Fixups, |
| 948 | const MCSubtargetInfo &STI) const { |
| 949 | return (MI.getNumOperands() - 4); |
| 950 | } |
| 951 | |
Zoran Jovanovic | 2deca34 | 2014-12-16 14:59:10 +0000 | [diff] [blame] | 952 | unsigned |
| 953 | MipsMCCodeEmitter::getRegisterPairOpValue(const MCInst &MI, unsigned OpNo, |
| 954 | SmallVectorImpl<MCFixup> &Fixups, |
| 955 | const MCSubtargetInfo &STI) const { |
| 956 | return getMachineOpValue(MI, MI.getOperand(OpNo), Fixups, STI); |
| 957 | } |
| 958 | |
Jozef Kolek | 2c6d732 | 2015-01-21 12:10:11 +0000 | [diff] [blame] | 959 | unsigned |
Zoran Jovanovic | 4168867 | 2015-02-10 16:36:20 +0000 | [diff] [blame] | 960 | MipsMCCodeEmitter::getMovePRegPairOpValue(const MCInst &MI, unsigned OpNo, |
| 961 | SmallVectorImpl<MCFixup> &Fixups, |
| 962 | const MCSubtargetInfo &STI) const { |
| 963 | unsigned res = 0; |
| 964 | |
| 965 | if (MI.getOperand(0).getReg() == Mips::A1 && |
| 966 | MI.getOperand(1).getReg() == Mips::A2) |
| 967 | res = 0; |
| 968 | else if (MI.getOperand(0).getReg() == Mips::A1 && |
| 969 | MI.getOperand(1).getReg() == Mips::A3) |
| 970 | res = 1; |
| 971 | else if (MI.getOperand(0).getReg() == Mips::A2 && |
| 972 | MI.getOperand(1).getReg() == Mips::A3) |
| 973 | res = 2; |
| 974 | else if (MI.getOperand(0).getReg() == Mips::A0 && |
| 975 | MI.getOperand(1).getReg() == Mips::S5) |
| 976 | res = 3; |
| 977 | else if (MI.getOperand(0).getReg() == Mips::A0 && |
| 978 | MI.getOperand(1).getReg() == Mips::S6) |
| 979 | res = 4; |
| 980 | else if (MI.getOperand(0).getReg() == Mips::A0 && |
| 981 | MI.getOperand(1).getReg() == Mips::A1) |
| 982 | res = 5; |
| 983 | else if (MI.getOperand(0).getReg() == Mips::A0 && |
| 984 | MI.getOperand(1).getReg() == Mips::A2) |
| 985 | res = 6; |
| 986 | else if (MI.getOperand(0).getReg() == Mips::A0 && |
| 987 | MI.getOperand(1).getReg() == Mips::A3) |
| 988 | res = 7; |
| 989 | |
| 990 | return res; |
| 991 | } |
| 992 | |
| 993 | unsigned |
Jozef Kolek | 2c6d732 | 2015-01-21 12:10:11 +0000 | [diff] [blame] | 994 | MipsMCCodeEmitter::getSimm23Lsl2Encoding(const MCInst &MI, unsigned OpNo, |
| 995 | SmallVectorImpl<MCFixup> &Fixups, |
| 996 | const MCSubtargetInfo &STI) const { |
| 997 | const MCOperand &MO = MI.getOperand(OpNo); |
| 998 | assert(MO.isImm() && "getSimm23Lsl2Encoding expects only an immediate"); |
| 999 | // The immediate is encoded as 'immediate >> 2'. |
| 1000 | unsigned Res = static_cast<unsigned>(MO.getImm()); |
| 1001 | assert((Res & 3) == 0); |
| 1002 | return Res >> 2; |
| 1003 | } |
| 1004 | |
Daniel Sanders | b59e1a4 | 2014-05-15 10:45:58 +0000 | [diff] [blame] | 1005 | #include "MipsGenMCCodeEmitter.inc" |