最近阅读代码主要牵涉到两个问题,一个是动态二进制翻译,一个是进程管理。
两个问题都很棘手,代码量大,复杂度高。今天主要备份一下关键笔记。
/
启动运行流程:
用户点击 运行 按钮
RunButtonActionPerformed
RunEmu
emulator.RunEmu()
mainThread.start()
start就是调用该线程的run方法
Emulator.run()
如果使能动态二进制翻译
RuntimeContext.run()
-->getExecutable(processor.cpu.pc)
-->Compiler.getInstance().compile(address)
-->compile(address, getResetCount())
-->executable = analyse(context, address, false, instanceIndex)
-->context.analysedAddresses.add(pc)
codeBlock.addInstruction(pc, opcode, insn, isBranchTarget, isBranching, branchingTo)
executable = codeBlock.getExecutable(context)
-->compile(context)
-->prepare(context, context.getMethodMaxInstructions())
compile(context, mv, codeInstructions)
compile(context, mv, sequenceCodeInstruction.getCodeSequence().getInstructions())
否则,不使能动态二进制翻译,就单步执行
processor.step()
/
进程调度与进程切换:
ThreadManForUser.java(Initialise创建了第一个进程)
hleRescheduleCurrentThread
contextSwitch *************************关键函数,关于callback此处还有疑问**********************
internalContextSwitch
这个函数调用过程完成了操作系统层面的进程调度和上下文切换,在系统层的进程管理器中记录了新的运行态进程,并且根据该进程的描
述符更新了processor
internalContextSwitch最后调用了RuntimeContext.update
RuntimeContext.update
则是更新运行时上下文,将运行上下文切换到新进程。函数调用过程:
RuntimeContext.update
switchThread
switchRealThread
{
thread = new RuntimeThread(threadInfo)
thread.start()
-->thread.run()
-->RuntimeContext.runThread(this)
-->getExecutable(processor.cpu.pc)
-->executable = Compiler.getInstance().compile(address)
如果目标进程尚未创建运行时上下文,就在此时创建(也就是动态编译出可执行程序)
}
restoreContext中也调用了update操作,似乎与前述内容有重复