blob: 250d5455918d376d86a729b184776f3990dcc654 [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"
15#include "PPC.h"
Chris Lattnera04084e2010-11-15 04:51:55 +000016#include "PPCRegisterInfo.h"
Chris Lattnera9d9ab92010-11-15 05:57:53 +000017#include "PPCFixupKinds.h"
Chris Lattner5ffe38e2010-11-15 04:16:32 +000018#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"
23using namespace llvm;
24
25STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
26
27namespace {
28class PPCMCCodeEmitter : public MCCodeEmitter {
29 PPCMCCodeEmitter(const PPCMCCodeEmitter &); // DO NOT IMPLEMENT
30 void operator=(const PPCMCCodeEmitter &); // DO NOT IMPLEMENT
Chris Lattner5ffe38e2010-11-15 04:16:32 +000031 MCContext &Ctx;
32
33public:
Evan Cheng59ee62d2011-07-11 03:57:24 +000034 PPCMCCodeEmitter(const MCInstrInfo &mcii, const MCSubtargetInfo &sti,
35 MCContext &ctx)
36 : Ctx(ctx) {
Chris Lattner5ffe38e2010-11-15 04:16:32 +000037 }
38
39 ~PPCMCCodeEmitter() {}
Chris Lattner7192eb82010-11-15 05:19:25 +000040
Chris Lattner8d704112010-11-15 06:09:35 +000041 unsigned getDirectBrEncoding(const MCInst &MI, unsigned OpNo,
42 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattner8d704112010-11-15 06:09:35 +000043 unsigned getCondBrEncoding(const MCInst &MI, unsigned OpNo,
44 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattner85cf7d72010-11-15 06:33:39 +000045 unsigned getHA16Encoding(const MCInst &MI, unsigned OpNo,
46 SmallVectorImpl<MCFixup> &Fixups) const;
47 unsigned getLO16Encoding(const MCInst &MI, unsigned OpNo,
48 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattnerb7035d02010-11-15 08:22:03 +000049 unsigned getMemRIEncoding(const MCInst &MI, unsigned OpNo,
50 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattner17e2c182010-11-15 08:02:41 +000051 unsigned getMemRIXEncoding(const MCInst &MI, unsigned OpNo,
52 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattner7192eb82010-11-15 05:19:25 +000053 unsigned get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
54 SmallVectorImpl<MCFixup> &Fixups) const;
55
Chris Lattner5ffe38e2010-11-15 04:16:32 +000056 /// getMachineOpValue - Return binary encoding of operand. If the machine
57 /// operand requires relocation, record the relocation and return zero.
58 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
59 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattner5ffe38e2010-11-15 04:16:32 +000060
61 // getBinaryCodeForInstr - TableGen'erated function for getting the
62 // binary encoding for an instruction.
63 unsigned getBinaryCodeForInstr(const MCInst &MI,
64 SmallVectorImpl<MCFixup> &Fixups) const;
65 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
66 SmallVectorImpl<MCFixup> &Fixups) const {
67 unsigned Bits = getBinaryCodeForInstr(MI, Fixups);
68
69 // Output the constant in big endian byte order.
70 for (unsigned i = 0; i != 4; ++i) {
71 OS << (char)(Bits >> 24);
72 Bits <<= 8;
73 }
74
75 ++MCNumEmitted; // Keep track of the # of mi's emitted.
76 }
77
78};
79
80} // end anonymous namespace
81
Evan Cheng59ee62d2011-07-11 03:57:24 +000082MCCodeEmitter *llvm::createPPCMCCodeEmitter(const MCInstrInfo &MCII,
83 const MCSubtargetInfo &STI,
Chris Lattner5ffe38e2010-11-15 04:16:32 +000084 MCContext &Ctx) {
Evan Cheng59ee62d2011-07-11 03:57:24 +000085 return new PPCMCCodeEmitter(MCII, STI, Ctx);
Chris Lattner5ffe38e2010-11-15 04:16:32 +000086}
87
88unsigned PPCMCCodeEmitter::
Chris Lattner8d704112010-11-15 06:09:35 +000089getDirectBrEncoding(const MCInst &MI, unsigned OpNo,
90 SmallVectorImpl<MCFixup> &Fixups) const {
Chris Lattnera9d9ab92010-11-15 05:57:53 +000091 const MCOperand &MO = MI.getOperand(OpNo);
92 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
93
94 // Add a fixup for the branch target.
95 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
96 (MCFixupKind)PPC::fixup_ppc_br24));
97 return 0;
98}
99
Chris Lattner8d704112010-11-15 06:09:35 +0000100unsigned PPCMCCodeEmitter::getCondBrEncoding(const MCInst &MI, unsigned OpNo,
101 SmallVectorImpl<MCFixup> &Fixups) const {
102 const MCOperand &MO = MI.getOperand(OpNo);
103 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
104
Chris Lattnerb7194372010-11-15 06:12:22 +0000105 // Add a fixup for the branch target.
106 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
107 (MCFixupKind)PPC::fixup_ppc_brcond14));
Chris Lattner8d704112010-11-15 06:09:35 +0000108 return 0;
109}
110
Chris Lattner85cf7d72010-11-15 06:33:39 +0000111unsigned PPCMCCodeEmitter::getHA16Encoding(const MCInst &MI, unsigned OpNo,
112 SmallVectorImpl<MCFixup> &Fixups) const {
113 const MCOperand &MO = MI.getOperand(OpNo);
114 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
115
116 // Add a fixup for the branch target.
117 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
118 (MCFixupKind)PPC::fixup_ppc_ha16));
119 return 0;
120}
121
122unsigned PPCMCCodeEmitter::getLO16Encoding(const MCInst &MI, unsigned OpNo,
123 SmallVectorImpl<MCFixup> &Fixups) const {
124 const MCOperand &MO = MI.getOperand(OpNo);
125 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
126
127 // Add a fixup for the branch target.
128 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
129 (MCFixupKind)PPC::fixup_ppc_lo16));
130 return 0;
131}
132
Chris Lattnerb7035d02010-11-15 08:22:03 +0000133unsigned PPCMCCodeEmitter::getMemRIEncoding(const MCInst &MI, unsigned OpNo,
134 SmallVectorImpl<MCFixup> &Fixups) const {
135 // Encode (imm, reg) as a memri, which has the low 16-bits as the
136 // displacement and the next 5 bits as the register #.
137 assert(MI.getOperand(OpNo+1).isReg());
138 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups) << 16;
139
140 const MCOperand &MO = MI.getOperand(OpNo);
141 if (MO.isImm())
142 return (getMachineOpValue(MI, MO, Fixups) & 0xFFFF) | RegBits;
143
144 // Add a fixup for the displacement field.
145 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
146 (MCFixupKind)PPC::fixup_ppc_lo16));
147 return RegBits;
148}
149
150
Chris Lattner17e2c182010-11-15 08:02:41 +0000151unsigned PPCMCCodeEmitter::getMemRIXEncoding(const MCInst &MI, unsigned OpNo,
Chris Lattner85cf7d72010-11-15 06:33:39 +0000152 SmallVectorImpl<MCFixup> &Fixups) const {
Chris Lattner17e2c182010-11-15 08:02:41 +0000153 // Encode (imm, reg) as a memrix, which has the low 14-bits as the
154 // displacement and the next 5 bits as the register #.
Chris Lattnerb7035d02010-11-15 08:22:03 +0000155 assert(MI.getOperand(OpNo+1).isReg());
Chris Lattner17e2c182010-11-15 08:02:41 +0000156 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups) << 14;
157
Chris Lattner85cf7d72010-11-15 06:33:39 +0000158 const MCOperand &MO = MI.getOperand(OpNo);
Chris Lattner17e2c182010-11-15 08:02:41 +0000159 if (MO.isImm())
160 return (getMachineOpValue(MI, MO, Fixups) & 0x3FFF) | RegBits;
Chris Lattner85cf7d72010-11-15 06:33:39 +0000161
162 // Add a fixup for the branch target.
163 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
164 (MCFixupKind)PPC::fixup_ppc_lo14));
Chris Lattner17e2c182010-11-15 08:02:41 +0000165 return RegBits;
Chris Lattner85cf7d72010-11-15 06:33:39 +0000166}
167
Chris Lattner8d704112010-11-15 06:09:35 +0000168
Chris Lattnera9d9ab92010-11-15 05:57:53 +0000169unsigned PPCMCCodeEmitter::
Chris Lattner7192eb82010-11-15 05:19:25 +0000170get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
171 SmallVectorImpl<MCFixup> &Fixups) const {
172 const MCOperand &MO = MI.getOperand(OpNo);
173 assert((MI.getOpcode() == PPC::MTCRF || MI.getOpcode() == PPC::MFOCRF) &&
174 (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7));
175 return 0x80 >> PPCRegisterInfo::getRegisterNumbering(MO.getReg());
176}
177
178
179unsigned PPCMCCodeEmitter::
Chris Lattner5ffe38e2010-11-15 04:16:32 +0000180getMachineOpValue(const MCInst &MI, const MCOperand &MO,
181 SmallVectorImpl<MCFixup> &Fixups) const {
Chris Lattner7192eb82010-11-15 05:19:25 +0000182 if (MO.isReg()) {
Chris Lattner0382a4c2010-11-16 00:57:32 +0000183 // MTCRF/MFOCRF should go through get_crbitm_encoding for the CR operand.
184 // The GPR operand should come through here though.
Chris Lattnerb69cdfa2010-11-16 00:55:51 +0000185 assert((MI.getOpcode() != PPC::MTCRF && MI.getOpcode() != PPC::MFOCRF) ||
186 MO.getReg() < PPC::CR0 || MO.getReg() > PPC::CR7);
Chris Lattnera04084e2010-11-15 04:51:55 +0000187 return PPCRegisterInfo::getRegisterNumbering(MO.getReg());
Chris Lattner7192eb82010-11-15 05:19:25 +0000188 }
Chris Lattnera04084e2010-11-15 04:51:55 +0000189
Chris Lattnerb7035d02010-11-15 08:22:03 +0000190 assert(MO.isImm() &&
191 "Relocation required in an instruction that we cannot encode!");
192 return MO.getImm();
Chris Lattner5ffe38e2010-11-15 04:16:32 +0000193}
194
195
196#include "PPCGenMCCodeEmitter.inc"