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
);
44 PPCCodeEmitter(TargetMachine
&T
, MachineCodeEmitter
&M
)
45 : MachineFunctionPass((intptr_t)&ID
), TM(T
), MCE(M
) {}
47 const char *getPassName() const { return "PowerPC Machine Code Emitter"; }
49 /// runOnMachineFunction - emits the given MachineFunction to memory
51 bool runOnMachineFunction(MachineFunction
&MF
);
53 /// emitBasicBlock - emits the given MachineBasicBlock to memory
55 void emitBasicBlock(MachineBasicBlock
&MBB
);
57 /// getValueBit - return the particular bit of Val
59 unsigned getValueBit(int64_t Val
, unsigned bit
) { return (Val
>> bit
) & 1; }
61 /// getBinaryCodeForInstr - This function, generated by the
62 /// CodeEmitterGenerator using TableGen, produces the binary encoding for
63 /// machine instructions.
65 unsigned getBinaryCodeForInstr(MachineInstr
&MI
);
67 char PPCCodeEmitter::ID
= 0;
70 /// createPPCCodeEmitterPass - Return a pass that emits the collected PPC code
71 /// to the specified MCE object.
72 FunctionPass
*llvm::createPPCCodeEmitterPass(PPCTargetMachine
&TM
,
73 MachineCodeEmitter
&MCE
) {
74 return new PPCCodeEmitter(TM
, MCE
);
78 extern "C" void sys_icache_invalidate(const void *Addr
, size_t len
);
81 bool PPCCodeEmitter::runOnMachineFunction(MachineFunction
&MF
) {
82 assert((MF
.getTarget().getRelocationModel() != Reloc::Default
||
83 MF
.getTarget().getRelocationModel() != Reloc::Static
) &&
84 "JIT relocation model must be set to static or default!");
87 MCE
.startFunction(MF
);
88 for (MachineFunction::iterator BB
= MF
.begin(), E
= MF
.end(); BB
!= E
; ++BB
)
90 } while (MCE
.finishFunction(MF
));
95 void PPCCodeEmitter::emitBasicBlock(MachineBasicBlock
&MBB
) {
96 MCE
.StartMachineBasicBlock(&MBB
);
98 for (MachineBasicBlock::iterator I
= MBB
.begin(), E
= MBB
.end(); I
!= E
; ++I
){
99 MachineInstr
&MI
= *I
;
100 switch (MI
.getOpcode()) {
102 MCE
.emitWordBE(getBinaryCodeForInstr(*I
));
104 case PPC::IMPLICIT_DEF_GPRC
:
105 case PPC::IMPLICIT_DEF_G8RC
:
106 case PPC::IMPLICIT_DEF_F8
:
107 case PPC::IMPLICIT_DEF_F4
:
108 case PPC::IMPLICIT_DEF_VRRC
:
109 break; // pseudo opcode, no side effects
110 case PPC::MovePCtoLR
:
111 case PPC::MovePCtoLR8
:
112 assert(TM
.getRelocationModel() == Reloc::PIC_
);
113 MovePCtoLROffset
= (void*)MCE
.getCurrentPCValue();
114 MCE
.emitWordBE(0x48000005); // bl 1
120 int PPCCodeEmitter::getMachineOpValue(MachineInstr
&MI
, MachineOperand
&MO
) {
122 intptr_t rv
= 0; // Return value; defaults to 0 for unhandled cases
123 // or things that get fixed up later by the JIT.
124 if (MO
.isRegister()) {
125 rv
= PPCRegisterInfo::getRegisterNumbering(MO
.getReg());
127 // Special encoding for MTCRF and MFOCRF, which uses a bit mask for the
128 // register, not the register number directly.
129 if ((MI
.getOpcode() == PPC::MTCRF
|| MI
.getOpcode() == PPC::MFOCRF
) &&
130 (MO
.getReg() >= PPC::CR0
&& MO
.getReg() <= PPC::CR7
)) {
133 } else if (MO
.isImmediate()) {
134 rv
= MO
.getImmedValue();
135 } else if (MO
.isGlobalAddress() || MO
.isExternalSymbol() ||
136 MO
.isConstantPoolIndex() || MO
.isJumpTableIndex()) {
138 if (MI
.getOpcode() == PPC::BL_Macho
|| MI
.getOpcode() == PPC::BL8_Macho
||
139 MI
.getOpcode() == PPC::BL_ELF
|| MI
.getOpcode() == PPC::BL8_ELF
)
140 Reloc
= PPC::reloc_pcrel_bx
;
142 if (TM
.getRelocationModel() == Reloc::PIC_
) {
143 assert(MovePCtoLROffset
&& "MovePCtoLR not seen yet?");
145 switch (MI
.getOpcode()) {
146 default: MI
.dump(); assert(0 && "Unknown instruction for relocation!");
151 Reloc
= PPC::reloc_absolute_high
; // Pointer to symbol
177 Reloc
= PPC::reloc_absolute_low
;
184 Reloc
= PPC::reloc_absolute_low_ix
;
190 if (MO
.isGlobalAddress()) {
191 R
= MachineRelocation::getGV(MCE
.getCurrentPCOffset(), Reloc
,
193 } else if (MO
.isExternalSymbol()) {
194 R
= MachineRelocation::getExtSym(MCE
.getCurrentPCOffset(),
195 Reloc
, MO
.getSymbolName(), 0);
196 } else if (MO
.isConstantPoolIndex()) {
197 R
= MachineRelocation::getConstPool(MCE
.getCurrentPCOffset(),
198 Reloc
, MO
.getConstantPoolIndex(), 0);
200 assert(MO
.isJumpTableIndex());
201 R
= MachineRelocation::getJumpTable(MCE
.getCurrentPCOffset(),
202 Reloc
, MO
.getJumpTableIndex(), 0);
205 // If in PIC mode, we need to encode the negated address of the
206 // 'movepctolr' into the unrelocated field. After relocation, we'll have
207 // &gv-&movepctolr-4 in the imm field. Once &movepctolr is added to the imm
208 // field, we get &gv. This doesn't happen for branch relocations, which are
209 // always implicitly pc relative.
210 if (TM
.getRelocationModel() == Reloc::PIC_
&& Reloc
!= PPC::reloc_pcrel_bx
){
211 assert(MovePCtoLROffset
&& "MovePCtoLR not seen yet?");
212 R
.setConstantVal(-(intptr_t)MovePCtoLROffset
- 4);
214 MCE
.addRelocation(R
);
216 } else if (MO
.isMachineBasicBlock()) {
218 unsigned Opcode
= MI
.getOpcode();
219 if (Opcode
== PPC::B
|| Opcode
== PPC::BL_Macho
||
220 Opcode
== PPC::BLA_Macho
|| Opcode
== PPC::BL_ELF
||
221 Opcode
== PPC::BLA_ELF
)
222 Reloc
= PPC::reloc_pcrel_bx
;
223 else // BCC instruction
224 Reloc
= PPC::reloc_pcrel_bcx
;
225 MCE
.addRelocation(MachineRelocation::getBB(MCE
.getCurrentPCOffset(),
227 MO
.getMachineBasicBlock()));
229 cerr
<< "ERROR: Unknown type of MachineOperand: " << MO
<< "\n";
236 #include "PPCGenCodeEmitter.inc"