1 //===-- PPCMCCodeEmitter.cpp - Convert PPC code to machine code -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the PPCMCCodeEmitter class.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "mccodeemitter"
16 #include "PPCRegisterInfo.h"
17 #include "PPCFixupKinds.h"
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"
25 STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
28 class PPCMCCodeEmitter : public MCCodeEmitter {
29 PPCMCCodeEmitter(const PPCMCCodeEmitter &); // DO NOT IMPLEMENT
30 void operator=(const PPCMCCodeEmitter &); // DO NOT IMPLEMENT
31 const TargetMachine &TM;
35 PPCMCCodeEmitter(TargetMachine &tm, MCContext &ctx)
39 ~PPCMCCodeEmitter() {}
41 unsigned getNumFixupKinds() const { return PPC::NumTargetFixupKinds; }
43 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
44 const static MCFixupKindInfo Infos[] = {
45 // name offset bits flags
46 { "fixup_ppc_br24", 6, 24, MCFixupKindInfo::FKF_IsPCRel },
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 }
53 if (Kind < FirstTargetFixupKind)
54 return MCCodeEmitter::getFixupKindInfo(Kind);
56 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
58 return Infos[Kind - FirstTargetFixupKind];
61 unsigned getDirectBrEncoding(const MCInst &MI, unsigned OpNo,
62 SmallVectorImpl<MCFixup> &Fixups) const;
63 unsigned getCondBrEncoding(const MCInst &MI, unsigned OpNo,
64 SmallVectorImpl<MCFixup> &Fixups) const;
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;
69 unsigned getMemRIXEncoding(const MCInst &MI, unsigned OpNo,
70 SmallVectorImpl<MCFixup> &Fixups) const;
71 unsigned get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
72 SmallVectorImpl<MCFixup> &Fixups) const;
74 /// getMachineOpValue - Return binary encoding of operand. If the machine
75 /// operand requires relocation, record the relocation and return zero.
76 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
77 SmallVectorImpl<MCFixup> &Fixups) const;
79 // getBinaryCodeForInstr - TableGen'erated function for getting the
80 // binary encoding for an instruction.
81 unsigned getBinaryCodeForInstr(const MCInst &MI,
82 SmallVectorImpl<MCFixup> &Fixups) const;
83 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
84 SmallVectorImpl<MCFixup> &Fixups) const {
85 unsigned Bits = getBinaryCodeForInstr(MI, Fixups);
87 // Output the constant in big endian byte order.
88 for (unsigned i = 0; i != 4; ++i) {
89 OS << (char)(Bits >> 24);
93 ++MCNumEmitted; // Keep track of the # of mi's emitted.
98 } // end anonymous namespace
100 MCCodeEmitter *llvm::createPPCMCCodeEmitter(const Target &, TargetMachine &TM,
102 return new PPCMCCodeEmitter(TM, Ctx);
105 unsigned PPCMCCodeEmitter::
106 getDirectBrEncoding(const MCInst &MI, unsigned OpNo,
107 SmallVectorImpl<MCFixup> &Fixups) const {
108 const MCOperand &MO = MI.getOperand(OpNo);
109 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
111 // Add a fixup for the branch target.
112 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
113 (MCFixupKind)PPC::fixup_ppc_br24));
117 unsigned PPCMCCodeEmitter::getCondBrEncoding(const MCInst &MI, unsigned OpNo,
118 SmallVectorImpl<MCFixup> &Fixups) const {
119 const MCOperand &MO = MI.getOperand(OpNo);
120 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
122 // Add a fixup for the branch target.
123 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
124 (MCFixupKind)PPC::fixup_ppc_brcond14));
128 unsigned PPCMCCodeEmitter::getHA16Encoding(const MCInst &MI, unsigned OpNo,
129 SmallVectorImpl<MCFixup> &Fixups) const {
130 const MCOperand &MO = MI.getOperand(OpNo);
131 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
133 // Add a fixup for the branch target.
134 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
135 (MCFixupKind)PPC::fixup_ppc_ha16));
139 unsigned PPCMCCodeEmitter::getLO16Encoding(const MCInst &MI, unsigned OpNo,
140 SmallVectorImpl<MCFixup> &Fixups) const {
141 const MCOperand &MO = MI.getOperand(OpNo);
142 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
144 // Add a fixup for the branch target.
145 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
146 (MCFixupKind)PPC::fixup_ppc_lo16));
150 unsigned PPCMCCodeEmitter::getMemRIXEncoding(const MCInst &MI, unsigned OpNo,
151 SmallVectorImpl<MCFixup> &Fixups) const {
152 // Encode (imm, reg) as a memrix, which has the low 14-bits as the
153 // displacement and the next 5 bits as the register #.
154 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups) << 14;
156 const MCOperand &MO = MI.getOperand(OpNo);
158 return (getMachineOpValue(MI, MO, Fixups) & 0x3FFF) | RegBits;
160 // Add a fixup for the branch target.
161 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
162 (MCFixupKind)PPC::fixup_ppc_lo14));
167 unsigned PPCMCCodeEmitter::
168 get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
169 SmallVectorImpl<MCFixup> &Fixups) const {
170 const MCOperand &MO = MI.getOperand(OpNo);
171 assert((MI.getOpcode() == PPC::MTCRF || MI.getOpcode() == PPC::MFOCRF) &&
172 (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7));
173 return 0x80 >> PPCRegisterInfo::getRegisterNumbering(MO.getReg());
177 unsigned PPCMCCodeEmitter::
178 getMachineOpValue(const MCInst &MI, const MCOperand &MO,
179 SmallVectorImpl<MCFixup> &Fixups) const {
181 assert(MI.getOpcode() != PPC::MTCRF && MI.getOpcode() != PPC::MFOCRF);
182 return PPCRegisterInfo::getRegisterNumbering(MO.getReg());
193 #include "PPCGenMCCodeEmitter.inc"