可疑的IL产生 [英] Questionable IL Generated

查看:65
本文介绍了可疑的IL产生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在分析一些IL,而我恰巧是在我的代码中从一个非常简单的lambda函数生成的IL.原始代码如下所示:

I am mucking around with parsing some IL at the moment, and I just so happened upon the IL generated from a very simple lambda function in my code. The original code looks like this:

((User x) => x.Id == 2) // this is a lambda function from a Where call



从此C#代码生成的IL如下所示:



The IL generated from this C# code looks like so:

IL_0000: ldarg.0
IL_0001: callvirt instance int32 Utilities.Test.User::get_Id()
IL_0006: ldc.i4.2
IL_0007: ceq
IL_0009: stloc.0
IL_000a: br.s IL_000c  // originally "br.s 0"
IL_000c: ldloc.0
IL_000d: ret



我得到了大部分-它将User参数``x''加载到堆栈上,调用Id属性的getter将结果插在堆栈上,在堆栈上插两个,然后检查是否相等.

因此,"ceq"将此布尔结果放置到堆栈中.因此,这就是我从地址IL_0009开始读取的内容:



I get most of it - it loads the User parameter ''x'' onto the stack, calls the getter of the Id property chucks the result on the stack, chucks a two on the stack, then checks for equality.

So, ''ceq'' places this boolean result onto the stack. So this is what I read from addresses IL_0009 onwards:

IL_0009: pop the result of the ceq evaluation off the stack into local var 0
IL_000a: branch 0 instructions forward
IL_000c: load the local var 0 back onto the stack
IL_000d: return



我看不到指令9到C的相关性,看似毫无意义的分支0"调用是否正在做某些事情,而我在这里完全被误解了?分支的副作用在这里有用吗?

任何帮助表示赞赏:)

我一直在阅读本文,并以为它是本机代码而使自己发疯,我忘记了这些指令不是直接在CPU上执行的.因此,也许IL中的一个分支命令被JITter替换为一些x86指令,例如"jmp",该指令清除了ceq函数中设置的标志寄存器,这些标志寄存器从函数返回后可能会引起问题...就像它的''自行清理'',并具有跳跃的副作用.

真的不知道,我在这里抓着稻草...



I can''t see how instructions 9 through C are relevant, is there something that the seemingly pointless "branch 0" call is doing that I am completely misunderstanding here? Is there some side-effect of branching that is useful here?

Any help is appreciated :)

I keep reading this and catch myself thinking it''s native code, I forget these instructions aren''t executed directly on the CPU. So maybe a branch command in IL is replaced by the JITter with some x86 instruction like ''jmp'' that clears flags registers set in the ceq function that could cause problems after returning from the function... like it''s ''cleaning up after itself'' with the side-effects of a jump.

Really I have no idea, I''m clutching at straws here...

推荐答案

确认这是调试模式的产物,这是Release IL:

Confirmed that this was an artifact of debug mode, this is the Release IL:

IL_0000: ldarg.0
IL_0001: callvirt instance int32 Collabowrite.Utilities.Test.User::get_Id()
IL_0006: ldc.i4.2
IL_0007: ceq // no crazy instructions below!
IL_0009: ret



这无法确定是否方便断点,但目前看来,这似乎是最有可能的解释.



This is no confirmation of whether it was to facilitate breakpoints or otherwise, but at this point this seems like the most likely explanation.


这篇关于可疑的IL产生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