blob: 244c534d9c9af5c64003836286a8838209a877c8 [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/PPCFixupKinds.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/ADT/Statistic.h"
Chris Lattner9ec375c2010-11-15 04:16:32 +000018#include "llvm/MC/MCCodeEmitter.h"
Hal Finkelfeea6532013-03-26 20:08:20 +000019#include "llvm/MC/MCContext.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;
Hal Finkelfeea6532013-03-26 20:08:20 +000036 const MCContext &CTX;
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +000037 Triple TT;
38
Chris Lattner9ec375c2010-11-15 04:16:32 +000039public:
Evan Chengc5e6d2f2011-07-11 03:57:24 +000040 PPCMCCodeEmitter(const MCInstrInfo &mcii, const MCSubtargetInfo &sti,
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +000041 MCContext &ctx)
Hal Finkelfeea6532013-03-26 20:08:20 +000042 : STI(sti), CTX(ctx), TT(STI.getTargetTriple()) {
Chris Lattner9ec375c2010-11-15 04:16:32 +000043 }
44
45 ~PPCMCCodeEmitter() {}
Chris Lattnerd6a07cc2010-11-15 05:19:25 +000046
Chris Lattner0e3461e2010-11-15 06:09:35 +000047 unsigned getDirectBrEncoding(const MCInst &MI, unsigned OpNo,
48 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattner0e3461e2010-11-15 06:09:35 +000049 unsigned getCondBrEncoding(const MCInst &MI, unsigned OpNo,
50 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattner65661122010-11-15 06:33:39 +000051 unsigned getHA16Encoding(const MCInst &MI, unsigned OpNo,
52 SmallVectorImpl<MCFixup> &Fixups) const;
53 unsigned getLO16Encoding(const MCInst &MI, unsigned OpNo,
54 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattnerefacb9e2010-11-15 08:22:03 +000055 unsigned getMemRIEncoding(const MCInst &MI, unsigned OpNo,
56 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattner8f4444d2010-11-15 08:02:41 +000057 unsigned getMemRIXEncoding(const MCInst &MI, unsigned OpNo,
58 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Schmidtca4a0c92012-12-04 16:18:08 +000059 unsigned getTLSRegEncoding(const MCInst &MI, unsigned OpNo,
60 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattnerd6a07cc2010-11-15 05:19:25 +000061 unsigned get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
62 SmallVectorImpl<MCFixup> &Fixups) const;
63
Chris Lattner9ec375c2010-11-15 04:16:32 +000064 /// getMachineOpValue - Return binary encoding of operand. If the machine
65 /// operand requires relocation, record the relocation and return zero.
66 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
67 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattner9ec375c2010-11-15 04:16:32 +000068
69 // getBinaryCodeForInstr - TableGen'erated function for getting the
70 // binary encoding for an instruction.
Owen Andersond845d9d2012-01-24 18:37:29 +000071 uint64_t getBinaryCodeForInstr(const MCInst &MI,
Chris Lattner9ec375c2010-11-15 04:16:32 +000072 SmallVectorImpl<MCFixup> &Fixups) const;
73 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
74 SmallVectorImpl<MCFixup> &Fixups) const {
Adhemerval Zanella1be10dc2012-10-25 14:29:13 +000075 uint64_t Bits = getBinaryCodeForInstr(MI, Fixups);
76
Ulrich Weigandf62e83f2013-03-22 15:24:13 +000077 // BL8_NOP etc. all have a size of 8 because of the following 'nop'.
Adhemerval Zanella1be10dc2012-10-25 14:29:13 +000078 unsigned Size = 4; // FIXME: Have Desc.getSize() return the correct value!
79 unsigned Opcode = MI.getOpcode();
Ulrich Weigandf62e83f2013-03-22 15:24:13 +000080 if (Opcode == PPC::BL8_NOP || Opcode == PPC::BLA8_NOP ||
81 Opcode == PPC::BL8_NOP_TLSGD || Opcode == PPC::BL8_NOP_TLSLD)
Adhemerval Zanella1be10dc2012-10-25 14:29:13 +000082 Size = 8;
Chris Lattner9ec375c2010-11-15 04:16:32 +000083
84 // Output the constant in big endian byte order.
Adhemerval Zanella1be10dc2012-10-25 14:29:13 +000085 int ShiftValue = (Size * 8) - 8;
86 for (unsigned i = 0; i != Size; ++i) {
87 OS << (char)(Bits >> ShiftValue);
Chris Lattner9ec375c2010-11-15 04:16:32 +000088 Bits <<= 8;
89 }
90
91 ++MCNumEmitted; // Keep track of the # of mi's emitted.
92 }
93
94};
95
96} // end anonymous namespace
97
Evan Chengc5e6d2f2011-07-11 03:57:24 +000098MCCodeEmitter *llvm::createPPCMCCodeEmitter(const MCInstrInfo &MCII,
Jim Grosbachc3b04272012-05-15 17:35:52 +000099 const MCRegisterInfo &MRI,
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000100 const MCSubtargetInfo &STI,
Chris Lattner9ec375c2010-11-15 04:16:32 +0000101 MCContext &Ctx) {
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000102 return new PPCMCCodeEmitter(MCII, STI, Ctx);
Chris Lattner9ec375c2010-11-15 04:16:32 +0000103}
104
105unsigned PPCMCCodeEmitter::
Chris Lattner0e3461e2010-11-15 06:09:35 +0000106getDirectBrEncoding(const MCInst &MI, unsigned OpNo,
107 SmallVectorImpl<MCFixup> &Fixups) const {
Chris Lattner79fa3712010-11-15 05:57:53 +0000108 const MCOperand &MO = MI.getOperand(OpNo);
109 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
110
111 // Add a fixup for the branch target.
112 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
113 (MCFixupKind)PPC::fixup_ppc_br24));
Bill Schmidtc56f1d32012-12-11 20:30:11 +0000114
115 // For special TLS calls, add another fixup for the symbol. Apparently
Ulrich Weigandf62e83f2013-03-22 15:24:13 +0000116 // BL8_NOP, BL8_NOP_TLSGD, and BL8_NOP_TLSLD are sufficiently
Bill Schmidt24b8dd62012-12-12 19:29:35 +0000117 // similar that TblGen will not generate a separate case for the latter
118 // two, so this is the only way to get the extra fixup generated.
119 unsigned Opcode = MI.getOpcode();
Ulrich Weigandf62e83f2013-03-22 15:24:13 +0000120 if (Opcode == PPC::BL8_NOP_TLSGD || Opcode == PPC::BL8_NOP_TLSLD) {
Bill Schmidtc56f1d32012-12-11 20:30:11 +0000121 const MCOperand &MO2 = MI.getOperand(OpNo+1);
122 Fixups.push_back(MCFixup::Create(0, MO2.getExpr(),
Bill Schmidt24b8dd62012-12-12 19:29:35 +0000123 (MCFixupKind)PPC::fixup_ppc_nofixup));
Bill Schmidtc56f1d32012-12-11 20:30:11 +0000124 }
Chris Lattner79fa3712010-11-15 05:57:53 +0000125 return 0;
126}
127
Chris Lattner0e3461e2010-11-15 06:09:35 +0000128unsigned PPCMCCodeEmitter::getCondBrEncoding(const MCInst &MI, unsigned OpNo,
129 SmallVectorImpl<MCFixup> &Fixups) const {
130 const MCOperand &MO = MI.getOperand(OpNo);
131 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
132
Chris Lattner85e37682010-11-15 06:12:22 +0000133 // Add a fixup for the branch target.
134 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
135 (MCFixupKind)PPC::fixup_ppc_brcond14));
Chris Lattner0e3461e2010-11-15 06:09:35 +0000136 return 0;
137}
138
Chris Lattner65661122010-11-15 06:33:39 +0000139unsigned PPCMCCodeEmitter::getHA16Encoding(const MCInst &MI, unsigned OpNo,
140 SmallVectorImpl<MCFixup> &Fixups) const {
141 const MCOperand &MO = MI.getOperand(OpNo);
142 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
143
144 // Add a fixup for the branch target.
Ulrich Weigand2fb140e2013-05-15 15:07:06 +0000145 Fixups.push_back(MCFixup::Create(2, MO.getExpr(),
Chris Lattner65661122010-11-15 06:33:39 +0000146 (MCFixupKind)PPC::fixup_ppc_ha16));
147 return 0;
148}
149
150unsigned PPCMCCodeEmitter::getLO16Encoding(const MCInst &MI, unsigned OpNo,
151 SmallVectorImpl<MCFixup> &Fixups) const {
152 const MCOperand &MO = MI.getOperand(OpNo);
153 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
154
155 // Add a fixup for the branch target.
Ulrich Weigand2fb140e2013-05-15 15:07:06 +0000156 Fixups.push_back(MCFixup::Create(2, MO.getExpr(),
Chris Lattner65661122010-11-15 06:33:39 +0000157 (MCFixupKind)PPC::fixup_ppc_lo16));
158 return 0;
159}
160
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000161unsigned PPCMCCodeEmitter::getMemRIEncoding(const MCInst &MI, unsigned OpNo,
162 SmallVectorImpl<MCFixup> &Fixups) const {
163 // Encode (imm, reg) as a memri, which has the low 16-bits as the
164 // displacement and the next 5 bits as the register #.
165 assert(MI.getOperand(OpNo+1).isReg());
166 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups) << 16;
167
168 const MCOperand &MO = MI.getOperand(OpNo);
169 if (MO.isImm())
170 return (getMachineOpValue(MI, MO, Fixups) & 0xFFFF) | RegBits;
171
172 // Add a fixup for the displacement field.
Ulrich Weigand2fb140e2013-05-15 15:07:06 +0000173 Fixups.push_back(MCFixup::Create(2, MO.getExpr(),
Ulrich Weigand3e186012013-03-26 10:56:47 +0000174 (MCFixupKind)PPC::fixup_ppc_lo16));
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000175 return RegBits;
176}
177
178
Chris Lattner8f4444d2010-11-15 08:02:41 +0000179unsigned PPCMCCodeEmitter::getMemRIXEncoding(const MCInst &MI, unsigned OpNo,
Chris Lattner65661122010-11-15 06:33:39 +0000180 SmallVectorImpl<MCFixup> &Fixups) const {
Chris Lattner8f4444d2010-11-15 08:02:41 +0000181 // Encode (imm, reg) as a memrix, which has the low 14-bits as the
182 // displacement and the next 5 bits as the register #.
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000183 assert(MI.getOperand(OpNo+1).isReg());
Chris Lattner8f4444d2010-11-15 08:02:41 +0000184 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups) << 14;
185
Chris Lattner65661122010-11-15 06:33:39 +0000186 const MCOperand &MO = MI.getOperand(OpNo);
Chris Lattner8f4444d2010-11-15 08:02:41 +0000187 if (MO.isImm())
Ulrich Weigand9d980cb2013-05-16 17:58:02 +0000188 return ((getMachineOpValue(MI, MO, Fixups) >> 2) & 0x3FFF) | RegBits;
Chris Lattner65661122010-11-15 06:33:39 +0000189
Ulrich Weigand3e186012013-03-26 10:56:47 +0000190 // Add a fixup for the displacement field.
Ulrich Weigand2fb140e2013-05-15 15:07:06 +0000191 Fixups.push_back(MCFixup::Create(2, MO.getExpr(),
Ulrich Weigand3e186012013-03-26 10:56:47 +0000192 (MCFixupKind)PPC::fixup_ppc_lo16_ds));
Chris Lattner8f4444d2010-11-15 08:02:41 +0000193 return RegBits;
Chris Lattner65661122010-11-15 06:33:39 +0000194}
195
Chris Lattner0e3461e2010-11-15 06:09:35 +0000196
Bill Schmidtca4a0c92012-12-04 16:18:08 +0000197unsigned PPCMCCodeEmitter::getTLSRegEncoding(const MCInst &MI, unsigned OpNo,
198 SmallVectorImpl<MCFixup> &Fixups) const {
199 const MCOperand &MO = MI.getOperand(OpNo);
200 if (MO.isReg()) return getMachineOpValue(MI, MO, Fixups);
201
202 // Add a fixup for the TLS register, which simply provides a relocation
203 // hint to the linker that this statement is part of a relocation sequence.
204 // Return the thread-pointer register's encoding.
205 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
206 (MCFixupKind)PPC::fixup_ppc_tlsreg));
Hal Finkelfeea6532013-03-26 20:08:20 +0000207 return CTX.getRegisterInfo().getEncodingValue(PPC::X13);
Bill Schmidtca4a0c92012-12-04 16:18:08 +0000208}
209
Chris Lattner79fa3712010-11-15 05:57:53 +0000210unsigned PPCMCCodeEmitter::
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000211get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
212 SmallVectorImpl<MCFixup> &Fixups) const {
213 const MCOperand &MO = MI.getOperand(OpNo);
Adhemerval Zanella22b9fd22012-10-08 18:25:11 +0000214 assert((MI.getOpcode() == PPC::MTCRF ||
215 MI.getOpcode() == PPC::MFOCRF ||
216 MI.getOpcode() == PPC::MTCRF8) &&
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000217 (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7));
Hal Finkelfeea6532013-03-26 20:08:20 +0000218 return 0x80 >> CTX.getRegisterInfo().getEncodingValue(MO.getReg());
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000219}
220
221
222unsigned PPCMCCodeEmitter::
Chris Lattner9ec375c2010-11-15 04:16:32 +0000223getMachineOpValue(const MCInst &MI, const MCOperand &MO,
224 SmallVectorImpl<MCFixup> &Fixups) const {
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000225 if (MO.isReg()) {
Chris Lattner7b25d6f2010-11-16 00:57:32 +0000226 // MTCRF/MFOCRF should go through get_crbitm_encoding for the CR operand.
227 // The GPR operand should come through here though.
Chris Lattner73716a62010-11-16 00:55:51 +0000228 assert((MI.getOpcode() != PPC::MTCRF && MI.getOpcode() != PPC::MFOCRF) ||
229 MO.getReg() < PPC::CR0 || MO.getReg() > PPC::CR7);
Hal Finkelfeea6532013-03-26 20:08:20 +0000230 return CTX.getRegisterInfo().getEncodingValue(MO.getReg());
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000231 }
Chris Lattnerc877d8f2010-11-15 04:51:55 +0000232
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000233 assert(MO.isImm() &&
234 "Relocation required in an instruction that we cannot encode!");
235 return MO.getImm();
Chris Lattner9ec375c2010-11-15 04:16:32 +0000236}
237
238
239#include "PPCGenMCCodeEmitter.inc"