写一个编译器.NET - IL或字节code? [英] Writing a Compiler for .net - IL or Bytecode?

查看:223
本文介绍了写一个编译器.NET - IL或字节code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前潜入.NET的内部运作,这意味着IL。作为练习,我想建立一个brainf..k编译器.NET(是的,他们已经存在,但表示这是出于学习目的)。

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

有关的那一刻,我只是写了包含.il内和ILASM,其中工程编译它们一些文本文件。但我不知道如果我可以/应该去更深一层和写字节code直接?

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?

我的关注是Windows PE东西编译的EXE时 - 而不是ILASM我将需要某种形式的字节code连接器,将采取我的MSIL / CIL字节code和生成PE工具和它?

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产生一个内存组件编译code,然后将其保存到磁盘上?应该比写出。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.

链接:

  • 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");
        }
    }
}

<打击> 您可以下载测试解决方案文件从这里。在这里解决方案直接链接到压缩文件

如果你先编译并运行这个程序,它会产生磁盘上的一个新的EXE文件,名为TestOutput,然后你就可以以具有执行的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 - IL或字节code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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