This situation can occur:
[oota-llvm.git] / lib / Target / ARM / ARMJITInfo.cpp
index 294a12bc7862cffdd2bcb8928a3265e10f371894..bc2bba36c80a340293ad8ac07586cfbec88c8b3b 100644 (file)
@@ -2,8 +2,8 @@
 //
 //                     The LLVM Compiler Infrastructure
 //
-// This file was developed by the Raul Herbster and is distributed under the 
-// University of Illinois Open Source License. See LICENSE.TXT for details.
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
 //
 //===----------------------------------------------------------------------===//
 //
 #include "ARMJITInfo.h"
 #include "ARMRelocations.h"
 #include "ARMSubtarget.h"
+#include "llvm/Function.h"
 #include "llvm/CodeGen/MachineCodeEmitter.h"
 #include "llvm/Config/alloca.h"
 #include <cstdlib>
 using namespace llvm;
 
 void ARMJITInfo::replaceMachineCodeForFunction(void *Old, void *New) {
-  unsigned char *OldByte = (unsigned char *)Old;
-  *OldByte++ = 0xEA;                // Emit B opcode.
-  unsigned *OldWord = (unsigned *)OldByte;
-  unsigned NewAddr = (intptr_t)New;
-  unsigned OldAddr = (intptr_t)OldWord;
-  *OldWord = NewAddr - OldAddr - 4; // Emit PC-relative addr of New code.
+  abort();
 }
 
 /// JITCompilerFunction - This contains the address of the JIT function used to
@@ -80,18 +76,16 @@ extern "C" void ARMCompilationCallbackC(intptr_t *StackPtr, intptr_t RetAddr) {
        << ": Resolving call to function: "
        << TheVM->getFunctionReferencedName((void*)RetAddr) << "\n";
 #endif
+  intptr_t Addr = RetAddr - 4;
 
-  // Sanity check to make sure this really is a branch and link instruction.
-  assert(((unsigned char*)RetAddr-1)[3] == 0xEB && "Not a branch and link instr!");
-
-  intptr_t NewVal = (intptr_t)JITCompilerFunction((void*)RetAddr);
+  intptr_t NewVal = (intptr_t)JITCompilerFunction((void*)Addr);
 
   // Rewrite the call target... so that we don't end up here every time we
   // execute the call.
-  *(intptr_t *)RetAddr = (intptr_t)(NewVal-RetAddr-4);
+  *(intptr_t *)Addr = NewVal;
 
   // Change the return address to reexecute the branch and link instruction...
-  *RetAddrLoc -= 1;
+  *RetAddrLoc -= 12;
 }
 
 TargetJITInfo::LazyResolverFn
@@ -100,26 +94,29 @@ ARMJITInfo::getLazyResolverFunction(JITCompilerFn F) {
   return ARMCompilationCallback;
 }
 
-void *ARMJITInfo::emitFunctionStub(void *Fn, MachineCodeEmitter &MCE) {
-  unsigned addr = (intptr_t)Fn-MCE.getCurrentPCValue()-4;
+void *ARMJITInfo::emitFunctionStub(const Function* F, void *Fn,
+                                   MachineCodeEmitter &MCE) {
+  unsigned addr = (intptr_t)Fn;
   // If this is just a call to an external function, emit a branch instead of a
   // call.  The code is the same except for one bit of the last instruction.
   if (Fn != (void*)(intptr_t)ARMCompilationCallback) {
-    MCE.startFunctionStub(4, 2);
-    MCE.emitByte(0xEA);  // branch to the corresponding function addr 
-    MCE.emitByte((unsigned char)(addr >>  0));
-    MCE.emitByte((unsigned char)(addr >>  8));
-    MCE.emitByte((unsigned char)(addr >>  16));
-    return MCE.finishFunctionStub(0);
+    // branch to the corresponding function addr
+    // the stub is 8-byte size and 4-aligned
+    MCE.startFunctionStub(F, 8, 4);
+    MCE.emitWordLE(0xE51FF004); // LDR PC, [PC,#-4]
+    MCE.emitWordLE(addr);       // addr of function
   } else {
-    MCE.startFunctionStub(5, 2);
-    MCE.emitByte(0xEB);  // branch and link to the corresponding function addr
+    // branch and link to the corresponding function addr
+    // the stub is 20-byte size and 4-aligned
+    MCE.startFunctionStub(F, 20, 4);
+    MCE.emitWordLE(0xE92D4800); // STMFD SP!, [R11, LR]
+    MCE.emitWordLE(0xE28FE004); // ADD LR, PC, #4
+    MCE.emitWordLE(0xE51FF004); // LDR PC, [PC,#-4]
+    MCE.emitWordLE(addr);       // addr of function
+    MCE.emitWordLE(0xE8BD8800); // LDMFD SP!, [R11, PC]
   }
-  MCE.emitByte((unsigned char)(addr >>  0));
-  MCE.emitByte((unsigned char)(addr >>  8));
-  MCE.emitByte((unsigned char)(addr >>  16));
 
-  return MCE.finishFunctionStub(0);
+  return MCE.finishFunctionStub(F);
 }
 
 /// relocate - Before the JIT can run a block of code that has been emitted,
@@ -127,5 +124,41 @@ void *ARMJITInfo::emitFunctionStub(void *Fn, MachineCodeEmitter &MCE) {
 /// referenced global symbols.
 void ARMJITInfo::relocate(void *Function, MachineRelocation *MR,
                           unsigned NumRelocs, unsigned char* GOTBase) {
-
+  for (unsigned i = 0; i != NumRelocs; ++i, ++MR) {
+    void *RelocPos = (char*)Function + MR->getMachineCodeOffset();
+    intptr_t ResultPtr = (intptr_t)MR->getResultPointer();
+    switch ((ARM::RelocationType)MR->getRelocationType()) {
+    case ARM::reloc_arm_relative: {
+      // It is necessary to calculate the correct PC relative value. We
+      // subtract the base addr from the target addr to form a byte offset.
+      ResultPtr = ResultPtr-(intptr_t)RelocPos-8;
+      // If the result is positive, set bit U(23) to 1.
+      if (ResultPtr >= 0)
+        *((unsigned*)RelocPos) |= 1 << 23;
+      else {
+      // otherwise, obtain the absolute value and set
+      // bit U(23) to 0.
+        ResultPtr *= -1;
+        *((unsigned*)RelocPos) &= 0xFF7FFFFF;
+      }
+      // set the immed value calculated
+      *((unsigned*)RelocPos) |= (unsigned)ResultPtr;
+      // set register Rn to PC
+      *((unsigned*)RelocPos) |= 0xF << 16;
+      break;
+    }
+    case ARM::reloc_arm_branch: {
+      // It is necessary to calculate the correct value of signed_immed_24
+      // field. We subtract the base addr from the target addr to form a
+      // byte offset, which must be inside the range -33554432 and +33554428.
+      // Then, we set the signed_immed_24 field of the instruction to bits
+      // [25:2] of the byte offset. More details ARM-ARM p. A4-11.
+      ResultPtr = ResultPtr-(intptr_t)RelocPos-8;
+      ResultPtr = (ResultPtr & 0x03FFFFFC) >> 2;
+      assert(ResultPtr >= -33554432 && ResultPtr <= 33554428);
+      *((unsigned*)RelocPos) |= ResultPtr;
+      break;
+    }
+    }
+  }
 }