gpt4 book ai didi

python - PyPy 何时/何处生成机器代码?

转载 作者:行者123 更新时间:2023-12-03 13:40:25 25 4
gpt4 key购买 nike

我浏览了 PyPy implementation details并查看了源代码,但 PyPy 的执行路径对我来说仍然不完全清楚。
有时会生成字节码,有时会跳过它以进行即时机器代码编译(解释器级别/应用程序级别代码),但我无法弄清楚机器代码是何时何地生成的,要交给操作系统进行二进制执行通过低级指令(RAM/CPU)。
在 CPython 的情况下,我设法做到了这一点,因为在 ceval.c 中有一个巨大的开关- 已经编译 - 解释字节码并运行相应的代码(实际上是在 C 中)。有道理。但就 PyPy 而言,我没有清楚地了解这是如何完成的,具体来说(我不想深入了解 PyPy 的各种优化细节,这不是我在这里追求的) .
我会对指向 PYPY 源代码的答案感到满意,因此为了避免“道听途说”并能够“用我的眼睛”看到它(我在/rpython 下发现了 JIT 后端部分,其中包含各种 CPU 架构汇编程序)

最佳答案

您最好的指南是 pypy architecture documentation ,和实际 JIT documentation .
对我来说跳得最多的是:

we have a tracing JIT that traces the interpreter written in RPython, rather than the user program that it interprets.


这在 JIT overview 中有更详细的介绍。 .
似乎“核心”是这样的(来自 here ):

Once the meta-interpreter has verified that it has traced a loop, it decides how to compile what it has. There is an optional optimization phase between these actions which is covered future down this page. The backend converts the trace operations into assembly for the particular machine. It then hands the compiled loop back to the frontend. The next time the loop is seen in application code, the optimized assembly can be run instead of the normal interpreter.


This paper (PDF) 也可能有帮助。
编辑:查看 x86 后端 rpython/jit/backend/x86/rx86.py , 后端并没有编译,而是直接吐出机器码。看 X86_64_CodeBuilderAbstractX86CodeBuilder类。高一级是 Assembler386类(class) rpython/jit/backend/x86/assembler.py .这个汇编器使用 MachineCodeBlockWrapper来自 rpython/jit/backend/x86/codebuf.py这是基于 X86_64_CodeBuilder对于 x86-64。

关于python - PyPy 何时/何处生成机器代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63661711/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com