1 //===-- ARMJITInfo.cpp - Implement the JIT interfaces for the ARM target --===//
3 // The LLVM Compiler Infrastructure
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.
9 //===----------------------------------------------------------------------===//
11 // This file implements the JIT interfaces for the ARM target.
13 //===----------------------------------------------------------------------===//
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"
24 void 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.
33 /// JITCompilerFunction - This contains the address of the JIT function used to
34 /// compile a function lazily.
35 static TargetJITInfo::JITCompilerFn JITCompilerFunction;
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.
43 void ARMCompilationCallback(void);
47 ".globl ARMCompilationCallback\n"
48 "ARMCompilationCallback:\n"
49 // save main registers
51 "stmfd sp!, {fp, ip, lr, pc}\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");
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.
73 extern "C" void ARMCompilationCallbackC(intptr_t *StackPtr, intptr_t RetAddr) {
74 intptr_t *RetAddrLoc = &StackPtr[-1];
76 assert(*RetAddrLoc == RetAddr &&
77 "Could not find return address on the stack!");
79 DOUT << "In callback! Addr=" << (void*)RetAddr
80 << " FP=" << (void*)StackPtr
81 << ": Resolving call to function: "
82 << TheVM->getFunctionReferencedName((void*)RetAddr) << "\n";
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!");
88 intptr_t NewVal = (intptr_t)JITCompilerFunction((void*)RetAddr);
90 // Rewrite the call target... so that we don't end up here every time we
92 *(intptr_t *)RetAddr = (intptr_t)(NewVal-RetAddr-4);
94 // Change the return address to reexecute the branch and link instruction...
98 TargetJITInfo::LazyResolverFn
99 ARMJITInfo::getLazyResolverFunction(JITCompilerFn F) {
100 JITCompilerFunction = F;
101 return ARMCompilationCallback;
104 void *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);
116 MCE.startFunctionStub(5, 2);
117 MCE.emitByte(0xEB); // branch and link to the corresponding function addr
119 MCE.emitByte((unsigned char)(addr >> 0));
120 MCE.emitByte((unsigned char)(addr >> 8));
121 MCE.emitByte((unsigned char)(addr >> 16));
123 return MCE.finishFunctionStub(0);
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.
129 void ARMJITInfo::relocate(void *Function, MachineRelocation *MR,
130 unsigned NumRelocs, unsigned char* GOTBase) {