blob: d048426d43a45ec79c290022c97a0170ec68d7e3 [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"
Bill Schmidtc56f1d32012-12-11 20:30:11 +000020#include "llvm/MC/MCExpr.h"
Chris Lattner9ec375c2010-11-15 04:16:32 +000021#include "llvm/MC/MCInst.h"
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +000022#include "llvm/MC/MCInstrInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/MC/MCSubtargetInfo.h"
Chris Lattner9ec375c2010-11-15 04:16:32 +000024#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/Support/raw_ostream.h"
Chris Lattner9ec375c2010-11-15 04:16:32 +000026using namespace llvm;
27
28STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
29
30namespace {
31class PPCMCCodeEmitter : public MCCodeEmitter {
Craig Toppera60c0f12012-09-15 17:09:36 +000032 PPCMCCodeEmitter(const PPCMCCodeEmitter &) LLVM_DELETED_FUNCTION;
33 void operator=(const PPCMCCodeEmitter &) LLVM_DELETED_FUNCTION;
34
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +000035 const MCSubtargetInfo &STI;
36 Triple TT;
37
Chris Lattner9ec375c2010-11-15 04:16:32 +000038public:
Evan Chengc5e6d2f2011-07-11 03:57:24 +000039 PPCMCCodeEmitter(const MCInstrInfo &mcii, const MCSubtargetInfo &sti,
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +000040 MCContext &ctx)
41 : STI(sti), TT(STI.getTargetTriple()) {
Chris Lattner9ec375c2010-11-15 04:16:32 +000042 }
43
44 ~PPCMCCodeEmitter() {}
Chris Lattnerd6a07cc2010-11-15 05:19:25 +000045
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +000046 bool is64BitMode() const {
47 return (STI.getFeatureBits() & PPC::Feature64Bit) != 0;
48 }
49
50 bool isSVR4ABI() const {
51 return TT.isMacOSX() == 0;
52 }
53
Chris Lattner0e3461e2010-11-15 06:09:35 +000054 unsigned getDirectBrEncoding(const MCInst &MI, unsigned OpNo,
55 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattner0e3461e2010-11-15 06:09:35 +000056 unsigned getCondBrEncoding(const MCInst &MI, unsigned OpNo,
57 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattner65661122010-11-15 06:33:39 +000058 unsigned getHA16Encoding(const MCInst &MI, unsigned OpNo,
59 SmallVectorImpl<MCFixup> &Fixups) const;
60 unsigned getLO16Encoding(const MCInst &MI, unsigned OpNo,
61 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattnerefacb9e2010-11-15 08:22:03 +000062 unsigned getMemRIEncoding(const MCInst &MI, unsigned OpNo,
63 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattner8f4444d2010-11-15 08:02:41 +000064 unsigned getMemRIXEncoding(const MCInst &MI, unsigned OpNo,
65 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Schmidtca4a0c92012-12-04 16:18:08 +000066 unsigned getTLSRegEncoding(const MCInst &MI, unsigned OpNo,
67 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattnerd6a07cc2010-11-15 05:19:25 +000068 unsigned get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
69 SmallVectorImpl<MCFixup> &Fixups) const;
70
Chris Lattner9ec375c2010-11-15 04:16:32 +000071 /// getMachineOpValue - Return binary encoding of operand. If the machine
72 /// operand requires relocation, record the relocation and return zero.
73 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
74 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattner9ec375c2010-11-15 04:16:32 +000075
76 // getBinaryCodeForInstr - TableGen'erated function for getting the
77 // binary encoding for an instruction.
Owen Andersond845d9d2012-01-24 18:37:29 +000078 uint64_t getBinaryCodeForInstr(const MCInst &MI,
Chris Lattner9ec375c2010-11-15 04:16:32 +000079 SmallVectorImpl<MCFixup> &Fixups) const;
80 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
81 SmallVectorImpl<MCFixup> &Fixups) const {
Adhemerval Zanella1be10dc2012-10-25 14:29:13 +000082 uint64_t Bits = getBinaryCodeForInstr(MI, Fixups);
83
Bill Schmidt24b8dd62012-12-12 19:29:35 +000084 // BL8_NOP_ELF, BLA8_NOP_ELF, etc., all have a size of 8 because of the
Adhemerval Zanella1be10dc2012-10-25 14:29:13 +000085 // following 'nop'.
86 unsigned Size = 4; // FIXME: Have Desc.getSize() return the correct value!
87 unsigned Opcode = MI.getOpcode();
Bill Schmidt24b8dd62012-12-12 19:29:35 +000088 if (Opcode == PPC::BL8_NOP_ELF || Opcode == PPC::BLA8_NOP_ELF ||
89 Opcode == PPC::BL8_NOP_ELF_TLSGD || Opcode == PPC::BL8_NOP_ELF_TLSLD)
Adhemerval Zanella1be10dc2012-10-25 14:29:13 +000090 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));
Bill Schmidtc56f1d32012-12-11 20:30:11 +0000122
123 // For special TLS calls, add another fixup for the symbol. Apparently
Bill Schmidt24b8dd62012-12-12 19:29:35 +0000124 // BL8_NOP_ELF, BL8_NOP_ELF_TLSGD, and BL8_NOP_ELF_TLSLD are sufficiently
125 // similar that TblGen will not generate a separate case for the latter
126 // two, so this is the only way to get the extra fixup generated.
127 unsigned Opcode = MI.getOpcode();
128 if (Opcode == PPC::BL8_NOP_ELF_TLSGD || Opcode == PPC::BL8_NOP_ELF_TLSLD) {
Bill Schmidtc56f1d32012-12-11 20:30:11 +0000129 const MCOperand &MO2 = MI.getOperand(OpNo+1);
130 Fixups.push_back(MCFixup::Create(0, MO2.getExpr(),
Bill Schmidt24b8dd62012-12-12 19:29:35 +0000131 (MCFixupKind)PPC::fixup_ppc_nofixup));
Bill Schmidtc56f1d32012-12-11 20:30:11 +0000132 }
Chris Lattner79fa3712010-11-15 05:57:53 +0000133 return 0;
134}
135
Chris Lattner0e3461e2010-11-15 06:09:35 +0000136unsigned PPCMCCodeEmitter::getCondBrEncoding(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
Chris Lattner85e37682010-11-15 06:12:22 +0000141 // Add a fixup for the branch target.
142 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
143 (MCFixupKind)PPC::fixup_ppc_brcond14));
Chris Lattner0e3461e2010-11-15 06:09:35 +0000144 return 0;
145}
146
Chris Lattner65661122010-11-15 06:33:39 +0000147unsigned PPCMCCodeEmitter::getHA16Encoding(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_ha16));
155 return 0;
156}
157
158unsigned PPCMCCodeEmitter::getLO16Encoding(const MCInst &MI, unsigned OpNo,
159 SmallVectorImpl<MCFixup> &Fixups) const {
160 const MCOperand &MO = MI.getOperand(OpNo);
161 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
162
163 // Add a fixup for the branch target.
164 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
165 (MCFixupKind)PPC::fixup_ppc_lo16));
166 return 0;
167}
168
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000169unsigned PPCMCCodeEmitter::getMemRIEncoding(const MCInst &MI, unsigned OpNo,
170 SmallVectorImpl<MCFixup> &Fixups) const {
171 // Encode (imm, reg) as a memri, which has the low 16-bits as the
172 // displacement and the next 5 bits as the register #.
173 assert(MI.getOperand(OpNo+1).isReg());
174 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups) << 16;
175
176 const MCOperand &MO = MI.getOperand(OpNo);
177 if (MO.isImm())
178 return (getMachineOpValue(MI, MO, Fixups) & 0xFFFF) | RegBits;
179
180 // Add a fixup for the displacement field.
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +0000181 if (isSVR4ABI() && is64BitMode())
182 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
183 (MCFixupKind)PPC::fixup_ppc_toc16));
184 else
185 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
186 (MCFixupKind)PPC::fixup_ppc_lo16));
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000187 return RegBits;
188}
189
190
Chris Lattner8f4444d2010-11-15 08:02:41 +0000191unsigned PPCMCCodeEmitter::getMemRIXEncoding(const MCInst &MI, unsigned OpNo,
Chris Lattner65661122010-11-15 06:33:39 +0000192 SmallVectorImpl<MCFixup> &Fixups) const {
Chris Lattner8f4444d2010-11-15 08:02:41 +0000193 // Encode (imm, reg) as a memrix, which has the low 14-bits as the
194 // displacement and the next 5 bits as the register #.
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000195 assert(MI.getOperand(OpNo+1).isReg());
Chris Lattner8f4444d2010-11-15 08:02:41 +0000196 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups) << 14;
197
Chris Lattner65661122010-11-15 06:33:39 +0000198 const MCOperand &MO = MI.getOperand(OpNo);
Chris Lattner8f4444d2010-11-15 08:02:41 +0000199 if (MO.isImm())
200 return (getMachineOpValue(MI, MO, Fixups) & 0x3FFF) | RegBits;
Chris Lattner65661122010-11-15 06:33:39 +0000201
202 // Add a fixup for the branch target.
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +0000203 if (isSVR4ABI() && is64BitMode())
204 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
205 (MCFixupKind)PPC::fixup_ppc_toc16_ds));
206 else
207 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
208 (MCFixupKind)PPC::fixup_ppc_lo14));
Chris Lattner8f4444d2010-11-15 08:02:41 +0000209 return RegBits;
Chris Lattner65661122010-11-15 06:33:39 +0000210}
211
Chris Lattner0e3461e2010-11-15 06:09:35 +0000212
Bill Schmidtca4a0c92012-12-04 16:18:08 +0000213unsigned 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
Chris Lattner79fa3712010-11-15 05:57:53 +0000226unsigned PPCMCCodeEmitter::
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000227get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
228 SmallVectorImpl<MCFixup> &Fixups) const {
229 const MCOperand &MO = MI.getOperand(OpNo);
Adhemerval Zanella22b9fd22012-10-08 18:25:11 +0000230 assert((MI.getOpcode() == PPC::MTCRF ||
231 MI.getOpcode() == PPC::MFOCRF ||
232 MI.getOpcode() == PPC::MTCRF8) &&
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000233 (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7));
Evan Cheng61d4a202011-07-25 19:53:23 +0000234 return 0x80 >> getPPCRegisterNumbering(MO.getReg());
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000235}
236
237
238unsigned PPCMCCodeEmitter::
Chris Lattner9ec375c2010-11-15 04:16:32 +0000239getMachineOpValue(const MCInst &MI, const MCOperand &MO,
240 SmallVectorImpl<MCFixup> &Fixups) const {
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000241 if (MO.isReg()) {
Chris Lattner7b25d6f2010-11-16 00:57:32 +0000242 // MTCRF/MFOCRF should go through get_crbitm_encoding for the CR operand.
243 // The GPR operand should come through here though.
Chris Lattner73716a62010-11-16 00:55:51 +0000244 assert((MI.getOpcode() != PPC::MTCRF && MI.getOpcode() != PPC::MFOCRF) ||
245 MO.getReg() < PPC::CR0 || MO.getReg() > PPC::CR7);
Evan Cheng61d4a202011-07-25 19:53:23 +0000246 return getPPCRegisterNumbering(MO.getReg());
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000247 }
Chris Lattnerc877d8f2010-11-15 04:51:55 +0000248
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000249 assert(MO.isImm() &&
250 "Relocation required in an instruction that we cannot encode!");
251 return MO.getImm();
Chris Lattner9ec375c2010-11-15 04:16:32 +0000252}
253
254
255#include "PPCGenMCCodeEmitter.inc"