1 //===-- PPCCodeEmitter.cpp - JIT Code Emitter for PowerPC32 -------*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the PowerPC 32-bit CodeEmitter and associated machinery to
11 // JIT-compile bitcode to native PowerPC.
13 //===----------------------------------------------------------------------===//
15 #include "PPCTargetMachine.h"
16 #include "PPCRelocations.h"
18 #include "llvm/Module.h"
19 #include "llvm/PassManager.h"
20 #include "llvm/CodeGen/JITCodeEmitter.h"
21 #include "llvm/CodeGen/MachineFunctionPass.h"
22 #include "llvm/CodeGen/MachineInstrBuilder.h"
23 #include "llvm/CodeGen/MachineModuleInfo.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Target/TargetOptions.h"
30 class PPCCodeEmitter : public MachineFunctionPass {
34 void getAnalysisUsage(AnalysisUsage &AU) const {
35 AU.addRequired<MachineModuleInfo>();
36 MachineFunctionPass::getAnalysisUsage(AU);
41 /// MovePCtoLROffset - When/if we see a MovePCtoLR instruction, we record
42 /// its address in the function into this pointer.
43 void *MovePCtoLROffset;
46 PPCCodeEmitter(TargetMachine &tm, JITCodeEmitter &mce)
47 : MachineFunctionPass(&ID), TM(tm), MCE(mce) {}
49 /// getBinaryCodeForInstr - This function, generated by the
50 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
51 /// machine instructions.
53 unsigned getBinaryCodeForInstr(const MachineInstr &MI);
55 /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
57 unsigned getMachineOpValue(const MachineInstr &MI,
58 const MachineOperand &MO);
60 const char *getPassName() const { return "PowerPC Machine Code Emitter"; }
62 /// runOnMachineFunction - emits the given MachineFunction to memory
64 bool runOnMachineFunction(MachineFunction &MF);
66 /// emitBasicBlock - emits the given MachineBasicBlock to memory
68 void emitBasicBlock(MachineBasicBlock &MBB);
70 /// getValueBit - return the particular bit of Val
72 unsigned getValueBit(int64_t Val, unsigned bit) { return (Val >> bit) & 1; }
76 char PPCCodeEmitter::ID = 0;
78 /// createPPCCodeEmitterPass - Return a pass that emits the collected PPC code
79 /// to the specified MCE object.
80 FunctionPass *llvm::createPPCJITCodeEmitterPass(PPCTargetMachine &TM,
81 JITCodeEmitter &JCE) {
82 return new PPCCodeEmitter(TM, JCE);
85 bool PPCCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
86 assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
87 MF.getTarget().getRelocationModel() != Reloc::Static) &&
88 "JIT relocation model must be set to static or default!");
90 MCE.setModuleInfo(&getAnalysis<MachineModuleInfo>());
93 MCE.startFunction(MF);
94 for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
96 } while (MCE.finishFunction(MF));
101 void PPCCodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
102 MCE.StartMachineBasicBlock(&MBB);
104 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
105 const MachineInstr &MI = *I;
106 MCE.processDebugLoc(MI.getDebugLoc(), true);
107 switch (MI.getOpcode()) {
109 MCE.emitWordBE(getBinaryCodeForInstr(MI));
111 case TargetInstrInfo::DBG_LABEL:
112 case TargetInstrInfo::EH_LABEL:
113 MCE.emitLabel(MI.getOperand(0).getImm());
115 case TargetInstrInfo::IMPLICIT_DEF:
116 case TargetInstrInfo::KILL:
117 break; // pseudo opcode, no side effects
118 case PPC::MovePCtoLR:
119 case PPC::MovePCtoLR8:
120 assert(TM.getRelocationModel() == Reloc::PIC_);
121 MovePCtoLROffset = (void*)MCE.getCurrentPCValue();
122 MCE.emitWordBE(0x48000005); // bl 1
125 MCE.processDebugLoc(MI.getDebugLoc(), false);
129 unsigned PPCCodeEmitter::getMachineOpValue(const MachineInstr &MI,
130 const MachineOperand &MO) {
132 unsigned rv = 0; // Return value; defaults to 0 for unhandled cases
133 // or things that get fixed up later by the JIT.
135 rv = PPCRegisterInfo::getRegisterNumbering(MO.getReg());
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)) {
143 } else if (MO.isImm()) {
145 } else if (MO.isGlobal() || MO.isSymbol() ||
146 MO.isCPI() || MO.isJTI()) {
148 if (MI.getOpcode() == PPC::BL_Darwin || MI.getOpcode() == PPC::BL8_Darwin ||
149 MI.getOpcode() == PPC::BL_SVR4 || MI.getOpcode() == PPC::BL8_ELF ||
150 MI.getOpcode() == PPC::TAILB || MI.getOpcode() == PPC::TAILB8)
151 Reloc = PPC::reloc_pcrel_bx;
153 if (TM.getRelocationModel() == Reloc::PIC_) {
154 assert(MovePCtoLROffset && "MovePCtoLR not seen yet?");
156 switch (MI.getOpcode()) {
157 default: MI.dump(); llvm_unreachable("Unknown instruction for relocation!");
162 Reloc = PPC::reloc_absolute_high; // Pointer to symbol
188 Reloc = PPC::reloc_absolute_low;
195 Reloc = PPC::reloc_absolute_low_ix;
202 R = MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
204 isa<Function>(MO.getGlobal()));
205 } else if (MO.isSymbol()) {
206 R = MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
207 Reloc, MO.getSymbolName(), 0);
208 } else if (MO.isCPI()) {
209 R = MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
210 Reloc, MO.getIndex(), 0);
213 R = MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
214 Reloc, MO.getIndex(), 0);
217 // 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);
226 MCE.addRelocation(R);
228 } else if (MO.isMBB()) {
230 unsigned Opcode = MI.getOpcode();
231 if (Opcode == PPC::B || Opcode == PPC::BL_Darwin ||
232 Opcode == PPC::BLA_Darwin|| Opcode == PPC::BL_SVR4 ||
233 Opcode == PPC::BLA_SVR4)
234 Reloc = PPC::reloc_pcrel_bx;
235 else // BCC instruction
236 Reloc = PPC::reloc_pcrel_bcx;
238 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
239 Reloc, MO.getMBB()));
242 errs() << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
250 #include "PPCGenCodeEmitter.inc"