Git Product home page Git Product logo

il2c's Introduction

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

Intermediate language everywhere!

What's this?

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

  • We're aiming for:

    • Better predictability of runtime costs
      Better human readability of C source code translated by IL2C.
    • Very tiny footprint requirements
      We're thinking about how to fit from large system with many resources to tiny embedded system. (KB order for the non-OSes system)
    • Better code/runtime portability
      Minimum requirement is only C99 compiler. The runtime minimum requires only the heap, CAS instructions, (POSIX) signal and setjmp/longjmp. Additional better feature is threading API (Win32, pthreads and FreeRTOS.)
    • Better interoperabilities for existed C libraries
      You can use the standard .NET interop technics (like P/Invoke.)
    • Containing seamless building systems 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
    {
        const IL2C_EXECUTION_FRAME* pNext__;
        const uint16_t objRefCount__;
        const uint16_t valueCount__;
        System_String* stack0_0__;
    } frame__ = { NULL, 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 / other sample translation results (contain complex results)

Getting started

IL2C current status is experimental, read a simple "Getting started" for first step.

If you need understanding deep knowledge for IL2C, see "Inside IL2C" .

Project status

Following lists are auto-generated by unit test.

Supported features (old)

Packages

Packages master devel
IL2C.Build NuGet IL2C.Build MyGet IL2C.Build
IL2C.Interop NuGet IL2C.Interop MyGet IL2C.Interop
IL2C.Core NuGet IL2C.Core MyGet IL2C.Core
IL2C.Runtime NuGet IL2C.Runtime MyGet IL2C.Runtime
IL2C.Runtime.msvc NuGet IL2C.Runtime.msvc MyGet IL2C.Runtime.msvc
IL2C.Runtime.Arduino (Constructing) (Constructing)

Build status

Configuration master
Publish Azure pipelines (.NET 4.5 / .NET Core 2.0)
Debug Azure pipelines (.NET 4.5 / .NET Core 2.0) Azure pipelines tests
Release Azure pipelines (.NET 4.5 / .NET Core 2.0) Azure pipelines tests
Configuration devel
Publish Azure pipelines (.NET 4.5 / .NET Core 2.0)
Debug Azure pipelines (.NET 4.5 / .NET Core 2.0) Azure pipelines tests
Build Stats
Release Azure pipelines (.NET 4.5 / .NET Core 2.0) Azure pipelines tests
Build Stats

License

Under Apache v2.

Related information

Photos of design process

  • #6-6: Near milestones

    #6-6: Near milestones

  • #6-14: Data flow analysis

    #6-14: Data flow analysis

  • Milestone 1: Test on VC++ and check how C compiler's optimizer works.

    Milestone 1: Test on VC++ and check how C compiler's optimizer works

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

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

  • How overload/override/virtual method symbol calculus work.

    How overload/override/virtual method symbol calculus work

  • 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

il2c's People

Contributors

kekyo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

il2c's Issues

Support Thread features.

Basic features.

  • System.Threading.Thread
  • System.Threading.ThreadPool
  • Thread local storage
  • Collaborate GC (Giant lock)
  • Collaborate GC (Concurrent)

What support platforms

  • Windows (Native Win32)
  • Linux pthreads
  • Azure Sphere pthreads
  • FreeRTOS (M5Stack)
  • No-threading environment (UEFI)

Advanced features.

  • System.Threading.Tasks.Task
  • How optimize handling for Tasks.

Support delegate.

We have to think about how to implements thunk or thunk-like trampolines.

Support closed generic types.

Idea

  • Value Witness Table (came from swift)
  • Aggregate implementation for generic parameters are objref type.
    • How to analyze and fix implementation for partial objref arguments at generic parameters?

Improvement for evaluation stack handling.

    int32_t stack0_0;
    intptr_t stack0_1;
    int32_t stack1_0;
    intptr_t stack1_1;
    int32_t stack2_0;

    // Body...

Combined to:

    union {
        int32_t slot0;
        intptr_t slot1;
    } stack0;
    union {
        int32_t slot0;
        intptr_t slot1;
    } stack1;
    int32_t stack2_0;

    // Body...

If contained OR:

    union {
        int32_t slot0;
        intptr_t slot1;
    } stack0;
    union {
        int32_t slot0;
        intptr_t slot1;
    } stack1;
    System_String* stack1_2;   // Have to NOT combined into stack1.
    int32_t stack2_0;

    // Body...

Because GC's required trackable for OR.

Pros:

  • Reduce stack allocation.

Cons:

  • Bad readability.
  • C compiler maybe can't sense usage for variables, so will not draw optimize.

Support synchronization features.

Basic features

  • System.Threading.Interlocked
  • System.Threading.Monitor

Advanced features

  • System.Threading.WaitHandle
  • System.Threading.Mutex
  • System.Threading.Event
  • System.Threading.ReaderWriterLock

Symbol name collects difficult

var rawTypeName = declaredType
                .GetFullMemberName()
                .ManglingSymbolName();
var typeName = extractContext.GetCLanguageTypeName(
                declaredType,
                TypeNameFlags.Dereferenced);
  • Unify these collecter to simple way.

Occur "Build FAILED" error at building on .NET Core on Linux

Occur building failure at .NET Core on Linux.
I'm using following at Debian GNU/Linux sid:

https://docs.microsoft.com/en-us/dotnet/core/linux-prerequisites?tabs=netcore2x

kiwamu@casper:~/src/IL2C$ cat circle.yml 
version: 2
jobs:
  build:
    working_directory: /temp
    docker:
      - image: microsoft/dotnet:sdk
    environment:
      DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1
      DOTNET_CLI_TELEMETRY_OPTOUT: 1
    steps:
      - checkout
      - run: dotnet restore il2c.sln
      - run: dotnet build il2c.sln
kiwamu@casper:~/src/IL2C$ dotnet --version
2.1.3
kiwamu@casper:~/src/IL2C$ dotnet restore il2c.sln
/usr/share/dotnet/sdk/2.1.3/NuGet.targets(227,5): warning MSB3202: The project file "/home/kiwamu/src/IL2C/IL2C/IL2C.csproj" was not found. [/home/kiwamu/src/IL2C/il2c.sln]
/home/kiwamu/src/IL2C/IL2C/IL2C.csproj : warning NU1503: Skipping restore for project '/home/kiwamu/src/IL2C/IL2C/IL2C.csproj'. The project file may be invalid or missing targets required for restore. [/home/kiwamu/src/IL2C/il2c.sln]
Segmentation fault
kiwamu@casper:~/src/IL2C$   Restoring packages for /home/kiwamu/src/IL2C/IL2C.Core/IL2C.Core.csproj...
  Restoring packages for /home/kiwamu/src/IL2C/IL2C.Core.Tests/IL2C.Core.Tests.csproj...

kiwamu@casper:~/src/IL2C$ dotnet build il2c.sln
Microsoft (R) Build Engine version 15.5.179.9764 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

Segmentation fault
kiwamu@casper:~/src/IL2C$ /usr/share/dotnet/sdk/2.1.3/NuGet.targets(227,5): warning MSB3202: The project file "/home/kiwamu/src/IL2C/IL2C/IL2C.csproj" was not found. [/home/kiwamu/src/IL2C/il2c.sln]
/home/kiwamu/src/IL2C/IL2C/IL2C.csproj : warning NU1503: Skipping restore for project '/home/kiwamu/src/IL2C/IL2C/IL2C.csproj'. The project file may be invalid or missing targets required for restore. [/home/kiwamu/src/IL2C/il2c.sln]
  Restoring packages for /home/kiwamu/src/IL2C/IL2C.Core.Tests/IL2C.Core.Tests.csproj...
  Restoring packages for /home/kiwamu/src/IL2C/IL2C.Core/IL2C.Core.csproj...

Invalid flow analysis strategy at branch junction point.

Valid flow analysis pattern ex:

|  // P.1
|
* <--+  // Junction point, P.2
|    |
|    |
BR --+

Invalid ex:

|      |
| P.2  | P.1
v      |
* <----+  // Junction point, P.1 and P.2.
|
| P.1
v

Junction point already analyzed by P.1.
But no checked evaluation stack usage by P.2.

cc65 / c89 / c90 compiler support suggestions.

To support c89 with try catch you could base your output off "setjmp" && "longjmp".

#include <stdio.h>
#include <setjmp.h>

jmp_buf __threadExceptionBuff;

#define TRY switch(setjmp(__threadExceptionBuff)) { case 0: while(1) {
#define CATCH(x) break; case x:
#define FINALLY break; } default: {
#define TRY_END break; } }
#define THROW(x) longjmp(__threadExceptionBuff, x)

#define EXCEPTION (1)
#define NOT_IMPLEMENTED_EXCEPTION (2)

void Foo()
{
    THROW(NOT_IMPLEMENTED_EXCEPTION);
}

void main()
{
    TRY
    {
        printf("Start\n");
        Foo();
        printf("End\n");
    }
    CATCH (NOT_IMPLEMENTED_EXCEPTION)
    {
        printf("Catch Not Implemented Exception\n");
    }
    CATCH (EXCEPTION)
    {
        printf("Catch Exception\n");
    }
    FINALLY
    {
        printf("Finally\n");
    }

To support c89 local variable scoping rules. You could define all of local variables that live on the stack at the top of each block.
Example:

struct A
{
    int x, y;
};

void Foo()
{
    // all local stack objects within this scope get pre-defined up here at the top of the block
    A a;
    A a2;
    int i;

    // Now you can use them normally and this will compile in c89....
    for (i = 0; i != 1; ++i)
    {
        // all local stack objects within this scope get pre-defined up here at the top of the block
        A b;
        float t;
        // do stuff....
    }
}

I use the c65 compiler to test out portability: https://github.com/cc65/cc65

Support event member for generated by Roslyn.

The Roslyn generates event member with System.Threading.Interlocked.CompareExchange(...), so IL2C will fail because can't handle the generic method.
Will temporary (hacky) support it.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.