how about converting C# into C? (or C++)

Coordinator
Feb 4, 2015 at 2:36 PM
I am thinking about writing additional converter which will convert into C/C++, would it be more beneficial then just having conversion into LLVM?
Feb 4, 2015 at 3:44 PM
Seems like a great idea, I would love it. However, is this realistically possible? Are you going to build a garbage collector with the C++ generated code?
Coordinator
Feb 4, 2015 at 4:47 PM
jasonjg wrote:
Seems like a great idea, I would love it. However, is this realistically possible? Are you going to build a garbage collector with the C++ generated code?
it is already done with LLVM IR which is just other view of C/C++
Feb 4, 2015 at 5:05 PM
Sounds great, then I think you should definitely proceed. It would be really useful.
Feb 13, 2015 at 1:16 PM
Do you have something to share in this front?
Coordinator
Feb 13, 2015 at 5:57 PM
no, I have not started it.
Coordinator
Mar 6, 2015 at 11:48 AM
ok, just started prototyping
Coordinator
Mar 17, 2015 at 12:46 PM
soon I will be able to show something
Coordinator
Apr 1, 2015 at 11:24 AM
meet IL2C app, Converting MSIL to C backend
Coordinator
Apr 11, 2015 at 1:15 PM
any feedback on IL2C?
Apr 14, 2015 at 9:15 PM
Hi AlexDev,
I've tried IL2C, but apart from the basic Hello World, most code i try just hangs at runtime. Give for example the following simple benchmark (NSieve) from CrossNet

/*
CrossNet - C# Benchmark
*/

using System;
using System.Collections.Generic;
using System.Text;

namespace CSharpBenchmark._Benchmark
{
public static class NSieveTest
{
    static int nsieve(int m, bool[] isPrime)
    {
        for (int i = 2; i <= m; i++)
            isPrime[i] = true;
        int count = 0;

        for (int i = 2; i <= m; i++)
        {
            if (isPrime[i])
            {
                for (int k = i + i; k <= m; k += i)
                    isPrime[k] = false;
                count++;
            }
        }
        return count;
    }


    public static void Main()
    {
        Console.WriteLine(Test (700));
    }

    public static bool Test(int N)
    {
        for (int i = 0; i < N; ++i)
        {
            const int m = 1 * 1024 * 1024;      // 1 MB so it mostly stays in cache
                                                //  We are benchmarking algorithm, not memory accesses ;)
            bool[] flags = new bool[m + 1];
            int numPrimes = nsieve(m, flags);
            if (numPrimes != 82025)
            {
                return (false);
            }
        }
        return (true);
    }
}
}

Compiling this works well, but the program segfaults on my windows VM.
Coordinator
Apr 14, 2015 at 10:50 PM
Yes, I see, I will debug it, BTW Going to update it soon (after testing IL2C on MSCorlib).
Coordinator
Apr 15, 2015 at 10:20 AM
afrogeek wrote:
Hi AlexDev,
I've tried IL2C, but apart from the basic Hello World, most code i try just hangs at runtime. Give for example the following simple benchmark (NSieve) from CrossNet


Compiling this works well, but the program segfaults on my windows VM.
afrogeek, Thank you for your feedback. take the latest. I have fixed your issues.
Apr 17, 2015 at 12:13 AM
Hi AlexDev

csnative is a great project and for me it looks really promising!

I've also tested the last version of il2c with the helloworld.cs file, just to see how it works. It's awesome!
I have some doubts, let me ask them here.

I would like to know what are the language features supported in il2c.exe in your last beta (05032015)
Did you publish any comprehensive list of all supported language features?

