On 24.03.2010 11:00, Tedd Hansen wrote:
> Hi
>
> I have one wish list item. :)
>
> Some way of knowing the argument stack at any given instruction. (count and
> what types in what order)
>   
Just this week I wrote code to compute the control flow graph from Cecil
IL Instructions - unlike Cecil.FlowAnalysis, my code also handles
control flow for try-catch and try-finally blocks (the latter are quite
complicated and took a whole day to implement correctly).
Then I'm transforming the instruction to 'register IL' - basically I add
metadata to each instruction specifying variables (registers) as inputs
and outputs. Each stack location is transformed into a variable.
After that, I'm calculating the dominance frontiers on the CFG, and
using that info to transform my register IL representation into SSA
form. I also implemented some trivial SSA-based optimizations like copy
propagation and dead code elimination -- these are primarily meant to
make subsequent analysis easier, not as actual optimizations (the JIT
will repeat them anyways).

The end result I'm working towards is statically checking a program for
NullReferenceExceptions. Basically "code contracts lite" - where lite
should refer both to runtime of the tool (I want to be faster than
Microsofts static contract checking tool) and to the ease of use
(annotate parameters with [NonNull] instead of writing a "contract
class"; automatically infer stuff whenever possible). But then again
it'll be focused on introducing non-nullable variables, and not do
anything else that contracts do.

I don't know how useful my SSA classes would be to someone who wants to
modify IL - I'm focusing on the code analysis part and give up the 1:1
correspondence with IL instructions.
You can take a look yourself - here's the CFG and SSA form for a method
using a foreach loop to sum up the length of all strings in a List<string>:
http://www.danielgrunwald.de/coding/null/ForEach.cfg.png (green edges
aren't control flow edges but represent dominance)
http://www.danielgrunwald.de/coding/null/ForEach.ssa.png (ldloca
instructions are eliminated where possible because SSA form can't be
used with them)
The code is a work is progress, but I'm planning on making the CFG and
SSA code available as open-source code when it's done.

Daniel

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to