blob: 66c0bb72a9c2f6009c3fbdd067394a453f4930c5 [file] [log] [blame]
Chris Lattner5ffe38e2010-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"
15#include "PPC.h"
Chris Lattnera04084e2010-11-15 04:51:55 +000016#include "PPCRegisterInfo.h"
Chris Lattnera9d9ab92010-11-15 05:57:53 +000017#include "PPCFixupKinds.h"
Chris Lattner5ffe38e2010-11-15 04:16:32 +000018#include "llvm/MC/MCCodeEmitter.h"
19#include "llvm/MC/MCInst.h"
20#include "llvm/ADT/Statistic.h"
21#include "llvm/Support/raw_ostream.h"
22#include "llvm/Support/ErrorHandling.h"
23using namespace llvm;
24
25STATISTIC(MCNumEmitted, "Number of MC instructions emitted");
26
27namespace {
28class PPCMCCodeEmitter : public MCCodeEmitter {
29 PPCMCCodeEmitter(const PPCMCCodeEmitter &); // DO NOT IMPLEMENT
30 void operator=(const PPCMCCodeEmitter &); // DO NOT IMPLEMENT
31 const TargetMachine &TM;
32 MCContext &Ctx;
33
34public:
35 PPCMCCodeEmitter(TargetMachine &tm, MCContext &ctx)
36 : TM(tm), Ctx(ctx) {
37 }
38
39 ~PPCMCCodeEmitter() {}
40
Chris Lattnera9d9ab92010-11-15 05:57:53 +000041 unsigned getNumFixupKinds() const { return PPC::NumTargetFixupKinds; }
Chris Lattner5ffe38e2010-11-15 04:16:32 +000042
43 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const {
44 const static MCFixupKindInfo Infos[] = {
Chris Lattner5ffe38e2010-11-15 04:16:32 +000045 // name offset bits flags
Chris Lattnerb7194372010-11-15 06:12:22 +000046 { "fixup_ppc_br24", 6, 24, MCFixupKindInfo::FKF_IsPCRel },
Chris Lattner85cf7d72010-11-15 06:33:39 +000047 { "fixup_ppc_brcond14", 16, 14, MCFixupKindInfo::FKF_IsPCRel },
48 { "fixup_ppc_lo16", 16, 16, 0 },
49 { "fixup_ppc_ha16", 16, 16, 0 },
50 { "fixup_ppc_lo14", 16, 14, 0 }
Chris Lattner5ffe38e2010-11-15 04:16:32 +000051 };
52
53 if (Kind < FirstTargetFixupKind)
54 return MCCodeEmitter::getFixupKindInfo(Kind);
55
56 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
57 "Invalid kind!");
58 return Infos[Kind - FirstTargetFixupKind];
59 }
Chris Lattner7192eb82010-11-15 05:19:25 +000060
Chris Lattner8d704112010-11-15 06:09:35 +000061 unsigned getDirectBrEncoding(const MCInst &MI, unsigned OpNo,
62 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattner8d704112010-11-15 06:09:35 +000063 unsigned getCondBrEncoding(const MCInst &MI, unsigned OpNo,
64 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattner85cf7d72010-11-15 06:33:39 +000065 unsigned getHA16Encoding(const MCInst &MI, unsigned OpNo,
66 SmallVectorImpl<MCFixup> &Fixups) const;
67 unsigned getLO16Encoding(const MCInst &MI, unsigned OpNo,
68 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattner17e2c182010-11-15 08:02:41 +000069 unsigned getMemRIXEncoding(const MCInst &MI, unsigned OpNo,
70 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattner7192eb82010-11-15 05:19:25 +000071 unsigned get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
72 SmallVectorImpl<MCFixup> &Fixups) const;
73
Chris Lattner5ffe38e2010-11-15 04:16:32 +000074 /// getMachineOpValue - Return binary encoding of operand. If the machine
75 /// operand requires relocation, record the relocation and return zero.
76 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
77 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattner5ffe38e2010-11-15 04:16:32 +000078
79 // getBinaryCodeForInstr - TableGen'erated function for getting the
80 // binary encoding for an instruction.
81 unsigned getBinaryCodeForInstr(const MCInst &MI,
82 SmallVectorImpl<MCFixup> &Fixups) const;
83 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
84 SmallVectorImpl<MCFixup> &Fixups) const {
85 unsigned Bits = getBinaryCodeForInstr(MI, Fixups);
86
87 // Output the constant in big endian byte order.
88 for (unsigned i = 0; i != 4; ++i) {
89 OS << (char)(Bits >> 24);
90 Bits <<= 8;
91 }
92
93 ++MCNumEmitted; // Keep track of the # of mi's emitted.
94 }
95
96};
97
98} // end anonymous namespace
99
100MCCodeEmitter *llvm::createPPCMCCodeEmitter(const Target &, TargetMachine &TM,
101 MCContext &Ctx) {
102 return new PPCMCCodeEmitter(TM, Ctx);
103}
104
105unsigned PPCMCCodeEmitter::
Chris Lattner8d704112010-11-15 06:09:35 +0000106getDirectBrEncoding(const MCInst &MI, unsigned OpNo,
107 SmallVectorImpl<MCFixup> &Fixups) const {
Chris Lattnera9d9ab92010-11-15 05:57:53 +0000108 const MCOperand &MO = MI.getOperand(OpNo);
109 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
110
111 // Add a fixup for the branch target.
112 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
113 (MCFixupKind)PPC::fixup_ppc_br24));
114 return 0;
115}
116
Chris Lattner8d704112010-11-15 06:09:35 +0000117unsigned PPCMCCodeEmitter::getCondBrEncoding(const MCInst &MI, unsigned OpNo,
118 SmallVectorImpl<MCFixup> &Fixups) const {
119 const MCOperand &MO = MI.getOperand(OpNo);
120 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
121
Chris Lattnerb7194372010-11-15 06:12:22 +0000122 // Add a fixup for the branch target.
123 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
124 (MCFixupKind)PPC::fixup_ppc_brcond14));
Chris Lattner8d704112010-11-15 06:09:35 +0000125 return 0;
126}
127
Chris Lattner85cf7d72010-11-15 06:33:39 +0000128unsigned PPCMCCodeEmitter::getHA16Encoding(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
133 // Add a fixup for the branch target.
134 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
135 (MCFixupKind)PPC::fixup_ppc_ha16));
136 return 0;
137}
138
139unsigned PPCMCCodeEmitter::getLO16Encoding(const MCInst &MI, unsigned OpNo,
140 SmallVectorImpl<MCFixup> &Fixups) const {
141 const MCOperand &MO = MI.getOperand(OpNo);
142 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
143
144 // Add a fixup for the branch target.
145 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
146 (MCFixupKind)PPC::fixup_ppc_lo16));
147 return 0;
148}
149
Chris Lattner17e2c182010-11-15 08:02:41 +0000150unsigned PPCMCCodeEmitter::getMemRIXEncoding(const MCInst &MI, unsigned OpNo,
Chris Lattner85cf7d72010-11-15 06:33:39 +0000151 SmallVectorImpl<MCFixup> &Fixups) const {
Chris Lattner17e2c182010-11-15 08:02:41 +0000152 // Encode (imm, reg) as a memrix, which has the low 14-bits as the
153 // displacement and the next 5 bits as the register #.
154 unsigned RegBits = getMachineOpValue(MI, MI.getOperand(OpNo+1), Fixups) << 14;
155
Chris Lattner85cf7d72010-11-15 06:33:39 +0000156 const MCOperand &MO = MI.getOperand(OpNo);
Chris Lattner17e2c182010-11-15 08:02:41 +0000157 if (MO.isImm())
158 return (getMachineOpValue(MI, MO, Fixups) & 0x3FFF) | RegBits;
Chris Lattner85cf7d72010-11-15 06:33:39 +0000159
160 // Add a fixup for the branch target.
161 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
162 (MCFixupKind)PPC::fixup_ppc_lo14));
Chris Lattner17e2c182010-11-15 08:02:41 +0000163 return RegBits;
Chris Lattner85cf7d72010-11-15 06:33:39 +0000164}
165
Chris Lattner8d704112010-11-15 06:09:35 +0000166
Chris Lattnera9d9ab92010-11-15 05:57:53 +0000167unsigned PPCMCCodeEmitter::
Chris Lattner7192eb82010-11-15 05:19:25 +0000168get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
169 SmallVectorImpl<MCFixup> &Fixups) const {
170 const MCOperand &MO = MI.getOperand(OpNo);
171 assert((MI.getOpcode() == PPC::MTCRF || MI.getOpcode() == PPC::MFOCRF) &&
172 (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7));
173 return 0x80 >> PPCRegisterInfo::getRegisterNumbering(MO.getReg());
174}
175
176
177unsigned PPCMCCodeEmitter::
Chris Lattner5ffe38e2010-11-15 04:16:32 +0000178getMachineOpValue(const MCInst &MI, const MCOperand &MO,
179 SmallVectorImpl<MCFixup> &Fixups) const {
Chris Lattner7192eb82010-11-15 05:19:25 +0000180 if (MO.isReg()) {
181 assert(MI.getOpcode() != PPC::MTCRF && MI.getOpcode() != PPC::MFOCRF);
Chris Lattnera04084e2010-11-15 04:51:55 +0000182 return PPCRegisterInfo::getRegisterNumbering(MO.getReg());
Chris Lattner7192eb82010-11-15 05:19:25 +0000183 }
Chris Lattnera04084e2010-11-15 04:51:55 +0000184
185 if (MO.isImm())
186 return MO.getImm();
187
Chris Lattner5ffe38e2010-11-15 04:16:32 +0000188 // FIXME.
189 return 0;
190}
191
192
193#include "PPCGenMCCodeEmitter.inc"