user624939
user624939

Reputation: 61

How do I translate CIL to LLVM IR?

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?

Upvotes: 6

Views: 8006

Answers (3)

Alexander77
Alexander77

Reputation: 132

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 .
    

Upvotes: 5

techsaint
techsaint

Reputation: 772

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.

Upvotes: 4

Scott Wisniewski
Scott Wisniewski

Reputation: 25061

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.

Upvotes: 4

Related Questions