blob: 262f97c36a9364677e1068f3dc79264ed3d1a643 [file] [log] [blame]
Chris Lattner5ffe38e2010-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 Cheng966aeb52011-07-25 19:53:23 +000015#include "MCTargetDesc/PPCBaseInfo.h"
16#include "MCTargetDesc/PPCFixupKinds.h"
Chris Lattner5ffe38e2010-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 {
28 PPCMCCodeEmitter(const PPCMCCodeEmitter &); // DO NOT IMPLEMENT
29 void operator=(const PPCMCCodeEmitter &); // DO NOT IMPLEMENT
Chris Lattner5ffe38e2010-11-15 04:16:32 +000030
31public:
Evan Cheng59ee62d2011-07-11 03:57:24 +000032 PPCMCCodeEmitter(const MCInstrInfo &mcii, const MCSubtargetInfo &sti,
Evan Chengaf0a2e62011-07-11 21:24:15 +000033 MCContext &ctx) {
Chris Lattner5ffe38e2010-11-15 04:16:32 +000034 }
35
36 ~PPCMCCodeEmitter() {}
Chris Lattner7192eb82010-11-15 05:19:25 +000037
Chris Lattner8d704112010-11-15 06:09:35 +000038 unsigned getDirectBrEncoding(const MCInst &MI, unsigned OpNo,
39 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattner8d704112010-11-15 06:09:35 +000040 unsigned getCondBrEncoding(const MCInst &MI, unsigned OpNo,
41 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattner85cf7d72010-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 Lattnerb7035d02010-11-15 08:22:03 +000046 unsigned getMemRIEncoding(const MCInst &MI, unsigned OpNo,
47 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattner17e2c182010-11-15 08:02:41 +000048 unsigned getMemRIXEncoding(const MCInst &MI, unsigned OpNo,
49 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattner7192eb82010-11-15 05:19:25 +000050 unsigned get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
51 SmallVectorImpl<MCFixup> &Fixups) const;
52
Chris Lattner5ffe38e2010-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 Lattner5ffe38e2010-11-15 04:16:32 +000057
58 // getBinaryCodeForInstr - TableGen'erated function for getting the
59 // binary encoding for an instruction.
60 unsigned getBinaryCodeForInstr(const MCInst &MI,
61 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 Cheng59ee62d2011-07-11 03:57:24 +000079MCCodeEmitter *llvm::createPPCMCCodeEmitter(const MCInstrInfo &MCII,
80 const MCSubtargetInfo &STI,
Chris Lattner5ffe38e2010-11-15 04:16:32 +000081 MCContext &Ctx) {
Evan Cheng59ee62d2011-07-11 03:57:24 +000082 return new PPCMCCodeEmitter(MCII, STI, Ctx);
Chris Lattner5ffe38e2010-11-15 04:16:32 +000083}
84
85unsigned PPCMCCodeEmitter::
Chris Lattner8d704112010-11-15 06:09:35 +000086getDirectBrEncoding(const MCInst &MI, unsigned OpNo,
87 SmallVectorImpl<MCFixup> &Fixups) const {
Chris Lattnera9d9ab92010-11-15 05:57:53 +000088 const MCOperand &MO = MI.getOperand(OpNo);
89 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
90
91 // Add a fixup for the branch target.
92 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
93 (MCFixupKind)PPC::fixup_ppc_br24));
94 return 0;
95}
96
Chris Lattner8d704112010-11-15 06:09:35 +000097unsigned PPCMCCodeEmitter::getCondBrEncoding(const MCInst &MI, unsigned OpNo,
98 SmallVectorImpl<MCFixup> &Fixups) const {
99 const MCOperand &MO = MI.getOperand(OpNo);
100 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
101
Chris Lattnerb7194372010-11-15 06:12:22 +0000102 // Add a fixup for the branch target.
103 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
104 (MCFixupKind)PPC::fixup_ppc_brcond14));
Chris Lattner8d704112010-11-15 06:09:35 +0000105 return 0;
106}
107
Chris Lattner85cf7d72010-11-15 06:33:39 +0000108unsigned PPCMCCodeEmitter::getHA16Encoding(const MCInst &MI, unsigned OpNo,
109 SmallVectorImpl<MCFixup> &Fixups) const {
110 const MCOperand &MO = MI.getOperand(OpNo);
111 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
112
113 // Add a fixup for the branch target.
114 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
115 (MCFixupKind)PPC::fixup_ppc_ha16));
116 return 0;
117}
118
119unsigned PPCMCCodeEmitter::getLO16Encoding(const MCInst &MI, unsigned OpNo,
120 SmallVectorImpl<MCFixup> &Fixups) const {
121 const MCOperand &MO = MI.getOperand(OpNo);
122 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
123
124 // Add a fixup for the branch target.
125 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
126 (MCFixupKind)PPC::fixup_ppc_lo16));
127 return 0;
128}
129
Chris Lattnerb7035d02010-11-15 08:22:03 +0000130unsigned PPCMCCodeEmitter::getMemRIEncoding(const MCInst &MI, unsigned OpNo,
131 SmallVectorImpl<MCFixup> &Fixups) const {
132 // Encode (imm, reg) as a memri, which has the low 16-bits as the
133 // displacement and the next 5 bits as the register #.
134 assert(MI.getOperand(OpNo+1).isReg());
135 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups) << 16;
136
137 const MCOperand &MO = MI.getOperand(OpNo);
138 if (MO.isImm())
139 return (getMachineOpValue(MI, MO, Fixups) & 0xFFFF) | RegBits;
140
141 // Add a fixup for the displacement field.
142 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
143 (MCFixupKind)PPC::fixup_ppc_lo16));
144 return RegBits;
145}
146
147
Chris Lattner17e2c182010-11-15 08:02:41 +0000148unsigned PPCMCCodeEmitter::getMemRIXEncoding(const MCInst &MI, unsigned OpNo,
Chris Lattner85cf7d72010-11-15 06:33:39 +0000149 SmallVectorImpl<MCFixup> &Fixups) const {
Chris Lattner17e2c182010-11-15 08:02:41 +0000150 // Encode (imm, reg) as a memrix, which has the low 14-bits as the
151 // displacement and the next 5 bits as the register #.
Chris Lattnerb7035d02010-11-15 08:22:03 +0000152 assert(MI.getOperand(OpNo+1).isReg());
Chris Lattner17e2c182010-11-15 08:02:41 +0000153 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups) << 14;
154
Chris Lattner85cf7d72010-11-15 06:33:39 +0000155 const MCOperand &MO = MI.getOperand(OpNo);
Chris Lattner17e2c182010-11-15 08:02:41 +0000156 if (MO.isImm())
157 return (getMachineOpValue(MI, MO, Fixups) & 0x3FFF) | RegBits;
Chris Lattner85cf7d72010-11-15 06:33:39 +0000158
159 // Add a fixup for the branch target.
160 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
161 (MCFixupKind)PPC::fixup_ppc_lo14));
Chris Lattner17e2c182010-11-15 08:02:41 +0000162 return RegBits;
Chris Lattner85cf7d72010-11-15 06:33:39 +0000163}
164
Chris Lattner8d704112010-11-15 06:09:35 +0000165
Chris Lattnera9d9ab92010-11-15 05:57:53 +0000166unsigned PPCMCCodeEmitter::
Chris Lattner7192eb82010-11-15 05:19:25 +0000167get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
168 SmallVectorImpl<MCFixup> &Fixups) const {
169 const MCOperand &MO = MI.getOperand(OpNo);
170 assert((MI.getOpcode() == PPC::MTCRF || MI.getOpcode() == PPC::MFOCRF) &&
171 (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7));
Evan Cheng966aeb52011-07-25 19:53:23 +0000172 return 0x80 >> getPPCRegisterNumbering(MO.getReg());
Chris Lattner7192eb82010-11-15 05:19:25 +0000173}
174
175
176unsigned PPCMCCodeEmitter::
Chris Lattner5ffe38e2010-11-15 04:16:32 +0000177getMachineOpValue(const MCInst &MI, const MCOperand &MO,
178 SmallVectorImpl<MCFixup> &Fixups) const {
Chris Lattner7192eb82010-11-15 05:19:25 +0000179 if (MO.isReg()) {
Chris Lattner0382a4c2010-11-16 00:57:32 +0000180 // MTCRF/MFOCRF should go through get_crbitm_encoding for the CR operand.
181 // The GPR operand should come through here though.
Chris Lattnerb69cdfa2010-11-16 00:55:51 +0000182 assert((MI.getOpcode() != PPC::MTCRF && MI.getOpcode() != PPC::MFOCRF) ||
183 MO.getReg() < PPC::CR0 || MO.getReg() > PPC::CR7);
Evan Cheng966aeb52011-07-25 19:53:23 +0000184 return getPPCRegisterNumbering(MO.getReg());
Chris Lattner7192eb82010-11-15 05:19:25 +0000185 }
Chris Lattnera04084e2010-11-15 04:51:55 +0000186
Chris Lattnerb7035d02010-11-15 08:22:03 +0000187 assert(MO.isImm() &&
188 "Relocation required in an instruction that we cannot encode!");
189 return MO.getImm();
Chris Lattner5ffe38e2010-11-15 04:16:32 +0000190}
191
192
193#include "PPCGenMCCodeEmitter.inc"