LLVM API Documentation

ARMJITInfo.cpp

Go to the documentation of this file.
00001 //===-- ARMJITInfo.cpp - Implement the JIT interfaces for the ARM target --===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the JIT interfaces for the ARM target.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #define DEBUG_TYPE "jit"
00015 #include "ARMJITInfo.h"
00016 #include "ARMInstrInfo.h"
00017 #include "ARMConstantPoolValue.h"
00018 #include "ARMRelocations.h"
00019 #include "ARMSubtarget.h"
00020 #include "llvm/Function.h"
00021 #include "llvm/CodeGen/JITCodeEmitter.h"
00022 #include "llvm/Config/alloca.h"
00023 #include "llvm/Support/Debug.h"
00024 #include "llvm/Support/ErrorHandling.h"
00025 #include "llvm/Support/Streams.h"
00026 #include "llvm/Support/raw_ostream.h"
00027 #include "llvm/System/Memory.h"
00028 #include <cstdlib>
00029 using namespace llvm;
00030 
00031 void ARMJITInfo::replaceMachineCodeForFunction(void *Old, void *New) {
00032   llvm_report_error("ARMJITInfo::replaceMachineCodeForFunction");
00033 }
00034 
00035 /// JITCompilerFunction - This contains the address of the JIT function used to
00036 /// compile a function lazily.
00037 static TargetJITInfo::JITCompilerFn JITCompilerFunction;
00038 
00039 // Get the ASMPREFIX for the current host.  This is often '_'.
00040 #ifndef __USER_LABEL_PREFIX__
00041 #define __USER_LABEL_PREFIX__
00042 #endif
00043 #define GETASMPREFIX2(X) #X
00044 #define GETASMPREFIX(X) GETASMPREFIX2(X)
00045 #define ASMPREFIX GETASMPREFIX(__USER_LABEL_PREFIX__)
00046 
00047 // CompilationCallback stub - We can't use a C function with inline assembly in
00048 // it, because we the prolog/epilog inserted by GCC won't work for us (we need
00049 // to preserve more context and manipulate the stack directly).  Instead,
00050 // write our own wrapper, which does things our way, so we have complete
00051 // control over register saving and restoring.
00052 extern "C" {
00053 #if defined(__arm__)
00054   void ARMCompilationCallback();
00055   asm(
00056     ".text\n"
00057     ".align 2\n"
00058     ".globl " ASMPREFIX "ARMCompilationCallback\n"
00059     ASMPREFIX "ARMCompilationCallback:\n"
00060     // Save caller saved registers since they may contain stuff
00061     // for the real target function right now. We have to act as if this
00062     // whole compilation callback doesn't exist as far as the caller is
00063     // concerned, so we can't just preserve the callee saved regs.
00064     "stmdb sp!, {r0, r1, r2, r3, lr}\n"
00065 #ifndef __SOFTFP__
00066     "fstmfdd sp!, {d0, d1, d2, d3, d4, d5, d6, d7}\n"
00067 #endif
00068     // The LR contains the address of the stub function on entry.
00069     // pass it as the argument to the C part of the callback
00070     "mov  r0, lr\n"
00071     "sub  sp, sp, #4\n"
00072     // Call the C portion of the callback
00073     "bl   " ASMPREFIX "ARMCompilationCallbackC\n"
00074     "add  sp, sp, #4\n"
00075     // Restoring the LR to the return address of the function that invoked
00076     // the stub and de-allocating the stack space for it requires us to
00077     // swap the two saved LR values on the stack, as they're backwards
00078     // for what we need since the pop instruction has a pre-determined
00079     // order for the registers.
00080     //      +--------+
00081     //   0  | LR     | Original return address
00082     //      +--------+
00083     //   1  | LR     | Stub address (start of stub)
00084     // 2-5  | R3..R0 | Saved registers (we need to preserve all regs)
00085     // 6-20 | D0..D7 | Saved VFP registers
00086     //      +--------+
00087     //
00088 #ifndef __SOFTFP__
00089     // Restore VFP caller-saved registers.
00090     "fldmfdd sp!, {d0, d1, d2, d3, d4, d5, d6, d7}\n"
00091 #endif
00092     //
00093     //      We need to exchange the values in slots 0 and 1 so we can
00094     //      return to the address in slot 1 with the address in slot 0
00095     //      restored to the LR.
00096     "ldr  r0, [sp,#20]\n"
00097     "ldr  r1, [sp,#16]\n"
00098     "str  r1, [sp,#20]\n"
00099     "str  r0, [sp,#16]\n"
00100     // Return to the (newly modified) stub to invoke the real function.
00101     // The above twiddling of the saved return addresses allows us to
00102     // deallocate everything, including the LR the stub saved, all in one
00103     // pop instruction.
00104     "ldmia  sp!, {r0, r1, r2, r3, lr, pc}\n"
00105       );
00106 #else  // Not an ARM host
00107   void ARMCompilationCallback() {
00108     llvm_unreachable("Cannot call ARMCompilationCallback() on a non-ARM arch!");
00109   }
00110 #endif
00111 }
00112 
00113 /// ARMCompilationCallbackC - This is the target-specific function invoked
00114 /// by the function stub when we did not know the real target of a call.
00115 /// This function must locate the start of the stub or call site and pass
00116 /// it into the JIT compiler function.
00117 extern "C" void ARMCompilationCallbackC(intptr_t StubAddr) {
00118   // Get the address of the compiled code for this function.
00119   intptr_t NewVal = (intptr_t)JITCompilerFunction((void*)StubAddr);
00120 
00121   // Rewrite the call target... so that we don't end up here every time we
00122   // execute the call. We're replacing the first two instructions of the
00123   // stub with:
00124   //   ldr pc, [pc,#-4]
00125   //   <addr>
00126   if (!sys::Memory::setRangeWritable((void*)StubAddr, 8)) {
00127     llvm_unreachable("ERROR: Unable to mark stub writable");
00128   }
00129   *(intptr_t *)StubAddr = 0xe51ff004;  // ldr pc, [pc, #-4]
00130   *(intptr_t *)(StubAddr+4) = NewVal;
00131   if (!sys::Memory::setRangeExecutable((void*)StubAddr, 8)) {
00132     llvm_unreachable("ERROR: Unable to mark stub executable");
00133   }
00134 }
00135 
00136 TargetJITInfo::LazyResolverFn
00137 ARMJITInfo::getLazyResolverFunction(JITCompilerFn F) {
00138   JITCompilerFunction = F;
00139   return ARMCompilationCallback;
00140 }
00141 
00142 void *ARMJITInfo::emitGlobalValueIndirectSym(const GlobalValue *GV, void *Ptr,
00143                                              JITCodeEmitter &JCE) {
00144   JCE.startGVStub(GV, 4, 4);
00145   intptr_t Addr = (intptr_t)JCE.getCurrentPCValue();
00146   if (!sys::Memory::setRangeWritable((void*)Addr, 4)) {
00147     llvm_unreachable("ERROR: Unable to mark indirect symbol writable");
00148   }
00149   JCE.emitWordLE((intptr_t)Ptr);
00150   if (!sys::Memory::setRangeExecutable((void*)Addr, 4)) {
00151     llvm_unreachable("ERROR: Unable to mark indirect symbol executable");
00152   }
00153   void *PtrAddr = JCE.finishGVStub(GV);
00154   addIndirectSymAddr(Ptr, (intptr_t)PtrAddr);
00155   return PtrAddr;
00156 }
00157 
00158 void *ARMJITInfo::emitFunctionStub(const Function* F, void *Fn,
00159                                    JITCodeEmitter &JCE) {
00160   // If this is just a call to an external function, emit a branch instead of a
00161   // call.  The code is the same except for one bit of the last instruction.
00162   if (Fn != (void*)(intptr_t)ARMCompilationCallback) {
00163     // Branch to the corresponding function addr.
00164     if (IsPIC) {
00165       // The stub is 8-byte size and 4-aligned.
00166       intptr_t LazyPtr = getIndirectSymAddr(Fn);
00167       if (!LazyPtr) {
00168         // In PIC mode, the function stub is loading a lazy-ptr.
00169         LazyPtr= (intptr_t)emitGlobalValueIndirectSym((GlobalValue*)F, Fn, JCE);
00170         DEBUG(if (F)
00171                 errs() << "JIT: Indirect symbol emitted at [" << LazyPtr
00172                        << "] for GV '" << F->getName() << "'\n";
00173               else
00174                 errs() << "JIT: Stub emitted at [" << LazyPtr
00175                        << "] for external function at '" << Fn << "'\n");
00176       }
00177       JCE.startGVStub(F, 16, 4);
00178       intptr_t Addr = (intptr_t)JCE.getCurrentPCValue();
00179       if (!sys::Memory::setRangeWritable((void*)Addr, 16)) {
00180         llvm_unreachable("ERROR: Unable to mark stub writable");
00181       }
00182       JCE.emitWordLE(0xe59fc004);            // ldr pc, [pc, #+4]
00183       JCE.emitWordLE(0xe08fc00c);            // L_func$scv: add ip, pc, ip
00184       JCE.emitWordLE(0xe59cf000);            // ldr pc, [ip]
00185       JCE.emitWordLE(LazyPtr - (Addr+4+8));  // func - (L_func$scv+8)
00186       sys::Memory::InvalidateInstructionCache((void*)Addr, 16);
00187       if (!sys::Memory::setRangeExecutable((void*)Addr, 16)) {
00188         llvm_unreachable("ERROR: Unable to mark stub executable");
00189       }
00190     } else {
00191       // The stub is 8-byte size and 4-aligned.
00192       JCE.startGVStub(F, 8, 4);
00193       intptr_t Addr = (intptr_t)JCE.getCurrentPCValue();
00194       if (!sys::Memory::setRangeWritable((void*)Addr, 8)) {
00195         llvm_unreachable("ERROR: Unable to mark stub writable");
00196       }
00197       JCE.emitWordLE(0xe51ff004);    // ldr pc, [pc, #-4]
00198       JCE.emitWordLE((intptr_t)Fn);  // addr of function
00199       sys::Memory::InvalidateInstructionCache((void*)Addr, 8);
00200       if (!sys::Memory::setRangeExecutable((void*)Addr, 8)) {
00201         llvm_unreachable("ERROR: Unable to mark stub executable");
00202       }
00203     }
00204   } else {
00205     // The compilation callback will overwrite the first two words of this
00206     // stub with indirect branch instructions targeting the compiled code.
00207     // This stub sets the return address to restart the stub, so that
00208     // the new branch will be invoked when we come back.
00209     //
00210     // Branch and link to the compilation callback.
00211     // The stub is 16-byte size and 4-byte aligned.
00212     JCE.startGVStub(F, 16, 4);
00213     intptr_t Addr = (intptr_t)JCE.getCurrentPCValue();
00214     if (!sys::Memory::setRangeWritable((void*)Addr, 16)) {
00215       llvm_unreachable("ERROR: Unable to mark stub writable");
00216     }
00217     // Save LR so the callback can determine which stub called it.
00218     // The compilation callback is responsible for popping this prior
00219     // to returning.
00220     JCE.emitWordLE(0xe92d4000); // push {lr}
00221     // Set the return address to go back to the start of this stub.
00222     JCE.emitWordLE(0xe24fe00c); // sub lr, pc, #12
00223     // Invoke the compilation callback.
00224     JCE.emitWordLE(0xe51ff004); // ldr pc, [pc, #-4]
00225     // The address of the compilation callback.
00226     JCE.emitWordLE((intptr_t)ARMCompilationCallback);
00227     sys::Memory::InvalidateInstructionCache((void*)Addr, 16);
00228     if (!sys::Memory::setRangeExecutable((void*)Addr, 16)) {
00229       llvm_unreachable("ERROR: Unable to mark stub executable");
00230     }
00231   }
00232 
00233   return JCE.finishGVStub(F);
00234 }
00235 
00236 intptr_t ARMJITInfo::resolveRelocDestAddr(MachineRelocation *MR) const {
00237   ARM::RelocationType RT = (ARM::RelocationType)MR->getRelocationType();
00238   switch (RT) {
00239   default:
00240     return (intptr_t)(MR->getResultPointer());
00241   case ARM::reloc_arm_pic_jt:
00242     // Destination address - jump table base.
00243     return (intptr_t)(MR->getResultPointer()) - MR->getConstantVal();
00244   case ARM::reloc_arm_jt_base:
00245     // Jump table base address.
00246     return getJumpTableBaseAddr(MR->getJumpTableIndex());
00247   case ARM::reloc_arm_cp_entry:
00248   case ARM::reloc_arm_vfp_cp_entry:
00249     // Constant pool entry address.
00250     return getConstantPoolEntryAddr(MR->getConstantPoolIndex());
00251   case ARM::reloc_arm_machine_cp_entry: {
00252     ARMConstantPoolValue *ACPV = (ARMConstantPoolValue*)MR->getConstantVal();
00253     assert((!ACPV->hasModifier() && !ACPV->mustAddCurrentAddress()) &&
00254            "Can't handle this machine constant pool entry yet!");
00255     intptr_t Addr = (intptr_t)(MR->getResultPointer());
00256     Addr -= getPCLabelAddr(ACPV->getLabelId()) + ACPV->getPCAdjustment();
00257     return Addr;
00258   }
00259   }
00260 }
00261 
00262 /// relocate - Before the JIT can run a block of code that has been emitted,
00263 /// it must rewrite the code to contain the actual addresses of any
00264 /// referenced global symbols.
00265 void ARMJITInfo::relocate(void *Function, MachineRelocation *MR,
00266                           unsigned NumRelocs, unsigned char* GOTBase) {
00267   for (unsigned i = 0; i != NumRelocs; ++i, ++MR) {
00268     void *RelocPos = (char*)Function + MR->getMachineCodeOffset();
00269     intptr_t ResultPtr = resolveRelocDestAddr(MR);
00270     switch ((ARM::RelocationType)MR->getRelocationType()) {
00271     case ARM::reloc_arm_cp_entry:
00272     case ARM::reloc_arm_vfp_cp_entry:
00273     case ARM::reloc_arm_relative: {
00274       // It is necessary to calculate the correct PC relative value. We
00275       // subtract the base addr from the target addr to form a byte offset.
00276       ResultPtr = ResultPtr - (intptr_t)RelocPos - 8;
00277       // If the result is positive, set bit U(23) to 1.
00278       if (ResultPtr >= 0)
00279         *((intptr_t*)RelocPos) |= 1 << ARMII::U_BitShift;
00280       else {
00281         // Otherwise, obtain the absolute value and set bit U(23) to 0.
00282         *((intptr_t*)RelocPos) &= ~(1 << ARMII::U_BitShift);
00283         ResultPtr = - ResultPtr;
00284       }
00285       // Set the immed value calculated.
00286       // VFP immediate offset is multiplied by 4.
00287       if (MR->getRelocationType() == ARM::reloc_arm_vfp_cp_entry)
00288         ResultPtr = ResultPtr >> 2;
00289       *((intptr_t*)RelocPos) |= ResultPtr;
00290       // Set register Rn to PC.
00291       *((intptr_t*)RelocPos) |=
00292         ARMRegisterInfo::getRegisterNumbering(ARM::PC) << ARMII::RegRnShift;
00293       break;
00294     }
00295     case ARM::reloc_arm_pic_jt:
00296     case ARM::reloc_arm_machine_cp_entry:
00297     case ARM::reloc_arm_absolute: {
00298       // These addresses have already been resolved.
00299       *((intptr_t*)RelocPos) |= (intptr_t)ResultPtr;
00300       break;
00301     }
00302     case ARM::reloc_arm_branch: {
00303       // It is necessary to calculate the correct value of signed_immed_24
00304       // field. We subtract the base addr from the target addr to form a
00305       // byte offset, which must be inside the range -33554432 and +33554428.
00306       // Then, we set the signed_immed_24 field of the instruction to bits
00307       // [25:2] of the byte offset. More details ARM-ARM p. A4-11.
00308       ResultPtr = ResultPtr - (intptr_t)RelocPos - 8;
00309       ResultPtr = (ResultPtr & 0x03FFFFFC) >> 2;
00310       assert(ResultPtr >= -33554432 && ResultPtr <= 33554428);
00311       *((intptr_t*)RelocPos) |= ResultPtr;
00312       break;
00313     }
00314     case ARM::reloc_arm_jt_base: {
00315       // JT base - (instruction addr + 8)
00316       ResultPtr = ResultPtr - (intptr_t)RelocPos - 8;
00317       *((intptr_t*)RelocPos) |= ResultPtr;
00318       break;
00319     }
00320     }
00321   }
00322 }