| Evan Cheng | 9546a5c | 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 | f3ebc3f | 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 | 9546a5c | 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" | 
| Craig Topper | 07720d8 | 2012-03-25 23:49:58 +0000 | [diff] [blame] | 16 | #include "ARM.h" | 
| Evan Cheng | 6dd08b6 | 2008-11-04 00:50:32 +0000 | [diff] [blame] | 17 | #include "ARMConstantPoolValue.h" | 
| Evan Cheng | 9546a5c | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 18 | #include "ARMRelocations.h" | 
|  | 19 | #include "ARMSubtarget.h" | 
| Bruno Cardoso Lopes | a194c3a | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/JITCodeEmitter.h" | 
| Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Function.h" | 
| Evan Cheng | 02771dc | 2008-11-10 23:14:47 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Debug.h" | 
| Torok Edwin | 6dd2730 | 2009-07-08 18:01:40 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ErrorHandling.h" | 
| Michael J. Spencer | ab425d8 | 2010-11-29 18:47:54 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Memory.h" | 
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" | 
| Evan Cheng | 9546a5c | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 26 | #include <cstdlib> | 
|  | 27 | using namespace llvm; | 
|  | 28 |  | 
|  | 29 | void ARMJITInfo::replaceMachineCodeForFunction(void *Old, void *New) { | 
| Chris Lattner | 2104b8d | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 30 | report_fatal_error("ARMJITInfo::replaceMachineCodeForFunction"); | 
| Evan Cheng | 9546a5c | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 31 | } | 
|  | 32 |  | 
|  | 33 | /// JITCompilerFunction - This contains the address of the JIT function used to | 
|  | 34 | /// compile a function lazily. | 
|  | 35 | static TargetJITInfo::JITCompilerFn JITCompilerFunction; | 
|  | 36 |  | 
| Evan Cheng | df8cdc3 | 2008-09-02 07:49:03 +0000 | [diff] [blame] | 37 | // Get the ASMPREFIX for the current host.  This is often '_'. | 
|  | 38 | #ifndef __USER_LABEL_PREFIX__ | 
|  | 39 | #define __USER_LABEL_PREFIX__ | 
|  | 40 | #endif | 
|  | 41 | #define GETASMPREFIX2(X) #X | 
|  | 42 | #define GETASMPREFIX(X) GETASMPREFIX2(X) | 
|  | 43 | #define ASMPREFIX GETASMPREFIX(__USER_LABEL_PREFIX__) | 
|  | 44 |  | 
| Evan Cheng | 9546a5c | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 45 | // CompilationCallback stub - We can't use a C function with inline assembly in | 
| Bob Wilson | 5951320 | 2011-02-01 22:30:51 +0000 | [diff] [blame] | 46 | // it, because the prolog/epilog inserted by GCC won't work for us. (We need | 
| Jim Grosbach | 9396051 | 2008-10-20 21:39:23 +0000 | [diff] [blame] | 47 | // to preserve more context and manipulate the stack directly).  Instead, | 
| Jim Grosbach | f24f9d9 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 48 | // write our own wrapper, which does things our way, so we have complete | 
| Jim Grosbach | 9396051 | 2008-10-20 21:39:23 +0000 | [diff] [blame] | 49 | // control over register saving and restoring. | 
| Evan Cheng | 9546a5c | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 50 | extern "C" { | 
|  | 51 | #if defined(__arm__) | 
| Dan Gohman | 1432ef8 | 2009-08-12 22:10:57 +0000 | [diff] [blame] | 52 | void ARMCompilationCallback(); | 
| Evan Cheng | 9546a5c | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 53 | asm( | 
|  | 54 | ".text\n" | 
|  | 55 | ".align 2\n" | 
| Evan Cheng | df8cdc3 | 2008-09-02 07:49:03 +0000 | [diff] [blame] | 56 | ".globl " ASMPREFIX "ARMCompilationCallback\n" | 
|  | 57 | ASMPREFIX "ARMCompilationCallback:\n" | 
| Jim Grosbach | 9396051 | 2008-10-20 21:39:23 +0000 | [diff] [blame] | 58 | // Save caller saved registers since they may contain stuff | 
|  | 59 | // for the real target function right now. We have to act as if this | 
|  | 60 | // whole compilation callback doesn't exist as far as the caller is | 
|  | 61 | // concerned, so we can't just preserve the callee saved regs. | 
| Jim Grosbach | cfebc18 | 2008-10-21 16:54:12 +0000 | [diff] [blame] | 62 | "stmdb sp!, {r0, r1, r2, r3, lr}\n" | 
| Xerxes Ranby | 09d9a69 | 2010-03-02 13:42:03 +0000 | [diff] [blame] | 63 | #if (defined(__VFP_FP__) && !defined(__SOFTFP__)) | 
| Bob Wilson | 274d6f1 | 2012-03-12 06:15:36 +0000 | [diff] [blame] | 64 | "vstmdb sp!, {d0, d1, d2, d3, d4, d5, d6, d7}\n" | 
| Evan Cheng | ea68423 | 2008-11-13 23:28:54 +0000 | [diff] [blame] | 65 | #endif | 
| Jim Grosbach | 9396051 | 2008-10-20 21:39:23 +0000 | [diff] [blame] | 66 | // The LR contains the address of the stub function on entry. | 
|  | 67 | // pass it as the argument to the C part of the callback | 
|  | 68 | "mov  r0, lr\n" | 
|  | 69 | "sub  sp, sp, #4\n" | 
|  | 70 | // Call the C portion of the callback | 
|  | 71 | "bl   " ASMPREFIX "ARMCompilationCallbackC\n" | 
|  | 72 | "add  sp, sp, #4\n" | 
|  | 73 | // Restoring the LR to the return address of the function that invoked | 
|  | 74 | // the stub and de-allocating the stack space for it requires us to | 
|  | 75 | // swap the two saved LR values on the stack, as they're backwards | 
|  | 76 | // for what we need since the pop instruction has a pre-determined | 
|  | 77 | // order for the registers. | 
|  | 78 | //      +--------+ | 
|  | 79 | //   0  | LR     | Original return address | 
| Jim Grosbach | f24f9d9 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 80 | //      +--------+ | 
| Jim Grosbach | 9396051 | 2008-10-20 21:39:23 +0000 | [diff] [blame] | 81 | //   1  | LR     | Stub address (start of stub) | 
|  | 82 | // 2-5  | R3..R0 | Saved registers (we need to preserve all regs) | 
| Evan Cheng | ea68423 | 2008-11-13 23:28:54 +0000 | [diff] [blame] | 83 | // 6-20 | D0..D7 | Saved VFP registers | 
| Jim Grosbach | f24f9d9 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 84 | //      +--------+ | 
| Jim Grosbach | 9396051 | 2008-10-20 21:39:23 +0000 | [diff] [blame] | 85 | // | 
| Xerxes Ranby | 09d9a69 | 2010-03-02 13:42:03 +0000 | [diff] [blame] | 86 | #if (defined(__VFP_FP__) && !defined(__SOFTFP__)) | 
| Evan Cheng | ea68423 | 2008-11-13 23:28:54 +0000 | [diff] [blame] | 87 | // Restore VFP caller-saved registers. | 
| Bob Wilson | 274d6f1 | 2012-03-12 06:15:36 +0000 | [diff] [blame] | 88 | "vldmia sp!, {d0, d1, d2, d3, d4, d5, d6, d7}\n" | 
| Evan Cheng | ea68423 | 2008-11-13 23:28:54 +0000 | [diff] [blame] | 89 | #endif | 
|  | 90 | // | 
| Jim Grosbach | 9396051 | 2008-10-20 21:39:23 +0000 | [diff] [blame] | 91 | //      We need to exchange the values in slots 0 and 1 so we can | 
|  | 92 | //      return to the address in slot 1 with the address in slot 0 | 
|  | 93 | //      restored to the LR. | 
|  | 94 | "ldr  r0, [sp,#20]\n" | 
|  | 95 | "ldr  r1, [sp,#16]\n" | 
|  | 96 | "str  r1, [sp,#20]\n" | 
|  | 97 | "str  r0, [sp,#16]\n" | 
|  | 98 | // Return to the (newly modified) stub to invoke the real function. | 
|  | 99 | // The above twiddling of the saved return addresses allows us to | 
| Bob Wilson | 09a6b46 | 2011-02-02 17:29:40 +0000 | [diff] [blame] | 100 | // deallocate everything, including the LR the stub saved, with two | 
|  | 101 | // updating load instructions. | 
| Bob Wilson | 5951320 | 2011-02-01 22:30:51 +0000 | [diff] [blame] | 102 | "ldmia  sp!, {r0, r1, r2, r3, lr}\n" | 
|  | 103 | "ldr    pc, [sp], #4\n" | 
| Evan Cheng | df8cdc3 | 2008-09-02 07:49:03 +0000 | [diff] [blame] | 104 | ); | 
|  | 105 | #else  // Not an ARM host | 
| Evan Cheng | 9546a5c | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 106 | void ARMCompilationCallback() { | 
| Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 107 | llvm_unreachable("Cannot call ARMCompilationCallback() on a non-ARM arch!"); | 
| Evan Cheng | 9546a5c | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 108 | } | 
|  | 109 | #endif | 
|  | 110 | } | 
|  | 111 |  | 
| Jim Grosbach | f24f9d9 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 112 | /// ARMCompilationCallbackC - This is the target-specific function invoked | 
|  | 113 | /// by the function stub when we did not know the real target of a call. | 
|  | 114 | /// This function must locate the start of the stub or call site and pass | 
| Jim Grosbach | 9396051 | 2008-10-20 21:39:23 +0000 | [diff] [blame] | 115 | /// it into the JIT compiler function. | 
|  | 116 | extern "C" void ARMCompilationCallbackC(intptr_t StubAddr) { | 
|  | 117 | // Get the address of the compiled code for this function. | 
|  | 118 | intptr_t NewVal = (intptr_t)JITCompilerFunction((void*)StubAddr); | 
| Evan Cheng | 9546a5c | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 119 |  | 
|  | 120 | // Rewrite the call target... so that we don't end up here every time we | 
| Jim Grosbach | 9396051 | 2008-10-20 21:39:23 +0000 | [diff] [blame] | 121 | // execute the call. We're replacing the first two instructions of the | 
|  | 122 | // stub with: | 
|  | 123 | //   ldr pc, [pc,#-4] | 
|  | 124 | //   <addr> | 
| Evan Cheng | 436bdcd | 2008-11-08 08:16:49 +0000 | [diff] [blame] | 125 | if (!sys::Memory::setRangeWritable((void*)StubAddr, 8)) { | 
| Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 126 | llvm_unreachable("ERROR: Unable to mark stub writable"); | 
| Evan Cheng | 436bdcd | 2008-11-08 08:16:49 +0000 | [diff] [blame] | 127 | } | 
|  | 128 | *(intptr_t *)StubAddr = 0xe51ff004;  // ldr pc, [pc, #-4] | 
| Jim Grosbach | 9396051 | 2008-10-20 21:39:23 +0000 | [diff] [blame] | 129 | *(intptr_t *)(StubAddr+4) = NewVal; | 
| Evan Cheng | 436bdcd | 2008-11-08 08:16:49 +0000 | [diff] [blame] | 130 | if (!sys::Memory::setRangeExecutable((void*)StubAddr, 8)) { | 
| Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 131 | llvm_unreachable("ERROR: Unable to mark stub executable"); | 
| Evan Cheng | 436bdcd | 2008-11-08 08:16:49 +0000 | [diff] [blame] | 132 | } | 
| Evan Cheng | 9546a5c | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 133 | } | 
|  | 134 |  | 
|  | 135 | TargetJITInfo::LazyResolverFn | 
|  | 136 | ARMJITInfo::getLazyResolverFunction(JITCompilerFn F) { | 
|  | 137 | JITCompilerFunction = F; | 
|  | 138 | return ARMCompilationCallback; | 
|  | 139 | } | 
|  | 140 |  | 
| Evan Cheng | 9f3058f | 2008-11-10 01:08:07 +0000 | [diff] [blame] | 141 | void *ARMJITInfo::emitGlobalValueIndirectSym(const GlobalValue *GV, void *Ptr, | 
| Bruno Cardoso Lopes | a194c3a | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 142 | JITCodeEmitter &JCE) { | 
| Jeffrey Yasskin | e0d8e14 | 2009-12-15 22:42:46 +0000 | [diff] [blame] | 143 | uint8_t Buffer[4]; | 
|  | 144 | uint8_t *Cur = Buffer; | 
|  | 145 | MachineCodeEmitter::emitWordLEInto(Cur, (intptr_t)Ptr); | 
|  | 146 | void *PtrAddr = JCE.allocIndirectGV( | 
|  | 147 | GV, Buffer, sizeof(Buffer), /*Alignment=*/4); | 
| Evan Cheng | 02771dc | 2008-11-10 23:14:47 +0000 | [diff] [blame] | 148 | addIndirectSymAddr(Ptr, (intptr_t)PtrAddr); | 
|  | 149 | return PtrAddr; | 
| Evan Cheng | ffdd91e | 2008-11-08 01:31:27 +0000 | [diff] [blame] | 150 | } | 
|  | 151 |  | 
| Jeffrey Yasskin | f2ad571 | 2009-11-23 23:35:19 +0000 | [diff] [blame] | 152 | TargetJITInfo::StubLayout ARMJITInfo::getStubLayout() { | 
|  | 153 | // The stub contains up to 3 4-byte instructions, aligned at 4 bytes, and a | 
|  | 154 | // 4-byte address.  See emitFunctionStub for details. | 
|  | 155 | StubLayout Result = {16, 4}; | 
|  | 156 | return Result; | 
|  | 157 | } | 
|  | 158 |  | 
| Nicolas Geoffray | a7557df | 2008-04-16 20:46:05 +0000 | [diff] [blame] | 159 | void *ARMJITInfo::emitFunctionStub(const Function* F, void *Fn, | 
| Bruno Cardoso Lopes | a194c3a | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 160 | JITCodeEmitter &JCE) { | 
| Jeffrey Yasskin | f2ad571 | 2009-11-23 23:35:19 +0000 | [diff] [blame] | 161 | void *Addr; | 
| Evan Cheng | 9546a5c | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 162 | // If this is just a call to an external function, emit a branch instead of a | 
|  | 163 | // call.  The code is the same except for one bit of the last instruction. | 
|  | 164 | if (Fn != (void*)(intptr_t)ARMCompilationCallback) { | 
| Evan Cheng | ffdd91e | 2008-11-08 01:31:27 +0000 | [diff] [blame] | 165 | // Branch to the corresponding function addr. | 
| Evan Cheng | 02771dc | 2008-11-10 23:14:47 +0000 | [diff] [blame] | 166 | if (IsPIC) { | 
| Jeffrey Yasskin | f2ad571 | 2009-11-23 23:35:19 +0000 | [diff] [blame] | 167 | // The stub is 16-byte size and 4-aligned. | 
| Evan Cheng | 02771dc | 2008-11-10 23:14:47 +0000 | [diff] [blame] | 168 | intptr_t LazyPtr = getIndirectSymAddr(Fn); | 
|  | 169 | if (!LazyPtr) { | 
|  | 170 | // In PIC mode, the function stub is loading a lazy-ptr. | 
| Roman Divacky | ad06cee | 2012-09-05 22:26:57 +0000 | [diff] [blame] | 171 | LazyPtr= (intptr_t)emitGlobalValueIndirectSym((const GlobalValue*)F, Fn, JCE); | 
| Daniel Dunbar | 0dd5e1e | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 172 | DEBUG(if (F) | 
| Jim Grosbach | f24f9d9 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 173 | errs() << "JIT: Indirect symbol emitted at [" << LazyPtr | 
| Daniel Dunbar | 0dd5e1e | 2009-07-25 00:23:56 +0000 | [diff] [blame] | 174 | << "] for GV '" << F->getName() << "'\n"; | 
|  | 175 | else | 
|  | 176 | errs() << "JIT: Stub emitted at [" << LazyPtr | 
|  | 177 | << "] for external function at '" << Fn << "'\n"); | 
| Evan Cheng | 02771dc | 2008-11-10 23:14:47 +0000 | [diff] [blame] | 178 | } | 
| Jeffrey Yasskin | f2ad571 | 2009-11-23 23:35:19 +0000 | [diff] [blame] | 179 | JCE.emitAlignment(4); | 
|  | 180 | Addr = (void*)JCE.getCurrentPCValue(); | 
|  | 181 | if (!sys::Memory::setRangeWritable(Addr, 16)) { | 
| Evan Cheng | 4029b85 | 2009-09-09 01:56:29 +0000 | [diff] [blame] | 182 | llvm_unreachable("ERROR: Unable to mark stub writable"); | 
|  | 183 | } | 
| Eric Christopher | a6380af | 2009-11-20 00:21:55 +0000 | [diff] [blame] | 184 | JCE.emitWordLE(0xe59fc004);            // ldr ip, [pc, #+4] | 
| Bruno Cardoso Lopes | a194c3a | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 185 | JCE.emitWordLE(0xe08fc00c);            // L_func$scv: add ip, pc, ip | 
|  | 186 | JCE.emitWordLE(0xe59cf000);            // ldr pc, [ip] | 
| Jeffrey Yasskin | f2ad571 | 2009-11-23 23:35:19 +0000 | [diff] [blame] | 187 | JCE.emitWordLE(LazyPtr - (intptr_t(Addr)+4+8));  // func - (L_func$scv+8) | 
|  | 188 | sys::Memory::InvalidateInstructionCache(Addr, 16); | 
|  | 189 | if (!sys::Memory::setRangeExecutable(Addr, 16)) { | 
| Evan Cheng | 4029b85 | 2009-09-09 01:56:29 +0000 | [diff] [blame] | 190 | llvm_unreachable("ERROR: Unable to mark stub executable"); | 
|  | 191 | } | 
| Evan Cheng | 02771dc | 2008-11-10 23:14:47 +0000 | [diff] [blame] | 192 | } else { | 
|  | 193 | // The stub is 8-byte size and 4-aligned. | 
| Jeffrey Yasskin | f2ad571 | 2009-11-23 23:35:19 +0000 | [diff] [blame] | 194 | JCE.emitAlignment(4); | 
|  | 195 | Addr = (void*)JCE.getCurrentPCValue(); | 
|  | 196 | if (!sys::Memory::setRangeWritable(Addr, 8)) { | 
| Evan Cheng | 4029b85 | 2009-09-09 01:56:29 +0000 | [diff] [blame] | 197 | llvm_unreachable("ERROR: Unable to mark stub writable"); | 
|  | 198 | } | 
| Bruno Cardoso Lopes | a194c3a | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 199 | JCE.emitWordLE(0xe51ff004);    // ldr pc, [pc, #-4] | 
|  | 200 | JCE.emitWordLE((intptr_t)Fn);  // addr of function | 
| Jeffrey Yasskin | f2ad571 | 2009-11-23 23:35:19 +0000 | [diff] [blame] | 201 | sys::Memory::InvalidateInstructionCache(Addr, 8); | 
|  | 202 | if (!sys::Memory::setRangeExecutable(Addr, 8)) { | 
| Evan Cheng | 4029b85 | 2009-09-09 01:56:29 +0000 | [diff] [blame] | 203 | llvm_unreachable("ERROR: Unable to mark stub executable"); | 
|  | 204 | } | 
| Evan Cheng | 02771dc | 2008-11-10 23:14:47 +0000 | [diff] [blame] | 205 | } | 
| Evan Cheng | 9546a5c | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 206 | } else { | 
| Jim Grosbach | 9396051 | 2008-10-20 21:39:23 +0000 | [diff] [blame] | 207 | // The compilation callback will overwrite the first two words of this | 
| Jim Grosbach | f24f9d9 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 208 | // stub with indirect branch instructions targeting the compiled code. | 
| Jim Grosbach | 9396051 | 2008-10-20 21:39:23 +0000 | [diff] [blame] | 209 | // This stub sets the return address to restart the stub, so that | 
|  | 210 | // the new branch will be invoked when we come back. | 
|  | 211 | // | 
| Evan Cheng | ffdd91e | 2008-11-08 01:31:27 +0000 | [diff] [blame] | 212 | // Branch and link to the compilation callback. | 
|  | 213 | // The stub is 16-byte size and 4-byte aligned. | 
| Jeffrey Yasskin | f2ad571 | 2009-11-23 23:35:19 +0000 | [diff] [blame] | 214 | JCE.emitAlignment(4); | 
|  | 215 | Addr = (void*)JCE.getCurrentPCValue(); | 
|  | 216 | if (!sys::Memory::setRangeWritable(Addr, 16)) { | 
| Evan Cheng | 4029b85 | 2009-09-09 01:56:29 +0000 | [diff] [blame] | 217 | llvm_unreachable("ERROR: Unable to mark stub writable"); | 
|  | 218 | } | 
| Jim Grosbach | 9396051 | 2008-10-20 21:39:23 +0000 | [diff] [blame] | 219 | // Save LR so the callback can determine which stub called it. | 
|  | 220 | // The compilation callback is responsible for popping this prior | 
|  | 221 | // to returning. | 
| Bruno Cardoso Lopes | a194c3a | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 222 | JCE.emitWordLE(0xe92d4000); // push {lr} | 
| Evan Cheng | 436bdcd | 2008-11-08 08:16:49 +0000 | [diff] [blame] | 223 | // Set the return address to go back to the start of this stub. | 
| Bruno Cardoso Lopes | a194c3a | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 224 | JCE.emitWordLE(0xe24fe00c); // sub lr, pc, #12 | 
| Evan Cheng | 436bdcd | 2008-11-08 08:16:49 +0000 | [diff] [blame] | 225 | // Invoke the compilation callback. | 
| Bruno Cardoso Lopes | a194c3a | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 226 | JCE.emitWordLE(0xe51ff004); // ldr pc, [pc, #-4] | 
| Evan Cheng | 436bdcd | 2008-11-08 08:16:49 +0000 | [diff] [blame] | 227 | // The address of the compilation callback. | 
| Bruno Cardoso Lopes | a194c3a | 2009-05-30 20:51:52 +0000 | [diff] [blame] | 228 | JCE.emitWordLE((intptr_t)ARMCompilationCallback); | 
| Jeffrey Yasskin | f2ad571 | 2009-11-23 23:35:19 +0000 | [diff] [blame] | 229 | sys::Memory::InvalidateInstructionCache(Addr, 16); | 
|  | 230 | if (!sys::Memory::setRangeExecutable(Addr, 16)) { | 
| Evan Cheng | 4029b85 | 2009-09-09 01:56:29 +0000 | [diff] [blame] | 231 | llvm_unreachable("ERROR: Unable to mark stub executable"); | 
|  | 232 | } | 
| Evan Cheng | 9546a5c | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 233 | } | 
| Evan Cheng | 9546a5c | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 234 |  | 
| Jeffrey Yasskin | f2ad571 | 2009-11-23 23:35:19 +0000 | [diff] [blame] | 235 | return Addr; | 
| Evan Cheng | 9546a5c | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 236 | } | 
|  | 237 |  | 
| Evan Cheng | 7095cd2 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 238 | intptr_t ARMJITInfo::resolveRelocDestAddr(MachineRelocation *MR) const { | 
| Evan Cheng | 6dd08b6 | 2008-11-04 00:50:32 +0000 | [diff] [blame] | 239 | ARM::RelocationType RT = (ARM::RelocationType)MR->getRelocationType(); | 
| Evan Cheng | bfcee5b | 2008-11-12 01:02:24 +0000 | [diff] [blame] | 240 | switch (RT) { | 
|  | 241 | default: | 
|  | 242 | return (intptr_t)(MR->getResultPointer()); | 
|  | 243 | case ARM::reloc_arm_pic_jt: | 
| Evan Cheng | 8467e24 | 2008-11-07 22:30:53 +0000 | [diff] [blame] | 244 | // Destination address - jump table base. | 
|  | 245 | return (intptr_t)(MR->getResultPointer()) - MR->getConstantVal(); | 
| Evan Cheng | bfcee5b | 2008-11-12 01:02:24 +0000 | [diff] [blame] | 246 | case ARM::reloc_arm_jt_base: | 
| Evan Cheng | 8467e24 | 2008-11-07 22:30:53 +0000 | [diff] [blame] | 247 | // Jump table base address. | 
| Evan Cheng | 7095cd2 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 248 | return getJumpTableBaseAddr(MR->getJumpTableIndex()); | 
| Evan Cheng | bfcee5b | 2008-11-12 01:02:24 +0000 | [diff] [blame] | 249 | case ARM::reloc_arm_cp_entry: | 
|  | 250 | case ARM::reloc_arm_vfp_cp_entry: | 
| Evan Cheng | 8467e24 | 2008-11-07 22:30:53 +0000 | [diff] [blame] | 251 | // Constant pool entry address. | 
| Evan Cheng | 6dd08b6 | 2008-11-04 00:50:32 +0000 | [diff] [blame] | 252 | return getConstantPoolEntryAddr(MR->getConstantPoolIndex()); | 
| Evan Cheng | bfcee5b | 2008-11-12 01:02:24 +0000 | [diff] [blame] | 253 | case ARM::reloc_arm_machine_cp_entry: { | 
| Evan Cheng | ef4d78b | 2008-11-07 22:57:53 +0000 | [diff] [blame] | 254 | ARMConstantPoolValue *ACPV = (ARMConstantPoolValue*)MR->getConstantVal(); | 
| Evan Cheng | 6dd08b6 | 2008-11-04 00:50:32 +0000 | [diff] [blame] | 255 | assert((!ACPV->hasModifier() && !ACPV->mustAddCurrentAddress()) && | 
|  | 256 | "Can't handle this machine constant pool entry yet!"); | 
|  | 257 | intptr_t Addr = (intptr_t)(MR->getResultPointer()); | 
|  | 258 | Addr -= getPCLabelAddr(ACPV->getLabelId()) + ACPV->getPCAdjustment(); | 
|  | 259 | return Addr; | 
|  | 260 | } | 
| Evan Cheng | bfcee5b | 2008-11-12 01:02:24 +0000 | [diff] [blame] | 261 | } | 
| Evan Cheng | 6dd08b6 | 2008-11-04 00:50:32 +0000 | [diff] [blame] | 262 | } | 
|  | 263 |  | 
| Evan Cheng | 9546a5c | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 264 | /// relocate - Before the JIT can run a block of code that has been emitted, | 
|  | 265 | /// it must rewrite the code to contain the actual addresses of any | 
|  | 266 | /// referenced global symbols. | 
|  | 267 | void ARMJITInfo::relocate(void *Function, MachineRelocation *MR, | 
|  | 268 | unsigned NumRelocs, unsigned char* GOTBase) { | 
| Evan Cheng | f7c6eff | 2007-08-07 01:37:15 +0000 | [diff] [blame] | 269 | for (unsigned i = 0; i != NumRelocs; ++i, ++MR) { | 
|  | 270 | void *RelocPos = (char*)Function + MR->getMachineCodeOffset(); | 
| Evan Cheng | 7095cd2 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 271 | intptr_t ResultPtr = resolveRelocDestAddr(MR); | 
| Evan Cheng | f7c6eff | 2007-08-07 01:37:15 +0000 | [diff] [blame] | 272 | switch ((ARM::RelocationType)MR->getRelocationType()) { | 
| Evan Cheng | 19d64ba | 2008-10-29 23:55:43 +0000 | [diff] [blame] | 273 | case ARM::reloc_arm_cp_entry: | 
| Evan Cheng | bfcee5b | 2008-11-12 01:02:24 +0000 | [diff] [blame] | 274 | case ARM::reloc_arm_vfp_cp_entry: | 
| Evan Cheng | f7c6eff | 2007-08-07 01:37:15 +0000 | [diff] [blame] | 275 | case ARM::reloc_arm_relative: { | 
| Raul Herbster | ae1b924 | 2007-08-30 23:21:27 +0000 | [diff] [blame] | 276 | // It is necessary to calculate the correct PC relative value. We | 
|  | 277 | // subtract the base addr from the target addr to form a byte offset. | 
| Evan Cheng | bfcee5b | 2008-11-12 01:02:24 +0000 | [diff] [blame] | 278 | ResultPtr = ResultPtr - (intptr_t)RelocPos - 8; | 
| Raul Herbster | ae1b924 | 2007-08-30 23:21:27 +0000 | [diff] [blame] | 279 | // If the result is positive, set bit U(23) to 1. | 
|  | 280 | if (ResultPtr >= 0) | 
| Evan Cheng | bfcee5b | 2008-11-12 01:02:24 +0000 | [diff] [blame] | 281 | *((intptr_t*)RelocPos) |= 1 << ARMII::U_BitShift; | 
| Raul Herbster | ae1b924 | 2007-08-30 23:21:27 +0000 | [diff] [blame] | 282 | else { | 
| Evan Cheng | bfcee5b | 2008-11-12 01:02:24 +0000 | [diff] [blame] | 283 | // Otherwise, obtain the absolute value and set bit U(23) to 0. | 
| Evan Cheng | 287a25d | 2008-11-12 21:37:59 +0000 | [diff] [blame] | 284 | *((intptr_t*)RelocPos) &= ~(1 << ARMII::U_BitShift); | 
| Evan Cheng | bfcee5b | 2008-11-12 01:02:24 +0000 | [diff] [blame] | 285 | ResultPtr = - ResultPtr; | 
| Raul Herbster | ae1b924 | 2007-08-30 23:21:27 +0000 | [diff] [blame] | 286 | } | 
| Evan Cheng | 6dd08b6 | 2008-11-04 00:50:32 +0000 | [diff] [blame] | 287 | // Set the immed value calculated. | 
| Evan Cheng | bfcee5b | 2008-11-12 01:02:24 +0000 | [diff] [blame] | 288 | // VFP immediate offset is multiplied by 4. | 
|  | 289 | if (MR->getRelocationType() == ARM::reloc_arm_vfp_cp_entry) | 
|  | 290 | ResultPtr = ResultPtr >> 2; | 
|  | 291 | *((intptr_t*)RelocPos) |= ResultPtr; | 
| Eric Christopher | 6ac277c | 2012-08-09 22:10:21 +0000 | [diff] [blame] | 292 | // Set register Rn to PC (which is register 15 on all architectures). | 
|  | 293 | // FIXME: This avoids the need for register info in the JIT class. | 
|  | 294 | *((intptr_t*)RelocPos) |= 15 << ARMII::RegRnShift; | 
| Evan Cheng | f7c6eff | 2007-08-07 01:37:15 +0000 | [diff] [blame] | 295 | break; | 
|  | 296 | } | 
| Evan Cheng | 8467e24 | 2008-11-07 22:30:53 +0000 | [diff] [blame] | 297 | case ARM::reloc_arm_pic_jt: | 
| Evan Cheng | 6dd08b6 | 2008-11-04 00:50:32 +0000 | [diff] [blame] | 298 | case ARM::reloc_arm_machine_cp_entry: | 
| Evan Cheng | 19d64ba | 2008-10-29 23:55:43 +0000 | [diff] [blame] | 299 | case ARM::reloc_arm_absolute: { | 
| Evan Cheng | 6dd08b6 | 2008-11-04 00:50:32 +0000 | [diff] [blame] | 300 | // These addresses have already been resolved. | 
| Evan Cheng | bfcee5b | 2008-11-12 01:02:24 +0000 | [diff] [blame] | 301 | *((intptr_t*)RelocPos) |= (intptr_t)ResultPtr; | 
| Evan Cheng | 19d64ba | 2008-10-29 23:55:43 +0000 | [diff] [blame] | 302 | break; | 
|  | 303 | } | 
| Evan Cheng | f7c6eff | 2007-08-07 01:37:15 +0000 | [diff] [blame] | 304 | case ARM::reloc_arm_branch: { | 
| Raul Herbster | ae1b924 | 2007-08-30 23:21:27 +0000 | [diff] [blame] | 305 | // It is necessary to calculate the correct value of signed_immed_24 | 
|  | 306 | // field. We subtract the base addr from the target addr to form a | 
|  | 307 | // byte offset, which must be inside the range -33554432 and +33554428. | 
|  | 308 | // Then, we set the signed_immed_24 field of the instruction to bits | 
|  | 309 | // [25:2] of the byte offset. More details ARM-ARM p. A4-11. | 
| Evan Cheng | 7095cd2 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 310 | ResultPtr = ResultPtr - (intptr_t)RelocPos - 8; | 
| Raul Herbster | ae1b924 | 2007-08-30 23:21:27 +0000 | [diff] [blame] | 311 | ResultPtr = (ResultPtr & 0x03FFFFFC) >> 2; | 
|  | 312 | assert(ResultPtr >= -33554432 && ResultPtr <= 33554428); | 
| Evan Cheng | bfcee5b | 2008-11-12 01:02:24 +0000 | [diff] [blame] | 313 | *((intptr_t*)RelocPos) |= ResultPtr; | 
| Evan Cheng | f7c6eff | 2007-08-07 01:37:15 +0000 | [diff] [blame] | 314 | break; | 
|  | 315 | } | 
| Evan Cheng | 7095cd2 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 316 | case ARM::reloc_arm_jt_base: { | 
|  | 317 | // JT base - (instruction addr + 8) | 
|  | 318 | ResultPtr = ResultPtr - (intptr_t)RelocPos - 8; | 
| Evan Cheng | bfcee5b | 2008-11-12 01:02:24 +0000 | [diff] [blame] | 319 | *((intptr_t*)RelocPos) |= ResultPtr; | 
| Evan Cheng | 7095cd2 | 2008-11-07 09:06:08 +0000 | [diff] [blame] | 320 | break; | 
|  | 321 | } | 
| Zonr Chang | 2da5aa1 | 2010-05-25 08:42:45 +0000 | [diff] [blame] | 322 | case ARM::reloc_arm_movw: { | 
|  | 323 | ResultPtr = ResultPtr & 0xFFFF; | 
|  | 324 | *((intptr_t*)RelocPos) |= ResultPtr & 0xFFF; | 
|  | 325 | *((intptr_t*)RelocPos) |= ((ResultPtr >> 12) & 0xF) << 16; | 
|  | 326 | break; | 
|  | 327 | } | 
|  | 328 | case ARM::reloc_arm_movt: { | 
|  | 329 | ResultPtr = (ResultPtr >> 16) & 0xFFFF; | 
|  | 330 | *((intptr_t*)RelocPos) |= ResultPtr & 0xFFF; | 
|  | 331 | *((intptr_t*)RelocPos) |= ((ResultPtr >> 12) & 0xF) << 16; | 
|  | 332 | break; | 
|  | 333 | } | 
| Evan Cheng | f7c6eff | 2007-08-07 01:37:15 +0000 | [diff] [blame] | 334 | } | 
|  | 335 | } | 
| Evan Cheng | 9546a5c | 2007-07-05 21:15:40 +0000 | [diff] [blame] | 336 | } |