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 "ARMRelocations.h"
17 #include "ARMSubtarget.h"
18 #include "llvm/Function.h"
19 #include "llvm/CodeGen/MachineCodeEmitter.h"
20 #include "llvm/Config/alloca.h"
21 #include "llvm/Support/Streams.h"
22 #include "llvm/System/Memory.h"
26 void ARMJITInfo::replaceMachineCodeForFunction(void *Old, void *New) {
30 /// JITCompilerFunction - This contains the address of the JIT function used to
31 /// compile a function lazily.
32 static TargetJITInfo::JITCompilerFn JITCompilerFunction;
34 // Get the ASMPREFIX for the current host. This is often '_'.
35 #ifndef __USER_LABEL_PREFIX__
36 #define __USER_LABEL_PREFIX__
38 #define GETASMPREFIX2(X) #X
39 #define GETASMPREFIX(X) GETASMPREFIX2(X)
40 #define ASMPREFIX GETASMPREFIX(__USER_LABEL_PREFIX__)
42 // CompilationCallback stub - We can't use a C function with inline assembly in
43 // it, because we the prolog/epilog inserted by GCC won't work for us (we need
44 // to preserve more context and manipulate the stack directly). Instead,
45 // write our own wrapper, which does things our way, so we have complete
46 // control over register saving and restoring.
49 void ARMCompilationCallback(void);
53 ".globl " ASMPREFIX "ARMCompilationCallback\n"
54 ASMPREFIX "ARMCompilationCallback:\n"
55 // Save caller saved registers since they may contain stuff
56 // for the real target function right now. We have to act as if this
57 // whole compilation callback doesn't exist as far as the caller is
58 // concerned, so we can't just preserve the callee saved regs.
59 "push {r0, r1, r2, r3, lr}\n"
60 // The LR contains the address of the stub function on entry.
61 // pass it as the argument to the C part of the callback
64 // Call the C portion of the callback
65 "bl " ASMPREFIX "ARMCompilationCallbackC\n"
67 // Restoring the LR to the return address of the function that invoked
68 // the stub and de-allocating the stack space for it requires us to
69 // swap the two saved LR values on the stack, as they're backwards
70 // for what we need since the pop instruction has a pre-determined
71 // order for the registers.
73 // 0 | LR | Original return address
75 // 1 | LR | Stub address (start of stub)
76 // 2-5 | R3..R0 | Saved registers (we need to preserve all regs)
79 // We need to exchange the values in slots 0 and 1 so we can
80 // return to the address in slot 1 with the address in slot 0
81 // restored to the LR.
86 // Return to the (newly modified) stub to invoke the real function.
87 // The above twiddling of the saved return addresses allows us to
88 // deallocate everything, including the LR the stub saved, all in one
90 "pop {r0, r1, r2, r3, lr, pc}\n"
92 #else // Not an ARM host
93 void ARMCompilationCallback() {
94 assert(0 && "Cannot call ARMCompilationCallback() on a non-ARM arch!\n");
100 /// ARMCompilationCallbackC - This is the target-specific function invoked
101 /// by the function stub when we did not know the real target of a call.
102 /// This function must locate the start of the stub or call site and pass
103 /// it into the JIT compiler function.
104 extern "C" void ARMCompilationCallbackC(intptr_t StubAddr) {
105 // Get the address of the compiled code for this function.
106 intptr_t NewVal = (intptr_t)JITCompilerFunction((void*)StubAddr);
108 // Rewrite the call target... so that we don't end up here every time we
109 // execute the call. We're replacing the first two instructions of the
113 #if defined(__APPLE__)
114 bool ok = sys::Memory::setRangeWritable ((void*)StubAddr, 8);
117 cerr << "ERROR: Unable to mark stub writable\n";
121 *(intptr_t *)StubAddr = 0xe51ff004;
122 *(intptr_t *)(StubAddr+4) = NewVal;
123 #if defined(__APPLE__)
124 ok = sys::Memory::setRangeExecutable ((void*)StubAddr, 8);
127 cerr << "ERROR: Unable to mark stub executable\n";
133 TargetJITInfo::LazyResolverFn
134 ARMJITInfo::getLazyResolverFunction(JITCompilerFn F) {
135 JITCompilerFunction = F;
136 return ARMCompilationCallback;
139 void *ARMJITInfo::emitFunctionStub(const Function* F, void *Fn,
140 MachineCodeEmitter &MCE) {
141 unsigned addr = (intptr_t)Fn;
142 // If this is just a call to an external function, emit a branch instead of a
143 // call. The code is the same except for one bit of the last instruction.
144 if (Fn != (void*)(intptr_t)ARMCompilationCallback) {
145 // branch to the corresponding function addr
146 // the stub is 8-byte size and 4-aligned
147 MCE.startFunctionStub(F, 8, 4);
148 MCE.emitWordLE(0xe51ff004); // LDR PC, [PC,#-4]
149 MCE.emitWordLE(addr); // addr of function
151 // The compilation callback will overwrite the first two words of this
152 // stub with indirect branch instructions targeting the compiled code.
153 // This stub sets the return address to restart the stub, so that
154 // the new branch will be invoked when we come back.
156 // branch and link to the compilation callback.
157 // the stub is 16-byte size and 4-byte aligned.
158 MCE.startFunctionStub(F, 16, 4);
159 // Save LR so the callback can determine which stub called it.
160 // The compilation callback is responsible for popping this prior
162 MCE.emitWordLE(0xe92d4000); // PUSH {lr}
163 // Set the return address to go back to the start of this stub
164 MCE.emitWordLE(0xe24fe00c); // SUB LR, PC, #12
165 // Invoke the compilation callback
166 MCE.emitWordLE(0xe51ff004); // LDR PC, [PC,#-4]
167 // The address of the compilation callback
168 MCE.emitWordLE((intptr_t)ARMCompilationCallback);
171 return MCE.finishFunctionStub(F);
174 /// relocate - Before the JIT can run a block of code that has been emitted,
175 /// it must rewrite the code to contain the actual addresses of any
176 /// referenced global symbols.
177 void ARMJITInfo::relocate(void *Function, MachineRelocation *MR,
178 unsigned NumRelocs, unsigned char* GOTBase) {
179 for (unsigned i = 0; i != NumRelocs; ++i, ++MR) {
180 void *RelocPos = (char*)Function + MR->getMachineCodeOffset();
181 intptr_t ResultPtr = (intptr_t)MR->getResultPointer();
182 switch ((ARM::RelocationType)MR->getRelocationType()) {
183 case ARM::reloc_arm_relative: {
184 // It is necessary to calculate the correct PC relative value. We
185 // subtract the base addr from the target addr to form a byte offset.
186 ResultPtr = ResultPtr-(intptr_t)RelocPos-8;
187 // If the result is positive, set bit U(23) to 1.
189 *((unsigned*)RelocPos) |= 1 << 23;
191 // otherwise, obtain the absolute value and set
194 *((unsigned*)RelocPos) &= 0xFF7FFFFF;
196 // set the immed value calculated
197 *((unsigned*)RelocPos) |= (unsigned)ResultPtr;
198 // set register Rn to PC
199 *((unsigned*)RelocPos) |= 0xF << 16;
202 case ARM::reloc_arm_branch: {
203 // It is necessary to calculate the correct value of signed_immed_24
204 // field. We subtract the base addr from the target addr to form a
205 // byte offset, which must be inside the range -33554432 and +33554428.
206 // Then, we set the signed_immed_24 field of the instruction to bits
207 // [25:2] of the byte offset. More details ARM-ARM p. A4-11.
208 ResultPtr = ResultPtr-(intptr_t)RelocPos-8;
209 ResultPtr = (ResultPtr & 0x03FFFFFC) >> 2;
210 assert(ResultPtr >= -33554432 && ResultPtr <= 33554428);
211 *((unsigned*)RelocPos) |= ResultPtr;