blob: bc2bba36c80a340293ad8ac07586cfbec88c8b3b [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- ARMJITInfo.cpp - Implement the JIT interfaces for the ARM target --===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the JIT interfaces for the ARM target.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "jit"
15#include "ARMJITInfo.h"
16#include "ARMRelocations.h"
17#include "ARMSubtarget.h"
Nicolas Geoffray2b483b52008-04-16 20:46:05 +000018#include "llvm/Function.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#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) {
Raul Herbster72551da2007-08-30 23:21:27 +000025 abort();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026}
27
28/// JITCompilerFunction - This contains the address of the JIT function used to
29/// compile a function lazily.
30static TargetJITInfo::JITCompilerFn JITCompilerFunction;
31
32// CompilationCallback stub - We can't use a C function with inline assembly in
33// it, because we the prolog/epilog inserted by GCC won't work for us. Instead,
34// write our own wrapper, which does things our way, so we have complete control
35// over register saving and restoring.
36extern "C" {
37#if defined(__arm__)
38 void ARMCompilationCallback(void);
39 asm(
40 ".text\n"
41 ".align 2\n"
42 ".globl ARMCompilationCallback\n"
43 "ARMCompilationCallback:\n"
44 // save main registers
45 "mov ip, sp\n"
46 "stmfd sp!, {fp, ip, lr, pc}\n"
47 "sub fp, ip, #4\n"
48 // arguments to Compilation Callback
49 // r0 - our lr (address of the call instruction in stub plus 4)
50 // r1 - stub's lr (address of instruction that called the stub plus 4)
51 "mov r0, fp\n" // stub's frame
52 "mov r1, lr\n" // stub's lr
53 "bl ARMCompilationCallbackC\n"
54 // restore main registers
55 "ldmfd sp, {fp, sp, pc}\n");
56#else // Not an ARM host
57 void ARMCompilationCallback() {
58 assert(0 && "Cannot call ARMCompilationCallback() on a non-ARM arch!\n");
59 abort();
60 }
61#endif
62}
63
64/// ARMCompilationCallbackC - This is the target-specific function invoked by the
65/// function stub when we did not know the real target of a call. This function
66/// must locate the start of the stub or call site and pass it into the JIT
67/// compiler function.
68extern "C" void ARMCompilationCallbackC(intptr_t *StackPtr, intptr_t RetAddr) {
69 intptr_t *RetAddrLoc = &StackPtr[-1];
70
71 assert(*RetAddrLoc == RetAddr &&
72 "Could not find return address on the stack!");
73#if 0
74 DOUT << "In callback! Addr=" << (void*)RetAddr
75 << " FP=" << (void*)StackPtr
76 << ": Resolving call to function: "
77 << TheVM->getFunctionReferencedName((void*)RetAddr) << "\n";
78#endif
Raul Herbster72551da2007-08-30 23:21:27 +000079 intptr_t Addr = RetAddr - 4;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000080
Raul Herbster72551da2007-08-30 23:21:27 +000081 intptr_t NewVal = (intptr_t)JITCompilerFunction((void*)Addr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082
83 // Rewrite the call target... so that we don't end up here every time we
84 // execute the call.
Raul Herbster72551da2007-08-30 23:21:27 +000085 *(intptr_t *)Addr = NewVal;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000086
87 // Change the return address to reexecute the branch and link instruction...
Raul Herbster72551da2007-08-30 23:21:27 +000088 *RetAddrLoc -= 12;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089}
90
91TargetJITInfo::LazyResolverFn
92ARMJITInfo::getLazyResolverFunction(JITCompilerFn F) {
93 JITCompilerFunction = F;
94 return ARMCompilationCallback;
95}
96
Nicolas Geoffray2b483b52008-04-16 20:46:05 +000097void *ARMJITInfo::emitFunctionStub(const Function* F, void *Fn,
98 MachineCodeEmitter &MCE) {
Raul Herbster72551da2007-08-30 23:21:27 +000099 unsigned addr = (intptr_t)Fn;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000100 // If this is just a call to an external function, emit a branch instead of a
101 // call. The code is the same except for one bit of the last instruction.
102 if (Fn != (void*)(intptr_t)ARMCompilationCallback) {
Raul Herbster72551da2007-08-30 23:21:27 +0000103 // branch to the corresponding function addr
104 // the stub is 8-byte size and 4-aligned
Nicolas Geoffray2b483b52008-04-16 20:46:05 +0000105 MCE.startFunctionStub(F, 8, 4);
Raul Herbster72551da2007-08-30 23:21:27 +0000106 MCE.emitWordLE(0xE51FF004); // LDR PC, [PC,#-4]
107 MCE.emitWordLE(addr); // addr of function
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000108 } else {
Raul Herbster72551da2007-08-30 23:21:27 +0000109 // branch and link to the corresponding function addr
110 // the stub is 20-byte size and 4-aligned
Nicolas Geoffray2b483b52008-04-16 20:46:05 +0000111 MCE.startFunctionStub(F, 20, 4);
Raul Herbster72551da2007-08-30 23:21:27 +0000112 MCE.emitWordLE(0xE92D4800); // STMFD SP!, [R11, LR]
113 MCE.emitWordLE(0xE28FE004); // ADD LR, PC, #4
114 MCE.emitWordLE(0xE51FF004); // LDR PC, [PC,#-4]
115 MCE.emitWordLE(addr); // addr of function
116 MCE.emitWordLE(0xE8BD8800); // LDMFD SP!, [R11, PC]
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118
Nicolas Geoffray2b483b52008-04-16 20:46:05 +0000119 return MCE.finishFunctionStub(F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120}
121
122/// relocate - Before the JIT can run a block of code that has been emitted,
123/// it must rewrite the code to contain the actual addresses of any
124/// referenced global symbols.
125void ARMJITInfo::relocate(void *Function, MachineRelocation *MR,
126 unsigned NumRelocs, unsigned char* GOTBase) {
Evan Chenga7b3e7c2007-08-07 01:37:15 +0000127 for (unsigned i = 0; i != NumRelocs; ++i, ++MR) {
128 void *RelocPos = (char*)Function + MR->getMachineCodeOffset();
129 intptr_t ResultPtr = (intptr_t)MR->getResultPointer();
130 switch ((ARM::RelocationType)MR->getRelocationType()) {
131 case ARM::reloc_arm_relative: {
Raul Herbster72551da2007-08-30 23:21:27 +0000132 // It is necessary to calculate the correct PC relative value. We
133 // subtract the base addr from the target addr to form a byte offset.
134 ResultPtr = ResultPtr-(intptr_t)RelocPos-8;
135 // If the result is positive, set bit U(23) to 1.
136 if (ResultPtr >= 0)
137 *((unsigned*)RelocPos) |= 1 << 23;
138 else {
139 // otherwise, obtain the absolute value and set
140 // bit U(23) to 0.
141 ResultPtr *= -1;
142 *((unsigned*)RelocPos) &= 0xFF7FFFFF;
143 }
144 // set the immed value calculated
145 *((unsigned*)RelocPos) |= (unsigned)ResultPtr;
146 // set register Rn to PC
147 *((unsigned*)RelocPos) |= 0xF << 16;
Evan Chenga7b3e7c2007-08-07 01:37:15 +0000148 break;
149 }
Evan Chenga7b3e7c2007-08-07 01:37:15 +0000150 case ARM::reloc_arm_branch: {
Raul Herbster72551da2007-08-30 23:21:27 +0000151 // It is necessary to calculate the correct value of signed_immed_24
152 // field. We subtract the base addr from the target addr to form a
153 // byte offset, which must be inside the range -33554432 and +33554428.
154 // Then, we set the signed_immed_24 field of the instruction to bits
155 // [25:2] of the byte offset. More details ARM-ARM p. A4-11.
156 ResultPtr = ResultPtr-(intptr_t)RelocPos-8;
157 ResultPtr = (ResultPtr & 0x03FFFFFC) >> 2;
158 assert(ResultPtr >= -33554432 && ResultPtr <= 33554428);
Evan Chenga7b3e7c2007-08-07 01:37:15 +0000159 *((unsigned*)RelocPos) |= ResultPtr;
160 break;
161 }
162 }
163 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000164}