首页
社区
课程
招聘
[翻译]The CLR x86 JIT, an overview
2006-9-10 16:31 6584

[翻译]The CLR x86 JIT, an overview

2006-9-10 16:31
6584
The CLR x86 JIT, an overview (.net下JIT过程的官方简介)

原文出处:http://blogs.msdn.com/davidnotario/archive/2004/10/26/247792.aspx
边看边翻

I'll be doing a series of articles on how the x86 CLR JIT compiler works and the different optimizations it does for you.
我将就x86机器上通用语言运行时(CLR)的既时编译(JIT compiler)的工作原理以及各种优化手段写些文章。

Whenever a method is about to be executed for the first time, the VM realizes it doesn't have native code for it, and invokes the JIT to generate it (if you are curious about how it works and have the Rotor source code, go to CallCompileMethodWithSEHWrapper in jitinterface.cpp and backtrack from there).
当一个方法第一次被调用时,虚拟机检测到还没有与该方法相对应的本机代码,这时虚拟机会调用JIT来编译生成(如果你对该过程的具体内容感兴趣,可以在Rotor的源代码中找jitinterface.cpp这个文件,然后看CallCompileMethodWithSEHWrapper部分,看完再回来继续。)

Although it is designed to be a fast compiler (compiling is happening at runtime, you can't keep the user waiting) and a lot of trade offs, design decisions and heuristics are in place to make this happen, the JIT really doesn't look that different from a 'normal' compiler (where 'normal' means as per the Dragon Book ;)
JIT是一个速度很快的编译器(因为编译工作在程序运行时进行,你不能让用户等着),有多种手段保证它的速度:如交替换位、设计决策和启发式编译,但本质上看它和普通的编译器差不多。(这里的普通的编译器就是指龙书中说的那些)

Work is roughly divided into the following stages:
编译流程大致分为以下步骤:

1. Importing: In this stage, the input IL is transformed to the JIT's internal representation. Verification (if required) also happens here. A lot of communication happens back and forth between the JIT and the VM, as the JIT has to ask a lot of questions to the VM (for example, if the IL has a LDFLD instruction, it has to make sure the VM loads the class, and ask how it can access the field, directly? need a helper (for eg MarshalByRef classes), what helper?).
1、输入:这一步是将IL指令转化为JIT的内部表示,还包括指令确认(如果有必要的话)。JIT和VM之间会进行很多互通讯:JIT会向VM提问(例:假如IL指令中有LDFLD的话,它就要确认VM已经载入了必需的类,并询问怎么访问类中的信息,是直接访问还是需要帮助,比如使用MarshalByRef类。)

2. Morphing: Here, the compiler just applies a transformations to our internal structures in order to simplify or optimize the code. Things that happen here include detecting JIT intrinsics (functions the JIT has special knowledge about, such as Math.Sin(), which will end up being an fsin x86 instruction), constant propagation, inlining, etc...
2、变形:这时,编译器将代码转换为内部结构,目的是为简化或优化代码。具体的事项包括:检测JIT内部函数(指JIT特有的函数,比如Math.Sin()被转化为fsin这样的x86指令),常数值的传播(??),内联等等。

3. Flowgraph analysis: The JIT performs a traditional flowgraph analysis, to determine the liveness of variables and gen/kill sets, dominator information, loop detection, etc.... This information is used in all subsequent stages.
3、流程分析:接下来JIT进行传统的流程分析,以决定变量的生存期和“初始化/删除”的配对,控制点信息,loop循环等。这些信息接下来的步骤都会用到。

4. Optimization phase: In this stage, the heavyweight optimizations happen: Common Subexpression and Range Check Elimination, loop hoisting, etc...
4、代码阶段:这一步中,JIT进行最为烦重的优化工作:公共子表达式消元、范围检查、循环运算外提等

5. Register allocation: Registers are a scarce resource on x86. Operations performed on registers are generally faster than those done on memory, hence its important to make variables live in registers. This stage tries to select the most optimal placement for each variable. To do this it takes in account the number of uses of each variable in the method. It also makes a decision about what type of frame the method will use (EBP or ESP based, will the frame be double aligned), etc...
5、寄存器分配:在x86机器上寄存器明显不足。由于寄存器操作比内存操作速度快很多,所以应尽可能地让变量保存在寄存器中。JIT尽可能让变量保存在合适的寄存器中。要做到这点首先JIT要计算变量在各方法中使用的次数,它还要决定以哪种开式使用变量(比如基于ebp还esp定位,是否双字节对齐)等。

6. Code generation: We finally get to generate the x86 code. Here we take in account what processor we're generating code for, in order to choose the best idioms for it. GC and debugger information is also recorded here.
6、代码生成:终于到了生成x86代码的时候了。这里必需要考虑当前机器运行的处理器,并选择最适合该处理器的指令。垃圾收集(GC)和调试信息也是在这时候被记录。

7. Emit: This is the last stage, at this point we have the x86 code the method will use and information to generate the GC and debugger information. Everything will be packaged together and returned to the VM, which will then redirect control to the newly generated code.
7、Emit(用什么中文都不合适):这是最后一步,到这里我们已经得到了该方法的x86指令和相应的运行GC以及调试的信息。所有这些都将被送回至VM,然后VM将控制权转移给这些新生成的代码。

[培训]《安卓高级研修班(网课)》月薪三万计划,掌 握调试、分析还原ollvm、vmp的方法,定制art虚拟机自动化脱壳的方法

收藏
点赞0
打赏
分享
最新回复 (3)
雪    币: 85485
活跃值: (198795)
能力值: (RANK:10 )
在线值:
发帖
回帖
粉丝
linhanshi 2006-9-10 17:21
2
0
sustain.
雪    币: 235
活跃值: (40)
能力值: ( LV8,RANK:130 )
在线值:
发帖
回帖
粉丝
kkbing 3 2006-9-10 18:42
3
0
正需要这个
雪    币: 333
活跃值: (11)
能力值: ( LV12,RANK:770 )
在线值:
发帖
回帖
粉丝
jdxyw 19 2006-9-10 22:15
4
0
游客
登录 | 注册 方可回帖
返回