1 //===-- ARMJITInfo.cpp - Implement the JIT interfaces for the ARM target --===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the JIT interfaces for the ARM target.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "jit"
15 #include "ARMJITInfo.h"
16 #include "ARMInstrInfo.h"
17 #include "ARMConstantPoolValue.h"
18 #include "ARMRelocations.h"
19 #include "ARMSubtarget.h"
20 #include "llvm/Function.h"
21 #include "llvm/CodeGen/MachineCodeEmitter.h"
22 #include "llvm/Config/alloca.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/Streams.h"
25 #include "llvm/System/Memory.h"
29 void ARMJITInfo::replaceMachineCodeForFunction(void *Old
, void *New
) {
33 /// JITCompilerFunction - This contains the address of the JIT function used to
34 /// compile a function lazily.
35 static TargetJITInfo::JITCompilerFn JITCompilerFunction
;
37 // Get the ASMPREFIX for the current host. This is often '_'.
38 #ifndef __USER_LABEL_PREFIX__
39 #define __USER_LABEL_PREFIX__
41 #define GETASMPREFIX2(X) #X
42 #define GETASMPREFIX(X) GETASMPREFIX2(X)
43 #define ASMPREFIX GETASMPREFIX(__USER_LABEL_PREFIX__)
45 // CompilationCallback stub - We can't use a C function with inline assembly in
46 // it, because we the prolog/epilog inserted by GCC won't work for us (we need
47 // to preserve more context and manipulate the stack directly). Instead,
48 // write our own wrapper, which does things our way, so we have complete
49 // control over register saving and restoring.
52 void ARMCompilationCallback(void);
56 ".globl " ASMPREFIX
"ARMCompilationCallback\n"
57 ASMPREFIX
"ARMCompilationCallback:\n"
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.
62 "stmdb sp!, {r0, r1, r2, r3, lr}\n"
64 "fstmfdd sp!, {d0, d1, d2, d3, d4, d5, d6, d7}\n"
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
70 // Call the C portion of the callback
71 "bl " ASMPREFIX
"ARMCompilationCallbackC\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.
79 // 0 | LR | Original return address
81 // 1 | LR | Stub address (start of stub)
82 // 2-5 | R3..R0 | Saved registers (we need to preserve all regs)
83 // 6-20 | D0..D7 | Saved VFP registers
87 // Restore VFP caller-saved registers.
88 "fldmfdd sp!, {d0, d1, d2, d3, d4, d5, d6, d7}\n"
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.
98 // Return to the (newly modified) stub to invoke the real function.
99 // The above twiddling of the saved return addresses allows us to
100 // deallocate everything, including the LR the stub saved, all in one
102 "ldmia sp!, {r0, r1, r2, r3, lr, pc}\n"
104 #else // Not an ARM host
105 void ARMCompilationCallback() {
106 assert(0 && "Cannot call ARMCompilationCallback() on a non-ARM arch!\n");
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
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
);
120 // Rewrite the call target... so that we don't end up here every time we
121 // execute the call. We're replacing the first two instructions of the
125 if (!sys::Memory::setRangeWritable((void*)StubAddr
, 8)) {
126 cerr
<< "ERROR: Unable to mark stub writable\n";
129 *(intptr_t *)StubAddr
= 0xe51ff004; // ldr pc, [pc, #-4]
130 *(intptr_t *)(StubAddr
+4) = NewVal
;
131 if (!sys::Memory::setRangeExecutable((void*)StubAddr
, 8)) {
132 cerr
<< "ERROR: Unable to mark stub executable\n";
137 TargetJITInfo::LazyResolverFn
138 ARMJITInfo::getLazyResolverFunction(JITCompilerFn F
) {
139 JITCompilerFunction
= F
;
140 return ARMCompilationCallback
;
143 void *ARMJITInfo::emitGlobalValueIndirectSym(const GlobalValue
*GV
, void *Ptr
,
144 MachineCodeEmitter
&MCE
) {
145 MCE
.startGVStub(GV
, 4, 4);
146 MCE
.emitWordLE((intptr_t)Ptr
);
147 void *PtrAddr
= MCE
.finishGVStub(GV
);
148 addIndirectSymAddr(Ptr
, (intptr_t)PtrAddr
);
152 void *ARMJITInfo::emitFunctionStub(const Function
* F
, void *Fn
,
153 MachineCodeEmitter
&MCE
) {
154 // If this is just a call to an external function, emit a branch instead of a
155 // call. The code is the same except for one bit of the last instruction.
156 if (Fn
!= (void*)(intptr_t)ARMCompilationCallback
) {
157 // Branch to the corresponding function addr.
159 // The stub is 8-byte size and 4-aligned.
160 intptr_t LazyPtr
= getIndirectSymAddr(Fn
);
162 // In PIC mode, the function stub is loading a lazy-ptr.
163 LazyPtr
= (intptr_t)emitGlobalValueIndirectSym((GlobalValue
*)F
, Fn
, MCE
);
165 DOUT
<< "JIT: Indirect symbol emitted at [" << LazyPtr
<< "] for GV '"
166 << F
->getName() << "'\n";
168 DOUT
<< "JIT: Stub emitted at [" << LazyPtr
169 << "] for external function at '" << Fn
<< "'\n";
171 MCE
.startGVStub(F
, 16, 4);
172 intptr_t Addr
= (intptr_t)MCE
.getCurrentPCValue();
173 MCE
.emitWordLE(0xe59fc004); // ldr pc, [pc, #+4]
174 MCE
.emitWordLE(0xe08fc00c); // L_func$scv: add ip, pc, ip
175 MCE
.emitWordLE(0xe59cf000); // ldr pc, [ip]
176 MCE
.emitWordLE(LazyPtr
- (Addr
+4+8)); // func - (L_func$scv+8)
177 sys::Memory::InvalidateInstructionCache((void*)Addr
, 16);
179 // The stub is 8-byte size and 4-aligned.
180 MCE
.startGVStub(F
, 8, 4);
181 intptr_t Addr
= (intptr_t)MCE
.getCurrentPCValue();
182 MCE
.emitWordLE(0xe51ff004); // ldr pc, [pc, #-4]
183 MCE
.emitWordLE((intptr_t)Fn
); // addr of function
184 sys::Memory::InvalidateInstructionCache((void*)Addr
, 8);
187 // The compilation callback will overwrite the first two words of this
188 // stub with indirect branch instructions targeting the compiled code.
189 // This stub sets the return address to restart the stub, so that
190 // the new branch will be invoked when we come back.
192 // Branch and link to the compilation callback.
193 // The stub is 16-byte size and 4-byte aligned.
194 MCE
.startGVStub(F
, 16, 4);
195 intptr_t Addr
= (intptr_t)MCE
.getCurrentPCValue();
196 // Save LR so the callback can determine which stub called it.
197 // The compilation callback is responsible for popping this prior
199 MCE
.emitWordLE(0xe92d4000); // push {lr}
200 // Set the return address to go back to the start of this stub.
201 MCE
.emitWordLE(0xe24fe00c); // sub lr, pc, #12
202 // Invoke the compilation callback.
203 MCE
.emitWordLE(0xe51ff004); // ldr pc, [pc, #-4]
204 // The address of the compilation callback.
205 MCE
.emitWordLE((intptr_t)ARMCompilationCallback
);
206 sys::Memory::InvalidateInstructionCache((void*)Addr
, 16);
209 return MCE
.finishGVStub(F
);
212 intptr_t ARMJITInfo::resolveRelocDestAddr(MachineRelocation
*MR
) const {
213 ARM::RelocationType RT
= (ARM::RelocationType
)MR
->getRelocationType();
216 return (intptr_t)(MR
->getResultPointer());
217 case ARM::reloc_arm_pic_jt
:
218 // Destination address - jump table base.
219 return (intptr_t)(MR
->getResultPointer()) - MR
->getConstantVal();
220 case ARM::reloc_arm_jt_base
:
221 // Jump table base address.
222 return getJumpTableBaseAddr(MR
->getJumpTableIndex());
223 case ARM::reloc_arm_cp_entry
:
224 case ARM::reloc_arm_vfp_cp_entry
:
225 // Constant pool entry address.
226 return getConstantPoolEntryAddr(MR
->getConstantPoolIndex());
227 case ARM::reloc_arm_machine_cp_entry
: {
228 ARMConstantPoolValue
*ACPV
= (ARMConstantPoolValue
*)MR
->getConstantVal();
229 assert((!ACPV
->hasModifier() && !ACPV
->mustAddCurrentAddress()) &&
230 "Can't handle this machine constant pool entry yet!");
231 intptr_t Addr
= (intptr_t)(MR
->getResultPointer());
232 Addr
-= getPCLabelAddr(ACPV
->getLabelId()) + ACPV
->getPCAdjustment();
238 /// relocate - Before the JIT can run a block of code that has been emitted,
239 /// it must rewrite the code to contain the actual addresses of any
240 /// referenced global symbols.
241 void ARMJITInfo::relocate(void *Function
, MachineRelocation
*MR
,
242 unsigned NumRelocs
, unsigned char* GOTBase
) {
243 for (unsigned i
= 0; i
!= NumRelocs
; ++i
, ++MR
) {
244 void *RelocPos
= (char*)Function
+ MR
->getMachineCodeOffset();
245 intptr_t ResultPtr
= resolveRelocDestAddr(MR
);
246 switch ((ARM::RelocationType
)MR
->getRelocationType()) {
247 case ARM::reloc_arm_cp_entry
:
248 case ARM::reloc_arm_vfp_cp_entry
:
249 case ARM::reloc_arm_relative
: {
250 // It is necessary to calculate the correct PC relative value. We
251 // subtract the base addr from the target addr to form a byte offset.
252 ResultPtr
= ResultPtr
- (intptr_t)RelocPos
- 8;
253 // If the result is positive, set bit U(23) to 1.
255 *((intptr_t*)RelocPos
) |= 1 << ARMII::U_BitShift
;
257 // Otherwise, obtain the absolute value and set bit U(23) to 0.
258 *((intptr_t*)RelocPos
) &= ~(1 << ARMII::U_BitShift
);
259 ResultPtr
= - ResultPtr
;
261 // Set the immed value calculated.
262 // VFP immediate offset is multiplied by 4.
263 if (MR
->getRelocationType() == ARM::reloc_arm_vfp_cp_entry
)
264 ResultPtr
= ResultPtr
>> 2;
265 *((intptr_t*)RelocPos
) |= ResultPtr
;
266 // Set register Rn to PC.
267 *((intptr_t*)RelocPos
) |=
268 ARMRegisterInfo::getRegisterNumbering(ARM::PC
) << ARMII::RegRnShift
;
271 case ARM::reloc_arm_pic_jt
:
272 case ARM::reloc_arm_machine_cp_entry
:
273 case ARM::reloc_arm_absolute
: {
274 // These addresses have already been resolved.
275 *((intptr_t*)RelocPos
) |= (intptr_t)ResultPtr
;
278 case ARM::reloc_arm_branch
: {
279 // It is necessary to calculate the correct value of signed_immed_24
280 // field. We subtract the base addr from the target addr to form a
281 // byte offset, which must be inside the range -33554432 and +33554428.
282 // Then, we set the signed_immed_24 field of the instruction to bits
283 // [25:2] of the byte offset. More details ARM-ARM p. A4-11.
284 ResultPtr
= ResultPtr
- (intptr_t)RelocPos
- 8;
285 ResultPtr
= (ResultPtr
& 0x03FFFFFC) >> 2;
286 assert(ResultPtr
>= -33554432 && ResultPtr
<= 33554428);
287 *((intptr_t*)RelocPos
) |= ResultPtr
;
290 case ARM::reloc_arm_jt_base
: {
291 // JT base - (instruction addr + 8)
292 ResultPtr
= ResultPtr
- (intptr_t)RelocPos
- 8;
293 *((intptr_t*)RelocPos
) |= ResultPtr
;