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