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