blob: 9bae398ecf10f65f5a26cffb23d58c9e526fcc90 [file] [log] [blame]
Chris Lattner9b3d9892004-11-23 06:02:06 +00001//===-- PPC32JITInfo.cpp - Implement the JIT interfaces for the PowerPC ---===//
2//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the JIT interfaces for the 32-bit PowerPC target.
11//
12//===----------------------------------------------------------------------===//
13
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"
19using namespace llvm;
20
21static TargetJITInfo::JITCompilerFn JITCompilerFunction;
22
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))
31
32// Pseudo-ops
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)
36
37static void CompilationCallback() {
38 //JITCompilerFunction
39 assert(0 && "CompilationCallback not implemented yet!");
40}
41
42
43TargetJITInfo::LazyResolverFn
44PPC32JITInfo::getLazyResolverFunction(JITCompilerFn Fn) {
45 return CompilationCallback;
46}
47
Chris Lattner7c83dc22004-11-23 06:27:02 +000048static void EmitBranchToAt(void *At, void *To, bool isCall) {
Chris Lattner9b3d9892004-11-23 06:02:06 +000049 intptr_t Addr = (intptr_t)To;
50
51 // FIXME: should special case the short branch case.
52 unsigned *AtI = (unsigned*)At;
53
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
Chris Lattner7c83dc22004-11-23 06:27:02 +000057 AtI[3] = BUILD_BCTR(isCall); // bctr/bctrl
Chris Lattner9b3d9892004-11-23 06:02:06 +000058}
59
60void *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();
66 MCE.emitWord(0);
67 MCE.emitWord(0);
68 MCE.emitWord(0);
69 MCE.emitWord(0);
Chris Lattner7c83dc22004-11-23 06:27:02 +000070 EmitBranchToAt(Addr, Fn, false);
Chris Lattner9b3d9892004-11-23 06:02:06 +000071 return MCE.finishFunctionStub(0);
72 }
73
Chris Lattner7c83dc22004-11-23 06:27:02 +000074 MCE.startFunctionStub(4*7);
75 MCE.emitWord(0x9421ffe0); // stwu r1,-32(r1)
76 MCE.emitWord(0x7d6802a6); // mflr r11
77 MCE.emitWord(0x91610028); // stw r11, 40(r1)
78 void *Addr = (void*)(intptr_t)MCE.getCurrentPCValue();
79 MCE.emitWord(0);
80 MCE.emitWord(0);
81 MCE.emitWord(0);
82 MCE.emitWord(0);
83 EmitBranchToAt(Addr, Fn, true/*is call*/);
Chris Lattner9b3d9892004-11-23 06:02:06 +000084 return MCE.finishFunctionStub(0);
85}
86
87
88void PPC32JITInfo::relocate(void *Function, MachineRelocation *MR,
89 unsigned NumRelocs) {
90 for (unsigned i = 0; i != NumRelocs; ++i, ++MR) {
91 unsigned *RelocPos = (unsigned*)Function + MR->getMachineCodeOffset()/4;
92 intptr_t ResultPtr = (intptr_t)MR->getResultPointer();
93 switch ((PPC::RelocationType)MR->getRelocationType()) {
94 default: assert(0 && "Unknown relocation type!");
95 case PPC::reloc_pcrel_bx:
96 // PC-relative relocation for b and bl instructions.
97 ResultPtr = (ResultPtr-(intptr_t)RelocPos) >> 2;
98 assert(ResultPtr >= -(1 << 23) && ResultPtr < (1 << 23) &&
99 "Relocation out of range!");
100 *RelocPos |= (ResultPtr & ((1 << 24)-1)) << 2;
101 break;
102 case PPC::reloc_absolute_loadhi: // Relocate high bits into addis
103 case PPC::reloc_absolute_la: // Relocate low bits into addi
104 ResultPtr += MR->getConstantVal();
105
106 if (MR->getRelocationType() == PPC::reloc_absolute_loadhi) {
107 // If the low part will have a carry (really a borrow) from the low
108 // 16-bits into the high 16, add a bit to borrow from.
109 if (((int)ResultPtr << 16) < 0)
110 ResultPtr += 1 << 16;
111 ResultPtr >>= 16;
112 }
113
114 // Do the addition then mask, so the addition does not overflow the 16-bit
115 // immediate section of the instruction.
116 unsigned LowBits = (*RelocPos + ResultPtr) & 65535;
117 unsigned HighBits = *RelocPos & ~65535;
118 *RelocPos = LowBits | HighBits; // Slam into low 16-bits
119 break;
120 }
121 }
122}
123
124void PPC32JITInfo::replaceMachineCodeForFunction(void *Old, void *New) {
Chris Lattner7c83dc22004-11-23 06:27:02 +0000125 EmitBranchToAt(Old, New, false);
Chris Lattner9b3d9892004-11-23 06:02:06 +0000126}