编写.net - IL或Bytecode的编译器? [英] Writing a Compiler for .net - IL or Bytecode?

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

问题描述

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

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编译它,这工作。但是我想知道我是否应该更深一层,直接写字节码?

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?

推荐答案

为什么不使用< =http://msdn.microsoft.com/en-us/library/3y322t50.aspx =nofollow noreferrer> 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.

链接:

  • 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 - IL或Bytecode的编译器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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