1 //===-- PPC32JITInfo.cpp - Implement the JIT interfaces for the PowerPC ---===//
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 implements the JIT interfaces for the 32-bit PowerPC target.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "jit"
15 #include "PPC32JITInfo.h"
16 #include "PPC32Relocations.h"
17 #include "llvm/CodeGen/MachineCodeEmitter.h"
18 #include "llvm/Config/alloca.h"
21 static TargetJITInfo::JITCompilerFn JITCompilerFunction;
23 #define BUILD_ADDIS(RD,RS,IMM16) \
24 ((15 << 26) | ((RD) << 21) | ((RS) << 16) | ((IMM16) & 65535))
25 #define BUILD_ORI(RD,RS,UIMM16) \
26 ((24 << 26) | ((RS) << 21) | ((RD) << 16) | ((UIMM16) & 65535))
27 #define BUILD_MTSPR(RS,SPR) \
28 ((31 << 26) | ((RS) << 21) | ((SPR) << 16) | (467 << 1))
29 #define BUILD_BCCTRx(BO,BI,LINK) \
30 ((19 << 26) | ((BO) << 21) | ((BI) << 16) | (528 << 1) | ((LINK) & 1))
33 #define BUILD_LIS(RD,IMM16) BUILD_ADDIS(RD,0,IMM16)
34 #define BUILD_MTCTR(RS) BUILD_MTSPR(RS,9)
35 #define BUILD_BCTR(LINK) BUILD_BCCTRx(20,0,LINK)
37 static void CompilationCallback() {
39 assert(0 && "CompilationCallback not implemented yet!");
43 TargetJITInfo::LazyResolverFn
44 PPC32JITInfo::getLazyResolverFunction(JITCompilerFn Fn) {
45 return CompilationCallback;
48 static void EmitBranchToAt(void *At, void *To) {
49 intptr_t Addr = (intptr_t)To;
51 // FIXME: should special case the short branch case.
52 unsigned *AtI = (unsigned*)At;
54 AtI[0] = BUILD_LIS(12, Addr >> 16); // lis r12, hi16(address)
55 AtI[1] = BUILD_ORI(12, 12, Addr); // ori r12, r12, low16(address)
56 AtI[2] = BUILD_MTCTR(12); // mtctr r12
57 AtI[3] = BUILD_BCTR(0); // bctr
60 void *PPC32JITInfo::emitFunctionStub(void *Fn, MachineCodeEmitter &MCE) {
61 // If this is just a call to an external function, emit a branch instead of a
62 // call. The code is the same except for one bit of the last instruction.
63 if (Fn != CompilationCallback) {
64 MCE.startFunctionStub(4*4);
65 void *Addr = (void*)(intptr_t)MCE.getCurrentPCValue();
70 EmitBranchToAt(Addr, Fn);
71 return MCE.finishFunctionStub(0);
74 MCE.startFunctionStub(4*4);
75 return MCE.finishFunctionStub(0);
79 void PPC32JITInfo::relocate(void *Function, MachineRelocation *MR,
81 for (unsigned i = 0; i != NumRelocs; ++i, ++MR) {
82 unsigned *RelocPos = (unsigned*)Function + MR->getMachineCodeOffset()/4;
83 intptr_t ResultPtr = (intptr_t)MR->getResultPointer();
84 switch ((PPC::RelocationType)MR->getRelocationType()) {
85 default: assert(0 && "Unknown relocation type!");
86 case PPC::reloc_pcrel_bx:
87 // PC-relative relocation for b and bl instructions.
88 ResultPtr = (ResultPtr-(intptr_t)RelocPos) >> 2;
89 assert(ResultPtr >= -(1 << 23) && ResultPtr < (1 << 23) &&
90 "Relocation out of range!");
91 *RelocPos |= (ResultPtr & ((1 << 24)-1)) << 2;
93 case PPC::reloc_absolute_loadhi: // Relocate high bits into addis
94 case PPC::reloc_absolute_la: // Relocate low bits into addi
95 ResultPtr += MR->getConstantVal();
97 if (MR->getRelocationType() == PPC::reloc_absolute_loadhi) {
98 // If the low part will have a carry (really a borrow) from the low
99 // 16-bits into the high 16, add a bit to borrow from.
100 if (((int)ResultPtr << 16) < 0)
101 ResultPtr += 1 << 16;
105 // Do the addition then mask, so the addition does not overflow the 16-bit
106 // immediate section of the instruction.
107 unsigned LowBits = (*RelocPos + ResultPtr) & 65535;
108 unsigned HighBits = *RelocPos & ~65535;
109 *RelocPos = LowBits | HighBits; // Slam into low 16-bits
115 void PPC32JITInfo::replaceMachineCodeForFunction(void *Old, void *New) {
116 EmitBranchToAt(Old, New);