blob: c7c65ca22c324b1d4d0529865556e229f82115ac [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"
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"
Bill Schmidtc763c222013-09-16 17:25:12 +000026#include "llvm/Target/TargetOpcodes.h"
Chris Lattner9ec375c2010-11-15 04:16:32 +000027using namespace llvm;
28
Chandler Carruth84e68b22014-04-22 02:41:26 +000029#define DEBUG_TYPE "mccodeemitter"
30
Chris Lattner9ec375c2010-11-15 04:16:32 +000031STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
32
33namespace {
34class PPCMCCodeEmitter : public MCCodeEmitter {
Aaron Ballmanf9a18972015-02-15 22:54:22 +000035 PPCMCCodeEmitter(const PPCMCCodeEmitter &) = delete;
36 void operator=(const PPCMCCodeEmitter &) = delete;
Craig Toppera60c0f12012-09-15 17:09:36 +000037
Hal Finkela7bbaf62014-02-02 06:12:27 +000038 const MCInstrInfo &MCII;
Hal Finkelfeea6532013-03-26 20:08:20 +000039 const MCContext &CTX;
Ulrich Weigandcae3a172014-03-24 18:16:09 +000040 bool IsLittleEndian;
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +000041
Chris Lattner9ec375c2010-11-15 04:16:32 +000042public:
Eric Christopher0169e422015-03-10 22:03:14 +000043 PPCMCCodeEmitter(const MCInstrInfo &mcii, MCContext &ctx)
44 : MCII(mcii), CTX(ctx),
45 IsLittleEndian(ctx.getAsmInfo()->isLittleEndian()) {}
46
Alexander Kornienkof817c1c2015-04-11 02:11:45 +000047 ~PPCMCCodeEmitter() override {}
Chris Lattnerd6a07cc2010-11-15 05:19:25 +000048
Chris Lattner0e3461e2010-11-15 06:09:35 +000049 unsigned getDirectBrEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +000050 SmallVectorImpl<MCFixup> &Fixups,
51 const MCSubtargetInfo &STI) const;
Chris Lattner0e3461e2010-11-15 06:09:35 +000052 unsigned getCondBrEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +000053 SmallVectorImpl<MCFixup> &Fixups,
54 const MCSubtargetInfo &STI) const;
Ulrich Weigandb6a30d12013-06-24 11:03:33 +000055 unsigned getAbsDirectBrEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +000056 SmallVectorImpl<MCFixup> &Fixups,
57 const MCSubtargetInfo &STI) const;
Ulrich Weigandb6a30d12013-06-24 11:03:33 +000058 unsigned getAbsCondBrEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +000059 SmallVectorImpl<MCFixup> &Fixups,
60 const MCSubtargetInfo &STI) const;
Ulrich Weigandfd3ad692013-06-26 13:49:15 +000061 unsigned getImm16Encoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +000062 SmallVectorImpl<MCFixup> &Fixups,
63 const MCSubtargetInfo &STI) const;
Chris Lattnerefacb9e2010-11-15 08:22:03 +000064 unsigned getMemRIEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +000065 SmallVectorImpl<MCFixup> &Fixups,
66 const MCSubtargetInfo &STI) const;
Chris Lattner8f4444d2010-11-15 08:02:41 +000067 unsigned getMemRIXEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +000068 SmallVectorImpl<MCFixup> &Fixups,
69 const MCSubtargetInfo &STI) const;
Joerg Sonnenberger0013b922014-08-08 16:43:49 +000070 unsigned getSPE8DisEncoding(const MCInst &MI, unsigned OpNo,
71 SmallVectorImpl<MCFixup> &Fixups,
72 const MCSubtargetInfo &STI) const;
73 unsigned getSPE4DisEncoding(const MCInst &MI, unsigned OpNo,
74 SmallVectorImpl<MCFixup> &Fixups,
75 const MCSubtargetInfo &STI) const;
76 unsigned getSPE2DisEncoding(const MCInst &MI, unsigned OpNo,
77 SmallVectorImpl<MCFixup> &Fixups,
78 const MCSubtargetInfo &STI) const;
Bill Schmidtca4a0c92012-12-04 16:18:08 +000079 unsigned getTLSRegEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +000080 SmallVectorImpl<MCFixup> &Fixups,
81 const MCSubtargetInfo &STI) const;
Ulrich Weigand5143bab2013-07-02 21:31:04 +000082 unsigned getTLSCallEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +000083 SmallVectorImpl<MCFixup> &Fixups,
84 const MCSubtargetInfo &STI) const;
Chris Lattnerd6a07cc2010-11-15 05:19:25 +000085 unsigned get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +000086 SmallVectorImpl<MCFixup> &Fixups,
87 const MCSubtargetInfo &STI) const;
Chris Lattnerd6a07cc2010-11-15 05:19:25 +000088
Chris Lattner9ec375c2010-11-15 04:16:32 +000089 /// getMachineOpValue - Return binary encoding of operand. If the machine
90 /// operand requires relocation, record the relocation and return zero.
91 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
David Woodhouse3fa98a62014-01-28 23:13:18 +000092 SmallVectorImpl<MCFixup> &Fixups,
93 const MCSubtargetInfo &STI) const;
Chris Lattner9ec375c2010-11-15 04:16:32 +000094
95 // getBinaryCodeForInstr - TableGen'erated function for getting the
96 // binary encoding for an instruction.
Owen Andersond845d9d2012-01-24 18:37:29 +000097 uint64_t getBinaryCodeForInstr(const MCInst &MI,
David Woodhouse3fa98a62014-01-28 23:13:18 +000098 SmallVectorImpl<MCFixup> &Fixups,
99 const MCSubtargetInfo &STI) const;
Jim Grosbach91df21f2015-05-15 19:13:16 +0000100 void encodeInstruction(const MCInst &MI, raw_ostream &OS,
David Woodhouse9784cef2014-01-28 23:13:07 +0000101 SmallVectorImpl<MCFixup> &Fixups,
Craig Topper0d3fa922014-04-29 07:57:37 +0000102 const MCSubtargetInfo &STI) const override {
Bill Schmidtc763c222013-09-16 17:25:12 +0000103 // For fast-isel, a float COPY_TO_REGCLASS can survive this long.
104 // It's just a nop to keep the register classes happy, so don't
105 // generate anything.
106 unsigned Opcode = MI.getOpcode();
Hal Finkela7bbaf62014-02-02 06:12:27 +0000107 const MCInstrDesc &Desc = MCII.get(Opcode);
Bill Schmidtc763c222013-09-16 17:25:12 +0000108 if (Opcode == TargetOpcode::COPY_TO_REGCLASS)
109 return;
110
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) {
116 case 4:
117 if (IsLittleEndian) {
118 OS << (char)(Bits);
119 OS << (char)(Bits >> 8);
120 OS << (char)(Bits >> 16);
121 OS << (char)(Bits >> 24);
122 } else {
123 OS << (char)(Bits >> 24);
124 OS << (char)(Bits >> 16);
125 OS << (char)(Bits >> 8);
126 OS << (char)(Bits);
Ulrich Weigandcae3a172014-03-24 18:16:09 +0000127 }
Ulrich Weigand7c3f0dc2014-06-18 15:37:07 +0000128 break;
129 case 8:
130 // If we emit a pair of instructions, the first one is
131 // always in the top 32 bits, even on little-endian.
132 if (IsLittleEndian) {
133 OS << (char)(Bits >> 32);
134 OS << (char)(Bits >> 40);
135 OS << (char)(Bits >> 48);
136 OS << (char)(Bits >> 56);
137 OS << (char)(Bits);
138 OS << (char)(Bits >> 8);
139 OS << (char)(Bits >> 16);
140 OS << (char)(Bits >> 24);
141 } else {
142 OS << (char)(Bits >> 56);
143 OS << (char)(Bits >> 48);
144 OS << (char)(Bits >> 40);
145 OS << (char)(Bits >> 32);
146 OS << (char)(Bits >> 24);
147 OS << (char)(Bits >> 16);
148 OS << (char)(Bits >> 8);
149 OS << (char)(Bits);
Ulrich Weigandcae3a172014-03-24 18:16:09 +0000150 }
Ulrich Weigand7c3f0dc2014-06-18 15:37:07 +0000151 break;
152 default:
153 llvm_unreachable ("Invalid instruction size");
Chris Lattner9ec375c2010-11-15 04:16:32 +0000154 }
155
156 ++MCNumEmitted; // Keep track of the # of mi's emitted.
157 }
158
159};
160
161} // end anonymous namespace
Eric Christopher0169e422015-03-10 22:03:14 +0000162
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000163MCCodeEmitter *llvm::createPPCMCCodeEmitter(const MCInstrInfo &MCII,
Jim Grosbachc3b04272012-05-15 17:35:52 +0000164 const MCRegisterInfo &MRI,
Chris Lattner9ec375c2010-11-15 04:16:32 +0000165 MCContext &Ctx) {
Eric Christopher0169e422015-03-10 22:03:14 +0000166 return new PPCMCCodeEmitter(MCII, Ctx);
Chris Lattner9ec375c2010-11-15 04:16:32 +0000167}
168
169unsigned PPCMCCodeEmitter::
Chris Lattner0e3461e2010-11-15 06:09:35 +0000170getDirectBrEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000171 SmallVectorImpl<MCFixup> &Fixups,
172 const MCSubtargetInfo &STI) const {
Chris Lattner79fa3712010-11-15 05:57:53 +0000173 const MCOperand &MO = MI.getOperand(OpNo);
David Woodhouse3fa98a62014-01-28 23:13:18 +0000174 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI);
Chris Lattner79fa3712010-11-15 05:57:53 +0000175
176 // Add a fixup for the branch target.
Jim Grosbach63661f82015-05-15 19:13:05 +0000177 Fixups.push_back(MCFixup::create(0, MO.getExpr(),
Chris Lattner79fa3712010-11-15 05:57:53 +0000178 (MCFixupKind)PPC::fixup_ppc_br24));
179 return 0;
180}
181
Chris Lattner0e3461e2010-11-15 06:09:35 +0000182unsigned PPCMCCodeEmitter::getCondBrEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000183 SmallVectorImpl<MCFixup> &Fixups,
184 const MCSubtargetInfo &STI) const {
Chris Lattner0e3461e2010-11-15 06:09:35 +0000185 const MCOperand &MO = MI.getOperand(OpNo);
David Woodhouse3fa98a62014-01-28 23:13:18 +0000186 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI);
Chris Lattner0e3461e2010-11-15 06:09:35 +0000187
Chris Lattner85e37682010-11-15 06:12:22 +0000188 // Add a fixup for the branch target.
Jim Grosbach63661f82015-05-15 19:13:05 +0000189 Fixups.push_back(MCFixup::create(0, MO.getExpr(),
Chris Lattner85e37682010-11-15 06:12:22 +0000190 (MCFixupKind)PPC::fixup_ppc_brcond14));
Chris Lattner0e3461e2010-11-15 06:09:35 +0000191 return 0;
192}
193
Ulrich Weigandb6a30d12013-06-24 11:03:33 +0000194unsigned PPCMCCodeEmitter::
195getAbsDirectBrEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000196 SmallVectorImpl<MCFixup> &Fixups,
197 const MCSubtargetInfo &STI) const {
Ulrich Weigandb6a30d12013-06-24 11:03:33 +0000198 const MCOperand &MO = MI.getOperand(OpNo);
David Woodhouse3fa98a62014-01-28 23:13:18 +0000199 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI);
Ulrich Weigandb6a30d12013-06-24 11:03:33 +0000200
201 // Add a fixup for the branch target.
Jim Grosbach63661f82015-05-15 19:13:05 +0000202 Fixups.push_back(MCFixup::create(0, MO.getExpr(),
Ulrich Weigandb6a30d12013-06-24 11:03:33 +0000203 (MCFixupKind)PPC::fixup_ppc_br24abs));
204 return 0;
205}
206
207unsigned PPCMCCodeEmitter::
208getAbsCondBrEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000209 SmallVectorImpl<MCFixup> &Fixups,
210 const MCSubtargetInfo &STI) const {
Ulrich Weigandb6a30d12013-06-24 11:03:33 +0000211 const MCOperand &MO = MI.getOperand(OpNo);
David Woodhouse3fa98a62014-01-28 23:13:18 +0000212 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI);
Ulrich Weigandb6a30d12013-06-24 11:03:33 +0000213
214 // Add a fixup for the branch target.
Jim Grosbach63661f82015-05-15 19:13:05 +0000215 Fixups.push_back(MCFixup::create(0, MO.getExpr(),
Ulrich Weigandb6a30d12013-06-24 11:03:33 +0000216 (MCFixupKind)PPC::fixup_ppc_brcond14abs));
217 return 0;
218}
219
Ulrich Weigandfd3ad692013-06-26 13:49:15 +0000220unsigned PPCMCCodeEmitter::getImm16Encoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000221 SmallVectorImpl<MCFixup> &Fixups,
222 const MCSubtargetInfo &STI) const {
Chris Lattner65661122010-11-15 06:33:39 +0000223 const MCOperand &MO = MI.getOperand(OpNo);
David Woodhouse3fa98a62014-01-28 23:13:18 +0000224 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups, STI);
Chris Lattner65661122010-11-15 06:33:39 +0000225
Ulrich Weigandfd3ad692013-06-26 13:49:15 +0000226 // Add a fixup for the immediate field.
Jim Grosbach63661f82015-05-15 19:13:05 +0000227 Fixups.push_back(MCFixup::create(IsLittleEndian? 0 : 2, MO.getExpr(),
Ulrich Weigand6e23ac62013-05-17 12:37:21 +0000228 (MCFixupKind)PPC::fixup_ppc_half16));
Chris Lattner65661122010-11-15 06:33:39 +0000229 return 0;
230}
231
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000232unsigned PPCMCCodeEmitter::getMemRIEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000233 SmallVectorImpl<MCFixup> &Fixups,
234 const MCSubtargetInfo &STI) const {
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000235 // Encode (imm, reg) as a memri, which has the low 16-bits as the
236 // displacement and the next 5 bits as the register #.
237 assert(MI.getOperand(OpNo+1).isReg());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000238 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 16;
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000239
240 const MCOperand &MO = MI.getOperand(OpNo);
241 if (MO.isImm())
David Woodhouse3fa98a62014-01-28 23:13:18 +0000242 return (getMachineOpValue(MI, MO, Fixups, STI) & 0xFFFF) | RegBits;
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000243
244 // Add a fixup for the displacement field.
Jim Grosbach63661f82015-05-15 19:13:05 +0000245 Fixups.push_back(MCFixup::create(IsLittleEndian? 0 : 2, MO.getExpr(),
Ulrich Weigand6e23ac62013-05-17 12:37:21 +0000246 (MCFixupKind)PPC::fixup_ppc_half16));
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000247 return RegBits;
248}
249
250
Chris Lattner8f4444d2010-11-15 08:02:41 +0000251unsigned PPCMCCodeEmitter::getMemRIXEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000252 SmallVectorImpl<MCFixup> &Fixups,
253 const MCSubtargetInfo &STI) const {
Chris Lattner8f4444d2010-11-15 08:02:41 +0000254 // Encode (imm, reg) as a memrix, which has the low 14-bits as the
255 // displacement and the next 5 bits as the register #.
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000256 assert(MI.getOperand(OpNo+1).isReg());
David Woodhouse3fa98a62014-01-28 23:13:18 +0000257 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 14;
Chris Lattner8f4444d2010-11-15 08:02:41 +0000258
Chris Lattner65661122010-11-15 06:33:39 +0000259 const MCOperand &MO = MI.getOperand(OpNo);
Chris Lattner8f4444d2010-11-15 08:02:41 +0000260 if (MO.isImm())
David Woodhouse3fa98a62014-01-28 23:13:18 +0000261 return ((getMachineOpValue(MI, MO, Fixups, STI) >> 2) & 0x3FFF) | RegBits;
Chris Lattner65661122010-11-15 06:33:39 +0000262
Ulrich Weigand3e186012013-03-26 10:56:47 +0000263 // Add a fixup for the displacement field.
Jim Grosbach63661f82015-05-15 19:13:05 +0000264 Fixups.push_back(MCFixup::create(IsLittleEndian? 0 : 2, MO.getExpr(),
Ulrich Weigand6e23ac62013-05-17 12:37:21 +0000265 (MCFixupKind)PPC::fixup_ppc_half16ds));
Chris Lattner8f4444d2010-11-15 08:02:41 +0000266 return RegBits;
Chris Lattner65661122010-11-15 06:33:39 +0000267}
268
Chris Lattner0e3461e2010-11-15 06:09:35 +0000269
Joerg Sonnenberger0013b922014-08-08 16:43:49 +0000270unsigned PPCMCCodeEmitter::getSPE8DisEncoding(const MCInst &MI, unsigned OpNo,
271 SmallVectorImpl<MCFixup> &Fixups,
272 const MCSubtargetInfo &STI)
273 const {
274 // Encode (imm, reg) as a spe8dis, which has the low 5-bits of (imm / 8)
275 // as the displacement and the next 5 bits as the register #.
276 assert(MI.getOperand(OpNo+1).isReg());
277 uint32_t RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 5;
278
279 const MCOperand &MO = MI.getOperand(OpNo);
280 assert(MO.isImm());
281 uint32_t Imm = getMachineOpValue(MI, MO, Fixups, STI) >> 3;
282 return reverseBits(Imm | RegBits) >> 22;
283}
284
285
286unsigned PPCMCCodeEmitter::getSPE4DisEncoding(const MCInst &MI, unsigned OpNo,
287 SmallVectorImpl<MCFixup> &Fixups,
288 const MCSubtargetInfo &STI)
289 const {
290 // Encode (imm, reg) as a spe4dis, which has the low 5-bits of (imm / 4)
291 // as the displacement and the next 5 bits as the register #.
292 assert(MI.getOperand(OpNo+1).isReg());
293 uint32_t RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 5;
294
295 const MCOperand &MO = MI.getOperand(OpNo);
296 assert(MO.isImm());
297 uint32_t Imm = getMachineOpValue(MI, MO, Fixups, STI) >> 2;
298 return reverseBits(Imm | RegBits) >> 22;
299}
300
301
302unsigned PPCMCCodeEmitter::getSPE2DisEncoding(const MCInst &MI, unsigned OpNo,
303 SmallVectorImpl<MCFixup> &Fixups,
304 const MCSubtargetInfo &STI)
305 const {
306 // Encode (imm, reg) as a spe2dis, which has the low 5-bits of (imm / 2)
307 // as the displacement and the next 5 bits as the register #.
308 assert(MI.getOperand(OpNo+1).isReg());
309 uint32_t RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups, STI) << 5;
310
311 const MCOperand &MO = MI.getOperand(OpNo);
312 assert(MO.isImm());
313 uint32_t Imm = getMachineOpValue(MI, MO, Fixups, STI) >> 1;
314 return reverseBits(Imm | RegBits) >> 22;
315}
316
317
Bill Schmidtca4a0c92012-12-04 16:18:08 +0000318unsigned PPCMCCodeEmitter::getTLSRegEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000319 SmallVectorImpl<MCFixup> &Fixups,
320 const MCSubtargetInfo &STI) const {
Bill Schmidtca4a0c92012-12-04 16:18:08 +0000321 const MCOperand &MO = MI.getOperand(OpNo);
David Woodhouse3fa98a62014-01-28 23:13:18 +0000322 if (MO.isReg()) return getMachineOpValue(MI, MO, Fixups, STI);
Bill Schmidtca4a0c92012-12-04 16:18:08 +0000323
324 // Add a fixup for the TLS register, which simply provides a relocation
325 // hint to the linker that this statement is part of a relocation sequence.
326 // Return the thread-pointer register's encoding.
Jim Grosbach63661f82015-05-15 19:13:05 +0000327 Fixups.push_back(MCFixup::create(0, MO.getExpr(),
Ulrich Weigand5b427592013-07-05 12:22:36 +0000328 (MCFixupKind)PPC::fixup_ppc_nofixup));
David Woodhoused2cca112014-01-28 23:13:25 +0000329 Triple TT(STI.getTargetTriple());
Roman Divackybc1655b42013-12-22 10:45:37 +0000330 bool isPPC64 = TT.getArch() == Triple::ppc64 || TT.getArch() == Triple::ppc64le;
331 return CTX.getRegisterInfo()->getEncodingValue(isPPC64 ? PPC::X13 : PPC::R2);
Bill Schmidtca4a0c92012-12-04 16:18:08 +0000332}
333
Ulrich Weigand5143bab2013-07-02 21:31:04 +0000334unsigned PPCMCCodeEmitter::getTLSCallEncoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000335 SmallVectorImpl<MCFixup> &Fixups,
336 const MCSubtargetInfo &STI) const {
Ulrich Weigand5143bab2013-07-02 21:31:04 +0000337 // For special TLS calls, we need two fixups; one for the branch target
338 // (__tls_get_addr), which we create via getDirectBrEncoding as usual,
339 // and one for the TLSGD or TLSLD symbol, which is emitted here.
340 const MCOperand &MO = MI.getOperand(OpNo+1);
Jim Grosbach63661f82015-05-15 19:13:05 +0000341 Fixups.push_back(MCFixup::create(0, MO.getExpr(),
Ulrich Weigand5143bab2013-07-02 21:31:04 +0000342 (MCFixupKind)PPC::fixup_ppc_nofixup));
David Woodhouse3fa98a62014-01-28 23:13:18 +0000343 return getDirectBrEncoding(MI, OpNo, Fixups, STI);
Ulrich Weigand5143bab2013-07-02 21:31:04 +0000344}
345
Chris Lattner79fa3712010-11-15 05:57:53 +0000346unsigned PPCMCCodeEmitter::
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000347get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000348 SmallVectorImpl<MCFixup> &Fixups,
349 const MCSubtargetInfo &STI) const {
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000350 const MCOperand &MO = MI.getOperand(OpNo);
Ulrich Weigand49f487e2013-07-03 17:59:07 +0000351 assert((MI.getOpcode() == PPC::MTOCRF || MI.getOpcode() == PPC::MTOCRF8 ||
Ulrich Weigandd5ebc622013-07-03 17:05:42 +0000352 MI.getOpcode() == PPC::MFOCRF || MI.getOpcode() == PPC::MFOCRF8) &&
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000353 (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7));
Bill Wendlingbc07a892013-06-18 07:20:20 +0000354 return 0x80 >> CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000355}
356
357
358unsigned PPCMCCodeEmitter::
Chris Lattner9ec375c2010-11-15 04:16:32 +0000359getMachineOpValue(const MCInst &MI, const MCOperand &MO,
David Woodhouse3fa98a62014-01-28 23:13:18 +0000360 SmallVectorImpl<MCFixup> &Fixups,
361 const MCSubtargetInfo &STI) const {
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000362 if (MO.isReg()) {
Ulrich Weigand49f487e2013-07-03 17:59:07 +0000363 // MTOCRF/MFOCRF should go through get_crbitm_encoding for the CR operand.
Chris Lattner7b25d6f2010-11-16 00:57:32 +0000364 // The GPR operand should come through here though.
Ulrich Weigand49f487e2013-07-03 17:59:07 +0000365 assert((MI.getOpcode() != PPC::MTOCRF && MI.getOpcode() != PPC::MTOCRF8 &&
Ulrich Weigandd5ebc622013-07-03 17:05:42 +0000366 MI.getOpcode() != PPC::MFOCRF && MI.getOpcode() != PPC::MFOCRF8) ||
Chris Lattner73716a62010-11-16 00:55:51 +0000367 MO.getReg() < PPC::CR0 || MO.getReg() > PPC::CR7);
Bill Wendlingbc07a892013-06-18 07:20:20 +0000368 return CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000369 }
Chris Lattnerc877d8f2010-11-15 04:51:55 +0000370
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000371 assert(MO.isImm() &&
372 "Relocation required in an instruction that we cannot encode!");
373 return MO.getImm();
Chris Lattner9ec375c2010-11-15 04:16:32 +0000374}
375
376
377#include "PPCGenMCCodeEmitter.inc"