Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 758
user624939 Avatar asked Feb 20 '11 02:02

user624939


People also ask

What is LLVM IR code?

LLVM can provide the middle layers of a complete compiler system, taking intermediate representation (IR) code from a compiler and emitting an optimized IR. This new IR can then be converted and linked into machine-dependent assembly language code for a target platform.

Is LLVM IR cross platform?

LLVM IR can be cross-platform, with the obvious exceptions others have listed. However, that does not mean Clang generates cross-platform code. As you note, the preprocessor is almost universally used to only pass parts of the code to the C/C++ compiler, depending on the platform.

Does GCC have an IR?

The data structure that the compiler translates your source code into is called an Intermediate Representation (IR), and gcc has three of them! This part of the compiler that parses the source code into IR is known as the front end.

Does GCC use LLVM?

The llvm-gcc command is the LLVM C front end. It is a modified version of gcc that compiles C/ObjC programs into native objects, LLVM bitcode or LLVM assembly language, depending upon the options. By default, llvm-gcc compiles to native objects just like GCC does.


1 Answers

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 .
    
like image 157
Alexander77 Avatar answered Sep 22 '22 05:09

Alexander77