如何仿制药获得由JIT编译器编译? [英] How do generics get compiled by the JIT compiler?

查看:130
本文介绍了如何仿制药获得由JIT编译器编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道泛型编译通过JIT(如一切),而相比之下,当你编译code生成模板。结果
问题是,新的通用类型可以在运行时通过使用反射来创建。结果
这当然会影响到通用的约束。其中已通过语义分析程序。

有人能解释这是怎么处理的?而到底发生了什么?结果
 (无论是code生成和语义检查)


解决方案

我建议在C#,Java的,和C阅读 ++泛型:安德斯Hejlsberg为一个会话


  

QN 1.如何仿制药获得通过编译
  JIT编译器?


这是采访内容:


  

安德斯Hejlsberg为:[...]
  在CLR [公共语言运行库]
  当您编译列表,或其他任何
  泛型类型,它编译成IL
  [中间语言]和元数据
  就像任何普通型。在IL和
  元数据包含额外的
  虽然知道有信息的类型
  参数,当然,但在
  原则上,一个泛型类型编译
  刚的方式,任何其它类型的将
  编译。在运行时,当你的
  应用使得其首次引用
  到列表中,系统将查看
  如果有人已经问
  列表与LT; INT> 。如果不是,它馈入
  JIT的IL和元数据列表< T>
  并且类型参int。该JITer,
  在JIT编译的IL,也过程
  替换类型参数。


  
  

[...]


  
  

现在,我们然后做是所有类型
  实例是价值
  类型,如列表< INT>中列出<长>中
  清单<双>中列出<浮动>
- 我们创造
  可执行本地的独特副本
  code。因此,列表与LT; INT> 都有自己的code。
  列表与LT;长> 都有自己的code。
  列表与LT;浮动> 都有自己的code。对全部
  我们分享code引用类型,
  因为他们正在重新presentationally
  相同。这只是指针。




  

QN 2的事情是,新的通用类型
  可在运行时通过创建
  反射。这当然可以影响
  通用的约束。哪一个
  已通过语义分析程序。
  有人能解释这是怎么回事
  如何处理?


从本质上讲,IL保留了泛型类型的高级视图,它允许CLR检查在运行时就像C#编译器为动态构建泛型类型的约束在C#源静态构造函数的类型可能会做 - code在编译时间

这里的另一个片段(重点煤矿):


  

安德斯Hejlsberg为:[...]
  有了一个约束,可以说吊
  动态检查你的code和
  有它是可核查的在编译时
  或加载时间。
当你说ķ必须
  实现IComparable,一对夫妇
  事情发生。在K型的任意值,
  您现在可以直接访问
  接口方法不打石膏,
  因为语义在程序
  它保证它将实现
  该接口。每当你尝试
  创建该类型的一个实例,
  编译器会检查任何类型
  你给作为钾的说法工具
  IComparable的,否则你会得到一个编译
  时间误差。 或如果你有这样做
  反思你会得到一个例外。


  
  

布鲁斯埃克尔:你说的编译器和
  运行。


  
  

安德斯Hejlsberg为:编译器检查
  它,但你也可以这样做的
  运行时与反射,然后
  系统会检查它。
正如我以前说过,
  任何你可以在编译时做的,
  你也可以做在与运行
  反思。


I know that generics are compiled by JIT (like everything else), in contrast to templates that are generated when you compile the code.
The thing is that new generic types can be created in runtime by using reflection.
Which can of course affect the generic's constraints. Which already passed the semantic parser.

Can someone explain how this is handled ? And what exactly happens ?
(Both the code generation and semantic check)

解决方案

I recommend reading Generics in C#, Java, and C++: A Conversation with Anders Hejlsberg.

Qn 1. How do generics get compiled by the JIT compiler?

From the interview:

Anders Hejlsberg: [...] In the CLR [Common Language Runtime], when you compile List, or any other generic type, it compiles down to IL [Intermediate Language] and metadata just like any normal type. The IL and metadata contains additional information that knows there's a type parameter, of course, but in principle, a generic type compiles just the way that any other type would compile. At runtime, when your application makes its first reference to List, the system looks to see if anyone already asked for List<int>. If not, it feeds into the JIT the IL and metadata for List<T> and the type argument int. The JITer, in the process of JITing the IL, also substitutes the type parameter.

[...]

Now, what we then do is for all type instantiations that are value types—such as List<int>, List<long>, List<double>, List<float>—we create a unique copy of the executable native code. So List<int> gets its own code. List<long> gets its own code. List<float>gets its own code. For all reference types we share the code, because they are representationally identical. It's just pointers.


Qn 2. The thing is that new generic types can be created in runtime by using reflection. Which can of course affect the generic's constraints. Which already passed the semantic parser. Can someone explain how this is handled?

Essentially, IL retains a high-level view of generic types, which allows the CLR to check constraints for 'dynamically constructed' generic types at run-time just like the C# compiler might do for 'statically constructed' types in C# source-code at compile-time.

Here's another snippet (emphasis mine):

Anders Hejlsberg: [...] With a constraint, you can hoist that dynamic check out of your code and have it be verifiable at compile time or load time. When you say K must implement IComparable, a couple of things happen. On any value of type K, you can now directly access the interface methods without a cast, because semantically in the program it's guaranteed that it will implement that interface. Whenever you try and create an instantiation of that type, the compiler will check that any type you give as the K argument implements IComparable, or else you get a compile time error. Or if you're doing it with reflection you get an exception.

Bruce Eckel: You said the compiler and the runtime.

Anders Hejlsberg: The compiler checks it, but you could also be doing it at runtime with reflection, and then the system checks it. As I said before, anything you can do at compile time, you can also do at runtime with reflection.

这篇关于如何仿制药获得由JIT编译器编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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