如何将 CIL 转换为 LLVM IR?

发布于 2024-10-18 05:59:21 字数 142 浏览 5 评论 0原文

我想将 C# 编译为 LLVM IR。所以我认为将编译的 CIL 翻译为 LLVM IR 是我可以尝试的一种方法。

我可以使用一些工具,例如 vmkit 和 mono-llvm。

有人使用这个工具吗?或者如何将 CIL 转换为 LLVM?

I want to compile C# to LLVM IR. So I think translate compiled CIL to LLVM IR is one way I can try.

There are some tools I can use such as vmkit and mono-llvm.

Is anybody using this tools? Or how can I translate CIL to LLVM?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不寐倦长更 2024-10-25 05:59:21

要从 CIL 获取 LLVM IR 代码,您需要使用工具 il2bc(也称为 C# Native),您可以从 http: //csnative.codeplex.com/

您只需要执行一些简单的步骤。

Il2Bc.exe <Your DLL>.dll

如果您想从中生成可执行文件,则需要编译生成的.ll 文件(LLVM IR 代码)。

例如,您有“Hello World”应用程序

  1. 编译它(它将生成一个 helloworld.ll 文件)

    Il2Bc.exe helloworld.cs /corelib:CoreLib.dll
    
  2. 为核心库生成 LLVM IR 文件(它将生成 corelib.ll 文件)

    Il2Bc.exe CoreLib.dll
    
  3. 您需要生成一个EXE文件(它将生成一个.EXE文件):

    llc -filetype=obj -mtriple=i686-w64-mingw32 CoreLib.ll
    llc -filetype=obj -mtriple=i686-w64-mingw32 helloworld.ll
    g++ -o helloworld.exe helloworld.obj CoreLib.obj -lstdc++ -lgc-lib -march=i686 -L 。
    

To get LLVM IR code from CIL you need to use the tool il2bc (other name C# Native) which you can download from http://csnative.codeplex.com/.

You just need to perform some simple steps.

Il2Bc.exe <Your DLL>.dll

If you want to generate an executable from it, you need to compile the generated .ll file (LLVM IR Code).

For example, you have your "Hello World" app

  1. Compile it (it will generate a helloworld.ll file)

    Il2Bc.exe helloworld.cs /corelib:CoreLib.dll
    
  2. Generate LLVM IR file for the core library (it will generate corelib.ll file)

    Il2Bc.exe CoreLib.dll
    
  3. You need to generate an EXE file (it will generate a .EXE file):

    llc -filetype=obj -mtriple=i686-w64-mingw32 CoreLib.ll
    llc -filetype=obj -mtriple=i686-w64-mingw32 helloworld.ll
    g++ -o helloworld.exe helloworld.obj CoreLib.obj -lstdc++ -lgc-lib -march=i686 -L .
    
街角迷惘 2024-10-25 05:59:21

答案取决于您的目标。为什么要将 C# 翻译成 LLVM?

VMKit 被设计为构建虚拟机实现的框架。我相信它曾经对 CLR 提供过一些支持,但这种支持后来停滞不前,转而支持其 JVM 实现。其目的是从头开始构建虚拟机。

Mono-llvm 是一个用 LLVM 后端替换 mono JIT 后端的项目。它的目标是提高 Mono 上 JIT 代码的性能。

如果你的目标是使用 Mono,具有更好的性能,mono-llvm 是一个不错的选择。

如果您想从头开始构建整个虚拟机,那么 VMKit 可能会起作用。

如果您只是想实现一个提前编译器来生成没有 CLR 依赖项的可执行文件,您可以从以下位置下载 LLVM 核心库:

http://llvm.org/

基本上,它将 CIL 转换为 LLVM IR 的文本表示,然后使用 LLVM API 将其编译为本机机器代码。

我不知道LLVM是否会为你生成目标文件。您可能需要自己生成它们,但这非常简单。它基本上只是将机器代码填充到数据结构中,构建字符串、节和符号表,然后将所有内容序列化到磁盘。

The answer depends on your goals. Why do you want to translate C# to LLVM?

VMKit was designed as a framework for building virtual machine implementations. I believe it had some support for the CLR at one point, but that support since stagnated in favor of its JVM implementation. Its purpose is to make building a VM from scratch.

Mono-llvm is a project that replaces the mono JIT backend with an LLVM back end. It's goal is to improve the performance of JITed code on Mono.

If your goal is to use Mono, with better performance, mono-llvm is a good choice.

If you want to build an entire VM from scratch, then VMKit might work.

If you are just looking to implement an ahead-of-time compiler that produces executables with no CLR dependencies, you can just download the LLVM core libraries from:

http://llvm.org/

Basically it would translate the CIL into a textual representation of LLVM IR and then use the LLVM APIs to compile it to native machine code.

I don't know if LLVM will generate object files for you. You may have to generate them yourself, but that's pretty easy. It's basically just stuffing the machine code into a data structure, building up string, section, and symbol tables, and then serializing everything to disk.

唱一曲作罢 2024-10-25 05:59:21

我想我理解这个问题是你想使用 LLVM IR 就像 GCC 可以使用 gcj 编译 Java 一样?

LLVM 可以选择直接从您使用的任何前端输出 CIL(因此理论上您可以将 C/C++ 转换为 CIL)。以下命令选项:

llc -march=msil

将从(理论上)任何支持的 LLVM 前端输出 CIL。

从 C# 或 CIL 到 LLVM IR 的转变尚未完成(或至少完成)。您需要一个 C# 前端。

VMKit 有某种 C# 前端脚手架。支持功能从未完成,人们的兴趣也随之消退。他们已经转向仅支持 Java。您可以尝试他们的源存储库,看看他们早期 C# 工作的任何残余是否可以重新加工成完整的 C# 前端。

另请注意,您可以使用 C# 编写自己的C# 到 LLVM IR 编译器(使用 Mono 或其他)并使用 P/Invoke 调用LLVM 库并创建 LLVM IR。有一些很好的信息,例如 Writing使用 Flex、Bison 和 LLVM 的您自己的玩具编译器

现在这个领域也变得有趣了,因为编译器即服务(Roslyn)项目已经有了它的前几个 CTP 版本,Mono 也有它的 Mono.CSharp 项目。不过我认为 Roslyn 的功能更丰富一些。

I think I understand the question to be that you want to use LLVM IR in the same way that the GCC can compile Java using gcj?

The LLVM had an option to output CIL directly from whatever front end you used (So in theory you could do C/C++ to CIL). The following command options:

llc -march=msil

would output CIL from (in theory) any supported LLVM Front-End.

Going from C# or CIL to LLVM IR hasn't been done yet (or at least finished). You'd need a C# front-end.

VMKit had some kind of C# front end scaffolding. Support was never feature complete and interest has since faded. They've moved to just supporting Java. You might try their source repository and see if there are any remnants of their early C# work can be reworked into a full C# frontend.

Also note that you can write your own C# to LLVM IR compiler in C# (using Mono or whatever) and use P/Invoke to call into LLVM libraries and create LLVM IR. There are some good information out there such as Writing Your Own Toy Compiler Using Flex, Bison and LLVM.

This area is also getting interesting now that the compiler as a service (Roslyn) project has had its first couple of CTP releases, and Mono has its Mono.CSharp project. Though I think Roslyn is a bit more feature-rich.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文