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