加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – .net中的IL&stack实现?

发布时间:2020-12-15 04:04:55 所属栏目:百科 来源:网络整理
导读:我写了一个简单的程序来检查IL的工作原理: void Main(){ int a=5; int b=6; if (ab) Console.Write("333"); Console.ReadLine();} IL: IL_0000: ldc.i4.5 IL_0001: stloc.0 IL_0002: ldc.i4.6 IL_0003: stloc.1 IL_0004: ldloc.0 IL_0005: ldloc.1 IL_0006
我写了一个简单的程序来检查IL的工作原理:
void Main()
{

 int a=5;
 int b=6;
 if (a<b) Console.Write("333");
 Console.ReadLine();
}

IL:

IL_0000:  ldc.i4.5    
IL_0001:  stloc.0     
IL_0002:  ldc.i4.6    
IL_0003:  stloc.1     
IL_0004:  ldloc.0     
IL_0005:  ldloc.1     
IL_0006:  bge.s       IL_0012
IL_0008:  ldstr       "333"
IL_000D:  call        System.Console.Write
IL_0012:  call        System.Console.ReadLine

我正在努力了解实施的效率:

>在第1行(IL代码),它将值5推送到堆栈(4字节,即int32)
>在行#2(IL代码),它从堆栈POPs到一个局部变量.

接下来的两条线也一样.

然后,它将这些局部变量加载到堆栈上,然后它评估bge.s.

问题#1

为什么他将本地变量加载到堆栈?这些值已经在堆栈中了.但他把他们放在了一个局部变量中.不是浪费吗?

我的意思是,为什么代码不能像:

IL_0000:  ldc.i4.5
IL_0001:  ldc.i4.6    
IL_0002:  bge.s       IL_0004
IL_0003:  ldstr       "333"
IL_0004:  call        System.Console.Write
IL_0005:  call        System.Console.ReadLine

我的代码示例只有5行代码.大约有50,000,000行代码? IL会发出大量的额外代码

问题#2

看代码地址:

> IL_0009地址在哪里?不是应该是连续的吗?

附: Im与发布模式下的Optimize标志

解决方法

Why does he loads the local variables to the stack? The values has already been in the stack. But he poped them in order to put them in a local variables. Isn’t it a waste?

浪费什么?你必须记住,IL(通常)不是按原样执行的,它由JIT编译器重新编译,JIT编译器执行大部分的优化.使用“中间语言”的一个要点就是可以在一个地方实现优化:JIT编译器和每种语言(C#,VB.NET,F#,…)不必再重新实现它们.这是由Eric Lippert在他的文章Why IL?中解释的

Where is the IL_0009 address? Isn’t it supposed to be sequential?

我们来看看ldstr指令的规范(从ECMA-335):

III.4.16 ldstr – load a literal string

Format: 72 <T> […]

The ldstr instruction pushes a new string object representing the literal stored in the metadata as string (which is a string literal).

对上述元数据的引用和< T>意味着指令的字节72后面是元数据令牌,它指向包含字符串的表.这样的标记有多大?从同一文件的第III.1.9节:

Many CIL instructions are followed by a “metadata token”. This is a 4-byte value,that specifies a row in a metadata table […]

因此,在您的情况下,指令的字节72位于地址0008,令牌(0x70000001,在这种情况下,0x70字节表示用户字符串表)在地址为0009至000C.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读