将 .NET 表达式树链接到新程序集中
我正在尝试编写我自己的玩具 My Toy Language -> MSIL 编译器,以便更好地了解编译器的工作原理。我的解析和词法分析工作正常,我已经构建了表达式树并使用 System.Linq.Expressions 表达式树 API,我有一个工作解释器。现在我想发出一些真正的 MSIL 程序集。
问题是,我不知道如何实际构建这些程序集。 MethodBuilder 类仅接受原始 MSIL 方法体,所以我必须获取表达式树的原始 MSIL。调用 Expression.Compile() 返回一个工作委托,但我没有能够获取其底层 MSIL。调用 MethodInfo.GetMethodBody() 会引发 InvalidOperationException因为它没有在特定的子类中实现。
如何将该代表链接到新程序集中?
I'm trying to write my own toy My Toy Language -> MSIL compiler in order to get a better understanding of how compilers work. I got the parsing and lexing working, I have built the expression trees and using the System.Linq.Expressions expression tree API, I have a working interpreter. Now I would like to emit some real MSIL assemblies.
The problem is, I can't figure out how to actually build these assemblies. The MethodBuilder class only accepts raw MSIL method bodies, so I have to get the raw MSIL of my expression tree. Calling Expression.Compile() returns a working delegate but I'm not able to get its underlying MSIL. Calling MethodInfo.GetMethodBody() throws an InvalidOperationException since it's not implemented in that specific child class.
How can I link that delegate into a new assembly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
刚刚找到了。 LambdaExpression 的 DLR 版本公开了一个 CompileToMethod 方法,该方法正是我所需要的。
Just found it. The DLR version of LambdaExpression exposes a CompileToMethod method which does exactly what I need.
为了发出原始 IL,您需要定义自己的 AST。您需要获取AssemblyBuilder,然后获取ModuleBuilder,然后您可以定义模块级方法,或者获取新的TypeBuilder和现在的MethodBuilder来定义类级方法。
你说你已经有了词法分析器和解析器。这意味着您能够构建 AST。因此,只需浏览已解析的表达式并发出您的 IL。
即使您获得生成的(通过编译)代码,您也无法用它做一些有用的事情,因为生成的代码取决于基础设施。例如,如果您需要编译闭包,那么您应该创建类或其他存储词汇变量等(例如需要在.net中使用异常的非词汇控制传输)
In order to emit raw IL you need to define your own AST. You need to Get AssemblyBuilder then ModuleBuilder and then you can define module-level method or get new TypeBuilder and now MethodBuilder to define class-level method.
You said that you already have lexer and parser. that means you able to build AST. So just walk through the parsed expressions and emit your IL.
Even if you get generated(by Compile) code you'll not be able to do something useful with it since generated code depends on infrastructure. For example if you need to compile closures then you should create class or other store fo lexical variables and so on(like non lexical control transfer which require to use Exceptions in .net)