When/Where does PyPy produce machine code?

Meh*_*ANI 7 python pypy

I have skimmed through the PyPy implementation details and went through the source code as well, but PyPy's execution path is still not totally clear to me.

Sometimes Bytecode is produced, sometimes it is skipped for immediate machine-code compiling (interpreter level/app level code), But I can't figure out when and where exactly is the machine code produced, to be handed to the OS for binary execution through low-level instructions (RAM/CPU).

I managed to get that straight in the case of CPython, as there is a giant switch in ceval.c - that is already compiled - which interprets bytecode and runs the corresponding code (in actual C actually). Makes sense.
But as far as PyPy is concerned, I did not manage to get a clear view on how this is done, specifically (I do not want to get into the various optimization details of PyPy, that's not what I am after here).

我会对指向 PYPY 源代码的答案感到满意,因此为了避免“道听途说”并能够“用我的眼睛”看到它(我在 /rpython 下发现了 JIT 后端部分,其中包含各种 CPU 架构汇编程序)

Rol*_*ith 3

最好的指南是pypy 架构文档和实际的JIT 文档

对我来说最跳出来的是:

我们有一个跟踪 JIT,它跟踪用 RPython 编写的解释器,而不是它解释的用户程序。

JIT 概述中对此进行了更详细的介绍。

似乎“核心”是这样的(来自这里):

一旦元解释器验证它已经跟踪了一个循环,它就会决定如何编译它所拥有的内容。这些操作之间有一个可选的优化阶段,本页后面将对此进行介绍。后端将跟踪操作转换为特定机器的汇编。然后它将编译后的循环返回给前端。下次在应用程序代码中看到循环时,可以运行优化的程序集而不是正常的解释器。

本文(PDF) 也可能会有所帮助。

编辑:查看 x86 后端rpython/jit/backend/x86/rx86.py,后端并没有编译,而是直接吐出机器代码。看看X86_64_CodeBuilderAbstractX86CodeBuilder类。更高一级是Assembler386中的类rpython/jit/backend/x86/assembler.py。该汇编器使用MachineCodeBlockWrapper基于rpython/jit/backend/x86/codebuf.pyx86-64 的X86_64_CodeBuilder