Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ilspy to detect loops #87

Open
alex4o opened this issue Aug 28, 2020 · 4 comments
Open

Use ilspy to detect loops #87

alex4o opened this issue Aug 28, 2020 · 4 comments

Comments

@alex4o
Copy link

alex4o commented Aug 28, 2020

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
Copy link
Owner

kekyo commented Aug 28, 2020

@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
Copy link
Owner

kekyo commented Aug 28, 2020

Bit related #25.

@alex4o
Copy link
Author

alex4o commented Aug 28, 2020

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
Copy link
Owner

kekyo commented Aug 31, 2020

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants