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/MachineCodeEmitter.h"
21 #include "llvm/CodeGen/MachineFunctionPass.h"
22 #include "llvm/CodeGen/MachineInstrBuilder.h"
23 #include "llvm/CodeGen/MachineModuleInfo.h"
24 #include "llvm/CodeGen/Passes.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Support/Compiler.h"
27 #include "llvm/Target/TargetOptions.h"
31 class VISIBILITY_HIDDEN PPCCodeEmitter : public MachineFunctionPass {
33 MachineCodeEmitter &MCE;
35 /// MovePCtoLROffset - When/if we see a MovePCtoLR instruction, we record
36 /// its address in the function into this pointer.
37 void *MovePCtoLROffset;
39 /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
41 int getMachineOpValue(MachineInstr &MI, MachineOperand &MO);
43 void getAnalysisUsage(AnalysisUsage &AU) const {
44 AU.addRequired<MachineModuleInfo>();
45 MachineFunctionPass::getAnalysisUsage(AU);
50 PPCCodeEmitter(TargetMachine &T, MachineCodeEmitter &M)
51 : MachineFunctionPass((intptr_t)&ID), TM(T), MCE(M) {}
53 const char *getPassName() const { return "PowerPC Machine Code Emitter"; }
55 /// runOnMachineFunction - emits the given MachineFunction to memory
57 bool runOnMachineFunction(MachineFunction &MF);
59 /// emitBasicBlock - emits the given MachineBasicBlock to memory
61 void emitBasicBlock(MachineBasicBlock &MBB);
63 /// getValueBit - return the particular bit of Val
65 unsigned getValueBit(int64_t Val, unsigned bit) { return (Val >> bit) & 1; }
67 /// getBinaryCodeForInstr - This function, generated by the
68 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
69 /// machine instructions.
71 unsigned getBinaryCodeForInstr(MachineInstr &MI);
73 char PPCCodeEmitter::ID = 0;
76 /// createPPCCodeEmitterPass - Return a pass that emits the collected PPC code
77 /// to the specified MCE object.
78 FunctionPass *llvm::createPPCCodeEmitterPass(PPCTargetMachine &TM,
79 MachineCodeEmitter &MCE) {
80 return new PPCCodeEmitter(TM, MCE);
83 bool PPCCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
84 assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
85 MF.getTarget().getRelocationModel() != Reloc::Static) &&
86 "JIT relocation model must be set to static or default!");
88 MCE.setModuleInfo(&getAnalysis<MachineModuleInfo>());
91 MCE.startFunction(MF);
92 for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
94 } while (MCE.finishFunction(MF));
99 void PPCCodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
100 MCE.StartMachineBasicBlock(&MBB);
102 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
103 MachineInstr &MI = *I;
104 switch (MI.getOpcode()) {
106 MCE.emitWordBE(getBinaryCodeForInstr(*I));
108 case TargetInstrInfo::LABEL:
109 MCE.emitLabel(MI.getOperand(0).getImm());
111 case TargetInstrInfo::IMPLICIT_DEF:
112 break; // pseudo opcode, no side effects
113 case PPC::MovePCtoLR:
114 case PPC::MovePCtoLR8:
115 assert(TM.getRelocationModel() == Reloc::PIC_);
116 MovePCtoLROffset = (void*)MCE.getCurrentPCValue();
117 MCE.emitWordBE(0x48000005); // bl 1
123 int PPCCodeEmitter::getMachineOpValue(MachineInstr &MI, MachineOperand &MO) {
125 intptr_t rv = 0; // Return value; defaults to 0 for unhandled cases
126 // or things that get fixed up later by the JIT.
127 if (MO.isRegister()) {
128 rv = PPCRegisterInfo::getRegisterNumbering(MO.getReg());
130 // Special encoding for MTCRF and MFOCRF, which uses a bit mask for the
131 // register, not the register number directly.
132 if ((MI.getOpcode() == PPC::MTCRF || MI.getOpcode() == PPC::MFOCRF) &&
133 (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7)) {
136 } else if (MO.isImmediate()) {
138 } else if (MO.isGlobalAddress() || MO.isExternalSymbol() ||
139 MO.isConstantPoolIndex() || MO.isJumpTableIndex()) {
141 if (MI.getOpcode() == PPC::BL_Macho || MI.getOpcode() == PPC::BL8_Macho ||
142 MI.getOpcode() == PPC::BL_ELF || MI.getOpcode() == PPC::BL8_ELF ||
143 MI.getOpcode() == PPC::TAILB || MI.getOpcode() == PPC::TAILB8)
144 Reloc = PPC::reloc_pcrel_bx;
146 if (TM.getRelocationModel() == Reloc::PIC_) {
147 assert(MovePCtoLROffset && "MovePCtoLR not seen yet?");
149 switch (MI.getOpcode()) {
150 default: MI.dump(); assert(0 && "Unknown instruction for relocation!");
155 Reloc = PPC::reloc_absolute_high; // Pointer to symbol
181 Reloc = PPC::reloc_absolute_low;
188 Reloc = PPC::reloc_absolute_low_ix;
194 if (MO.isGlobalAddress()) {
195 R = MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
197 isa<Function>(MO.getGlobal()));
198 } else if (MO.isExternalSymbol()) {
199 R = MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
200 Reloc, MO.getSymbolName(), 0);
201 } else if (MO.isConstantPoolIndex()) {
202 R = MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
203 Reloc, MO.getIndex(), 0);
205 assert(MO.isJumpTableIndex());
206 R = MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
207 Reloc, MO.getIndex(), 0);
210 // If in PIC mode, we need to encode the negated address of the
211 // 'movepctolr' into the unrelocated field. After relocation, we'll have
212 // &gv-&movepctolr-4 in the imm field. Once &movepctolr is added to the imm
213 // field, we get &gv. This doesn't happen for branch relocations, which are
214 // always implicitly pc relative.
215 if (TM.getRelocationModel() == Reloc::PIC_ && Reloc != PPC::reloc_pcrel_bx){
216 assert(MovePCtoLROffset && "MovePCtoLR not seen yet?");
217 R.setConstantVal(-(intptr_t)MovePCtoLROffset - 4);
219 MCE.addRelocation(R);
221 } else if (MO.isMachineBasicBlock()) {
223 unsigned Opcode = MI.getOpcode();
224 if (Opcode == PPC::B || Opcode == PPC::BL_Macho ||
225 Opcode == PPC::BLA_Macho || Opcode == PPC::BL_ELF ||
226 Opcode == PPC::BLA_ELF)
227 Reloc = PPC::reloc_pcrel_bx;
228 else // BCC instruction
229 Reloc = PPC::reloc_pcrel_bcx;
230 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
231 Reloc, MO.getMBB()));
233 cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
240 #include "PPCGenCodeEmitter.inc"