blob: 952a45684a9584e69ee834ccccc1ba8b52d24712 [file] [log] [blame]
Nate Begeman6cca84e2005-10-16 05:39:50 +00001//===-- PPCCodeEmitter.cpp - JIT Code Emitter for PowerPC32 -------*- C++ -*-=//
Misha Brukmanb4402432005-04-21 23:30:14 +00002//
Misha Brukmane05203f2004-06-21 16:55:25 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukmanb4402432005-04-21 23:30:14 +00007//
Misha Brukmane05203f2004-06-21 16:55:25 +00008//===----------------------------------------------------------------------===//
Misha Brukmanb4402432005-04-21 23:30:14 +00009//
Misha Brukman8b1bf432004-10-14 06:07:25 +000010// This file defines the PowerPC 32-bit CodeEmitter and associated machinery to
Gabor Greife16561c2007-07-05 17:07:56 +000011// JIT-compile bitcode to native PowerPC.
Misha Brukmane05203f2004-06-21 16:55:25 +000012//
13//===----------------------------------------------------------------------===//
14
Chris Lattner6f3b9542005-10-14 23:59:06 +000015#include "PPCTargetMachine.h"
16#include "PPCRelocations.h"
Chris Lattnerbfca1ab2005-10-14 23:51:18 +000017#include "PPC.h"
Misha Brukman2beb63a2004-10-21 01:42:02 +000018#include "llvm/Module.h"
Chris Lattner03354282005-10-15 21:58:54 +000019#include "llvm/PassManager.h"
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +000020#include "llvm/CodeGen/JITCodeEmitter.h"
Misha Brukman18922912004-08-09 23:03:59 +000021#include "llvm/CodeGen/MachineFunctionPass.h"
Misha Brukman9ce0da92004-10-23 23:47:34 +000022#include "llvm/CodeGen/MachineInstrBuilder.h"
Nicolas Geoffray21ad4942008-02-13 18:39:37 +000023#include "llvm/CodeGen/MachineModuleInfo.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000024#include "llvm/Support/ErrorHandling.h"
25#include "llvm/Support/raw_ostream.h"
Evan Cheng5f997602006-02-18 00:08:58 +000026#include "llvm/Target/TargetOptions.h"
Chris Lattnerf2429792004-11-16 04:47:33 +000027using namespace llvm;
Misha Brukmane05203f2004-06-21 16:55:25 +000028
Misha Brukman18922912004-08-09 23:03:59 +000029namespace {
Chris Lattner308acc42010-02-02 21:55:58 +000030 class PPCCodeEmitter : public MachineFunctionPass {
Misha Brukman18922912004-08-09 23:03:59 +000031 TargetMachine &TM;
Chris Lattner308acc42010-02-02 21:55:58 +000032 JITCodeEmitter &MCE;
Chris Lattner34adc8d2010-03-14 01:41:15 +000033 MachineModuleInfo *MMI;
Chris Lattner308acc42010-02-02 21:55:58 +000034
35 void getAnalysisUsage(AnalysisUsage &AU) const {
36 AU.addRequired<MachineModuleInfo>();
37 MachineFunctionPass::getAnalysisUsage(AU);
38 }
39
40 static char ID;
41
42 /// MovePCtoLROffset - When/if we see a MovePCtoLR instruction, we record
43 /// its address in the function into this pointer.
44 void *MovePCtoLROffset;
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +000045 public:
Chris Lattner308acc42010-02-02 21:55:58 +000046
47 PPCCodeEmitter(TargetMachine &tm, JITCodeEmitter &mce)
Owen Andersona7aed182010-08-06 18:33:48 +000048 : MachineFunctionPass(ID), TM(tm), MCE(mce) {}
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +000049
50 /// getBinaryCodeForInstr - This function, generated by the
51 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
52 /// machine instructions.
Jim Grosbacha7b6d582010-10-08 00:21:28 +000053 unsigned getBinaryCodeForInstr(const MachineInstr &MI) const;
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +000054
Chris Lattner79fa3712010-11-15 05:57:53 +000055
56 MachineRelocation GetRelocation(const MachineOperand &MO,
57 unsigned RelocID) const;
58
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +000059 /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +000060 unsigned getMachineOpValue(const MachineInstr &MI,
Jim Grosbacha7b6d582010-10-08 00:21:28 +000061 const MachineOperand &MO) const;
Misha Brukman18922912004-08-09 23:03:59 +000062
Chris Lattnerd6a07cc2010-11-15 05:19:25 +000063 unsigned get_crbitm_encoding(const MachineInstr &MI, unsigned OpNo) const;
Chris Lattner0e3461e2010-11-15 06:09:35 +000064 unsigned getDirectBrEncoding(const MachineInstr &MI, unsigned OpNo) const;
65 unsigned getCondBrEncoding(const MachineInstr &MI, unsigned OpNo) const;
Chris Lattnerd6a07cc2010-11-15 05:19:25 +000066
Misha Brukman18922912004-08-09 23:03:59 +000067 const char *getPassName() const { return "PowerPC Machine Code Emitter"; }
68
69 /// runOnMachineFunction - emits the given MachineFunction to memory
70 ///
71 bool runOnMachineFunction(MachineFunction &MF);
72
73 /// emitBasicBlock - emits the given MachineBasicBlock to memory
74 ///
75 void emitBasicBlock(MachineBasicBlock &MBB);
Misha Brukman18922912004-08-09 23:03:59 +000076 };
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +000077}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +000078
Chris Lattner308acc42010-02-02 21:55:58 +000079char PPCCodeEmitter::ID = 0;
80
Nate Begeman3cb39212006-08-23 21:08:52 +000081/// createPPCCodeEmitterPass - Return a pass that emits the collected PPC code
82/// to the specified MCE object.
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +000083FunctionPass *llvm::createPPCJITCodeEmitterPass(PPCTargetMachine &TM,
Bruno Cardoso Lopes9fd794b2009-06-01 19:57:37 +000084 JITCodeEmitter &JCE) {
Chris Lattner308acc42010-02-02 21:55:58 +000085 return new PPCCodeEmitter(TM, JCE);
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +000086}
87
Chris Lattner308acc42010-02-02 21:55:58 +000088bool PPCCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
Evan Cheng73136df2006-02-22 20:19:42 +000089 assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
90 MF.getTarget().getRelocationModel() != Reloc::Static) &&
91 "JIT relocation model must be set to static or default!");
Nicolas Geoffray21ad4942008-02-13 18:39:37 +000092
Chris Lattner34adc8d2010-03-14 01:41:15 +000093 MMI = &getAnalysis<MachineModuleInfo>();
94 MCE.setModuleInfo(MMI);
Chris Lattnerc9aa3712006-05-02 18:27:26 +000095 do {
Chris Lattner09fecf92006-12-08 04:54:03 +000096 MovePCtoLROffset = 0;
Chris Lattnerc9aa3712006-05-02 18:27:26 +000097 MCE.startFunction(MF);
Chris Lattnerc9aa3712006-05-02 18:27:26 +000098 for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
99 emitBasicBlock(*BB);
Chris Lattnerc9aa3712006-05-02 18:27:26 +0000100 } while (MCE.finishFunction(MF));
Misha Brukman2beb63a2004-10-21 01:42:02 +0000101
Misha Brukman18922912004-08-09 23:03:59 +0000102 return false;
103}
104
Chris Lattner308acc42010-02-02 21:55:58 +0000105void PPCCodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
Chris Lattner1d8ee1f2006-05-03 17:10:41 +0000106 MCE.StartMachineBasicBlock(&MBB);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000107
Misha Brukman421c3c12004-10-23 18:28:01 +0000108 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
Evan Cheng34f3a962008-09-02 06:51:36 +0000109 const MachineInstr &MI = *I;
Devang Patel051454a2009-10-06 02:19:11 +0000110 MCE.processDebugLoc(MI.getDebugLoc(), true);
Chris Lattner743a4342004-11-23 05:59:53 +0000111 switch (MI.getOpcode()) {
112 default:
Evan Cheng34f3a962008-09-02 06:51:36 +0000113 MCE.emitWordBE(getBinaryCodeForInstr(MI));
Chris Lattner743a4342004-11-23 05:59:53 +0000114 break;
Bill Wendling499f7972010-07-16 22:20:36 +0000115 case TargetOpcode::PROLOG_LABEL:
Chris Lattneree2fbbc2010-03-14 02:33:54 +0000116 case TargetOpcode::EH_LABEL:
117 MCE.emitLabel(MI.getOperand(0).getMCSymbol());
118 break;
Chris Lattnerb06015a2010-02-09 19:54:29 +0000119 case TargetOpcode::IMPLICIT_DEF:
120 case TargetOpcode::KILL:
Evan Cheng24bc1232008-03-17 06:56:52 +0000121 break; // pseudo opcode, no side effects
Chris Lattner743a4342004-11-23 05:59:53 +0000122 case PPC::MovePCtoLR:
Chris Lattner44dbdbe2006-11-14 18:44:47 +0000123 case PPC::MovePCtoLR8:
Chris Lattner09fecf92006-12-08 04:54:03 +0000124 assert(TM.getRelocationModel() == Reloc::PIC_);
125 MovePCtoLROffset = (void*)MCE.getCurrentPCValue();
126 MCE.emitWordBE(0x48000005); // bl 1
Chris Lattner743a4342004-11-23 05:59:53 +0000127 break;
128 }
Devang Patel051454a2009-10-06 02:19:11 +0000129 MCE.processDebugLoc(MI.getDebugLoc(), false);
Misha Brukman421c3c12004-10-23 18:28:01 +0000130 }
Misha Brukman18922912004-08-09 23:03:59 +0000131}
132
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000133unsigned PPCCodeEmitter::get_crbitm_encoding(const MachineInstr &MI,
134 unsigned OpNo) const {
135 const MachineOperand &MO = MI.getOperand(OpNo);
136 assert((MI.getOpcode() == PPC::MTCRF || MI.getOpcode() == PPC::MFOCRF) &&
137 (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7));
138 return 0x80 >> PPCRegisterInfo::getRegisterNumbering(MO.getReg());
139}
140
Chris Lattner79fa3712010-11-15 05:57:53 +0000141MachineRelocation PPCCodeEmitter::GetRelocation(const MachineOperand &MO,
142 unsigned RelocID) const {
143 if (MO.isGlobal())
144 return MachineRelocation::getGV(MCE.getCurrentPCOffset(), RelocID,
145 const_cast<GlobalValue *>(MO.getGlobal()),0,
146 isa<Function>(MO.getGlobal()));
147 if (MO.isSymbol())
148 return MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
149 RelocID, MO.getSymbolName(), 0);
150 if (MO.isCPI())
151 return MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
152 RelocID, MO.getIndex(), 0);
153
154 if (MO.isMBB())
155 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
156 RelocID, MO.getMBB()));
157
158 assert(MO.isJTI());
159 return MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
160 RelocID, MO.getIndex(), 0);
161}
162
Chris Lattner0e3461e2010-11-15 06:09:35 +0000163unsigned PPCCodeEmitter::getDirectBrEncoding(const MachineInstr &MI,
164 unsigned OpNo) const {
Chris Lattner79fa3712010-11-15 05:57:53 +0000165 const MachineOperand &MO = MI.getOperand(OpNo);
166 if (MO.isReg() || MO.isImm()) return getMachineOpValue(MI, MO);
167
168 MCE.addRelocation(GetRelocation(MO, PPC::reloc_pcrel_bx));
169 return 0;
170}
171
Chris Lattner0e3461e2010-11-15 06:09:35 +0000172unsigned PPCCodeEmitter::getCondBrEncoding(const MachineInstr &MI,
173 unsigned OpNo) const {
174 const MachineOperand &MO = MI.getOperand(OpNo);
175 MCE.addRelocation(GetRelocation(MO, PPC::reloc_pcrel_bcx));
176 return 0;
177}
178
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000179
Evan Cheng34f3a962008-09-02 06:51:36 +0000180unsigned PPCCodeEmitter::getMachineOpValue(const MachineInstr &MI,
Jim Grosbacha7b6d582010-10-08 00:21:28 +0000181 const MachineOperand &MO) const {
Misha Brukmanb4402432005-04-21 23:30:14 +0000182
Dan Gohman0d1e9a82008-10-03 15:45:36 +0000183 if (MO.isReg()) {
Chris Lattnerd6a07cc2010-11-15 05:19:25 +0000184 assert(MI.getOpcode() != PPC::MTCRF && MI.getOpcode() != PPC::MFOCRF);
185 return PPCRegisterInfo::getRegisterNumbering(MO.getReg());
186 }
187
188 if (MO.isImm())
189 return MO.getImm();
190
191 if (MO.isGlobal() || MO.isSymbol() || MO.isCPI() || MO.isJTI()) {
Chris Lattnerb1fd07a2004-11-23 15:56:38 +0000192 unsigned Reloc = 0;
Chris Lattner79fa3712010-11-15 05:57:53 +0000193 assert((TM.getRelocationModel() != Reloc::PIC_ || MovePCtoLROffset) &&
194 "MovePCtoLR not seen yet?");
195 switch (MI.getOpcode()) {
196 default: MI.dump(); llvm_unreachable("Unknown instruction for relocation!");
197 case PPC::LIS:
198 case PPC::LIS8:
199 case PPC::ADDIS:
200 case PPC::ADDIS8:
201 Reloc = PPC::reloc_absolute_high; // Pointer to symbol
202 break;
203 case PPC::LI:
204 case PPC::LI8:
205 case PPC::LA:
206 // Loads.
207 case PPC::LBZ:
208 case PPC::LBZ8:
209 case PPC::LHA:
210 case PPC::LHA8:
211 case PPC::LHZ:
212 case PPC::LHZ8:
213 case PPC::LWZ:
214 case PPC::LWZ8:
215 case PPC::LFS:
216 case PPC::LFD:
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000217
Chris Lattner79fa3712010-11-15 05:57:53 +0000218 // Stores.
219 case PPC::STB:
220 case PPC::STB8:
221 case PPC::STH:
222 case PPC::STH8:
223 case PPC::STW:
224 case PPC::STW8:
225 case PPC::STFS:
226 case PPC::STFD:
227 Reloc = PPC::reloc_absolute_low;
228 break;
Chris Lattner5b17dee2006-07-12 21:23:20 +0000229
Chris Lattner79fa3712010-11-15 05:57:53 +0000230 case PPC::LWA:
231 case PPC::LD:
232 case PPC::STD:
233 case PPC::STD_32:
234 Reloc = PPC::reloc_absolute_low_ix;
235 break;
Chris Lattner743a4342004-11-23 05:59:53 +0000236 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000237
Chris Lattner79fa3712010-11-15 05:57:53 +0000238 MachineRelocation R = GetRelocation(MO, Reloc);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000239
Chris Lattnerf4646a72006-12-11 23:22:45 +0000240 // If in PIC mode, we need to encode the negated address of the
241 // 'movepctolr' into the unrelocated field. After relocation, we'll have
242 // &gv-&movepctolr-4 in the imm field. Once &movepctolr is added to the imm
243 // field, we get &gv. This doesn't happen for branch relocations, which are
244 // always implicitly pc relative.
Chris Lattner79fa3712010-11-15 05:57:53 +0000245 if (TM.getRelocationModel() == Reloc::PIC_) {
Chris Lattnerf4646a72006-12-11 23:22:45 +0000246 assert(MovePCtoLROffset && "MovePCtoLR not seen yet?");
247 R.setConstantVal(-(intptr_t)MovePCtoLROffset - 4);
248 }
249 MCE.addRelocation(R);
Chris Lattner6a7ebe02004-11-24 01:56:12 +0000250 } else {
Torok Edwinfb8d6d52009-07-08 20:53:28 +0000251#ifndef NDEBUG
Chris Lattnera6f074f2009-08-23 03:41:05 +0000252 errs() << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
Torok Edwinfb8d6d52009-07-08 20:53:28 +0000253#endif
Torok Edwinfbcc6632009-07-14 16:55:14 +0000254 llvm_unreachable(0);
Misha Brukman421c3c12004-10-23 18:28:01 +0000255 }
Chris Lattner6a7ebe02004-11-24 01:56:12 +0000256
Chris Lattner79fa3712010-11-15 05:57:53 +0000257 return 0;
Misha Brukmane05203f2004-06-21 16:55:25 +0000258}
259
Chris Lattner0921e3b2005-10-14 23:37:35 +0000260#include "PPCGenCodeEmitter.inc"