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