blob: c0248a6045562969b925032838b6d2362d1e09fb [file] [log] [blame]
Chris Lattner9ec375c2010-11-15 04:16:32 +00001//===-- 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"
Evan Cheng61d4a202011-07-25 19:53:23 +000015#include "MCTargetDesc/PPCBaseInfo.h"
16#include "MCTargetDesc/PPCFixupKinds.h"
Chris Lattner9ec375c2010-11-15 04:16:32 +000017#include "llvm/MC/MCCodeEmitter.h"
18#include "llvm/MC/MCInst.h"
19#include "llvm/ADT/Statistic.h"
20#include "llvm/Support/raw_ostream.h"
21#include "llvm/Support/ErrorHandling.h"
22using namespace llvm;
23
24STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
25
26namespace {
27class PPCMCCodeEmitter : public MCCodeEmitter {
Craig Toppera60c0f12012-09-15 17:09:36 +000028 PPCMCCodeEmitter(const PPCMCCodeEmitter &) LLVM_DELETED_FUNCTION;
29 void operator=(const PPCMCCodeEmitter &) LLVM_DELETED_FUNCTION;
30
Chris Lattner9ec375c2010-11-15 04:16:32 +000031public:
Evan Chengc5e6d2f2011-07-11 03:57:24 +000032 PPCMCCodeEmitter(const MCInstrInfo &mcii, const MCSubtargetInfo &sti,
Evan Cheng58a98142011-07-11 21:24:15 +000033 MCContext &ctx) {
Chris Lattner9ec375c2010-11-15 04:16:32 +000034 }
35
36 ~PPCMCCodeEmitter() {}
Chris Lattnerd6a07cc2010-11-15 05:19:25 +000037
Chris Lattner0e3461e2010-11-15 06:09:35 +000038 unsigned getDirectBrEncoding(const MCInst &MI, unsigned OpNo,
39 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattner0e3461e2010-11-15 06:09:35 +000040 unsigned getCondBrEncoding(const MCInst &MI, unsigned OpNo,
41 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattner65661122010-11-15 06:33:39 +000042 unsigned getHA16Encoding(const MCInst &MI, unsigned OpNo,
43 SmallVectorImpl<MCFixup> &Fixups) const;
44 unsigned getLO16Encoding(const MCInst &MI, unsigned OpNo,
45 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattnerefacb9e2010-11-15 08:22:03 +000046 unsigned getMemRIEncoding(const MCInst &MI, unsigned OpNo,
47 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattner8f4444d2010-11-15 08:02:41 +000048 unsigned getMemRIXEncoding(const MCInst &MI, unsigned OpNo,
49 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattnerd6a07cc2010-11-15 05:19:25 +000050 unsigned get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
51 SmallVectorImpl<MCFixup> &Fixups) const;
52
Chris Lattner9ec375c2010-11-15 04:16:32 +000053 /// getMachineOpValue - Return binary encoding of operand. If the machine
54 /// operand requires relocation, record the relocation and return zero.
55 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
56 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattner9ec375c2010-11-15 04:16:32 +000057
58 // getBinaryCodeForInstr - TableGen'erated function for getting the
59 // binary encoding for an instruction.
Owen Andersond845d9d2012-01-24 18:37:29 +000060 uint64_t getBinaryCodeForInstr(const MCInst &MI,
Chris Lattner9ec375c2010-11-15 04:16:32 +000061 SmallVectorImpl<MCFixup> &Fixups) const;
62 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
63 SmallVectorImpl<MCFixup> &Fixups) const {
64 unsigned Bits = getBinaryCodeForInstr(MI, Fixups);
65
66 // Output the constant in big endian byte order.
67 for (unsigned i = 0; i != 4; ++i) {
68 OS << (char)(Bits >> 24);
69 Bits <<= 8;
70 }
71
72 ++MCNumEmitted; // Keep track of the # of mi's emitted.
73 }
74
75};
76
77} // end anonymous namespace
78
Evan Chengc5e6d2f2011-07-11 03:57:24 +000079MCCodeEmitter *llvm::createPPCMCCodeEmitter(const MCInstrInfo &MCII,
Jim Grosbachc3b04272012-05-15 17:35:52 +000080 const MCRegisterInfo &MRI,
Evan Chengc5e6d2f2011-07-11 03:57:24 +000081 const MCSubtargetInfo &STI,
Chris Lattner9ec375c2010-11-15 04:16:32 +000082 MCContext &Ctx) {
Evan Chengc5e6d2f2011-07-11 03:57:24 +000083 return new PPCMCCodeEmitter(MCII, STI, Ctx);
Chris Lattner9ec375c2010-11-15 04:16:32 +000084}
85
86unsigned PPCMCCodeEmitter::
Chris Lattner0e3461e2010-11-15 06:09:35 +000087getDirectBrEncoding(const MCInst &MI, unsigned OpNo,
88 SmallVectorImpl<MCFixup> &Fixups) const {
Chris Lattner79fa3712010-11-15 05:57:53 +000089 const MCOperand &MO = MI.getOperand(OpNo);
90 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
91
92 // Add a fixup for the branch target.
93 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
94 (MCFixupKind)PPC::fixup_ppc_br24));
95 return 0;
96}
97
Chris Lattner0e3461e2010-11-15 06:09:35 +000098unsigned PPCMCCodeEmitter::getCondBrEncoding(const MCInst &MI, unsigned OpNo,
99 SmallVectorImpl<MCFixup> &Fixups) const {
100 const MCOperand &MO = MI.getOperand(OpNo);
101 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
102
Chris Lattner85e37682010-11-15 06:12:22 +0000103 // Add a fixup for the branch target.
104 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
105 (MCFixupKind)PPC::fixup_ppc_brcond14));
Chris Lattner0e3461e2010-11-15 06:09:35 +0000106 return 0;
107}
108
Chris Lattner65661122010-11-15 06:33:39 +0000109unsigned PPCMCCodeEmitter::getHA16Encoding(const MCInst &MI, unsigned OpNo,
110 SmallVectorImpl<MCFixup> &Fixups) const {
111 const MCOperand &MO = MI.getOperand(OpNo);
112 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
113
114 // Add a fixup for the branch target.
115 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
116 (MCFixupKind)PPC::fixup_ppc_ha16));
117 return 0;
118}
119
120unsigned PPCMCCodeEmitter::getLO16Encoding(const MCInst &MI, unsigned OpNo,
121 SmallVectorImpl<MCFixup> &Fixups) const {
122 const MCOperand &MO = MI.getOperand(OpNo);
123 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
124
125 // Add a fixup for the branch target.
126 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
127 (MCFixupKind)PPC::fixup_ppc_lo16));
128 return 0;
129}
130
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000131unsigned PPCMCCodeEmitter::getMemRIEncoding(const MCInst &MI, unsigned OpNo,
132 SmallVectorImpl<MCFixup> &Fixups) const {
133 // Encode (imm, reg) as a memri, which has the low 16-bits as the
134 // displacement and the next 5 bits as the register #.
135 assert(MI.getOperand(OpNo+1).isReg());
136 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups) << 16;
137
138 const MCOperand &MO = MI.getOperand(OpNo);
139 if (MO.isImm())
140 return (getMachineOpValue(MI, MO, Fixups) & 0xFFFF) | RegBits;
141
142 // Add a fixup for the displacement field.
143 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
144 (MCFixupKind)PPC::fixup_ppc_lo16));
145 return RegBits;
146}
147
148
Chris Lattner8f4444d2010-11-15 08:02:41 +0000149unsigned PPCMCCodeEmitter::getMemRIXEncoding(const MCInst &MI, unsigned OpNo,
Chris Lattner65661122010-11-15 06:33:39 +0000150 SmallVectorImpl<MCFixup> &Fixups) const {
Chris Lattner8f4444d2010-11-15 08:02:41 +0000151 // Encode (imm, reg) as a memrix, which has the low 14-bits as the
152 // displacement and the next 5 bits as the register #.
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000153 assert(MI.getOperand(OpNo+1).isReg());
Chris Lattner8f4444d2010-11-15 08:02:41 +0000154 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups) << 14;
155
Chris Lattner65661122010-11-15 06:33:39 +0000156 const MCOperand &MO = MI.getOperand(OpNo);
Chris Lattner8f4444d2010-11-15 08:02:41 +0000157 if (MO.isImm())
158 return (getMachineOpValue(MI, MO, Fixups) & 0x3FFF) | RegBits;
Chris Lattner65661122010-11-15 06:33:39 +0000159
160 // Add a fixup for the branch target.
161 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
162 (MCFixupKind)PPC::fixup_ppc_lo14));
Chris Lattner8f4444d2010-11-15 08:02:41 +0000163 return RegBits;
Chris Lattner65661122010-11-15 06:33:39 +0000164}
165
Chris Lattner0e3461e2010-11-15 06:09:35 +0000166
Chris Lattner79fa3712010-11-15 05:57:53 +0000167unsigned PPCMCCodeEmitter::
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000168get_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));
Evan Cheng61d4a202011-07-25 19:53:23 +0000173 return 0x80 >> getPPCRegisterNumbering(MO.getReg());
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000174}
175
176
177unsigned PPCMCCodeEmitter::
Chris Lattner9ec375c2010-11-15 04:16:32 +0000178getMachineOpValue(const MCInst &MI, const MCOperand &MO,
179 SmallVectorImpl<MCFixup> &Fixups) const {
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000180 if (MO.isReg()) {
Chris Lattner7b25d6f2010-11-16 00:57:32 +0000181 // MTCRF/MFOCRF should go through get_crbitm_encoding for the CR operand.
182 // The GPR operand should come through here though.
Chris Lattner73716a62010-11-16 00:55:51 +0000183 assert((MI.getOpcode() != PPC::MTCRF && MI.getOpcode() != PPC::MFOCRF) ||
184 MO.getReg() < PPC::CR0 || MO.getReg() > PPC::CR7);
Evan Cheng61d4a202011-07-25 19:53:23 +0000185 return getPPCRegisterNumbering(MO.getReg());
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000186 }
Chris Lattnerc877d8f2010-11-15 04:51:55 +0000187
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000188 assert(MO.isImm() &&
189 "Relocation required in an instruction that we cannot encode!");
190 return MO.getImm();
Chris Lattner9ec375c2010-11-15 04:16:32 +0000191}
192
193
194#include "PPCGenMCCodeEmitter.inc"