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