LLVM API Documentation

MipsJITInfo.cpp
Go to the documentation of this file.
00001 //===-- MipsJITInfo.cpp - Implement the Mips JIT Interface ----------------===//
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 Mips target.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #define DEBUG_TYPE "jit"
00015 #include "MipsJITInfo.h"
00016 #include "MipsInstrInfo.h"
00017 #include "MipsRelocations.h"
00018 #include "MipsSubtarget.h"
00019 #include "llvm/CodeGen/JITCodeEmitter.h"
00020 #include "llvm/IR/Function.h"
00021 #include "llvm/Support/Debug.h"
00022 #include "llvm/Support/ErrorHandling.h"
00023 #include "llvm/Support/Memory.h"
00024 #include "llvm/Support/raw_ostream.h"
00025 #include <cstdlib>
00026 using namespace llvm;
00027 
00028 
00029 void MipsJITInfo::replaceMachineCodeForFunction(void *Old, void *New) {
00030   unsigned NewAddr = (intptr_t)New;
00031   unsigned OldAddr = (intptr_t)Old;
00032   const unsigned NopInstr = 0x0;
00033 
00034   // If the functions are in the same memory segment, insert PC-region branch.
00035   if ((NewAddr & 0xF0000000) == ((OldAddr + 4) & 0xF0000000)) {
00036     unsigned *OldInstruction = (unsigned *)Old;
00037     *OldInstruction = 0x08000000;
00038     unsigned JTargetAddr = NewAddr & 0x0FFFFFFC;
00039 
00040     JTargetAddr >>= 2;
00041     *OldInstruction |= JTargetAddr;
00042 
00043     // Insert a NOP.
00044     OldInstruction++;
00045     *OldInstruction = NopInstr;
00046 
00047     sys::Memory::InvalidateInstructionCache(Old, 2 * 4);
00048   } else {
00049     // We need to clear hint bits from the instruction, in case it is 'jr ra'.
00050     const unsigned HintMask = 0xFFFFF83F, ReturnSequence = 0x03e00008;
00051     unsigned* CurrentInstr = (unsigned*)Old;
00052     unsigned CurrInstrHintClear = (*CurrentInstr) & HintMask;
00053     unsigned* NextInstr = CurrentInstr + 1;
00054     unsigned NextInstrHintClear = (*NextInstr) & HintMask;
00055 
00056     // Do absolute jump if there are 2 or more instructions before return from
00057     // the old function.
00058     if ((CurrInstrHintClear != ReturnSequence) &&
00059         (NextInstrHintClear != ReturnSequence)) {
00060       const unsigned LuiT0Instr = 0x3c080000, AddiuT0Instr = 0x25080000;
00061       const unsigned JrT0Instr = 0x01000008;
00062       // lui  t0,  high 16 bit of the NewAddr
00063       (*(CurrentInstr++)) = LuiT0Instr | ((NewAddr & 0xffff0000) >> 16);
00064       // addiu  t0, t0, low 16 bit of the NewAddr
00065       (*(CurrentInstr++)) = AddiuT0Instr | (NewAddr & 0x0000ffff);
00066       // jr t0
00067       (*(CurrentInstr++)) = JrT0Instr;
00068       (*CurrentInstr) = NopInstr;
00069 
00070       sys::Memory::InvalidateInstructionCache(Old, 4 * 4);
00071     } else {
00072       // Unsupported case
00073       report_fatal_error("MipsJITInfo::replaceMachineCodeForFunction");
00074     }
00075   }
00076 }
00077 
00078 /// JITCompilerFunction - This contains the address of the JIT function used to
00079 /// compile a function lazily.
00080 static TargetJITInfo::JITCompilerFn JITCompilerFunction;
00081 
00082 // Get the ASMPREFIX for the current host.  This is often '_'.
00083 #ifndef __USER_LABEL_PREFIX__
00084 #define __USER_LABEL_PREFIX__
00085 #endif
00086 #define GETASMPREFIX2(X) #X
00087 #define GETASMPREFIX(X) GETASMPREFIX2(X)
00088 #define ASMPREFIX GETASMPREFIX(__USER_LABEL_PREFIX__)
00089 
00090 // CompilationCallback stub - We can't use a C function with inline assembly in
00091 // it, because the prolog/epilog inserted by GCC won't work for us. Instead,
00092 // write our own wrapper, which does things our way, so we have complete control
00093 // over register saving and restoring. This code saves registers, calls
00094 // MipsCompilationCallbackC and restores registers.
00095 extern "C" {
00096 #if defined (__mips__)
00097 void MipsCompilationCallback();
00098 
00099   asm(
00100     ".text\n"
00101     ".align 2\n"
00102     ".globl " ASMPREFIX "MipsCompilationCallback\n"
00103     ASMPREFIX "MipsCompilationCallback:\n"
00104     ".ent " ASMPREFIX "MipsCompilationCallback\n"
00105     ".frame  $sp, 32, $ra\n"
00106     ".set  noreorder\n"
00107     ".cpload $t9\n"
00108 
00109     "addiu $sp, $sp, -64\n"
00110     ".cprestore 16\n"
00111 
00112     // Save argument registers a0, a1, a2, a3, f12, f14 since they may contain
00113     // stuff for the real target function right now. We have to act as if this
00114     // whole compilation callback doesn't exist as far as the caller is
00115     // concerned. We also need to save the ra register since it contains the
00116     // original return address, and t8 register since it contains the address
00117     // of the end of function stub.
00118     "sw $a0, 20($sp)\n"
00119     "sw $a1, 24($sp)\n"
00120     "sw $a2, 28($sp)\n"
00121     "sw $a3, 32($sp)\n"
00122     "sw $ra, 36($sp)\n"
00123     "sw $t8, 40($sp)\n"
00124     "sdc1 $f12, 48($sp)\n"
00125     "sdc1 $f14, 56($sp)\n"
00126 
00127     // t8 points at the end of function stub. Pass the beginning of the stub
00128     // to the MipsCompilationCallbackC.
00129     "addiu $a0, $t8, -16\n"
00130     "jal " ASMPREFIX "MipsCompilationCallbackC\n"
00131     "nop\n"
00132 
00133     // Restore registers.
00134     "lw $a0, 20($sp)\n"
00135     "lw $a1, 24($sp)\n"
00136     "lw $a2, 28($sp)\n"
00137     "lw $a3, 32($sp)\n"
00138     "lw $ra, 36($sp)\n"
00139     "lw $t8, 40($sp)\n"
00140     "ldc1 $f12, 48($sp)\n"
00141     "ldc1 $f14, 56($sp)\n"
00142     "addiu $sp, $sp, 64\n"
00143 
00144     // Jump to the (newly modified) stub to invoke the real function.
00145     "addiu $t8, $t8, -16\n"
00146     "jr $t8\n"
00147     "nop\n"
00148 
00149     ".set  reorder\n"
00150     ".end " ASMPREFIX "MipsCompilationCallback\n"
00151       );
00152 #else  // host != Mips
00153   void MipsCompilationCallback() {
00154     llvm_unreachable(
00155       "Cannot call MipsCompilationCallback() on a non-Mips arch!");
00156   }
00157 #endif
00158 }
00159 
00160 /// MipsCompilationCallbackC - This is the target-specific function invoked
00161 /// by the function stub when we did not know the real target of a call.
00162 /// This function must locate the start of the stub or call site and pass
00163 /// it into the JIT compiler function.
00164 extern "C" void MipsCompilationCallbackC(intptr_t StubAddr) {
00165   // Get the address of the compiled code for this function.
00166   intptr_t NewVal = (intptr_t) JITCompilerFunction((void*) StubAddr);
00167 
00168   // Rewrite the function stub so that we don't end up here every time we
00169   // execute the call. We're replacing the first four instructions of the
00170   // stub with code that jumps to the compiled function:
00171   //   lui $t9, %hi(NewVal)
00172   //   addiu $t9, $t9, %lo(NewVal)
00173   //   jr $t9
00174   //   nop
00175 
00176   int Hi = ((unsigned)NewVal & 0xffff0000) >> 16;
00177   if ((NewVal & 0x8000) != 0)
00178     Hi++;
00179   int Lo = (int)(NewVal & 0xffff);
00180 
00181   *(intptr_t *)(StubAddr) = 0xf << 26 | 25 << 16 | Hi;
00182   *(intptr_t *)(StubAddr + 4) = 9 << 26 | 25 << 21 | 25 << 16 | Lo;
00183   *(intptr_t *)(StubAddr + 8) = 25 << 21 | 8;
00184   *(intptr_t *)(StubAddr + 12) = 0;
00185 
00186   sys::Memory::InvalidateInstructionCache((void*) StubAddr, 16);
00187 }
00188 
00189 TargetJITInfo::LazyResolverFn MipsJITInfo::getLazyResolverFunction(
00190     JITCompilerFn F) {
00191   JITCompilerFunction = F;
00192   return MipsCompilationCallback;
00193 }
00194 
00195 TargetJITInfo::StubLayout MipsJITInfo::getStubLayout() {
00196   // The stub contains 4 4-byte instructions, aligned at 4 bytes. See
00197   // emitFunctionStub for details.
00198   StubLayout Result = { 4*4, 4 };
00199   return Result;
00200 }
00201 
00202 void *MipsJITInfo::emitFunctionStub(const Function *F, void *Fn,
00203                                     JITCodeEmitter &JCE) {
00204   JCE.emitAlignment(4);
00205   void *Addr = (void*) (JCE.getCurrentPCValue());
00206   if (!sys::Memory::setRangeWritable(Addr, 16))
00207     llvm_unreachable("ERROR: Unable to mark stub writable.");
00208 
00209   intptr_t EmittedAddr;
00210   if (Fn != (void*)(intptr_t)MipsCompilationCallback)
00211     EmittedAddr = (intptr_t)Fn;
00212   else
00213     EmittedAddr = (intptr_t)MipsCompilationCallback;
00214 
00215 
00216   int Hi = ((unsigned)EmittedAddr & 0xffff0000) >> 16;
00217   if ((EmittedAddr & 0x8000) != 0)
00218     Hi++;
00219   int Lo = (int)(EmittedAddr & 0xffff);
00220 
00221   // lui t9, %hi(EmittedAddr)
00222   // addiu t9, t9, %lo(EmittedAddr)
00223   // jalr t8, t9
00224   // nop
00225   if (IsLittleEndian) {
00226     JCE.emitWordLE(0xf << 26 | 25 << 16 | Hi);
00227     JCE.emitWordLE(9 << 26 | 25 << 21 | 25 << 16 | Lo);
00228     JCE.emitWordLE(25 << 21 | 24 << 11 | 9);
00229     JCE.emitWordLE(0);
00230   } else {
00231     JCE.emitWordBE(0xf << 26 | 25 << 16 | Hi);
00232     JCE.emitWordBE(9 << 26 | 25 << 21 | 25 << 16 | Lo);
00233     JCE.emitWordBE(25 << 21 | 24 << 11 | 9);
00234     JCE.emitWordBE(0);
00235   }
00236 
00237   sys::Memory::InvalidateInstructionCache(Addr, 16);
00238   if (!sys::Memory::setRangeExecutable(Addr, 16))
00239     llvm_unreachable("ERROR: Unable to mark stub executable.");
00240 
00241   return Addr;
00242 }
00243 
00244 /// relocate - Before the JIT can run a block of code that has been emitted,
00245 /// it must rewrite the code to contain the actual addresses of any
00246 /// referenced global symbols.
00247 void MipsJITInfo::relocate(void *Function, MachineRelocation *MR,
00248                            unsigned NumRelocs, unsigned char *GOTBase) {
00249   for (unsigned i = 0; i != NumRelocs; ++i, ++MR) {
00250 
00251     void *RelocPos = (char*) Function + MR->getMachineCodeOffset();
00252     intptr_t ResultPtr = (intptr_t) MR->getResultPointer();
00253 
00254     switch ((Mips::RelocationType) MR->getRelocationType()) {
00255     case Mips::reloc_mips_pc16:
00256       ResultPtr = (((ResultPtr - (intptr_t) RelocPos) - 4) >> 2) & 0xffff;
00257       *((unsigned*) RelocPos) |= (unsigned) ResultPtr;
00258       break;
00259 
00260     case Mips::reloc_mips_26:
00261       ResultPtr = (ResultPtr & 0x0fffffff) >> 2;
00262       *((unsigned*) RelocPos) |= (unsigned) ResultPtr;
00263       break;
00264 
00265     case Mips::reloc_mips_hi:
00266       ResultPtr = ResultPtr >> 16;
00267       if ((((intptr_t) (MR->getResultPointer()) & 0xffff) >> 15) == 1) {
00268         ResultPtr += 1;
00269       }
00270       *((unsigned*) RelocPos) |= (unsigned) ResultPtr;
00271       break;
00272 
00273     case Mips::reloc_mips_lo: {
00274       // Addend is needed for unaligned load/store instructions, where offset
00275       // for the second load/store in the expanded instruction sequence must
00276       // be modified by +1 or +3. Otherwise, Addend is 0.
00277       int Addend = *((unsigned*) RelocPos) & 0xffff;
00278       ResultPtr = (ResultPtr + Addend) & 0xffff;
00279       *((unsigned*) RelocPos) &= 0xffff0000;
00280       *((unsigned*) RelocPos) |= (unsigned) ResultPtr;
00281       break;
00282     }
00283     }
00284   }
00285 }