kekyo / IL2C

IL2C - A translator for ECMA-335 CIL/MSIL to C language.
Apache License 2.0
401 stars 36 forks source link

How to cover BCL interfaces/implementations by semi-automatically aggregation for around members. #91

Open kekyo opened 3 years ago

kekyo commented 3 years ago

TODO:

cyborgyn commented 2 years ago

It is worth looking at https://github.com/nanoframework/CoreLibrary

I will try to compile it with IL2C, to see how much effort needed to be put into full blown implementation of it for IL2C. Would require to:

kekyo commented 2 years ago

We need to extract automated how much lack members on IL2C. I think it difficulty topic:

cyborgyn commented 2 years ago

Indeed, it is a difficult topic. I think, we need to pick a BCL implementation, and work against it, otherwise it will be an endless effort to build it from bottom to up. If we can make IL2C compile such an mscorlib into C, it will produce the same naming convention otherwise used in the app.

I looked at the Microsoft's implementation of BCL (mscorlib) a bit, tried to put together a stripped down version for a few days, but has a lot of problems:

The nanoFramework seems to be a fairly complete reimplementation of .NET, with much fewer methods, and all of those are not implemented in IL (C#), are internal.calls, and on top of that: it was specifically designed with resource constrained embedded systems in mind. (Just like IL2C, as I understand)

By compiling mscorlib into C, we would get the proper class descriptors, vtables, stack frames, everything, for even the method stubs. Those could be removed from the IL2C.Runtime files, and only leave the actual method implementations. Than trying to compile the mscorlib against the framework with gcc, would result in a well identifiable error messages, which methods don't have an implementation (the compile log could be 'grep'-ed for those, I think).

When we have identified the methods, not there in the IL2C.Runtime, we can generate those with a simple body, throwing NotImplementedException. And start to implement those, eventually.

If we fork from nanoFramework.CoreLibrary, we can even choose where to implement the missing parts: C# side, or C side.

kekyo commented 2 years ago

I will read/check too nanoFramework.CoreLibrary, I feel will take some idea from it.

It may be difficult to port nanoFramework.CoreLibrary as it is. (Because there is a difference between AOT and interpreter)

cyborgyn commented 2 years ago

According to my initial tests with compiling nanoFramework's mscorlib, I see the following path would be viable to have an optional "external" mscorlib implementation:

If after these changes compilation succeeds, we will have a new runtime static library, with external references to methods not implemented, and this could be collected probably with libtools somehow.

Edit

Don't need to patch the generated C code, instead I introduced the Interop attributes in nanoFramework, and used to annotate NativeType all the simple types, except for the Delegate and MulticastDelegate.

kekyo commented 2 years ago

I bit understood your strategy :) Would you tell me about my understanding is correct:

  1. Assuming that the published symbols that nanoFramework.CoreLibrary contains reasonable scale of symbols,
  2. Build IL2C with a little tweaking to refer to the CoreLibrary symbols,
  3. Extract based on the symbol information of the combined native binary (with nm and like).

The difference between the list of symbols obtained from a normal IL2C binary and the CoreLibrary combined binary is probably insufficient and it's close up next step for our implementation.

cyborgyn commented 2 years ago

Yes, something close. The produced combined library will contain a lot of extern symbols, which will be coming from the CoreLibrary's methods, annotated with [MethodImpl(MethodImplOptions.InternalCall)] but no implementation found in the Runtime. We can decide one by one, which one to implement on which side: C# or C. At least I hope, that it will work like this.

kekyo commented 2 years ago

I understood it!

Would you make ready to review for branch feature/implement-ilcodes before do it? It already long commit tree so I wanna review before continue this idea.

(I feel you will make on feature/external-mscorlib, could you rebase it on PR merged commit in devel ?)

cyborgyn commented 2 years ago

I understood it!

Would you make ready to review for branch feature/implement-ilcodes before do it? It already long commit tree so I wanna review before continue this idea.

(I feel you will make on feature/external-mscorlib, could you rebase it on PR merged commit in devel ?)

I think it is ready to review. In the meantime, I will stop working on the feature/external-mscorlib branch for now, and add some more unit tests to the PR, if I will have some time.