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