How to translate CIL to LLVM IL?

I want to compile C # in LLVM IL. Therefore, I think that translating the compiled CIL to LLVM IL is one of the ways I can try.

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

Does anyone use these tools? Or how can I translate CIL to LLVM?

+6
source share
3 answers

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

VMKit was developed as the basis for creating virtual machine implementations. I believe that at some point he supported the CLR, but this support was stagnant in favor of implementing the JVM. Its goal is to create a virtual virtual machine from scratch.

Mono-llvm is a project that replaces the mono JIT backend with the rear end of LLVM. The goal is to improve the performance of JIT code on Mono.

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

If you want to create an entire virtual machine from scratch, then VMKit can work.

If you just want to implement a time-consuming compiler that creates executable files without CLR dependencies, you can simply download the main LLVM libraries:

http://llvm.org/

It basically translates CIL into a textual representation of LLVM IR, and then uses the LLVM API to compile it into native machine code.

I do not know if LLVM will generate object files for you. You may need to generate them yourself, but it's pretty easy. It is basically just stuffing machine code into a data structure, creating strings, sections, and character tables, and then serializing everything to disk.

+4
source

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

LLVM was able to derive CIL directly from any interface you use (so theoretically you could do C / C ++ for CIL). The following command options:

llc -march=msil 

derives CIL from (theoretically) any supported LLVM Front-End.

The transition from C # or CIL to LLVM IR is not yet complete (or at least finished). You will need a C # interface.

VMKit had some kind of scaffolding under C #. Support has never been complete, and interest has since disappeared. They switched to Java support. You can try the original repository and see if there are any remnants of their early work with C #, you can rework in the full C # interface.

Also note that you can write your own C # in LLVM IR compiler in C # (using Mono or whatever) and use P / Invoke to invoke LLVM libraries and create LLVM IR. There is some good information like Writing your own toy compiler using Flex, Bison, and LLVM .

This area is also becoming interesting now that when the compiler as a service ( Roslyn ), the project had its first pair of CTP releases and Mono has its own Mono.CSharp project. Although I think Roslyn is a bit more functional.

+4
source

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

You just need to follow 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 the application "Hello World"

  • Compile it (it will generate the helloworld.ll file)

     Il2Bc.exe helloworld.cs /corelib:CoreLib.dll 
  • Create an LLVM IR file for the main library (it will generate the corelib.ll file)

     Il2Bc.exe CoreLib.dll 
  • You need to generate the EXE file (it will generate the .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 . 
+3
source

All Articles