blob: 346a9beada90537dd4682ba2afdde15b07db0d23 [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"
Bill Schmidtc763c222013-09-16 17:25:12 +000026#include "llvm/Target/TargetOpcodes.h"
Chris Lattner9ec375c2010-11-15 04:16:32 +000027using namespace llvm;
28
29STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
30
31namespace {
32class PPCMCCodeEmitter : public MCCodeEmitter {
Craig Toppera60c0f12012-09-15 17:09:36 +000033 PPCMCCodeEmitter(const PPCMCCodeEmitter &) LLVM_DELETED_FUNCTION;
34 void operator=(const PPCMCCodeEmitter &) LLVM_DELETED_FUNCTION;
35
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +000036 const MCSubtargetInfo &STI;
Hal Finkelfeea6532013-03-26 20:08:20 +000037 const MCContext &CTX;
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +000038 Triple TT;
39
Chris Lattner9ec375c2010-11-15 04:16:32 +000040public:
Evan Chengc5e6d2f2011-07-11 03:57:24 +000041 PPCMCCodeEmitter(const MCInstrInfo &mcii, const MCSubtargetInfo &sti,
Adhemerval Zanellaf2aceda2012-10-25 12:27:42 +000042 MCContext &ctx)
Hal Finkelfeea6532013-03-26 20:08:20 +000043 : STI(sti), CTX(ctx), TT(STI.getTargetTriple()) {
Chris Lattner9ec375c2010-11-15 04:16:32 +000044 }
45
46 ~PPCMCCodeEmitter() {}
Chris Lattnerd6a07cc2010-11-15 05:19:25 +000047
Chris Lattner0e3461e2010-11-15 06:09:35 +000048 unsigned getDirectBrEncoding(const MCInst &MI, unsigned OpNo,
49 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattner0e3461e2010-11-15 06:09:35 +000050 unsigned getCondBrEncoding(const MCInst &MI, unsigned OpNo,
51 SmallVectorImpl<MCFixup> &Fixups) const;
Ulrich Weigandb6a30d12013-06-24 11:03:33 +000052 unsigned getAbsDirectBrEncoding(const MCInst &MI, unsigned OpNo,
53 SmallVectorImpl<MCFixup> &Fixups) const;
54 unsigned getAbsCondBrEncoding(const MCInst &MI, unsigned OpNo,
55 SmallVectorImpl<MCFixup> &Fixups) const;
Ulrich Weigandfd3ad692013-06-26 13:49:15 +000056 unsigned getImm16Encoding(const MCInst &MI, unsigned OpNo,
Ulrich Weigand2dbe06a2013-05-17 14:14:12 +000057 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattnerefacb9e2010-11-15 08:22:03 +000058 unsigned getMemRIEncoding(const MCInst &MI, unsigned OpNo,
59 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattner8f4444d2010-11-15 08:02:41 +000060 unsigned getMemRIXEncoding(const MCInst &MI, unsigned OpNo,
61 SmallVectorImpl<MCFixup> &Fixups) const;
Bill Schmidtca4a0c92012-12-04 16:18:08 +000062 unsigned getTLSRegEncoding(const MCInst &MI, unsigned OpNo,
63 SmallVectorImpl<MCFixup> &Fixups) const;
Ulrich Weigand5143bab2013-07-02 21:31:04 +000064 unsigned getTLSCallEncoding(const MCInst &MI, unsigned OpNo,
65 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattnerd6a07cc2010-11-15 05:19:25 +000066 unsigned get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
67 SmallVectorImpl<MCFixup> &Fixups) const;
68
Chris Lattner9ec375c2010-11-15 04:16:32 +000069 /// getMachineOpValue - Return binary encoding of operand. If the machine
70 /// operand requires relocation, record the relocation and return zero.
71 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
72 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattner9ec375c2010-11-15 04:16:32 +000073
74 // getBinaryCodeForInstr - TableGen'erated function for getting the
75 // binary encoding for an instruction.
Owen Andersond845d9d2012-01-24 18:37:29 +000076 uint64_t getBinaryCodeForInstr(const MCInst &MI,
Chris Lattner9ec375c2010-11-15 04:16:32 +000077 SmallVectorImpl<MCFixup> &Fixups) const;
78 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
79 SmallVectorImpl<MCFixup> &Fixups) const {
Bill Schmidtc763c222013-09-16 17:25:12 +000080 // For fast-isel, a float COPY_TO_REGCLASS can survive this long.
81 // It's just a nop to keep the register classes happy, so don't
82 // generate anything.
83 unsigned Opcode = MI.getOpcode();
84 if (Opcode == TargetOpcode::COPY_TO_REGCLASS)
85 return;
86
Adhemerval Zanella1be10dc2012-10-25 14:29:13 +000087 uint64_t Bits = getBinaryCodeForInstr(MI, Fixups);
88
Ulrich Weigandf62e83f2013-03-22 15:24:13 +000089 // BL8_NOP etc. all have a size of 8 because of the following 'nop'.
Adhemerval Zanella1be10dc2012-10-25 14:29:13 +000090 unsigned Size = 4; // FIXME: Have Desc.getSize() return the correct value!
Ulrich Weigandf62e83f2013-03-22 15:24:13 +000091 if (Opcode == PPC::BL8_NOP || Opcode == PPC::BLA8_NOP ||
Ulrich Weigand5143bab2013-07-02 21:31:04 +000092 Opcode == PPC::BL8_NOP_TLS)
Adhemerval Zanella1be10dc2012-10-25 14:29:13 +000093 Size = 8;
Chris Lattner9ec375c2010-11-15 04:16:32 +000094
95 // Output the constant in big endian byte order.
Adhemerval Zanella1be10dc2012-10-25 14:29:13 +000096 int ShiftValue = (Size * 8) - 8;
97 for (unsigned i = 0; i != Size; ++i) {
98 OS << (char)(Bits >> ShiftValue);
Chris Lattner9ec375c2010-11-15 04:16:32 +000099 Bits <<= 8;
100 }
101
102 ++MCNumEmitted; // Keep track of the # of mi's emitted.
103 }
104
105};
106
107} // end anonymous namespace
108
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000109MCCodeEmitter *llvm::createPPCMCCodeEmitter(const MCInstrInfo &MCII,
Jim Grosbachc3b04272012-05-15 17:35:52 +0000110 const MCRegisterInfo &MRI,
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000111 const MCSubtargetInfo &STI,
Chris Lattner9ec375c2010-11-15 04:16:32 +0000112 MCContext &Ctx) {
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000113 return new PPCMCCodeEmitter(MCII, STI, Ctx);
Chris Lattner9ec375c2010-11-15 04:16:32 +0000114}
115
116unsigned PPCMCCodeEmitter::
Chris Lattner0e3461e2010-11-15 06:09:35 +0000117getDirectBrEncoding(const MCInst &MI, unsigned OpNo,
118 SmallVectorImpl<MCFixup> &Fixups) const {
Chris Lattner79fa3712010-11-15 05:57:53 +0000119 const MCOperand &MO = MI.getOperand(OpNo);
120 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
121
122 // Add a fixup for the branch target.
123 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
124 (MCFixupKind)PPC::fixup_ppc_br24));
125 return 0;
126}
127
Chris Lattner0e3461e2010-11-15 06:09:35 +0000128unsigned PPCMCCodeEmitter::getCondBrEncoding(const MCInst &MI, unsigned OpNo,
129 SmallVectorImpl<MCFixup> &Fixups) const {
130 const MCOperand &MO = MI.getOperand(OpNo);
131 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
132
Chris Lattner85e37682010-11-15 06:12:22 +0000133 // Add a fixup for the branch target.
134 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
135 (MCFixupKind)PPC::fixup_ppc_brcond14));
Chris Lattner0e3461e2010-11-15 06:09:35 +0000136 return 0;
137}
138
Ulrich Weigandb6a30d12013-06-24 11:03:33 +0000139unsigned PPCMCCodeEmitter::
140getAbsDirectBrEncoding(const MCInst &MI, unsigned OpNo,
141 SmallVectorImpl<MCFixup> &Fixups) const {
142 const MCOperand &MO = MI.getOperand(OpNo);
143 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
144
145 // Add a fixup for the branch target.
146 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
147 (MCFixupKind)PPC::fixup_ppc_br24abs));
148 return 0;
149}
150
151unsigned PPCMCCodeEmitter::
152getAbsCondBrEncoding(const MCInst &MI, unsigned OpNo,
153 SmallVectorImpl<MCFixup> &Fixups) const {
154 const MCOperand &MO = MI.getOperand(OpNo);
155 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
156
157 // Add a fixup for the branch target.
158 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
159 (MCFixupKind)PPC::fixup_ppc_brcond14abs));
160 return 0;
161}
162
Ulrich Weigandfd3ad692013-06-26 13:49:15 +0000163unsigned PPCMCCodeEmitter::getImm16Encoding(const MCInst &MI, unsigned OpNo,
Chris Lattner65661122010-11-15 06:33:39 +0000164 SmallVectorImpl<MCFixup> &Fixups) const {
165 const MCOperand &MO = MI.getOperand(OpNo);
166 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
167
Ulrich Weigandfd3ad692013-06-26 13:49:15 +0000168 // Add a fixup for the immediate field.
Ulrich Weigand2fb140e2013-05-15 15:07:06 +0000169 Fixups.push_back(MCFixup::Create(2, MO.getExpr(),
Ulrich Weigand6e23ac62013-05-17 12:37:21 +0000170 (MCFixupKind)PPC::fixup_ppc_half16));
Chris Lattner65661122010-11-15 06:33:39 +0000171 return 0;
172}
173
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000174unsigned PPCMCCodeEmitter::getMemRIEncoding(const MCInst &MI, unsigned OpNo,
175 SmallVectorImpl<MCFixup> &Fixups) const {
176 // Encode (imm, reg) as a memri, which has the low 16-bits as the
177 // displacement and the next 5 bits as the register #.
178 assert(MI.getOperand(OpNo+1).isReg());
179 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups) << 16;
180
181 const MCOperand &MO = MI.getOperand(OpNo);
182 if (MO.isImm())
183 return (getMachineOpValue(MI, MO, Fixups) & 0xFFFF) | RegBits;
184
185 // Add a fixup for the displacement field.
Ulrich Weigand2fb140e2013-05-15 15:07:06 +0000186 Fixups.push_back(MCFixup::Create(2, MO.getExpr(),
Ulrich Weigand6e23ac62013-05-17 12:37:21 +0000187 (MCFixupKind)PPC::fixup_ppc_half16));
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000188 return RegBits;
189}
190
191
Chris Lattner8f4444d2010-11-15 08:02:41 +0000192unsigned PPCMCCodeEmitter::getMemRIXEncoding(const MCInst &MI, unsigned OpNo,
Chris Lattner65661122010-11-15 06:33:39 +0000193 SmallVectorImpl<MCFixup> &Fixups) const {
Chris Lattner8f4444d2010-11-15 08:02:41 +0000194 // Encode (imm, reg) as a memrix, which has the low 14-bits as the
195 // displacement and the next 5 bits as the register #.
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000196 assert(MI.getOperand(OpNo+1).isReg());
Chris Lattner8f4444d2010-11-15 08:02:41 +0000197 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups) << 14;
198
Chris Lattner65661122010-11-15 06:33:39 +0000199 const MCOperand &MO = MI.getOperand(OpNo);
Chris Lattner8f4444d2010-11-15 08:02:41 +0000200 if (MO.isImm())
Ulrich Weigand9d980cb2013-05-16 17:58:02 +0000201 return ((getMachineOpValue(MI, MO, Fixups) >> 2) & 0x3FFF) | RegBits;
Chris Lattner65661122010-11-15 06:33:39 +0000202
Ulrich Weigand3e186012013-03-26 10:56:47 +0000203 // Add a fixup for the displacement field.
Ulrich Weigand2fb140e2013-05-15 15:07:06 +0000204 Fixups.push_back(MCFixup::Create(2, MO.getExpr(),
Ulrich Weigand6e23ac62013-05-17 12:37:21 +0000205 (MCFixupKind)PPC::fixup_ppc_half16ds));
Chris Lattner8f4444d2010-11-15 08:02:41 +0000206 return RegBits;
Chris Lattner65661122010-11-15 06:33:39 +0000207}
208
Chris Lattner0e3461e2010-11-15 06:09:35 +0000209
Bill Schmidtca4a0c92012-12-04 16:18:08 +0000210unsigned PPCMCCodeEmitter::getTLSRegEncoding(const MCInst &MI, unsigned OpNo,
211 SmallVectorImpl<MCFixup> &Fixups) const {
212 const MCOperand &MO = MI.getOperand(OpNo);
213 if (MO.isReg()) return getMachineOpValue(MI, MO, Fixups);
214
215 // Add a fixup for the TLS register, which simply provides a relocation
216 // hint to the linker that this statement is part of a relocation sequence.
217 // Return the thread-pointer register's encoding.
218 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
Ulrich Weigand5b427592013-07-05 12:22:36 +0000219 (MCFixupKind)PPC::fixup_ppc_nofixup));
Bill Wendlingbc07a892013-06-18 07:20:20 +0000220 return CTX.getRegisterInfo()->getEncodingValue(PPC::X13);
Bill Schmidtca4a0c92012-12-04 16:18:08 +0000221}
222
Ulrich Weigand5143bab2013-07-02 21:31:04 +0000223unsigned PPCMCCodeEmitter::getTLSCallEncoding(const MCInst &MI, unsigned OpNo,
224 SmallVectorImpl<MCFixup> &Fixups) const {
225 // For special TLS calls, we need two fixups; one for the branch target
226 // (__tls_get_addr), which we create via getDirectBrEncoding as usual,
227 // and one for the TLSGD or TLSLD symbol, which is emitted here.
228 const MCOperand &MO = MI.getOperand(OpNo+1);
229 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
230 (MCFixupKind)PPC::fixup_ppc_nofixup));
231 return getDirectBrEncoding(MI, OpNo, Fixups);
232}
233
Chris Lattner79fa3712010-11-15 05:57:53 +0000234unsigned PPCMCCodeEmitter::
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000235get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
236 SmallVectorImpl<MCFixup> &Fixups) const {
237 const MCOperand &MO = MI.getOperand(OpNo);
Ulrich Weigand49f487e2013-07-03 17:59:07 +0000238 assert((MI.getOpcode() == PPC::MTOCRF || MI.getOpcode() == PPC::MTOCRF8 ||
Ulrich Weigandd5ebc622013-07-03 17:05:42 +0000239 MI.getOpcode() == PPC::MFOCRF || MI.getOpcode() == PPC::MFOCRF8) &&
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000240 (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7));
Bill Wendlingbc07a892013-06-18 07:20:20 +0000241 return 0x80 >> CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000242}
243
244
245unsigned PPCMCCodeEmitter::
Chris Lattner9ec375c2010-11-15 04:16:32 +0000246getMachineOpValue(const MCInst &MI, const MCOperand &MO,
247 SmallVectorImpl<MCFixup> &Fixups) const {
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000248 if (MO.isReg()) {
Ulrich Weigand49f487e2013-07-03 17:59:07 +0000249 // MTOCRF/MFOCRF should go through get_crbitm_encoding for the CR operand.
Chris Lattner7b25d6f2010-11-16 00:57:32 +0000250 // The GPR operand should come through here though.
Ulrich Weigand49f487e2013-07-03 17:59:07 +0000251 assert((MI.getOpcode() != PPC::MTOCRF && MI.getOpcode() != PPC::MTOCRF8 &&
Ulrich Weigandd5ebc622013-07-03 17:05:42 +0000252 MI.getOpcode() != PPC::MFOCRF && MI.getOpcode() != PPC::MFOCRF8) ||
Chris Lattner73716a62010-11-16 00:55:51 +0000253 MO.getReg() < PPC::CR0 || MO.getReg() > PPC::CR7);
Bill Wendlingbc07a892013-06-18 07:20:20 +0000254 return CTX.getRegisterInfo()->getEncodingValue(MO.getReg());
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000255 }
Chris Lattnerc877d8f2010-11-15 04:51:55 +0000256
Chris Lattnerefacb9e2010-11-15 08:22:03 +0000257 assert(MO.isImm() &&
258 "Relocation required in an instruction that we cannot encode!");
259 return MO.getImm();
Chris Lattner9ec375c2010-11-15 04:16:32 +0000260}
261
262
263#include "PPCGenMCCodeEmitter.inc"