blob: c93e28be23b399ba0781026e3aa1c737d185ac26 [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 Cheng25e04782008-11-04 00:50:32 +000016#include "ARMConstantPoolValue.h"
Evan Cheng148b6a42007-07-05 21:15:40 +000017#include "ARMRelocations.h"
18#include "ARMSubtarget.h"
Nicolas Geoffray51cc3c12008-04-16 20:46:05 +000019#include "llvm/Function.h"
Evan Cheng148b6a42007-07-05 21:15:40 +000020#include "llvm/CodeGen/MachineCodeEmitter.h"
21#include "llvm/Config/alloca.h"
Evan Cheng588920b2008-11-10 23:14:47 +000022#include "llvm/Support/Debug.h"
Jim Grosbach932a32d2008-10-20 21:39:23 +000023#include "llvm/Support/Streams.h"
24#include "llvm/System/Memory.h"
Evan Cheng148b6a42007-07-05 21:15:40 +000025#include <cstdlib>
26using namespace llvm;
27
28void ARMJITInfo::replaceMachineCodeForFunction(void *Old, void *New) {
Raul Herbsterd05c04c2007-08-30 23:21:27 +000029 abort();
Evan Cheng148b6a42007-07-05 21:15:40 +000030}
31
32/// JITCompilerFunction - This contains the address of the JIT function used to
33/// compile a function lazily.
34static TargetJITInfo::JITCompilerFn JITCompilerFunction;
35
Evan Cheng95ce1172008-09-02 07:49:03 +000036// Get the ASMPREFIX for the current host. This is often '_'.
37#ifndef __USER_LABEL_PREFIX__
38#define __USER_LABEL_PREFIX__
39#endif
40#define GETASMPREFIX2(X) #X
41#define GETASMPREFIX(X) GETASMPREFIX2(X)
42#define ASMPREFIX GETASMPREFIX(__USER_LABEL_PREFIX__)
43
Evan Cheng148b6a42007-07-05 21:15:40 +000044// CompilationCallback stub - We can't use a C function with inline assembly in
Jim Grosbach932a32d2008-10-20 21:39:23 +000045// it, because we the prolog/epilog inserted by GCC won't work for us (we need
46// to preserve more context and manipulate the stack directly). Instead,
47// write our own wrapper, which does things our way, so we have complete
48// control over register saving and restoring.
Evan Cheng148b6a42007-07-05 21:15:40 +000049extern "C" {
50#if defined(__arm__)
51 void ARMCompilationCallback(void);
52 asm(
53 ".text\n"
54 ".align 2\n"
Evan Cheng95ce1172008-09-02 07:49:03 +000055 ".globl " ASMPREFIX "ARMCompilationCallback\n"
56 ASMPREFIX "ARMCompilationCallback:\n"
Jim Grosbach932a32d2008-10-20 21:39:23 +000057 // Save caller saved registers since they may contain stuff
58 // for the real target function right now. We have to act as if this
59 // whole compilation callback doesn't exist as far as the caller is
60 // concerned, so we can't just preserve the callee saved regs.
Jim Grosbacha9ab95b2008-10-21 16:54:12 +000061 "stmdb sp!, {r0, r1, r2, r3, lr}\n"
Jim Grosbach932a32d2008-10-20 21:39:23 +000062 // The LR contains the address of the stub function on entry.
63 // pass it as the argument to the C part of the callback
64 "mov r0, lr\n"
65 "sub sp, sp, #4\n"
66 // Call the C portion of the callback
67 "bl " ASMPREFIX "ARMCompilationCallbackC\n"
68 "add sp, sp, #4\n"
69 // Restoring the LR to the return address of the function that invoked
70 // the stub and de-allocating the stack space for it requires us to
71 // swap the two saved LR values on the stack, as they're backwards
72 // for what we need since the pop instruction has a pre-determined
73 // order for the registers.
74 // +--------+
75 // 0 | LR | Original return address
76 // +--------+
77 // 1 | LR | Stub address (start of stub)
78 // 2-5 | R3..R0 | Saved registers (we need to preserve all regs)
79 // +--------+
80 //
81 // We need to exchange the values in slots 0 and 1 so we can
82 // return to the address in slot 1 with the address in slot 0
83 // restored to the LR.
84 "ldr r0, [sp,#20]\n"
85 "ldr r1, [sp,#16]\n"
86 "str r1, [sp,#20]\n"
87 "str r0, [sp,#16]\n"
88 // Return to the (newly modified) stub to invoke the real function.
89 // The above twiddling of the saved return addresses allows us to
90 // deallocate everything, including the LR the stub saved, all in one
91 // pop instruction.
Jim Grosbacha9ab95b2008-10-21 16:54:12 +000092 "ldmia sp!, {r0, r1, r2, r3, lr, pc}\n"
Evan Cheng95ce1172008-09-02 07:49:03 +000093 );
94#else // Not an ARM host
Evan Cheng148b6a42007-07-05 21:15:40 +000095 void ARMCompilationCallback() {
96 assert(0 && "Cannot call ARMCompilationCallback() on a non-ARM arch!\n");
97 abort();
98 }
99#endif
100}
101
Jim Grosbach932a32d2008-10-20 21:39:23 +0000102/// ARMCompilationCallbackC - This is the target-specific function invoked
103/// by the function stub when we did not know the real target of a call.
104/// This function must locate the start of the stub or call site and pass
105/// it into the JIT compiler function.
106extern "C" void ARMCompilationCallbackC(intptr_t StubAddr) {
107 // Get the address of the compiled code for this function.
108 intptr_t NewVal = (intptr_t)JITCompilerFunction((void*)StubAddr);
Evan Cheng148b6a42007-07-05 21:15:40 +0000109
110 // Rewrite the call target... so that we don't end up here every time we
Jim Grosbach932a32d2008-10-20 21:39:23 +0000111 // execute the call. We're replacing the first two instructions of the
112 // stub with:
113 // ldr pc, [pc,#-4]
114 // <addr>
Evan Chengae166412008-11-08 08:16:49 +0000115 if (!sys::Memory::setRangeWritable((void*)StubAddr, 8)) {
116 cerr << "ERROR: Unable to mark stub writable\n";
117 abort();
118 }
119 *(intptr_t *)StubAddr = 0xe51ff004; // ldr pc, [pc, #-4]
Jim Grosbach932a32d2008-10-20 21:39:23 +0000120 *(intptr_t *)(StubAddr+4) = NewVal;
Evan Chengae166412008-11-08 08:16:49 +0000121 if (!sys::Memory::setRangeExecutable((void*)StubAddr, 8)) {
122 cerr << "ERROR: Unable to mark stub executable\n";
123 abort();
124 }
Evan Cheng148b6a42007-07-05 21:15:40 +0000125}
126
127TargetJITInfo::LazyResolverFn
128ARMJITInfo::getLazyResolverFunction(JITCompilerFn F) {
129 JITCompilerFunction = F;
130 return ARMCompilationCallback;
131}
132
Evan Cheng9ed2f802008-11-10 01:08:07 +0000133void *ARMJITInfo::emitGlobalValueIndirectSym(const GlobalValue *GV, void *Ptr,
134 MachineCodeEmitter &MCE) {
Evan Chengce4a70b2008-11-08 08:02:53 +0000135 MCE.startGVStub(GV, 4, 4);
Evan Chenge96a4902008-11-08 01:31:27 +0000136 MCE.emitWordLE((intptr_t)Ptr);
Evan Cheng588920b2008-11-10 23:14:47 +0000137 void *PtrAddr = MCE.finishGVStub(GV);
138 addIndirectSymAddr(Ptr, (intptr_t)PtrAddr);
139 return PtrAddr;
Evan Chenge96a4902008-11-08 01:31:27 +0000140}
141
Nicolas Geoffray51cc3c12008-04-16 20:46:05 +0000142void *ARMJITInfo::emitFunctionStub(const Function* F, void *Fn,
143 MachineCodeEmitter &MCE) {
Evan Cheng148b6a42007-07-05 21:15:40 +0000144 // If this is just a call to an external function, emit a branch instead of a
145 // call. The code is the same except for one bit of the last instruction.
146 if (Fn != (void*)(intptr_t)ARMCompilationCallback) {
Evan Chenge96a4902008-11-08 01:31:27 +0000147 // Branch to the corresponding function addr.
Evan Cheng588920b2008-11-10 23:14:47 +0000148 if (IsPIC) {
149 // The stub is 8-byte size and 4-aligned.
150 intptr_t LazyPtr = getIndirectSymAddr(Fn);
151 if (!LazyPtr) {
152 // In PIC mode, the function stub is loading a lazy-ptr.
153 LazyPtr= (intptr_t)emitGlobalValueIndirectSym((GlobalValue*)F, Fn, MCE);
154 if (F)
155 DOUT << "JIT: Indirect symbol emitted at [" << LazyPtr << "] for GV '"
156 << F->getName() << "'\n";
157 else
158 DOUT << "JIT: Stub emitted at [" << LazyPtr
159 << "] for external function at '" << Fn << "'\n";
160 }
161 MCE.startGVStub(F, 16, 4);
162 intptr_t Addr = (intptr_t)MCE.getCurrentPCValue();
163 MCE.emitWordLE(0xe59fc004); // ldr pc, [pc, #+4]
164 MCE.emitWordLE(0xe08fc00c); // L_func$scv: add ip, pc, ip
165 MCE.emitWordLE(0xe59cf000); // ldr pc, [ip]
166 MCE.emitWordLE(LazyPtr - (Addr+4+8)); // func - (L_func$scv+8)
167 sys::Memory::InvalidateInstructionCache((void*)Addr, 16);
168 } else {
169 // The stub is 8-byte size and 4-aligned.
170 MCE.startGVStub(F, 8, 4);
171 intptr_t Addr = (intptr_t)MCE.getCurrentPCValue();
172 MCE.emitWordLE(0xe51ff004); // ldr pc, [pc, #-4]
173 MCE.emitWordLE((intptr_t)Fn); // addr of function
174 sys::Memory::InvalidateInstructionCache((void*)Addr, 8);
175 }
Evan Cheng148b6a42007-07-05 21:15:40 +0000176 } else {
Jim Grosbach932a32d2008-10-20 21:39:23 +0000177 // The compilation callback will overwrite the first two words of this
178 // stub with indirect branch instructions targeting the compiled code.
179 // This stub sets the return address to restart the stub, so that
180 // the new branch will be invoked when we come back.
181 //
Evan Chenge96a4902008-11-08 01:31:27 +0000182 // Branch and link to the compilation callback.
183 // The stub is 16-byte size and 4-byte aligned.
Evan Chengce4a70b2008-11-08 08:02:53 +0000184 MCE.startGVStub(F, 16, 4);
Evan Chengae166412008-11-08 08:16:49 +0000185 intptr_t Addr = (intptr_t)MCE.getCurrentPCValue();
Jim Grosbach932a32d2008-10-20 21:39:23 +0000186 // Save LR so the callback can determine which stub called it.
187 // The compilation callback is responsible for popping this prior
188 // to returning.
Evan Chengae166412008-11-08 08:16:49 +0000189 MCE.emitWordLE(0xe92d4000); // push {lr}
190 // Set the return address to go back to the start of this stub.
191 MCE.emitWordLE(0xe24fe00c); // sub lr, pc, #12
192 // Invoke the compilation callback.
193 MCE.emitWordLE(0xe51ff004); // ldr pc, [pc, #-4]
194 // The address of the compilation callback.
Jim Grosbach932a32d2008-10-20 21:39:23 +0000195 MCE.emitWordLE((intptr_t)ARMCompilationCallback);
Evan Chengae166412008-11-08 08:16:49 +0000196 sys::Memory::InvalidateInstructionCache((void*)Addr, 16);
Evan Cheng148b6a42007-07-05 21:15:40 +0000197 }
Evan Cheng148b6a42007-07-05 21:15:40 +0000198
Evan Chengce4a70b2008-11-08 08:02:53 +0000199 return MCE.finishGVStub(F);
Evan Cheng148b6a42007-07-05 21:15:40 +0000200}
201
Evan Cheng4df60f52008-11-07 09:06:08 +0000202intptr_t ARMJITInfo::resolveRelocDestAddr(MachineRelocation *MR) const {
Evan Cheng25e04782008-11-04 00:50:32 +0000203 ARM::RelocationType RT = (ARM::RelocationType)MR->getRelocationType();
Evan Cheng437c1732008-11-07 22:30:53 +0000204 if (RT == ARM::reloc_arm_pic_jt)
205 // Destination address - jump table base.
206 return (intptr_t)(MR->getResultPointer()) - MR->getConstantVal();
207 else if (RT == ARM::reloc_arm_jt_base)
208 // Jump table base address.
Evan Cheng4df60f52008-11-07 09:06:08 +0000209 return getJumpTableBaseAddr(MR->getJumpTableIndex());
210 else if (RT == ARM::reloc_arm_cp_entry)
Evan Cheng437c1732008-11-07 22:30:53 +0000211 // Constant pool entry address.
Evan Cheng25e04782008-11-04 00:50:32 +0000212 return getConstantPoolEntryAddr(MR->getConstantPoolIndex());
213 else if (RT == ARM::reloc_arm_machine_cp_entry) {
Evan Cheng413a89f2008-11-07 22:57:53 +0000214 ARMConstantPoolValue *ACPV = (ARMConstantPoolValue*)MR->getConstantVal();
Evan Cheng25e04782008-11-04 00:50:32 +0000215 assert((!ACPV->hasModifier() && !ACPV->mustAddCurrentAddress()) &&
216 "Can't handle this machine constant pool entry yet!");
217 intptr_t Addr = (intptr_t)(MR->getResultPointer());
218 Addr -= getPCLabelAddr(ACPV->getLabelId()) + ACPV->getPCAdjustment();
219 return Addr;
220 }
221 return (intptr_t)(MR->getResultPointer());
222}
223
Evan Cheng148b6a42007-07-05 21:15:40 +0000224/// relocate - Before the JIT can run a block of code that has been emitted,
225/// it must rewrite the code to contain the actual addresses of any
226/// referenced global symbols.
227void ARMJITInfo::relocate(void *Function, MachineRelocation *MR,
228 unsigned NumRelocs, unsigned char* GOTBase) {
Evan Cheng0ff94f72007-08-07 01:37:15 +0000229 for (unsigned i = 0; i != NumRelocs; ++i, ++MR) {
230 void *RelocPos = (char*)Function + MR->getMachineCodeOffset();
Evan Cheng4df60f52008-11-07 09:06:08 +0000231 intptr_t ResultPtr = resolveRelocDestAddr(MR);
Evan Cheng0ff94f72007-08-07 01:37:15 +0000232 switch ((ARM::RelocationType)MR->getRelocationType()) {
Evan Cheng0f282432008-10-29 23:55:43 +0000233 case ARM::reloc_arm_cp_entry:
Evan Cheng0ff94f72007-08-07 01:37:15 +0000234 case ARM::reloc_arm_relative: {
Raul Herbsterd05c04c2007-08-30 23:21:27 +0000235 // It is necessary to calculate the correct PC relative value. We
236 // subtract the base addr from the target addr to form a byte offset.
237 ResultPtr = ResultPtr-(intptr_t)RelocPos-8;
238 // If the result is positive, set bit U(23) to 1.
239 if (ResultPtr >= 0)
240 *((unsigned*)RelocPos) |= 1 << 23;
241 else {
Evan Cheng25e04782008-11-04 00:50:32 +0000242 // Otherwise, obtain the absolute value and set
Raul Herbsterd05c04c2007-08-30 23:21:27 +0000243 // bit U(23) to 0.
244 ResultPtr *= -1;
245 *((unsigned*)RelocPos) &= 0xFF7FFFFF;
246 }
Evan Cheng25e04782008-11-04 00:50:32 +0000247 // Set the immed value calculated.
Raul Herbsterd05c04c2007-08-30 23:21:27 +0000248 *((unsigned*)RelocPos) |= (unsigned)ResultPtr;
Evan Cheng25e04782008-11-04 00:50:32 +0000249 // Set register Rn to PC.
Raul Herbsterd05c04c2007-08-30 23:21:27 +0000250 *((unsigned*)RelocPos) |= 0xF << 16;
Evan Cheng0ff94f72007-08-07 01:37:15 +0000251 break;
252 }
Evan Cheng437c1732008-11-07 22:30:53 +0000253 case ARM::reloc_arm_pic_jt:
Evan Cheng25e04782008-11-04 00:50:32 +0000254 case ARM::reloc_arm_machine_cp_entry:
Evan Cheng0f282432008-10-29 23:55:43 +0000255 case ARM::reloc_arm_absolute: {
Evan Cheng25e04782008-11-04 00:50:32 +0000256 // These addresses have already been resolved.
Evan Cheng4df60f52008-11-07 09:06:08 +0000257 *((unsigned*)RelocPos) |= (unsigned)ResultPtr;
Evan Cheng0f282432008-10-29 23:55:43 +0000258 break;
259 }
Evan Cheng0ff94f72007-08-07 01:37:15 +0000260 case ARM::reloc_arm_branch: {
Raul Herbsterd05c04c2007-08-30 23:21:27 +0000261 // It is necessary to calculate the correct value of signed_immed_24
262 // field. We subtract the base addr from the target addr to form a
263 // byte offset, which must be inside the range -33554432 and +33554428.
264 // Then, we set the signed_immed_24 field of the instruction to bits
265 // [25:2] of the byte offset. More details ARM-ARM p. A4-11.
Evan Cheng4df60f52008-11-07 09:06:08 +0000266 ResultPtr = ResultPtr - (intptr_t)RelocPos - 8;
Raul Herbsterd05c04c2007-08-30 23:21:27 +0000267 ResultPtr = (ResultPtr & 0x03FFFFFC) >> 2;
268 assert(ResultPtr >= -33554432 && ResultPtr <= 33554428);
Evan Cheng0ff94f72007-08-07 01:37:15 +0000269 *((unsigned*)RelocPos) |= ResultPtr;
270 break;
271 }
Evan Cheng4df60f52008-11-07 09:06:08 +0000272 case ARM::reloc_arm_jt_base: {
273 // JT base - (instruction addr + 8)
274 ResultPtr = ResultPtr - (intptr_t)RelocPos - 8;
275 *((unsigned*)RelocPos) |= ResultPtr;
276 break;
277 }
Evan Cheng0ff94f72007-08-07 01:37:15 +0000278 }
279 }
Evan Cheng148b6a42007-07-05 21:15:40 +0000280}