blob: b74195778cab5f0e1a798d8104271837ca67f106 [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,
61 public PPCCodeEmitter
62 {
63 TargetMachine &TM;
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +000064 CodeEmitter &MCE;
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000065
Nicolas Geoffray0e757e12008-02-13 18:39:37 +000066 void getAnalysisUsage(AnalysisUsage &AU) const {
67 AU.addRequired<MachineModuleInfo>();
68 MachineFunctionPass::getAnalysisUsage(AU);
69 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070
71 public:
72 static char ID;
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +000073 Emitter(TargetMachine &tm, CodeEmitter &mce)
74 : MachineFunctionPass(&ID), PPCCodeEmitter(tm, mce), TM(tm), MCE(mce) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +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
86 /// getValueBit - return the particular bit of Val
87 ///
88 unsigned getValueBit(int64_t Val, unsigned bit) { return (Val >> bit) & 1; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +000091 template <class CodeEmitter>
92 char Emitter<CodeEmitter>::ID = 0;
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000093}
94
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095/// createPPCCodeEmitterPass - Return a pass that emits the collected PPC code
96/// to the specified MCE object.
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +000097
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098FunctionPass *llvm::createPPCCodeEmitterPass(PPCTargetMachine &TM,
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +000099 MachineCodeEmitter &MCE) {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000100 return new Emitter<MachineCodeEmitter>(TM, MCE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101}
102
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000103FunctionPass *llvm::createPPCJITCodeEmitterPass(PPCTargetMachine &TM,
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000104 JITCodeEmitter &JCE) {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000105 return new Emitter<JITCodeEmitter>(TM, JCE);
106}
107
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000108FunctionPass *llvm::createPPCObjectCodeEmitterPass(PPCTargetMachine &TM,
109 ObjectCodeEmitter &OCE) {
110 return new Emitter<ObjectCodeEmitter>(TM, OCE);
111}
112
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000113template <class CodeEmitter>
114bool Emitter<CodeEmitter>::runOnMachineFunction(MachineFunction &MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Geoffray0e757e12008-02-13 18:39:37 +0000118
119 MCE.setModuleInfo(&getAnalysis<MachineModuleInfo>());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120 do {
121 MovePCtoLROffset = 0;
122 MCE.startFunction(MF);
123 for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
124 emitBasicBlock(*BB);
125 } while (MCE.finishFunction(MF));
126
127 return false;
128}
129
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000130template <class CodeEmitter>
131void Emitter<CodeEmitter>::emitBasicBlock(MachineBasicBlock &MBB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000132 MCE.StartMachineBasicBlock(&MBB);
133
134 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
Evan Cheng3ca89372008-09-02 06:51:36 +0000135 const MachineInstr &MI = *I;
Jeffrey Yasskin6628b5b2009-07-17 18:49:39 +0000136 MCE.processDebugLoc(MI.getDebugLoc());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137 switch (MI.getOpcode()) {
138 default:
Evan Cheng3ca89372008-09-02 06:51:36 +0000139 MCE.emitWordBE(getBinaryCodeForInstr(MI));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140 break;
Dan Gohmanfa607c92008-07-01 00:05:16 +0000141 case TargetInstrInfo::DBG_LABEL:
142 case TargetInstrInfo::EH_LABEL:
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000143 MCE.emitLabel(MI.getOperand(0).getImm());
144 break;
Evan Chengb74b4b62008-03-17 06:56:52 +0000145 case TargetInstrInfo::IMPLICIT_DEF:
146 break; // pseudo opcode, no side effects
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147 case PPC::MovePCtoLR:
148 case PPC::MovePCtoLR8:
149 assert(TM.getRelocationModel() == Reloc::PIC_);
150 MovePCtoLROffset = (void*)MCE.getCurrentPCValue();
151 MCE.emitWordBE(0x48000005); // bl 1
152 break;
153 }
154 }
155}
156
Evan Cheng3ca89372008-09-02 06:51:36 +0000157unsigned PPCCodeEmitter::getMachineOpValue(const MachineInstr &MI,
158 const MachineOperand &MO) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159
Evan Cheng3ca89372008-09-02 06:51:36 +0000160 unsigned rv = 0; // Return value; defaults to 0 for unhandled cases
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000161 // or things that get fixed up later by the JIT.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000162 if (MO.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163 rv = PPCRegisterInfo::getRegisterNumbering(MO.getReg());
164
165 // Special encoding for MTCRF and MFOCRF, which uses a bit mask for the
166 // register, not the register number directly.
167 if ((MI.getOpcode() == PPC::MTCRF || MI.getOpcode() == PPC::MFOCRF) &&
168 (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7)) {
169 rv = 0x80 >> rv;
170 }
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000171 } else if (MO.isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000172 rv = MO.getImm();
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000173 } else if (MO.isGlobal() || MO.isSymbol() ||
174 MO.isCPI() || MO.isJTI()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175 unsigned Reloc = 0;
Tilmann Scheller386330d2009-07-03 06:47:08 +0000176 if (MI.getOpcode() == PPC::BL_Darwin || MI.getOpcode() == PPC::BL8_Darwin ||
177 MI.getOpcode() == PPC::BL_SVR4 || MI.getOpcode() == PPC::BL8_ELF ||
Arnold Schwaighofera0032722008-04-30 09:16:33 +0000178 MI.getOpcode() == PPC::TAILB || MI.getOpcode() == PPC::TAILB8)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 Reloc = PPC::reloc_pcrel_bx;
180 else {
181 if (TM.getRelocationModel() == Reloc::PIC_) {
182 assert(MovePCtoLROffset && "MovePCtoLR not seen yet?");
183 }
184 switch (MI.getOpcode()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000185 default: MI.dump(); llvm_unreachable("Unknown instruction for relocation!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186 case PPC::LIS:
187 case PPC::LIS8:
188 case PPC::ADDIS:
189 case PPC::ADDIS8:
190 Reloc = PPC::reloc_absolute_high; // Pointer to symbol
191 break;
192 case PPC::LI:
193 case PPC::LI8:
194 case PPC::LA:
195 // Loads.
196 case PPC::LBZ:
197 case PPC::LBZ8:
198 case PPC::LHA:
199 case PPC::LHA8:
200 case PPC::LHZ:
201 case PPC::LHZ8:
202 case PPC::LWZ:
203 case PPC::LWZ8:
204 case PPC::LFS:
205 case PPC::LFD:
206
207 // Stores.
208 case PPC::STB:
209 case PPC::STB8:
210 case PPC::STH:
211 case PPC::STH8:
212 case PPC::STW:
213 case PPC::STW8:
214 case PPC::STFS:
215 case PPC::STFD:
216 Reloc = PPC::reloc_absolute_low;
217 break;
218
219 case PPC::LWA:
220 case PPC::LD:
221 case PPC::STD:
222 case PPC::STD_32:
223 Reloc = PPC::reloc_absolute_low_ix;
224 break;
225 }
226 }
227
228 MachineRelocation R;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000229 if (MO.isGlobal()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230 R = MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
Evan Cheng9f6942b2008-01-04 02:22:21 +0000231 MO.getGlobal(), 0,
232 isa<Function>(MO.getGlobal()));
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000233 } else if (MO.isSymbol()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000234 R = MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
235 Reloc, MO.getSymbolName(), 0);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000236 } else if (MO.isCPI()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237 R = MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
Chris Lattner6017d482007-12-30 23:10:15 +0000238 Reloc, MO.getIndex(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000239 } else {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000240 assert(MO.isJTI());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000241 R = MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
Chris Lattner6017d482007-12-30 23:10:15 +0000242 Reloc, MO.getIndex(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243 }
244
245 // If in PIC mode, we need to encode the negated address of the
246 // 'movepctolr' into the unrelocated field. After relocation, we'll have
247 // &gv-&movepctolr-4 in the imm field. Once &movepctolr is added to the imm
248 // field, we get &gv. This doesn't happen for branch relocations, which are
249 // always implicitly pc relative.
250 if (TM.getRelocationModel() == Reloc::PIC_ && Reloc != PPC::reloc_pcrel_bx){
251 assert(MovePCtoLROffset && "MovePCtoLR not seen yet?");
252 R.setConstantVal(-(intptr_t)MovePCtoLROffset - 4);
253 }
254 MCE.addRelocation(R);
255
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000256 } else if (MO.isMBB()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257 unsigned Reloc = 0;
258 unsigned Opcode = MI.getOpcode();
Tilmann Scheller386330d2009-07-03 06:47:08 +0000259 if (Opcode == PPC::B || Opcode == PPC::BL_Darwin ||
260 Opcode == PPC::BLA_Darwin|| Opcode == PPC::BL_SVR4 ||
261 Opcode == PPC::BLA_SVR4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262 Reloc = PPC::reloc_pcrel_bx;
263 else // BCC instruction
264 Reloc = PPC::reloc_pcrel_bcx;
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000265
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000266 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
Chris Lattner6017d482007-12-30 23:10:15 +0000267 Reloc, MO.getMBB()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000268 } else {
Edwin Török4d9756a2009-07-08 20:53:28 +0000269#ifndef NDEBUG
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000270 cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
Edwin Török4d9756a2009-07-08 20:53:28 +0000271#endif
Edwin Törökbd448e32009-07-14 16:55:14 +0000272 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000273 }
274
275 return rv;
276}
277
278#include "PPCGenCodeEmitter.inc"