blob: 50a798f14c175d0ad3188f3c0e04da093d752bb1 [file] [log] [blame]
Evan Cheng148b6a42007-07-05 21:15:40 +00001//===-- ARMJITInfo.cpp - Implement the JIT interfaces for the ARM target --===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Cheng148b6a42007-07-05 21:15:40 +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"
Evan Cheng580c0df2008-11-12 01:02:24 +000016#include "ARMInstrInfo.h"
Evan Cheng25e04782008-11-04 00:50:32 +000017#include "ARMConstantPoolValue.h"
Evan Cheng148b6a42007-07-05 21:15:40 +000018#include "ARMRelocations.h"
19#include "ARMSubtarget.h"
Nicolas Geoffray51cc3c12008-04-16 20:46:05 +000020#include "llvm/Function.h"
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +000021#include "llvm/CodeGen/JITCodeEmitter.h"
Evan Cheng148b6a42007-07-05 21:15:40 +000022#include "llvm/Config/alloca.h"
Evan Cheng588920b2008-11-10 23:14:47 +000023#include "llvm/Support/Debug.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000024#include "llvm/Support/ErrorHandling.h"
Jim Grosbach932a32d2008-10-20 21:39:23 +000025#include "llvm/Support/Streams.h"
Daniel Dunbarce63ffb2009-07-25 00:23:56 +000026#include "llvm/Support/raw_ostream.h"
Jim Grosbach932a32d2008-10-20 21:39:23 +000027#include "llvm/System/Memory.h"
Evan Cheng148b6a42007-07-05 21:15:40 +000028#include <cstdlib>
29using namespace llvm;
30
31void ARMJITInfo::replaceMachineCodeForFunction(void *Old, void *New) {
Torok Edwinab7c09b2009-07-08 18:01:40 +000032 llvm_report_error("ARMJITInfo::replaceMachineCodeForFunction");
Evan Cheng148b6a42007-07-05 21:15:40 +000033}
34
35/// JITCompilerFunction - This contains the address of the JIT function used to
36/// compile a function lazily.
37static TargetJITInfo::JITCompilerFn JITCompilerFunction;
38
Evan Cheng95ce1172008-09-02 07:49:03 +000039// Get the ASMPREFIX for the current host. This is often '_'.
40#ifndef __USER_LABEL_PREFIX__
41#define __USER_LABEL_PREFIX__
42#endif
43#define GETASMPREFIX2(X) #X
44#define GETASMPREFIX(X) GETASMPREFIX2(X)
45#define ASMPREFIX GETASMPREFIX(__USER_LABEL_PREFIX__)
46
Evan Cheng148b6a42007-07-05 21:15:40 +000047// CompilationCallback stub - We can't use a C function with inline assembly in
Jim Grosbach932a32d2008-10-20 21:39:23 +000048// it, because we the prolog/epilog inserted by GCC won't work for us (we need
49// to preserve more context and manipulate the stack directly). Instead,
50// write our own wrapper, which does things our way, so we have complete
51// control over register saving and restoring.
Evan Cheng148b6a42007-07-05 21:15:40 +000052extern "C" {
53#if defined(__arm__)
54 void ARMCompilationCallback(void);
55 asm(
56 ".text\n"
57 ".align 2\n"
Evan Cheng95ce1172008-09-02 07:49:03 +000058 ".globl " ASMPREFIX "ARMCompilationCallback\n"
59 ASMPREFIX "ARMCompilationCallback:\n"
Jim Grosbach932a32d2008-10-20 21:39:23 +000060 // Save caller saved registers since they may contain stuff
61 // for the real target function right now. We have to act as if this
62 // whole compilation callback doesn't exist as far as the caller is
63 // concerned, so we can't just preserve the callee saved regs.
Jim Grosbacha9ab95b2008-10-21 16:54:12 +000064 "stmdb sp!, {r0, r1, r2, r3, lr}\n"
Evan Chengbdfc5822009-01-16 02:16:37 +000065#ifndef __SOFTFP__
Evan Cheng28f31292008-11-13 23:28:54 +000066 "fstmfdd sp!, {d0, d1, d2, d3, d4, d5, d6, d7}\n"
67#endif
Jim Grosbach932a32d2008-10-20 21:39:23 +000068 // The LR contains the address of the stub function on entry.
69 // pass it as the argument to the C part of the callback
70 "mov r0, lr\n"
71 "sub sp, sp, #4\n"
72 // Call the C portion of the callback
73 "bl " ASMPREFIX "ARMCompilationCallbackC\n"
74 "add sp, sp, #4\n"
75 // Restoring the LR to the return address of the function that invoked
76 // the stub and de-allocating the stack space for it requires us to
77 // swap the two saved LR values on the stack, as they're backwards
78 // for what we need since the pop instruction has a pre-determined
79 // order for the registers.
80 // +--------+
81 // 0 | LR | Original return address
82 // +--------+
83 // 1 | LR | Stub address (start of stub)
84 // 2-5 | R3..R0 | Saved registers (we need to preserve all regs)
Evan Cheng28f31292008-11-13 23:28:54 +000085 // 6-20 | D0..D7 | Saved VFP registers
Jim Grosbach932a32d2008-10-20 21:39:23 +000086 // +--------+
87 //
Evan Chengbdfc5822009-01-16 02:16:37 +000088#ifndef __SOFTFP__
Evan Cheng28f31292008-11-13 23:28:54 +000089 // Restore VFP caller-saved registers.
90 "fldmfdd sp!, {d0, d1, d2, d3, d4, d5, d6, d7}\n"
91#endif
92 //
Jim Grosbach932a32d2008-10-20 21:39:23 +000093 // We need to exchange the values in slots 0 and 1 so we can
94 // return to the address in slot 1 with the address in slot 0
95 // restored to the LR.
96 "ldr r0, [sp,#20]\n"
97 "ldr r1, [sp,#16]\n"
98 "str r1, [sp,#20]\n"
99 "str r0, [sp,#16]\n"
100 // Return to the (newly modified) stub to invoke the real function.
101 // The above twiddling of the saved return addresses allows us to
102 // deallocate everything, including the LR the stub saved, all in one
103 // pop instruction.
Jim Grosbacha9ab95b2008-10-21 16:54:12 +0000104 "ldmia sp!, {r0, r1, r2, r3, lr, pc}\n"
Evan Cheng95ce1172008-09-02 07:49:03 +0000105 );
106#else // Not an ARM host
Evan Cheng148b6a42007-07-05 21:15:40 +0000107 void ARMCompilationCallback() {
Torok Edwinc23197a2009-07-14 16:55:14 +0000108 llvm_unreachable("Cannot call ARMCompilationCallback() on a non-ARM arch!");
Evan Cheng148b6a42007-07-05 21:15:40 +0000109 }
110#endif
111}
112
Jim Grosbach932a32d2008-10-20 21:39:23 +0000113/// ARMCompilationCallbackC - This is the target-specific function invoked
114/// by the function stub when we did not know the real target of a call.
115/// This function must locate the start of the stub or call site and pass
116/// it into the JIT compiler function.
117extern "C" void ARMCompilationCallbackC(intptr_t StubAddr) {
118 // Get the address of the compiled code for this function.
119 intptr_t NewVal = (intptr_t)JITCompilerFunction((void*)StubAddr);
Evan Cheng148b6a42007-07-05 21:15:40 +0000120
121 // Rewrite the call target... so that we don't end up here every time we
Jim Grosbach932a32d2008-10-20 21:39:23 +0000122 // execute the call. We're replacing the first two instructions of the
123 // stub with:
124 // ldr pc, [pc,#-4]
125 // <addr>
Evan Chengae166412008-11-08 08:16:49 +0000126 if (!sys::Memory::setRangeWritable((void*)StubAddr, 8)) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000127 llvm_unreachable("ERROR: Unable to mark stub writable");
Evan Chengae166412008-11-08 08:16:49 +0000128 }
129 *(intptr_t *)StubAddr = 0xe51ff004; // ldr pc, [pc, #-4]
Jim Grosbach932a32d2008-10-20 21:39:23 +0000130 *(intptr_t *)(StubAddr+4) = NewVal;
Evan Chengae166412008-11-08 08:16:49 +0000131 if (!sys::Memory::setRangeExecutable((void*)StubAddr, 8)) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000132 llvm_unreachable("ERROR: Unable to mark stub executable");
Evan Chengae166412008-11-08 08:16:49 +0000133 }
Evan Cheng148b6a42007-07-05 21:15:40 +0000134}
135
136TargetJITInfo::LazyResolverFn
137ARMJITInfo::getLazyResolverFunction(JITCompilerFn F) {
138 JITCompilerFunction = F;
139 return ARMCompilationCallback;
140}
141
Evan Cheng9ed2f802008-11-10 01:08:07 +0000142void *ARMJITInfo::emitGlobalValueIndirectSym(const GlobalValue *GV, void *Ptr,
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000143 JITCodeEmitter &JCE) {
144 JCE.startGVStub(GV, 4, 4);
145 JCE.emitWordLE((intptr_t)Ptr);
146 void *PtrAddr = JCE.finishGVStub(GV);
Evan Cheng588920b2008-11-10 23:14:47 +0000147 addIndirectSymAddr(Ptr, (intptr_t)PtrAddr);
148 return PtrAddr;
Evan Chenge96a4902008-11-08 01:31:27 +0000149}
150
Nicolas Geoffray51cc3c12008-04-16 20:46:05 +0000151void *ARMJITInfo::emitFunctionStub(const Function* F, void *Fn,
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000152 JITCodeEmitter &JCE) {
Evan Cheng148b6a42007-07-05 21:15:40 +0000153 // If this is just a call to an external function, emit a branch instead of a
154 // call. The code is the same except for one bit of the last instruction.
155 if (Fn != (void*)(intptr_t)ARMCompilationCallback) {
Evan Chenge96a4902008-11-08 01:31:27 +0000156 // Branch to the corresponding function addr.
Evan Cheng588920b2008-11-10 23:14:47 +0000157 if (IsPIC) {
158 // The stub is 8-byte size and 4-aligned.
159 intptr_t LazyPtr = getIndirectSymAddr(Fn);
160 if (!LazyPtr) {
161 // In PIC mode, the function stub is loading a lazy-ptr.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000162 LazyPtr= (intptr_t)emitGlobalValueIndirectSym((GlobalValue*)F, Fn, JCE);
Daniel Dunbarce63ffb2009-07-25 00:23:56 +0000163 DEBUG(if (F)
164 errs() << "JIT: Indirect symbol emitted at [" << LazyPtr
165 << "] for GV '" << F->getName() << "'\n";
166 else
167 errs() << "JIT: Stub emitted at [" << LazyPtr
168 << "] for external function at '" << Fn << "'\n");
Evan Cheng588920b2008-11-10 23:14:47 +0000169 }
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000170 JCE.startGVStub(F, 16, 4);
171 intptr_t Addr = (intptr_t)JCE.getCurrentPCValue();
172 JCE.emitWordLE(0xe59fc004); // ldr pc, [pc, #+4]
173 JCE.emitWordLE(0xe08fc00c); // L_func$scv: add ip, pc, ip
174 JCE.emitWordLE(0xe59cf000); // ldr pc, [ip]
175 JCE.emitWordLE(LazyPtr - (Addr+4+8)); // func - (L_func$scv+8)
Evan Cheng588920b2008-11-10 23:14:47 +0000176 sys::Memory::InvalidateInstructionCache((void*)Addr, 16);
177 } else {
178 // The stub is 8-byte size and 4-aligned.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000179 JCE.startGVStub(F, 8, 4);
180 intptr_t Addr = (intptr_t)JCE.getCurrentPCValue();
181 JCE.emitWordLE(0xe51ff004); // ldr pc, [pc, #-4]
182 JCE.emitWordLE((intptr_t)Fn); // addr of function
Evan Cheng588920b2008-11-10 23:14:47 +0000183 sys::Memory::InvalidateInstructionCache((void*)Addr, 8);
184 }
Evan Cheng148b6a42007-07-05 21:15:40 +0000185 } else {
Jim Grosbach932a32d2008-10-20 21:39:23 +0000186 // The compilation callback will overwrite the first two words of this
187 // stub with indirect branch instructions targeting the compiled code.
188 // This stub sets the return address to restart the stub, so that
189 // the new branch will be invoked when we come back.
190 //
Evan Chenge96a4902008-11-08 01:31:27 +0000191 // Branch and link to the compilation callback.
192 // The stub is 16-byte size and 4-byte aligned.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000193 JCE.startGVStub(F, 16, 4);
194 intptr_t Addr = (intptr_t)JCE.getCurrentPCValue();
Jim Grosbach932a32d2008-10-20 21:39:23 +0000195 // Save LR so the callback can determine which stub called it.
196 // The compilation callback is responsible for popping this prior
197 // to returning.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000198 JCE.emitWordLE(0xe92d4000); // push {lr}
Evan Chengae166412008-11-08 08:16:49 +0000199 // Set the return address to go back to the start of this stub.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000200 JCE.emitWordLE(0xe24fe00c); // sub lr, pc, #12
Evan Chengae166412008-11-08 08:16:49 +0000201 // Invoke the compilation callback.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000202 JCE.emitWordLE(0xe51ff004); // ldr pc, [pc, #-4]
Evan Chengae166412008-11-08 08:16:49 +0000203 // The address of the compilation callback.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000204 JCE.emitWordLE((intptr_t)ARMCompilationCallback);
Evan Chengae166412008-11-08 08:16:49 +0000205 sys::Memory::InvalidateInstructionCache((void*)Addr, 16);
Evan Cheng148b6a42007-07-05 21:15:40 +0000206 }
Evan Cheng148b6a42007-07-05 21:15:40 +0000207
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000208 return JCE.finishGVStub(F);
Evan Cheng148b6a42007-07-05 21:15:40 +0000209}
210
Evan Cheng4df60f52008-11-07 09:06:08 +0000211intptr_t ARMJITInfo::resolveRelocDestAddr(MachineRelocation *MR) const {
Evan Cheng25e04782008-11-04 00:50:32 +0000212 ARM::RelocationType RT = (ARM::RelocationType)MR->getRelocationType();
Evan Cheng580c0df2008-11-12 01:02:24 +0000213 switch (RT) {
214 default:
215 return (intptr_t)(MR->getResultPointer());
216 case ARM::reloc_arm_pic_jt:
Evan Cheng437c1732008-11-07 22:30:53 +0000217 // Destination address - jump table base.
218 return (intptr_t)(MR->getResultPointer()) - MR->getConstantVal();
Evan Cheng580c0df2008-11-12 01:02:24 +0000219 case ARM::reloc_arm_jt_base:
Evan Cheng437c1732008-11-07 22:30:53 +0000220 // Jump table base address.
Evan Cheng4df60f52008-11-07 09:06:08 +0000221 return getJumpTableBaseAddr(MR->getJumpTableIndex());
Evan Cheng580c0df2008-11-12 01:02:24 +0000222 case ARM::reloc_arm_cp_entry:
223 case ARM::reloc_arm_vfp_cp_entry:
Evan Cheng437c1732008-11-07 22:30:53 +0000224 // Constant pool entry address.
Evan Cheng25e04782008-11-04 00:50:32 +0000225 return getConstantPoolEntryAddr(MR->getConstantPoolIndex());
Evan Cheng580c0df2008-11-12 01:02:24 +0000226 case ARM::reloc_arm_machine_cp_entry: {
Evan Cheng413a89f2008-11-07 22:57:53 +0000227 ARMConstantPoolValue *ACPV = (ARMConstantPoolValue*)MR->getConstantVal();
Evan Cheng25e04782008-11-04 00:50:32 +0000228 assert((!ACPV->hasModifier() && !ACPV->mustAddCurrentAddress()) &&
229 "Can't handle this machine constant pool entry yet!");
230 intptr_t Addr = (intptr_t)(MR->getResultPointer());
231 Addr -= getPCLabelAddr(ACPV->getLabelId()) + ACPV->getPCAdjustment();
232 return Addr;
233 }
Evan Cheng580c0df2008-11-12 01:02:24 +0000234 }
Evan Cheng25e04782008-11-04 00:50:32 +0000235}
236
Evan Cheng148b6a42007-07-05 21:15:40 +0000237/// relocate - Before the JIT can run a block of code that has been emitted,
238/// it must rewrite the code to contain the actual addresses of any
239/// referenced global symbols.
240void ARMJITInfo::relocate(void *Function, MachineRelocation *MR,
241 unsigned NumRelocs, unsigned char* GOTBase) {
Evan Cheng0ff94f72007-08-07 01:37:15 +0000242 for (unsigned i = 0; i != NumRelocs; ++i, ++MR) {
243 void *RelocPos = (char*)Function + MR->getMachineCodeOffset();
Evan Cheng4df60f52008-11-07 09:06:08 +0000244 intptr_t ResultPtr = resolveRelocDestAddr(MR);
Evan Cheng0ff94f72007-08-07 01:37:15 +0000245 switch ((ARM::RelocationType)MR->getRelocationType()) {
Evan Cheng0f282432008-10-29 23:55:43 +0000246 case ARM::reloc_arm_cp_entry:
Evan Cheng580c0df2008-11-12 01:02:24 +0000247 case ARM::reloc_arm_vfp_cp_entry:
Evan Cheng0ff94f72007-08-07 01:37:15 +0000248 case ARM::reloc_arm_relative: {
Raul Herbsterd05c04c2007-08-30 23:21:27 +0000249 // It is necessary to calculate the correct PC relative value. We
250 // subtract the base addr from the target addr to form a byte offset.
Evan Cheng580c0df2008-11-12 01:02:24 +0000251 ResultPtr = ResultPtr - (intptr_t)RelocPos - 8;
Raul Herbsterd05c04c2007-08-30 23:21:27 +0000252 // If the result is positive, set bit U(23) to 1.
253 if (ResultPtr >= 0)
Evan Cheng580c0df2008-11-12 01:02:24 +0000254 *((intptr_t*)RelocPos) |= 1 << ARMII::U_BitShift;
Raul Herbsterd05c04c2007-08-30 23:21:27 +0000255 else {
Evan Cheng580c0df2008-11-12 01:02:24 +0000256 // Otherwise, obtain the absolute value and set bit U(23) to 0.
Evan Chengcb579822008-11-12 21:37:59 +0000257 *((intptr_t*)RelocPos) &= ~(1 << ARMII::U_BitShift);
Evan Cheng580c0df2008-11-12 01:02:24 +0000258 ResultPtr = - ResultPtr;
Raul Herbsterd05c04c2007-08-30 23:21:27 +0000259 }
Evan Cheng25e04782008-11-04 00:50:32 +0000260 // Set the immed value calculated.
Evan Cheng580c0df2008-11-12 01:02:24 +0000261 // VFP immediate offset is multiplied by 4.
262 if (MR->getRelocationType() == ARM::reloc_arm_vfp_cp_entry)
263 ResultPtr = ResultPtr >> 2;
264 *((intptr_t*)RelocPos) |= ResultPtr;
Evan Cheng25e04782008-11-04 00:50:32 +0000265 // Set register Rn to PC.
Evan Cheng580c0df2008-11-12 01:02:24 +0000266 *((intptr_t*)RelocPos) |=
267 ARMRegisterInfo::getRegisterNumbering(ARM::PC) << ARMII::RegRnShift;
Evan Cheng0ff94f72007-08-07 01:37:15 +0000268 break;
269 }
Evan Cheng437c1732008-11-07 22:30:53 +0000270 case ARM::reloc_arm_pic_jt:
Evan Cheng25e04782008-11-04 00:50:32 +0000271 case ARM::reloc_arm_machine_cp_entry:
Evan Cheng0f282432008-10-29 23:55:43 +0000272 case ARM::reloc_arm_absolute: {
Evan Cheng25e04782008-11-04 00:50:32 +0000273 // These addresses have already been resolved.
Evan Cheng580c0df2008-11-12 01:02:24 +0000274 *((intptr_t*)RelocPos) |= (intptr_t)ResultPtr;
Evan Cheng0f282432008-10-29 23:55:43 +0000275 break;
276 }
Evan Cheng0ff94f72007-08-07 01:37:15 +0000277 case ARM::reloc_arm_branch: {
Raul Herbsterd05c04c2007-08-30 23:21:27 +0000278 // It is necessary to calculate the correct value of signed_immed_24
279 // field. We subtract the base addr from the target addr to form a
280 // byte offset, which must be inside the range -33554432 and +33554428.
281 // Then, we set the signed_immed_24 field of the instruction to bits
282 // [25:2] of the byte offset. More details ARM-ARM p. A4-11.
Evan Cheng4df60f52008-11-07 09:06:08 +0000283 ResultPtr = ResultPtr - (intptr_t)RelocPos - 8;
Raul Herbsterd05c04c2007-08-30 23:21:27 +0000284 ResultPtr = (ResultPtr & 0x03FFFFFC) >> 2;
285 assert(ResultPtr >= -33554432 && ResultPtr <= 33554428);
Evan Cheng580c0df2008-11-12 01:02:24 +0000286 *((intptr_t*)RelocPos) |= ResultPtr;
Evan Cheng0ff94f72007-08-07 01:37:15 +0000287 break;
288 }
Evan Cheng4df60f52008-11-07 09:06:08 +0000289 case ARM::reloc_arm_jt_base: {
290 // JT base - (instruction addr + 8)
291 ResultPtr = ResultPtr - (intptr_t)RelocPos - 8;
Evan Cheng580c0df2008-11-12 01:02:24 +0000292 *((intptr_t*)RelocPos) |= ResultPtr;
Evan Cheng4df60f52008-11-07 09:06:08 +0000293 break;
294 }
Evan Cheng0ff94f72007-08-07 01:37:15 +0000295 }
296 }
Evan Cheng148b6a42007-07-05 21:15:40 +0000297}