blob: 327470dd103ec2cc565985b723833dc70eaf08e4 [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;
33
34 void getAnalysisUsage(AnalysisUsage &AU) const {
35 AU.addRequired<MachineModuleInfo>();
36 MachineFunctionPass::getAnalysisUsage(AU);
37 }
38
39 static char ID;
40
41 /// MovePCtoLROffset - When/if we see a MovePCtoLR instruction, we record
42 /// its address in the function into this pointer.
43 void *MovePCtoLROffset;
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000044 public:
Chris Lattner90ae6ff2010-02-02 21:55:58 +000045
46 PPCCodeEmitter(TargetMachine &tm, JITCodeEmitter &mce)
47 : MachineFunctionPass(&ID), TM(tm), MCE(mce) {}
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000048
49 /// getBinaryCodeForInstr - This function, generated by the
50 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
51 /// machine instructions.
52
53 unsigned getBinaryCodeForInstr(const MachineInstr &MI);
54
55 /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
56
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +000057 unsigned getMachineOpValue(const MachineInstr &MI,
58 const MachineOperand &MO);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060 const char *getPassName() const { return "PowerPC Machine Code Emitter"; }
61
62 /// runOnMachineFunction - emits the given MachineFunction to memory
63 ///
64 bool runOnMachineFunction(MachineFunction &MF);
65
66 /// emitBasicBlock - emits the given MachineBasicBlock to memory
67 ///
68 void emitBasicBlock(MachineBasicBlock &MBB);
69
70 /// getValueBit - return the particular bit of Val
71 ///
72 unsigned getValueBit(int64_t Val, unsigned bit) { return (Val >> bit) & 1; }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073 };
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000074}
Daniel Dunbar3be44e62009-09-20 02:20:51 +000075
Chris Lattner90ae6ff2010-02-02 21:55:58 +000076char PPCCodeEmitter::ID = 0;
77
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078/// createPPCCodeEmitterPass - Return a pass that emits the collected PPC code
79/// to the specified MCE object.
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000080FunctionPass *llvm::createPPCJITCodeEmitterPass(PPCTargetMachine &TM,
Bruno Cardoso Lopes8e2537b2009-06-01 19:57:37 +000081 JITCodeEmitter &JCE) {
Chris Lattner90ae6ff2010-02-02 21:55:58 +000082 return new PPCCodeEmitter(TM, JCE);
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +000083}
84
Chris Lattner90ae6ff2010-02-02 21:55:58 +000085bool PPCCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000086 assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
87 MF.getTarget().getRelocationModel() != Reloc::Static) &&
88 "JIT relocation model must be set to static or default!");
Nicolas Geoffray0e757e12008-02-13 18:39:37 +000089
90 MCE.setModuleInfo(&getAnalysis<MachineModuleInfo>());
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091 do {
92 MovePCtoLROffset = 0;
93 MCE.startFunction(MF);
94 for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
95 emitBasicBlock(*BB);
96 } while (MCE.finishFunction(MF));
97
98 return false;
99}
100
Chris Lattner90ae6ff2010-02-02 21:55:58 +0000101void PPCCodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102 MCE.StartMachineBasicBlock(&MBB);
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000103
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
Evan Cheng3ca89372008-09-02 06:51:36 +0000105 const MachineInstr &MI = *I;
Devang Patel5450fc12009-10-06 02:19:11 +0000106 MCE.processDebugLoc(MI.getDebugLoc(), true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107 switch (MI.getOpcode()) {
108 default:
Evan Cheng3ca89372008-09-02 06:51:36 +0000109 MCE.emitWordBE(getBinaryCodeForInstr(MI));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110 break;
Chris Lattner4052b292010-02-09 19:54:29 +0000111 case TargetOpcode::DBG_LABEL:
112 case TargetOpcode::EH_LABEL:
Nicolas Geoffray0e757e12008-02-13 18:39:37 +0000113 MCE.emitLabel(MI.getOperand(0).getImm());
114 break;
Chris Lattner4052b292010-02-09 19:54:29 +0000115 case TargetOpcode::IMPLICIT_DEF:
116 case TargetOpcode::KILL:
Evan Chengb74b4b62008-03-17 06:56:52 +0000117 break; // pseudo opcode, no side effects
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118 case PPC::MovePCtoLR:
119 case PPC::MovePCtoLR8:
120 assert(TM.getRelocationModel() == Reloc::PIC_);
121 MovePCtoLROffset = (void*)MCE.getCurrentPCValue();
122 MCE.emitWordBE(0x48000005); // bl 1
123 break;
124 }
Devang Patel5450fc12009-10-06 02:19:11 +0000125 MCE.processDebugLoc(MI.getDebugLoc(), false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126 }
127}
128
Evan Cheng3ca89372008-09-02 06:51:36 +0000129unsigned PPCCodeEmitter::getMachineOpValue(const MachineInstr &MI,
130 const MachineOperand &MO) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131
Evan Cheng3ca89372008-09-02 06:51:36 +0000132 unsigned rv = 0; // Return value; defaults to 0 for unhandled cases
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133 // or things that get fixed up later by the JIT.
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000134 if (MO.isReg()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135 rv = PPCRegisterInfo::getRegisterNumbering(MO.getReg());
136
137 // Special encoding for MTCRF and MFOCRF, which uses a bit mask for the
138 // register, not the register number directly.
139 if ((MI.getOpcode() == PPC::MTCRF || MI.getOpcode() == PPC::MFOCRF) &&
140 (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7)) {
141 rv = 0x80 >> rv;
142 }
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000143 } else if (MO.isImm()) {
Chris Lattnera96056a2007-12-30 20:49:49 +0000144 rv = MO.getImm();
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000145 } else if (MO.isGlobal() || MO.isSymbol() ||
146 MO.isCPI() || MO.isJTI()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147 unsigned Reloc = 0;
Tilmann Scheller386330d2009-07-03 06:47:08 +0000148 if (MI.getOpcode() == PPC::BL_Darwin || MI.getOpcode() == PPC::BL8_Darwin ||
149 MI.getOpcode() == PPC::BL_SVR4 || MI.getOpcode() == PPC::BL8_ELF ||
Arnold Schwaighofera0032722008-04-30 09:16:33 +0000150 MI.getOpcode() == PPC::TAILB || MI.getOpcode() == PPC::TAILB8)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151 Reloc = PPC::reloc_pcrel_bx;
152 else {
153 if (TM.getRelocationModel() == Reloc::PIC_) {
154 assert(MovePCtoLROffset && "MovePCtoLR not seen yet?");
155 }
156 switch (MI.getOpcode()) {
Edwin Törökbd448e32009-07-14 16:55:14 +0000157 default: MI.dump(); llvm_unreachable("Unknown instruction for relocation!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 case PPC::LIS:
159 case PPC::LIS8:
160 case PPC::ADDIS:
161 case PPC::ADDIS8:
162 Reloc = PPC::reloc_absolute_high; // Pointer to symbol
163 break;
164 case PPC::LI:
165 case PPC::LI8:
166 case PPC::LA:
167 // Loads.
168 case PPC::LBZ:
169 case PPC::LBZ8:
170 case PPC::LHA:
171 case PPC::LHA8:
172 case PPC::LHZ:
173 case PPC::LHZ8:
174 case PPC::LWZ:
175 case PPC::LWZ8:
176 case PPC::LFS:
177 case PPC::LFD:
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000178
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 // Stores.
180 case PPC::STB:
181 case PPC::STB8:
182 case PPC::STH:
183 case PPC::STH8:
184 case PPC::STW:
185 case PPC::STW8:
186 case PPC::STFS:
187 case PPC::STFD:
188 Reloc = PPC::reloc_absolute_low;
189 break;
190
191 case PPC::LWA:
192 case PPC::LD:
193 case PPC::STD:
194 case PPC::STD_32:
195 Reloc = PPC::reloc_absolute_low_ix;
196 break;
197 }
198 }
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000199
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200 MachineRelocation R;
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000201 if (MO.isGlobal()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202 R = MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
Evan Cheng9f6942b2008-01-04 02:22:21 +0000203 MO.getGlobal(), 0,
204 isa<Function>(MO.getGlobal()));
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000205 } else if (MO.isSymbol()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206 R = MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
207 Reloc, MO.getSymbolName(), 0);
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000208 } else if (MO.isCPI()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000209 R = MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
Chris Lattner6017d482007-12-30 23:10:15 +0000210 Reloc, MO.getIndex(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 } else {
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000212 assert(MO.isJTI());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213 R = MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
Chris Lattner6017d482007-12-30 23:10:15 +0000214 Reloc, MO.getIndex(), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000215 }
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000216
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217 // If in PIC mode, we need to encode the negated address of the
218 // 'movepctolr' into the unrelocated field. After relocation, we'll have
219 // &gv-&movepctolr-4 in the imm field. Once &movepctolr is added to the imm
220 // field, we get &gv. This doesn't happen for branch relocations, which are
221 // always implicitly pc relative.
222 if (TM.getRelocationModel() == Reloc::PIC_ && Reloc != PPC::reloc_pcrel_bx){
223 assert(MovePCtoLROffset && "MovePCtoLR not seen yet?");
224 R.setConstantVal(-(intptr_t)MovePCtoLROffset - 4);
225 }
226 MCE.addRelocation(R);
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000227
Dan Gohmanb9f4fa72008-10-03 15:45:36 +0000228 } else if (MO.isMBB()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229 unsigned Reloc = 0;
230 unsigned Opcode = MI.getOpcode();
Tilmann Scheller386330d2009-07-03 06:47:08 +0000231 if (Opcode == PPC::B || Opcode == PPC::BL_Darwin ||
232 Opcode == PPC::BLA_Darwin|| Opcode == PPC::BL_SVR4 ||
233 Opcode == PPC::BLA_SVR4)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000234 Reloc = PPC::reloc_pcrel_bx;
235 else // BCC instruction
236 Reloc = PPC::reloc_pcrel_bcx;
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000237
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
Chris Lattner6017d482007-12-30 23:10:15 +0000239 Reloc, MO.getMBB()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000240 } else {
Edwin Török4d9756a2009-07-08 20:53:28 +0000241#ifndef NDEBUG
Chris Lattnerd71b0b02009-08-23 03:41:05 +0000242 errs() << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
Edwin Török4d9756a2009-07-08 20:53:28 +0000243#endif
Edwin Törökbd448e32009-07-14 16:55:14 +0000244 llvm_unreachable(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000245 }
246
247 return rv;
248}
249
250#include "PPCGenCodeEmitter.inc"