blob: e7b2d8369f2f89e4110eb47a6d2313027766a661 [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
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "MCTargetDesc/PPCMCTargetDesc.h"
Evan Cheng61d4a202011-07-25 19:53:23 +000015#include "MCTargetDesc/PPCFixupKinds.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/Statistic.h"
Eric Christopher0169e422015-03-10 22:03:14 +000017#include "llvm/MC/MCAsmInfo.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"
Pete Cooper3de83e42015-05-15 21:58:42 +000023#include "llvm/MC/MCRegisterInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/MC/MCSubtargetInfo.h"
Benjamin Kramer50e2a292015-06-04 15:03:02 +000025#include "llvm/Support/EndianStream.h"
Chris Lattner9ec375c2010-11-15 04:16:32 +000026#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/Support/raw_ostream.h"
Bill Schmidtc763c222013-09-16 17:25:12 +000028#include "llvm/Target/TargetOpcodes.h"
Chris Lattner9ec375c2010-11-15 04:16:32 +000029using namespace llvm;
30
Chandler Carruth84e68b22014-04-22 02:41:26 +000031#define DEBUG_TYPE "mccodeemitter"
32
Chris Lattner9ec375c2010-11-15 04:16:32 +000033STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
34
35namespace {
36class PPCMCCodeEmitter : public MCCodeEmitter {
Aaron Ballmanf9a18972015-02-15 22:54:22 +000037 PPCMCCodeEmitter(const PPCMCCodeEmitter &) = delete;
38 void operator=(const PPCMCCodeEmitter &) = delete;
Craig Toppera60c0f12012-09-15 17:09:36 +000039
Hal Finkela7bbaf62014-02-02 06:12:27 +000040 const MCInstrInfo &MCII;
Hal Finkelfeea6532013-03-26 20:08:20 +000041 const MCContext &CTX;
Ulrich Weigandcae3a172014-03-24 18:16:09 +000042 bool IsLittleEndian;
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +000043
Chris Lattner9ec375c2010-11-15 04:16:32 +000044public:
Eric Christopher0169e422015-03-10 22:03:14 +000045 PPCMCCodeEmitter(const MCInstrInfo &mcii, MCContext &ctx)
46 : MCII(mcii), CTX(ctx),
47 IsLittleEndian(ctx.getAsmInfo()->isLittleEndian()) {}
48
Alexander Kornienkof817c1c2015-04-11 02:11:45 +000049 ~PPCMCCodeEmitter() override {}
Chris Lattnerd6a07cc2010-11-15 05:19:25 +000050
Chris Lattner0e3461e2010-11-15 06:09:35 +000051 unsigned getDirectBrEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +000052 SmallVectorImpl<MCFixup> &Fixups,
53 const MCSubtargetInfo &STI) const;
Chris Lattner0e3461e2010-11-15 06:09:35 +000054 unsigned getCondBrEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +000055 SmallVectorImpl<MCFixup> &Fixups,
56 const MCSubtargetInfo &STI) const;
Ulrich Weigandb6a30d12013-06-24 11:03:33 +000057 unsigned getAbsDirectBrEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +000058 SmallVectorImpl<MCFixup> &Fixups,
59 const MCSubtargetInfo &STI) const;
Ulrich Weigandb6a30d12013-06-24 11:03:33 +000060 unsigned getAbsCondBrEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +000061 SmallVectorImpl<MCFixup> &Fixups,
62 const MCSubtargetInfo &STI) const;
Ulrich Weigandfd3ad692013-06-26 13:49:15 +000063 unsigned getImm16Encoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +000064 SmallVectorImpl<MCFixup> &Fixups,
65 const MCSubtargetInfo &STI) const;
Chris Lattnerefacb9e2010-11-15 08:22:03 +000066 unsigned getMemRIEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +000067 SmallVectorImpl<MCFixup> &Fixups,
68 const MCSubtargetInfo &STI) const;
Chris Lattner8f4444d2010-11-15 08:02:41 +000069 unsigned getMemRIXEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +000070 SmallVectorImpl<MCFixup> &Fixups,
71 const MCSubtargetInfo &STI) const;
Kit Bartonba532dc2016-03-08 03:49:13 +000072 unsigned getMemRIX16Encoding(const MCInst &MI, unsigned OpNo,
73 SmallVectorImpl<MCFixup> &Fixups,
74 const MCSubtargetInfo &STI) const;
Joerg Sonnenberger0013b922014-08-08 16:43:49 +000075 unsigned getSPE8DisEncoding(const MCInst &MI, unsigned OpNo,
76 SmallVectorImpl<MCFixup> &Fixups,
77 const MCSubtargetInfo &STI) const;
78 unsigned getSPE4DisEncoding(const MCInst &MI, unsigned OpNo,
79 SmallVectorImpl<MCFixup> &Fixups,
80 const MCSubtargetInfo &STI) const;
81 unsigned getSPE2DisEncoding(const MCInst &MI, unsigned OpNo,
82 SmallVectorImpl<MCFixup> &Fixups,
83 const MCSubtargetInfo &STI) const;
Bill Schmidtca4a0c92012-12-04 16:18:08 +000084 unsigned getTLSRegEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +000085 SmallVectorImpl<MCFixup> &Fixups,
86 const MCSubtargetInfo &STI) const;
Ulrich Weigand5143bab2013-07-02 21:31:04 +000087 unsigned getTLSCallEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +000088 SmallVectorImpl<MCFixup> &Fixups,
89 const MCSubtargetInfo &STI) const;
Chris Lattnerd6a07cc2010-11-15 05:19:25 +000090 unsigned get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +000091 SmallVectorImpl<MCFixup> &Fixups,
92 const MCSubtargetInfo &STI) const;
Chris Lattnerd6a07cc2010-11-15 05:19:25 +000093
Chris Lattner9ec375c2010-11-15 04:16:32 +000094 /// getMachineOpValue - Return binary encoding of operand. If the machine
95 /// operand requires relocation, record the relocation and return zero.
96 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
David Woodhouse3fa98a62014-01-28 23:13:18 +000097 SmallVectorImpl<MCFixup> &Fixups,
98 const MCSubtargetInfo &STI) const;
Chris Lattner9ec375c2010-11-15 04:16:32 +000099
100 // getBinaryCodeForInstr - TableGen'erated function for getting the
101 // binary encoding for an instruction.
Owen Andersond845d9d2012-01-24 18:37:29 +0000102 uint64_t getBinaryCodeForInstr(const MCInst &MI,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000103 SmallVectorImpl<MCFixup> &Fixups,
104 const MCSubtargetInfo &STI) const;
Jim Grosbach91df21f2015-05-15 19:13:16 +0000105 void encodeInstruction(const MCInst &MI, raw_ostream &OS,
David Woodhouse9784cef2014-01-28 23:13:07 +0000106 SmallVectorImpl<MCFixup> &Fixups,
Craig Topper0d3fa922014-04-29 07:57:37 +0000107 const MCSubtargetInfo &STI) const override {
Bill Schmidtc763c222013-09-16 17:25:12 +0000108 unsigned Opcode = MI.getOpcode();
Hal Finkela7bbaf62014-02-02 06:12:27 +0000109 const MCInstrDesc &Desc = MCII.get(Opcode);
Bill Schmidtc763c222013-09-16 17:25:12 +0000110
David Woodhouse3fa98a62014-01-28 23:13:18 +0000111 uint64_t Bits = getBinaryCodeForInstr(MI, Fixups, STI);
Adhemerval Zanella1be10dc2012-10-25 14:29:13 +0000112
Ulrich Weigandcae3a172014-03-24 18:16:09 +0000113 // Output the constant in big/little endian byte order.
Hal Finkela7bbaf62014-02-02 06:12:27 +0000114 unsigned Size = Desc.getSize();
Ulrich Weigand7c3f0dc2014-06-18 15:37:07 +0000115 switch (Size) {
Marcin Koscielnicki7b329572016-04-28 21:24:37 +0000116 case 0:
117 break;
Ulrich Weigand7c3f0dc2014-06-18 15:37:07 +0000118 case 4:
119 if (IsLittleEndian) {
Benjamin Kramer50e2a292015-06-04 15:03:02 +0000120 support::endian::Writer<support::little>(OS).write<uint32_t>(Bits);
Ulrich Weigand7c3f0dc2014-06-18 15:37:07 +0000121 } else {
Benjamin Kramer50e2a292015-06-04 15:03:02 +0000122 support::endian::Writer<support::big>(OS).write<uint32_t>(Bits);
Ulrich Weigandcae3a172014-03-24 18:16:09 +0000123 }
Ulrich Weigand7c3f0dc2014-06-18 15:37:07 +0000124 break;
125 case 8:
126 // If we emit a pair of instructions, the first one is
127 // always in the top 32 bits, even on little-endian.
128 if (IsLittleEndian) {
Benjamin Kramer50e2a292015-06-04 15:03:02 +0000129 uint64_t Swapped = (Bits << 32) | (Bits >> 32);
130 support::endian::Writer<support::little>(OS).write<uint64_t>(Swapped);
Ulrich Weigand7c3f0dc2014-06-18 15:37:07 +0000131 } else {
Benjamin Kramer50e2a292015-06-04 15:03:02 +0000132 support::endian::Writer<support::big>(OS).write<uint64_t>(Bits);
Ulrich Weigandcae3a172014-03-24 18:16:09 +0000133 }
Ulrich Weigand7c3f0dc2014-06-18 15:37:07 +0000134 break;
135 default:
136 llvm_unreachable ("Invalid instruction size");
Chris Lattner9ec375c2010-11-15 04:16:32 +0000137 }
138
139 ++MCNumEmitted; // Keep track of the # of mi's emitted.
140 }
141
142};
143
144} // end anonymous namespace
Eric Christopher0169e422015-03-10 22:03:14 +0000145
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000146MCCodeEmitter *llvm::createPPCMCCodeEmitter(const MCInstrInfo &MCII,
Jim Grosbachc3b04272012-05-15 17:35:52 +0000147 const MCRegisterInfo &MRI,
Chris Lattner9ec375c2010-11-15 04:16:32 +0000148 MCContext &Ctx) {
Eric Christopher0169e422015-03-10 22:03:14 +0000149 return new PPCMCCodeEmitter(MCII, Ctx);
Chris Lattner9ec375c2010-11-15 04:16:32 +0000150}
151
152unsigned PPCMCCodeEmitter::
Chris Lattner0e3461e2010-11-15 06:09:35 +0000153getDirectBrEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000154 SmallVectorImpl<MCFixup> &Fixups,
155 const MCSubtargetInfo &STI) const {
Chris Lattner79fa3712010-11-15 05:57:53 +0000156 const MCOperand &MO = MI.getOperand(OpNo);
David Woodhouse3fa98a62014-01-28 23:13:18 +0000157 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI);
Chris Lattner79fa3712010-11-15 05:57:53 +0000158
159 // Add a fixup for the branch target.
Jim Grosbach63661f82015-05-15 19:13:05 +0000160 Fixups.push_back(MCFixup::create(0, MO.getExpr(),
Chris Lattner79fa3712010-11-15 05:57:53 +0000161 (MCFixupKind)PPC::fixup_ppc_br24));
162 return 0;
163}
164
Chris Lattner0e3461e2010-11-15 06:09:35 +0000165unsigned PPCMCCodeEmitter::getCondBrEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000166 SmallVectorImpl<MCFixup> &Fixups,
167 const MCSubtargetInfo &STI) const {
Chris Lattner0e3461e2010-11-15 06:09:35 +0000168 const MCOperand &MO = MI.getOperand(OpNo);
David Woodhouse3fa98a62014-01-28 23:13:18 +0000169 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI);
Chris Lattner0e3461e2010-11-15 06:09:35 +0000170
Chris Lattner85e37682010-11-15 06:12:22 +0000171 // Add a fixup for the branch target.
Jim Grosbach63661f82015-05-15 19:13:05 +0000172 Fixups.push_back(MCFixup::create(0, MO.getExpr(),
Chris Lattner85e37682010-11-15 06:12:22 +0000173 (MCFixupKind)PPC::fixup_ppc_brcond14));
Chris Lattner0e3461e2010-11-15 06:09:35 +0000174 return 0;
175}
176
Ulrich Weigandb6a30d12013-06-24 11:03:33 +0000177unsigned PPCMCCodeEmitter::
178getAbsDirectBrEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000179 SmallVectorImpl<MCFixup> &Fixups,
180 const MCSubtargetInfo &STI) const {
Ulrich Weigandb6a30d12013-06-24 11:03:33 +0000181 const MCOperand &MO = MI.getOperand(OpNo);
David Woodhouse3fa98a62014-01-28 23:13:18 +0000182 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI);
Ulrich Weigandb6a30d12013-06-24 11:03:33 +0000183
184 // Add a fixup for the branch target.
Jim Grosbach63661f82015-05-15 19:13:05 +0000185 Fixups.push_back(MCFixup::create(0, MO.getExpr(),
Ulrich Weigandb6a30d12013-06-24 11:03:33 +0000186 (MCFixupKind)PPC::fixup_ppc_br24abs));
187 return 0;
188}
189
190unsigned PPCMCCodeEmitter::
191getAbsCondBrEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000192 SmallVectorImpl<MCFixup> &Fixups,
193 const MCSubtargetInfo &STI) const {
Ulrich Weigandb6a30d12013-06-24 11:03:33 +0000194 const MCOperand &MO = MI.getOperand(OpNo);
David Woodhouse3fa98a62014-01-28 23:13:18 +0000195 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI);
Ulrich Weigandb6a30d12013-06-24 11:03:33 +0000196
197 // Add a fixup for the branch target.
Jim Grosbach63661f82015-05-15 19:13:05 +0000198 Fixups.push_back(MCFixup::create(0, MO.getExpr(),
Ulrich Weigandb6a30d12013-06-24 11:03:33 +0000199 (MCFixupKind)PPC::fixup_ppc_brcond14abs));
200 return 0;
201}
202
Ulrich Weigandfd3ad692013-06-26 13:49:15 +0000203unsigned PPCMCCodeEmitter::getImm16Encoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000204 SmallVectorImpl<MCFixup> &Fixups,
205 const MCSubtargetInfo &STI) const {
Chris Lattner65661122010-11-15 06:33:39 +0000206 const MCOperand &MO = MI.getOperand(OpNo);
David Woodhouse3fa98a62014-01-28 23:13:18 +0000207 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI);
Chris Lattner65661122010-11-15 06:33:39 +0000208
Ulrich Weigandfd3ad692013-06-26 13:49:15 +0000209 // Add a fixup for the immediate field.
Jim Grosbach63661f82015-05-15 19:13:05 +0000210 Fixups.push_back(MCFixup::create(IsLittleEndian? 0 : 2, MO.getExpr(),
Ulrich Weigand6e23ac62013-05-17 12:37:21 +0000211 (MCFixupKind)PPC::fixup_ppc_half16));
Chris Lattner65661122010-11-15 06:33:39 +0000212 return 0;
213}
214
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000215unsigned PPCMCCodeEmitter::getMemRIEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000216 SmallVectorImpl<MCFixup> &Fixups,
217 const MCSubtargetInfo &STI) const {
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000218 // Encode (imm, reg) as a memri, which has the low 16-bits as the
219 // displacement and the next 5 bits as the register #.
220 assert(MI.getOperand(OpNo+1).isReg());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000221 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 16;
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000222
223 const MCOperand &MO = MI.getOperand(OpNo);
224 if (MO.isImm())
David Woodhouse3fa98a62014-01-28 23:13:18 +0000225 return (getMachineOpValue(MI, MO, Fixups, STI) & 0xFFFF) | RegBits;
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000226
227 // Add a fixup for the displacement field.
Jim Grosbach63661f82015-05-15 19:13:05 +0000228 Fixups.push_back(MCFixup::create(IsLittleEndian? 0 : 2, MO.getExpr(),
Ulrich Weigand6e23ac62013-05-17 12:37:21 +0000229 (MCFixupKind)PPC::fixup_ppc_half16));
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000230 return RegBits;
231}
232
233
Chris Lattner8f4444d2010-11-15 08:02:41 +0000234unsigned PPCMCCodeEmitter::getMemRIXEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000235 SmallVectorImpl<MCFixup> &Fixups,
236 const MCSubtargetInfo &STI) const {
Chris Lattner8f4444d2010-11-15 08:02:41 +0000237 // Encode (imm, reg) as a memrix, which has the low 14-bits as the
238 // displacement and the next 5 bits as the register #.
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000239 assert(MI.getOperand(OpNo+1).isReg());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000240 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 14;
Chris Lattner8f4444d2010-11-15 08:02:41 +0000241
Chris Lattner65661122010-11-15 06:33:39 +0000242 const MCOperand &MO = MI.getOperand(OpNo);
Chris Lattner8f4444d2010-11-15 08:02:41 +0000243 if (MO.isImm())
David Woodhouse3fa98a62014-01-28 23:13:18 +0000244 return ((getMachineOpValue(MI, MO, Fixups, STI) >> 2) & 0x3FFF) | RegBits;
Chris Lattner65661122010-11-15 06:33:39 +0000245
Ulrich Weigand3e186012013-03-26 10:56:47 +0000246 // Add a fixup for the displacement field.
Jim Grosbach63661f82015-05-15 19:13:05 +0000247 Fixups.push_back(MCFixup::create(IsLittleEndian? 0 : 2, MO.getExpr(),
Ulrich Weigand6e23ac62013-05-17 12:37:21 +0000248 (MCFixupKind)PPC::fixup_ppc_half16ds));
Chris Lattner8f4444d2010-11-15 08:02:41 +0000249 return RegBits;
Chris Lattner65661122010-11-15 06:33:39 +0000250}
251
Kit Bartonba532dc2016-03-08 03:49:13 +0000252unsigned PPCMCCodeEmitter::getMemRIX16Encoding(const MCInst &MI, unsigned OpNo,
253 SmallVectorImpl<MCFixup> &Fixups,
254 const MCSubtargetInfo &STI) const {
255 // Encode (imm, reg) as a memrix16, which has the low 12-bits as the
256 // displacement and the next 5 bits as the register #.
257 assert(MI.getOperand(OpNo+1).isReg());
258 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 12;
259
260 const MCOperand &MO = MI.getOperand(OpNo);
261 assert(MO.isImm());
262
263 return ((getMachineOpValue(MI, MO, Fixups, STI) >> 4) & 0xFFF) | RegBits;
264}
Chris Lattner0e3461e2010-11-15 06:09:35 +0000265
Joerg Sonnenberger0013b922014-08-08 16:43:49 +0000266unsigned PPCMCCodeEmitter::getSPE8DisEncoding(const MCInst &MI, unsigned OpNo,
267 SmallVectorImpl<MCFixup> &Fixups,
268 const MCSubtargetInfo &STI)
269 const {
270 // Encode (imm, reg) as a spe8dis, which has the low 5-bits of (imm / 8)
271 // as the displacement and the next 5 bits as the register #.
272 assert(MI.getOperand(OpNo+1).isReg());
273 uint32_t RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 5;
274
275 const MCOperand &MO = MI.getOperand(OpNo);
276 assert(MO.isImm());
277 uint32_t Imm = getMachineOpValue(MI, MO, Fixups, STI) >> 3;
278 return reverseBits(Imm | RegBits) >> 22;
279}
280
281
282unsigned PPCMCCodeEmitter::getSPE4DisEncoding(const MCInst &MI, unsigned OpNo,
283 SmallVectorImpl<MCFixup> &Fixups,
284 const MCSubtargetInfo &STI)
285 const {
286 // Encode (imm, reg) as a spe4dis, which has the low 5-bits of (imm / 4)
287 // as the displacement and the next 5 bits as the register #.
288 assert(MI.getOperand(OpNo+1).isReg());
289 uint32_t RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 5;
290
291 const MCOperand &MO = MI.getOperand(OpNo);
292 assert(MO.isImm());
293 uint32_t Imm = getMachineOpValue(MI, MO, Fixups, STI) >> 2;
294 return reverseBits(Imm | RegBits) >> 22;
295}
296
297
298unsigned PPCMCCodeEmitter::getSPE2DisEncoding(const MCInst &MI, unsigned OpNo,
299 SmallVectorImpl<MCFixup> &Fixups,
300 const MCSubtargetInfo &STI)
301 const {
302 // Encode (imm, reg) as a spe2dis, which has the low 5-bits of (imm / 2)
303 // as the displacement and the next 5 bits as the register #.
304 assert(MI.getOperand(OpNo+1).isReg());
305 uint32_t RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 5;
306
307 const MCOperand &MO = MI.getOperand(OpNo);
308 assert(MO.isImm());
309 uint32_t Imm = getMachineOpValue(MI, MO, Fixups, STI) >> 1;
310 return reverseBits(Imm | RegBits) >> 22;
311}
312
313
Bill Schmidtca4a0c92012-12-04 16:18:08 +0000314unsigned PPCMCCodeEmitter::getTLSRegEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000315 SmallVectorImpl<MCFixup> &Fixups,
316 const MCSubtargetInfo &STI) const {
Bill Schmidtca4a0c92012-12-04 16:18:08 +0000317 const MCOperand &MO = MI.getOperand(OpNo);
David Woodhouse3fa98a62014-01-28 23:13:18 +0000318 if (MO.isReg()) return getMachineOpValue(MI, MO, Fixups, STI);
Bill Schmidtca4a0c92012-12-04 16:18:08 +0000319
320 // Add a fixup for the TLS register, which simply provides a relocation
321 // hint to the linker that this statement is part of a relocation sequence.
322 // Return the thread-pointer register's encoding.
Jim Grosbach63661f82015-05-15 19:13:05 +0000323 Fixups.push_back(MCFixup::create(0, MO.getExpr(),
Ulrich Weigand5b427592013-07-05 12:22:36 +0000324 (MCFixupKind)PPC::fixup_ppc_nofixup));
Daniel Sanders50f17232015-09-15 16:17:27 +0000325 const Triple &TT = STI.getTargetTriple();
326 bool isPPC64 = TT.getArch() == Triple::ppc64 || TT.getArch() == Triple::ppc64le;
Roman Divackybc1655b42013-12-22 10:45:37 +0000327 return CTX.getRegisterInfo()->getEncodingValue(isPPC64 ? PPC::X13 : PPC::R2);
Bill Schmidtca4a0c92012-12-04 16:18:08 +0000328}
329
Ulrich Weigand5143bab2013-07-02 21:31:04 +0000330unsigned PPCMCCodeEmitter::getTLSCallEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000331 SmallVectorImpl<MCFixup> &Fixups,
332 const MCSubtargetInfo &STI) const {
Ulrich Weigand5143bab2013-07-02 21:31:04 +0000333 // For special TLS calls, we need two fixups; one for the branch target
334 // (__tls_get_addr), which we create via getDirectBrEncoding as usual,
335 // and one for the TLSGD or TLSLD symbol, which is emitted here.
336 const MCOperand &MO = MI.getOperand(OpNo+1);
Jim Grosbach63661f82015-05-15 19:13:05 +0000337 Fixups.push_back(MCFixup::create(0, MO.getExpr(),
Ulrich Weigand5143bab2013-07-02 21:31:04 +0000338 (MCFixupKind)PPC::fixup_ppc_nofixup));
David Woodhouse3fa98a62014-01-28 23:13:18 +0000339 return getDirectBrEncoding(MI, OpNo, Fixups, STI);
Ulrich Weigand5143bab2013-07-02 21:31:04 +0000340}
341
Chris Lattner79fa3712010-11-15 05:57:53 +0000342unsigned PPCMCCodeEmitter::
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000343get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000344 SmallVectorImpl<MCFixup> &Fixups,
345 const MCSubtargetInfo &STI) const {
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000346 const MCOperand &MO = MI.getOperand(OpNo);
Ulrich Weigand49f487e2013-07-03 17:59:07 +0000347 assert((MI.getOpcode() == PPC::MTOCRF || MI.getOpcode() == PPC::MTOCRF8 ||
Ulrich Weigandd5ebc622013-07-03 17:05:42 +0000348 MI.getOpcode() == PPC::MFOCRF || MI.getOpcode() == PPC::MFOCRF8) &&
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000349 (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7));
Bill Wendlingbc07a892013-06-18 07:20:20 +0000350 return 0x80 >> CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000351}
352
353
354unsigned PPCMCCodeEmitter::
Chris Lattner9ec375c2010-11-15 04:16:32 +0000355getMachineOpValue(const MCInst &MI, const MCOperand &MO,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000356 SmallVectorImpl<MCFixup> &Fixups,
357 const MCSubtargetInfo &STI) const {
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000358 if (MO.isReg()) {
Ulrich Weigand49f487e2013-07-03 17:59:07 +0000359 // MTOCRF/MFOCRF should go through get_crbitm_encoding for the CR operand.
Chris Lattner7b25d6f2010-11-16 00:57:32 +0000360 // The GPR operand should come through here though.
Ulrich Weigand49f487e2013-07-03 17:59:07 +0000361 assert((MI.getOpcode() != PPC::MTOCRF && MI.getOpcode() != PPC::MTOCRF8 &&
Ulrich Weigandd5ebc622013-07-03 17:05:42 +0000362 MI.getOpcode() != PPC::MFOCRF && MI.getOpcode() != PPC::MFOCRF8) ||
Chris Lattner73716a62010-11-16 00:55:51 +0000363 MO.getReg() < PPC::CR0 || MO.getReg() > PPC::CR7);
Bill Wendlingbc07a892013-06-18 07:20:20 +0000364 return CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000365 }
Chris Lattnerc877d8f2010-11-15 04:51:55 +0000366
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000367 assert(MO.isImm() &&
368 "Relocation required in an instruction that we cannot encode!");
369 return MO.getImm();
Chris Lattner9ec375c2010-11-15 04:16:32 +0000370}
371
372
373#include "PPCGenMCCodeEmitter.inc"