Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===-- ARMJITInfo.cpp - Implement the JIT interfaces for the ARM target --===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 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 Geoffray | 2b483b5 | 2008-04-16 20:46:05 +0000 | [diff] [blame] | 18 | #include "llvm/Function.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineCodeEmitter.h" |
| 20 | #include "llvm/Config/alloca.h" |
| 21 | #include <cstdlib> |
| 22 | using namespace llvm; |
| 23 | |
| 24 | void ARMJITInfo::replaceMachineCodeForFunction(void *Old, void *New) { |
Raul Herbster | 72551da | 2007-08-30 23:21:27 +0000 | [diff] [blame] | 25 | abort(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | /// JITCompilerFunction - This contains the address of the JIT function used to |
| 29 | /// compile a function lazily. |
| 30 | static TargetJITInfo::JITCompilerFn JITCompilerFunction; |
| 31 | |
Evan Cheng | 5d582a6 | 2008-09-02 07:49:03 +0000 | [diff] [blame] | 32 | // Get the ASMPREFIX for the current host. This is often '_'. |
| 33 | #ifndef __USER_LABEL_PREFIX__ |
| 34 | #define __USER_LABEL_PREFIX__ |
| 35 | #endif |
| 36 | #define GETASMPREFIX2(X) #X |
| 37 | #define GETASMPREFIX(X) GETASMPREFIX2(X) |
| 38 | #define ASMPREFIX GETASMPREFIX(__USER_LABEL_PREFIX__) |
| 39 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 40 | // CompilationCallback stub - We can't use a C function with inline assembly in |
| 41 | // it, because we the prolog/epilog inserted by GCC won't work for us. Instead, |
| 42 | // write our own wrapper, which does things our way, so we have complete control |
| 43 | // over register saving and restoring. |
| 44 | extern "C" { |
| 45 | #if defined(__arm__) |
| 46 | void ARMCompilationCallback(void); |
| 47 | asm( |
| 48 | ".text\n" |
| 49 | ".align 2\n" |
Evan Cheng | 5d582a6 | 2008-09-02 07:49:03 +0000 | [diff] [blame] | 50 | ".globl " ASMPREFIX "ARMCompilationCallback\n" |
| 51 | ASMPREFIX "ARMCompilationCallback:\n" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 52 | // save main registers |
Evan Cheng | 5d582a6 | 2008-09-02 07:49:03 +0000 | [diff] [blame] | 53 | #if defined(__APPLE__) |
| 54 | "stmfd sp!, {r4, r5, r6, r7, lr}\n" |
| 55 | "mov r0, r7\n" // stub's frame |
| 56 | "stmfd sp!, {r8, r10, r11}\n" |
| 57 | #else |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 58 | "mov ip, sp\n" |
| 59 | "stmfd sp!, {fp, ip, lr, pc}\n" |
| 60 | "sub fp, ip, #4\n" |
Evan Cheng | 5d582a6 | 2008-09-02 07:49:03 +0000 | [diff] [blame] | 61 | #endif // __APPLE__ |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 62 | // arguments to Compilation Callback |
| 63 | // r0 - our lr (address of the call instruction in stub plus 4) |
| 64 | // r1 - stub's lr (address of instruction that called the stub plus 4) |
Evan Cheng | 5d582a6 | 2008-09-02 07:49:03 +0000 | [diff] [blame] | 65 | #if defined(__APPLE__) |
| 66 | "mov r0, r7\n" // stub's frame |
| 67 | #else |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 68 | "mov r0, fp\n" // stub's frame |
Evan Cheng | 5d582a6 | 2008-09-02 07:49:03 +0000 | [diff] [blame] | 69 | #endif // __APPLE__ |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 70 | "mov r1, lr\n" // stub's lr |
Evan Cheng | 5d582a6 | 2008-09-02 07:49:03 +0000 | [diff] [blame] | 71 | "bl " ASMPREFIX "ARMCompilationCallbackC\n" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 72 | // restore main registers |
Evan Cheng | 5d582a6 | 2008-09-02 07:49:03 +0000 | [diff] [blame] | 73 | #if defined(__APPLE__) |
| 74 | "ldmfd sp!, {r8, r10, r11}\n" |
| 75 | "ldmfd sp!, {r4, r5, r6, r7, pc}\n" |
| 76 | #else |
| 77 | "ldmfd sp, {fp, sp, pc}\n" |
| 78 | #endif // __APPLE__ |
| 79 | ); |
| 80 | #else // Not an ARM host |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 81 | void ARMCompilationCallback() { |
| 82 | assert(0 && "Cannot call ARMCompilationCallback() on a non-ARM arch!\n"); |
| 83 | abort(); |
| 84 | } |
| 85 | #endif |
| 86 | } |
| 87 | |
| 88 | /// ARMCompilationCallbackC - This is the target-specific function invoked by the |
| 89 | /// function stub when we did not know the real target of a call. This function |
| 90 | /// must locate the start of the stub or call site and pass it into the JIT |
| 91 | /// compiler function. |
| 92 | extern "C" void ARMCompilationCallbackC(intptr_t *StackPtr, intptr_t RetAddr) { |
| 93 | intptr_t *RetAddrLoc = &StackPtr[-1]; |
| 94 | |
| 95 | assert(*RetAddrLoc == RetAddr && |
| 96 | "Could not find return address on the stack!"); |
| 97 | #if 0 |
| 98 | DOUT << "In callback! Addr=" << (void*)RetAddr |
| 99 | << " FP=" << (void*)StackPtr |
| 100 | << ": Resolving call to function: " |
| 101 | << TheVM->getFunctionReferencedName((void*)RetAddr) << "\n"; |
| 102 | #endif |
Raul Herbster | 72551da | 2007-08-30 23:21:27 +0000 | [diff] [blame] | 103 | intptr_t Addr = RetAddr - 4; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 104 | |
Raul Herbster | 72551da | 2007-08-30 23:21:27 +0000 | [diff] [blame] | 105 | intptr_t NewVal = (intptr_t)JITCompilerFunction((void*)Addr); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 106 | |
| 107 | // Rewrite the call target... so that we don't end up here every time we |
| 108 | // execute the call. |
Raul Herbster | 72551da | 2007-08-30 23:21:27 +0000 | [diff] [blame] | 109 | *(intptr_t *)Addr = NewVal; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 110 | |
| 111 | // Change the return address to reexecute the branch and link instruction... |
Raul Herbster | 72551da | 2007-08-30 23:21:27 +0000 | [diff] [blame] | 112 | *RetAddrLoc -= 12; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | TargetJITInfo::LazyResolverFn |
| 116 | ARMJITInfo::getLazyResolverFunction(JITCompilerFn F) { |
| 117 | JITCompilerFunction = F; |
| 118 | return ARMCompilationCallback; |
| 119 | } |
| 120 | |
Nicolas Geoffray | 2b483b5 | 2008-04-16 20:46:05 +0000 | [diff] [blame] | 121 | void *ARMJITInfo::emitFunctionStub(const Function* F, void *Fn, |
| 122 | MachineCodeEmitter &MCE) { |
Raul Herbster | 72551da | 2007-08-30 23:21:27 +0000 | [diff] [blame] | 123 | unsigned addr = (intptr_t)Fn; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 124 | // If this is just a call to an external function, emit a branch instead of a |
| 125 | // call. The code is the same except for one bit of the last instruction. |
| 126 | if (Fn != (void*)(intptr_t)ARMCompilationCallback) { |
Raul Herbster | 72551da | 2007-08-30 23:21:27 +0000 | [diff] [blame] | 127 | // branch to the corresponding function addr |
| 128 | // the stub is 8-byte size and 4-aligned |
Nicolas Geoffray | 2b483b5 | 2008-04-16 20:46:05 +0000 | [diff] [blame] | 129 | MCE.startFunctionStub(F, 8, 4); |
Raul Herbster | 72551da | 2007-08-30 23:21:27 +0000 | [diff] [blame] | 130 | MCE.emitWordLE(0xE51FF004); // LDR PC, [PC,#-4] |
| 131 | MCE.emitWordLE(addr); // addr of function |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 132 | } else { |
Raul Herbster | 72551da | 2007-08-30 23:21:27 +0000 | [diff] [blame] | 133 | // branch and link to the corresponding function addr |
| 134 | // the stub is 20-byte size and 4-aligned |
Nicolas Geoffray | 2b483b5 | 2008-04-16 20:46:05 +0000 | [diff] [blame] | 135 | MCE.startFunctionStub(F, 20, 4); |
Raul Herbster | 72551da | 2007-08-30 23:21:27 +0000 | [diff] [blame] | 136 | MCE.emitWordLE(0xE92D4800); // STMFD SP!, [R11, LR] |
| 137 | MCE.emitWordLE(0xE28FE004); // ADD LR, PC, #4 |
| 138 | MCE.emitWordLE(0xE51FF004); // LDR PC, [PC,#-4] |
| 139 | MCE.emitWordLE(addr); // addr of function |
| 140 | MCE.emitWordLE(0xE8BD8800); // LDMFD SP!, [R11, PC] |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 141 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 142 | |
Nicolas Geoffray | 2b483b5 | 2008-04-16 20:46:05 +0000 | [diff] [blame] | 143 | return MCE.finishFunctionStub(F); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /// relocate - Before the JIT can run a block of code that has been emitted, |
| 147 | /// it must rewrite the code to contain the actual addresses of any |
| 148 | /// referenced global symbols. |
| 149 | void ARMJITInfo::relocate(void *Function, MachineRelocation *MR, |
| 150 | unsigned NumRelocs, unsigned char* GOTBase) { |
Evan Cheng | a7b3e7c | 2007-08-07 01:37:15 +0000 | [diff] [blame] | 151 | for (unsigned i = 0; i != NumRelocs; ++i, ++MR) { |
| 152 | void *RelocPos = (char*)Function + MR->getMachineCodeOffset(); |
| 153 | intptr_t ResultPtr = (intptr_t)MR->getResultPointer(); |
| 154 | switch ((ARM::RelocationType)MR->getRelocationType()) { |
| 155 | case ARM::reloc_arm_relative: { |
Raul Herbster | 72551da | 2007-08-30 23:21:27 +0000 | [diff] [blame] | 156 | // It is necessary to calculate the correct PC relative value. We |
| 157 | // subtract the base addr from the target addr to form a byte offset. |
| 158 | ResultPtr = ResultPtr-(intptr_t)RelocPos-8; |
| 159 | // If the result is positive, set bit U(23) to 1. |
| 160 | if (ResultPtr >= 0) |
| 161 | *((unsigned*)RelocPos) |= 1 << 23; |
| 162 | else { |
| 163 | // otherwise, obtain the absolute value and set |
| 164 | // bit U(23) to 0. |
| 165 | ResultPtr *= -1; |
| 166 | *((unsigned*)RelocPos) &= 0xFF7FFFFF; |
| 167 | } |
| 168 | // set the immed value calculated |
| 169 | *((unsigned*)RelocPos) |= (unsigned)ResultPtr; |
| 170 | // set register Rn to PC |
| 171 | *((unsigned*)RelocPos) |= 0xF << 16; |
Evan Cheng | a7b3e7c | 2007-08-07 01:37:15 +0000 | [diff] [blame] | 172 | break; |
| 173 | } |
Evan Cheng | a7b3e7c | 2007-08-07 01:37:15 +0000 | [diff] [blame] | 174 | case ARM::reloc_arm_branch: { |
Raul Herbster | 72551da | 2007-08-30 23:21:27 +0000 | [diff] [blame] | 175 | // It is necessary to calculate the correct value of signed_immed_24 |
| 176 | // field. We subtract the base addr from the target addr to form a |
| 177 | // byte offset, which must be inside the range -33554432 and +33554428. |
| 178 | // Then, we set the signed_immed_24 field of the instruction to bits |
| 179 | // [25:2] of the byte offset. More details ARM-ARM p. A4-11. |
| 180 | ResultPtr = ResultPtr-(intptr_t)RelocPos-8; |
| 181 | ResultPtr = (ResultPtr & 0x03FFFFFC) >> 2; |
| 182 | assert(ResultPtr >= -33554432 && ResultPtr <= 33554428); |
Evan Cheng | a7b3e7c | 2007-08-07 01:37:15 +0000 | [diff] [blame] | 183 | *((unsigned*)RelocPos) |= ResultPtr; |
| 184 | break; |
| 185 | } |
| 186 | } |
| 187 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 188 | } |