However, the .Net bytecode is built around a virtual stack-based machine, for example, a multiplication would be:
- push variable onto stack
- push second variable onto stack
- multiple
- pop answer
I was wondering how I would go about mapping the instructions. I don't want to say 'if 2 pushes, a multiply, and 1 pop' then replace it with a multiple because that is potentially dangerous since a value could be kept in the stack from a previous operation. I also don't want to emitting code only when I reach a non-stack-modifying instruction, because I might reach code where a lot of pop/push-ing is done to rearrange values on the stack and I skip over that completely.
The alternative way is to try to build a tree in memory of values being passed in and out, but this seems bad because it would create a lot of overhead for a JITer that is suppose to be light weight.
So, where should I go from here?