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 #include "MCTargetDesc/PPCFixupKinds.h"
15 #include "PPCInstrInfo.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/ADT/Statistic.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/MC/MCAsmInfo.h"
20 #include "llvm/MC/MCCodeEmitter.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCFixup.h"
23 #include "llvm/MC/MCInst.h"
24 #include "llvm/MC/MCInstrDesc.h"
25 #include "llvm/MC/MCInstrInfo.h"
26 #include "llvm/MC/MCRegisterInfo.h"
27 #include "llvm/MC/MCSubtargetInfo.h"
28 #include "llvm/Support/Endian.h"
29 #include "llvm/Support/EndianStream.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/MathExtras.h"
32 #include "llvm/Support/raw_ostream.h"
38 #define DEBUG_TYPE "mccodeemitter"
40 STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
44 class PPCMCCodeEmitter : public MCCodeEmitter {
45 const MCInstrInfo &MCII;
50 PPCMCCodeEmitter(const MCInstrInfo &mcii, MCContext &ctx)
51 : MCII(mcii), CTX(ctx),
52 IsLittleEndian(ctx.getAsmInfo()->isLittleEndian()) {}
53 PPCMCCodeEmitter(const PPCMCCodeEmitter &) = delete;
54 void operator=(const PPCMCCodeEmitter &) = delete;
55 ~PPCMCCodeEmitter() override = default;
57 unsigned getDirectBrEncoding(const MCInst &MI, unsigned OpNo,
58 SmallVectorImpl<MCFixup> &Fixups,
59 const MCSubtargetInfo &STI) const;
60 unsigned getCondBrEncoding(const MCInst &MI, unsigned OpNo,
61 SmallVectorImpl<MCFixup> &Fixups,
62 const MCSubtargetInfo &STI) const;
63 unsigned getAbsDirectBrEncoding(const MCInst &MI, unsigned OpNo,
64 SmallVectorImpl<MCFixup> &Fixups,
65 const MCSubtargetInfo &STI) const;
66 unsigned getAbsCondBrEncoding(const MCInst &MI, unsigned OpNo,
67 SmallVectorImpl<MCFixup> &Fixups,
68 const MCSubtargetInfo &STI) const;
69 unsigned getImm16Encoding(const MCInst &MI, unsigned OpNo,
70 SmallVectorImpl<MCFixup> &Fixups,
71 const MCSubtargetInfo &STI) const;
72 unsigned getMemRIEncoding(const MCInst &MI, unsigned OpNo,
73 SmallVectorImpl<MCFixup> &Fixups,
74 const MCSubtargetInfo &STI) const;
75 unsigned getMemRIXEncoding(const MCInst &MI, unsigned OpNo,
76 SmallVectorImpl<MCFixup> &Fixups,
77 const MCSubtargetInfo &STI) const;
78 unsigned getMemRIX16Encoding(const MCInst &MI, unsigned OpNo,
79 SmallVectorImpl<MCFixup> &Fixups,
80 const MCSubtargetInfo &STI) const;
81 unsigned getSPE8DisEncoding(const MCInst &MI, unsigned OpNo,
82 SmallVectorImpl<MCFixup> &Fixups,
83 const MCSubtargetInfo &STI) const;
84 unsigned getSPE4DisEncoding(const MCInst &MI, unsigned OpNo,
85 SmallVectorImpl<MCFixup> &Fixups,
86 const MCSubtargetInfo &STI) const;
87 unsigned getSPE2DisEncoding(const MCInst &MI, unsigned OpNo,
88 SmallVectorImpl<MCFixup> &Fixups,
89 const MCSubtargetInfo &STI) const;
90 unsigned getTLSRegEncoding(const MCInst &MI, unsigned OpNo,
91 SmallVectorImpl<MCFixup> &Fixups,
92 const MCSubtargetInfo &STI) const;
93 unsigned getTLSCallEncoding(const MCInst &MI, unsigned OpNo,
94 SmallVectorImpl<MCFixup> &Fixups,
95 const MCSubtargetInfo &STI) const;
96 unsigned get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
97 SmallVectorImpl<MCFixup> &Fixups,
98 const MCSubtargetInfo &STI) const;
100 /// getMachineOpValue - Return binary encoding of operand. If the machine
101 /// operand requires relocation, record the relocation and return zero.
102 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
103 SmallVectorImpl<MCFixup> &Fixups,
104 const MCSubtargetInfo &STI) const;
106 // getBinaryCodeForInstr - TableGen'erated function for getting the
107 // binary encoding for an instruction.
108 uint64_t getBinaryCodeForInstr(const MCInst &MI,
109 SmallVectorImpl<MCFixup> &Fixups,
110 const MCSubtargetInfo &STI) const;
112 void encodeInstruction(const MCInst &MI, raw_ostream &OS,
113 SmallVectorImpl<MCFixup> &Fixups,
114 const MCSubtargetInfo &STI) const override {
115 verifyInstructionPredicates(MI,
116 computeAvailableFeatures(STI.getFeatureBits()));
118 unsigned Opcode = MI.getOpcode();
119 const MCInstrDesc &Desc = MCII.get(Opcode);
121 uint64_t Bits = getBinaryCodeForInstr(MI, Fixups, STI);
123 // Output the constant in big/little endian byte order.
124 unsigned Size = Desc.getSize();
125 support::endianness E = IsLittleEndian ? support::little : support::big;
130 support::endian::write<uint32_t>(OS, Bits, E);
133 // If we emit a pair of instructions, the first one is
134 // always in the top 32 bits, even on little-endian.
135 support::endian::write<uint32_t>(OS, Bits >> 32, E);
136 support::endian::write<uint32_t>(OS, Bits, E);
139 llvm_unreachable("Invalid instruction size");
142 ++MCNumEmitted; // Keep track of the # of mi's emitted.
146 uint64_t computeAvailableFeatures(const FeatureBitset &FB) const;
147 void verifyInstructionPredicates(const MCInst &MI,
148 uint64_t AvailableFeatures) const;
151 } // end anonymous namespace
153 MCCodeEmitter *llvm::createPPCMCCodeEmitter(const MCInstrInfo &MCII,
154 const MCRegisterInfo &MRI,
156 return new PPCMCCodeEmitter(MCII, Ctx);
159 unsigned PPCMCCodeEmitter::
160 getDirectBrEncoding(const MCInst &MI, unsigned OpNo,
161 SmallVectorImpl<MCFixup> &Fixups,
162 const MCSubtargetInfo &STI) const {
163 const MCOperand &MO = MI.getOperand(OpNo);
164 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI);
166 // Add a fixup for the branch target.
167 Fixups.push_back(MCFixup::create(0, MO.getExpr(),
168 (MCFixupKind)PPC::fixup_ppc_br24));
172 unsigned PPCMCCodeEmitter::getCondBrEncoding(const MCInst &MI, unsigned OpNo,
173 SmallVectorImpl<MCFixup> &Fixups,
174 const MCSubtargetInfo &STI) const {
175 const MCOperand &MO = MI.getOperand(OpNo);
176 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI);
178 // Add a fixup for the branch target.
179 Fixups.push_back(MCFixup::create(0, MO.getExpr(),
180 (MCFixupKind)PPC::fixup_ppc_brcond14));
184 unsigned PPCMCCodeEmitter::
185 getAbsDirectBrEncoding(const MCInst &MI, unsigned OpNo,
186 SmallVectorImpl<MCFixup> &Fixups,
187 const MCSubtargetInfo &STI) const {
188 const MCOperand &MO = MI.getOperand(OpNo);
189 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI);
191 // Add a fixup for the branch target.
192 Fixups.push_back(MCFixup::create(0, MO.getExpr(),
193 (MCFixupKind)PPC::fixup_ppc_br24abs));
197 unsigned PPCMCCodeEmitter::
198 getAbsCondBrEncoding(const MCInst &MI, unsigned OpNo,
199 SmallVectorImpl<MCFixup> &Fixups,
200 const MCSubtargetInfo &STI) const {
201 const MCOperand &MO = MI.getOperand(OpNo);
202 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI);
204 // Add a fixup for the branch target.
205 Fixups.push_back(MCFixup::create(0, MO.getExpr(),
206 (MCFixupKind)PPC::fixup_ppc_brcond14abs));
210 unsigned PPCMCCodeEmitter::getImm16Encoding(const MCInst &MI, unsigned OpNo,
211 SmallVectorImpl<MCFixup> &Fixups,
212 const MCSubtargetInfo &STI) const {
213 const MCOperand &MO = MI.getOperand(OpNo);
214 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI);
216 // Add a fixup for the immediate field.
217 Fixups.push_back(MCFixup::create(IsLittleEndian? 0 : 2, MO.getExpr(),
218 (MCFixupKind)PPC::fixup_ppc_half16));
222 unsigned PPCMCCodeEmitter::getMemRIEncoding(const MCInst &MI, unsigned OpNo,
223 SmallVectorImpl<MCFixup> &Fixups,
224 const MCSubtargetInfo &STI) const {
225 // Encode (imm, reg) as a memri, which has the low 16-bits as the
226 // displacement and the next 5 bits as the register #.
227 assert(MI.getOperand(OpNo+1).isReg());
228 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 16;
230 const MCOperand &MO = MI.getOperand(OpNo);
232 return (getMachineOpValue(MI, MO, Fixups, STI) & 0xFFFF) | RegBits;
234 // Add a fixup for the displacement field.
235 Fixups.push_back(MCFixup::create(IsLittleEndian? 0 : 2, MO.getExpr(),
236 (MCFixupKind)PPC::fixup_ppc_half16));
240 unsigned PPCMCCodeEmitter::getMemRIXEncoding(const MCInst &MI, unsigned OpNo,
241 SmallVectorImpl<MCFixup> &Fixups,
242 const MCSubtargetInfo &STI) const {
243 // Encode (imm, reg) as a memrix, which has the low 14-bits as the
244 // displacement and the next 5 bits as the register #.
245 assert(MI.getOperand(OpNo+1).isReg());
246 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 14;
248 const MCOperand &MO = MI.getOperand(OpNo);
250 return ((getMachineOpValue(MI, MO, Fixups, STI) >> 2) & 0x3FFF) | RegBits;
252 // Add a fixup for the displacement field.
253 Fixups.push_back(MCFixup::create(IsLittleEndian? 0 : 2, MO.getExpr(),
254 (MCFixupKind)PPC::fixup_ppc_half16ds));
258 unsigned PPCMCCodeEmitter::getMemRIX16Encoding(const MCInst &MI, unsigned OpNo,
259 SmallVectorImpl<MCFixup> &Fixups,
260 const MCSubtargetInfo &STI) const {
261 // Encode (imm, reg) as a memrix16, which has the low 12-bits as the
262 // displacement and the next 5 bits as the register #.
263 assert(MI.getOperand(OpNo+1).isReg());
264 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 12;
266 const MCOperand &MO = MI.getOperand(OpNo);
267 assert(MO.isImm() && !(MO.getImm() % 16) &&
268 "Expecting an immediate that is a multiple of 16");
270 return ((getMachineOpValue(MI, MO, Fixups, STI) >> 4) & 0xFFF) | RegBits;
273 unsigned PPCMCCodeEmitter::getSPE8DisEncoding(const MCInst &MI, unsigned OpNo,
274 SmallVectorImpl<MCFixup> &Fixups,
275 const MCSubtargetInfo &STI)
277 // Encode (imm, reg) as a spe8dis, which has the low 5-bits of (imm / 8)
278 // as the displacement and the next 5 bits as the register #.
279 assert(MI.getOperand(OpNo+1).isReg());
280 uint32_t RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 5;
282 const MCOperand &MO = MI.getOperand(OpNo);
284 uint32_t Imm = getMachineOpValue(MI, MO, Fixups, STI) >> 3;
285 return reverseBits(Imm | RegBits) >> 22;
288 unsigned PPCMCCodeEmitter::getSPE4DisEncoding(const MCInst &MI, unsigned OpNo,
289 SmallVectorImpl<MCFixup> &Fixups,
290 const MCSubtargetInfo &STI)
292 // Encode (imm, reg) as a spe4dis, which has the low 5-bits of (imm / 4)
293 // as the displacement and the next 5 bits as the register #.
294 assert(MI.getOperand(OpNo+1).isReg());
295 uint32_t RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 5;
297 const MCOperand &MO = MI.getOperand(OpNo);
299 uint32_t Imm = getMachineOpValue(MI, MO, Fixups, STI) >> 2;
300 return reverseBits(Imm | RegBits) >> 22;
303 unsigned PPCMCCodeEmitter::getSPE2DisEncoding(const MCInst &MI, unsigned OpNo,
304 SmallVectorImpl<MCFixup> &Fixups,
305 const MCSubtargetInfo &STI)
307 // Encode (imm, reg) as a spe2dis, which has the low 5-bits of (imm / 2)
308 // as the displacement and the next 5 bits as the register #.
309 assert(MI.getOperand(OpNo+1).isReg());
310 uint32_t RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 5;
312 const MCOperand &MO = MI.getOperand(OpNo);
314 uint32_t Imm = getMachineOpValue(MI, MO, Fixups, STI) >> 1;
315 return reverseBits(Imm | RegBits) >> 22;
318 unsigned PPCMCCodeEmitter::getTLSRegEncoding(const MCInst &MI, unsigned OpNo,
319 SmallVectorImpl<MCFixup> &Fixups,
320 const MCSubtargetInfo &STI) const {
321 const MCOperand &MO = MI.getOperand(OpNo);
322 if (MO.isReg()) return getMachineOpValue(MI, MO, Fixups, STI);
324 // Add a fixup for the TLS register, which simply provides a relocation
325 // hint to the linker that this statement is part of a relocation sequence.
326 // Return the thread-pointer register's encoding.
327 Fixups.push_back(MCFixup::create(0, MO.getExpr(),
328 (MCFixupKind)PPC::fixup_ppc_nofixup));
329 const Triple &TT = STI.getTargetTriple();
330 bool isPPC64 = TT.getArch() == Triple::ppc64 || TT.getArch() == Triple::ppc64le;
331 return CTX.getRegisterInfo()->getEncodingValue(isPPC64 ? PPC::X13 : PPC::R2);
334 unsigned PPCMCCodeEmitter::getTLSCallEncoding(const MCInst &MI, unsigned OpNo,
335 SmallVectorImpl<MCFixup> &Fixups,
336 const MCSubtargetInfo &STI) const {
337 // For special TLS calls, we need two fixups; one for the branch target
338 // (__tls_get_addr), which we create via getDirectBrEncoding as usual,
339 // and one for the TLSGD or TLSLD symbol, which is emitted here.
340 const MCOperand &MO = MI.getOperand(OpNo+1);
341 Fixups.push_back(MCFixup::create(0, MO.getExpr(),
342 (MCFixupKind)PPC::fixup_ppc_nofixup));
343 return getDirectBrEncoding(MI, OpNo, Fixups, STI);
346 unsigned PPCMCCodeEmitter::
347 get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
348 SmallVectorImpl<MCFixup> &Fixups,
349 const MCSubtargetInfo &STI) const {
350 const MCOperand &MO = MI.getOperand(OpNo);
351 assert((MI.getOpcode() == PPC::MTOCRF || MI.getOpcode() == PPC::MTOCRF8 ||
352 MI.getOpcode() == PPC::MFOCRF || MI.getOpcode() == PPC::MFOCRF8) &&
353 (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7));
354 return 0x80 >> CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
357 unsigned PPCMCCodeEmitter::
358 getMachineOpValue(const MCInst &MI, const MCOperand &MO,
359 SmallVectorImpl<MCFixup> &Fixups,
360 const MCSubtargetInfo &STI) const {
362 // MTOCRF/MFOCRF should go through get_crbitm_encoding for the CR operand.
363 // The GPR operand should come through here though.
364 assert((MI.getOpcode() != PPC::MTOCRF && MI.getOpcode() != PPC::MTOCRF8 &&
365 MI.getOpcode() != PPC::MFOCRF && MI.getOpcode() != PPC::MFOCRF8) ||
366 MO.getReg() < PPC::CR0 || MO.getReg() > PPC::CR7);
367 unsigned Reg = MO.getReg();
368 unsigned Encode = CTX.getRegisterInfo()->getEncodingValue(Reg);
370 if ((MCII.get(MI.getOpcode()).TSFlags & PPCII::UseVSXReg))
371 if (PPCInstrInfo::isVRRegister(Reg))
378 "Relocation required in an instruction that we cannot encode!");
382 #define ENABLE_INSTR_PREDICATE_VERIFIER
383 #include "PPCGenMCCodeEmitter.inc"