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 | a9d9ab9 | 2010-11-15 05:57:53 +0000 | [diff] [blame] | 46 | { "fixup_ppc_br24", 6, 24, MCFixupKindInfo::FKF_IsPCRel } |
Chris Lattner | a2d6025 | 2010-11-15 04:47:16 +0000 | [diff] [blame] | 47 | #if 0 |
Chris Lattner | 5ffe38e | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 48 | { "fixup_arm_vfp_pcrel_12", 3, 8, MCFixupKindInfo::FKF_IsPCRel }, |
| 49 | { "fixup_arm_branch", 1, 24, MCFixupKindInfo::FKF_IsPCRel }, |
| 50 | #endif |
| 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; |
| 63 | |
| 64 | unsigned getCondBrEncoding(const MCInst &MI, unsigned OpNo, |
| 65 | SmallVectorImpl<MCFixup> &Fixups) const; |
| 66 | |
Chris Lattner | 7192eb8 | 2010-11-15 05:19:25 +0000 | [diff] [blame] | 67 | unsigned get_crbitm_encoding(const MCInst &MI, unsigned OpNo, |
| 68 | SmallVectorImpl<MCFixup> &Fixups) const; |
| 69 | |
Chris Lattner | 5ffe38e | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 70 | /// getMachineOpValue - Return binary encoding of operand. If the machine |
| 71 | /// operand requires relocation, record the relocation and return zero. |
| 72 | unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO, |
| 73 | SmallVectorImpl<MCFixup> &Fixups) const; |
Chris Lattner | 5ffe38e | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 74 | |
| 75 | // getBinaryCodeForInstr - TableGen'erated function for getting the |
| 76 | // binary encoding for an instruction. |
| 77 | unsigned getBinaryCodeForInstr(const MCInst &MI, |
| 78 | SmallVectorImpl<MCFixup> &Fixups) const; |
| 79 | void EncodeInstruction(const MCInst &MI, raw_ostream &OS, |
| 80 | SmallVectorImpl<MCFixup> &Fixups) const { |
| 81 | unsigned Bits = getBinaryCodeForInstr(MI, Fixups); |
| 82 | |
| 83 | // Output the constant in big endian byte order. |
| 84 | for (unsigned i = 0; i != 4; ++i) { |
| 85 | OS << (char)(Bits >> 24); |
| 86 | Bits <<= 8; |
| 87 | } |
| 88 | |
| 89 | ++MCNumEmitted; // Keep track of the # of mi's emitted. |
| 90 | } |
| 91 | |
| 92 | }; |
| 93 | |
| 94 | } // end anonymous namespace |
| 95 | |
| 96 | MCCodeEmitter *llvm::createPPCMCCodeEmitter(const Target &, TargetMachine &TM, |
| 97 | MCContext &Ctx) { |
| 98 | return new PPCMCCodeEmitter(TM, Ctx); |
| 99 | } |
| 100 | |
| 101 | unsigned PPCMCCodeEmitter:: |
Chris Lattner | 8d70411 | 2010-11-15 06:09:35 +0000 | [diff] [blame^] | 102 | getDirectBrEncoding(const MCInst &MI, unsigned OpNo, |
| 103 | SmallVectorImpl<MCFixup> &Fixups) const { |
Chris Lattner | a9d9ab9 | 2010-11-15 05:57:53 +0000 | [diff] [blame] | 104 | const MCOperand &MO = MI.getOperand(OpNo); |
| 105 | if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups); |
| 106 | |
| 107 | // Add a fixup for the branch target. |
| 108 | Fixups.push_back(MCFixup::Create(0, MO.getExpr(), |
| 109 | (MCFixupKind)PPC::fixup_ppc_br24)); |
| 110 | return 0; |
| 111 | } |
| 112 | |
Chris Lattner | 8d70411 | 2010-11-15 06:09:35 +0000 | [diff] [blame^] | 113 | unsigned PPCMCCodeEmitter::getCondBrEncoding(const MCInst &MI, unsigned OpNo, |
| 114 | SmallVectorImpl<MCFixup> &Fixups) const { |
| 115 | const MCOperand &MO = MI.getOperand(OpNo); |
| 116 | if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups); |
| 117 | |
| 118 | |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | |
Chris Lattner | a9d9ab9 | 2010-11-15 05:57:53 +0000 | [diff] [blame] | 124 | unsigned PPCMCCodeEmitter:: |
Chris Lattner | 7192eb8 | 2010-11-15 05:19:25 +0000 | [diff] [blame] | 125 | get_crbitm_encoding(const MCInst &MI, unsigned OpNo, |
| 126 | SmallVectorImpl<MCFixup> &Fixups) const { |
| 127 | const MCOperand &MO = MI.getOperand(OpNo); |
| 128 | assert((MI.getOpcode() == PPC::MTCRF || MI.getOpcode() == PPC::MFOCRF) && |
| 129 | (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7)); |
| 130 | return 0x80 >> PPCRegisterInfo::getRegisterNumbering(MO.getReg()); |
| 131 | } |
| 132 | |
| 133 | |
| 134 | unsigned PPCMCCodeEmitter:: |
Chris Lattner | 5ffe38e | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 135 | getMachineOpValue(const MCInst &MI, const MCOperand &MO, |
| 136 | SmallVectorImpl<MCFixup> &Fixups) const { |
Chris Lattner | 7192eb8 | 2010-11-15 05:19:25 +0000 | [diff] [blame] | 137 | if (MO.isReg()) { |
| 138 | assert(MI.getOpcode() != PPC::MTCRF && MI.getOpcode() != PPC::MFOCRF); |
Chris Lattner | a04084e | 2010-11-15 04:51:55 +0000 | [diff] [blame] | 139 | return PPCRegisterInfo::getRegisterNumbering(MO.getReg()); |
Chris Lattner | 7192eb8 | 2010-11-15 05:19:25 +0000 | [diff] [blame] | 140 | } |
Chris Lattner | a04084e | 2010-11-15 04:51:55 +0000 | [diff] [blame] | 141 | |
| 142 | if (MO.isImm()) |
| 143 | return MO.getImm(); |
| 144 | |
Chris Lattner | 5ffe38e | 2010-11-15 04:16:32 +0000 | [diff] [blame] | 145 | // FIXME. |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | |
| 150 | #include "PPCGenMCCodeEmitter.inc" |