I have had a look at Corlib but it seems like a work-in-progress standard-lib. Why don't you give a try to the Mono standard lib (https://github.com/mono/mono/tree/master/mcs/class) or the Microsoft alternative (https://github.com/dotnet/corefx)/(https://github.com/Microsoft/referencesource)?
Coordinator
Apr 17, 2015 at 9:44 AM
I have had a look at Corlib but it seems like a work-in-progress standard-lib. Why don't you give a try to the Mono standard lib
(https://github.com/mono/mono/tree/master/mcs/class) or the Microsoft alternative (https://github.com/dotnet/corefx)/(https://github.com/Microsoft/referencesource)?
Corlib has been written from Microsoft/referencesource lib.

corefx is not lib at all

I have not tried mono lib.

I am planning to use mscorlib from CoreCLR (open-source project).

But all those libraries have one big issue. They do not contain the implementation of classes which are "CORE" classes such as String, Decimals, Interlocked, Thread etc. They are simply wrappers for Virtual Machine calls. Which I need to implement. This is why you see Corlib.dll (not mscorlib or mono). Yes it is going to take a lot of time to review each class (at least in main namespaces such as System etc)
Apr 18, 2015 at 12:49 AM
I think Mono is mature enough to be used as a Corelib for your project, as well as the mscorlib and BCL from CoreCLR.

I had a look at the Mono String.cs sources (https://github.com/mono/mono/blob/master/mcs/class/corlib/ReferenceSources/String.cs) and the Thread.cs (https://github.com/mono/mono/blob/master/mcs/class/corlib/System.Threading/Thread.cs) file but as you said both contain [MethodImplAttribute(MethodImplOptions.InternalCall)] with externs to internal implementations in the mono runtime : (

There is a file with all these internal calls mono uses in their entire BCL -> https://github.com/mono/mono/blob/master/mono/metadata/icall-def.h.
I'm wondering if it will be possible to create a custom runtime native library with the implementation of all these internal functions and change the [MethodImplAttribute(MethodImplOptions.InternalCall)] attributes to [DllImport] to this custom runtime native lib.

Then, you only need to solve these new [DllImport] attributes and the old ones.
You could map them, in your il 2 C transpiler, as extern symbols in c++ and output a list of required native libs for the g++ linker.

With this approach you could benefit from all the good work on the mono BCL. So, you don't need to rewrite your Corelib from scratch.
I know you got part of your current Corelib from the .net Compact Framework for embedded devices. But this Corelib is clearly old and less powerful that the current mono BCL.

If this strategy were approachable, you would just need to work hard on the custom runtime native lib I mentioned and considering you could also take advantage of the current code of the mono runtime, I think it won't be that hard.
I think this strategy will also work with CoreCLR now that Micro open-sourced it :)

Just as a example of what I mean, in this file (https://github.com/mono/mono/blob/master/mono/metadata/threads.c#L2103) you have the code of one of the internal functions defined in the (https://github.com/mono/mono/blob/master/mono/metadata/icall-def.h#L914) icall-def.h of mono runtime. It's the System_Threading_Thread_Abort function.
This internal function gets call in the Thread.cs (https://github.com/mono/mono/blob/master/mcs/class/corlib/System.Threading/Thread.cs#L603) of mono's BCL.
So just by modifying the threads.c file a bit to decouple it from the mono runtime you could get part of the custom runtime native lib done.

For sure, I'm missing tons of details here, but I will like to know how approachable this method is.

I will like to help you with this or collaborate with you in this project. If you are opened to pull requests.
I have plans to use csnative for a personal project but right I find it impossible because of Corelib and it completion level. I know csnative is a work in progress but I really like it and would like to help.
Coordinator
Apr 18, 2015 at 10:22 AM
thank you for your input. BTW you do not need C internals anymore with IL2C :) you can write in plain C# (I mean C# 2.0) and it will be as native as C. If you have a look into class System.DataTime property UtcNow you will understand what I mean.

Before starting with corelib I still need to finish a lot of staff (today I have finished tuning Boehm GC and I need to finish finalization). Then I need to write code to allow generate C into many files because generated file from Mscorlib.dll is 160MB and g++ just can't compile it. (and on and on ...)
Coordinator
Apr 18, 2015 at 2:26 PM
jacanosalado wrote:
I will like to help you with this or collaborate with you in this project. If you are opened to pull requests.
I have plans to use csnative for a personal project but right I find it impossible because of Corelib and it completion level. I know csnative is a work in progress but I really like it and would like to help.
I am very happy get help as I do not have enough time for CoreLib. I would use MsCorlib from CoreCLR (which I have included into my project) and which is compilable.

Try it and tell me ho is going, which issues you hit etc.

Kind Regards
Alex
Apr 18, 2015 at 4:17 PM
Great, so with [MethodImplAttribute(MethodImplOptions.Unmanaged)] attribute you could import in C# an export of other native lib :)
I didn't know that.

Just to summarize my last message, I think that rewriting an entire new BCL and review each class is going to take you some time.
I think that using sources from .net Compact Framework C# 2.0 is a downside when it comes to using csnative for projects which target .net 4.5

I think it could be possible to grab all sources from a mature BCL like Microsoft or Mono and create a program that rewrite all extern symbols in a way that let you link the final obj that il2c generates against required native libs by [MethodImplAttribute(MethodImplOptions.InternalCall)], [MethodImplAttribute(MethodImplOptions.Unmanaged)] and [DllImport] attributes.

Them you don't reinvent the wheel by creating your custom Corelib, that will always be less tested and mature that the Micro or Mono ones.

Another benefit is that you won't need to review each class manually and make required adjustements in order to make it cross-platform (as the case of System.DateTime UtcNow property), just run this program, detect dependant native libraries, rewrite mentioned attributes and finally link against dependant libs.


We keep in touch.

Juan.
Coordinator
Apr 18, 2015 at 11:21 PM
jacanosalado wrote:
I think it could be possible to grab all sources from a mature BCL like Microsoft or Mono and create a program that rewrite all extern symbols in a way that let you link the final obj that il2c generates against required native libs by [MethodImplAttribute(MethodImplOptions.InternalCall)], [MethodImplAttribute(MethodImplOptions.Unmanaged)] and [DllImport] attributes.
The main problem that you need to test classes any way and in many cases it is easier to import it manually as it requires some review. BTW CoreLib has not been written by me. I have grabbed source from .NET Micro Framework and added classes from SSCLI (2.0 Rotor). (both works from Microsoft)

PS, SSCLI has very good tests set to test BCL so you will know if your lib is good