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 | // |
| 14 | #define DEBUG_TYPE "mccodeemitter" |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 15 | #include "MCTargetDesc/MipsBaseInfo.h" |
| 16 | #include "MCTargetDesc/MipsFixupKinds.h" |
| 17 | #include "MCTargetDesc/MipsMCTargetDesc.h" |
| 18 | #include "llvm/ADT/APFloat.h" |
| 19 | #include "llvm/ADT/Statistic.h" |
Akira Hatanaka | 750ecec | 2011-09-30 20:40:03 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCCodeEmitter.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" |
| 23 | #include "llvm/MC/MCInst.h" |
| 24 | #include "llvm/MC/MCInstrInfo.h" |
| 25 | #include "llvm/MC/MCRegisterInfo.h" |
| 26 | #include "llvm/MC/MCSubtargetInfo.h" |
Akira Hatanaka | 750ecec | 2011-09-30 20:40:03 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Akira Hatanaka | 750ecec | 2011-09-30 20:40:03 +0000 | [diff] [blame] | 28 | |
Akira Hatanaka | be6a818 | 2013-04-19 19:03:11 +0000 | [diff] [blame] | 29 | #define GET_INSTRMAP_INFO |
| 30 | #include "MipsGenInstrInfo.inc" |
| 31 | |
Akira Hatanaka | 750ecec | 2011-09-30 20:40:03 +0000 | [diff] [blame] | 32 | using namespace llvm; |
| 33 | |
| 34 | namespace { |
| 35 | class MipsMCCodeEmitter : public MCCodeEmitter { |
Craig Topper | 2ed23ce | 2012-09-15 17:08:51 +0000 | [diff] [blame] | 36 | MipsMCCodeEmitter(const MipsMCCodeEmitter &) LLVM_DELETED_FUNCTION; |
| 37 | void operator=(const MipsMCCodeEmitter &) LLVM_DELETED_FUNCTION; |
Akira Hatanaka | 750ecec | 2011-09-30 20:40:03 +0000 | [diff] [blame] | 38 | const MCInstrInfo &MCII; |
Akira Hatanaka | 5d6faed | 2012-12-10 20:04:40 +0000 | [diff] [blame] | 39 | MCContext &Ctx; |
Akira Hatanaka | be6a818 | 2013-04-19 19:03:11 +0000 | [diff] [blame] | 40 | const MCSubtargetInfo &STI; |
Akira Hatanaka | 1ee768d | 2012-03-01 01:53:15 +0000 | [diff] [blame] | 41 | bool IsLittleEndian; |
Jack Carter | 7bd3c7d | 2013-08-08 23:30:40 +0000 | [diff] [blame] | 42 | bool IsMicroMips; |
Akira Hatanaka | 750ecec | 2011-09-30 20:40:03 +0000 | [diff] [blame] | 43 | |
| 44 | public: |
Jack Carter | ab3cb42 | 2013-02-19 22:04:37 +0000 | [diff] [blame] | 45 | MipsMCCodeEmitter(const MCInstrInfo &mcii, MCContext &Ctx_, |
| 46 | const MCSubtargetInfo &sti, bool IsLittle) : |
Jack Carter | 7bd3c7d | 2013-08-08 23:30:40 +0000 | [diff] [blame] | 47 | MCII(mcii), Ctx(Ctx_), STI (sti), IsLittleEndian(IsLittle) { |
| 48 | IsMicroMips = STI.getFeatureBits() & Mips::FeatureMicroMips; |
| 49 | } |
Akira Hatanaka | 750ecec | 2011-09-30 20:40:03 +0000 | [diff] [blame] | 50 | |
| 51 | ~MipsMCCodeEmitter() {} |
| 52 | |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 53 | void EmitByte(unsigned char C, raw_ostream &OS) const { |
| 54 | OS << (char)C; |
Akira Hatanaka | 750ecec | 2011-09-30 20:40:03 +0000 | [diff] [blame] | 55 | } |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 56 | |
| 57 | void EmitInstruction(uint64_t Val, unsigned Size, raw_ostream &OS) const { |
| 58 | // Output the instruction encoding in little endian byte order. |
Jack Carter | 7bd3c7d | 2013-08-08 23:30:40 +0000 | [diff] [blame] | 59 | // Little-endian byte ordering: |
| 60 | // mips32r2: 4 | 3 | 2 | 1 |
| 61 | // microMIPS: 2 | 1 | 4 | 3 |
| 62 | if (IsLittleEndian && Size == 4 && IsMicroMips) { |
| 63 | EmitInstruction(Val>>16, 2, OS); |
| 64 | EmitInstruction(Val, 2, OS); |
| 65 | } else { |
| 66 | for (unsigned i = 0; i < Size; ++i) { |
| 67 | unsigned Shift = IsLittleEndian ? i * 8 : (Size - 1 - i) * 8; |
| 68 | EmitByte((Val >> Shift) & 0xff, OS); |
| 69 | } |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
| 73 | void EncodeInstruction(const MCInst &MI, raw_ostream &OS, |
| 74 | SmallVectorImpl<MCFixup> &Fixups) const; |
| 75 | |
| 76 | // getBinaryCodeForInstr - TableGen'erated function for getting the |
| 77 | // binary encoding for an instruction. |
Owen Anderson | d845d9d | 2012-01-24 18:37:29 +0000 | [diff] [blame] | 78 | uint64_t getBinaryCodeForInstr(const MCInst &MI, |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 79 | SmallVectorImpl<MCFixup> &Fixups) const; |
| 80 | |
| 81 | // getBranchJumpOpValue - Return binary encoding of the jump |
| 82 | // target operand. If the machine operand requires relocation, |
| 83 | // record the relocation and return zero. |
Mark Seaborn | 774c243 | 2013-12-29 10:47:04 +0000 | [diff] [blame] | 84 | unsigned getJumpTargetOpValue(const MCInst &MI, unsigned OpNo, |
| 85 | SmallVectorImpl<MCFixup> &Fixups) const; |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 86 | |
Zoran Jovanovic | 507e084 | 2013-10-29 16:38:59 +0000 | [diff] [blame] | 87 | // getBranchJumpOpValueMM - Return binary encoding of the microMIPS jump |
| 88 | // target operand. If the machine operand requires relocation, |
| 89 | // record the relocation and return zero. |
| 90 | unsigned getJumpTargetOpValueMM(const MCInst &MI, unsigned OpNo, |
| 91 | SmallVectorImpl<MCFixup> &Fixups) const; |
| 92 | |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 93 | // getBranchTargetOpValue - Return binary encoding of the branch |
| 94 | // target operand. If the machine operand requires relocation, |
| 95 | // record the relocation and return zero. |
| 96 | unsigned getBranchTargetOpValue(const MCInst &MI, unsigned OpNo, |
| 97 | SmallVectorImpl<MCFixup> &Fixups) const; |
| 98 | |
Zoran Jovanovic | 8a80aa7 | 2013-11-04 14:53:22 +0000 | [diff] [blame] | 99 | // getBranchTargetOpValue - Return binary encoding of the microMIPS branch |
| 100 | // target operand. If the machine operand requires relocation, |
| 101 | // record the relocation and return zero. |
| 102 | unsigned getBranchTargetOpValueMM(const MCInst &MI, unsigned OpNo, |
| 103 | SmallVectorImpl<MCFixup> &Fixups) const; |
| 104 | |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 105 | // getMachineOpValue - Return binary encoding of operand. If the machin |
| 106 | // operand requires relocation, record the relocation and return zero. |
| 107 | unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO, |
| 108 | SmallVectorImpl<MCFixup> &Fixups) const; |
| 109 | |
Matheus Almeida | 6b59c44 | 2013-12-05 11:06:22 +0000 | [diff] [blame] | 110 | unsigned getMSAMemEncoding(const MCInst &MI, unsigned OpNo, |
| 111 | SmallVectorImpl<MCFixup> &Fixups) const; |
| 112 | |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 113 | unsigned getMemEncoding(const MCInst &MI, unsigned OpNo, |
| 114 | SmallVectorImpl<MCFixup> &Fixups) const; |
Jack Carter | 9770097 | 2013-08-13 20:19:16 +0000 | [diff] [blame] | 115 | unsigned getMemEncodingMMImm12(const MCInst &MI, unsigned OpNo, |
| 116 | SmallVectorImpl<MCFixup> &Fixups) const; |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 117 | unsigned getSizeExtEncoding(const MCInst &MI, unsigned OpNo, |
| 118 | SmallVectorImpl<MCFixup> &Fixups) const; |
| 119 | unsigned getSizeInsEncoding(const MCInst &MI, unsigned OpNo, |
| 120 | SmallVectorImpl<MCFixup> &Fixups) const; |
| 121 | |
Matheus Almeida | 779c593 | 2013-11-18 12:32:49 +0000 | [diff] [blame] | 122 | // getLSAImmEncoding - Return binary encoding of LSA immediate. |
| 123 | unsigned getLSAImmEncoding(const MCInst &MI, unsigned OpNo, |
| 124 | SmallVectorImpl<MCFixup> &Fixups) const; |
| 125 | |
Jack Carter | b5cf590 | 2013-04-17 00:18:04 +0000 | [diff] [blame] | 126 | unsigned |
| 127 | getExprOpValue(const MCExpr *Expr,SmallVectorImpl<MCFixup> &Fixups) const; |
| 128 | |
Akira Hatanaka | 750ecec | 2011-09-30 20:40:03 +0000 | [diff] [blame] | 129 | }; // class MipsMCCodeEmitter |
| 130 | } // namespace |
| 131 | |
Akira Hatanaka | 1ee768d | 2012-03-01 01:53:15 +0000 | [diff] [blame] | 132 | MCCodeEmitter *llvm::createMipsMCCodeEmitterEB(const MCInstrInfo &MCII, |
Jim Grosbach | c3b0427 | 2012-05-15 17:35:52 +0000 | [diff] [blame] | 133 | const MCRegisterInfo &MRI, |
Akira Hatanaka | 1ee768d | 2012-03-01 01:53:15 +0000 | [diff] [blame] | 134 | const MCSubtargetInfo &STI, |
| 135 | MCContext &Ctx) |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 136 | { |
Jack Carter | ab3cb42 | 2013-02-19 22:04:37 +0000 | [diff] [blame] | 137 | return new MipsMCCodeEmitter(MCII, Ctx, STI, false); |
Akira Hatanaka | 1ee768d | 2012-03-01 01:53:15 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | MCCodeEmitter *llvm::createMipsMCCodeEmitterEL(const MCInstrInfo &MCII, |
Jim Grosbach | c3b0427 | 2012-05-15 17:35:52 +0000 | [diff] [blame] | 141 | const MCRegisterInfo &MRI, |
Akira Hatanaka | 1ee768d | 2012-03-01 01:53:15 +0000 | [diff] [blame] | 142 | const MCSubtargetInfo &STI, |
| 143 | MCContext &Ctx) |
| 144 | { |
Jack Carter | ab3cb42 | 2013-02-19 22:04:37 +0000 | [diff] [blame] | 145 | return new MipsMCCodeEmitter(MCII, Ctx, STI, true); |
Akira Hatanaka | 750ecec | 2011-09-30 20:40:03 +0000 | [diff] [blame] | 146 | } |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 147 | |
Rafael Espindola | f30f2cc | 2013-05-27 22:34:59 +0000 | [diff] [blame] | 148 | |
| 149 | // If the D<shift> instruction has a shift amount that is greater |
| 150 | // than 31 (checked in calling routine), lower it to a D<shift>32 instruction |
| 151 | static void LowerLargeShift(MCInst& Inst) { |
| 152 | |
| 153 | assert(Inst.getNumOperands() == 3 && "Invalid no. of operands for shift!"); |
| 154 | assert(Inst.getOperand(2).isImm()); |
| 155 | |
| 156 | int64_t Shift = Inst.getOperand(2).getImm(); |
| 157 | if (Shift <= 31) |
| 158 | return; // Do nothing |
| 159 | Shift -= 32; |
| 160 | |
| 161 | // saminus32 |
| 162 | Inst.getOperand(2).setImm(Shift); |
| 163 | |
| 164 | switch (Inst.getOpcode()) { |
| 165 | default: |
| 166 | // Calling function is not synchronized |
| 167 | llvm_unreachable("Unexpected shift instruction"); |
| 168 | case Mips::DSLL: |
| 169 | Inst.setOpcode(Mips::DSLL32); |
| 170 | return; |
| 171 | case Mips::DSRL: |
| 172 | Inst.setOpcode(Mips::DSRL32); |
| 173 | return; |
| 174 | case Mips::DSRA: |
| 175 | Inst.setOpcode(Mips::DSRA32); |
| 176 | return; |
Akira Hatanaka | 6a3fe57 | 2013-09-07 00:18:01 +0000 | [diff] [blame] | 177 | case Mips::DROTR: |
| 178 | Inst.setOpcode(Mips::DROTR32); |
| 179 | return; |
Rafael Espindola | f30f2cc | 2013-05-27 22:34:59 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
| 183 | // Pick a DEXT or DINS instruction variant based on the pos and size operands |
| 184 | static void LowerDextDins(MCInst& InstIn) { |
| 185 | int Opcode = InstIn.getOpcode(); |
| 186 | |
| 187 | if (Opcode == Mips::DEXT) |
| 188 | assert(InstIn.getNumOperands() == 4 && |
| 189 | "Invalid no. of machine operands for DEXT!"); |
| 190 | else // Only DEXT and DINS are possible |
| 191 | assert(InstIn.getNumOperands() == 5 && |
| 192 | "Invalid no. of machine operands for DINS!"); |
| 193 | |
| 194 | assert(InstIn.getOperand(2).isImm()); |
| 195 | int64_t pos = InstIn.getOperand(2).getImm(); |
| 196 | assert(InstIn.getOperand(3).isImm()); |
| 197 | int64_t size = InstIn.getOperand(3).getImm(); |
| 198 | |
| 199 | if (size <= 32) { |
| 200 | if (pos < 32) // DEXT/DINS, do nothing |
| 201 | return; |
| 202 | // DEXTU/DINSU |
| 203 | InstIn.getOperand(2).setImm(pos - 32); |
| 204 | InstIn.setOpcode((Opcode == Mips::DEXT) ? Mips::DEXTU : Mips::DINSU); |
| 205 | return; |
| 206 | } |
| 207 | // DEXTM/DINSM |
| 208 | assert(pos < 32 && "DEXT/DINS cannot have both size and pos > 32"); |
| 209 | InstIn.getOperand(3).setImm(size - 32); |
| 210 | InstIn.setOpcode((Opcode == Mips::DEXT) ? Mips::DEXTM : Mips::DINSM); |
| 211 | return; |
| 212 | } |
| 213 | |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 214 | /// EncodeInstruction - Emit the instruction. |
Jack Carter | 4e07b95d | 2013-08-27 19:45:28 +0000 | [diff] [blame] | 215 | /// Size the instruction with Desc.getSize(). |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 216 | void MipsMCCodeEmitter:: |
| 217 | EncodeInstruction(const MCInst &MI, raw_ostream &OS, |
| 218 | SmallVectorImpl<MCFixup> &Fixups) const |
| 219 | { |
Jack Carter | aa7aeaa | 2012-10-02 23:09:40 +0000 | [diff] [blame] | 220 | |
| 221 | // Non-pseudo instructions that get changed for direct object |
| 222 | // only based on operand values. |
| 223 | // If this list of instructions get much longer we will move |
| 224 | // the check to a function call. Until then, this is more efficient. |
| 225 | MCInst TmpInst = MI; |
| 226 | switch (MI.getOpcode()) { |
| 227 | // If shift amount is >= 32 it the inst needs to be lowered further |
| 228 | case Mips::DSLL: |
| 229 | case Mips::DSRL: |
| 230 | case Mips::DSRA: |
Akira Hatanaka | 6a3fe57 | 2013-09-07 00:18:01 +0000 | [diff] [blame] | 231 | case Mips::DROTR: |
Rafael Espindola | f30f2cc | 2013-05-27 22:34:59 +0000 | [diff] [blame] | 232 | LowerLargeShift(TmpInst); |
Jack Carter | aa7aeaa | 2012-10-02 23:09:40 +0000 | [diff] [blame] | 233 | break; |
| 234 | // Double extract instruction is chosen by pos and size operands |
| 235 | case Mips::DEXT: |
| 236 | case Mips::DINS: |
Rafael Espindola | f30f2cc | 2013-05-27 22:34:59 +0000 | [diff] [blame] | 237 | LowerDextDins(TmpInst); |
Jack Carter | aa7aeaa | 2012-10-02 23:09:40 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Jack Carter | 9770097 | 2013-08-13 20:19:16 +0000 | [diff] [blame] | 240 | unsigned long N = Fixups.size(); |
Jack Carter | aa7aeaa | 2012-10-02 23:09:40 +0000 | [diff] [blame] | 241 | uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups); |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 242 | |
| 243 | // Check for unimplemented opcodes. |
Jack Carter | aa7aeaa | 2012-10-02 23:09:40 +0000 | [diff] [blame] | 244 | // 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] | 245 | // so we have to special check for them. |
Jack Carter | aa7aeaa | 2012-10-02 23:09:40 +0000 | [diff] [blame] | 246 | unsigned Opcode = TmpInst.getOpcode(); |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 247 | if ((Opcode != Mips::NOP) && (Opcode != Mips::SLL) && !Binary) |
| 248 | llvm_unreachable("unimplemented opcode in EncodeInstruction()"); |
| 249 | |
Akira Hatanaka | be6a818 | 2013-04-19 19:03:11 +0000 | [diff] [blame] | 250 | if (STI.getFeatureBits() & Mips::FeatureMicroMips) { |
| 251 | int NewOpcode = Mips::Std2MicroMips (Opcode, Mips::Arch_micromips); |
| 252 | if (NewOpcode != -1) { |
Jack Carter | 9770097 | 2013-08-13 20:19:16 +0000 | [diff] [blame] | 253 | if (Fixups.size() > N) |
| 254 | Fixups.pop_back(); |
Akira Hatanaka | be6a818 | 2013-04-19 19:03:11 +0000 | [diff] [blame] | 255 | Opcode = NewOpcode; |
| 256 | TmpInst.setOpcode (NewOpcode); |
| 257 | Binary = getBinaryCodeForInstr(TmpInst, Fixups); |
| 258 | } |
| 259 | } |
| 260 | |
Jack Carter | aa7aeaa | 2012-10-02 23:09:40 +0000 | [diff] [blame] | 261 | const MCInstrDesc &Desc = MCII.get(TmpInst.getOpcode()); |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 262 | |
Jack Carter | 5b5559d | 2012-10-03 21:58:54 +0000 | [diff] [blame] | 263 | // Get byte count of instruction |
| 264 | unsigned Size = Desc.getSize(); |
| 265 | if (!Size) |
| 266 | llvm_unreachable("Desc.getSize() returns 0"); |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 267 | |
| 268 | EmitInstruction(Binary, Size, OS); |
| 269 | } |
| 270 | |
| 271 | /// getBranchTargetOpValue - Return binary encoding of the branch |
| 272 | /// target operand. If the machine operand requires relocation, |
| 273 | /// record the relocation and return zero. |
| 274 | unsigned MipsMCCodeEmitter:: |
| 275 | getBranchTargetOpValue(const MCInst &MI, unsigned OpNo, |
| 276 | SmallVectorImpl<MCFixup> &Fixups) const { |
| 277 | |
| 278 | const MCOperand &MO = MI.getOperand(OpNo); |
Jack Carter | 71e6a74 | 2012-09-06 00:43:26 +0000 | [diff] [blame] | 279 | |
Jack Carter | 4f69a0f | 2013-03-22 00:29:10 +0000 | [diff] [blame] | 280 | // If the destination is an immediate, divide by 4. |
| 281 | if (MO.isImm()) return MO.getImm() >> 2; |
| 282 | |
Jack Carter | 71e6a74 | 2012-09-06 00:43:26 +0000 | [diff] [blame] | 283 | assert(MO.isExpr() && |
| 284 | "getBranchTargetOpValue expects only expressions or immediates"); |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 285 | |
| 286 | const MCExpr *Expr = MO.getExpr(); |
| 287 | Fixups.push_back(MCFixup::Create(0, Expr, |
| 288 | MCFixupKind(Mips::fixup_Mips_PC16))); |
| 289 | return 0; |
| 290 | } |
| 291 | |
Zoran Jovanovic | 8a80aa7 | 2013-11-04 14:53:22 +0000 | [diff] [blame] | 292 | /// getBranchTargetOpValue - Return binary encoding of the microMIPS branch |
| 293 | /// target operand. If the machine operand requires relocation, |
| 294 | /// record the relocation and return zero. |
| 295 | unsigned MipsMCCodeEmitter:: |
| 296 | getBranchTargetOpValueMM(const MCInst &MI, unsigned OpNo, |
| 297 | SmallVectorImpl<MCFixup> &Fixups) const { |
| 298 | |
| 299 | const MCOperand &MO = MI.getOperand(OpNo); |
| 300 | |
| 301 | // If the destination is an immediate, divide by 2. |
| 302 | if (MO.isImm()) return MO.getImm() >> 1; |
| 303 | |
| 304 | assert(MO.isExpr() && |
| 305 | "getBranchTargetOpValueMM expects only expressions or immediates"); |
| 306 | |
| 307 | const MCExpr *Expr = MO.getExpr(); |
| 308 | Fixups.push_back(MCFixup::Create(0, Expr, |
| 309 | MCFixupKind(Mips:: |
| 310 | fixup_MICROMIPS_PC16_S1))); |
| 311 | return 0; |
| 312 | } |
| 313 | |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 314 | /// getJumpTargetOpValue - Return binary encoding of the jump |
| 315 | /// target operand. If the machine operand requires relocation, |
| 316 | /// record the relocation and return zero. |
| 317 | unsigned MipsMCCodeEmitter:: |
| 318 | getJumpTargetOpValue(const MCInst &MI, unsigned OpNo, |
| 319 | SmallVectorImpl<MCFixup> &Fixups) const { |
| 320 | |
| 321 | const MCOperand &MO = MI.getOperand(OpNo); |
Jack Carter | 4f69a0f | 2013-03-22 00:29:10 +0000 | [diff] [blame] | 322 | // If the destination is an immediate, divide by 4. |
| 323 | if (MO.isImm()) return MO.getImm()>>2; |
| 324 | |
Jack Carter | 71e6a74 | 2012-09-06 00:43:26 +0000 | [diff] [blame] | 325 | assert(MO.isExpr() && |
| 326 | "getJumpTargetOpValue expects only expressions or an immediate"); |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 327 | |
| 328 | const MCExpr *Expr = MO.getExpr(); |
| 329 | Fixups.push_back(MCFixup::Create(0, Expr, |
| 330 | MCFixupKind(Mips::fixup_Mips_26))); |
| 331 | return 0; |
| 332 | } |
| 333 | |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 334 | unsigned MipsMCCodeEmitter:: |
Zoran Jovanovic | 507e084 | 2013-10-29 16:38:59 +0000 | [diff] [blame] | 335 | getJumpTargetOpValueMM(const MCInst &MI, unsigned OpNo, |
| 336 | SmallVectorImpl<MCFixup> &Fixups) const { |
| 337 | |
| 338 | const MCOperand &MO = MI.getOperand(OpNo); |
| 339 | // If the destination is an immediate, divide by 2. |
| 340 | if (MO.isImm()) return MO.getImm() >> 1; |
| 341 | |
| 342 | assert(MO.isExpr() && |
| 343 | "getJumpTargetOpValueMM expects only expressions or an immediate"); |
| 344 | |
| 345 | const MCExpr *Expr = MO.getExpr(); |
| 346 | Fixups.push_back(MCFixup::Create(0, Expr, |
| 347 | MCFixupKind(Mips::fixup_MICROMIPS_26_S1))); |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | unsigned MipsMCCodeEmitter:: |
Jack Carter | b5cf590 | 2013-04-17 00:18:04 +0000 | [diff] [blame] | 352 | getExprOpValue(const MCExpr *Expr,SmallVectorImpl<MCFixup> &Fixups) const { |
| 353 | int64_t Res; |
Akira Hatanaka | 049e9e4 | 2011-11-23 22:19:28 +0000 | [diff] [blame] | 354 | |
Jack Carter | b5cf590 | 2013-04-17 00:18:04 +0000 | [diff] [blame] | 355 | if (Expr->EvaluateAsAbsolute(Res)) |
| 356 | return Res; |
Akira Hatanaka | 049e9e4 | 2011-11-23 22:19:28 +0000 | [diff] [blame] | 357 | |
Akira Hatanaka | fe384a2 | 2012-03-27 02:33:05 +0000 | [diff] [blame] | 358 | MCExpr::ExprKind Kind = Expr->getKind(); |
Jack Carter | b5cf590 | 2013-04-17 00:18:04 +0000 | [diff] [blame] | 359 | if (Kind == MCExpr::Constant) { |
| 360 | return cast<MCConstantExpr>(Expr)->getValue(); |
| 361 | } |
Akira Hatanaka | e2eed96 | 2011-12-22 01:05:17 +0000 | [diff] [blame] | 362 | |
Akira Hatanaka | fe384a2 | 2012-03-27 02:33:05 +0000 | [diff] [blame] | 363 | if (Kind == MCExpr::Binary) { |
Jack Carter | b5cf590 | 2013-04-17 00:18:04 +0000 | [diff] [blame] | 364 | unsigned Res = getExprOpValue(cast<MCBinaryExpr>(Expr)->getLHS(), Fixups); |
| 365 | Res += getExprOpValue(cast<MCBinaryExpr>(Expr)->getRHS(), Fixups); |
| 366 | return Res; |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 367 | } |
Jack Carter | b5cf590 | 2013-04-17 00:18:04 +0000 | [diff] [blame] | 368 | if (Kind == MCExpr::SymbolRef) { |
Mark Seaborn | c3bd177 | 2013-12-31 13:05:15 +0000 | [diff] [blame^] | 369 | Mips::Fixups FixupKind = Mips::Fixups(0); |
Akira Hatanaka | fe384a2 | 2012-03-27 02:33:05 +0000 | [diff] [blame] | 370 | |
Mark Seaborn | c3bd177 | 2013-12-31 13:05:15 +0000 | [diff] [blame^] | 371 | switch(cast<MCSymbolRefExpr>(Expr)->getKind()) { |
| 372 | default: llvm_unreachable("Unknown fixup kind!"); |
| 373 | break; |
| 374 | case MCSymbolRefExpr::VK_Mips_GPOFF_HI : |
| 375 | FixupKind = Mips::fixup_Mips_GPOFF_HI; |
| 376 | break; |
| 377 | case MCSymbolRefExpr::VK_Mips_GPOFF_LO : |
| 378 | FixupKind = Mips::fixup_Mips_GPOFF_LO; |
| 379 | break; |
| 380 | case MCSymbolRefExpr::VK_Mips_GOT_PAGE : |
| 381 | FixupKind = IsMicroMips ? Mips::fixup_MICROMIPS_GOT_PAGE |
| 382 | : Mips::fixup_Mips_GOT_PAGE; |
| 383 | break; |
| 384 | case MCSymbolRefExpr::VK_Mips_GOT_OFST : |
| 385 | FixupKind = IsMicroMips ? Mips::fixup_MICROMIPS_GOT_OFST |
| 386 | : Mips::fixup_Mips_GOT_OFST; |
| 387 | break; |
| 388 | case MCSymbolRefExpr::VK_Mips_GOT_DISP : |
| 389 | FixupKind = IsMicroMips ? Mips::fixup_MICROMIPS_GOT_DISP |
| 390 | : Mips::fixup_Mips_GOT_DISP; |
| 391 | break; |
| 392 | case MCSymbolRefExpr::VK_Mips_GPREL: |
| 393 | FixupKind = Mips::fixup_Mips_GPREL16; |
| 394 | break; |
| 395 | case MCSymbolRefExpr::VK_Mips_GOT_CALL: |
| 396 | FixupKind = IsMicroMips ? Mips::fixup_MICROMIPS_CALL16 |
| 397 | : Mips::fixup_Mips_CALL16; |
| 398 | break; |
| 399 | case MCSymbolRefExpr::VK_Mips_GOT16: |
| 400 | FixupKind = IsMicroMips ? Mips::fixup_MICROMIPS_GOT16 |
| 401 | : Mips::fixup_Mips_GOT_Global; |
| 402 | break; |
| 403 | case MCSymbolRefExpr::VK_Mips_GOT: |
| 404 | FixupKind = IsMicroMips ? Mips::fixup_MICROMIPS_GOT16 |
| 405 | : Mips::fixup_Mips_GOT_Local; |
| 406 | break; |
| 407 | case MCSymbolRefExpr::VK_Mips_ABS_HI: |
| 408 | FixupKind = IsMicroMips ? Mips::fixup_MICROMIPS_HI16 |
| 409 | : Mips::fixup_Mips_HI16; |
| 410 | break; |
| 411 | case MCSymbolRefExpr::VK_Mips_ABS_LO: |
| 412 | FixupKind = IsMicroMips ? Mips::fixup_MICROMIPS_LO16 |
| 413 | : Mips::fixup_Mips_LO16; |
| 414 | break; |
| 415 | case MCSymbolRefExpr::VK_Mips_TLSGD: |
| 416 | FixupKind = IsMicroMips ? Mips::fixup_MICROMIPS_TLS_GD |
| 417 | : Mips::fixup_Mips_TLSGD; |
| 418 | break; |
| 419 | case MCSymbolRefExpr::VK_Mips_TLSLDM: |
| 420 | FixupKind = IsMicroMips ? Mips::fixup_MICROMIPS_TLS_LDM |
| 421 | : Mips::fixup_Mips_TLSLDM; |
| 422 | break; |
| 423 | case MCSymbolRefExpr::VK_Mips_DTPREL_HI: |
| 424 | FixupKind = IsMicroMips ? Mips::fixup_MICROMIPS_TLS_DTPREL_HI16 |
| 425 | : Mips::fixup_Mips_DTPREL_HI; |
| 426 | break; |
| 427 | case MCSymbolRefExpr::VK_Mips_DTPREL_LO: |
| 428 | FixupKind = IsMicroMips ? Mips::fixup_MICROMIPS_TLS_DTPREL_LO16 |
| 429 | : Mips::fixup_Mips_DTPREL_LO; |
| 430 | break; |
| 431 | case MCSymbolRefExpr::VK_Mips_GOTTPREL: |
| 432 | FixupKind = Mips::fixup_Mips_GOTTPREL; |
| 433 | break; |
| 434 | case MCSymbolRefExpr::VK_Mips_TPREL_HI: |
| 435 | FixupKind = IsMicroMips ? Mips::fixup_MICROMIPS_TLS_TPREL_HI16 |
| 436 | : Mips::fixup_Mips_TPREL_HI; |
| 437 | break; |
| 438 | case MCSymbolRefExpr::VK_Mips_TPREL_LO: |
| 439 | FixupKind = IsMicroMips ? Mips::fixup_MICROMIPS_TLS_TPREL_LO16 |
| 440 | : Mips::fixup_Mips_TPREL_LO; |
| 441 | break; |
| 442 | case MCSymbolRefExpr::VK_Mips_HIGHER: |
| 443 | FixupKind = Mips::fixup_Mips_HIGHER; |
| 444 | break; |
| 445 | case MCSymbolRefExpr::VK_Mips_HIGHEST: |
| 446 | FixupKind = Mips::fixup_Mips_HIGHEST; |
| 447 | break; |
| 448 | case MCSymbolRefExpr::VK_Mips_GOT_HI16: |
| 449 | FixupKind = Mips::fixup_Mips_GOT_HI16; |
| 450 | break; |
| 451 | case MCSymbolRefExpr::VK_Mips_GOT_LO16: |
| 452 | FixupKind = Mips::fixup_Mips_GOT_LO16; |
| 453 | break; |
| 454 | case MCSymbolRefExpr::VK_Mips_CALL_HI16: |
| 455 | FixupKind = Mips::fixup_Mips_CALL_HI16; |
| 456 | break; |
| 457 | case MCSymbolRefExpr::VK_Mips_CALL_LO16: |
| 458 | FixupKind = Mips::fixup_Mips_CALL_LO16; |
| 459 | break; |
| 460 | } // switch |
Akira Hatanaka | fe384a2 | 2012-03-27 02:33:05 +0000 | [diff] [blame] | 461 | |
Jack Carter | b5cf590 | 2013-04-17 00:18:04 +0000 | [diff] [blame] | 462 | Fixups.push_back(MCFixup::Create(0, Expr, MCFixupKind(FixupKind))); |
| 463 | return 0; |
| 464 | } |
Akira Hatanaka | fe384a2 | 2012-03-27 02:33:05 +0000 | [diff] [blame] | 465 | return 0; |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Jack Carter | b5cf590 | 2013-04-17 00:18:04 +0000 | [diff] [blame] | 468 | /// getMachineOpValue - Return binary encoding of operand. If the machine |
| 469 | /// operand requires relocation, record the relocation and return zero. |
| 470 | unsigned MipsMCCodeEmitter:: |
| 471 | getMachineOpValue(const MCInst &MI, const MCOperand &MO, |
| 472 | SmallVectorImpl<MCFixup> &Fixups) const { |
| 473 | if (MO.isReg()) { |
| 474 | unsigned Reg = MO.getReg(); |
Bill Wendling | bc07a89 | 2013-06-18 07:20:20 +0000 | [diff] [blame] | 475 | unsigned RegNo = Ctx.getRegisterInfo()->getEncodingValue(Reg); |
Jack Carter | b5cf590 | 2013-04-17 00:18:04 +0000 | [diff] [blame] | 476 | return RegNo; |
| 477 | } else if (MO.isImm()) { |
| 478 | return static_cast<unsigned>(MO.getImm()); |
| 479 | } else if (MO.isFPImm()) { |
| 480 | return static_cast<unsigned>(APFloat(MO.getFPImm()) |
| 481 | .bitcastToAPInt().getHiBits(32).getLimitedValue()); |
| 482 | } |
| 483 | // MO must be an Expr. |
| 484 | assert(MO.isExpr()); |
| 485 | return getExprOpValue(MO.getExpr(),Fixups); |
| 486 | } |
| 487 | |
Matheus Almeida | 6b59c44 | 2013-12-05 11:06:22 +0000 | [diff] [blame] | 488 | /// getMSAMemEncoding - Return binary encoding of memory operand for LD/ST |
| 489 | /// instructions. |
| 490 | unsigned |
| 491 | MipsMCCodeEmitter::getMSAMemEncoding(const MCInst &MI, unsigned OpNo, |
| 492 | SmallVectorImpl<MCFixup> &Fixups) const { |
| 493 | // Base register is encoded in bits 20-16, offset is encoded in bits 15-0. |
| 494 | assert(MI.getOperand(OpNo).isReg()); |
| 495 | unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups) << 16; |
| 496 | unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups); |
| 497 | |
| 498 | // The immediate field of an LD/ST instruction is scaled which means it must |
| 499 | // be divided (when encoding) by the size (in bytes) of the instructions' |
| 500 | // data format. |
| 501 | // .b - 1 byte |
| 502 | // .h - 2 bytes |
| 503 | // .w - 4 bytes |
| 504 | // .d - 8 bytes |
| 505 | switch(MI.getOpcode()) |
| 506 | { |
| 507 | default: |
| 508 | assert (0 && "Unexpected instruction"); |
| 509 | break; |
| 510 | case Mips::LD_B: |
| 511 | case Mips::ST_B: |
| 512 | // We don't need to scale the offset in this case |
| 513 | break; |
| 514 | case Mips::LD_H: |
| 515 | case Mips::ST_H: |
| 516 | OffBits >>= 1; |
| 517 | break; |
| 518 | case Mips::LD_W: |
| 519 | case Mips::ST_W: |
| 520 | OffBits >>= 2; |
| 521 | break; |
| 522 | case Mips::LD_D: |
| 523 | case Mips::ST_D: |
| 524 | OffBits >>= 3; |
| 525 | break; |
| 526 | } |
| 527 | |
| 528 | return (OffBits & 0xFFFF) | RegBits; |
| 529 | } |
| 530 | |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 531 | /// getMemEncoding - Return binary encoding of memory related operand. |
| 532 | /// If the offset operand requires relocation, record the relocation. |
| 533 | unsigned |
| 534 | MipsMCCodeEmitter::getMemEncoding(const MCInst &MI, unsigned OpNo, |
| 535 | SmallVectorImpl<MCFixup> &Fixups) const { |
| 536 | // Base register is encoded in bits 20-16, offset is encoded in bits 15-0. |
| 537 | assert(MI.getOperand(OpNo).isReg()); |
| 538 | unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups) << 16; |
| 539 | unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups); |
| 540 | |
| 541 | return (OffBits & 0xFFFF) | RegBits; |
| 542 | } |
| 543 | |
Jack Carter | 9770097 | 2013-08-13 20:19:16 +0000 | [diff] [blame] | 544 | unsigned MipsMCCodeEmitter:: |
| 545 | getMemEncodingMMImm12(const MCInst &MI, unsigned OpNo, |
| 546 | SmallVectorImpl<MCFixup> &Fixups) const { |
| 547 | // Base register is encoded in bits 20-16, offset is encoded in bits 11-0. |
| 548 | assert(MI.getOperand(OpNo).isReg()); |
| 549 | unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups) << 16; |
| 550 | unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups); |
| 551 | |
| 552 | return (OffBits & 0x0FFF) | RegBits; |
| 553 | } |
| 554 | |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 555 | unsigned |
| 556 | MipsMCCodeEmitter::getSizeExtEncoding(const MCInst &MI, unsigned OpNo, |
| 557 | SmallVectorImpl<MCFixup> &Fixups) const { |
Akira Hatanaka | 049e9e4 | 2011-11-23 22:19:28 +0000 | [diff] [blame] | 558 | assert(MI.getOperand(OpNo).isImm()); |
Bruno Cardoso Lopes | 56b70de | 2011-12-07 22:35:30 +0000 | [diff] [blame] | 559 | unsigned SizeEncoding = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups); |
| 560 | return SizeEncoding - 1; |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Akira Hatanaka | 049e9e4 | 2011-11-23 22:19:28 +0000 | [diff] [blame] | 563 | // FIXME: should be called getMSBEncoding |
| 564 | // |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 565 | unsigned |
| 566 | MipsMCCodeEmitter::getSizeInsEncoding(const MCInst &MI, unsigned OpNo, |
| 567 | SmallVectorImpl<MCFixup> &Fixups) const { |
Akira Hatanaka | 049e9e4 | 2011-11-23 22:19:28 +0000 | [diff] [blame] | 568 | assert(MI.getOperand(OpNo-1).isImm()); |
| 569 | assert(MI.getOperand(OpNo).isImm()); |
Bruno Cardoso Lopes | 56b70de | 2011-12-07 22:35:30 +0000 | [diff] [blame] | 570 | unsigned Position = getMachineOpValue(MI, MI.getOperand(OpNo-1), Fixups); |
| 571 | unsigned Size = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups); |
Akira Hatanaka | 049e9e4 | 2011-11-23 22:19:28 +0000 | [diff] [blame] | 572 | |
Bruno Cardoso Lopes | 56b70de | 2011-12-07 22:35:30 +0000 | [diff] [blame] | 573 | return Position + Size - 1; |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 574 | } |
| 575 | |
Matheus Almeida | 779c593 | 2013-11-18 12:32:49 +0000 | [diff] [blame] | 576 | unsigned |
| 577 | MipsMCCodeEmitter::getLSAImmEncoding(const MCInst &MI, unsigned OpNo, |
| 578 | SmallVectorImpl<MCFixup> &Fixups) const { |
| 579 | assert(MI.getOperand(OpNo).isImm()); |
| 580 | // The immediate is encoded as 'immediate - 1'. |
| 581 | return getMachineOpValue(MI, MI.getOperand(OpNo), Fixups) - 1; |
| 582 | } |
| 583 | |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 584 | #include "MipsGenMCCodeEmitter.inc" |
| 585 | |