-namespace {
- class JITResolver {
- SparcV9CodeEmitter &SparcV9;
- MachineCodeEmitter &MCE;
-
- /// LazyCodeGenMap - Keep track of call sites for functions that are to be
- /// lazily resolved.
- ///
- std::map<uint64_t, Function*> LazyCodeGenMap;
-
- /// LazyResolverMap - Keep track of the lazy resolver created for a
- /// particular function so that we can reuse them if necessary.
- ///
- std::map<Function*, uint64_t> LazyResolverMap;
-
- public:
- enum CallType { ShortCall, FarCall };
-
- private:
- /// We need to keep track of whether we used a simple call or a far call
- /// (many instructions) in sequence. This means we need to keep track of
- /// what type of stub we generate.
- static std::map<uint64_t, CallType> LazyCallFlavor;
-
- public:
- JITResolver(SparcV9CodeEmitter &V9,
- MachineCodeEmitter &mce) : SparcV9(V9), MCE(mce) {}
- uint64_t getLazyResolver(Function *F);
- uint64_t addFunctionReference(uint64_t Address, Function *F);
- void deleteFunctionReference(uint64_t Address);
- void addCallFlavor(uint64_t Address, CallType Flavor) {
- LazyCallFlavor[Address] = Flavor;
- }
-
- // Utility functions for accessing data from static callback
- uint64_t getCurrentPCValue() {
- return MCE.getCurrentPCValue();
- }
- unsigned getBinaryCodeForInstr(MachineInstr &MI) {
- return SparcV9.getBinaryCodeForInstr(MI);
- }
-
- inline uint64_t insertFarJumpAtAddr(int64_t Value, uint64_t Addr);
-
- private:
- uint64_t emitStubForFunction(Function *F);
- static void CompilationCallback();
- uint64_t resolveFunctionReference(uint64_t RetAddr);
-
- };
-
- JITResolver *TheJITResolver;
- std::map<uint64_t, JITResolver::CallType> JITResolver::LazyCallFlavor;
-}
-
-/// addFunctionReference - This method is called when we need to emit the
-/// address of a function that has not yet been emitted, so we don't know the
-/// address. Instead, we emit a call to the CompilationCallback method, and
-/// keep track of where we are.
-///
-uint64_t JITResolver::addFunctionReference(uint64_t Address, Function *F) {
- LazyCodeGenMap[Address] = F;
- return (intptr_t)&JITResolver::CompilationCallback;
-}
-
-/// deleteFunctionReference - If we are emitting a far call, we already added a
-/// reference to the function, but it is now incorrect, since the address to the
-/// JIT resolver is too far away to be a simple call instruction. This is used
-/// to remove the address from the map.
-///
-void JITResolver::deleteFunctionReference(uint64_t Address) {
- std::map<uint64_t, Function*>::iterator I = LazyCodeGenMap.find(Address);
- assert(I != LazyCodeGenMap.end() && "Not in map!");
- LazyCodeGenMap.erase(I);
-}
-
-uint64_t JITResolver::resolveFunctionReference(uint64_t RetAddr) {
- std::map<uint64_t, Function*>::iterator I = LazyCodeGenMap.find(RetAddr);
- assert(I != LazyCodeGenMap.end() && "Not in map!");
- Function *F = I->second;
- LazyCodeGenMap.erase(I);
- return MCE.forceCompilationOf(F);
-}
-
-uint64_t JITResolver::getLazyResolver(Function *F) {
- std::map<Function*, uint64_t>::iterator I = LazyResolverMap.lower_bound(F);
- if (I != LazyResolverMap.end() && I->first == F) return I->second;
-
-//std::cerr << "Getting lazy resolver for : " << ((Value*)F)->getName() << "\n";
-
- uint64_t Stub = emitStubForFunction(F);
- LazyResolverMap.insert(I, std::make_pair(F, Stub));
- return Stub;
-}
-
-uint64_t JITResolver::insertFarJumpAtAddr(int64_t Target, uint64_t Addr) {
-
- static const unsigned i1 = SparcIntRegClass::i1, i2 = SparcIntRegClass::i2,
- i7 = SparcIntRegClass::i7,
- o6 = SparcIntRegClass::o6, g0 = SparcIntRegClass::g0,
- g1 = SparcIntRegClass::g1, g5 = SparcIntRegClass::g5;
-
- MachineInstr* BinaryCode[] = {
- //
- // Get address to branch into %g1, using %g5 as a temporary
- //
- // sethi %uhi(Target), %g5 ;; get upper 22 bits of Target into %g5
- BuildMI(V9::SETHI, 2).addSImm(Target >> 42).addReg(g5),
- // or %g5, %ulo(Target), %g5 ;; get 10 lower bits of upper word into %g5
- BuildMI(V9::ORi, 3).addReg(g5).addSImm((Target >> 32) & 0x03ff).addReg(g5),
- // sllx %g5, 32, %g5 ;; shift those 10 bits to the upper word
- BuildMI(V9::SLLXi6, 3).addReg(g5).addSImm(32).addReg(g5),
- // sethi %hi(Target), %g1 ;; extract bits 10-31 into the dest reg
- BuildMI(V9::SETHI, 2).addSImm((Target >> 10) & 0x03fffff).addReg(g1),
- // or %g5, %g1, %g1 ;; get upper word (in %i1) into %g1
- BuildMI(V9::ORr, 3).addReg(g5).addReg(g1).addReg(g1),
- // or %g1, %lo(Target), %g1 ;; get lowest 10 bits of Target into %g1
- BuildMI(V9::ORi, 3).addReg(g1).addSImm(Target & 0x03ff).addReg(g1),
- // jmpl %g1, %g0, %g0 ;; indirect branch on %g1
- BuildMI(V9::JMPLRETr, 3).addReg(g1).addReg(g0).addReg(g0),
- // nop ;; delay slot
- BuildMI(V9::NOP, 0)
- };
-
- for (unsigned i=0, e=sizeof(BinaryCode)/sizeof(BinaryCode[0]); i!=e; ++i) {
- *((unsigned*)(intptr_t)Addr) = getBinaryCodeForInstr(*BinaryCode[i]);
- delete BinaryCode[i];
- Addr += 4;
- }
-
- return Addr;
-}
-
-void JITResolver::CompilationCallback() {
- uint64_t CameFrom = (uint64_t)(intptr_t)__builtin_return_address(0);
- int64_t Target = (int64_t)TheJITResolver->resolveFunctionReference(CameFrom);
- DEBUG(std::cerr << "In callback! Addr=0x" << std::hex << CameFrom << "\n");
-#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
- register int64_t returnAddr;
- __asm__ __volatile__ ("add %%i7, %%g0, %0" : "=r" (returnAddr) : );
- DEBUG(std::cerr << "Read i7 (return addr) = "
- << std::hex << returnAddr << ", value: "
- << std::hex << *(unsigned*)returnAddr << "\n");
-#endif
-
- // Rewrite the call target so that we don't fault every time we execute it.
- //
-
- static const unsigned o6 = SparcIntRegClass::o6;
-
- // Subtract enough to overwrite up to the 'save' instruction
- // This depends on whether we made a short call (1 instruction) or the
- // farCall (long form: 10 instructions, short form: 7 instructions)
- uint64_t Offset = (LazyCallFlavor[CameFrom] == ShortCall) ? 4 : 28;
- uint64_t CodeBegin = CameFrom - Offset;
-
- // Make sure that what we're about to overwrite is indeed "save"
- MachineInstr *SV = BuildMI(V9::SAVEi, 3).addReg(o6).addSImm(-192).addReg(o6);
- unsigned SaveInst = TheJITResolver->getBinaryCodeForInstr(*SV);
- delete SV;
- unsigned CodeInMem = *(unsigned*)(intptr_t)CodeBegin;
- assert(CodeInMem == SaveInst && "About to overwrite smthg not a save instr!");
- DEBUG(std::cerr << "Emitting a far jump to 0x" << std::hex << Target << "\n");
- TheJITResolver->insertFarJumpAtAddr(Target, CodeBegin);
-
- // FIXME: if the target function is close enough to fit into the 19bit disp of
- // BA, we should use this version, as its much cheaper to generate.
-#if 0
- uint64_t InstAddr = CodeBegin;
- // ba <target>
- MachineInstr *MI = BuildMI(V9::BA, 1).addSImm(Target);
- *((unsigned*)(intptr_t)InstAddr)=TheJITResolver->getBinaryCodeForInstr(*MI);
- InstAddr += 4;
- delete MI;
-
- // nop
- MI = BuildMI(V9::NOP, 0);
- *((unsigned*)(intptr_t))=TheJITResolver->getBinaryCodeForInstr(*Nop);
- delete MI;
-#endif
-
- // Change the return address to reexecute the restore, then the jump
- // The return address is really %o7, but will disappear after this function
- // returns, and the register windows are rotated away.
-#if defined(sparc) || defined(__sparc__) || defined(__sparcv9)
- __asm__ __volatile__ ("sub %%i7, %0, %%i7" : : "r" (Offset+12));
- DEBUG(std::cerr << "Callback setting return addr to "
- << std::hex << (CameFrom-Offset-12) << "\n");
-#endif
-}
-
-/// emitStubForFunction - This method is used by the JIT when it needs to emit
-/// the address of a function for a function whose code has not yet been
-/// generated. In order to do this, it generates a stub which jumps to the lazy
-/// function compiler, which will eventually get fixed to call the function
-/// directly.
-///
-uint64_t JITResolver::emitStubForFunction(Function *F) {
- MCE.startFunctionStub(*F, 20);
-
- DEBUG(std::cerr << "Emitting stub at addr: 0x"
- << std::hex << MCE.getCurrentPCValue() << "\n");
-
- unsigned o6 = SparcIntRegClass::o6, g0 = SparcIntRegClass::g0;
-
- // restore %g0, 0, %g0
- MachineInstr *R = BuildMI(V9::RESTOREi, 3).addMReg(g0).addSImm(0)
- .addMReg(g0, MOTy::Def);
- SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*R));
- delete R;
-
- // save %sp, -192, %sp
- MachineInstr *SV = BuildMI(V9::SAVEi, 3).addReg(o6).addSImm(-192).addReg(o6);
- SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*SV));
- delete SV;
-
- int64_t CurrPC = MCE.getCurrentPCValue();
- int64_t Addr = (int64_t)addFunctionReference(CurrPC, F);
- int64_t CallTarget = (Addr-CurrPC) >> 2;
- //if (CallTarget >= (1 << 29) || CallTarget <= -(1 << 29)) {
- // Since this is a far call, the actual address of the call is shifted
- // by the number of instructions it takes to calculate the exact address
- deleteFunctionReference(CurrPC);
- SparcV9.emitFarCall(Addr, F);
-#if 0
- else {
- // call CallTarget ;; invoke the callback
- MachineInstr *Call = BuildMI(V9::CALL, 1).addSImm(CallTarget);
- SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*Call));
- delete Call;
-
- // nop ;; call delay slot
- MachineInstr *Nop = BuildMI(V9::NOP, 0);
- SparcV9.emitWord(SparcV9.getBinaryCodeForInstr(*Nop));
- delete Nop;
-
- addCallFlavor(CurrPC, ShortCall);
- }
-#endif
-
- SparcV9.emitWord(0xDEADBEEF); // marker so that we know it's really a stub
- return (intptr_t)MCE.finishFunctionStub(*F)+4; /* 1 instr past the restore */
-}
-
-