blob: 5b208d41f4f64bd854140ee0248f6ee27f7d5ab4 [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"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "MCTargetDesc/PPCMCTargetDesc.h"
Evan Cheng61d4a202011-07-25 19:53:23 +000016#include "MCTargetDesc/PPCBaseInfo.h"
17#include "MCTargetDesc/PPCFixupKinds.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/ADT/Statistic.h"
Chris Lattner9ec375c2010-11-15 04:16:32 +000019#include "llvm/MC/MCCodeEmitter.h"
20#include "llvm/MC/MCInst.h"
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +000021#include "llvm/MC/MCInstrInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/MC/MCSubtargetInfo.h"
Chris Lattner9ec375c2010-11-15 04:16:32 +000023#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/Support/raw_ostream.h"
Chris Lattner9ec375c2010-11-15 04:16:32 +000025using namespace llvm;
26
27STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
28
29namespace {
30class PPCMCCodeEmitter : public MCCodeEmitter {
Craig Toppera60c0f12012-09-15 17:09:36 +000031 PPCMCCodeEmitter(const PPCMCCodeEmitter &) LLVM_DELETED_FUNCTION;
32 void operator=(const PPCMCCodeEmitter &) LLVM_DELETED_FUNCTION;
33
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +000034 const MCSubtargetInfo &STI;
35 Triple TT;
36
Chris Lattner9ec375c2010-11-15 04:16:32 +000037public:
Evan Chengc5e6d2f2011-07-11 03:57:24 +000038 PPCMCCodeEmitter(const MCInstrInfo &mcii, const MCSubtargetInfo &sti,
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +000039 MCContext &ctx)
40 : STI(sti), TT(STI.getTargetTriple()) {
Chris Lattner9ec375c2010-11-15 04:16:32 +000041 }
42
43 ~PPCMCCodeEmitter() {}
Chris Lattnerd6a07cc2010-11-15 05:19:25 +000044
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +000045 bool is64BitMode() const {
46 return (STI.getFeatureBits() & PPC::Feature64Bit) != 0;
47 }
48
49 bool isSVR4ABI() const {
50 return TT.isMacOSX() == 0;
51 }
52
Chris Lattner0e3461e2010-11-15 06:09:35 +000053 unsigned getDirectBrEncoding(const MCInst &MI, unsigned OpNo,
54 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattner0e3461e2010-11-15 06:09:35 +000055 unsigned getCondBrEncoding(const MCInst &MI, unsigned OpNo,
56 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattner65661122010-11-15 06:33:39 +000057 unsigned getHA16Encoding(const MCInst &MI, unsigned OpNo,
58 SmallVectorImpl<MCFixup> &Fixups) const;
59 unsigned getLO16Encoding(const MCInst &MI, unsigned OpNo,
60 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattnerefacb9e2010-11-15 08:22:03 +000061 unsigned getMemRIEncoding(const MCInst &MI, unsigned OpNo,
62 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattner8f4444d2010-11-15 08:02:41 +000063 unsigned getMemRIXEncoding(const MCInst &MI, unsigned OpNo,
64 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Schmidtca4a0c92012-12-04 16:18:08 +000065 unsigned getTLSOffsetEncoding(const MCInst &MI, unsigned OpNo,
66 SmallVectorImpl<MCFixup> &Fixups) const;
67 unsigned getTLSRegEncoding(const MCInst &MI, unsigned OpNo,
68 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattnerd6a07cc2010-11-15 05:19:25 +000069 unsigned get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
70 SmallVectorImpl<MCFixup> &Fixups) const;
71
Chris Lattner9ec375c2010-11-15 04:16:32 +000072 /// getMachineOpValue - Return binary encoding of operand. If the machine
73 /// operand requires relocation, record the relocation and return zero.
74 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
75 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattner9ec375c2010-11-15 04:16:32 +000076
77 // getBinaryCodeForInstr - TableGen'erated function for getting the
78 // binary encoding for an instruction.
Owen Andersond845d9d2012-01-24 18:37:29 +000079 uint64_t getBinaryCodeForInstr(const MCInst &MI,
Chris Lattner9ec375c2010-11-15 04:16:32 +000080 SmallVectorImpl<MCFixup> &Fixups) const;
81 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
82 SmallVectorImpl<MCFixup> &Fixups) const {
Adhemerval Zanella1be10dc2012-10-25 14:29:13 +000083 uint64_t Bits = getBinaryCodeForInstr(MI, Fixups);
84
85 // BL8_NOPELF and BLA8_NOP_ELF is both size of 8 bacause of the
86 // following 'nop'.
87 unsigned Size = 4; // FIXME: Have Desc.getSize() return the correct value!
88 unsigned Opcode = MI.getOpcode();
89 if (Opcode == PPC::BL8_NOP_ELF || Opcode == PPC::BLA8_NOP_ELF)
90 Size = 8;
Chris Lattner9ec375c2010-11-15 04:16:32 +000091
92 // Output the constant in big endian byte order.
Adhemerval Zanella1be10dc2012-10-25 14:29:13 +000093 int ShiftValue = (Size * 8) - 8;
94 for (unsigned i = 0; i != Size; ++i) {
95 OS << (char)(Bits >> ShiftValue);
Chris Lattner9ec375c2010-11-15 04:16:32 +000096 Bits <<= 8;
97 }
98
99 ++MCNumEmitted; // Keep track of the # of mi's emitted.
100 }
101
102};
103
104} // end anonymous namespace
105
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000106MCCodeEmitter *llvm::createPPCMCCodeEmitter(const MCInstrInfo &MCII,
Jim Grosbachc3b04272012-05-15 17:35:52 +0000107 const MCRegisterInfo &MRI,
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000108 const MCSubtargetInfo &STI,
Chris Lattner9ec375c2010-11-15 04:16:32 +0000109 MCContext &Ctx) {
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000110 return new PPCMCCodeEmitter(MCII, STI, Ctx);
Chris Lattner9ec375c2010-11-15 04:16:32 +0000111}
112
113unsigned PPCMCCodeEmitter::
Chris Lattner0e3461e2010-11-15 06:09:35 +0000114getDirectBrEncoding(const MCInst &MI, unsigned OpNo,
115 SmallVectorImpl<MCFixup> &Fixups) const {
Chris Lattner79fa3712010-11-15 05:57:53 +0000116 const MCOperand &MO = MI.getOperand(OpNo);
117 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
118
119 // Add a fixup for the branch target.
120 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
121 (MCFixupKind)PPC::fixup_ppc_br24));
122 return 0;
123}
124
Chris Lattner0e3461e2010-11-15 06:09:35 +0000125unsigned PPCMCCodeEmitter::getCondBrEncoding(const MCInst &MI, unsigned OpNo,
126 SmallVectorImpl<MCFixup> &Fixups) const {
127 const MCOperand &MO = MI.getOperand(OpNo);
128 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
129
Chris Lattner85e37682010-11-15 06:12:22 +0000130 // Add a fixup for the branch target.
131 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
132 (MCFixupKind)PPC::fixup_ppc_brcond14));
Chris Lattner0e3461e2010-11-15 06:09:35 +0000133 return 0;
134}
135
Chris Lattner65661122010-11-15 06:33:39 +0000136unsigned PPCMCCodeEmitter::getHA16Encoding(const MCInst &MI, unsigned OpNo,
137 SmallVectorImpl<MCFixup> &Fixups) const {
138 const MCOperand &MO = MI.getOperand(OpNo);
139 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
140
141 // Add a fixup for the branch target.
142 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
143 (MCFixupKind)PPC::fixup_ppc_ha16));
144 return 0;
145}
146
147unsigned PPCMCCodeEmitter::getLO16Encoding(const MCInst &MI, unsigned OpNo,
148 SmallVectorImpl<MCFixup> &Fixups) const {
149 const MCOperand &MO = MI.getOperand(OpNo);
150 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
151
152 // Add a fixup for the branch target.
153 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
154 (MCFixupKind)PPC::fixup_ppc_lo16));
155 return 0;
156}
157
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000158unsigned PPCMCCodeEmitter::getMemRIEncoding(const MCInst &MI, unsigned OpNo,
159 SmallVectorImpl<MCFixup> &Fixups) const {
160 // Encode (imm, reg) as a memri, which has the low 16-bits as the
161 // displacement and the next 5 bits as the register #.
162 assert(MI.getOperand(OpNo+1).isReg());
163 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups) << 16;
164
165 const MCOperand &MO = MI.getOperand(OpNo);
166 if (MO.isImm())
167 return (getMachineOpValue(MI, MO, Fixups) & 0xFFFF) | RegBits;
168
169 // Add a fixup for the displacement field.
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +0000170 if (isSVR4ABI() && is64BitMode())
171 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
172 (MCFixupKind)PPC::fixup_ppc_toc16));
173 else
174 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
175 (MCFixupKind)PPC::fixup_ppc_lo16));
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000176 return RegBits;
177}
178
179
Chris Lattner8f4444d2010-11-15 08:02:41 +0000180unsigned PPCMCCodeEmitter::getMemRIXEncoding(const MCInst &MI, unsigned OpNo,
Chris Lattner65661122010-11-15 06:33:39 +0000181 SmallVectorImpl<MCFixup> &Fixups) const {
Chris Lattner8f4444d2010-11-15 08:02:41 +0000182 // Encode (imm, reg) as a memrix, which has the low 14-bits as the
183 // displacement and the next 5 bits as the register #.
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000184 assert(MI.getOperand(OpNo+1).isReg());
Chris Lattner8f4444d2010-11-15 08:02:41 +0000185 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups) << 14;
186
Chris Lattner65661122010-11-15 06:33:39 +0000187 const MCOperand &MO = MI.getOperand(OpNo);
Chris Lattner8f4444d2010-11-15 08:02:41 +0000188 if (MO.isImm())
189 return (getMachineOpValue(MI, MO, Fixups) & 0x3FFF) | RegBits;
Chris Lattner65661122010-11-15 06:33:39 +0000190
191 // Add a fixup for the branch target.
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +0000192 if (isSVR4ABI() && is64BitMode())
193 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
194 (MCFixupKind)PPC::fixup_ppc_toc16_ds));
195 else
196 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
197 (MCFixupKind)PPC::fixup_ppc_lo14));
Chris Lattner8f4444d2010-11-15 08:02:41 +0000198 return RegBits;
Chris Lattner65661122010-11-15 06:33:39 +0000199}
200
Chris Lattner0e3461e2010-11-15 06:09:35 +0000201
Bill Schmidtca4a0c92012-12-04 16:18:08 +0000202unsigned PPCMCCodeEmitter::getTLSOffsetEncoding(const MCInst &MI, unsigned OpNo,
203 SmallVectorImpl<MCFixup> &Fixups) const {
204 const MCOperand &MO = MI.getOperand(OpNo);
205
206 // Add a fixup for the GOT displacement to the TLS block offset.
207 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
208 (MCFixupKind)PPC::fixup_ppc_toc16_ds));
209 return 0;
210}
211
212
213unsigned PPCMCCodeEmitter::getTLSRegEncoding(const MCInst &MI, unsigned OpNo,
214 SmallVectorImpl<MCFixup> &Fixups) const {
215 const MCOperand &MO = MI.getOperand(OpNo);
216 if (MO.isReg()) return getMachineOpValue(MI, MO, Fixups);
217
218 // Add a fixup for the TLS register, which simply provides a relocation
219 // hint to the linker that this statement is part of a relocation sequence.
220 // Return the thread-pointer register's encoding.
221 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
222 (MCFixupKind)PPC::fixup_ppc_tlsreg));
223 return getPPCRegisterNumbering(PPC::X13);
224}
225
226
Chris Lattner79fa3712010-11-15 05:57:53 +0000227unsigned PPCMCCodeEmitter::
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000228get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
229 SmallVectorImpl<MCFixup> &Fixups) const {
230 const MCOperand &MO = MI.getOperand(OpNo);
Adhemerval Zanella22b9fd22012-10-08 18:25:11 +0000231 assert((MI.getOpcode() == PPC::MTCRF ||
232 MI.getOpcode() == PPC::MFOCRF ||
233 MI.getOpcode() == PPC::MTCRF8) &&
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000234 (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7));
Evan Cheng61d4a202011-07-25 19:53:23 +0000235 return 0x80 >> getPPCRegisterNumbering(MO.getReg());
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000236}
237
238
239unsigned PPCMCCodeEmitter::
Chris Lattner9ec375c2010-11-15 04:16:32 +0000240getMachineOpValue(const MCInst &MI, const MCOperand &MO,
241 SmallVectorImpl<MCFixup> &Fixups) const {
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000242 if (MO.isReg()) {
Chris Lattner7b25d6f2010-11-16 00:57:32 +0000243 // MTCRF/MFOCRF should go through get_crbitm_encoding for the CR operand.
244 // The GPR operand should come through here though.
Chris Lattner73716a62010-11-16 00:55:51 +0000245 assert((MI.getOpcode() != PPC::MTCRF && MI.getOpcode() != PPC::MFOCRF) ||
246 MO.getReg() < PPC::CR0 || MO.getReg() > PPC::CR7);
Evan Cheng61d4a202011-07-25 19:53:23 +0000247 return getPPCRegisterNumbering(MO.getReg());
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000248 }
Chris Lattnerc877d8f2010-11-15 04:51:55 +0000249
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000250 assert(MO.isImm() &&
251 "Relocation required in an instruction that we cannot encode!");
252 return MO.getImm();
Chris Lattner9ec375c2010-11-15 04:16:32 +0000253}
254
255
256#include "PPCGenMCCodeEmitter.inc"