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