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