blob: 16535b258d92def314ba476b7dafe8743c1edb69 [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"
Edwin Török4d9756a2009-07-08 20:53:28 +000029#include "llvm/Support/ErrorHandling.h"
30#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031#include "llvm/Target/TargetOptions.h"
32using namespace llvm;
33
34namespace {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000035 class PPCCodeEmitter {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036 TargetMachine &TM;
37 MachineCodeEmitter &MCE;
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000038 public:
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +000039 PPCCodeEmitter(TargetMachine &tm, MachineCodeEmitter &mce):
40 TM(tm), MCE(mce) {}
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000041
42 /// getBinaryCodeForInstr - This function, generated by the
43 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
44 /// machine instructions.
45
46 unsigned getBinaryCodeForInstr(const MachineInstr &MI);
47
48 /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
49
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +000050 unsigned getMachineOpValue(const MachineInstr &MI,
51 const MachineOperand &MO);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052
53 /// MovePCtoLROffset - When/if we see a MovePCtoLR instruction, we record
54 /// its address in the function into this pointer.
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000055
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056 void *MovePCtoLROffset;
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000057 };
58
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +000059 template <class CodeEmitter>
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000060 class VISIBILITY_HIDDEN Emitter : public MachineFunctionPass,
Chris Lattnerd71b0b02009-08-23 03:41:05 +000061 public PPCCodeEmitter {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000062 TargetMachine &TM;
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +000063 CodeEmitter &MCE;
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000064
Nicolas Geoffray0e757e12008-02-13 18:39:37 +000065 void getAnalysisUsage(AnalysisUsage &AU) const {
66 AU.addRequired<MachineModuleInfo>();
67 MachineFunctionPass::getAnalysisUsage(AU);
68 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069
70 public:
71 static char ID;
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +000072 Emitter(TargetMachine &tm, CodeEmitter &mce)
73 : MachineFunctionPass(&ID), PPCCodeEmitter(tm, mce), TM(tm), MCE(mce) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074
75 const char *getPassName() const { return "PowerPC Machine Code Emitter"; }
76
77 /// runOnMachineFunction - emits the given MachineFunction to memory
78 ///
79 bool runOnMachineFunction(MachineFunction &MF);
80
81 /// emitBasicBlock - emits the given MachineBasicBlock to memory
82 ///
83 void emitBasicBlock(MachineBasicBlock &MBB);
84
85 /// getValueBit - return the particular bit of Val
86 ///
87 unsigned getValueBit(int64_t Val, unsigned bit) { return (Val >> bit) & 1; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +000090 template <class CodeEmitter>
91 char Emitter<CodeEmitter>::ID = 0;
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000092}
93
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094/// createPPCCodeEmitterPass - Return a pass that emits the collected PPC code
95/// to the specified MCE object.
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +000096
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097FunctionPass *llvm::createPPCCodeEmitterPass(PPCTargetMachine &TM,
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +000098 MachineCodeEmitter &MCE) {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000099 return new Emitter<MachineCodeEmitter>(TM, MCE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100}
101
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000102FunctionPass *llvm::createPPCJITCodeEmitterPass(PPCTargetMachine &TM,
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000103 JITCodeEmitter &JCE) {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000104 return new Emitter<JITCodeEmitter>(TM, JCE);
105}
106
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000107FunctionPass *llvm::createPPCObjectCodeEmitterPass(PPCTargetMachine &TM,
108 ObjectCodeEmitter &OCE) {
109 return new Emitter<ObjectCodeEmitter>(TM, OCE);
110}
111
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000112template <class CodeEmitter>
113bool Emitter<CodeEmitter>::runOnMachineFunction(MachineFunction &MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114 assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
115 MF.getTarget().getRelocationModel() != Reloc::Static) &&
116 "JIT relocation model must be set to static or default!");
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000117
118 MCE.setModuleInfo(&getAnalysis<MachineModuleInfo>());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119 do {
120 MovePCtoLROffset = 0;
121 MCE.startFunction(MF);
122 for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
123 emitBasicBlock(*BB);
124 } while (MCE.finishFunction(MF));
125
126 return false;
127}
128
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000129template <class CodeEmitter>
130void Emitter<CodeEmitter>::emitBasicBlock(MachineBasicBlock &MBB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131 MCE.StartMachineBasicBlock(&MBB);
132
133 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
Evan Cheng3ca89372008-09-02 06:51:36 +0000134 const MachineInstr &MI = *I;
Jeffrey Yasskin6628b5b2009-07-17 18:49:39 +0000135 MCE.processDebugLoc(MI.getDebugLoc());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000136 switch (MI.getOpcode()) {
137 default:
Evan Cheng3ca89372008-09-02 06:51:36 +0000138 MCE.emitWordBE(getBinaryCodeForInstr(MI));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000139 break;
Dan Gohmanfa607c92008-07-01 00:05:16 +0000140 case TargetInstrInfo::DBG_LABEL:
141 case TargetInstrInfo::EH_LABEL:
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000142 MCE.emitLabel(MI.getOperand(0).getImm());
143 break;
Evan Chengb74b4b62008-03-17 06:56:52 +0000144 case TargetInstrInfo::IMPLICIT_DEF:
145 break; // pseudo opcode, no side effects
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146 case PPC::MovePCtoLR:
147 case PPC::MovePCtoLR8:
148 assert(TM.getRelocationModel() == Reloc::PIC_);
149 MovePCtoLROffset = (void*)MCE.getCurrentPCValue();
150 MCE.emitWordBE(0x48000005); // bl 1
151 break;
152 }
153 }
154}
155
Evan Cheng3ca89372008-09-02 06:51:36 +0000156unsigned PPCCodeEmitter::getMachineOpValue(const MachineInstr &MI,
157 const MachineOperand &MO) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158
Evan Cheng3ca89372008-09-02 06:51:36 +0000159 unsigned rv = 0; // Return value; defaults to 0 for unhandled cases
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 // or things that get fixed up later by the JIT.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000161 if (MO.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162 rv = PPCRegisterInfo::getRegisterNumbering(MO.getReg());
163
164 // Special encoding for MTCRF and MFOCRF, which uses a bit mask for the
165 // register, not the register number directly.
166 if ((MI.getOpcode() == PPC::MTCRF || MI.getOpcode() == PPC::MFOCRF) &&
167 (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7)) {
168 rv = 0x80 >> rv;
169 }
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000170 } else if (MO.isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000171 rv = MO.getImm();
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000172 } else if (MO.isGlobal() || MO.isSymbol() ||
173 MO.isCPI() || MO.isJTI()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174 unsigned Reloc = 0;
Tilmann Scheller386330d2009-07-03 06:47:08 +0000175 if (MI.getOpcode() == PPC::BL_Darwin || MI.getOpcode() == PPC::BL8_Darwin ||
176 MI.getOpcode() == PPC::BL_SVR4 || MI.getOpcode() == PPC::BL8_ELF ||
Arnold Schwaighofera0032722008-04-30 09:16:33 +0000177 MI.getOpcode() == PPC::TAILB || MI.getOpcode() == PPC::TAILB8)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 Reloc = PPC::reloc_pcrel_bx;
179 else {
180 if (TM.getRelocationModel() == Reloc::PIC_) {
181 assert(MovePCtoLROffset && "MovePCtoLR not seen yet?");
182 }
183 switch (MI.getOpcode()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000184 default: MI.dump(); llvm_unreachable("Unknown instruction for relocation!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000185 case PPC::LIS:
186 case PPC::LIS8:
187 case PPC::ADDIS:
188 case PPC::ADDIS8:
189 Reloc = PPC::reloc_absolute_high; // Pointer to symbol
190 break;
191 case PPC::LI:
192 case PPC::LI8:
193 case PPC::LA:
194 // Loads.
195 case PPC::LBZ:
196 case PPC::LBZ8:
197 case PPC::LHA:
198 case PPC::LHA8:
199 case PPC::LHZ:
200 case PPC::LHZ8:
201 case PPC::LWZ:
202 case PPC::LWZ8:
203 case PPC::LFS:
204 case PPC::LFD:
205
206 // Stores.
207 case PPC::STB:
208 case PPC::STB8:
209 case PPC::STH:
210 case PPC::STH8:
211 case PPC::STW:
212 case PPC::STW8:
213 case PPC::STFS:
214 case PPC::STFD:
215 Reloc = PPC::reloc_absolute_low;
216 break;
217
218 case PPC::LWA:
219 case PPC::LD:
220 case PPC::STD:
221 case PPC::STD_32:
222 Reloc = PPC::reloc_absolute_low_ix;
223 break;
224 }
225 }
226
227 MachineRelocation R;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000228 if (MO.isGlobal()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229 R = MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
Evan Cheng9f6942b2008-01-04 02:22:21 +0000230 MO.getGlobal(), 0,
231 isa<Function>(MO.getGlobal()));
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000232 } else if (MO.isSymbol()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233 R = MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
234 Reloc, MO.getSymbolName(), 0);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000235 } else if (MO.isCPI()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236 R = MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
Chris Lattner6017d482007-12-30 23:10:15 +0000237 Reloc, MO.getIndex(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238 } else {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000239 assert(MO.isJTI());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000240 R = MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
Chris Lattner6017d482007-12-30 23:10:15 +0000241 Reloc, MO.getIndex(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000242 }
243
244 // If in PIC mode, we need to encode the negated address of the
245 // 'movepctolr' into the unrelocated field. After relocation, we'll have
246 // &gv-&movepctolr-4 in the imm field. Once &movepctolr is added to the imm
247 // field, we get &gv. This doesn't happen for branch relocations, which are
248 // always implicitly pc relative.
249 if (TM.getRelocationModel() == Reloc::PIC_ && Reloc != PPC::reloc_pcrel_bx){
250 assert(MovePCtoLROffset && "MovePCtoLR not seen yet?");
251 R.setConstantVal(-(intptr_t)MovePCtoLROffset - 4);
252 }
253 MCE.addRelocation(R);
254
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000255 } else if (MO.isMBB()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000256 unsigned Reloc = 0;
257 unsigned Opcode = MI.getOpcode();
Tilmann Scheller386330d2009-07-03 06:47:08 +0000258 if (Opcode == PPC::B || Opcode == PPC::BL_Darwin ||
259 Opcode == PPC::BLA_Darwin|| Opcode == PPC::BL_SVR4 ||
260 Opcode == PPC::BLA_SVR4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261 Reloc = PPC::reloc_pcrel_bx;
262 else // BCC instruction
263 Reloc = PPC::reloc_pcrel_bcx;
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000264
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000265 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
Chris Lattner6017d482007-12-30 23:10:15 +0000266 Reloc, MO.getMBB()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000267 } else {
Edwin Török4d9756a2009-07-08 20:53:28 +0000268#ifndef NDEBUG
Chris Lattnerd71b0b02009-08-23 03:41:05 +0000269 errs() << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
Edwin Török4d9756a2009-07-08 20:53:28 +0000270#endif
Edwin Törökbd448e32009-07-14 16:55:14 +0000271 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272 }
273
274 return rv;
275}
276
277#include "PPCGenCodeEmitter.inc"