blob: 4943e5c8e11740e52f79f4a7e8e2964ab2836d69 [file] [log] [blame]
Nate Begeman21e463b2005-10-16 05:39:50 +00001//===-- PPCCodeEmitter.cpp - JIT Code Emitter for PowerPC32 -------*- C++ -*-=//
Misha Brukmanb5f662f2005-04-21 23:30:14 +00002//
Misha Brukman5dfe3a92004-06-21 16:55:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb5f662f2005-04-21 23:30:14 +00007//
Misha Brukman5dfe3a92004-06-21 16:55:25 +00008//===----------------------------------------------------------------------===//
Misha Brukmanb5f662f2005-04-21 23:30:14 +00009//
Misha Brukmand37faba2004-10-14 06:07:25 +000010// This file defines the PowerPC 32-bit CodeEmitter and associated machinery to
Gabor Greifa99be512007-07-05 17:07:56 +000011// JIT-compile bitcode to native PowerPC.
Misha Brukman5dfe3a92004-06-21 16:55:25 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattner16e71f22005-10-14 23:59:06 +000015#include "PPCTargetMachine.h"
16#include "PPCRelocations.h"
Chris Lattner26689592005-10-14 23:51:18 +000017#include "PPC.h"
Misha Brukman3070e2f2004-10-21 01:42:02 +000018#include "llvm/Module.h"
Chris Lattnerde123822005-10-15 21:58:54 +000019#include "llvm/PassManager.h"
Misha Brukmanb05daff2004-08-09 23:03:59 +000020#include "llvm/CodeGen/MachineCodeEmitter.h"
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000021#include "llvm/CodeGen/JITCodeEmitter.h"
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000022#include "llvm/CodeGen/ObjectCodeEmitter.h"
Misha Brukmanb05daff2004-08-09 23:03:59 +000023#include "llvm/CodeGen/MachineFunctionPass.h"
Misha Brukmand4b4a992004-10-23 23:47:34 +000024#include "llvm/CodeGen/MachineInstrBuilder.h"
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000025#include "llvm/CodeGen/MachineModuleInfo.h"
Misha Brukmanb05daff2004-08-09 23:03:59 +000026#include "llvm/CodeGen/Passes.h"
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +000027#include "llvm/Support/Debug.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000028#include "llvm/Support/Compiler.h"
Torok Edwindac237e2009-07-08 20:53:28 +000029#include "llvm/Support/ErrorHandling.h"
30#include "llvm/Support/raw_ostream.h"
Evan Chengd2ee2182006-02-18 00:08:58 +000031#include "llvm/Target/TargetOptions.h"
Chris Lattnereea9b132004-11-16 04:47:33 +000032using namespace llvm;
Misha Brukman5dfe3a92004-06-21 16:55:25 +000033
Misha Brukmanb05daff2004-08-09 23:03:59 +000034namespace {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000035 class PPCCodeEmitter {
Misha Brukmanb05daff2004-08-09 23:03:59 +000036 TargetMachine &TM;
37 MachineCodeEmitter &MCE;
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000038 public:
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +000039 PPCCodeEmitter(TargetMachine &tm, MachineCodeEmitter &mce):
40 TM(tm), MCE(mce) {}
Bruno Cardoso Lopesa3f99f92009-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 Lopes434dd4f2009-06-01 19:57:37 +000050 unsigned getMachineOpValue(const MachineInstr &MI,
51 const MachineOperand &MO);
Misha Brukmanb05daff2004-08-09 23:03:59 +000052
Chris Lattnere150b8e2006-12-08 04:54:03 +000053 /// MovePCtoLROffset - When/if we see a MovePCtoLR instruction, we record
54 /// its address in the function into this pointer.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000055
Chris Lattnere150b8e2006-12-08 04:54:03 +000056 void *MovePCtoLROffset;
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000057 };
58
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +000059 template <class CodeEmitter>
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000060 class VISIBILITY_HIDDEN Emitter : public MachineFunctionPass,
61 public PPCCodeEmitter
62 {
63 TargetMachine &TM;
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +000064 CodeEmitter &MCE;
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000065
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +000066 void getAnalysisUsage(AnalysisUsage &AU) const {
67 AU.addRequired<MachineModuleInfo>();
68 MachineFunctionPass::getAnalysisUsage(AU);
69 }
Misha Brukman3070e2f2004-10-21 01:42:02 +000070
Misha Brukmanb05daff2004-08-09 23:03:59 +000071 public:
Devang Patel19974732007-05-03 01:11:54 +000072 static char ID;
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +000073 Emitter(TargetMachine &tm, CodeEmitter &mce)
74 : MachineFunctionPass(&ID), PPCCodeEmitter(tm, mce), TM(tm), MCE(mce) {}
Misha Brukmanb05daff2004-08-09 23:03:59 +000075
76 const char *getPassName() const { return "PowerPC Machine Code Emitter"; }
77
78 /// runOnMachineFunction - emits the given MachineFunction to memory
79 ///
80 bool runOnMachineFunction(MachineFunction &MF);
81
82 /// emitBasicBlock - emits the given MachineBasicBlock to memory
83 ///
84 void emitBasicBlock(MachineBasicBlock &MBB);
85
Misha Brukmand37faba2004-10-14 06:07:25 +000086 /// getValueBit - return the particular bit of Val
87 ///
88 unsigned getValueBit(int64_t Val, unsigned bit) { return (Val >> bit) & 1; }
Misha Brukmanb05daff2004-08-09 23:03:59 +000089 };
Misha Brukmanb05daff2004-08-09 23:03:59 +000090
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +000091 template <class CodeEmitter>
92 char Emitter<CodeEmitter>::ID = 0;
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000093}
94
Nate Begemaneb883af2006-08-23 21:08:52 +000095/// createPPCCodeEmitterPass - Return a pass that emits the collected PPC code
96/// to the specified MCE object.
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +000097
Nate Begemaneb883af2006-08-23 21:08:52 +000098FunctionPass *llvm::createPPCCodeEmitterPass(PPCTargetMachine &TM,
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +000099 MachineCodeEmitter &MCE) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000100 return new Emitter<MachineCodeEmitter>(TM, MCE);
Misha Brukmanb05daff2004-08-09 23:03:59 +0000101}
102
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000103FunctionPass *llvm::createPPCJITCodeEmitterPass(PPCTargetMachine &TM,
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000104 JITCodeEmitter &JCE) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000105 return new Emitter<JITCodeEmitter>(TM, JCE);
106}
107
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000108FunctionPass *llvm::createPPCObjectCodeEmitterPass(PPCTargetMachine &TM,
109 ObjectCodeEmitter &OCE) {
110 return new Emitter<ObjectCodeEmitter>(TM, OCE);
111}
112
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000113template <class CodeEmitter>
114bool Emitter<CodeEmitter>::runOnMachineFunction(MachineFunction &MF) {
Evan Cheng4c1aa862006-02-22 20:19:42 +0000115 assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
116 MF.getTarget().getRelocationModel() != Reloc::Static) &&
117 "JIT relocation model must be set to static or default!");
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000118
119 MCE.setModuleInfo(&getAnalysis<MachineModuleInfo>());
Chris Lattner43b429b2006-05-02 18:27:26 +0000120 do {
Chris Lattnere150b8e2006-12-08 04:54:03 +0000121 MovePCtoLROffset = 0;
Chris Lattner43b429b2006-05-02 18:27:26 +0000122 MCE.startFunction(MF);
Chris Lattner43b429b2006-05-02 18:27:26 +0000123 for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
124 emitBasicBlock(*BB);
Chris Lattner43b429b2006-05-02 18:27:26 +0000125 } while (MCE.finishFunction(MF));
Misha Brukman3070e2f2004-10-21 01:42:02 +0000126
Misha Brukmanb05daff2004-08-09 23:03:59 +0000127 return false;
128}
129
Bruno Cardoso Lopes434dd4f2009-06-01 19:57:37 +0000130template <class CodeEmitter>
131void Emitter<CodeEmitter>::emitBasicBlock(MachineBasicBlock &MBB) {
Chris Lattnerb4432f32006-05-03 17:10:41 +0000132 MCE.StartMachineBasicBlock(&MBB);
133
Misha Brukmana4df3502004-10-23 18:28:01 +0000134 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
Evan Chengacff3392008-09-02 06:51:36 +0000135 const MachineInstr &MI = *I;
Chris Lattnere94c5172004-11-23 05:59:53 +0000136 switch (MI.getOpcode()) {
137 default:
Evan Chengacff3392008-09-02 06:51:36 +0000138 MCE.emitWordBE(getBinaryCodeForInstr(MI));
Chris Lattnere94c5172004-11-23 05:59:53 +0000139 break;
Dan Gohman44066042008-07-01 00:05:16 +0000140 case TargetInstrInfo::DBG_LABEL:
141 case TargetInstrInfo::EH_LABEL:
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000142 MCE.emitLabel(MI.getOperand(0).getImm());
143 break;
Evan Chengd1833072008-03-17 06:56:52 +0000144 case TargetInstrInfo::IMPLICIT_DEF:
145 break; // pseudo opcode, no side effects
Chris Lattnere94c5172004-11-23 05:59:53 +0000146 case PPC::MovePCtoLR:
Chris Lattner6a5339b2006-11-14 18:44:47 +0000147 case PPC::MovePCtoLR8:
Chris Lattnere150b8e2006-12-08 04:54:03 +0000148 assert(TM.getRelocationModel() == Reloc::PIC_);
149 MovePCtoLROffset = (void*)MCE.getCurrentPCValue();
150 MCE.emitWordBE(0x48000005); // bl 1
Chris Lattnere94c5172004-11-23 05:59:53 +0000151 break;
152 }
Misha Brukmana4df3502004-10-23 18:28:01 +0000153 }
Misha Brukmanb05daff2004-08-09 23:03:59 +0000154}
155
Evan Chengacff3392008-09-02 06:51:36 +0000156unsigned PPCCodeEmitter::getMachineOpValue(const MachineInstr &MI,
157 const MachineOperand &MO) {
Misha Brukmanb5f662f2005-04-21 23:30:14 +0000158
Evan Chengacff3392008-09-02 06:51:36 +0000159 unsigned rv = 0; // Return value; defaults to 0 for unhandled cases
Chris Lattnerb4432f32006-05-03 17:10:41 +0000160 // or things that get fixed up later by the JIT.
Dan Gohmand735b802008-10-03 15:45:36 +0000161 if (MO.isReg()) {
Chris Lattner369503f2006-04-17 21:07:20 +0000162 rv = PPCRegisterInfo::getRegisterNumbering(MO.getReg());
Chris Lattnerf577c612005-04-19 05:41:52 +0000163
Nate Begemanadeb43d2005-07-20 22:42:00 +0000164 // Special encoding for MTCRF and MFOCRF, which uses a bit mask for the
Chris Lattnerf577c612005-04-19 05:41:52 +0000165 // register, not the register number directly.
Nate Begemanadeb43d2005-07-20 22:42:00 +0000166 if ((MI.getOpcode() == PPC::MTCRF || MI.getOpcode() == PPC::MFOCRF) &&
Chris Lattnerf577c612005-04-19 05:41:52 +0000167 (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7)) {
168 rv = 0x80 >> rv;
169 }
Dan Gohmand735b802008-10-03 15:45:36 +0000170 } else if (MO.isImm()) {
Chris Lattner9a1ceae2007-12-30 20:49:49 +0000171 rv = MO.getImm();
Dan Gohmand735b802008-10-03 15:45:36 +0000172 } else if (MO.isGlobal() || MO.isSymbol() ||
173 MO.isCPI() || MO.isJTI()) {
Chris Lattnerd7fa35c2004-11-23 15:56:38 +0000174 unsigned Reloc = 0;
Tilmann Scheller2a9ddfb2009-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 Schwaighofer30e62c02008-04-30 09:16:33 +0000177 MI.getOpcode() == PPC::TAILB || MI.getOpcode() == PPC::TAILB8)
Chris Lattnere94c5172004-11-23 05:59:53 +0000178 Reloc = PPC::reloc_pcrel_bx;
Chris Lattner5efb75d2004-11-24 22:30:08 +0000179 else {
Chris Lattnere150b8e2006-12-08 04:54:03 +0000180 if (TM.getRelocationModel() == Reloc::PIC_) {
181 assert(MovePCtoLROffset && "MovePCtoLR not seen yet?");
Chris Lattnere150b8e2006-12-08 04:54:03 +0000182 }
Nate Begeman2497e632005-07-21 20:44:43 +0000183 switch (MI.getOpcode()) {
Torok Edwinc25e7582009-07-11 20:10:48 +0000184 default: MI.dump(); LLVM_UNREACHABLE("Unknown instruction for relocation!");
Nate Begeman2497e632005-07-21 20:44:43 +0000185 case PPC::LIS:
Chris Lattner3bc8a762006-07-12 21:23:20 +0000186 case PPC::LIS8:
Nate Begemaneb883af2006-08-23 21:08:52 +0000187 case PPC::ADDIS:
Chris Lattner3bc8a762006-07-12 21:23:20 +0000188 case PPC::ADDIS8:
Nate Begeman6fcbd692006-04-21 22:04:15 +0000189 Reloc = PPC::reloc_absolute_high; // Pointer to symbol
Nate Begeman2497e632005-07-21 20:44:43 +0000190 break;
Chris Lattnerea4a9c52006-04-22 06:17:56 +0000191 case PPC::LI:
Chris Lattner3bc8a762006-07-12 21:23:20 +0000192 case PPC::LI8:
Nate Begeman2497e632005-07-21 20:44:43 +0000193 case PPC::LA:
Chris Lattner3bc8a762006-07-12 21:23:20 +0000194 // Loads.
Nate Begeman2497e632005-07-21 20:44:43 +0000195 case PPC::LBZ:
Jim Laskey34da7262006-12-15 16:44:10 +0000196 case PPC::LBZ8:
Nate Begeman2497e632005-07-21 20:44:43 +0000197 case PPC::LHA:
Jim Laskey34da7262006-12-15 16:44:10 +0000198 case PPC::LHA8:
Nate Begeman2497e632005-07-21 20:44:43 +0000199 case PPC::LHZ:
Jim Laskey34da7262006-12-15 16:44:10 +0000200 case PPC::LHZ8:
Nate Begeman2497e632005-07-21 20:44:43 +0000201 case PPC::LWZ:
Jim Laskey34da7262006-12-15 16:44:10 +0000202 case PPC::LWZ8:
Nate Begeman2497e632005-07-21 20:44:43 +0000203 case PPC::LFS:
204 case PPC::LFD:
Chris Lattner3bc8a762006-07-12 21:23:20 +0000205
206 // Stores.
Nate Begeman2497e632005-07-21 20:44:43 +0000207 case PPC::STB:
Jim Laskey34da7262006-12-15 16:44:10 +0000208 case PPC::STB8:
Nate Begeman2497e632005-07-21 20:44:43 +0000209 case PPC::STH:
Jim Laskey34da7262006-12-15 16:44:10 +0000210 case PPC::STH8:
Nate Begeman2497e632005-07-21 20:44:43 +0000211 case PPC::STW:
Jim Laskey34da7262006-12-15 16:44:10 +0000212 case PPC::STW8:
Nate Begeman2497e632005-07-21 20:44:43 +0000213 case PPC::STFS:
214 case PPC::STFD:
Nate Begeman6fcbd692006-04-21 22:04:15 +0000215 Reloc = PPC::reloc_absolute_low;
Nate Begeman2497e632005-07-21 20:44:43 +0000216 break;
Chris Lattner3bc8a762006-07-12 21:23:20 +0000217
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;
Chris Lattner5efb75d2004-11-24 22:30:08 +0000224 }
Chris Lattnere94c5172004-11-23 05:59:53 +0000225 }
Chris Lattner57fc62c2006-12-11 23:22:45 +0000226
227 MachineRelocation R;
Dan Gohmand735b802008-10-03 15:45:36 +0000228 if (MO.isGlobal()) {
Chris Lattner57fc62c2006-12-11 23:22:45 +0000229 R = MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
Evan Cheng165b60d2008-01-04 02:22:21 +0000230 MO.getGlobal(), 0,
231 isa<Function>(MO.getGlobal()));
Dan Gohmand735b802008-10-03 15:45:36 +0000232 } else if (MO.isSymbol()) {
Chris Lattner57fc62c2006-12-11 23:22:45 +0000233 R = MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
234 Reloc, MO.getSymbolName(), 0);
Dan Gohmand735b802008-10-03 15:45:36 +0000235 } else if (MO.isCPI()) {
Chris Lattner57fc62c2006-12-11 23:22:45 +0000236 R = MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
Chris Lattner8aa797a2007-12-30 23:10:15 +0000237 Reloc, MO.getIndex(), 0);
Chris Lattner57fc62c2006-12-11 23:22:45 +0000238 } else {
Dan Gohmand735b802008-10-03 15:45:36 +0000239 assert(MO.isJTI());
Chris Lattner57fc62c2006-12-11 23:22:45 +0000240 R = MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
Chris Lattner8aa797a2007-12-30 23:10:15 +0000241 Reloc, MO.getIndex(), 0);
Chris Lattner57fc62c2006-12-11 23:22:45 +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 Gohmand735b802008-10-03 15:45:36 +0000255 } else if (MO.isMBB()) {
Evan Chengf141cc42006-07-27 18:21:10 +0000256 unsigned Reloc = 0;
257 unsigned Opcode = MI.getOpcode();
Tilmann Scheller2a9ddfb2009-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)
Evan Chengf141cc42006-07-27 18:21:10 +0000261 Reloc = PPC::reloc_pcrel_bx;
Chris Lattnere150b8e2006-12-08 04:54:03 +0000262 else // BCC instruction
Evan Chengf141cc42006-07-27 18:21:10 +0000263 Reloc = PPC::reloc_pcrel_bcx;
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000264
Evan Chengf141cc42006-07-27 18:21:10 +0000265 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
Chris Lattner8aa797a2007-12-30 23:10:15 +0000266 Reloc, MO.getMBB()));
Chris Lattnerb9f26da2004-11-24 01:56:12 +0000267 } else {
Torok Edwindac237e2009-07-08 20:53:28 +0000268#ifndef NDEBUG
Bill Wendlingf5da1332006-12-07 22:21:48 +0000269 cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
Torok Edwindac237e2009-07-08 20:53:28 +0000270#endif
271 llvm_unreachable();
Misha Brukmana4df3502004-10-23 18:28:01 +0000272 }
Chris Lattnerb9f26da2004-11-24 01:56:12 +0000273
Misha Brukmanc982cfa2004-10-14 06:39:56 +0000274 return rv;
Misha Brukman5dfe3a92004-06-21 16:55:25 +0000275}
276
Chris Lattner4c7b43b2005-10-14 23:37:35 +0000277#include "PPCGenCodeEmitter.inc"
Misha Brukmanb05daff2004-08-09 23:03:59 +0000278