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"
15 #include "MCTargetDesc/PPCMCTargetDesc.h"
16 #include "MCTargetDesc/PPCBaseInfo.h"
17 #include "MCTargetDesc/PPCFixupKinds.h"
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/MC/MCCodeEmitter.h"
20 #include "llvm/MC/MCExpr.h"
21 #include "llvm/MC/MCInst.h"
22 #include "llvm/MC/MCInstrInfo.h"
23 #include "llvm/MC/MCSubtargetInfo.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/raw_ostream.h"
28 STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
31 class PPCMCCodeEmitter : public MCCodeEmitter {
32 PPCMCCodeEmitter(const PPCMCCodeEmitter &) LLVM_DELETED_FUNCTION;
33 void operator=(const PPCMCCodeEmitter &) LLVM_DELETED_FUNCTION;
35 const MCSubtargetInfo &STI;
39 PPCMCCodeEmitter(const MCInstrInfo &mcii, const MCSubtargetInfo &sti,
41 : STI(sti), TT(STI.getTargetTriple()) {
44 ~PPCMCCodeEmitter() {}
46 bool is64BitMode() const {
47 return (STI.getFeatureBits() & PPC::Feature64Bit) != 0;
50 bool isSVR4ABI() const {
51 return TT.isMacOSX() == 0;
54 unsigned getDirectBrEncoding(const MCInst &MI, unsigned OpNo,
55 SmallVectorImpl<MCFixup> &Fixups) const;
56 unsigned getCondBrEncoding(const MCInst &MI, unsigned OpNo,
57 SmallVectorImpl<MCFixup> &Fixups) const;
58 unsigned getHA16Encoding(const MCInst &MI, unsigned OpNo,
59 SmallVectorImpl<MCFixup> &Fixups) const;
60 unsigned getLO16Encoding(const MCInst &MI, unsigned OpNo,
61 SmallVectorImpl<MCFixup> &Fixups) const;
62 unsigned getMemRIEncoding(const MCInst &MI, unsigned OpNo,
63 SmallVectorImpl<MCFixup> &Fixups) const;
64 unsigned getMemRIXEncoding(const MCInst &MI, unsigned OpNo,
65 SmallVectorImpl<MCFixup> &Fixups) const;
66 unsigned getTLSOffsetEncoding(const MCInst &MI, unsigned OpNo,
67 SmallVectorImpl<MCFixup> &Fixups) const;
68 unsigned getTLSRegEncoding(const MCInst &MI, unsigned OpNo,
69 SmallVectorImpl<MCFixup> &Fixups) const;
70 unsigned get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
71 SmallVectorImpl<MCFixup> &Fixups) const;
73 /// getMachineOpValue - Return binary encoding of operand. If the machine
74 /// operand requires relocation, record the relocation and return zero.
75 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
76 SmallVectorImpl<MCFixup> &Fixups) const;
78 // getBinaryCodeForInstr - TableGen'erated function for getting the
79 // binary encoding for an instruction.
80 uint64_t getBinaryCodeForInstr(const MCInst &MI,
81 SmallVectorImpl<MCFixup> &Fixups) const;
82 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
83 SmallVectorImpl<MCFixup> &Fixups) const {
84 uint64_t Bits = getBinaryCodeForInstr(MI, Fixups);
86 // BL8_NOP_ELF, BLA8_NOP_ELF, etc., all have a size of 8 because of the
88 unsigned Size = 4; // FIXME: Have Desc.getSize() return the correct value!
89 unsigned Opcode = MI.getOpcode();
90 if (Opcode == PPC::BL8_NOP_ELF || Opcode == PPC::BLA8_NOP_ELF ||
91 Opcode == PPC::BL8_NOP_ELF_TLSGD || Opcode == PPC::BL8_NOP_ELF_TLSLD)
94 // Output the constant in big endian byte order.
95 int ShiftValue = (Size * 8) - 8;
96 for (unsigned i = 0; i != Size; ++i) {
97 OS << (char)(Bits >> ShiftValue);
101 ++MCNumEmitted; // Keep track of the # of mi's emitted.
106 } // end anonymous namespace
108 MCCodeEmitter *llvm::createPPCMCCodeEmitter(const MCInstrInfo &MCII,
109 const MCRegisterInfo &MRI,
110 const MCSubtargetInfo &STI,
112 return new PPCMCCodeEmitter(MCII, STI, Ctx);
115 unsigned PPCMCCodeEmitter::
116 getDirectBrEncoding(const MCInst &MI, unsigned OpNo,
117 SmallVectorImpl<MCFixup> &Fixups) const {
118 const MCOperand &MO = MI.getOperand(OpNo);
119 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
121 // Add a fixup for the branch target.
122 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
123 (MCFixupKind)PPC::fixup_ppc_br24));
125 // For special TLS calls, add another fixup for the symbol. Apparently
126 // BL8_NOP_ELF, BL8_NOP_ELF_TLSGD, and BL8_NOP_ELF_TLSLD are sufficiently
127 // similar that TblGen will not generate a separate case for the latter
128 // two, so this is the only way to get the extra fixup generated.
129 unsigned Opcode = MI.getOpcode();
130 if (Opcode == PPC::BL8_NOP_ELF_TLSGD || Opcode == PPC::BL8_NOP_ELF_TLSLD) {
131 const MCOperand &MO2 = MI.getOperand(OpNo+1);
132 Fixups.push_back(MCFixup::Create(0, MO2.getExpr(),
133 (MCFixupKind)PPC::fixup_ppc_nofixup));
138 unsigned PPCMCCodeEmitter::getCondBrEncoding(const MCInst &MI, unsigned OpNo,
139 SmallVectorImpl<MCFixup> &Fixups) const {
140 const MCOperand &MO = MI.getOperand(OpNo);
141 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
143 // Add a fixup for the branch target.
144 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
145 (MCFixupKind)PPC::fixup_ppc_brcond14));
149 unsigned PPCMCCodeEmitter::getHA16Encoding(const MCInst &MI, unsigned OpNo,
150 SmallVectorImpl<MCFixup> &Fixups) const {
151 const MCOperand &MO = MI.getOperand(OpNo);
152 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
154 // Add a fixup for the branch target.
155 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
156 (MCFixupKind)PPC::fixup_ppc_ha16));
160 unsigned PPCMCCodeEmitter::getLO16Encoding(const MCInst &MI, unsigned OpNo,
161 SmallVectorImpl<MCFixup> &Fixups) const {
162 const MCOperand &MO = MI.getOperand(OpNo);
163 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
165 // Add a fixup for the branch target.
166 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
167 (MCFixupKind)PPC::fixup_ppc_lo16));
171 unsigned PPCMCCodeEmitter::getMemRIEncoding(const MCInst &MI, unsigned OpNo,
172 SmallVectorImpl<MCFixup> &Fixups) const {
173 // Encode (imm, reg) as a memri, which has the low 16-bits as the
174 // displacement and the next 5 bits as the register #.
175 assert(MI.getOperand(OpNo+1).isReg());
176 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups) << 16;
178 const MCOperand &MO = MI.getOperand(OpNo);
180 return (getMachineOpValue(MI, MO, Fixups) & 0xFFFF) | RegBits;
182 // Add a fixup for the displacement field.
183 if (isSVR4ABI() && is64BitMode())
184 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
185 (MCFixupKind)PPC::fixup_ppc_toc16));
187 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
188 (MCFixupKind)PPC::fixup_ppc_lo16));
193 unsigned PPCMCCodeEmitter::getMemRIXEncoding(const MCInst &MI, unsigned OpNo,
194 SmallVectorImpl<MCFixup> &Fixups) const {
195 // Encode (imm, reg) as a memrix, which has the low 14-bits as the
196 // displacement and the next 5 bits as the register #.
197 assert(MI.getOperand(OpNo+1).isReg());
198 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups) << 14;
200 const MCOperand &MO = MI.getOperand(OpNo);
202 return (getMachineOpValue(MI, MO, Fixups) & 0x3FFF) | RegBits;
204 // Add a fixup for the branch target.
205 if (isSVR4ABI() && is64BitMode())
206 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
207 (MCFixupKind)PPC::fixup_ppc_toc16_ds));
209 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
210 (MCFixupKind)PPC::fixup_ppc_lo14));
215 unsigned PPCMCCodeEmitter::getTLSOffsetEncoding(const MCInst &MI, unsigned OpNo,
216 SmallVectorImpl<MCFixup> &Fixups) const {
217 const MCOperand &MO = MI.getOperand(OpNo);
219 // Add a fixup for the GOT displacement to the TLS block offset.
220 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
221 (MCFixupKind)PPC::fixup_ppc_toc16_ds));
226 unsigned PPCMCCodeEmitter::getTLSRegEncoding(const MCInst &MI, unsigned OpNo,
227 SmallVectorImpl<MCFixup> &Fixups) const {
228 const MCOperand &MO = MI.getOperand(OpNo);
229 if (MO.isReg()) return getMachineOpValue(MI, MO, Fixups);
231 // Add a fixup for the TLS register, which simply provides a relocation
232 // hint to the linker that this statement is part of a relocation sequence.
233 // Return the thread-pointer register's encoding.
234 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
235 (MCFixupKind)PPC::fixup_ppc_tlsreg));
236 return getPPCRegisterNumbering(PPC::X13);
239 unsigned PPCMCCodeEmitter::
240 get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
241 SmallVectorImpl<MCFixup> &Fixups) const {
242 const MCOperand &MO = MI.getOperand(OpNo);
243 assert((MI.getOpcode() == PPC::MTCRF ||
244 MI.getOpcode() == PPC::MFOCRF ||
245 MI.getOpcode() == PPC::MTCRF8) &&
246 (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7));
247 return 0x80 >> getPPCRegisterNumbering(MO.getReg());
251 unsigned PPCMCCodeEmitter::
252 getMachineOpValue(const MCInst &MI, const MCOperand &MO,
253 SmallVectorImpl<MCFixup> &Fixups) const {
255 // MTCRF/MFOCRF should go through get_crbitm_encoding for the CR operand.
256 // The GPR operand should come through here though.
257 assert((MI.getOpcode() != PPC::MTCRF && MI.getOpcode() != PPC::MFOCRF) ||
258 MO.getReg() < PPC::CR0 || MO.getReg() > PPC::CR7);
259 return getPPCRegisterNumbering(MO.getReg());
263 "Relocation required in an instruction that we cannot encode!");
268 #include "PPCGenMCCodeEmitter.inc"