kekyo / IL2C

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

Implement IL codes for #91, #93, fix #105 #104

Open cyborgyn opened 2 years ago

cyborgyn commented 2 years ago

For #91, investigating nanoFramework BCL (mscorlib) compilation, implement all missing opcodes to be able to compile. Solve #93 by implementing switch IL opcode Solve #105 by adding stop condition, and handling static constructors

cyborgyn commented 2 years ago

No regression up to this point on already available unit tests, though new implementations are not tested yet

cyborgyn commented 2 years ago

No regression from unit tests. https://github.com/cyborgyn/CoreLibrary can be transpiled to C so far, however compiling it's C will fail (with 91 errors), as expected. But no syntax errors. The gcc error messages are mostly like this:

/root/sources/bcl/include/mscorlib/System/IDisposable.h:20:56: error: conflicting types for ‘System_IDisposable_VTABLE_DECL__’; have ‘const struct System_IDisposable_VTABLE_DECL___’
   20 | typedef const struct System_IDisposable_VTABLE_DECL___ System_IDisposable_VTABLE_DECL__;
      |                                                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /root/sources/IL2C/IL2C.Runtime/cmake/../include/il2c.h:390,
                 from /root/sources/bcl/include/mscorlib.h:8,
                 from /root/sources/bcl/src/mscorlib/Microsoft/CodeAnalysis/EmbeddedAttribute.c:3:
/root/sources/IL2C/IL2C.Runtime/cmake/../include/System/IDisposable.h:40:3: note: previous declaration of ‘System_IDisposable_VTABLE_DECL__’ with type ‘System_IDisposable_VTABLE_DECL__’
   40 | } System_IDisposable_VTABLE_DECL__;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /root/sources/bcl/include/mscorlib.h:170,
                 from /root/sources/bcl/src/mscorlib/Microsoft/CodeAnalysis/EmbeddedAttribute.c:3:

This is expected, as now there are multiple definitions conflicting in IL2C.Runtime and the transpiled mscorlib C sources.

cyborgyn commented 2 years ago

What am I doing wrong? Visual Studio doesn't show up my new test...

kekyo commented 2 years ago

I'm checking your switch test:

.method public static string TestSwitch(int value) cil managed
.method public static string TestSwitch(int32 value_) cil managed

Passed build sequence, shown up all testcase on my environment (and failed on switch test).


MEMO:

IL2C unit test has partially dynamic testcase generator on NUnit. It has unstable collector when new testcase was added/changed. (I understand it's ugly bad way...)

Please rebuild (on VS solution), will show all testcase on test explorer when you come across again and the build is unsuccessful.

cyborgyn commented 2 years ago

Getting to understand why unsafe pointer handling is not implemented yet. :) There are too many differences between C and C# pointer arithmetics, and the differences in the array headers how you, and .NET describes in memory. Will revert back to a simpler ldobj unit test...

cyborgyn commented 2 years ago

Ready for review. Until you review it, I will add probably a bit more unit tests, if I will have some time.

cyborgyn commented 2 years ago

Merged everything from your devel branch

kekyo commented 2 years ago

Thank you, I will review!

cyborgyn commented 2 years ago

Wow! Nice, long CI pipeline. I looked into the results, and came across such ones:

  Failed NullReference(null) [104 ms]
  Error Message:
     Bad MSIL, or test case!
  Expected: True
  But was:  False

I put those messages into it: "Bad MSIL, or test case!" and it means, even the .NET runtime fails to test correctly that test, before even running the actual compiled ones. In my case (why I added it at the first place) was, that I played around with pointers, which were not fixed, and sometimes GC moved the data around. But in this specific scenario, otherwise it's evaluating OK under Windows, it suggests that under .NET Core the MSIL or the BCL behaves differently, as the .NET Framework runtime. Odd, good to note! :)

Also, a suggestion, this many unit tests will always fail, due to time out. Let's shorten the list the way: only execute a single test case randomly chosen per test method. Will still give a good coverage, and probably will have enough time to execute.

kekyo commented 2 years ago

I put those messages into it: "Bad MSIL, or test case!" and it means, ...

Yes, regression test is important on IL2C and nice improvement for insightful message! (I'm working ports completion CI on GitHub Actions, currently broken on Windows test, please wait... #100 )

Also, a suggestion, this many unit tests will always fail, due to time out. Let's shorten the list the way: only execute a single test case randomly chosen per test method. Will still give a good coverage, and probably will have enough time to execute.

So that's it. In that case, it would be good to devise the timing of CI so that complete test and randomly select test can be separated. I'll think about it.

Do you know a common way to randomly select test cases in NUnit? (I think it is necessary to implement the dynamic generation part well in order to realize it with IL2C)

cyborgyn commented 2 years ago

Thank you very much, will look into it soon!

cyborgyn commented 2 years ago

For a few days, unfortunately I have no spare time, but will be back on the topic as soon as I can. Thank you for your efforts so far!

kekyo commented 2 years ago

No problem, keep your pace!