Chris Lattner | 5ffe38e | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 1 | //===-- PPCMCCodeEmitter.cpp - Convert PPC code to machine code -----------===// |
| 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 PPCMCCodeEmitter class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #define DEBUG_TYPE "mccodeemitter" |
| 15 | #include "PPC.h" |
Chris Lattner | a04084e | 2010-11-15 04:51:55 +0000 | [diff] [blame] | 16 | #include "PPCRegisterInfo.h" |
Chris Lattner | a9d9ab9 | 2010-11-15 05:57:53 +0000 | [diff] [blame] | 17 | #include "PPCFixupKinds.h" |
Chris Lattner | 5ffe38e | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCCodeEmitter.h" |
| 19 | #include "llvm/MC/MCInst.h" |
| 20 | #include "llvm/ADT/Statistic.h" |
| 21 | #include "llvm/Support/raw_ostream.h" |
| 22 | #include "llvm/Support/ErrorHandling.h" |
| 23 | using namespace llvm; |
| 24 | |
| 25 | STATISTIC(MCNumEmitted, "Number of MC instructions emitted"); |
| 26 | |
| 27 | namespace { |
| 28 | class PPCMCCodeEmitter : public MCCodeEmitter { |
| 29 | PPCMCCodeEmitter(const PPCMCCodeEmitter &); // DO NOT IMPLEMENT |
| 30 | void operator=(const PPCMCCodeEmitter &); // DO NOT IMPLEMENT |
| 31 | const TargetMachine &TM; |
| 32 | MCContext &Ctx; |
| 33 | |
| 34 | public: |
| 35 | PPCMCCodeEmitter(TargetMachine &tm, MCContext &ctx) |
| 36 | : TM(tm), Ctx(ctx) { |
| 37 | } |
| 38 | |
| 39 | ~PPCMCCodeEmitter() {} |
| 40 | |
Chris Lattner | a9d9ab9 | 2010-11-15 05:57:53 +0000 | [diff] [blame] | 41 | unsigned getNumFixupKinds() const { return PPC::NumTargetFixupKinds; } |
Chris Lattner | 5ffe38e | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 42 | |
| 43 | const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const { |
| 44 | const static MCFixupKindInfo Infos[] = { |
Chris Lattner | 5ffe38e | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 45 | // name offset bits flags |
Chris Lattner | b719437 | 2010-11-15 06:12:22 +0000 | [diff] [blame] | 46 | { "fixup_ppc_br24", 6, 24, MCFixupKindInfo::FKF_IsPCRel }, |
Chris Lattner | 85cf7d7 | 2010-11-15 06:33:39 +0000 | [diff] [blame] | 47 | { "fixup_ppc_brcond14", 16, 14, MCFixupKindInfo::FKF_IsPCRel }, |
| 48 | { "fixup_ppc_lo16", 16, 16, 0 }, |
| 49 | { "fixup_ppc_ha16", 16, 16, 0 }, |
| 50 | { "fixup_ppc_lo14", 16, 14, 0 } |
Chris Lattner | 5ffe38e | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | if (Kind < FirstTargetFixupKind) |
| 54 | return MCCodeEmitter::getFixupKindInfo(Kind); |
| 55 | |
| 56 | assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() && |
| 57 | "Invalid kind!"); |
| 58 | return Infos[Kind - FirstTargetFixupKind]; |
| 59 | } |
Chris Lattner | 7192eb8 | 2010-11-15 05:19:25 +0000 | [diff] [blame] | 60 | |
Chris Lattner | 8d70411 | 2010-11-15 06:09:35 +0000 | [diff] [blame] | 61 | unsigned getDirectBrEncoding(const MCInst &MI, unsigned OpNo, |
| 62 | SmallVectorImpl<MCFixup> &Fixups) const; |
Chris Lattner | 8d70411 | 2010-11-15 06:09:35 +0000 | [diff] [blame] | 63 | unsigned getCondBrEncoding(const MCInst &MI, unsigned OpNo, |
| 64 | SmallVectorImpl<MCFixup> &Fixups) const; |
Chris Lattner | 85cf7d7 | 2010-11-15 06:33:39 +0000 | [diff] [blame] | 65 | unsigned getHA16Encoding(const MCInst &MI, unsigned OpNo, |
| 66 | SmallVectorImpl<MCFixup> &Fixups) const; |
| 67 | unsigned getLO16Encoding(const MCInst &MI, unsigned OpNo, |
| 68 | SmallVectorImpl<MCFixup> &Fixups) const; |
Chris Lattner | b7035d0 | 2010-11-15 08:22:03 +0000 | [diff] [blame] | 69 | unsigned getMemRIEncoding(const MCInst &MI, unsigned OpNo, |
| 70 | SmallVectorImpl<MCFixup> &Fixups) const; |
Chris Lattner | 17e2c18 | 2010-11-15 08:02:41 +0000 | [diff] [blame] | 71 | unsigned getMemRIXEncoding(const MCInst &MI, unsigned OpNo, |
| 72 | SmallVectorImpl<MCFixup> &Fixups) const; |
Chris Lattner | 7192eb8 | 2010-11-15 05:19:25 +0000 | [diff] [blame] | 73 | unsigned get_crbitm_encoding(const MCInst &MI, unsigned OpNo, |
| 74 | SmallVectorImpl<MCFixup> &Fixups) const; |
| 75 | |
Chris Lattner | 5ffe38e | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 76 | /// getMachineOpValue - Return binary encoding of operand. If the machine |
| 77 | /// operand requires relocation, record the relocation and return zero. |
| 78 | unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO, |
| 79 | SmallVectorImpl<MCFixup> &Fixups) const; |
Chris Lattner | 5ffe38e | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 80 | |
| 81 | // getBinaryCodeForInstr - TableGen'erated function for getting the |
| 82 | // binary encoding for an instruction. |
| 83 | unsigned getBinaryCodeForInstr(const MCInst &MI, |
| 84 | SmallVectorImpl<MCFixup> &Fixups) const; |
| 85 | void EncodeInstruction(const MCInst &MI, raw_ostream &OS, |
| 86 | SmallVectorImpl<MCFixup> &Fixups) const { |
| 87 | unsigned Bits = getBinaryCodeForInstr(MI, Fixups); |
| 88 | |
| 89 | // Output the constant in big endian byte order. |
| 90 | for (unsigned i = 0; i != 4; ++i) { |
| 91 | OS << (char)(Bits >> 24); |
| 92 | Bits <<= 8; |
| 93 | } |
| 94 | |
| 95 | ++MCNumEmitted; // Keep track of the # of mi's emitted. |
| 96 | } |
| 97 | |
| 98 | }; |
| 99 | |
| 100 | } // end anonymous namespace |
| 101 | |
| 102 | MCCodeEmitter *llvm::createPPCMCCodeEmitter(const Target &, TargetMachine &TM, |
| 103 | MCContext &Ctx) { |
| 104 | return new PPCMCCodeEmitter(TM, Ctx); |
| 105 | } |
| 106 | |
| 107 | unsigned PPCMCCodeEmitter:: |
Chris Lattner | 8d70411 | 2010-11-15 06:09:35 +0000 | [diff] [blame] | 108 | getDirectBrEncoding(const MCInst &MI, unsigned OpNo, |
| 109 | SmallVectorImpl<MCFixup> &Fixups) const { |
Chris Lattner | a9d9ab9 | 2010-11-15 05:57:53 +0000 | [diff] [blame] | 110 | const MCOperand &MO = MI.getOperand(OpNo); |
| 111 | if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups); |
| 112 | |
| 113 | // Add a fixup for the branch target. |
| 114 | Fixups.push_back(MCFixup::Create(0, MO.getExpr(), |
| 115 | (MCFixupKind)PPC::fixup_ppc_br24)); |
| 116 | return 0; |
| 117 | } |
| 118 | |
Chris Lattner | 8d70411 | 2010-11-15 06:09:35 +0000 | [diff] [blame] | 119 | unsigned PPCMCCodeEmitter::getCondBrEncoding(const MCInst &MI, unsigned OpNo, |
| 120 | SmallVectorImpl<MCFixup> &Fixups) const { |
| 121 | const MCOperand &MO = MI.getOperand(OpNo); |
| 122 | if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups); |
| 123 | |
Chris Lattner | b719437 | 2010-11-15 06:12:22 +0000 | [diff] [blame] | 124 | // Add a fixup for the branch target. |
| 125 | Fixups.push_back(MCFixup::Create(0, MO.getExpr(), |
| 126 | (MCFixupKind)PPC::fixup_ppc_brcond14)); |
Chris Lattner | 8d70411 | 2010-11-15 06:09:35 +0000 | [diff] [blame] | 127 | return 0; |
| 128 | } |
| 129 | |
Chris Lattner | 85cf7d7 | 2010-11-15 06:33:39 +0000 | [diff] [blame] | 130 | unsigned PPCMCCodeEmitter::getHA16Encoding(const MCInst &MI, unsigned OpNo, |
| 131 | SmallVectorImpl<MCFixup> &Fixups) const { |
| 132 | const MCOperand &MO = MI.getOperand(OpNo); |
| 133 | if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups); |
| 134 | |
| 135 | // Add a fixup for the branch target. |
| 136 | Fixups.push_back(MCFixup::Create(0, MO.getExpr(), |
| 137 | (MCFixupKind)PPC::fixup_ppc_ha16)); |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | unsigned PPCMCCodeEmitter::getLO16Encoding(const MCInst &MI, unsigned OpNo, |
| 142 | SmallVectorImpl<MCFixup> &Fixups) const { |
| 143 | const MCOperand &MO = MI.getOperand(OpNo); |
| 144 | if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups); |
| 145 | |
| 146 | // Add a fixup for the branch target. |
| 147 | Fixups.push_back(MCFixup::Create(0, MO.getExpr(), |
| 148 | (MCFixupKind)PPC::fixup_ppc_lo16)); |
| 149 | return 0; |
| 150 | } |
| 151 | |
Chris Lattner | b7035d0 | 2010-11-15 08:22:03 +0000 | [diff] [blame] | 152 | unsigned PPCMCCodeEmitter::getMemRIEncoding(const MCInst &MI, unsigned OpNo, |
| 153 | SmallVectorImpl<MCFixup> &Fixups) const { |
| 154 | // Encode (imm, reg) as a memri, which has the low 16-bits as the |
| 155 | // displacement and the next 5 bits as the register #. |
| 156 | assert(MI.getOperand(OpNo+1).isReg()); |
| 157 | unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups) << 16; |
| 158 | |
| 159 | const MCOperand &MO = MI.getOperand(OpNo); |
| 160 | if (MO.isImm()) |
| 161 | return (getMachineOpValue(MI, MO, Fixups) & 0xFFFF) | RegBits; |
| 162 | |
| 163 | // Add a fixup for the displacement field. |
| 164 | Fixups.push_back(MCFixup::Create(0, MO.getExpr(), |
| 165 | (MCFixupKind)PPC::fixup_ppc_lo16)); |
| 166 | return RegBits; |
| 167 | } |
| 168 | |
| 169 | |
Chris Lattner | 17e2c18 | 2010-11-15 08:02:41 +0000 | [diff] [blame] | 170 | unsigned PPCMCCodeEmitter::getMemRIXEncoding(const MCInst &MI, unsigned OpNo, |
Chris Lattner | 85cf7d7 | 2010-11-15 06:33:39 +0000 | [diff] [blame] | 171 | SmallVectorImpl<MCFixup> &Fixups) const { |
Chris Lattner | 17e2c18 | 2010-11-15 08:02:41 +0000 | [diff] [blame] | 172 | // Encode (imm, reg) as a memrix, which has the low 14-bits as the |
| 173 | // displacement and the next 5 bits as the register #. |
Chris Lattner | b7035d0 | 2010-11-15 08:22:03 +0000 | [diff] [blame] | 174 | assert(MI.getOperand(OpNo+1).isReg()); |
Chris Lattner | 17e2c18 | 2010-11-15 08:02:41 +0000 | [diff] [blame] | 175 | unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups) << 14; |
| 176 | |
Chris Lattner | 85cf7d7 | 2010-11-15 06:33:39 +0000 | [diff] [blame] | 177 | const MCOperand &MO = MI.getOperand(OpNo); |
Chris Lattner | 17e2c18 | 2010-11-15 08:02:41 +0000 | [diff] [blame] | 178 | if (MO.isImm()) |
| 179 | return (getMachineOpValue(MI, MO, Fixups) & 0x3FFF) | RegBits; |
Chris Lattner | 85cf7d7 | 2010-11-15 06:33:39 +0000 | [diff] [blame] | 180 | |
| 181 | // Add a fixup for the branch target. |
| 182 | Fixups.push_back(MCFixup::Create(0, MO.getExpr(), |
| 183 | (MCFixupKind)PPC::fixup_ppc_lo14)); |
Chris Lattner | 17e2c18 | 2010-11-15 08:02:41 +0000 | [diff] [blame] | 184 | return RegBits; |
Chris Lattner | 85cf7d7 | 2010-11-15 06:33:39 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Chris Lattner | 8d70411 | 2010-11-15 06:09:35 +0000 | [diff] [blame] | 187 | |
Chris Lattner | a9d9ab9 | 2010-11-15 05:57:53 +0000 | [diff] [blame] | 188 | unsigned PPCMCCodeEmitter:: |
Chris Lattner | 7192eb8 | 2010-11-15 05:19:25 +0000 | [diff] [blame] | 189 | get_crbitm_encoding(const MCInst &MI, unsigned OpNo, |
| 190 | SmallVectorImpl<MCFixup> &Fixups) const { |
| 191 | const MCOperand &MO = MI.getOperand(OpNo); |
| 192 | assert((MI.getOpcode() == PPC::MTCRF || MI.getOpcode() == PPC::MFOCRF) && |
| 193 | (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7)); |
| 194 | return 0x80 >> PPCRegisterInfo::getRegisterNumbering(MO.getReg()); |
| 195 | } |
| 196 | |
| 197 | |
| 198 | unsigned PPCMCCodeEmitter:: |
Chris Lattner | 5ffe38e | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 199 | getMachineOpValue(const MCInst &MI, const MCOperand &MO, |
| 200 | SmallVectorImpl<MCFixup> &Fixups) const { |
Chris Lattner | 7192eb8 | 2010-11-15 05:19:25 +0000 | [diff] [blame] | 201 | if (MO.isReg()) { |
Chris Lattner | 0382a4c | 2010-11-16 00:57:32 +0000 | [diff] [blame] | 202 | // MTCRF/MFOCRF should go through get_crbitm_encoding for the CR operand. |
| 203 | // The GPR operand should come through here though. |
Chris Lattner | b69cdfa | 2010-11-16 00:55:51 +0000 | [diff] [blame] | 204 | assert((MI.getOpcode() != PPC::MTCRF && MI.getOpcode() != PPC::MFOCRF) || |
| 205 | MO.getReg() < PPC::CR0 || MO.getReg() > PPC::CR7); |
Chris Lattner | a04084e | 2010-11-15 04:51:55 +0000 | [diff] [blame] | 206 | return PPCRegisterInfo::getRegisterNumbering(MO.getReg()); |
Chris Lattner | 7192eb8 | 2010-11-15 05:19:25 +0000 | [diff] [blame] | 207 | } |
Chris Lattner | a04084e | 2010-11-15 04:51:55 +0000 | [diff] [blame] | 208 | |
Chris Lattner | b7035d0 | 2010-11-15 08:22:03 +0000 | [diff] [blame] | 209 | assert(MO.isImm() && |
| 210 | "Relocation required in an instruction that we cannot encode!"); |
| 211 | return MO.getImm(); |
Chris Lattner | 5ffe38e | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | |
| 215 | #include "PPCGenMCCodeEmitter.inc" |