blob: 05dbd3ce0224e43c914734bd07cccf59124e173c [file] [log] [blame]
Evan Cheng148b6a42007-07-05 21:15:40 +00001//===-- ARMJITInfo.cpp - Implement the JIT interfaces for the ARM target --===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the Raul Herbster (raulherbster [at] gmail [dot]
6// com) and is distributed under the University of Illinois Open Source License.
7// See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10//
11// This file implements the JIT interfaces for the ARM target.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "jit"
16#include "ARMJITInfo.h"
17#include "ARMRelocations.h"
18#include "ARMSubtarget.h"
19#include "llvm/CodeGen/MachineCodeEmitter.h"
20#include "llvm/Config/alloca.h"
21#include <cstdlib>
22using namespace llvm;
23
24void ARMJITInfo::replaceMachineCodeForFunction(void *Old, void *New) {
25 unsigned char *OldByte = (unsigned char *)Old;
26 *OldByte++ = 0xEA; // Emit B opcode.
27 unsigned *OldWord = (unsigned *)OldByte;
28 unsigned NewAddr = (intptr_t)New;
29 unsigned OldAddr = (intptr_t)OldWord;
30 *OldWord = NewAddr - OldAddr - 4; // Emit PC-relative addr of New code.
31}
32
33/// JITCompilerFunction - This contains the address of the JIT function used to
34/// compile a function lazily.
35static TargetJITInfo::JITCompilerFn JITCompilerFunction;
36
37// CompilationCallback stub - We can't use a C function with inline assembly in
38// it, because we the prolog/epilog inserted by GCC won't work for us. Instead,
39// write our own wrapper, which does things our way, so we have complete control
40// over register saving and restoring.
41extern "C" {
42#if defined(__arm__)
43 void ARMCompilationCallback(void);
44 asm(
45 ".text\n"
46 ".align 2\n"
47 ".globl ARMCompilationCallback\n"
48 "ARMCompilationCallback:\n"
49 // save main registers
50 "mov ip, sp\n"
51 "stmfd sp!, {fp, ip, lr, pc}\n"
52 "sub fp, ip, #4\n"
53 // arguments to Compilation Callback
54 // r0 - our lr (address of the call instruction in stub plus 4)
55 // r1 - stub's lr (address of instruction that called the stub plus 4)
56 "mov r0, fp\n" // stub's frame
57 "mov r1, lr\n" // stub's lr
58 "bl ARMCompilationCallbackC\n"
59 // restore main registers
60 "ldmfd sp, {fp, sp, pc}\n");
61#else // Not an ARM host
62 void ARMCompilationCallback() {
63 assert(0 && "Cannot call ARMCompilationCallback() on a non-ARM arch!\n");
64 abort();
65 }
66#endif
67}
68
69/// ARMCompilationCallbackC - This is the target-specific function invoked by the
70/// function stub when we did not know the real target of a call. This function
71/// must locate the start of the stub or call site and pass it into the JIT
72/// compiler function.
73extern "C" void ARMCompilationCallbackC(intptr_t *StackPtr, intptr_t RetAddr) {
74 intptr_t *RetAddrLoc = &StackPtr[-1];
75
76 assert(*RetAddrLoc == RetAddr &&
77 "Could not find return address on the stack!");
78#if 0
79 DOUT << "In callback! Addr=" << (void*)RetAddr
80 << " FP=" << (void*)StackPtr
81 << ": Resolving call to function: "
82 << TheVM->getFunctionReferencedName((void*)RetAddr) << "\n";
83#endif
84
85 // Sanity check to make sure this really is a branch and link instruction.
86 assert(((unsigned char*)RetAddr-1)[3] == 0xEB && "Not a branch and link instr!");
87
88 intptr_t NewVal = (intptr_t)JITCompilerFunction((void*)RetAddr);
89
90 // Rewrite the call target... so that we don't end up here every time we
91 // execute the call.
92 *(intptr_t *)RetAddr = (intptr_t)(NewVal-RetAddr-4);
93
94 // Change the return address to reexecute the branch and link instruction...
95 *RetAddrLoc -= 1;
96}
97
98TargetJITInfo::LazyResolverFn
99ARMJITInfo::getLazyResolverFunction(JITCompilerFn F) {
100 JITCompilerFunction = F;
101 return ARMCompilationCallback;
102}
103
104void *ARMJITInfo::emitFunctionStub(void *Fn, MachineCodeEmitter &MCE) {
105 unsigned addr = (intptr_t)Fn-MCE.getCurrentPCValue()-4;
106 // If this is just a call to an external function, emit a branch instead of a
107 // call. The code is the same except for one bit of the last instruction.
108 if (Fn != (void*)(intptr_t)ARMCompilationCallback) {
109 MCE.startFunctionStub(4, 2);
110 MCE.emitByte(0xEA); // branch to the corresponding function addr
111 MCE.emitByte((unsigned char)(addr >> 0));
112 MCE.emitByte((unsigned char)(addr >> 8));
113 MCE.emitByte((unsigned char)(addr >> 16));
114 return MCE.finishFunctionStub(0);
115 } else {
116 MCE.startFunctionStub(5, 2);
117 MCE.emitByte(0xEB); // branch and link to the corresponding function addr
118 }
119 MCE.emitByte((unsigned char)(addr >> 0));
120 MCE.emitByte((unsigned char)(addr >> 8));
121 MCE.emitByte((unsigned char)(addr >> 16));
122
123 return MCE.finishFunctionStub(0);
124}
125
126/// relocate - Before the JIT can run a block of code that has been emitted,
127/// it must rewrite the code to contain the actual addresses of any
128/// referenced global symbols.
129void ARMJITInfo::relocate(void *Function, MachineRelocation *MR,
130 unsigned NumRelocs, unsigned char* GOTBase) {
131
132}