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