blob: 67ab66537a4603ec44b9f0b100579b9774d09b72 [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 Lattnera9d9ab92010-11-15 05:57:53 +000046 { "fixup_ppc_br24", 6, 24, MCFixupKindInfo::FKF_IsPCRel }
Chris Lattnera2d60252010-11-15 04:47:16 +000047#if 0
Chris Lattner5ffe38e2010-11-15 04:16:32 +000048 { "fixup_arm_vfp_pcrel_12", 3, 8, MCFixupKindInfo::FKF_IsPCRel },
49 { "fixup_arm_branch", 1, 24, MCFixupKindInfo::FKF_IsPCRel },
50#endif
51 };
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 Lattnera9d9ab92010-11-15 05:57:53 +000061 unsigned getCallTargetEncoding(const MCInst &MI, unsigned OpNo,
62 SmallVectorImpl<MCFixup> &Fixups) const;
63
Chris Lattner7192eb82010-11-15 05:19:25 +000064 unsigned get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
65 SmallVectorImpl<MCFixup> &Fixups) const;
66
Chris Lattner5ffe38e2010-11-15 04:16:32 +000067 /// getMachineOpValue - Return binary encoding of operand. If the machine
68 /// operand requires relocation, record the relocation and return zero.
69 unsigned getMachineOpValue(const MCInst &MI,const MCOperand &MO,
70 SmallVectorImpl<MCFixup> &Fixups) const;
Chris Lattner5ffe38e2010-11-15 04:16:32 +000071
72 // getBinaryCodeForInstr - TableGen'erated function for getting the
73 // binary encoding for an instruction.
74 unsigned getBinaryCodeForInstr(const MCInst &MI,
75 SmallVectorImpl<MCFixup> &Fixups) const;
76 void EncodeInstruction(const MCInst &MI, raw_ostream &OS,
77 SmallVectorImpl<MCFixup> &Fixups) const {
78 unsigned Bits = getBinaryCodeForInstr(MI, Fixups);
79
80 // Output the constant in big endian byte order.
81 for (unsigned i = 0; i != 4; ++i) {
82 OS << (char)(Bits >> 24);
83 Bits <<= 8;
84 }
85
86 ++MCNumEmitted; // Keep track of the # of mi's emitted.
87 }
88
89};
90
91} // end anonymous namespace
92
93MCCodeEmitter *llvm::createPPCMCCodeEmitter(const Target &, TargetMachine &TM,
94 MCContext &Ctx) {
95 return new PPCMCCodeEmitter(TM, Ctx);
96}
97
98unsigned PPCMCCodeEmitter::
Chris Lattnera9d9ab92010-11-15 05:57:53 +000099getCallTargetEncoding(const MCInst &MI, unsigned OpNo,
100 SmallVectorImpl<MCFixup> &Fixups) const {
101 const MCOperand &MO = MI.getOperand(OpNo);
102 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO, Fixups);
103
104 // Add a fixup for the branch target.
105 Fixups.push_back(MCFixup::Create(0, MO.getExpr(),
106 (MCFixupKind)PPC::fixup_ppc_br24));
107 return 0;
108}
109
110unsigned PPCMCCodeEmitter::
Chris Lattner7192eb82010-11-15 05:19:25 +0000111get_crbitm_encoding(const MCInst &MI, unsigned OpNo,
112 SmallVectorImpl<MCFixup> &Fixups) const {
113 const MCOperand &MO = MI.getOperand(OpNo);
114 assert((MI.getOpcode() == PPC::MTCRF || MI.getOpcode() == PPC::MFOCRF) &&
115 (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7));
116 return 0x80 >> PPCRegisterInfo::getRegisterNumbering(MO.getReg());
117}
118
119
120unsigned PPCMCCodeEmitter::
Chris Lattner5ffe38e2010-11-15 04:16:32 +0000121getMachineOpValue(const MCInst &MI, const MCOperand &MO,
122 SmallVectorImpl<MCFixup> &Fixups) const {
Chris Lattner7192eb82010-11-15 05:19:25 +0000123 if (MO.isReg()) {
124 assert(MI.getOpcode() != PPC::MTCRF && MI.getOpcode() != PPC::MFOCRF);
Chris Lattnera04084e2010-11-15 04:51:55 +0000125 return PPCRegisterInfo::getRegisterNumbering(MO.getReg());
Chris Lattner7192eb82010-11-15 05:19:25 +0000126 }
Chris Lattnera04084e2010-11-15 04:51:55 +0000127
128 if (MO.isImm())
129 return MO.getImm();
130
Chris Lattner5ffe38e2010-11-15 04:16:32 +0000131 // FIXME.
132 return 0;
133}
134
135
136#include "PPCGenMCCodeEmitter.inc"