kekyo / IL2C

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

Use ilspy to detect loops #87

Open alex4o opened 4 years ago

alex4o commented 4 years ago

Using ilspy would allow the generated c code to look more organic by producing correct if and for/while statements in c instead of using goto, only in the worst-case scenarios goto will be needed. This will slow down the compilation time but should still be feasible non the less. It is also possible to use only to loop detection algorithms from ilspy, but I believe that it still might be beneficial to directly use it as a dependency.

kekyo commented 4 years ago

@alex4o Yes, agreed. I know it makes to be accurate translated code readability.

Currently IL2C's flow detection algorithm is making safer on minor (but legal) bytecode sequence cases. It's difficult to switch more flexible algorithm with safer when turns to while/for loops (related both local scope variables and environment stacks).

I don't know how about ports easier the ILSpy's algorithm fitting to IL2C analysis code. Do you know about it?

kekyo commented 4 years ago

Bit related #25.

alex4o commented 4 years ago

There are 2 ways I see that you can use IlSpy. The first one is to use it as a library. This will be fairly simple and would allow you to use it for other things other then loop detection. The second one is to use is only as an example. Because IL2C and IlSpy have very different architectures I don't think that code from there can just be copied. I believe that using IlSpy would simplify development and as you said because it is difficult to write a correct algorithm it is easier to use an already implemented one. This will also reap the benefits of all the work done by them already.

About flow detection. I have written my own flow detection algorithm that works on basic blocks and works correctly as far as I know, because of the constraints of the common language runtime. The proper way to do flow analysis afaik is to first split the instructions into basic blocks and then the flow analysis won't break when loops are encountered. I would be happy to help implement an algorithm like that if you decide against using IlSpy.

Here is a small example to show my idea: https://github.com/alex4o/ilspy-example-usage.

kekyo commented 4 years ago

Surprise for me your example code is too short! Thank you told me your suggestion.

Could you wait a while, I think how to replace it. Currently IL2C has two parts, first step is analysing opcodes and flow. And second step is converts minor attributes (ex:related devirtualization) and generate C code.

The C code generator will fit easier, but (unfortunately for me) attributes came from first step, their are depending implicitly...