blob: 837213994b28fae051263ce9efcef8fb4006939a [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"
Edwin Török4d9756a2009-07-08 20:53:28 +000028#include "llvm/Support/ErrorHandling.h"
29#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030#include "llvm/Target/TargetOptions.h"
31using namespace llvm;
32
33namespace {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000034 class PPCCodeEmitter {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035 TargetMachine &TM;
36 MachineCodeEmitter &MCE;
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000037 public:
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +000038 PPCCodeEmitter(TargetMachine &tm, MachineCodeEmitter &mce):
39 TM(tm), MCE(mce) {}
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000040
41 /// getBinaryCodeForInstr - This function, generated by the
42 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
43 /// machine instructions.
44
45 unsigned getBinaryCodeForInstr(const MachineInstr &MI);
46
47 /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
48
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +000049 unsigned getMachineOpValue(const MachineInstr &MI,
50 const MachineOperand &MO);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051
52 /// MovePCtoLROffset - When/if we see a MovePCtoLR instruction, we record
53 /// its address in the function into this pointer.
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000054
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055 void *MovePCtoLROffset;
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000056 };
57
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +000058 template <class CodeEmitter>
Nick Lewycky492d06e2009-10-25 06:33:48 +000059 class Emitter : public MachineFunctionPass, public PPCCodeEmitter {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000060 TargetMachine &TM;
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +000061 CodeEmitter &MCE;
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000062
Nicolas Geoffray0e757e12008-02-13 18:39:37 +000063 void getAnalysisUsage(AnalysisUsage &AU) const {
64 AU.addRequired<MachineModuleInfo>();
65 MachineFunctionPass::getAnalysisUsage(AU);
66 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067
68 public:
69 static char ID;
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +000070 Emitter(TargetMachine &tm, CodeEmitter &mce)
71 : MachineFunctionPass(&ID), PPCCodeEmitter(tm, mce), TM(tm), MCE(mce) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072
73 const char *getPassName() const { return "PowerPC Machine Code Emitter"; }
74
75 /// runOnMachineFunction - emits the given MachineFunction to memory
76 ///
77 bool runOnMachineFunction(MachineFunction &MF);
78
79 /// emitBasicBlock - emits the given MachineBasicBlock to memory
80 ///
81 void emitBasicBlock(MachineBasicBlock &MBB);
82
83 /// getValueBit - return the particular bit of Val
84 ///
85 unsigned getValueBit(int64_t Val, unsigned bit) { return (Val >> bit) & 1; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000086 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +000088 template <class CodeEmitter>
89 char Emitter<CodeEmitter>::ID = 0;
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000090}
Daniel Dunbar3be44e62009-09-20 02:20:51 +000091
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092/// createPPCCodeEmitterPass - Return a pass that emits the collected PPC code
93/// to the specified MCE object.
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000094FunctionPass *llvm::createPPCJITCodeEmitterPass(PPCTargetMachine &TM,
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +000095 JITCodeEmitter &JCE) {
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000096 return new Emitter<JITCodeEmitter>(TM, JCE);
97}
98
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +000099template <class CodeEmitter>
100bool Emitter<CodeEmitter>::runOnMachineFunction(MachineFunction &MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101 assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
102 MF.getTarget().getRelocationModel() != Reloc::Static) &&
103 "JIT relocation model must be set to static or default!");
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000104
105 MCE.setModuleInfo(&getAnalysis<MachineModuleInfo>());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106 do {
107 MovePCtoLROffset = 0;
108 MCE.startFunction(MF);
109 for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
110 emitBasicBlock(*BB);
111 } while (MCE.finishFunction(MF));
112
113 return false;
114}
115
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +0000116template <class CodeEmitter>
117void Emitter<CodeEmitter>::emitBasicBlock(MachineBasicBlock &MBB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118 MCE.StartMachineBasicBlock(&MBB);
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000119
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
Evan Cheng3ca89372008-09-02 06:51:36 +0000121 const MachineInstr &MI = *I;
Devang Patel5450fc12009-10-06 02:19:11 +0000122 MCE.processDebugLoc(MI.getDebugLoc(), true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123 switch (MI.getOpcode()) {
124 default:
Evan Cheng3ca89372008-09-02 06:51:36 +0000125 MCE.emitWordBE(getBinaryCodeForInstr(MI));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126 break;
Dan Gohmanfa607c92008-07-01 00:05:16 +0000127 case TargetInstrInfo::DBG_LABEL:
128 case TargetInstrInfo::EH_LABEL:
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000129 MCE.emitLabel(MI.getOperand(0).getImm());
130 break;
Evan Chengb74b4b62008-03-17 06:56:52 +0000131 case TargetInstrInfo::IMPLICIT_DEF:
Jakob Stoklund Olesen8f12c7c2009-09-28 20:32:26 +0000132 case TargetInstrInfo::KILL:
Evan Chengb74b4b62008-03-17 06:56:52 +0000133 break; // pseudo opcode, no side effects
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134 case PPC::MovePCtoLR:
135 case PPC::MovePCtoLR8:
136 assert(TM.getRelocationModel() == Reloc::PIC_);
137 MovePCtoLROffset = (void*)MCE.getCurrentPCValue();
138 MCE.emitWordBE(0x48000005); // bl 1
139 break;
140 }
Devang Patel5450fc12009-10-06 02:19:11 +0000141 MCE.processDebugLoc(MI.getDebugLoc(), false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142 }
143}
144
Evan Cheng3ca89372008-09-02 06:51:36 +0000145unsigned PPCCodeEmitter::getMachineOpValue(const MachineInstr &MI,
146 const MachineOperand &MO) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147
Evan Cheng3ca89372008-09-02 06:51:36 +0000148 unsigned rv = 0; // Return value; defaults to 0 for unhandled cases
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149 // or things that get fixed up later by the JIT.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000150 if (MO.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151 rv = PPCRegisterInfo::getRegisterNumbering(MO.getReg());
152
153 // Special encoding for MTCRF and MFOCRF, which uses a bit mask for the
154 // register, not the register number directly.
155 if ((MI.getOpcode() == PPC::MTCRF || MI.getOpcode() == PPC::MFOCRF) &&
156 (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7)) {
157 rv = 0x80 >> rv;
158 }
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000159 } else if (MO.isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000160 rv = MO.getImm();
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000161 } else if (MO.isGlobal() || MO.isSymbol() ||
162 MO.isCPI() || MO.isJTI()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163 unsigned Reloc = 0;
Tilmann Scheller386330d2009-07-03 06:47:08 +0000164 if (MI.getOpcode() == PPC::BL_Darwin || MI.getOpcode() == PPC::BL8_Darwin ||
165 MI.getOpcode() == PPC::BL_SVR4 || MI.getOpcode() == PPC::BL8_ELF ||
Arnold Schwaighofera0032722008-04-30 09:16:33 +0000166 MI.getOpcode() == PPC::TAILB || MI.getOpcode() == PPC::TAILB8)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167 Reloc = PPC::reloc_pcrel_bx;
168 else {
169 if (TM.getRelocationModel() == Reloc::PIC_) {
170 assert(MovePCtoLROffset && "MovePCtoLR not seen yet?");
171 }
172 switch (MI.getOpcode()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000173 default: MI.dump(); llvm_unreachable("Unknown instruction for relocation!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174 case PPC::LIS:
175 case PPC::LIS8:
176 case PPC::ADDIS:
177 case PPC::ADDIS8:
178 Reloc = PPC::reloc_absolute_high; // Pointer to symbol
179 break;
180 case PPC::LI:
181 case PPC::LI8:
182 case PPC::LA:
183 // Loads.
184 case PPC::LBZ:
185 case PPC::LBZ8:
186 case PPC::LHA:
187 case PPC::LHA8:
188 case PPC::LHZ:
189 case PPC::LHZ8:
190 case PPC::LWZ:
191 case PPC::LWZ8:
192 case PPC::LFS:
193 case PPC::LFD:
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000194
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195 // Stores.
196 case PPC::STB:
197 case PPC::STB8:
198 case PPC::STH:
199 case PPC::STH8:
200 case PPC::STW:
201 case PPC::STW8:
202 case PPC::STFS:
203 case PPC::STFD:
204 Reloc = PPC::reloc_absolute_low;
205 break;
206
207 case PPC::LWA:
208 case PPC::LD:
209 case PPC::STD:
210 case PPC::STD_32:
211 Reloc = PPC::reloc_absolute_low_ix;
212 break;
213 }
214 }
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000215
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000216 MachineRelocation R;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000217 if (MO.isGlobal()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000218 R = MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
Evan Cheng9f6942b2008-01-04 02:22:21 +0000219 MO.getGlobal(), 0,
220 isa<Function>(MO.getGlobal()));
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000221 } else if (MO.isSymbol()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000222 R = MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
223 Reloc, MO.getSymbolName(), 0);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000224 } else if (MO.isCPI()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000225 R = MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
Chris Lattner6017d482007-12-30 23:10:15 +0000226 Reloc, MO.getIndex(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000227 } else {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000228 assert(MO.isJTI());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229 R = MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
Chris Lattner6017d482007-12-30 23:10:15 +0000230 Reloc, MO.getIndex(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231 }
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000232
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233 // If in PIC mode, we need to encode the negated address of the
234 // 'movepctolr' into the unrelocated field. After relocation, we'll have
235 // &gv-&movepctolr-4 in the imm field. Once &movepctolr is added to the imm
236 // field, we get &gv. This doesn't happen for branch relocations, which are
237 // always implicitly pc relative.
238 if (TM.getRelocationModel() == Reloc::PIC_ && Reloc != PPC::reloc_pcrel_bx){
239 assert(MovePCtoLROffset && "MovePCtoLR not seen yet?");
240 R.setConstantVal(-(intptr_t)MovePCtoLROffset - 4);
241 }
242 MCE.addRelocation(R);
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000243
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000244 } else if (MO.isMBB()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000245 unsigned Reloc = 0;
246 unsigned Opcode = MI.getOpcode();
Tilmann Scheller386330d2009-07-03 06:47:08 +0000247 if (Opcode == PPC::B || Opcode == PPC::BL_Darwin ||
248 Opcode == PPC::BLA_Darwin|| Opcode == PPC::BL_SVR4 ||
249 Opcode == PPC::BLA_SVR4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 Reloc = PPC::reloc_pcrel_bx;
251 else // BCC instruction
252 Reloc = PPC::reloc_pcrel_bcx;
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000253
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
Chris Lattner6017d482007-12-30 23:10:15 +0000255 Reloc, MO.getMBB()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000256 } else {
Edwin Török4d9756a2009-07-08 20:53:28 +0000257#ifndef NDEBUG
Chris Lattnerd71b0b02009-08-23 03:41:05 +0000258 errs() << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
Edwin Török4d9756a2009-07-08 20:53:28 +0000259#endif
Edwin Törökbd448e32009-07-14 16:55:14 +0000260 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261 }
262
263 return rv;
264}
265
266#include "PPCGenCodeEmitter.inc"