blob: 92c8c224b71b4c0a326c3843b4f4c4d38e69e3a2 [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
Evan Cheng61d4a202011-07-25 19:53:23 +000014#include "MCTargetDesc/PPCFixupKinds.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000015#include "PPCInstrInfo.h"
16#include "llvm/ADT/SmallVector.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/ADT/Statistic.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000018#include "llvm/ADT/Triple.h"
Eric Christopher0169e422015-03-10 22:03:14 +000019#include "llvm/MC/MCAsmInfo.h"
Chris Lattner9ec375c2010-11-15 04:16:32 +000020#include "llvm/MC/MCCodeEmitter.h"
Hal Finkelfeea6532013-03-26 20:08:20 +000021#include "llvm/MC/MCContext.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000022#include "llvm/MC/MCFixup.h"
Chris Lattner9ec375c2010-11-15 04:16:32 +000023#include "llvm/MC/MCInst.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000024#include "llvm/MC/MCInstrDesc.h"
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +000025#include "llvm/MC/MCInstrInfo.h"
Pete Cooper3de83e42015-05-15 21:58:42 +000026#include "llvm/MC/MCRegisterInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/MC/MCSubtargetInfo.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000028#include "llvm/Support/Endian.h"
Benjamin Kramer50e2a292015-06-04 15:03:02 +000029#include "llvm/Support/EndianStream.h"
Chris Lattner9ec375c2010-11-15 04:16:32 +000030#include "llvm/Support/ErrorHandling.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000031#include "llvm/Support/MathExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/Support/raw_ostream.h"
Eugene Zelenko8187c192017-01-13 00:58:58 +000033#include <cassert>
34#include <cstdint>
35
Chris Lattner9ec375c2010-11-15 04:16:32 +000036using namespace llvm;
37
Chandler Carruth84e68b22014-04-22 02:41:26 +000038#define DEBUG_TYPE "mccodeemitter"
39
Chris Lattner9ec375c2010-11-15 04:16:32 +000040STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
41
42namespace {
Craig Toppera60c0f12012-09-15 17:09:36 +000043
Eugene Zelenko8187c192017-01-13 00:58:58 +000044class PPCMCCodeEmitter : public MCCodeEmitter {
Hal Finkela7bbaf62014-02-02 06:12:27 +000045 const MCInstrInfo &MCII;
Hal Finkelfeea6532013-03-26 20:08:20 +000046 const MCContext &CTX;
Ulrich Weigandcae3a172014-03-24 18:16:09 +000047 bool IsLittleEndian;
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +000048
Chris Lattner9ec375c2010-11-15 04:16:32 +000049public:
Eric Christopher0169e422015-03-10 22:03:14 +000050 PPCMCCodeEmitter(const MCInstrInfo &mcii, MCContext &ctx)
51 : MCII(mcii), CTX(ctx),
52 IsLittleEndian(ctx.getAsmInfo()->isLittleEndian()) {}
Eugene Zelenko8187c192017-01-13 00:58:58 +000053 PPCMCCodeEmitter(const PPCMCCodeEmitter &) = delete;
54 void operator=(const PPCMCCodeEmitter &) = delete;
55 ~PPCMCCodeEmitter() override = default;
Chris Lattnerd6a07cc2010-11-15 05:19:25 +000056
Chris Lattner0e3461e2010-11-15 06:09:35 +000057 unsigned getDirectBrEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +000058 SmallVectorImpl<MCFixup> &Fixups,
59 const MCSubtargetInfo &STI) const;
Chris Lattner0e3461e2010-11-15 06:09:35 +000060 unsigned getCondBrEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +000061 SmallVectorImpl<MCFixup> &Fixups,
62 const MCSubtargetInfo &STI) const;
Ulrich Weigandb6a30d12013-06-24 11:03:33 +000063 unsigned getAbsDirectBrEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +000064 SmallVectorImpl<MCFixup> &Fixups,
65 const MCSubtargetInfo &STI) const;
Ulrich Weigandb6a30d12013-06-24 11:03:33 +000066 unsigned getAbsCondBrEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +000067 SmallVectorImpl<MCFixup> &Fixups,
68 const MCSubtargetInfo &STI) const;
Ulrich Weigandfd3ad692013-06-26 13:49:15 +000069 unsigned getImm16Encoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +000070 SmallVectorImpl<MCFixup> &Fixups,
71 const MCSubtargetInfo &STI) const;
Chris Lattnerefacb9e2010-11-15 08:22:03 +000072 unsigned getMemRIEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +000073 SmallVectorImpl<MCFixup> &Fixups,
74 const MCSubtargetInfo &STI) const;
Chris Lattner8f4444d2010-11-15 08:02:41 +000075 unsigned getMemRIXEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +000076 SmallVectorImpl<MCFixup> &Fixups,
77 const MCSubtargetInfo &STI) const;
Kit Bartonba532dc2016-03-08 03:49:13 +000078 unsigned getMemRIX16Encoding(const MCInst &MI, unsigned OpNo,
79 SmallVectorImpl<MCFixup> &Fixups,
80 const MCSubtargetInfo &STI) const;
Joerg Sonnenberger0013b922014-08-08 16:43:49 +000081 unsigned getSPE8DisEncoding(const MCInst &MI, unsigned OpNo,
82 SmallVectorImpl<MCFixup> &Fixups,
83 const MCSubtargetInfo &STI) const;
84 unsigned getSPE4DisEncoding(const MCInst &MI, unsigned OpNo,
85 SmallVectorImpl<MCFixup> &Fixups,
86 const MCSubtargetInfo &STI) const;
87 unsigned getSPE2DisEncoding(const MCInst &MI, unsigned OpNo,
88 SmallVectorImpl<MCFixup> &Fixups,
89 const MCSubtargetInfo &STI) const;
Bill Schmidtca4a0c92012-12-04 16:18:08 +000090 unsigned getTLSRegEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +000091 SmallVectorImpl<MCFixup> &Fixups,
92 const MCSubtargetInfo &STI) const;
Ulrich Weigand5143bab2013-07-02 21:31:04 +000093 unsigned getTLSCallEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +000094 SmallVectorImpl<MCFixup> &Fixups,
95 const MCSubtargetInfo &STI) const;
Chris Lattnerd6a07cc2010-11-15 05:19:25 +000096 unsigned get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +000097 SmallVectorImpl<MCFixup> &Fixups,
98 const MCSubtargetInfo &STI) const;
Chris Lattnerd6a07cc2010-11-15 05:19:25 +000099
Chris Lattner9ec375c2010-11-15 04:16:32 +0000100 /// getMachineOpValue - Return binary encoding of operand. If the machine
101 /// operand requires relocation, record the relocation and return zero.
102 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000103 SmallVectorImpl<MCFixup> &Fixups,
104 const MCSubtargetInfo &STI) const;
Chris Lattner9ec375c2010-11-15 04:16:32 +0000105
106 // getBinaryCodeForInstr - TableGen'erated function for getting the
107 // binary encoding for an instruction.
Owen Andersond845d9d2012-01-24 18:37:29 +0000108 uint64_t getBinaryCodeForInstr(const MCInst &MI,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000109 SmallVectorImpl<MCFixup> &Fixups,
110 const MCSubtargetInfo &STI) const;
Eugene Zelenko8187c192017-01-13 00:58:58 +0000111
Jim Grosbach91df21f2015-05-15 19:13:16 +0000112 void encodeInstruction(const MCInst &MI, raw_ostream &OS,
David Woodhouse9784cef2014-01-28 23:13:07 +0000113 SmallVectorImpl<MCFixup> &Fixups,
Craig Topper0d3fa922014-04-29 07:57:37 +0000114 const MCSubtargetInfo &STI) const override {
Daniel Sanders72db2a32016-11-19 13:05:44 +0000115 verifyInstructionPredicates(MI,
116 computeAvailableFeatures(STI.getFeatureBits()));
117
Bill Schmidtc763c222013-09-16 17:25:12 +0000118 unsigned Opcode = MI.getOpcode();
Hal Finkela7bbaf62014-02-02 06:12:27 +0000119 const MCInstrDesc &Desc = MCII.get(Opcode);
Bill Schmidtc763c222013-09-16 17:25:12 +0000120
David Woodhouse3fa98a62014-01-28 23:13:18 +0000121 uint64_t Bits = getBinaryCodeForInstr(MI, Fixups, STI);
Adhemerval Zanella1be10dc2012-10-25 14:29:13 +0000122
Ulrich Weigandcae3a172014-03-24 18:16:09 +0000123 // Output the constant in big/little endian byte order.
Hal Finkela7bbaf62014-02-02 06:12:27 +0000124 unsigned Size = Desc.getSize();
Ulrich Weigand7c3f0dc2014-06-18 15:37:07 +0000125 switch (Size) {
Marcin Koscielnicki7b329572016-04-28 21:24:37 +0000126 case 0:
127 break;
Ulrich Weigand7c3f0dc2014-06-18 15:37:07 +0000128 case 4:
129 if (IsLittleEndian) {
Benjamin Kramer50e2a292015-06-04 15:03:02 +0000130 support::endian::Writer<support::little>(OS).write<uint32_t>(Bits);
Ulrich Weigand7c3f0dc2014-06-18 15:37:07 +0000131 } else {
Benjamin Kramer50e2a292015-06-04 15:03:02 +0000132 support::endian::Writer<support::big>(OS).write<uint32_t>(Bits);
Ulrich Weigandcae3a172014-03-24 18:16:09 +0000133 }
Ulrich Weigand7c3f0dc2014-06-18 15:37:07 +0000134 break;
135 case 8:
136 // If we emit a pair of instructions, the first one is
137 // always in the top 32 bits, even on little-endian.
138 if (IsLittleEndian) {
Benjamin Kramer50e2a292015-06-04 15:03:02 +0000139 uint64_t Swapped = (Bits << 32) | (Bits >> 32);
140 support::endian::Writer<support::little>(OS).write<uint64_t>(Swapped);
Ulrich Weigand7c3f0dc2014-06-18 15:37:07 +0000141 } else {
Benjamin Kramer50e2a292015-06-04 15:03:02 +0000142 support::endian::Writer<support::big>(OS).write<uint64_t>(Bits);
Ulrich Weigandcae3a172014-03-24 18:16:09 +0000143 }
Ulrich Weigand7c3f0dc2014-06-18 15:37:07 +0000144 break;
145 default:
Eugene Zelenko8187c192017-01-13 00:58:58 +0000146 llvm_unreachable("Invalid instruction size");
Chris Lattner9ec375c2010-11-15 04:16:32 +0000147 }
148
149 ++MCNumEmitted; // Keep track of the # of mi's emitted.
150 }
Daniel Sanders72db2a32016-11-19 13:05:44 +0000151
152private:
153 uint64_t computeAvailableFeatures(const FeatureBitset &FB) const;
154 void verifyInstructionPredicates(const MCInst &MI,
155 uint64_t AvailableFeatures) const;
Chris Lattner9ec375c2010-11-15 04:16:32 +0000156};
157
158} // end anonymous namespace
Eric Christopher0169e422015-03-10 22:03:14 +0000159
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000160MCCodeEmitter *llvm::createPPCMCCodeEmitter(const MCInstrInfo &MCII,
Jim Grosbachc3b04272012-05-15 17:35:52 +0000161 const MCRegisterInfo &MRI,
Chris Lattner9ec375c2010-11-15 04:16:32 +0000162 MCContext &Ctx) {
Eric Christopher0169e422015-03-10 22:03:14 +0000163 return new PPCMCCodeEmitter(MCII, Ctx);
Chris Lattner9ec375c2010-11-15 04:16:32 +0000164}
165
166unsigned PPCMCCodeEmitter::
Chris Lattner0e3461e2010-11-15 06:09:35 +0000167getDirectBrEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000168 SmallVectorImpl<MCFixup> &Fixups,
169 const MCSubtargetInfo &STI) const {
Chris Lattner79fa3712010-11-15 05:57:53 +0000170 const MCOperand &MO = MI.getOperand(OpNo);
David Woodhouse3fa98a62014-01-28 23:13:18 +0000171 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI);
Chris Lattner79fa3712010-11-15 05:57:53 +0000172
173 // Add a fixup for the branch target.
Jim Grosbach63661f82015-05-15 19:13:05 +0000174 Fixups.push_back(MCFixup::create(0, MO.getExpr(),
Chris Lattner79fa3712010-11-15 05:57:53 +0000175 (MCFixupKind)PPC::fixup_ppc_br24));
176 return 0;
177}
178
Chris Lattner0e3461e2010-11-15 06:09:35 +0000179unsigned PPCMCCodeEmitter::getCondBrEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000180 SmallVectorImpl<MCFixup> &Fixups,
181 const MCSubtargetInfo &STI) const {
Chris Lattner0e3461e2010-11-15 06:09:35 +0000182 const MCOperand &MO = MI.getOperand(OpNo);
David Woodhouse3fa98a62014-01-28 23:13:18 +0000183 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI);
Chris Lattner0e3461e2010-11-15 06:09:35 +0000184
Chris Lattner85e37682010-11-15 06:12:22 +0000185 // Add a fixup for the branch target.
Jim Grosbach63661f82015-05-15 19:13:05 +0000186 Fixups.push_back(MCFixup::create(0, MO.getExpr(),
Chris Lattner85e37682010-11-15 06:12:22 +0000187 (MCFixupKind)PPC::fixup_ppc_brcond14));
Chris Lattner0e3461e2010-11-15 06:09:35 +0000188 return 0;
189}
190
Ulrich Weigandb6a30d12013-06-24 11:03:33 +0000191unsigned PPCMCCodeEmitter::
192getAbsDirectBrEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000193 SmallVectorImpl<MCFixup> &Fixups,
194 const MCSubtargetInfo &STI) const {
Ulrich Weigandb6a30d12013-06-24 11:03:33 +0000195 const MCOperand &MO = MI.getOperand(OpNo);
David Woodhouse3fa98a62014-01-28 23:13:18 +0000196 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI);
Ulrich Weigandb6a30d12013-06-24 11:03:33 +0000197
198 // Add a fixup for the branch target.
Jim Grosbach63661f82015-05-15 19:13:05 +0000199 Fixups.push_back(MCFixup::create(0, MO.getExpr(),
Ulrich Weigandb6a30d12013-06-24 11:03:33 +0000200 (MCFixupKind)PPC::fixup_ppc_br24abs));
201 return 0;
202}
203
204unsigned PPCMCCodeEmitter::
205getAbsCondBrEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000206 SmallVectorImpl<MCFixup> &Fixups,
207 const MCSubtargetInfo &STI) const {
Ulrich Weigandb6a30d12013-06-24 11:03:33 +0000208 const MCOperand &MO = MI.getOperand(OpNo);
David Woodhouse3fa98a62014-01-28 23:13:18 +0000209 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI);
Ulrich Weigandb6a30d12013-06-24 11:03:33 +0000210
211 // Add a fixup for the branch target.
Jim Grosbach63661f82015-05-15 19:13:05 +0000212 Fixups.push_back(MCFixup::create(0, MO.getExpr(),
Ulrich Weigandb6a30d12013-06-24 11:03:33 +0000213 (MCFixupKind)PPC::fixup_ppc_brcond14abs));
214 return 0;
215}
216
Ulrich Weigandfd3ad692013-06-26 13:49:15 +0000217unsigned PPCMCCodeEmitter::getImm16Encoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000218 SmallVectorImpl<MCFixup> &Fixups,
219 const MCSubtargetInfo &STI) const {
Chris Lattner65661122010-11-15 06:33:39 +0000220 const MCOperand &MO = MI.getOperand(OpNo);
David Woodhouse3fa98a62014-01-28 23:13:18 +0000221 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI);
Chris Lattner65661122010-11-15 06:33:39 +0000222
Ulrich Weigandfd3ad692013-06-26 13:49:15 +0000223 // Add a fixup for the immediate field.
Jim Grosbach63661f82015-05-15 19:13:05 +0000224 Fixups.push_back(MCFixup::create(IsLittleEndian? 0 : 2, MO.getExpr(),
Ulrich Weigand6e23ac62013-05-17 12:37:21 +0000225 (MCFixupKind)PPC::fixup_ppc_half16));
Chris Lattner65661122010-11-15 06:33:39 +0000226 return 0;
227}
228
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000229unsigned PPCMCCodeEmitter::getMemRIEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000230 SmallVectorImpl<MCFixup> &Fixups,
231 const MCSubtargetInfo &STI) const {
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000232 // Encode (imm, reg) as a memri, which has the low 16-bits as the
233 // displacement and the next 5 bits as the register #.
234 assert(MI.getOperand(OpNo+1).isReg());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000235 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 16;
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000236
237 const MCOperand &MO = MI.getOperand(OpNo);
238 if (MO.isImm())
David Woodhouse3fa98a62014-01-28 23:13:18 +0000239 return (getMachineOpValue(MI, MO, Fixups, STI) & 0xFFFF) | RegBits;
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000240
241 // Add a fixup for the displacement field.
Jim Grosbach63661f82015-05-15 19:13:05 +0000242 Fixups.push_back(MCFixup::create(IsLittleEndian? 0 : 2, MO.getExpr(),
Ulrich Weigand6e23ac62013-05-17 12:37:21 +0000243 (MCFixupKind)PPC::fixup_ppc_half16));
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000244 return RegBits;
245}
246
Chris Lattner8f4444d2010-11-15 08:02:41 +0000247unsigned PPCMCCodeEmitter::getMemRIXEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000248 SmallVectorImpl<MCFixup> &Fixups,
249 const MCSubtargetInfo &STI) const {
Chris Lattner8f4444d2010-11-15 08:02:41 +0000250 // Encode (imm, reg) as a memrix, which has the low 14-bits as the
251 // displacement and the next 5 bits as the register #.
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000252 assert(MI.getOperand(OpNo+1).isReg());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000253 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 14;
Chris Lattner8f4444d2010-11-15 08:02:41 +0000254
Chris Lattner65661122010-11-15 06:33:39 +0000255 const MCOperand &MO = MI.getOperand(OpNo);
Chris Lattner8f4444d2010-11-15 08:02:41 +0000256 if (MO.isImm())
David Woodhouse3fa98a62014-01-28 23:13:18 +0000257 return ((getMachineOpValue(MI, MO, Fixups, STI) >> 2) & 0x3FFF) | RegBits;
Chris Lattner65661122010-11-15 06:33:39 +0000258
Ulrich Weigand3e186012013-03-26 10:56:47 +0000259 // Add a fixup for the displacement field.
Jim Grosbach63661f82015-05-15 19:13:05 +0000260 Fixups.push_back(MCFixup::create(IsLittleEndian? 0 : 2, MO.getExpr(),
Ulrich Weigand6e23ac62013-05-17 12:37:21 +0000261 (MCFixupKind)PPC::fixup_ppc_half16ds));
Chris Lattner8f4444d2010-11-15 08:02:41 +0000262 return RegBits;
Chris Lattner65661122010-11-15 06:33:39 +0000263}
264
Kit Bartonba532dc2016-03-08 03:49:13 +0000265unsigned PPCMCCodeEmitter::getMemRIX16Encoding(const MCInst &MI, unsigned OpNo,
266 SmallVectorImpl<MCFixup> &Fixups,
267 const MCSubtargetInfo &STI) const {
268 // Encode (imm, reg) as a memrix16, which has the low 12-bits as the
269 // displacement and the next 5 bits as the register #.
270 assert(MI.getOperand(OpNo+1).isReg());
271 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 12;
272
273 const MCOperand &MO = MI.getOperand(OpNo);
Nemanja Ivanovic3c7e276d2017-07-13 18:17:10 +0000274 assert(MO.isImm() && !(MO.getImm() % 16) &&
275 "Expecting an immediate that is a multiple of 16");
Kit Bartonba532dc2016-03-08 03:49:13 +0000276
277 return ((getMachineOpValue(MI, MO, Fixups, STI) >> 4) & 0xFFF) | RegBits;
278}
Chris Lattner0e3461e2010-11-15 06:09:35 +0000279
Joerg Sonnenberger0013b922014-08-08 16:43:49 +0000280unsigned PPCMCCodeEmitter::getSPE8DisEncoding(const MCInst &MI, unsigned OpNo,
281 SmallVectorImpl<MCFixup> &Fixups,
282 const MCSubtargetInfo &STI)
283 const {
284 // Encode (imm, reg) as a spe8dis, which has the low 5-bits of (imm / 8)
285 // as the displacement and the next 5 bits as the register #.
286 assert(MI.getOperand(OpNo+1).isReg());
287 uint32_t RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 5;
288
289 const MCOperand &MO = MI.getOperand(OpNo);
290 assert(MO.isImm());
291 uint32_t Imm = getMachineOpValue(MI, MO, Fixups, STI) >> 3;
292 return reverseBits(Imm | RegBits) >> 22;
293}
294
Joerg Sonnenberger0013b922014-08-08 16:43:49 +0000295unsigned PPCMCCodeEmitter::getSPE4DisEncoding(const MCInst &MI, unsigned OpNo,
296 SmallVectorImpl<MCFixup> &Fixups,
297 const MCSubtargetInfo &STI)
298 const {
299 // Encode (imm, reg) as a spe4dis, which has the low 5-bits of (imm / 4)
300 // as the displacement and the next 5 bits as the register #.
301 assert(MI.getOperand(OpNo+1).isReg());
302 uint32_t RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 5;
303
304 const MCOperand &MO = MI.getOperand(OpNo);
305 assert(MO.isImm());
306 uint32_t Imm = getMachineOpValue(MI, MO, Fixups, STI) >> 2;
307 return reverseBits(Imm | RegBits) >> 22;
308}
309
Joerg Sonnenberger0013b922014-08-08 16:43:49 +0000310unsigned PPCMCCodeEmitter::getSPE2DisEncoding(const MCInst &MI, unsigned OpNo,
311 SmallVectorImpl<MCFixup> &Fixups,
312 const MCSubtargetInfo &STI)
313 const {
314 // Encode (imm, reg) as a spe2dis, which has the low 5-bits of (imm / 2)
315 // as the displacement and the next 5 bits as the register #.
316 assert(MI.getOperand(OpNo+1).isReg());
317 uint32_t RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 5;
318
319 const MCOperand &MO = MI.getOperand(OpNo);
320 assert(MO.isImm());
321 uint32_t Imm = getMachineOpValue(MI, MO, Fixups, STI) >> 1;
322 return reverseBits(Imm | RegBits) >> 22;
323}
324
Bill Schmidtca4a0c92012-12-04 16:18:08 +0000325unsigned PPCMCCodeEmitter::getTLSRegEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000326 SmallVectorImpl<MCFixup> &Fixups,
327 const MCSubtargetInfo &STI) const {
Bill Schmidtca4a0c92012-12-04 16:18:08 +0000328 const MCOperand &MO = MI.getOperand(OpNo);
David Woodhouse3fa98a62014-01-28 23:13:18 +0000329 if (MO.isReg()) return getMachineOpValue(MI, MO, Fixups, STI);
Bill Schmidtca4a0c92012-12-04 16:18:08 +0000330
331 // Add a fixup for the TLS register, which simply provides a relocation
332 // hint to the linker that this statement is part of a relocation sequence.
333 // Return the thread-pointer register's encoding.
Jim Grosbach63661f82015-05-15 19:13:05 +0000334 Fixups.push_back(MCFixup::create(0, MO.getExpr(),
Ulrich Weigand5b427592013-07-05 12:22:36 +0000335 (MCFixupKind)PPC::fixup_ppc_nofixup));
Daniel Sanders50f17232015-09-15 16:17:27 +0000336 const Triple &TT = STI.getTargetTriple();
337 bool isPPC64 = TT.getArch() == Triple::ppc64 || TT.getArch() == Triple::ppc64le;
Roman Divackybc1655b42013-12-22 10:45:37 +0000338 return CTX.getRegisterInfo()->getEncodingValue(isPPC64 ? PPC::X13 : PPC::R2);
Bill Schmidtca4a0c92012-12-04 16:18:08 +0000339}
340
Ulrich Weigand5143bab2013-07-02 21:31:04 +0000341unsigned PPCMCCodeEmitter::getTLSCallEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000342 SmallVectorImpl<MCFixup> &Fixups,
343 const MCSubtargetInfo &STI) const {
Ulrich Weigand5143bab2013-07-02 21:31:04 +0000344 // For special TLS calls, we need two fixups; one for the branch target
345 // (__tls_get_addr), which we create via getDirectBrEncoding as usual,
346 // and one for the TLSGD or TLSLD symbol, which is emitted here.
347 const MCOperand &MO = MI.getOperand(OpNo+1);
Jim Grosbach63661f82015-05-15 19:13:05 +0000348 Fixups.push_back(MCFixup::create(0, MO.getExpr(),
Ulrich Weigand5143bab2013-07-02 21:31:04 +0000349 (MCFixupKind)PPC::fixup_ppc_nofixup));
David Woodhouse3fa98a62014-01-28 23:13:18 +0000350 return getDirectBrEncoding(MI, OpNo, Fixups, STI);
Ulrich Weigand5143bab2013-07-02 21:31:04 +0000351}
352
Chris Lattner79fa3712010-11-15 05:57:53 +0000353unsigned PPCMCCodeEmitter::
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000354get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000355 SmallVectorImpl<MCFixup> &Fixups,
356 const MCSubtargetInfo &STI) const {
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000357 const MCOperand &MO = MI.getOperand(OpNo);
Ulrich Weigand49f487e2013-07-03 17:59:07 +0000358 assert((MI.getOpcode() == PPC::MTOCRF || MI.getOpcode() == PPC::MTOCRF8 ||
Ulrich Weigandd5ebc622013-07-03 17:05:42 +0000359 MI.getOpcode() == PPC::MFOCRF || MI.getOpcode() == PPC::MFOCRF8) &&
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000360 (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7));
Bill Wendlingbc07a892013-06-18 07:20:20 +0000361 return 0x80 >> CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000362}
363
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000364unsigned PPCMCCodeEmitter::
Chris Lattner9ec375c2010-11-15 04:16:32 +0000365getMachineOpValue(const MCInst &MI, const MCOperand &MO,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000366 SmallVectorImpl<MCFixup> &Fixups,
367 const MCSubtargetInfo &STI) const {
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000368 if (MO.isReg()) {
Ulrich Weigand49f487e2013-07-03 17:59:07 +0000369 // MTOCRF/MFOCRF should go through get_crbitm_encoding for the CR operand.
Chris Lattner7b25d6f2010-11-16 00:57:32 +0000370 // The GPR operand should come through here though.
Ulrich Weigand49f487e2013-07-03 17:59:07 +0000371 assert((MI.getOpcode() != PPC::MTOCRF && MI.getOpcode() != PPC::MTOCRF8 &&
Ulrich Weigandd5ebc622013-07-03 17:05:42 +0000372 MI.getOpcode() != PPC::MFOCRF && MI.getOpcode() != PPC::MFOCRF8) ||
Chris Lattner73716a62010-11-16 00:55:51 +0000373 MO.getReg() < PPC::CR0 || MO.getReg() > PPC::CR7);
Nemanja Ivanovic11049f82016-10-04 06:59:23 +0000374 unsigned Reg = MO.getReg();
375 unsigned Encode = CTX.getRegisterInfo()->getEncodingValue(Reg);
376
377 if ((MCII.get(MI.getOpcode()).TSFlags & PPCII::UseVSXReg))
378 if (PPCInstrInfo::isVRRegister(Reg))
379 Encode += 32;
380
381 return Encode;
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000382 }
Chris Lattnerc877d8f2010-11-15 04:51:55 +0000383
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000384 assert(MO.isImm() &&
385 "Relocation required in an instruction that we cannot encode!");
386 return MO.getImm();
Chris Lattner9ec375c2010-11-15 04:16:32 +0000387}
388
Daniel Sanders72db2a32016-11-19 13:05:44 +0000389#define ENABLE_INSTR_PREDICATE_VERIFIER
Chris Lattner9ec375c2010-11-15 04:16:32 +0000390#include "PPCGenMCCodeEmitter.inc"