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