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" |
Jack Carter | aa7aeaa | 2012-10-02 23:09:40 +0000 | [diff] [blame] | 16 | #include "MCTargetDesc/MipsDirectObjLower.h" |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 17 | #include "MCTargetDesc/MipsFixupKinds.h" |
| 18 | #include "MCTargetDesc/MipsMCTargetDesc.h" |
| 19 | #include "llvm/ADT/APFloat.h" |
| 20 | #include "llvm/ADT/Statistic.h" |
Akira Hatanaka | 750ecec | 2011-09-30 20:40:03 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCCodeEmitter.h" |
Akira Hatanaka | 5d6faed | 2012-12-10 20:04:40 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCContext.h" |
Akira Hatanaka | 750ecec | 2011-09-30 20:40:03 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCExpr.h" |
| 24 | #include "llvm/MC/MCInst.h" |
| 25 | #include "llvm/MC/MCInstrInfo.h" |
| 26 | #include "llvm/MC/MCRegisterInfo.h" |
| 27 | #include "llvm/MC/MCSubtargetInfo.h" |
Akira Hatanaka | 750ecec | 2011-09-30 20:40:03 +0000 | [diff] [blame] | 28 | #include "llvm/Support/raw_ostream.h" |
Akira Hatanaka | 750ecec | 2011-09-30 20:40:03 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace llvm; |
| 31 | |
| 32 | namespace { |
| 33 | class MipsMCCodeEmitter : public MCCodeEmitter { |
Craig Topper | 2ed23ce | 2012-09-15 17:08:51 +0000 | [diff] [blame] | 34 | MipsMCCodeEmitter(const MipsMCCodeEmitter &) LLVM_DELETED_FUNCTION; |
| 35 | void operator=(const MipsMCCodeEmitter &) LLVM_DELETED_FUNCTION; |
Akira Hatanaka | 750ecec | 2011-09-30 20:40:03 +0000 | [diff] [blame] | 36 | const MCInstrInfo &MCII; |
Akira Hatanaka | 5d6faed | 2012-12-10 20:04:40 +0000 | [diff] [blame] | 37 | MCContext &Ctx; |
Akira Hatanaka | 1ee768d | 2012-03-01 01:53:15 +0000 | [diff] [blame] | 38 | bool IsLittleEndian; |
Akira Hatanaka | 750ecec | 2011-09-30 20:40:03 +0000 | [diff] [blame] | 39 | |
| 40 | public: |
Jack Carter | ab3cb42 | 2013-02-19 22:04:37 +0000 | [diff] [blame] | 41 | MipsMCCodeEmitter(const MCInstrInfo &mcii, MCContext &Ctx_, |
| 42 | const MCSubtargetInfo &sti, bool IsLittle) : |
David Blaikie | 725fda1 | 2013-02-20 07:39:18 +0000 | [diff] [blame] | 43 | MCII(mcii), Ctx(Ctx_), IsLittleEndian(IsLittle) {} |
Akira Hatanaka | 750ecec | 2011-09-30 20:40:03 +0000 | [diff] [blame] | 44 | |
| 45 | ~MipsMCCodeEmitter() {} |
| 46 | |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 47 | void EmitByte(unsigned char C, raw_ostream &OS) const { |
| 48 | OS << (char)C; |
Akira Hatanaka | 750ecec | 2011-09-30 20:40:03 +0000 | [diff] [blame] | 49 | } |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 50 | |
| 51 | void EmitInstruction(uint64_t Val, unsigned Size, raw_ostream &OS) const { |
| 52 | // Output the instruction encoding in little endian byte order. |
Akira Hatanaka | 0137dfe | 2012-03-21 00:52:01 +0000 | [diff] [blame] | 53 | for (unsigned i = 0; i < Size; ++i) { |
| 54 | unsigned Shift = IsLittleEndian ? i * 8 : (Size - 1 - i) * 8; |
| 55 | EmitByte((Val >> Shift) & 0xff, OS); |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
| 59 | void EncodeInstruction(const MCInst &MI, raw_ostream &OS, |
| 60 | SmallVectorImpl<MCFixup> &Fixups) const; |
| 61 | |
| 62 | // getBinaryCodeForInstr - TableGen'erated function for getting the |
| 63 | // binary encoding for an instruction. |
Owen Anderson | d845d9d | 2012-01-24 18:37:29 +0000 | [diff] [blame] | 64 | uint64_t getBinaryCodeForInstr(const MCInst &MI, |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 65 | SmallVectorImpl<MCFixup> &Fixups) const; |
| 66 | |
| 67 | // getBranchJumpOpValue - Return binary encoding of the jump |
| 68 | // target operand. If the machine operand requires relocation, |
| 69 | // record the relocation and return zero. |
| 70 | unsigned getJumpTargetOpValue(const MCInst &MI, unsigned OpNo, |
| 71 | SmallVectorImpl<MCFixup> &Fixups) const; |
| 72 | |
| 73 | // getBranchTargetOpValue - Return binary encoding of the branch |
| 74 | // target operand. If the machine operand requires relocation, |
| 75 | // record the relocation and return zero. |
| 76 | unsigned getBranchTargetOpValue(const MCInst &MI, unsigned OpNo, |
| 77 | SmallVectorImpl<MCFixup> &Fixups) const; |
| 78 | |
| 79 | // getMachineOpValue - Return binary encoding of operand. If the machin |
| 80 | // operand requires relocation, record the relocation and return zero. |
| 81 | unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO, |
| 82 | SmallVectorImpl<MCFixup> &Fixups) const; |
| 83 | |
| 84 | unsigned getMemEncoding(const MCInst &MI, unsigned OpNo, |
| 85 | SmallVectorImpl<MCFixup> &Fixups) const; |
| 86 | unsigned getSizeExtEncoding(const MCInst &MI, unsigned OpNo, |
| 87 | SmallVectorImpl<MCFixup> &Fixups) const; |
| 88 | unsigned getSizeInsEncoding(const MCInst &MI, unsigned OpNo, |
| 89 | SmallVectorImpl<MCFixup> &Fixups) const; |
| 90 | |
Jack Carter | b5cf590 | 2013-04-17 00:18:04 +0000 | [diff] [blame^] | 91 | unsigned |
| 92 | getExprOpValue(const MCExpr *Expr,SmallVectorImpl<MCFixup> &Fixups) const; |
| 93 | |
Akira Hatanaka | 750ecec | 2011-09-30 20:40:03 +0000 | [diff] [blame] | 94 | }; // class MipsMCCodeEmitter |
| 95 | } // namespace |
| 96 | |
Akira Hatanaka | 1ee768d | 2012-03-01 01:53:15 +0000 | [diff] [blame] | 97 | MCCodeEmitter *llvm::createMipsMCCodeEmitterEB(const MCInstrInfo &MCII, |
Jim Grosbach | c3b0427 | 2012-05-15 17:35:52 +0000 | [diff] [blame] | 98 | const MCRegisterInfo &MRI, |
Akira Hatanaka | 1ee768d | 2012-03-01 01:53:15 +0000 | [diff] [blame] | 99 | const MCSubtargetInfo &STI, |
| 100 | MCContext &Ctx) |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 101 | { |
Jack Carter | ab3cb42 | 2013-02-19 22:04:37 +0000 | [diff] [blame] | 102 | return new MipsMCCodeEmitter(MCII, Ctx, STI, false); |
Akira Hatanaka | 1ee768d | 2012-03-01 01:53:15 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | MCCodeEmitter *llvm::createMipsMCCodeEmitterEL(const MCInstrInfo &MCII, |
Jim Grosbach | c3b0427 | 2012-05-15 17:35:52 +0000 | [diff] [blame] | 106 | const MCRegisterInfo &MRI, |
Akira Hatanaka | 1ee768d | 2012-03-01 01:53:15 +0000 | [diff] [blame] | 107 | const MCSubtargetInfo &STI, |
| 108 | MCContext &Ctx) |
| 109 | { |
Jack Carter | ab3cb42 | 2013-02-19 22:04:37 +0000 | [diff] [blame] | 110 | return new MipsMCCodeEmitter(MCII, Ctx, STI, true); |
Akira Hatanaka | 750ecec | 2011-09-30 20:40:03 +0000 | [diff] [blame] | 111 | } |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 112 | |
| 113 | /// EncodeInstruction - Emit the instruction. |
| 114 | /// Size the instruction (currently only 4 bytes |
| 115 | void MipsMCCodeEmitter:: |
| 116 | EncodeInstruction(const MCInst &MI, raw_ostream &OS, |
| 117 | SmallVectorImpl<MCFixup> &Fixups) const |
| 118 | { |
Jack Carter | aa7aeaa | 2012-10-02 23:09:40 +0000 | [diff] [blame] | 119 | |
| 120 | // Non-pseudo instructions that get changed for direct object |
| 121 | // only based on operand values. |
| 122 | // If this list of instructions get much longer we will move |
| 123 | // the check to a function call. Until then, this is more efficient. |
| 124 | MCInst TmpInst = MI; |
| 125 | switch (MI.getOpcode()) { |
| 126 | // If shift amount is >= 32 it the inst needs to be lowered further |
| 127 | case Mips::DSLL: |
| 128 | case Mips::DSRL: |
| 129 | case Mips::DSRA: |
| 130 | Mips::LowerLargeShift(TmpInst); |
| 131 | break; |
| 132 | // Double extract instruction is chosen by pos and size operands |
| 133 | case Mips::DEXT: |
| 134 | case Mips::DINS: |
| 135 | Mips::LowerDextDins(TmpInst); |
| 136 | } |
| 137 | |
| 138 | uint32_t Binary = getBinaryCodeForInstr(TmpInst, Fixups); |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 139 | |
| 140 | // Check for unimplemented opcodes. |
Jack Carter | aa7aeaa | 2012-10-02 23:09:40 +0000 | [diff] [blame] | 141 | // 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] | 142 | // so we have to special check for them. |
Jack Carter | aa7aeaa | 2012-10-02 23:09:40 +0000 | [diff] [blame] | 143 | unsigned Opcode = TmpInst.getOpcode(); |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 144 | if ((Opcode != Mips::NOP) && (Opcode != Mips::SLL) && !Binary) |
| 145 | llvm_unreachable("unimplemented opcode in EncodeInstruction()"); |
| 146 | |
Jack Carter | aa7aeaa | 2012-10-02 23:09:40 +0000 | [diff] [blame] | 147 | const MCInstrDesc &Desc = MCII.get(TmpInst.getOpcode()); |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 148 | |
Jack Carter | 5b5559d | 2012-10-03 21:58:54 +0000 | [diff] [blame] | 149 | // Get byte count of instruction |
| 150 | unsigned Size = Desc.getSize(); |
| 151 | if (!Size) |
| 152 | llvm_unreachable("Desc.getSize() returns 0"); |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 153 | |
| 154 | EmitInstruction(Binary, Size, OS); |
| 155 | } |
| 156 | |
| 157 | /// getBranchTargetOpValue - Return binary encoding of the branch |
| 158 | /// target operand. If the machine operand requires relocation, |
| 159 | /// record the relocation and return zero. |
| 160 | unsigned MipsMCCodeEmitter:: |
| 161 | getBranchTargetOpValue(const MCInst &MI, unsigned OpNo, |
| 162 | SmallVectorImpl<MCFixup> &Fixups) const { |
| 163 | |
| 164 | const MCOperand &MO = MI.getOperand(OpNo); |
Jack Carter | 71e6a74 | 2012-09-06 00:43:26 +0000 | [diff] [blame] | 165 | |
Jack Carter | 4f69a0f | 2013-03-22 00:29:10 +0000 | [diff] [blame] | 166 | // If the destination is an immediate, divide by 4. |
| 167 | if (MO.isImm()) return MO.getImm() >> 2; |
| 168 | |
Jack Carter | 71e6a74 | 2012-09-06 00:43:26 +0000 | [diff] [blame] | 169 | assert(MO.isExpr() && |
| 170 | "getBranchTargetOpValue expects only expressions or immediates"); |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 171 | |
| 172 | const MCExpr *Expr = MO.getExpr(); |
| 173 | Fixups.push_back(MCFixup::Create(0, Expr, |
| 174 | MCFixupKind(Mips::fixup_Mips_PC16))); |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | /// getJumpTargetOpValue - Return binary encoding of the jump |
| 179 | /// target operand. If the machine operand requires relocation, |
| 180 | /// record the relocation and return zero. |
| 181 | unsigned MipsMCCodeEmitter:: |
| 182 | getJumpTargetOpValue(const MCInst &MI, unsigned OpNo, |
| 183 | SmallVectorImpl<MCFixup> &Fixups) const { |
| 184 | |
| 185 | const MCOperand &MO = MI.getOperand(OpNo); |
Jack Carter | 4f69a0f | 2013-03-22 00:29:10 +0000 | [diff] [blame] | 186 | // If the destination is an immediate, divide by 4. |
| 187 | if (MO.isImm()) return MO.getImm()>>2; |
| 188 | |
Jack Carter | 71e6a74 | 2012-09-06 00:43:26 +0000 | [diff] [blame] | 189 | assert(MO.isExpr() && |
| 190 | "getJumpTargetOpValue expects only expressions or an immediate"); |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 191 | |
| 192 | const MCExpr *Expr = MO.getExpr(); |
| 193 | Fixups.push_back(MCFixup::Create(0, Expr, |
| 194 | MCFixupKind(Mips::fixup_Mips_26))); |
| 195 | return 0; |
| 196 | } |
| 197 | |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 198 | unsigned MipsMCCodeEmitter:: |
Jack Carter | b5cf590 | 2013-04-17 00:18:04 +0000 | [diff] [blame^] | 199 | getExprOpValue(const MCExpr *Expr,SmallVectorImpl<MCFixup> &Fixups) const { |
| 200 | int64_t Res; |
Akira Hatanaka | 049e9e4 | 2011-11-23 22:19:28 +0000 | [diff] [blame] | 201 | |
Jack Carter | b5cf590 | 2013-04-17 00:18:04 +0000 | [diff] [blame^] | 202 | if (Expr->EvaluateAsAbsolute(Res)) |
| 203 | return Res; |
Akira Hatanaka | 049e9e4 | 2011-11-23 22:19:28 +0000 | [diff] [blame] | 204 | |
Akira Hatanaka | fe384a2 | 2012-03-27 02:33:05 +0000 | [diff] [blame] | 205 | MCExpr::ExprKind Kind = Expr->getKind(); |
Jack Carter | b5cf590 | 2013-04-17 00:18:04 +0000 | [diff] [blame^] | 206 | if (Kind == MCExpr::Constant) { |
| 207 | return cast<MCConstantExpr>(Expr)->getValue(); |
| 208 | } |
Akira Hatanaka | e2eed96 | 2011-12-22 01:05:17 +0000 | [diff] [blame] | 209 | |
Akira Hatanaka | fe384a2 | 2012-03-27 02:33:05 +0000 | [diff] [blame] | 210 | if (Kind == MCExpr::Binary) { |
Jack Carter | b5cf590 | 2013-04-17 00:18:04 +0000 | [diff] [blame^] | 211 | unsigned Res = getExprOpValue(cast<MCBinaryExpr>(Expr)->getLHS(), Fixups); |
| 212 | Res += getExprOpValue(cast<MCBinaryExpr>(Expr)->getRHS(), Fixups); |
| 213 | return Res; |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 214 | } |
Jack Carter | b5cf590 | 2013-04-17 00:18:04 +0000 | [diff] [blame^] | 215 | if (Kind == MCExpr::SymbolRef) { |
Bill Wendling | f9774c3 | 2012-04-22 07:23:04 +0000 | [diff] [blame] | 216 | Mips::Fixups FixupKind = Mips::Fixups(0); |
Akira Hatanaka | fe384a2 | 2012-03-27 02:33:05 +0000 | [diff] [blame] | 217 | |
| 218 | switch(cast<MCSymbolRefExpr>(Expr)->getKind()) { |
Jack Carter | b9f9de9 | 2012-06-27 22:48:25 +0000 | [diff] [blame] | 219 | default: llvm_unreachable("Unknown fixup kind!"); |
| 220 | break; |
Jack Carter | b9f9de9 | 2012-06-27 22:48:25 +0000 | [diff] [blame] | 221 | case MCSymbolRefExpr::VK_Mips_GPOFF_HI : |
| 222 | FixupKind = Mips::fixup_Mips_GPOFF_HI; |
| 223 | break; |
| 224 | case MCSymbolRefExpr::VK_Mips_GPOFF_LO : |
| 225 | FixupKind = Mips::fixup_Mips_GPOFF_LO; |
| 226 | break; |
| 227 | case MCSymbolRefExpr::VK_Mips_GOT_PAGE : |
| 228 | FixupKind = Mips::fixup_Mips_GOT_PAGE; |
| 229 | break; |
| 230 | case MCSymbolRefExpr::VK_Mips_GOT_OFST : |
| 231 | FixupKind = Mips::fixup_Mips_GOT_OFST; |
| 232 | break; |
Jack Carter | 5ddcfda | 2012-07-13 19:15:47 +0000 | [diff] [blame] | 233 | case MCSymbolRefExpr::VK_Mips_GOT_DISP : |
| 234 | FixupKind = Mips::fixup_Mips_GOT_DISP; |
| 235 | break; |
Akira Hatanaka | fe384a2 | 2012-03-27 02:33:05 +0000 | [diff] [blame] | 236 | case MCSymbolRefExpr::VK_Mips_GPREL: |
| 237 | FixupKind = Mips::fixup_Mips_GPREL16; |
| 238 | break; |
| 239 | case MCSymbolRefExpr::VK_Mips_GOT_CALL: |
| 240 | FixupKind = Mips::fixup_Mips_CALL16; |
| 241 | break; |
| 242 | case MCSymbolRefExpr::VK_Mips_GOT16: |
| 243 | FixupKind = Mips::fixup_Mips_GOT_Global; |
| 244 | break; |
| 245 | case MCSymbolRefExpr::VK_Mips_GOT: |
| 246 | FixupKind = Mips::fixup_Mips_GOT_Local; |
| 247 | break; |
| 248 | case MCSymbolRefExpr::VK_Mips_ABS_HI: |
| 249 | FixupKind = Mips::fixup_Mips_HI16; |
| 250 | break; |
| 251 | case MCSymbolRefExpr::VK_Mips_ABS_LO: |
| 252 | FixupKind = Mips::fixup_Mips_LO16; |
| 253 | break; |
| 254 | case MCSymbolRefExpr::VK_Mips_TLSGD: |
| 255 | FixupKind = Mips::fixup_Mips_TLSGD; |
| 256 | break; |
| 257 | case MCSymbolRefExpr::VK_Mips_TLSLDM: |
| 258 | FixupKind = Mips::fixup_Mips_TLSLDM; |
| 259 | break; |
| 260 | case MCSymbolRefExpr::VK_Mips_DTPREL_HI: |
| 261 | FixupKind = Mips::fixup_Mips_DTPREL_HI; |
| 262 | break; |
| 263 | case MCSymbolRefExpr::VK_Mips_DTPREL_LO: |
| 264 | FixupKind = Mips::fixup_Mips_DTPREL_LO; |
| 265 | break; |
| 266 | case MCSymbolRefExpr::VK_Mips_GOTTPREL: |
| 267 | FixupKind = Mips::fixup_Mips_GOTTPREL; |
| 268 | break; |
| 269 | case MCSymbolRefExpr::VK_Mips_TPREL_HI: |
| 270 | FixupKind = Mips::fixup_Mips_TPREL_HI; |
| 271 | break; |
| 272 | case MCSymbolRefExpr::VK_Mips_TPREL_LO: |
| 273 | FixupKind = Mips::fixup_Mips_TPREL_LO; |
| 274 | break; |
Jack Carter | 84491ab | 2012-08-06 21:26:03 +0000 | [diff] [blame] | 275 | case MCSymbolRefExpr::VK_Mips_HIGHER: |
| 276 | FixupKind = Mips::fixup_Mips_HIGHER; |
| 277 | break; |
| 278 | case MCSymbolRefExpr::VK_Mips_HIGHEST: |
| 279 | FixupKind = Mips::fixup_Mips_HIGHEST; |
| 280 | break; |
Jack Carter | b05cb67 | 2012-11-21 23:38:59 +0000 | [diff] [blame] | 281 | case MCSymbolRefExpr::VK_Mips_GOT_HI16: |
| 282 | FixupKind = Mips::fixup_Mips_GOT_HI16; |
| 283 | break; |
| 284 | case MCSymbolRefExpr::VK_Mips_GOT_LO16: |
| 285 | FixupKind = Mips::fixup_Mips_GOT_LO16; |
| 286 | break; |
| 287 | case MCSymbolRefExpr::VK_Mips_CALL_HI16: |
| 288 | FixupKind = Mips::fixup_Mips_CALL_HI16; |
| 289 | break; |
| 290 | case MCSymbolRefExpr::VK_Mips_CALL_LO16: |
| 291 | FixupKind = Mips::fixup_Mips_CALL_LO16; |
| 292 | break; |
Akira Hatanaka | fe384a2 | 2012-03-27 02:33:05 +0000 | [diff] [blame] | 293 | } // switch |
| 294 | |
Jack Carter | b5cf590 | 2013-04-17 00:18:04 +0000 | [diff] [blame^] | 295 | Fixups.push_back(MCFixup::Create(0, Expr, MCFixupKind(FixupKind))); |
| 296 | return 0; |
| 297 | } |
Akira Hatanaka | fe384a2 | 2012-03-27 02:33:05 +0000 | [diff] [blame] | 298 | return 0; |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Jack Carter | b5cf590 | 2013-04-17 00:18:04 +0000 | [diff] [blame^] | 301 | /// getMachineOpValue - Return binary encoding of operand. If the machine |
| 302 | /// operand requires relocation, record the relocation and return zero. |
| 303 | unsigned MipsMCCodeEmitter:: |
| 304 | getMachineOpValue(const MCInst &MI, const MCOperand &MO, |
| 305 | SmallVectorImpl<MCFixup> &Fixups) const { |
| 306 | if (MO.isReg()) { |
| 307 | unsigned Reg = MO.getReg(); |
| 308 | unsigned RegNo = Ctx.getRegisterInfo().getEncodingValue(Reg); |
| 309 | return RegNo; |
| 310 | } else if (MO.isImm()) { |
| 311 | return static_cast<unsigned>(MO.getImm()); |
| 312 | } else if (MO.isFPImm()) { |
| 313 | return static_cast<unsigned>(APFloat(MO.getFPImm()) |
| 314 | .bitcastToAPInt().getHiBits(32).getLimitedValue()); |
| 315 | } |
| 316 | // MO must be an Expr. |
| 317 | assert(MO.isExpr()); |
| 318 | return getExprOpValue(MO.getExpr(),Fixups); |
| 319 | } |
| 320 | |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 321 | /// getMemEncoding - Return binary encoding of memory related operand. |
| 322 | /// If the offset operand requires relocation, record the relocation. |
| 323 | unsigned |
| 324 | MipsMCCodeEmitter::getMemEncoding(const MCInst &MI, unsigned OpNo, |
| 325 | SmallVectorImpl<MCFixup> &Fixups) const { |
| 326 | // Base register is encoded in bits 20-16, offset is encoded in bits 15-0. |
| 327 | assert(MI.getOperand(OpNo).isReg()); |
| 328 | unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo),Fixups) << 16; |
| 329 | unsigned OffBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups); |
| 330 | |
| 331 | return (OffBits & 0xFFFF) | RegBits; |
| 332 | } |
| 333 | |
| 334 | unsigned |
| 335 | MipsMCCodeEmitter::getSizeExtEncoding(const MCInst &MI, unsigned OpNo, |
| 336 | SmallVectorImpl<MCFixup> &Fixups) const { |
Akira Hatanaka | 049e9e4 | 2011-11-23 22:19:28 +0000 | [diff] [blame] | 337 | assert(MI.getOperand(OpNo).isImm()); |
Bruno Cardoso Lopes | 56b70de | 2011-12-07 22:35:30 +0000 | [diff] [blame] | 338 | unsigned SizeEncoding = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups); |
| 339 | return SizeEncoding - 1; |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Akira Hatanaka | 049e9e4 | 2011-11-23 22:19:28 +0000 | [diff] [blame] | 342 | // FIXME: should be called getMSBEncoding |
| 343 | // |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 344 | unsigned |
| 345 | MipsMCCodeEmitter::getSizeInsEncoding(const MCInst &MI, unsigned OpNo, |
| 346 | SmallVectorImpl<MCFixup> &Fixups) const { |
Akira Hatanaka | 049e9e4 | 2011-11-23 22:19:28 +0000 | [diff] [blame] | 347 | assert(MI.getOperand(OpNo-1).isImm()); |
| 348 | assert(MI.getOperand(OpNo).isImm()); |
Bruno Cardoso Lopes | 56b70de | 2011-12-07 22:35:30 +0000 | [diff] [blame] | 349 | unsigned Position = getMachineOpValue(MI, MI.getOperand(OpNo-1), Fixups); |
| 350 | unsigned Size = getMachineOpValue(MI, MI.getOperand(OpNo), Fixups); |
Akira Hatanaka | 049e9e4 | 2011-11-23 22:19:28 +0000 | [diff] [blame] | 351 | |
Bruno Cardoso Lopes | 56b70de | 2011-12-07 22:35:30 +0000 | [diff] [blame] | 352 | return Position + Size - 1; |
Bruno Cardoso Lopes | c85e3ff | 2011-11-11 22:58:42 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | #include "MipsGenMCCodeEmitter.inc" |
| 356 | |