编写.net

编程入门 行业动态 更新时间:2024-10-21 13:11:49
本文介绍了编写 - IL或Bytecode的编译器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我目前正在深入的内部工作,这意味着IL。作为一个练习,我想为编译一个brainf..k编译器(是的,他们已经存在,但是为了学习的目的)。

I'm currently diving into the inner workings of , which means IL. As an exercise, I want to build a brainf..k compiler for (yes, they already exist, but as said it's for learning purposes).

时刻我只是写一些包含.IL的文本文件,并用ilasm编译它,这工作。但是我想知道我是否应该更深一层,直接写字节码?

For the moment I'm just writing some text files that contain .il and compile them with ilasm, which works. But I wonder if I could/should go one level deeper and write bytecode directly?

我的关心是编译一个EXE而不是ilasm我的Windows PE需要某种类型的Bytecode链接器,它会接受我的MSIL / CIL字节码并为其生成PE Stuff?

My "concern" is the Windows PE Stuff when compiling an EXE - instead of ilasm I would need some sort of Bytecode linker that would take my MSIL/CIL bytecode and generate the PE Stuff for it?

或者编译器只编译他们的语言到IL和执行ilasm?是否有一个托管版本,我可以从我的编译器调用/嵌入?

Or do compilers "only" compile their language to IL and execute ilasm? Is there a managed version of it that I can call/embed from my compiler?

推荐答案

为什么不使用 Reflection.Emit api以生成带有编译代码的内存中程序集,然后保存到磁盘?应该比写出.IL文件容易得多。

Why not simply use the Reflection.Emit api to produce a in-memory assembly with the compiled code and then save it to disk? Should be a lot easier than writing out .IL files.

链接:

  • 使用Reflection.Emit
  • ILGenerator.Emit方法
  • Using Reflection.Emit
  • ILGenerator.Emit Method

如果您想沿着这条路走,如果您在 SO ,你将得到如何定义一个动态程序集并将其保存到磁盘的例子。

If you want to go down this road, if you ask more specific questions here on SO you'll get plenty of example of how to define a dynamic assembly and save it to disk.

这里有一个例子:

using System; using System.Reflection.Emit; using System.Reflection; namespace SO2598958 { class Program { static void Main() { AssemblyBuilder asm = AppDomain.CurrentDomain.DefineDynamicAssembly( new AssemblyName("TestOutput"), AssemblyBuilderAccess.RunAndSave); ModuleBuilder mod = asm.DefineDynamicModule("TestOutput.exe", "TestOutput.exe"); TypeBuilder type = mod.Definetype("Program", TypeAttributes.Class); MethodBuilder main = type.DefineMethod("Main", MethodAttributes.Public | MethodAttributes.Static); ILGenerator il = main.GetILGenerator(); il.Emit(OpCodes.Ldstr, "Hello world!"); il.Emit(OpCodes.Call, typeof(Console).GetMethod("WriteLine", BindingFlags.Public | BindingFlags.Static, null, new Type[] { typeof(String) }, null)); il.Emit(OpCodes.Ret); type.CreateType(); asm.SetEntryPoint(main); asm.Save("TestOutput.exe"); } } }

您可以从这里下载测试解决方案文件。使用解决方案直接链接到zip文件此处。

You can download the test solution file from here. Direct link to zip file with solution here.

如果你首先编译并运行这个程序,它会在磁盘上产生一个名为TestOutput的新exe文件,命令有Hello World!打印在控制台上。

If you first compile and run this program, it'll produce a new exe file on disk, called TestOutput, which you can then execute in order to have "Hello World!" printed on the console.

更多推荐

编写.net

本文发布于:2023-11-15 05:32:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1591587.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!