blob: 3d8daf7586dde6d2a31f69038fc92fa1e8dfc77f [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"
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000020#include "llvm/CodeGen/JITCodeEmitter.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include "llvm/CodeGen/MachineFunctionPass.h"
22#include "llvm/CodeGen/MachineInstrBuilder.h"
Nicolas Geoffray0e757e12008-02-13 18:39:37 +000023#include "llvm/CodeGen/MachineModuleInfo.h"
Edwin Török4d9756a2009-07-08 20:53:28 +000024#include "llvm/Support/ErrorHandling.h"
25#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026#include "llvm/Target/TargetOptions.h"
27using namespace llvm;
28
29namespace {
Chris Lattner90ae6ff2010-02-02 21:55:58 +000030 class PPCCodeEmitter : public MachineFunctionPass {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031 TargetMachine &TM;
Chris Lattner90ae6ff2010-02-02 21:55:58 +000032 JITCodeEmitter &MCE;
Chris Lattnere3330172010-03-14 01:41:15 +000033 MachineModuleInfo *MMI;
Chris Lattner90ae6ff2010-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 Lopes1ea31ff2009-05-30 20:51:52 +000045 public:
Chris Lattner90ae6ff2010-02-02 21:55:58 +000046
47 PPCCodeEmitter(TargetMachine &tm, JITCodeEmitter &mce)
48 : MachineFunctionPass(&ID), TM(tm), MCE(mce) {}
Bruno Cardoso Lopes1ea31ff2009-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.
53
54 unsigned getBinaryCodeForInstr(const MachineInstr &MI);
55
56 /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
57
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +000058 unsigned getMachineOpValue(const MachineInstr &MI,
59 const MachineOperand &MO);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061 const char *getPassName() const { return "PowerPC Machine Code Emitter"; }
62
63 /// runOnMachineFunction - emits the given MachineFunction to memory
64 ///
65 bool runOnMachineFunction(MachineFunction &MF);
66
67 /// emitBasicBlock - emits the given MachineBasicBlock to memory
68 ///
69 void emitBasicBlock(MachineBasicBlock &MBB);
70
71 /// getValueBit - return the particular bit of Val
72 ///
73 unsigned getValueBit(int64_t Val, unsigned bit) { return (Val >> bit) & 1; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074 };
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000075}
Daniel Dunbar3be44e62009-09-20 02:20:51 +000076
Chris Lattner90ae6ff2010-02-02 21:55:58 +000077char PPCCodeEmitter::ID = 0;
78
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079/// createPPCCodeEmitterPass - Return a pass that emits the collected PPC code
80/// to the specified MCE object.
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000081FunctionPass *llvm::createPPCJITCodeEmitterPass(PPCTargetMachine &TM,
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +000082 JITCodeEmitter &JCE) {
Chris Lattner90ae6ff2010-02-02 21:55:58 +000083 return new PPCCodeEmitter(TM, JCE);
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000084}
85
Chris Lattner90ae6ff2010-02-02 21:55:58 +000086bool PPCCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087 assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
88 MF.getTarget().getRelocationModel() != Reloc::Static) &&
89 "JIT relocation model must be set to static or default!");
Nicolas Geoffray0e757e12008-02-13 18:39:37 +000090
Chris Lattnere3330172010-03-14 01:41:15 +000091 MMI = &getAnalysis<MachineModuleInfo>();
92 MCE.setModuleInfo(MMI);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093 do {
94 MovePCtoLROffset = 0;
95 MCE.startFunction(MF);
96 for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
97 emitBasicBlock(*BB);
98 } while (MCE.finishFunction(MF));
99
100 return false;
101}
102
Chris Lattner90ae6ff2010-02-02 21:55:58 +0000103void PPCCodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104 MCE.StartMachineBasicBlock(&MBB);
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000105
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
Evan Cheng3ca89372008-09-02 06:51:36 +0000107 const MachineInstr &MI = *I;
Devang Patel5450fc12009-10-06 02:19:11 +0000108 MCE.processDebugLoc(MI.getDebugLoc(), true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109 switch (MI.getOpcode()) {
110 default:
Evan Cheng3ca89372008-09-02 06:51:36 +0000111 MCE.emitWordBE(getBinaryCodeForInstr(MI));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112 break;
Chris Lattner4052b292010-02-09 19:54:29 +0000113 case TargetOpcode::DBG_LABEL:
114 case TargetOpcode::EH_LABEL:
Chris Lattnere3330172010-03-14 01:41:15 +0000115 MCE.emitLabel(MMI->getLabelSym(MI.getOperand(0).getImm()));
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000116 break;
Chris Lattner4052b292010-02-09 19:54:29 +0000117 case TargetOpcode::IMPLICIT_DEF:
118 case TargetOpcode::KILL:
Evan Chengb74b4b62008-03-17 06:56:52 +0000119 break; // pseudo opcode, no side effects
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120 case PPC::MovePCtoLR:
121 case PPC::MovePCtoLR8:
122 assert(TM.getRelocationModel() == Reloc::PIC_);
123 MovePCtoLROffset = (void*)MCE.getCurrentPCValue();
124 MCE.emitWordBE(0x48000005); // bl 1
125 break;
126 }
Devang Patel5450fc12009-10-06 02:19:11 +0000127 MCE.processDebugLoc(MI.getDebugLoc(), false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000128 }
129}
130
Evan Cheng3ca89372008-09-02 06:51:36 +0000131unsigned PPCCodeEmitter::getMachineOpValue(const MachineInstr &MI,
132 const MachineOperand &MO) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133
Evan Cheng3ca89372008-09-02 06:51:36 +0000134 unsigned rv = 0; // Return value; defaults to 0 for unhandled cases
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135 // or things that get fixed up later by the JIT.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000136 if (MO.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137 rv = PPCRegisterInfo::getRegisterNumbering(MO.getReg());
138
139 // Special encoding for MTCRF and MFOCRF, which uses a bit mask for the
140 // register, not the register number directly.
141 if ((MI.getOpcode() == PPC::MTCRF || MI.getOpcode() == PPC::MFOCRF) &&
142 (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7)) {
143 rv = 0x80 >> rv;
144 }
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000145 } else if (MO.isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000146 rv = MO.getImm();
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000147 } else if (MO.isGlobal() || MO.isSymbol() ||
148 MO.isCPI() || MO.isJTI()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149 unsigned Reloc = 0;
Tilmann Scheller386330d2009-07-03 06:47:08 +0000150 if (MI.getOpcode() == PPC::BL_Darwin || MI.getOpcode() == PPC::BL8_Darwin ||
151 MI.getOpcode() == PPC::BL_SVR4 || MI.getOpcode() == PPC::BL8_ELF ||
Arnold Schwaighofera0032722008-04-30 09:16:33 +0000152 MI.getOpcode() == PPC::TAILB || MI.getOpcode() == PPC::TAILB8)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153 Reloc = PPC::reloc_pcrel_bx;
154 else {
155 if (TM.getRelocationModel() == Reloc::PIC_) {
156 assert(MovePCtoLROffset && "MovePCtoLR not seen yet?");
157 }
158 switch (MI.getOpcode()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000159 default: MI.dump(); llvm_unreachable("Unknown instruction for relocation!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 case PPC::LIS:
161 case PPC::LIS8:
162 case PPC::ADDIS:
163 case PPC::ADDIS8:
164 Reloc = PPC::reloc_absolute_high; // Pointer to symbol
165 break;
166 case PPC::LI:
167 case PPC::LI8:
168 case PPC::LA:
169 // Loads.
170 case PPC::LBZ:
171 case PPC::LBZ8:
172 case PPC::LHA:
173 case PPC::LHA8:
174 case PPC::LHZ:
175 case PPC::LHZ8:
176 case PPC::LWZ:
177 case PPC::LWZ8:
178 case PPC::LFS:
179 case PPC::LFD:
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000180
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 // Stores.
182 case PPC::STB:
183 case PPC::STB8:
184 case PPC::STH:
185 case PPC::STH8:
186 case PPC::STW:
187 case PPC::STW8:
188 case PPC::STFS:
189 case PPC::STFD:
190 Reloc = PPC::reloc_absolute_low;
191 break;
192
193 case PPC::LWA:
194 case PPC::LD:
195 case PPC::STD:
196 case PPC::STD_32:
197 Reloc = PPC::reloc_absolute_low_ix;
198 break;
199 }
200 }
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000201
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202 MachineRelocation R;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000203 if (MO.isGlobal()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000204 R = MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
Evan Cheng9f6942b2008-01-04 02:22:21 +0000205 MO.getGlobal(), 0,
206 isa<Function>(MO.getGlobal()));
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000207 } else if (MO.isSymbol()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 R = MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
209 Reloc, MO.getSymbolName(), 0);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000210 } else if (MO.isCPI()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 R = MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
Chris Lattner6017d482007-12-30 23:10:15 +0000212 Reloc, MO.getIndex(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213 } else {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000214 assert(MO.isJTI());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000215 R = MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
Chris Lattner6017d482007-12-30 23:10:15 +0000216 Reloc, MO.getIndex(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217 }
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000218
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000219 // If in PIC mode, we need to encode the negated address of the
220 // 'movepctolr' into the unrelocated field. After relocation, we'll have
221 // &gv-&movepctolr-4 in the imm field. Once &movepctolr is added to the imm
222 // field, we get &gv. This doesn't happen for branch relocations, which are
223 // always implicitly pc relative.
224 if (TM.getRelocationModel() == Reloc::PIC_ && Reloc != PPC::reloc_pcrel_bx){
225 assert(MovePCtoLROffset && "MovePCtoLR not seen yet?");
226 R.setConstantVal(-(intptr_t)MovePCtoLROffset - 4);
227 }
228 MCE.addRelocation(R);
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000229
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000230 } else if (MO.isMBB()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231 unsigned Reloc = 0;
232 unsigned Opcode = MI.getOpcode();
Tilmann Scheller386330d2009-07-03 06:47:08 +0000233 if (Opcode == PPC::B || Opcode == PPC::BL_Darwin ||
234 Opcode == PPC::BLA_Darwin|| Opcode == PPC::BL_SVR4 ||
235 Opcode == PPC::BLA_SVR4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236 Reloc = PPC::reloc_pcrel_bx;
237 else // BCC instruction
238 Reloc = PPC::reloc_pcrel_bcx;
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000239
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000240 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
Chris Lattner6017d482007-12-30 23:10:15 +0000241 Reloc, MO.getMBB()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000242 } else {
Edwin Török4d9756a2009-07-08 20:53:28 +0000243#ifndef NDEBUG
Chris Lattnerd71b0b02009-08-23 03:41:05 +0000244 errs() << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
Edwin Török4d9756a2009-07-08 20:53:28 +0000245#endif
Edwin Törökbd448e32009-07-14 16:55:14 +0000246 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 }
248
249 return rv;
250}
251
252#include "PPCGenCodeEmitter.inc"