Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 1 | //===-- ARMJITInfo.cpp - Implement the JIT interfaces for the ARM target --===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 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. |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +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" |
Evan Cheng | 25e0478 | 2008-11-04 00:50:32 +0000 | [diff] [blame] | 16 | #include "ARMConstantPoolValue.h" |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 17 | #include "ARMRelocations.h" |
| 18 | #include "ARMSubtarget.h" |
Nicolas Geoffray | 51cc3c1 | 2008-04-16 20:46:05 +0000 | [diff] [blame] | 19 | #include "llvm/Function.h" |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineCodeEmitter.h" |
| 21 | #include "llvm/Config/alloca.h" |
Jim Grosbach | 932a32d | 2008-10-20 21:39:23 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Streams.h" |
| 23 | #include "llvm/System/Memory.h" |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 24 | #include <cstdlib> |
| 25 | using namespace llvm; |
| 26 | |
| 27 | void ARMJITInfo::replaceMachineCodeForFunction(void *Old, void *New) { |
Raul Herbster | d05c04c | 2007-08-30 23:21:27 +0000 | [diff] [blame] | 28 | abort(); |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | /// JITCompilerFunction - This contains the address of the JIT function used to |
| 32 | /// compile a function lazily. |
| 33 | static TargetJITInfo::JITCompilerFn JITCompilerFunction; |
| 34 | |
Evan Cheng | 95ce117 | 2008-09-02 07:49:03 +0000 | [diff] [blame] | 35 | // Get the ASMPREFIX for the current host. This is often '_'. |
| 36 | #ifndef __USER_LABEL_PREFIX__ |
| 37 | #define __USER_LABEL_PREFIX__ |
| 38 | #endif |
| 39 | #define GETASMPREFIX2(X) #X |
| 40 | #define GETASMPREFIX(X) GETASMPREFIX2(X) |
| 41 | #define ASMPREFIX GETASMPREFIX(__USER_LABEL_PREFIX__) |
| 42 | |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 43 | // CompilationCallback stub - We can't use a C function with inline assembly in |
Jim Grosbach | 932a32d | 2008-10-20 21:39:23 +0000 | [diff] [blame] | 44 | // it, because we the prolog/epilog inserted by GCC won't work for us (we need |
| 45 | // to preserve more context and manipulate the stack directly). Instead, |
| 46 | // write our own wrapper, which does things our way, so we have complete |
| 47 | // control over register saving and restoring. |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 48 | extern "C" { |
| 49 | #if defined(__arm__) |
| 50 | void ARMCompilationCallback(void); |
| 51 | asm( |
| 52 | ".text\n" |
| 53 | ".align 2\n" |
Evan Cheng | 95ce117 | 2008-09-02 07:49:03 +0000 | [diff] [blame] | 54 | ".globl " ASMPREFIX "ARMCompilationCallback\n" |
| 55 | ASMPREFIX "ARMCompilationCallback:\n" |
Jim Grosbach | 932a32d | 2008-10-20 21:39:23 +0000 | [diff] [blame] | 56 | // Save caller saved registers since they may contain stuff |
| 57 | // for the real target function right now. We have to act as if this |
| 58 | // whole compilation callback doesn't exist as far as the caller is |
| 59 | // concerned, so we can't just preserve the callee saved regs. |
Jim Grosbach | a9ab95b | 2008-10-21 16:54:12 +0000 | [diff] [blame] | 60 | "stmdb sp!, {r0, r1, r2, r3, lr}\n" |
Jim Grosbach | 932a32d | 2008-10-20 21:39:23 +0000 | [diff] [blame] | 61 | // The LR contains the address of the stub function on entry. |
| 62 | // pass it as the argument to the C part of the callback |
| 63 | "mov r0, lr\n" |
| 64 | "sub sp, sp, #4\n" |
| 65 | // Call the C portion of the callback |
| 66 | "bl " ASMPREFIX "ARMCompilationCallbackC\n" |
| 67 | "add sp, sp, #4\n" |
| 68 | // Restoring the LR to the return address of the function that invoked |
| 69 | // the stub and de-allocating the stack space for it requires us to |
| 70 | // swap the two saved LR values on the stack, as they're backwards |
| 71 | // for what we need since the pop instruction has a pre-determined |
| 72 | // order for the registers. |
| 73 | // +--------+ |
| 74 | // 0 | LR | Original return address |
| 75 | // +--------+ |
| 76 | // 1 | LR | Stub address (start of stub) |
| 77 | // 2-5 | R3..R0 | Saved registers (we need to preserve all regs) |
| 78 | // +--------+ |
| 79 | // |
| 80 | // We need to exchange the values in slots 0 and 1 so we can |
| 81 | // return to the address in slot 1 with the address in slot 0 |
| 82 | // restored to the LR. |
| 83 | "ldr r0, [sp,#20]\n" |
| 84 | "ldr r1, [sp,#16]\n" |
| 85 | "str r1, [sp,#20]\n" |
| 86 | "str r0, [sp,#16]\n" |
| 87 | // Return to the (newly modified) stub to invoke the real function. |
| 88 | // The above twiddling of the saved return addresses allows us to |
| 89 | // deallocate everything, including the LR the stub saved, all in one |
| 90 | // pop instruction. |
Jim Grosbach | a9ab95b | 2008-10-21 16:54:12 +0000 | [diff] [blame] | 91 | "ldmia sp!, {r0, r1, r2, r3, lr, pc}\n" |
Evan Cheng | 95ce117 | 2008-09-02 07:49:03 +0000 | [diff] [blame] | 92 | ); |
| 93 | #else // Not an ARM host |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 94 | void ARMCompilationCallback() { |
| 95 | assert(0 && "Cannot call ARMCompilationCallback() on a non-ARM arch!\n"); |
| 96 | abort(); |
| 97 | } |
| 98 | #endif |
| 99 | } |
| 100 | |
Jim Grosbach | 932a32d | 2008-10-20 21:39:23 +0000 | [diff] [blame] | 101 | /// ARMCompilationCallbackC - This is the target-specific function invoked |
| 102 | /// by the function stub when we did not know the real target of a call. |
| 103 | /// This function must locate the start of the stub or call site and pass |
| 104 | /// it into the JIT compiler function. |
| 105 | extern "C" void ARMCompilationCallbackC(intptr_t StubAddr) { |
| 106 | // Get the address of the compiled code for this function. |
| 107 | intptr_t NewVal = (intptr_t)JITCompilerFunction((void*)StubAddr); |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 108 | |
| 109 | // Rewrite the call target... so that we don't end up here every time we |
Jim Grosbach | 932a32d | 2008-10-20 21:39:23 +0000 | [diff] [blame] | 110 | // execute the call. We're replacing the first two instructions of the |
| 111 | // stub with: |
| 112 | // ldr pc, [pc,#-4] |
| 113 | // <addr> |
Evan Cheng | ae16641 | 2008-11-08 08:16:49 +0000 | [diff] [blame] | 114 | if (!sys::Memory::setRangeWritable((void*)StubAddr, 8)) { |
| 115 | cerr << "ERROR: Unable to mark stub writable\n"; |
| 116 | abort(); |
| 117 | } |
| 118 | *(intptr_t *)StubAddr = 0xe51ff004; // ldr pc, [pc, #-4] |
Jim Grosbach | 932a32d | 2008-10-20 21:39:23 +0000 | [diff] [blame] | 119 | *(intptr_t *)(StubAddr+4) = NewVal; |
Evan Cheng | ae16641 | 2008-11-08 08:16:49 +0000 | [diff] [blame] | 120 | if (!sys::Memory::setRangeExecutable((void*)StubAddr, 8)) { |
| 121 | cerr << "ERROR: Unable to mark stub executable\n"; |
| 122 | abort(); |
| 123 | } |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | TargetJITInfo::LazyResolverFn |
| 127 | ARMJITInfo::getLazyResolverFunction(JITCompilerFn F) { |
| 128 | JITCompilerFunction = F; |
| 129 | return ARMCompilationCallback; |
| 130 | } |
| 131 | |
Evan Cheng | 9ed2f80 | 2008-11-10 01:08:07 +0000 | [diff] [blame] | 132 | void *ARMJITInfo::emitGlobalValueIndirectSym(const GlobalValue *GV, void *Ptr, |
| 133 | MachineCodeEmitter &MCE) { |
Evan Cheng | ce4a70b | 2008-11-08 08:02:53 +0000 | [diff] [blame] | 134 | MCE.startGVStub(GV, 4, 4); |
Evan Cheng | e96a490 | 2008-11-08 01:31:27 +0000 | [diff] [blame] | 135 | MCE.emitWordLE((intptr_t)Ptr); |
Evan Cheng | ce4a70b | 2008-11-08 08:02:53 +0000 | [diff] [blame] | 136 | return MCE.finishGVStub(GV); |
Evan Cheng | e96a490 | 2008-11-08 01:31:27 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Nicolas Geoffray | 51cc3c1 | 2008-04-16 20:46:05 +0000 | [diff] [blame] | 139 | void *ARMJITInfo::emitFunctionStub(const Function* F, void *Fn, |
| 140 | MachineCodeEmitter &MCE) { |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 141 | // If this is just a call to an external function, emit a branch instead of a |
| 142 | // call. The code is the same except for one bit of the last instruction. |
| 143 | if (Fn != (void*)(intptr_t)ARMCompilationCallback) { |
Evan Cheng | e96a490 | 2008-11-08 01:31:27 +0000 | [diff] [blame] | 144 | // Branch to the corresponding function addr. |
| 145 | // The stub is 8-byte size and 4-aligned. |
Evan Cheng | ce4a70b | 2008-11-08 08:02:53 +0000 | [diff] [blame] | 146 | MCE.startGVStub(F, 8, 4); |
Evan Cheng | ae16641 | 2008-11-08 08:16:49 +0000 | [diff] [blame] | 147 | intptr_t Addr = (intptr_t)MCE.getCurrentPCValue(); |
| 148 | MCE.emitWordLE(0xe51ff004); // ldr pc, [pc, #-4] |
| 149 | MCE.emitWordLE((intptr_t)Fn); // addr of function |
| 150 | sys::Memory::InvalidateInstructionCache((void*)Addr, 8); |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 151 | } else { |
Jim Grosbach | 932a32d | 2008-10-20 21:39:23 +0000 | [diff] [blame] | 152 | // The compilation callback will overwrite the first two words of this |
| 153 | // stub with indirect branch instructions targeting the compiled code. |
| 154 | // This stub sets the return address to restart the stub, so that |
| 155 | // the new branch will be invoked when we come back. |
| 156 | // |
Evan Cheng | e96a490 | 2008-11-08 01:31:27 +0000 | [diff] [blame] | 157 | // Branch and link to the compilation callback. |
| 158 | // The stub is 16-byte size and 4-byte aligned. |
Evan Cheng | ce4a70b | 2008-11-08 08:02:53 +0000 | [diff] [blame] | 159 | MCE.startGVStub(F, 16, 4); |
Evan Cheng | ae16641 | 2008-11-08 08:16:49 +0000 | [diff] [blame] | 160 | intptr_t Addr = (intptr_t)MCE.getCurrentPCValue(); |
Jim Grosbach | 932a32d | 2008-10-20 21:39:23 +0000 | [diff] [blame] | 161 | // Save LR so the callback can determine which stub called it. |
| 162 | // The compilation callback is responsible for popping this prior |
| 163 | // to returning. |
Evan Cheng | ae16641 | 2008-11-08 08:16:49 +0000 | [diff] [blame] | 164 | MCE.emitWordLE(0xe92d4000); // push {lr} |
| 165 | // Set the return address to go back to the start of this stub. |
| 166 | MCE.emitWordLE(0xe24fe00c); // sub lr, pc, #12 |
| 167 | // Invoke the compilation callback. |
| 168 | MCE.emitWordLE(0xe51ff004); // ldr pc, [pc, #-4] |
| 169 | // The address of the compilation callback. |
Jim Grosbach | 932a32d | 2008-10-20 21:39:23 +0000 | [diff] [blame] | 170 | MCE.emitWordLE((intptr_t)ARMCompilationCallback); |
Evan Cheng | ae16641 | 2008-11-08 08:16:49 +0000 | [diff] [blame] | 171 | sys::Memory::InvalidateInstructionCache((void*)Addr, 16); |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 172 | } |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 173 | |
Evan Cheng | ce4a70b | 2008-11-08 08:02:53 +0000 | [diff] [blame] | 174 | return MCE.finishGVStub(F); |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Evan Cheng | 4df60f5 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 177 | intptr_t ARMJITInfo::resolveRelocDestAddr(MachineRelocation *MR) const { |
Evan Cheng | 25e0478 | 2008-11-04 00:50:32 +0000 | [diff] [blame] | 178 | ARM::RelocationType RT = (ARM::RelocationType)MR->getRelocationType(); |
Evan Cheng | 437c173 | 2008-11-07 22:30:53 +0000 | [diff] [blame] | 179 | if (RT == ARM::reloc_arm_pic_jt) |
| 180 | // Destination address - jump table base. |
| 181 | return (intptr_t)(MR->getResultPointer()) - MR->getConstantVal(); |
| 182 | else if (RT == ARM::reloc_arm_jt_base) |
| 183 | // Jump table base address. |
Evan Cheng | 4df60f5 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 184 | return getJumpTableBaseAddr(MR->getJumpTableIndex()); |
| 185 | else if (RT == ARM::reloc_arm_cp_entry) |
Evan Cheng | 437c173 | 2008-11-07 22:30:53 +0000 | [diff] [blame] | 186 | // Constant pool entry address. |
Evan Cheng | 25e0478 | 2008-11-04 00:50:32 +0000 | [diff] [blame] | 187 | return getConstantPoolEntryAddr(MR->getConstantPoolIndex()); |
| 188 | else if (RT == ARM::reloc_arm_machine_cp_entry) { |
Evan Cheng | 413a89f | 2008-11-07 22:57:53 +0000 | [diff] [blame] | 189 | ARMConstantPoolValue *ACPV = (ARMConstantPoolValue*)MR->getConstantVal(); |
Evan Cheng | 25e0478 | 2008-11-04 00:50:32 +0000 | [diff] [blame] | 190 | assert((!ACPV->hasModifier() && !ACPV->mustAddCurrentAddress()) && |
| 191 | "Can't handle this machine constant pool entry yet!"); |
| 192 | intptr_t Addr = (intptr_t)(MR->getResultPointer()); |
| 193 | Addr -= getPCLabelAddr(ACPV->getLabelId()) + ACPV->getPCAdjustment(); |
| 194 | return Addr; |
| 195 | } |
| 196 | return (intptr_t)(MR->getResultPointer()); |
| 197 | } |
| 198 | |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 199 | /// relocate - Before the JIT can run a block of code that has been emitted, |
| 200 | /// it must rewrite the code to contain the actual addresses of any |
| 201 | /// referenced global symbols. |
| 202 | void ARMJITInfo::relocate(void *Function, MachineRelocation *MR, |
| 203 | unsigned NumRelocs, unsigned char* GOTBase) { |
Evan Cheng | 0ff94f7 | 2007-08-07 01:37:15 +0000 | [diff] [blame] | 204 | for (unsigned i = 0; i != NumRelocs; ++i, ++MR) { |
| 205 | void *RelocPos = (char*)Function + MR->getMachineCodeOffset(); |
Evan Cheng | 4df60f5 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 206 | intptr_t ResultPtr = resolveRelocDestAddr(MR); |
Evan Cheng | 0ff94f7 | 2007-08-07 01:37:15 +0000 | [diff] [blame] | 207 | switch ((ARM::RelocationType)MR->getRelocationType()) { |
Evan Cheng | 0f28243 | 2008-10-29 23:55:43 +0000 | [diff] [blame] | 208 | case ARM::reloc_arm_cp_entry: |
Evan Cheng | 0ff94f7 | 2007-08-07 01:37:15 +0000 | [diff] [blame] | 209 | case ARM::reloc_arm_relative: { |
Raul Herbster | d05c04c | 2007-08-30 23:21:27 +0000 | [diff] [blame] | 210 | // It is necessary to calculate the correct PC relative value. We |
| 211 | // subtract the base addr from the target addr to form a byte offset. |
| 212 | ResultPtr = ResultPtr-(intptr_t)RelocPos-8; |
| 213 | // If the result is positive, set bit U(23) to 1. |
| 214 | if (ResultPtr >= 0) |
| 215 | *((unsigned*)RelocPos) |= 1 << 23; |
| 216 | else { |
Evan Cheng | 25e0478 | 2008-11-04 00:50:32 +0000 | [diff] [blame] | 217 | // Otherwise, obtain the absolute value and set |
Raul Herbster | d05c04c | 2007-08-30 23:21:27 +0000 | [diff] [blame] | 218 | // bit U(23) to 0. |
| 219 | ResultPtr *= -1; |
| 220 | *((unsigned*)RelocPos) &= 0xFF7FFFFF; |
| 221 | } |
Evan Cheng | 25e0478 | 2008-11-04 00:50:32 +0000 | [diff] [blame] | 222 | // Set the immed value calculated. |
Raul Herbster | d05c04c | 2007-08-30 23:21:27 +0000 | [diff] [blame] | 223 | *((unsigned*)RelocPos) |= (unsigned)ResultPtr; |
Evan Cheng | 25e0478 | 2008-11-04 00:50:32 +0000 | [diff] [blame] | 224 | // Set register Rn to PC. |
Raul Herbster | d05c04c | 2007-08-30 23:21:27 +0000 | [diff] [blame] | 225 | *((unsigned*)RelocPos) |= 0xF << 16; |
Evan Cheng | 0ff94f7 | 2007-08-07 01:37:15 +0000 | [diff] [blame] | 226 | break; |
| 227 | } |
Evan Cheng | 437c173 | 2008-11-07 22:30:53 +0000 | [diff] [blame] | 228 | case ARM::reloc_arm_pic_jt: |
Evan Cheng | 25e0478 | 2008-11-04 00:50:32 +0000 | [diff] [blame] | 229 | case ARM::reloc_arm_machine_cp_entry: |
Evan Cheng | 0f28243 | 2008-10-29 23:55:43 +0000 | [diff] [blame] | 230 | case ARM::reloc_arm_absolute: { |
Evan Cheng | 25e0478 | 2008-11-04 00:50:32 +0000 | [diff] [blame] | 231 | // These addresses have already been resolved. |
Evan Cheng | 4df60f5 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 232 | *((unsigned*)RelocPos) |= (unsigned)ResultPtr; |
Evan Cheng | 0f28243 | 2008-10-29 23:55:43 +0000 | [diff] [blame] | 233 | break; |
| 234 | } |
Evan Cheng | 0ff94f7 | 2007-08-07 01:37:15 +0000 | [diff] [blame] | 235 | case ARM::reloc_arm_branch: { |
Raul Herbster | d05c04c | 2007-08-30 23:21:27 +0000 | [diff] [blame] | 236 | // It is necessary to calculate the correct value of signed_immed_24 |
| 237 | // field. We subtract the base addr from the target addr to form a |
| 238 | // byte offset, which must be inside the range -33554432 and +33554428. |
| 239 | // Then, we set the signed_immed_24 field of the instruction to bits |
| 240 | // [25:2] of the byte offset. More details ARM-ARM p. A4-11. |
Evan Cheng | 4df60f5 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 241 | ResultPtr = ResultPtr - (intptr_t)RelocPos - 8; |
Raul Herbster | d05c04c | 2007-08-30 23:21:27 +0000 | [diff] [blame] | 242 | ResultPtr = (ResultPtr & 0x03FFFFFC) >> 2; |
| 243 | assert(ResultPtr >= -33554432 && ResultPtr <= 33554428); |
Evan Cheng | 0ff94f7 | 2007-08-07 01:37:15 +0000 | [diff] [blame] | 244 | *((unsigned*)RelocPos) |= ResultPtr; |
| 245 | break; |
| 246 | } |
Evan Cheng | 4df60f5 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 247 | case ARM::reloc_arm_jt_base: { |
| 248 | // JT base - (instruction addr + 8) |
| 249 | ResultPtr = ResultPtr - (intptr_t)RelocPos - 8; |
| 250 | *((unsigned*)RelocPos) |= ResultPtr; |
| 251 | break; |
| 252 | } |
Evan Cheng | 0ff94f7 | 2007-08-07 01:37:15 +0000 | [diff] [blame] | 253 | } |
| 254 | } |
Evan Cheng | 148b6a4 | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 255 | } |