blob: df2cf8f01001670f5263b0172acf7a88b13f41d9 [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/PPCBaseInfo.h"
17#include "MCTargetDesc/PPCFixupKinds.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/ADT/Statistic.h"
Chris Lattner9ec375c2010-11-15 04:16:32 +000019#include "llvm/MC/MCCodeEmitter.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;
36 Triple TT;
37
Chris Lattner9ec375c2010-11-15 04:16:32 +000038public:
Evan Chengc5e6d2f2011-07-11 03:57:24 +000039 PPCMCCodeEmitter(const MCInstrInfo &mcii, const MCSubtargetInfo &sti,
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +000040 MCContext &ctx)
41 : STI(sti), TT(STI.getTargetTriple()) {
Chris Lattner9ec375c2010-11-15 04:16:32 +000042 }
43
44 ~PPCMCCodeEmitter() {}
Chris Lattnerd6a07cc2010-11-15 05:19:25 +000045
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +000046 bool is64BitMode() const {
47 return (STI.getFeatureBits() & PPC::Feature64Bit) != 0;
48 }
49
50 bool isSVR4ABI() const {
51 return TT.isMacOSX() == 0;
52 }
53
Chris Lattner0e3461e2010-11-15 06:09:35 +000054 unsigned getDirectBrEncoding(const MCInst &MI, unsigned OpNo,
55 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattner0e3461e2010-11-15 06:09:35 +000056 unsigned getCondBrEncoding(const MCInst &MI, unsigned OpNo,
57 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattner65661122010-11-15 06:33:39 +000058 unsigned getHA16Encoding(const MCInst &MI, unsigned OpNo,
59 SmallVectorImpl<MCFixup> &Fixups) const;
60 unsigned getLO16Encoding(const MCInst &MI, unsigned OpNo,
61 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattnerefacb9e2010-11-15 08:22:03 +000062 unsigned getMemRIEncoding(const MCInst &MI, unsigned OpNo,
63 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattner8f4444d2010-11-15 08:02:41 +000064 unsigned getMemRIXEncoding(const MCInst &MI, unsigned OpNo,
65 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Schmidtca4a0c92012-12-04 16:18:08 +000066 unsigned getTLSRegEncoding(const MCInst &MI, unsigned OpNo,
67 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattnerd6a07cc2010-11-15 05:19:25 +000068 unsigned get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
69 SmallVectorImpl<MCFixup> &Fixups) const;
70
Chris Lattner9ec375c2010-11-15 04:16:32 +000071 /// getMachineOpValue - Return binary encoding of operand. If the machine
72 /// operand requires relocation, record the relocation and return zero.
73 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
74 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattner9ec375c2010-11-15 04:16:32 +000075
76 // getBinaryCodeForInstr - TableGen'erated function for getting the
77 // binary encoding for an instruction.
Owen Andersond845d9d2012-01-24 18:37:29 +000078 uint64_t getBinaryCodeForInstr(const MCInst &MI,
Chris Lattner9ec375c2010-11-15 04:16:32 +000079 SmallVectorImpl<MCFixup> &Fixups) const;
80 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
81 SmallVectorImpl<MCFixup> &Fixups) const {
Adhemerval Zanella1be10dc2012-10-25 14:29:13 +000082 uint64_t Bits = getBinaryCodeForInstr(MI, Fixups);
83
Ulrich Weigandf62e83f2013-03-22 15:24:13 +000084 // BL8_NOP etc. all have a size of 8 because of the following 'nop'.
Adhemerval Zanella1be10dc2012-10-25 14:29:13 +000085 unsigned Size = 4; // FIXME: Have Desc.getSize() return the correct value!
86 unsigned Opcode = MI.getOpcode();
Ulrich Weigandf62e83f2013-03-22 15:24:13 +000087 if (Opcode == PPC::BL8_NOP || Opcode == PPC::BLA8_NOP ||
88 Opcode == PPC::BL8_NOP_TLSGD || Opcode == PPC::BL8_NOP_TLSLD)
Adhemerval Zanella1be10dc2012-10-25 14:29:13 +000089 Size = 8;
Chris Lattner9ec375c2010-11-15 04:16:32 +000090
91 // Output the constant in big endian byte order.
Adhemerval Zanella1be10dc2012-10-25 14:29:13 +000092 int ShiftValue = (Size * 8) - 8;
93 for (unsigned i = 0; i != Size; ++i) {
94 OS << (char)(Bits >> ShiftValue);
Chris Lattner9ec375c2010-11-15 04:16:32 +000095 Bits <<= 8;
96 }
97
98 ++MCNumEmitted; // Keep track of the # of mi's emitted.
99 }
100
101};
102
103} // end anonymous namespace
104
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000105MCCodeEmitter *llvm::createPPCMCCodeEmitter(const MCInstrInfo &MCII,
Jim Grosbachc3b04272012-05-15 17:35:52 +0000106 const MCRegisterInfo &MRI,
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000107 const MCSubtargetInfo &STI,
Chris Lattner9ec375c2010-11-15 04:16:32 +0000108 MCContext &Ctx) {
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000109 return new PPCMCCodeEmitter(MCII, STI, Ctx);
Chris Lattner9ec375c2010-11-15 04:16:32 +0000110}
111
112unsigned PPCMCCodeEmitter::
Chris Lattner0e3461e2010-11-15 06:09:35 +0000113getDirectBrEncoding(const MCInst &MI, unsigned OpNo,
114 SmallVectorImpl<MCFixup> &Fixups) const {
Chris Lattner79fa3712010-11-15 05:57:53 +0000115 const MCOperand &MO = MI.getOperand(OpNo);
116 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
117
118 // Add a fixup for the branch target.
119 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
120 (MCFixupKind)PPC::fixup_ppc_br24));
Bill Schmidtc56f1d32012-12-11 20:30:11 +0000121
122 // For special TLS calls, add another fixup for the symbol. Apparently
Ulrich Weigandf62e83f2013-03-22 15:24:13 +0000123 // BL8_NOP, BL8_NOP_TLSGD, and BL8_NOP_TLSLD are sufficiently
Bill Schmidt24b8dd62012-12-12 19:29:35 +0000124 // similar that TblGen will not generate a separate case for the latter
125 // two, so this is the only way to get the extra fixup generated.
126 unsigned Opcode = MI.getOpcode();
Ulrich Weigandf62e83f2013-03-22 15:24:13 +0000127 if (Opcode == PPC::BL8_NOP_TLSGD || Opcode == PPC::BL8_NOP_TLSLD) {
Bill Schmidtc56f1d32012-12-11 20:30:11 +0000128 const MCOperand &MO2 = MI.getOperand(OpNo+1);
129 Fixups.push_back(MCFixup::Create(0, MO2.getExpr(),
Bill Schmidt24b8dd62012-12-12 19:29:35 +0000130 (MCFixupKind)PPC::fixup_ppc_nofixup));
Bill Schmidtc56f1d32012-12-11 20:30:11 +0000131 }
Chris Lattner79fa3712010-11-15 05:57:53 +0000132 return 0;
133}
134
Chris Lattner0e3461e2010-11-15 06:09:35 +0000135unsigned PPCMCCodeEmitter::getCondBrEncoding(const MCInst &MI, unsigned OpNo,
136 SmallVectorImpl<MCFixup> &Fixups) const {
137 const MCOperand &MO = MI.getOperand(OpNo);
138 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
139
Chris Lattner85e37682010-11-15 06:12:22 +0000140 // Add a fixup for the branch target.
141 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
142 (MCFixupKind)PPC::fixup_ppc_brcond14));
Chris Lattner0e3461e2010-11-15 06:09:35 +0000143 return 0;
144}
145
Chris Lattner65661122010-11-15 06:33:39 +0000146unsigned PPCMCCodeEmitter::getHA16Encoding(const MCInst &MI, unsigned OpNo,
147 SmallVectorImpl<MCFixup> &Fixups) const {
148 const MCOperand &MO = MI.getOperand(OpNo);
149 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
150
151 // Add a fixup for the branch target.
152 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
153 (MCFixupKind)PPC::fixup_ppc_ha16));
154 return 0;
155}
156
157unsigned PPCMCCodeEmitter::getLO16Encoding(const MCInst &MI, unsigned OpNo,
158 SmallVectorImpl<MCFixup> &Fixups) const {
159 const MCOperand &MO = MI.getOperand(OpNo);
160 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
161
162 // Add a fixup for the branch target.
163 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
164 (MCFixupKind)PPC::fixup_ppc_lo16));
165 return 0;
166}
167
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000168unsigned PPCMCCodeEmitter::getMemRIEncoding(const MCInst &MI, unsigned OpNo,
169 SmallVectorImpl<MCFixup> &Fixups) const {
170 // Encode (imm, reg) as a memri, which has the low 16-bits as the
171 // displacement and the next 5 bits as the register #.
172 assert(MI.getOperand(OpNo+1).isReg());
173 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups) << 16;
174
175 const MCOperand &MO = MI.getOperand(OpNo);
176 if (MO.isImm())
177 return (getMachineOpValue(MI, MO, Fixups) & 0xFFFF) | RegBits;
178
179 // Add a fixup for the displacement field.
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +0000180 if (isSVR4ABI() && is64BitMode())
181 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
182 (MCFixupKind)PPC::fixup_ppc_toc16));
183 else
184 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
185 (MCFixupKind)PPC::fixup_ppc_lo16));
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000186 return RegBits;
187}
188
189
Chris Lattner8f4444d2010-11-15 08:02:41 +0000190unsigned PPCMCCodeEmitter::getMemRIXEncoding(const MCInst &MI, unsigned OpNo,
Chris Lattner65661122010-11-15 06:33:39 +0000191 SmallVectorImpl<MCFixup> &Fixups) const {
Chris Lattner8f4444d2010-11-15 08:02:41 +0000192 // Encode (imm, reg) as a memrix, which has the low 14-bits as the
193 // displacement and the next 5 bits as the register #.
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000194 assert(MI.getOperand(OpNo+1).isReg());
Chris Lattner8f4444d2010-11-15 08:02:41 +0000195 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups) << 14;
196
Chris Lattner65661122010-11-15 06:33:39 +0000197 const MCOperand &MO = MI.getOperand(OpNo);
Chris Lattner8f4444d2010-11-15 08:02:41 +0000198 if (MO.isImm())
199 return (getMachineOpValue(MI, MO, Fixups) & 0x3FFF) | RegBits;
Chris Lattner65661122010-11-15 06:33:39 +0000200
201 // Add a fixup for the branch target.
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +0000202 if (isSVR4ABI() && is64BitMode())
203 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
204 (MCFixupKind)PPC::fixup_ppc_toc16_ds));
205 else
206 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
207 (MCFixupKind)PPC::fixup_ppc_lo14));
Chris Lattner8f4444d2010-11-15 08:02:41 +0000208 return RegBits;
Chris Lattner65661122010-11-15 06:33:39 +0000209}
210
Chris Lattner0e3461e2010-11-15 06:09:35 +0000211
Bill Schmidtca4a0c92012-12-04 16:18:08 +0000212unsigned PPCMCCodeEmitter::getTLSRegEncoding(const MCInst &MI, unsigned OpNo,
213 SmallVectorImpl<MCFixup> &Fixups) const {
214 const MCOperand &MO = MI.getOperand(OpNo);
215 if (MO.isReg()) return getMachineOpValue(MI, MO, Fixups);
216
217 // Add a fixup for the TLS register, which simply provides a relocation
218 // hint to the linker that this statement is part of a relocation sequence.
219 // Return the thread-pointer register's encoding.
220 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
221 (MCFixupKind)PPC::fixup_ppc_tlsreg));
222 return getPPCRegisterNumbering(PPC::X13);
223}
224
Chris Lattner79fa3712010-11-15 05:57:53 +0000225unsigned PPCMCCodeEmitter::
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000226get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
227 SmallVectorImpl<MCFixup> &Fixups) const {
228 const MCOperand &MO = MI.getOperand(OpNo);
Adhemerval Zanella22b9fd22012-10-08 18:25:11 +0000229 assert((MI.getOpcode() == PPC::MTCRF ||
230 MI.getOpcode() == PPC::MFOCRF ||
231 MI.getOpcode() == PPC::MTCRF8) &&
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000232 (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7));
Evan Cheng61d4a202011-07-25 19:53:23 +0000233 return 0x80 >> getPPCRegisterNumbering(MO.getReg());
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000234}
235
236
237unsigned PPCMCCodeEmitter::
Chris Lattner9ec375c2010-11-15 04:16:32 +0000238getMachineOpValue(const MCInst &MI, const MCOperand &MO,
239 SmallVectorImpl<MCFixup> &Fixups) const {
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000240 if (MO.isReg()) {
Chris Lattner7b25d6f2010-11-16 00:57:32 +0000241 // MTCRF/MFOCRF should go through get_crbitm_encoding for the CR operand.
242 // The GPR operand should come through here though.
Chris Lattner73716a62010-11-16 00:55:51 +0000243 assert((MI.getOpcode() != PPC::MTCRF && MI.getOpcode() != PPC::MFOCRF) ||
244 MO.getReg() < PPC::CR0 || MO.getReg() > PPC::CR7);
Evan Cheng61d4a202011-07-25 19:53:23 +0000245 return getPPCRegisterNumbering(MO.getReg());
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000246 }
Chris Lattnerc877d8f2010-11-15 04:51:55 +0000247
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000248 assert(MO.isImm() &&
249 "Relocation required in an instruction that we cannot encode!");
250 return MO.getImm();
Chris Lattner9ec375c2010-11-15 04:16:32 +0000251}
252
253
254#include "PPCGenMCCodeEmitter.inc"