1 //===-- PPCCodeEmitter.cpp - JIT Code Emitter for PowerPC32 -------*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the PowerPC 32-bit CodeEmitter and associated machinery to
11 // JIT-compile bytecode 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/Passes.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/Compiler.h"
26 #include "llvm/Target/TargetOptions.h"
30 class VISIBILITY_HIDDEN PPCCodeEmitter : public MachineFunctionPass {
32 MachineCodeEmitter &MCE;
34 /// MovePCtoLROffset - When/if we see a MovePCtoLR instruction, we record
35 /// its address in the function into this pointer.
36 void *MovePCtoLROffset;
38 /// getMachineOpValue - evaluates the MachineOperand of a given MachineInstr
40 int getMachineOpValue(MachineInstr &MI, MachineOperand &MO);
43 PPCCodeEmitter(TargetMachine &T, MachineCodeEmitter &M)
46 const char *getPassName() const { return "PowerPC Machine Code Emitter"; }
48 /// runOnMachineFunction - emits the given MachineFunction to memory
50 bool runOnMachineFunction(MachineFunction &MF);
52 /// emitBasicBlock - emits the given MachineBasicBlock to memory
54 void emitBasicBlock(MachineBasicBlock &MBB);
56 /// getValueBit - return the particular bit of Val
58 unsigned getValueBit(int64_t Val, unsigned bit) { return (Val >> bit) & 1; }
60 /// getBinaryCodeForInstr - This function, generated by the
61 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
62 /// machine instructions.
64 unsigned getBinaryCodeForInstr(MachineInstr &MI);
68 /// createPPCCodeEmitterPass - Return a pass that emits the collected PPC code
69 /// to the specified MCE object.
70 FunctionPass *llvm::createPPCCodeEmitterPass(PPCTargetMachine &TM,
71 MachineCodeEmitter &MCE) {
72 return new PPCCodeEmitter(TM, MCE);
76 extern "C" void sys_icache_invalidate(const void *Addr, size_t len);
79 bool PPCCodeEmitter::runOnMachineFunction(MachineFunction &MF) {
80 assert((MF.getTarget().getRelocationModel() != Reloc::Default ||
81 MF.getTarget().getRelocationModel() != Reloc::Static) &&
82 "JIT relocation model must be set to static or default!");
85 MCE.startFunction(MF);
86 for (MachineFunction::iterator BB = MF.begin(), E = MF.end(); BB != E; ++BB)
88 } while (MCE.finishFunction(MF));
93 void PPCCodeEmitter::emitBasicBlock(MachineBasicBlock &MBB) {
94 MCE.StartMachineBasicBlock(&MBB);
96 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ++I){
97 MachineInstr &MI = *I;
98 switch (MI.getOpcode()) {
100 MCE.emitWordBE(getBinaryCodeForInstr(*I));
102 case PPC::IMPLICIT_DEF_GPRC:
103 case PPC::IMPLICIT_DEF_G8RC:
104 case PPC::IMPLICIT_DEF_F8:
105 case PPC::IMPLICIT_DEF_F4:
106 case PPC::IMPLICIT_DEF_VRRC:
107 break; // pseudo opcode, no side effects
108 case PPC::MovePCtoLR:
109 case PPC::MovePCtoLR8:
110 assert(TM.getRelocationModel() == Reloc::PIC_);
111 MovePCtoLROffset = (void*)MCE.getCurrentPCValue();
112 MCE.emitWordBE(0x48000005); // bl 1
118 int PPCCodeEmitter::getMachineOpValue(MachineInstr &MI, MachineOperand &MO) {
120 intptr_t rv = 0; // Return value; defaults to 0 for unhandled cases
121 // or things that get fixed up later by the JIT.
122 if (MO.isRegister()) {
123 rv = PPCRegisterInfo::getRegisterNumbering(MO.getReg());
125 // Special encoding for MTCRF and MFOCRF, which uses a bit mask for the
126 // register, not the register number directly.
127 if ((MI.getOpcode() == PPC::MTCRF || MI.getOpcode() == PPC::MFOCRF) &&
128 (MO.getReg() >= PPC::CR0 && MO.getReg() <= PPC::CR7)) {
131 } else if (MO.isImmediate()) {
132 rv = MO.getImmedValue();
133 } else if (MO.isGlobalAddress() || MO.isExternalSymbol() ||
134 MO.isConstantPoolIndex() || MO.isJumpTableIndex()) {
136 if (MI.getOpcode() == PPC::BL_Macho || MI.getOpcode() == PPC::BL8_Macho ||
137 MI.getOpcode() == PPC::BL_ELF || MI.getOpcode() == PPC::BL8_ELF)
138 Reloc = PPC::reloc_pcrel_bx;
140 if (TM.getRelocationModel() == Reloc::PIC_) {
141 assert(MovePCtoLROffset && "MovePCtoLR not seen yet?");
143 switch (MI.getOpcode()) {
144 default: MI.dump(); assert(0 && "Unknown instruction for relocation!");
149 Reloc = PPC::reloc_absolute_high; // Pointer to symbol
175 Reloc = PPC::reloc_absolute_low;
182 Reloc = PPC::reloc_absolute_low_ix;
188 if (MO.isGlobalAddress()) {
189 R = MachineRelocation::getGV(MCE.getCurrentPCOffset(), Reloc,
191 } else if (MO.isExternalSymbol()) {
192 R = MachineRelocation::getExtSym(MCE.getCurrentPCOffset(),
193 Reloc, MO.getSymbolName(), 0);
194 } else if (MO.isConstantPoolIndex()) {
195 R = MachineRelocation::getConstPool(MCE.getCurrentPCOffset(),
196 Reloc, MO.getConstantPoolIndex(), 0);
198 assert(MO.isJumpTableIndex());
199 R = MachineRelocation::getJumpTable(MCE.getCurrentPCOffset(),
200 Reloc, MO.getJumpTableIndex(), 0);
203 // If in PIC mode, we need to encode the negated address of the
204 // 'movepctolr' into the unrelocated field. After relocation, we'll have
205 // &gv-&movepctolr-4 in the imm field. Once &movepctolr is added to the imm
206 // field, we get &gv. This doesn't happen for branch relocations, which are
207 // always implicitly pc relative.
208 if (TM.getRelocationModel() == Reloc::PIC_ && Reloc != PPC::reloc_pcrel_bx){
209 assert(MovePCtoLROffset && "MovePCtoLR not seen yet?");
210 R.setConstantVal(-(intptr_t)MovePCtoLROffset - 4);
212 MCE.addRelocation(R);
214 } else if (MO.isMachineBasicBlock()) {
216 unsigned Opcode = MI.getOpcode();
217 if (Opcode == PPC::B || Opcode == PPC::BL_Macho ||
218 Opcode == PPC::BLA_Macho || Opcode == PPC::BL_ELF ||
219 Opcode == PPC::BLA_ELF)
220 Reloc = PPC::reloc_pcrel_bx;
221 else // BCC instruction
222 Reloc = PPC::reloc_pcrel_bcx;
223 MCE.addRelocation(MachineRelocation::getBB(MCE.getCurrentPCOffset(),
225 MO.getMachineBasicBlock()));
227 cerr << "ERROR: Unknown type of MachineOperand: " << MO << "\n";
234 #include "PPCGenCodeEmitter.inc"