blob: 31c73aeff5658cf398eb47dd8720d30faccee586 [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;
Ulrich Weigand2dbe06a2013-05-17 14:14:12 +000051 unsigned getS16ImmEncoding(const MCInst &MI, unsigned OpNo,
52 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattnerefacb9e2010-11-15 08:22:03 +000053 unsigned getMemRIEncoding(const MCInst &MI, unsigned OpNo,
54 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattner8f4444d2010-11-15 08:02:41 +000055 unsigned getMemRIXEncoding(const MCInst &MI, unsigned OpNo,
56 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Schmidtca4a0c92012-12-04 16:18:08 +000057 unsigned getTLSRegEncoding(const MCInst &MI, unsigned OpNo,
58 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattnerd6a07cc2010-11-15 05:19:25 +000059 unsigned get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
60 SmallVectorImpl<MCFixup> &Fixups) const;
61
Chris Lattner9ec375c2010-11-15 04:16:32 +000062 /// getMachineOpValue - Return binary encoding of operand. If the machine
63 /// operand requires relocation, record the relocation and return zero.
64 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
65 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattner9ec375c2010-11-15 04:16:32 +000066
67 // getBinaryCodeForInstr - TableGen'erated function for getting the
68 // binary encoding for an instruction.
Owen Andersond845d9d2012-01-24 18:37:29 +000069 uint64_t getBinaryCodeForInstr(const MCInst &MI,
Chris Lattner9ec375c2010-11-15 04:16:32 +000070 SmallVectorImpl<MCFixup> &Fixups) const;
71 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
72 SmallVectorImpl<MCFixup> &Fixups) const {
Adhemerval Zanella1be10dc2012-10-25 14:29:13 +000073 uint64_t Bits = getBinaryCodeForInstr(MI, Fixups);
74
Ulrich Weigandf62e83f2013-03-22 15:24:13 +000075 // BL8_NOP etc. all have a size of 8 because of the following 'nop'.
Adhemerval Zanella1be10dc2012-10-25 14:29:13 +000076 unsigned Size = 4; // FIXME: Have Desc.getSize() return the correct value!
77 unsigned Opcode = MI.getOpcode();
Ulrich Weigandf62e83f2013-03-22 15:24:13 +000078 if (Opcode == PPC::BL8_NOP || Opcode == PPC::BLA8_NOP ||
79 Opcode == PPC::BL8_NOP_TLSGD || Opcode == PPC::BL8_NOP_TLSLD)
Adhemerval Zanella1be10dc2012-10-25 14:29:13 +000080 Size = 8;
Chris Lattner9ec375c2010-11-15 04:16:32 +000081
82 // Output the constant in big endian byte order.
Adhemerval Zanella1be10dc2012-10-25 14:29:13 +000083 int ShiftValue = (Size * 8) - 8;
84 for (unsigned i = 0; i != Size; ++i) {
85 OS << (char)(Bits >> ShiftValue);
Chris Lattner9ec375c2010-11-15 04:16:32 +000086 Bits <<= 8;
87 }
88
89 ++MCNumEmitted; // Keep track of the # of mi's emitted.
90 }
91
92};
93
94} // end anonymous namespace
95
Evan Chengc5e6d2f2011-07-11 03:57:24 +000096MCCodeEmitter *llvm::createPPCMCCodeEmitter(const MCInstrInfo &MCII,
Jim Grosbachc3b04272012-05-15 17:35:52 +000097 const MCRegisterInfo &MRI,
Evan Chengc5e6d2f2011-07-11 03:57:24 +000098 const MCSubtargetInfo &STI,
Chris Lattner9ec375c2010-11-15 04:16:32 +000099 MCContext &Ctx) {
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000100 return new PPCMCCodeEmitter(MCII, STI, Ctx);
Chris Lattner9ec375c2010-11-15 04:16:32 +0000101}
102
103unsigned PPCMCCodeEmitter::
Chris Lattner0e3461e2010-11-15 06:09:35 +0000104getDirectBrEncoding(const MCInst &MI, unsigned OpNo,
105 SmallVectorImpl<MCFixup> &Fixups) const {
Chris Lattner79fa3712010-11-15 05:57:53 +0000106 const MCOperand &MO = MI.getOperand(OpNo);
107 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
108
109 // Add a fixup for the branch target.
110 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
111 (MCFixupKind)PPC::fixup_ppc_br24));
Bill Schmidtc56f1d32012-12-11 20:30:11 +0000112
113 // For special TLS calls, add another fixup for the symbol. Apparently
Ulrich Weigandf62e83f2013-03-22 15:24:13 +0000114 // BL8_NOP, BL8_NOP_TLSGD, and BL8_NOP_TLSLD are sufficiently
Bill Schmidt24b8dd62012-12-12 19:29:35 +0000115 // similar that TblGen will not generate a separate case for the latter
116 // two, so this is the only way to get the extra fixup generated.
117 unsigned Opcode = MI.getOpcode();
Ulrich Weigandf62e83f2013-03-22 15:24:13 +0000118 if (Opcode == PPC::BL8_NOP_TLSGD || Opcode == PPC::BL8_NOP_TLSLD) {
Bill Schmidtc56f1d32012-12-11 20:30:11 +0000119 const MCOperand &MO2 = MI.getOperand(OpNo+1);
120 Fixups.push_back(MCFixup::Create(0, MO2.getExpr(),
Bill Schmidt24b8dd62012-12-12 19:29:35 +0000121 (MCFixupKind)PPC::fixup_ppc_nofixup));
Bill Schmidtc56f1d32012-12-11 20:30:11 +0000122 }
Chris Lattner79fa3712010-11-15 05:57:53 +0000123 return 0;
124}
125
Chris Lattner0e3461e2010-11-15 06:09:35 +0000126unsigned PPCMCCodeEmitter::getCondBrEncoding(const MCInst &MI, unsigned OpNo,
127 SmallVectorImpl<MCFixup> &Fixups) const {
128 const MCOperand &MO = MI.getOperand(OpNo);
129 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
130
Chris Lattner85e37682010-11-15 06:12:22 +0000131 // Add a fixup for the branch target.
132 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
133 (MCFixupKind)PPC::fixup_ppc_brcond14));
Chris Lattner0e3461e2010-11-15 06:09:35 +0000134 return 0;
135}
136
Ulrich Weigand2dbe06a2013-05-17 14:14:12 +0000137unsigned PPCMCCodeEmitter::getS16ImmEncoding(const MCInst &MI, unsigned OpNo,
Chris Lattner65661122010-11-15 06:33:39 +0000138 SmallVectorImpl<MCFixup> &Fixups) const {
139 const MCOperand &MO = MI.getOperand(OpNo);
140 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
141
142 // Add a fixup for the branch target.
Ulrich Weigand2fb140e2013-05-15 15:07:06 +0000143 Fixups.push_back(MCFixup::Create(2, MO.getExpr(),
Ulrich Weigand6e23ac62013-05-17 12:37:21 +0000144 (MCFixupKind)PPC::fixup_ppc_half16));
Chris Lattner65661122010-11-15 06:33:39 +0000145 return 0;
146}
147
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000148unsigned PPCMCCodeEmitter::getMemRIEncoding(const MCInst &MI, unsigned OpNo,
149 SmallVectorImpl<MCFixup> &Fixups) const {
150 // Encode (imm, reg) as a memri, which has the low 16-bits as the
151 // displacement and the next 5 bits as the register #.
152 assert(MI.getOperand(OpNo+1).isReg());
153 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups) << 16;
154
155 const MCOperand &MO = MI.getOperand(OpNo);
156 if (MO.isImm())
157 return (getMachineOpValue(MI, MO, Fixups) & 0xFFFF) | RegBits;
158
159 // Add a fixup for the displacement field.
Ulrich Weigand2fb140e2013-05-15 15:07:06 +0000160 Fixups.push_back(MCFixup::Create(2, MO.getExpr(),
Ulrich Weigand6e23ac62013-05-17 12:37:21 +0000161 (MCFixupKind)PPC::fixup_ppc_half16));
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000162 return RegBits;
163}
164
165
Chris Lattner8f4444d2010-11-15 08:02:41 +0000166unsigned PPCMCCodeEmitter::getMemRIXEncoding(const MCInst &MI, unsigned OpNo,
Chris Lattner65661122010-11-15 06:33:39 +0000167 SmallVectorImpl<MCFixup> &Fixups) const {
Chris Lattner8f4444d2010-11-15 08:02:41 +0000168 // Encode (imm, reg) as a memrix, which has the low 14-bits as the
169 // displacement and the next 5 bits as the register #.
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000170 assert(MI.getOperand(OpNo+1).isReg());
Chris Lattner8f4444d2010-11-15 08:02:41 +0000171 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups) << 14;
172
Chris Lattner65661122010-11-15 06:33:39 +0000173 const MCOperand &MO = MI.getOperand(OpNo);
Chris Lattner8f4444d2010-11-15 08:02:41 +0000174 if (MO.isImm())
Ulrich Weigand9d980cb2013-05-16 17:58:02 +0000175 return ((getMachineOpValue(MI, MO, Fixups) >> 2) & 0x3FFF) | RegBits;
Chris Lattner65661122010-11-15 06:33:39 +0000176
Ulrich Weigand3e186012013-03-26 10:56:47 +0000177 // Add a fixup for the displacement field.
Ulrich Weigand2fb140e2013-05-15 15:07:06 +0000178 Fixups.push_back(MCFixup::Create(2, MO.getExpr(),
Ulrich Weigand6e23ac62013-05-17 12:37:21 +0000179 (MCFixupKind)PPC::fixup_ppc_half16ds));
Chris Lattner8f4444d2010-11-15 08:02:41 +0000180 return RegBits;
Chris Lattner65661122010-11-15 06:33:39 +0000181}
182
Chris Lattner0e3461e2010-11-15 06:09:35 +0000183
Bill Schmidtca4a0c92012-12-04 16:18:08 +0000184unsigned PPCMCCodeEmitter::getTLSRegEncoding(const MCInst &MI, unsigned OpNo,
185 SmallVectorImpl<MCFixup> &Fixups) const {
186 const MCOperand &MO = MI.getOperand(OpNo);
187 if (MO.isReg()) return getMachineOpValue(MI, MO, Fixups);
188
189 // Add a fixup for the TLS register, which simply provides a relocation
190 // hint to the linker that this statement is part of a relocation sequence.
191 // Return the thread-pointer register's encoding.
192 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
193 (MCFixupKind)PPC::fixup_ppc_tlsreg));
Hal Finkelfeea6532013-03-26 20:08:20 +0000194 return CTX.getRegisterInfo().getEncodingValue(PPC::X13);
Bill Schmidtca4a0c92012-12-04 16:18:08 +0000195}
196
Chris Lattner79fa3712010-11-15 05:57:53 +0000197unsigned PPCMCCodeEmitter::
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000198get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
199 SmallVectorImpl<MCFixup> &Fixups) const {
200 const MCOperand &MO = MI.getOperand(OpNo);
Adhemerval Zanella22b9fd22012-10-08 18:25:11 +0000201 assert((MI.getOpcode() == PPC::MTCRF ||
202 MI.getOpcode() == PPC::MFOCRF ||
203 MI.getOpcode() == PPC::MTCRF8) &&
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000204 (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7));
Hal Finkelfeea6532013-03-26 20:08:20 +0000205 return 0x80 >> CTX.getRegisterInfo().getEncodingValue(MO.getReg());
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000206}
207
208
209unsigned PPCMCCodeEmitter::
Chris Lattner9ec375c2010-11-15 04:16:32 +0000210getMachineOpValue(const MCInst &MI, const MCOperand &MO,
211 SmallVectorImpl<MCFixup> &Fixups) const {
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000212 if (MO.isReg()) {
Chris Lattner7b25d6f2010-11-16 00:57:32 +0000213 // MTCRF/MFOCRF should go through get_crbitm_encoding for the CR operand.
214 // The GPR operand should come through here though.
Chris Lattner73716a62010-11-16 00:55:51 +0000215 assert((MI.getOpcode() != PPC::MTCRF && MI.getOpcode() != PPC::MFOCRF) ||
216 MO.getReg() < PPC::CR0 || MO.getReg() > PPC::CR7);
Hal Finkelfeea6532013-03-26 20:08:20 +0000217 return CTX.getRegisterInfo().getEncodingValue(MO.getReg());
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000218 }
Chris Lattnerc877d8f2010-11-15 04:51:55 +0000219
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000220 assert(MO.isImm() &&
221 "Relocation required in an instruction that we cannot encode!");
222 return MO.getImm();
Chris Lattner9ec375c2010-11-15 04:16:32 +0000223}
224
225
226#include "PPCGenMCCodeEmitter.inc"