IL2C - A translator for ECMA-335 CIL/MSIL to C language.
Latest commit 6c6184a Dec 7, 2018

README.md

IL2C - A translator for ECMA-335 CIL/MSIL to C language.

Provider Branch Build
AppVeyor master AppVeyor (.NET 4.5 / .NET Core 2.0) AppVeyor tests
Build Stats
AppVeyor devel AppVeyor (.NET 4.5 / .NET Core 2.0) AppVeyor tests
Build Stats

What's this?

  • IL2C is a translator (transpiler) for ECMA-335 CIL/MSIL to C language.

  • We're aiming for:

    • Better predictability for runtime costs, better human readability for the IL2C translated C source code.
    • Very tiny footprint requirements, we are thinking about how fit between tiny embedded system and large system with many resources.
    • Better code/runtime portability, minimum requirements are only C99 compiler.
    • Better interoperabilities for exist C libraries, we can use standard .NET interop technics (likely P/Invoke.)
    • Contains seamless building system for major C toolkits, for example: CMake system, Arduino IDE, VC++ ...

Simple hello-world like code

Original C# source code:

public static class HelloWorld
{
    public static void Main()
    {
        Console.WriteLine("Hello world with IL2C!");
    }
}

Translated to C source code (all comments are stripped):

IL2C_CONST_STRING(string0__, L"Hello world with IL2C!");

void HelloWorld_Main()
{
    struct
    {
        uint8_t objRefCount__;
        uint8_t objRefRefCount__;
        IL2C_EXECUTION_FRAME* pNext__;
        System_String* stack0_0__;
    } frame__ = { 1, 0 };
    il2c_link_execution_frame(&frame__);

    frame__.stack0_0__ = string0__;
    System_Console_WriteLine_10(frame__.stack0_0__);
    il2c_unlink_execution_frame(&frame__);
    return;
}

View with comments and other sample translation results (contains complex results), see this page.

How to beginning

The IL2C current status is experimental. Contributions are welcome but there's nothing the official guide documents. This is first step for use the IL2C:

  1. Open the il2c.sln by Visual Studio 2017. Your environment requires enabling the C#, VC++ and NUnit3 vsix addin.
  2. Build with "Debug - AnyCPU" configuration.
  3. If didn't show any errors, kick starts unit test at the Test Explorer (Run All).
  4. Unit tests need long time for first time execution. These tests are running with the gcc by automated downloads mingw platform.
  5. All test passed, you are ready to hacks!

Note

  • You have to know the unit tests compared and verified results both executed on the .NET CLR and the gcc compiled native code.
  • I asked for you have to see in "tests/IL2C.Core.Test.Target project". It has a lot of unit test code. And if unit tests are success, you can find translated results in "tests/IL2C.Core.Test.Fixture project" subfolder "bin/Debug/net462."
  • If you know the CI engineering, you can get more information from the "appveyor.yml" file.
  • Currently IL2C contains the VC++ project file in the folder because it's better debugging mates. The "IL2C.Runtime.vcxproj" project file DOESN'T REQUIRE for any building (manually and CI buildings.)
  • If you wanna the IL2C internals, try to read Making archive IL2C #6-55: dotNET 600 2018 session slide

Overall status

These list are auto-generated by unit test.

These list are manually maintanance (and maybe older...)

Support basic types

Issue Status Milestone
Value types Partial supported M3
Class types Partial supported
Managed reference Partial supported M3
Unmanaged pointer
Enum types Partial supported
Delegate types Partial supported
Interface types Partial supported
Nested types
Class inherits Partial supported
Interface implements Partial supported
Array types Partial supported
Multi dimensional array types
Closed generic types
Open generic types (not support) -

Support type members

Issue Status Milestone
Static field Partial supported M3
Instance field Partial supported M3
Static method Partial supported M3
Instance method Partial supported M3
Virtual method (class) Supported
Virtual method (interface) Partial supported
Interop method (extern static) Partial supported
Internal method (internal call)
Constructor Partial supported
Constructor (ValueType) Partial supported M3
Type initializer
Static property
Instance property
Static event
Instance event

Support metadata handling

Issue Status Milestone
Value type scopes Partial supported M3
Class type scopes Partial supported
Enum type scopes
Delegate type scopes
Interface type scopes Partial supported
Nested type scopes
Field scopes Partial supported M3
Method scopes Partial supported M3
Property scopes
Event scopes
Can handle reflection (not support) -

Support runtime relates

Issue Status Milestone
Referenced instance handler Partial supported
Value type boxed instance handler Partial supported
Collect unused instance (GC) Partial supported
Heap compaction (GC)
Exception throw and handles Partial supported
Async exception throws Partial supported
Monitor lock features
Platform invoke (P/Invoke) Partial supported
Can handle unsafe pointers
Can handle marshaling features (Marshal)

Application lifecycle managements

Issue Status Milestone
Basic commandline driver Partial supported M2
MSBuild (old/Task handler)
MSBuild (new/.NET Core CLI)
Platform independent core library
Translator platform on .NET 4.5 Supported M1
Translator platform on .NET Core 1 (not support) -
Translator platform on .NET Core 2 Supported
Translator platform on mono
Support C99 compilers Supported M1
Support non C99 (ANSI) compilers Supported
Support better C code output via C++ symbol declarations
Support continuous integrations Partial supported

License

Under Apache v2.

Related informations

Took pictures on design process

  • #6-6: Near milestones

    #6-6: Near milestones

  • #6-14: Data flow analysis

    #6-14: Data flow analysis

  • Milestone 1: Tested on VC++ and we checked how C compiler's optimizer work.

    Milestone 1: Tested on VC++ and we checked how C compiler's optimizer work

  • #6-48: How works mark-and-sweep garbage collection on translated code.

    #6-48: How works mark-and-sweep garbage collection on translated code

  • How to do overload/override/virtual method symbol calculus.

    How to do overload/override/virtual method symbol calculus

  • How to translate exception handlers when combined the local unwind and global unwind.

    How to translate exception handlers when combined the local unwind and global unwind

  • This is the strcuture graph for the exection-frame and exception-frame.

    This is the strcuture graph for the exection-frame and exception-frame