blob: 2dfdda3cde656b70f573a9e1b15161cc31fb0514 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- PPCCodeEmitter.cpp - JIT Code Emitter for PowerPC32 -------*- C++ -*-=//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the PowerPC 32-bit CodeEmitter and associated machinery to
11// JIT-compile bitcode to native PowerPC.
12//
13//===----------------------------------------------------------------------===//
14
15#include "PPCTargetMachine.h"
16#include "PPCRelocations.h"
17#include "PPC.h"
18#include "llvm/Module.h"
19#include "llvm/PassManager.h"
20#include "llvm/CodeGen/MachineCodeEmitter.h"
21#include "llvm/CodeGen/MachineFunctionPass.h"
22#include "llvm/CodeGen/MachineInstrBuilder.h"
Nicolas Geoffray0e757e12008-02-13 18:39:37 +000023#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000024#include "llvm/CodeGen/Passes.h"
25#include "llvm/Support/Debug.h"
26#include "llvm/Support/Compiler.h"
27#include "llvm/Target/TargetOptions.h"
28using namespace llvm;
29
30namespace {
31 class VISIBILITY_HIDDEN PPCCodeEmitter : public MachineFunctionPass {
32 TargetMachine &TM;
33 MachineCodeEmitter &MCE;
34
35 /// MovePCtoLROffset - When/if we see a MovePCtoLR instruction, we record
36 /// its address in the function into this pointer.
37 void *MovePCtoLROffset;
38
39 /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
40 ///
41 int getMachineOpValue(MachineInstr &MI, MachineOperand &MO);
Nicolas Geoffray0e757e12008-02-13 18:39:37 +000042
43 void getAnalysisUsage(AnalysisUsage &AU) const {
44 AU.addRequired<MachineModuleInfo>();
45 MachineFunctionPass::getAnalysisUsage(AU);
46 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047
48 public:
49 static char ID;
50 PPCCodeEmitter(TargetMachine &T, MachineCodeEmitter &M)
51 : MachineFunctionPass((intptr_t)&ID), TM(T), MCE(M) {}
52
53 const char *getPassName() const { return "PowerPC Machine Code Emitter"; }
54
55 /// runOnMachineFunction - emits the given MachineFunction to memory
56 ///
57 bool runOnMachineFunction(MachineFunction &MF);
58
59 /// emitBasicBlock - emits the given MachineBasicBlock to memory
60 ///
61 void emitBasicBlock(MachineBasicBlock &MBB);
62
63 /// getValueBit - return the particular bit of Val
64 ///
65 unsigned getValueBit(int64_t Val, unsigned bit) { return (Val >> bit) & 1; }
66
67 /// getBinaryCodeForInstr - This function, generated by the
68 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
69 /// machine instructions.
70 ///
71 unsigned getBinaryCodeForInstr(MachineInstr &MI);
72 };
73 char PPCCodeEmitter::ID = 0;
74}
75
76/// createPPCCodeEmitterPass - Return a pass that emits the collected PPC code
77/// to the specified MCE object.
78FunctionPass *llvm::createPPCCodeEmitterPass(PPCTargetMachine &TM,
79 MachineCodeEmitter &MCE) {
80 return new PPCCodeEmitter(TM, MCE);
81}
82
Dan Gohmanf17a25c2007-07-18 16:29:46 +000083bool PPCCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
84 assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
85 MF.getTarget().getRelocationModel() != Reloc::Static) &&
86 "JIT relocation model must be set to static or default!");
Nicolas Geoffray0e757e12008-02-13 18:39:37 +000087
88 MCE.setModuleInfo(&getAnalysis<MachineModuleInfo>());
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089 do {
90 MovePCtoLROffset = 0;
91 MCE.startFunction(MF);
92 for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
93 emitBasicBlock(*BB);
94 } while (MCE.finishFunction(MF));
95
96 return false;
97}
98
99void PPCCodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
100 MCE.StartMachineBasicBlock(&MBB);
101
102 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
103 MachineInstr &MI = *I;
104 switch (MI.getOpcode()) {
105 default:
106 MCE.emitWordBE(getBinaryCodeForInstr(*I));
107 break;
Dan Gohmanfa607c92008-07-01 00:05:16 +0000108 case TargetInstrInfo::DBG_LABEL:
109 case TargetInstrInfo::EH_LABEL:
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000110 MCE.emitLabel(MI.getOperand(0).getImm());
111 break;
Evan Chengb74b4b62008-03-17 06:56:52 +0000112 case TargetInstrInfo::IMPLICIT_DEF:
113 break; // pseudo opcode, no side effects
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114 case PPC::MovePCtoLR:
115 case PPC::MovePCtoLR8:
116 assert(TM.getRelocationModel() == Reloc::PIC_);
117 MovePCtoLROffset = (void*)MCE.getCurrentPCValue();
118 MCE.emitWordBE(0x48000005); // bl 1
119 break;
120 }
121 }
122}
123
124int PPCCodeEmitter::getMachineOpValue(MachineInstr &MI, MachineOperand &MO) {
125
126 intptr_t rv = 0; // Return value; defaults to 0 for unhandled cases
127 // or things that get fixed up later by the JIT.
128 if (MO.isRegister()) {
129 rv = PPCRegisterInfo::getRegisterNumbering(MO.getReg());
130
131 // Special encoding for MTCRF and MFOCRF, which uses a bit mask for the
132 // register, not the register number directly.
133 if ((MI.getOpcode() == PPC::MTCRF || MI.getOpcode() == PPC::MFOCRF) &&
134 (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7)) {
135 rv = 0x80 >> rv;
136 }
137 } else if (MO.isImmediate()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000138 rv = MO.getImm();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139 } else if (MO.isGlobalAddress() || MO.isExternalSymbol() ||
140 MO.isConstantPoolIndex() || MO.isJumpTableIndex()) {
141 unsigned Reloc = 0;
142 if (MI.getOpcode() == PPC::BL_Macho || MI.getOpcode() == PPC::BL8_Macho ||
Arnold Schwaighofera0032722008-04-30 09:16:33 +0000143 MI.getOpcode() == PPC::BL_ELF || MI.getOpcode() == PPC::BL8_ELF ||
144 MI.getOpcode() == PPC::TAILB || MI.getOpcode() == PPC::TAILB8)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145 Reloc = PPC::reloc_pcrel_bx;
146 else {
147 if (TM.getRelocationModel() == Reloc::PIC_) {
148 assert(MovePCtoLROffset && "MovePCtoLR not seen yet?");
149 }
150 switch (MI.getOpcode()) {
151 default: MI.dump(); assert(0 && "Unknown instruction for relocation!");
152 case PPC::LIS:
153 case PPC::LIS8:
154 case PPC::ADDIS:
155 case PPC::ADDIS8:
156 Reloc = PPC::reloc_absolute_high; // Pointer to symbol
157 break;
158 case PPC::LI:
159 case PPC::LI8:
160 case PPC::LA:
161 // Loads.
162 case PPC::LBZ:
163 case PPC::LBZ8:
164 case PPC::LHA:
165 case PPC::LHA8:
166 case PPC::LHZ:
167 case PPC::LHZ8:
168 case PPC::LWZ:
169 case PPC::LWZ8:
170 case PPC::LFS:
171 case PPC::LFD:
172
173 // Stores.
174 case PPC::STB:
175 case PPC::STB8:
176 case PPC::STH:
177 case PPC::STH8:
178 case PPC::STW:
179 case PPC::STW8:
180 case PPC::STFS:
181 case PPC::STFD:
182 Reloc = PPC::reloc_absolute_low;
183 break;
184
185 case PPC::LWA:
186 case PPC::LD:
187 case PPC::STD:
188 case PPC::STD_32:
189 Reloc = PPC::reloc_absolute_low_ix;
190 break;
191 }
192 }
193
194 MachineRelocation R;
195 if (MO.isGlobalAddress()) {
196 R = MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
Evan Cheng9f6942b2008-01-04 02:22:21 +0000197 MO.getGlobal(), 0,
198 isa<Function>(MO.getGlobal()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000199 } else if (MO.isExternalSymbol()) {
200 R = MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
201 Reloc, MO.getSymbolName(), 0);
202 } else if (MO.isConstantPoolIndex()) {
203 R = MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
Chris Lattner6017d482007-12-30 23:10:15 +0000204 Reloc, MO.getIndex(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000205 } else {
206 assert(MO.isJumpTableIndex());
207 R = MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
Chris Lattner6017d482007-12-30 23:10:15 +0000208 Reloc, MO.getIndex(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000209 }
210
211 // If in PIC mode, we need to encode the negated address of the
212 // 'movepctolr' into the unrelocated field. After relocation, we'll have
213 // &gv-&movepctolr-4 in the imm field. Once &movepctolr is added to the imm
214 // field, we get &gv. This doesn't happen for branch relocations, which are
215 // always implicitly pc relative.
216 if (TM.getRelocationModel() == Reloc::PIC_ && Reloc != PPC::reloc_pcrel_bx){
217 assert(MovePCtoLROffset && "MovePCtoLR not seen yet?");
218 R.setConstantVal(-(intptr_t)MovePCtoLROffset - 4);
219 }
220 MCE.addRelocation(R);
221
222 } else if (MO.isMachineBasicBlock()) {
223 unsigned Reloc = 0;
224 unsigned Opcode = MI.getOpcode();
225 if (Opcode == PPC::B || Opcode == PPC::BL_Macho ||
226 Opcode == PPC::BLA_Macho || Opcode == PPC::BL_ELF ||
227 Opcode == PPC::BLA_ELF)
228 Reloc = PPC::reloc_pcrel_bx;
229 else // BCC instruction
230 Reloc = PPC::reloc_pcrel_bcx;
231 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
Chris Lattner6017d482007-12-30 23:10:15 +0000232 Reloc, MO.getMBB()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233 } else {
234 cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
235 abort();
236 }
237
238 return rv;
239}
240
241#include "PPCGenCodeEmitter.inc"
242