blob: f67bd8cc59fb9ad6ae2715c713c9799be9ef164b [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"
26#include "llvm/System/Memory.h"
Evan Cheng148b6a42007-07-05 21:15:40 +000027#include <cstdlib>
28using namespace llvm;
29
30void ARMJITInfo::replaceMachineCodeForFunction(void *Old, void *New) {
Torok Edwinab7c09b2009-07-08 18:01:40 +000031 llvm_report_error("ARMJITInfo::replaceMachineCodeForFunction");
Evan Cheng148b6a42007-07-05 21:15:40 +000032}
33
34/// JITCompilerFunction - This contains the address of the JIT function used to
35/// compile a function lazily.
36static TargetJITInfo::JITCompilerFn JITCompilerFunction;
37
Evan Cheng95ce1172008-09-02 07:49:03 +000038// Get the ASMPREFIX for the current host. This is often '_'.
39#ifndef __USER_LABEL_PREFIX__
40#define __USER_LABEL_PREFIX__
41#endif
42#define GETASMPREFIX2(X) #X
43#define GETASMPREFIX(X) GETASMPREFIX2(X)
44#define ASMPREFIX GETASMPREFIX(__USER_LABEL_PREFIX__)
45
Evan Cheng148b6a42007-07-05 21:15:40 +000046// CompilationCallback stub - We can't use a C function with inline assembly in
Jim Grosbach932a32d2008-10-20 21:39:23 +000047// it, because we the prolog/epilog inserted by GCC won't work for us (we need
48// to preserve more context and manipulate the stack directly). Instead,
49// write our own wrapper, which does things our way, so we have complete
50// control over register saving and restoring.
Evan Cheng148b6a42007-07-05 21:15:40 +000051extern "C" {
52#if defined(__arm__)
53 void ARMCompilationCallback(void);
54 asm(
55 ".text\n"
56 ".align 2\n"
Evan Cheng95ce1172008-09-02 07:49:03 +000057 ".globl " ASMPREFIX "ARMCompilationCallback\n"
58 ASMPREFIX "ARMCompilationCallback:\n"
Jim Grosbach932a32d2008-10-20 21:39:23 +000059 // Save caller saved registers since they may contain stuff
60 // for the real target function right now. We have to act as if this
61 // whole compilation callback doesn't exist as far as the caller is
62 // concerned, so we can't just preserve the callee saved regs.
Jim Grosbacha9ab95b2008-10-21 16:54:12 +000063 "stmdb sp!, {r0, r1, r2, r3, lr}\n"
Evan Chengbdfc5822009-01-16 02:16:37 +000064#ifndef __SOFTFP__
Evan Cheng28f31292008-11-13 23:28:54 +000065 "fstmfdd sp!, {d0, d1, d2, d3, d4, d5, d6, d7}\n"
66#endif
Jim Grosbach932a32d2008-10-20 21:39:23 +000067 // The LR contains the address of the stub function on entry.
68 // pass it as the argument to the C part of the callback
69 "mov r0, lr\n"
70 "sub sp, sp, #4\n"
71 // Call the C portion of the callback
72 "bl " ASMPREFIX "ARMCompilationCallbackC\n"
73 "add sp, sp, #4\n"
74 // Restoring the LR to the return address of the function that invoked
75 // the stub and de-allocating the stack space for it requires us to
76 // swap the two saved LR values on the stack, as they're backwards
77 // for what we need since the pop instruction has a pre-determined
78 // order for the registers.
79 // +--------+
80 // 0 | LR | Original return address
81 // +--------+
82 // 1 | LR | Stub address (start of stub)
83 // 2-5 | R3..R0 | Saved registers (we need to preserve all regs)
Evan Cheng28f31292008-11-13 23:28:54 +000084 // 6-20 | D0..D7 | Saved VFP registers
Jim Grosbach932a32d2008-10-20 21:39:23 +000085 // +--------+
86 //
Evan Chengbdfc5822009-01-16 02:16:37 +000087#ifndef __SOFTFP__
Evan Cheng28f31292008-11-13 23:28:54 +000088 // Restore VFP caller-saved registers.
89 "fldmfdd sp!, {d0, d1, d2, d3, d4, d5, d6, d7}\n"
90#endif
91 //
Jim Grosbach932a32d2008-10-20 21:39:23 +000092 // We need to exchange the values in slots 0 and 1 so we can
93 // return to the address in slot 1 with the address in slot 0
94 // restored to the LR.
95 "ldr r0, [sp,#20]\n"
96 "ldr r1, [sp,#16]\n"
97 "str r1, [sp,#20]\n"
98 "str r0, [sp,#16]\n"
99 // Return to the (newly modified) stub to invoke the real function.
100 // The above twiddling of the saved return addresses allows us to
101 // deallocate everything, including the LR the stub saved, all in one
102 // pop instruction.
Jim Grosbacha9ab95b2008-10-21 16:54:12 +0000103 "ldmia sp!, {r0, r1, r2, r3, lr, pc}\n"
Evan Cheng95ce1172008-09-02 07:49:03 +0000104 );
105#else // Not an ARM host
Evan Cheng148b6a42007-07-05 21:15:40 +0000106 void ARMCompilationCallback() {
Torok Edwinc23197a2009-07-14 16:55:14 +0000107 llvm_unreachable("Cannot call ARMCompilationCallback() on a non-ARM arch!");
Evan Cheng148b6a42007-07-05 21:15:40 +0000108 }
109#endif
110}
111
Jim Grosbach932a32d2008-10-20 21:39:23 +0000112/// 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
115/// it into the JIT compiler function.
116extern "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 Cheng148b6a42007-07-05 21:15:40 +0000119
120 // Rewrite the call target... so that we don't end up here every time we
Jim Grosbach932a32d2008-10-20 21:39:23 +0000121 // execute the call. We're replacing the first two instructions of the
122 // stub with:
123 // ldr pc, [pc,#-4]
124 // <addr>
Evan Chengae166412008-11-08 08:16:49 +0000125 if (!sys::Memory::setRangeWritable((void*)StubAddr, 8)) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000126 llvm_unreachable("ERROR: Unable to mark stub writable");
Evan Chengae166412008-11-08 08:16:49 +0000127 }
128 *(intptr_t *)StubAddr = 0xe51ff004; // ldr pc, [pc, #-4]
Jim Grosbach932a32d2008-10-20 21:39:23 +0000129 *(intptr_t *)(StubAddr+4) = NewVal;
Evan Chengae166412008-11-08 08:16:49 +0000130 if (!sys::Memory::setRangeExecutable((void*)StubAddr, 8)) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000131 llvm_unreachable("ERROR: Unable to mark stub executable");
Evan Chengae166412008-11-08 08:16:49 +0000132 }
Evan Cheng148b6a42007-07-05 21:15:40 +0000133}
134
135TargetJITInfo::LazyResolverFn
136ARMJITInfo::getLazyResolverFunction(JITCompilerFn F) {
137 JITCompilerFunction = F;
138 return ARMCompilationCallback;
139}
140
Evan Cheng9ed2f802008-11-10 01:08:07 +0000141void *ARMJITInfo::emitGlobalValueIndirectSym(const GlobalValue *GV, void *Ptr,
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000142 JITCodeEmitter &JCE) {
143 JCE.startGVStub(GV, 4, 4);
144 JCE.emitWordLE((intptr_t)Ptr);
145 void *PtrAddr = JCE.finishGVStub(GV);
Evan Cheng588920b2008-11-10 23:14:47 +0000146 addIndirectSymAddr(Ptr, (intptr_t)PtrAddr);
147 return PtrAddr;
Evan Chenge96a4902008-11-08 01:31:27 +0000148}
149
Nicolas Geoffray51cc3c12008-04-16 20:46:05 +0000150void *ARMJITInfo::emitFunctionStub(const Function* F, void *Fn,
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000151 JITCodeEmitter &JCE) {
Evan Cheng148b6a42007-07-05 21:15:40 +0000152 // If this is just a call to an external function, emit a branch instead of a
153 // call. The code is the same except for one bit of the last instruction.
154 if (Fn != (void*)(intptr_t)ARMCompilationCallback) {
Evan Chenge96a4902008-11-08 01:31:27 +0000155 // Branch to the corresponding function addr.
Evan Cheng588920b2008-11-10 23:14:47 +0000156 if (IsPIC) {
157 // The stub is 8-byte size and 4-aligned.
158 intptr_t LazyPtr = getIndirectSymAddr(Fn);
159 if (!LazyPtr) {
160 // In PIC mode, the function stub is loading a lazy-ptr.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000161 LazyPtr= (intptr_t)emitGlobalValueIndirectSym((GlobalValue*)F, Fn, JCE);
Evan Cheng588920b2008-11-10 23:14:47 +0000162 if (F)
163 DOUT << "JIT: Indirect symbol emitted at [" << LazyPtr << "] for GV '"
164 << F->getName() << "'\n";
165 else
166 DOUT << "JIT: Stub emitted at [" << LazyPtr
167 << "] for external function at '" << Fn << "'\n";
168 }
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000169 JCE.startGVStub(F, 16, 4);
170 intptr_t Addr = (intptr_t)JCE.getCurrentPCValue();
171 JCE.emitWordLE(0xe59fc004); // ldr pc, [pc, #+4]
172 JCE.emitWordLE(0xe08fc00c); // L_func$scv: add ip, pc, ip
173 JCE.emitWordLE(0xe59cf000); // ldr pc, [ip]
174 JCE.emitWordLE(LazyPtr - (Addr+4+8)); // func - (L_func$scv+8)
Evan Cheng588920b2008-11-10 23:14:47 +0000175 sys::Memory::InvalidateInstructionCache((void*)Addr, 16);
176 } else {
177 // The stub is 8-byte size and 4-aligned.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000178 JCE.startGVStub(F, 8, 4);
179 intptr_t Addr = (intptr_t)JCE.getCurrentPCValue();
180 JCE.emitWordLE(0xe51ff004); // ldr pc, [pc, #-4]
181 JCE.emitWordLE((intptr_t)Fn); // addr of function
Evan Cheng588920b2008-11-10 23:14:47 +0000182 sys::Memory::InvalidateInstructionCache((void*)Addr, 8);
183 }
Evan Cheng148b6a42007-07-05 21:15:40 +0000184 } else {
Jim Grosbach932a32d2008-10-20 21:39:23 +0000185 // The compilation callback will overwrite the first two words of this
186 // stub with indirect branch instructions targeting the compiled code.
187 // This stub sets the return address to restart the stub, so that
188 // the new branch will be invoked when we come back.
189 //
Evan Chenge96a4902008-11-08 01:31:27 +0000190 // Branch and link to the compilation callback.
191 // The stub is 16-byte size and 4-byte aligned.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000192 JCE.startGVStub(F, 16, 4);
193 intptr_t Addr = (intptr_t)JCE.getCurrentPCValue();
Jim Grosbach932a32d2008-10-20 21:39:23 +0000194 // Save LR so the callback can determine which stub called it.
195 // The compilation callback is responsible for popping this prior
196 // to returning.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000197 JCE.emitWordLE(0xe92d4000); // push {lr}
Evan Chengae166412008-11-08 08:16:49 +0000198 // Set the return address to go back to the start of this stub.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000199 JCE.emitWordLE(0xe24fe00c); // sub lr, pc, #12
Evan Chengae166412008-11-08 08:16:49 +0000200 // Invoke the compilation callback.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000201 JCE.emitWordLE(0xe51ff004); // ldr pc, [pc, #-4]
Evan Chengae166412008-11-08 08:16:49 +0000202 // The address of the compilation callback.
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000203 JCE.emitWordLE((intptr_t)ARMCompilationCallback);
Evan Chengae166412008-11-08 08:16:49 +0000204 sys::Memory::InvalidateInstructionCache((void*)Addr, 16);
Evan Cheng148b6a42007-07-05 21:15:40 +0000205 }
Evan Cheng148b6a42007-07-05 21:15:40 +0000206
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000207 return JCE.finishGVStub(F);
Evan Cheng148b6a42007-07-05 21:15:40 +0000208}
209
Evan Cheng4df60f52008-11-07 09:06:08 +0000210intptr_t ARMJITInfo::resolveRelocDestAddr(MachineRelocation *MR) const {
Evan Cheng25e04782008-11-04 00:50:32 +0000211 ARM::RelocationType RT = (ARM::RelocationType)MR->getRelocationType();
Evan Cheng580c0df2008-11-12 01:02:24 +0000212 switch (RT) {
213 default:
214 return (intptr_t)(MR->getResultPointer());
215 case ARM::reloc_arm_pic_jt:
Evan Cheng437c1732008-11-07 22:30:53 +0000216 // Destination address - jump table base.
217 return (intptr_t)(MR->getResultPointer()) - MR->getConstantVal();
Evan Cheng580c0df2008-11-12 01:02:24 +0000218 case ARM::reloc_arm_jt_base:
Evan Cheng437c1732008-11-07 22:30:53 +0000219 // Jump table base address.
Evan Cheng4df60f52008-11-07 09:06:08 +0000220 return getJumpTableBaseAddr(MR->getJumpTableIndex());
Evan Cheng580c0df2008-11-12 01:02:24 +0000221 case ARM::reloc_arm_cp_entry:
222 case ARM::reloc_arm_vfp_cp_entry:
Evan Cheng437c1732008-11-07 22:30:53 +0000223 // Constant pool entry address.
Evan Cheng25e04782008-11-04 00:50:32 +0000224 return getConstantPoolEntryAddr(MR->getConstantPoolIndex());
Evan Cheng580c0df2008-11-12 01:02:24 +0000225 case ARM::reloc_arm_machine_cp_entry: {
Evan Cheng413a89f2008-11-07 22:57:53 +0000226 ARMConstantPoolValue *ACPV = (ARMConstantPoolValue*)MR->getConstantVal();
Evan Cheng25e04782008-11-04 00:50:32 +0000227 assert((!ACPV->hasModifier() && !ACPV->mustAddCurrentAddress()) &&
228 "Can't handle this machine constant pool entry yet!");
229 intptr_t Addr = (intptr_t)(MR->getResultPointer());
230 Addr -= getPCLabelAddr(ACPV->getLabelId()) + ACPV->getPCAdjustment();
231 return Addr;
232 }
Evan Cheng580c0df2008-11-12 01:02:24 +0000233 }
Evan Cheng25e04782008-11-04 00:50:32 +0000234}
235
Evan Cheng148b6a42007-07-05 21:15:40 +0000236/// relocate - Before the JIT can run a block of code that has been emitted,
237/// it must rewrite the code to contain the actual addresses of any
238/// referenced global symbols.
239void ARMJITInfo::relocate(void *Function, MachineRelocation *MR,
240 unsigned NumRelocs, unsigned char* GOTBase) {
Evan Cheng0ff94f72007-08-07 01:37:15 +0000241 for (unsigned i = 0; i != NumRelocs; ++i, ++MR) {
242 void *RelocPos = (char*)Function + MR->getMachineCodeOffset();
Evan Cheng4df60f52008-11-07 09:06:08 +0000243 intptr_t ResultPtr = resolveRelocDestAddr(MR);
Evan Cheng0ff94f72007-08-07 01:37:15 +0000244 switch ((ARM::RelocationType)MR->getRelocationType()) {
Evan Cheng0f282432008-10-29 23:55:43 +0000245 case ARM::reloc_arm_cp_entry:
Evan Cheng580c0df2008-11-12 01:02:24 +0000246 case ARM::reloc_arm_vfp_cp_entry:
Evan Cheng0ff94f72007-08-07 01:37:15 +0000247 case ARM::reloc_arm_relative: {
Raul Herbsterd05c04c2007-08-30 23:21:27 +0000248 // It is necessary to calculate the correct PC relative value. We
249 // subtract the base addr from the target addr to form a byte offset.
Evan Cheng580c0df2008-11-12 01:02:24 +0000250 ResultPtr = ResultPtr - (intptr_t)RelocPos - 8;
Raul Herbsterd05c04c2007-08-30 23:21:27 +0000251 // If the result is positive, set bit U(23) to 1.
252 if (ResultPtr >= 0)
Evan Cheng580c0df2008-11-12 01:02:24 +0000253 *((intptr_t*)RelocPos) |= 1 << ARMII::U_BitShift;
Raul Herbsterd05c04c2007-08-30 23:21:27 +0000254 else {
Evan Cheng580c0df2008-11-12 01:02:24 +0000255 // Otherwise, obtain the absolute value and set bit U(23) to 0.
Evan Chengcb579822008-11-12 21:37:59 +0000256 *((intptr_t*)RelocPos) &= ~(1 << ARMII::U_BitShift);
Evan Cheng580c0df2008-11-12 01:02:24 +0000257 ResultPtr = - ResultPtr;
Raul Herbsterd05c04c2007-08-30 23:21:27 +0000258 }
Evan Cheng25e04782008-11-04 00:50:32 +0000259 // Set the immed value calculated.
Evan Cheng580c0df2008-11-12 01:02:24 +0000260 // VFP immediate offset is multiplied by 4.
261 if (MR->getRelocationType() == ARM::reloc_arm_vfp_cp_entry)
262 ResultPtr = ResultPtr >> 2;
263 *((intptr_t*)RelocPos) |= ResultPtr;
Evan Cheng25e04782008-11-04 00:50:32 +0000264 // Set register Rn to PC.
Evan Cheng580c0df2008-11-12 01:02:24 +0000265 *((intptr_t*)RelocPos) |=
266 ARMRegisterInfo::getRegisterNumbering(ARM::PC) << ARMII::RegRnShift;
Evan Cheng0ff94f72007-08-07 01:37:15 +0000267 break;
268 }
Evan Cheng437c1732008-11-07 22:30:53 +0000269 case ARM::reloc_arm_pic_jt:
Evan Cheng25e04782008-11-04 00:50:32 +0000270 case ARM::reloc_arm_machine_cp_entry:
Evan Cheng0f282432008-10-29 23:55:43 +0000271 case ARM::reloc_arm_absolute: {
Evan Cheng25e04782008-11-04 00:50:32 +0000272 // These addresses have already been resolved.
Evan Cheng580c0df2008-11-12 01:02:24 +0000273 *((intptr_t*)RelocPos) |= (intptr_t)ResultPtr;
Evan Cheng0f282432008-10-29 23:55:43 +0000274 break;
275 }
Evan Cheng0ff94f72007-08-07 01:37:15 +0000276 case ARM::reloc_arm_branch: {
Raul Herbsterd05c04c2007-08-30 23:21:27 +0000277 // It is necessary to calculate the correct value of signed_immed_24
278 // field. We subtract the base addr from the target addr to form a
279 // byte offset, which must be inside the range -33554432 and +33554428.
280 // Then, we set the signed_immed_24 field of the instruction to bits
281 // [25:2] of the byte offset. More details ARM-ARM p. A4-11.
Evan Cheng4df60f52008-11-07 09:06:08 +0000282 ResultPtr = ResultPtr - (intptr_t)RelocPos - 8;
Raul Herbsterd05c04c2007-08-30 23:21:27 +0000283 ResultPtr = (ResultPtr & 0x03FFFFFC) >> 2;
284 assert(ResultPtr >= -33554432 && ResultPtr <= 33554428);
Evan Cheng580c0df2008-11-12 01:02:24 +0000285 *((intptr_t*)RelocPos) |= ResultPtr;
Evan Cheng0ff94f72007-08-07 01:37:15 +0000286 break;
287 }
Evan Cheng4df60f52008-11-07 09:06:08 +0000288 case ARM::reloc_arm_jt_base: {
289 // JT base - (instruction addr + 8)
290 ResultPtr = ResultPtr - (intptr_t)RelocPos - 8;
Evan Cheng580c0df2008-11-12 01:02:24 +0000291 *((intptr_t*)RelocPos) |= ResultPtr;
Evan Cheng4df60f52008-11-07 09:06:08 +0000292 break;
293 }
Evan Cheng0ff94f72007-08-07 01:37:15 +0000294 }
295 }
Evan Cheng148b6a42007-07-05 21:15:40 +0000296}