blob: eaa8269499d9a4052ac828b10fd7bd56551be8dd [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"
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000021#include "llvm/CodeGen/JITCodeEmitter.h"
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +000022#include "llvm/CodeGen/ObjectCodeEmitter.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include "llvm/CodeGen/MachineFunctionPass.h"
24#include "llvm/CodeGen/MachineInstrBuilder.h"
Nicolas Geoffray0e757e12008-02-13 18:39:37 +000025#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026#include "llvm/CodeGen/Passes.h"
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +000027#include "llvm/Support/Debug.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028#include "llvm/Support/Compiler.h"
29#include "llvm/Target/TargetOptions.h"
30using namespace llvm;
31
32namespace {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000033 class PPCCodeEmitter {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034 TargetMachine &TM;
35 MachineCodeEmitter &MCE;
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000036 public:
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +000037 PPCCodeEmitter(TargetMachine &tm, MachineCodeEmitter &mce):
38 TM(tm), MCE(mce) {}
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000039
40 /// getBinaryCodeForInstr - This function, generated by the
41 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
42 /// machine instructions.
43
44 unsigned getBinaryCodeForInstr(const MachineInstr &MI);
45
46 /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
47
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +000048 unsigned getMachineOpValue(const MachineInstr &MI,
49 const MachineOperand &MO);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050
51 /// MovePCtoLROffset - When/if we see a MovePCtoLR instruction, we record
52 /// its address in the function into this pointer.
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000053
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054 void *MovePCtoLROffset;
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000055 };
56
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +000057 template <class CodeEmitter>
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000058 class VISIBILITY_HIDDEN Emitter : public MachineFunctionPass,
59 public PPCCodeEmitter
60 {
61 TargetMachine &TM;
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +000062 CodeEmitter &MCE;
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000063
Nicolas Geoffray0e757e12008-02-13 18:39:37 +000064 void getAnalysisUsage(AnalysisUsage &AU) const {
65 AU.addRequired<MachineModuleInfo>();
66 MachineFunctionPass::getAnalysisUsage(AU);
67 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068
69 public:
70 static char ID;
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +000071 Emitter(TargetMachine &tm, CodeEmitter &mce)
72 : MachineFunctionPass(&ID), PPCCodeEmitter(tm, mce), TM(tm), MCE(mce) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073
74 const char *getPassName() const { return "PowerPC Machine Code Emitter"; }
75
76 /// runOnMachineFunction - emits the given MachineFunction to memory
77 ///
78 bool runOnMachineFunction(MachineFunction &MF);
79
80 /// emitBasicBlock - emits the given MachineBasicBlock to memory
81 ///
82 void emitBasicBlock(MachineBasicBlock &MBB);
83
84 /// getValueBit - return the particular bit of Val
85 ///
86 unsigned getValueBit(int64_t Val, unsigned bit) { return (Val >> bit) & 1; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +000089 template <class CodeEmitter>
90 char Emitter<CodeEmitter>::ID = 0;
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000091}
92
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093/// createPPCCodeEmitterPass - Return a pass that emits the collected PPC code
94/// to the specified MCE object.
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +000095
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096FunctionPass *llvm::createPPCCodeEmitterPass(PPCTargetMachine &TM,
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +000097 MachineCodeEmitter &MCE) {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000098 return new Emitter<MachineCodeEmitter>(TM, MCE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099}
100
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000101FunctionPass *llvm::createPPCJITCodeEmitterPass(PPCTargetMachine &TM,
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000102 JITCodeEmitter &JCE) {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000103 return new Emitter<JITCodeEmitter>(TM, JCE);
104}
105
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000106FunctionPass *llvm::createPPCObjectCodeEmitterPass(PPCTargetMachine &TM,
107 ObjectCodeEmitter &OCE) {
108 return new Emitter<ObjectCodeEmitter>(TM, OCE);
109}
110
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000111template <class CodeEmitter>
112bool Emitter<CodeEmitter>::runOnMachineFunction(MachineFunction &MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113 assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
114 MF.getTarget().getRelocationModel() != Reloc::Static) &&
115 "JIT relocation model must be set to static or default!");
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000116
117 MCE.setModuleInfo(&getAnalysis<MachineModuleInfo>());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118 do {
119 MovePCtoLROffset = 0;
120 MCE.startFunction(MF);
121 for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
122 emitBasicBlock(*BB);
123 } while (MCE.finishFunction(MF));
124
125 return false;
126}
127
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000128template <class CodeEmitter>
129void Emitter<CodeEmitter>::emitBasicBlock(MachineBasicBlock &MBB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000130 MCE.StartMachineBasicBlock(&MBB);
131
132 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
Evan Cheng3ca89372008-09-02 06:51:36 +0000133 const MachineInstr &MI = *I;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134 switch (MI.getOpcode()) {
135 default:
Evan Cheng3ca89372008-09-02 06:51:36 +0000136 MCE.emitWordBE(getBinaryCodeForInstr(MI));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137 break;
Dan Gohmanfa607c92008-07-01 00:05:16 +0000138 case TargetInstrInfo::DBG_LABEL:
139 case TargetInstrInfo::EH_LABEL:
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000140 MCE.emitLabel(MI.getOperand(0).getImm());
141 break;
Evan Chengb74b4b62008-03-17 06:56:52 +0000142 case TargetInstrInfo::IMPLICIT_DEF:
143 break; // pseudo opcode, no side effects
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000144 case PPC::MovePCtoLR:
145 case PPC::MovePCtoLR8:
146 assert(TM.getRelocationModel() == Reloc::PIC_);
147 MovePCtoLROffset = (void*)MCE.getCurrentPCValue();
148 MCE.emitWordBE(0x48000005); // bl 1
149 break;
150 }
151 }
152}
153
Evan Cheng3ca89372008-09-02 06:51:36 +0000154unsigned PPCCodeEmitter::getMachineOpValue(const MachineInstr &MI,
155 const MachineOperand &MO) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156
Evan Cheng3ca89372008-09-02 06:51:36 +0000157 unsigned rv = 0; // Return value; defaults to 0 for unhandled cases
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 // or things that get fixed up later by the JIT.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000159 if (MO.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 rv = PPCRegisterInfo::getRegisterNumbering(MO.getReg());
161
162 // Special encoding for MTCRF and MFOCRF, which uses a bit mask for the
163 // register, not the register number directly.
164 if ((MI.getOpcode() == PPC::MTCRF || MI.getOpcode() == PPC::MFOCRF) &&
165 (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7)) {
166 rv = 0x80 >> rv;
167 }
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000168 } else if (MO.isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000169 rv = MO.getImm();
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000170 } else if (MO.isGlobal() || MO.isSymbol() ||
171 MO.isCPI() || MO.isJTI()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 unsigned Reloc = 0;
Tilmann Scheller386330d2009-07-03 06:47:08 +0000173 if (MI.getOpcode() == PPC::BL_Darwin || MI.getOpcode() == PPC::BL8_Darwin ||
174 MI.getOpcode() == PPC::BL_SVR4 || MI.getOpcode() == PPC::BL8_ELF ||
Arnold Schwaighofera0032722008-04-30 09:16:33 +0000175 MI.getOpcode() == PPC::TAILB || MI.getOpcode() == PPC::TAILB8)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 Reloc = PPC::reloc_pcrel_bx;
177 else {
178 if (TM.getRelocationModel() == Reloc::PIC_) {
179 assert(MovePCtoLROffset && "MovePCtoLR not seen yet?");
180 }
181 switch (MI.getOpcode()) {
182 default: MI.dump(); assert(0 && "Unknown instruction for relocation!");
183 case PPC::LIS:
184 case PPC::LIS8:
185 case PPC::ADDIS:
186 case PPC::ADDIS8:
187 Reloc = PPC::reloc_absolute_high; // Pointer to symbol
188 break;
189 case PPC::LI:
190 case PPC::LI8:
191 case PPC::LA:
192 // Loads.
193 case PPC::LBZ:
194 case PPC::LBZ8:
195 case PPC::LHA:
196 case PPC::LHA8:
197 case PPC::LHZ:
198 case PPC::LHZ8:
199 case PPC::LWZ:
200 case PPC::LWZ8:
201 case PPC::LFS:
202 case PPC::LFD:
203
204 // Stores.
205 case PPC::STB:
206 case PPC::STB8:
207 case PPC::STH:
208 case PPC::STH8:
209 case PPC::STW:
210 case PPC::STW8:
211 case PPC::STFS:
212 case PPC::STFD:
213 Reloc = PPC::reloc_absolute_low;
214 break;
215
216 case PPC::LWA:
217 case PPC::LD:
218 case PPC::STD:
219 case PPC::STD_32:
220 Reloc = PPC::reloc_absolute_low_ix;
221 break;
222 }
223 }
224
225 MachineRelocation R;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000226 if (MO.isGlobal()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000227 R = MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
Evan Cheng9f6942b2008-01-04 02:22:21 +0000228 MO.getGlobal(), 0,
229 isa<Function>(MO.getGlobal()));
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000230 } else if (MO.isSymbol()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231 R = MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
232 Reloc, MO.getSymbolName(), 0);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000233 } else if (MO.isCPI()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000234 R = MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
Chris Lattner6017d482007-12-30 23:10:15 +0000235 Reloc, MO.getIndex(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236 } else {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000237 assert(MO.isJTI());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238 R = MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
Chris Lattner6017d482007-12-30 23:10:15 +0000239 Reloc, MO.getIndex(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000240 }
241
242 // If in PIC mode, we need to encode the negated address of the
243 // 'movepctolr' into the unrelocated field. After relocation, we'll have
244 // &gv-&movepctolr-4 in the imm field. Once &movepctolr is added to the imm
245 // field, we get &gv. This doesn't happen for branch relocations, which are
246 // always implicitly pc relative.
247 if (TM.getRelocationModel() == Reloc::PIC_ && Reloc != PPC::reloc_pcrel_bx){
248 assert(MovePCtoLROffset && "MovePCtoLR not seen yet?");
249 R.setConstantVal(-(intptr_t)MovePCtoLROffset - 4);
250 }
251 MCE.addRelocation(R);
252
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000253 } else if (MO.isMBB()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254 unsigned Reloc = 0;
255 unsigned Opcode = MI.getOpcode();
Tilmann Scheller386330d2009-07-03 06:47:08 +0000256 if (Opcode == PPC::B || Opcode == PPC::BL_Darwin ||
257 Opcode == PPC::BLA_Darwin|| Opcode == PPC::BL_SVR4 ||
258 Opcode == PPC::BLA_SVR4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000259 Reloc = PPC::reloc_pcrel_bx;
260 else // BCC instruction
261 Reloc = PPC::reloc_pcrel_bcx;
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000262
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000263 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
Chris Lattner6017d482007-12-30 23:10:15 +0000264 Reloc, MO.getMBB()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000265 } else {
266 cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
267 abort();
268 }
269
270 return rv;
271}
272
273#include "PPCGenCodeEmitter.inc"
274