[pypy-dev] CLI code generation

Samuele Pedroni pedronis at strakt.com
Mon Mar 20 15:58:43 CET 2006


Antonio Cuni wrote:

> holger krekel wrote:
>
>> Hi Antonio,
>> On Sun, Mar 19, 2006 at 20:53 +0100, Antonio Cuni wrote:
>>
>>> as I said I've begun writing the .NET CLI backend; it is still very 
>>> experimental but it can already compile correctly some code snippets 
>>> such as the algorithm for computing fibonacci's numbers.
>>
>>
>> cool!  I would be interested to hear a bit more about your concrete
>> current approach. 
>
>
> I respond here so that other can read, if they are interested.
>
> The first decision I took is whether to generate IL code (to be 
> assembled with ilasm) or C# code: I choose the first mainly because C# 
> lacks the goto statement and it would be difficult to implement flow 
> control.
>
FWIW, I thought C# has goto: 
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vclrfthegotostatement.asp

> Given this, my current approach if fairly naive and certainly not 
> efficient: at the moment the compiler does a 1-to-1 translation 
> between low level operation expressed in SSA; for example the simple 
> function:
>
> def bar(a,b):
>     return a+b
>
> is compiled into the following IL code:
>
> .method static public int32 bar(int32 a_1, int32 b_1) il managed
> {
>     .locals (int32 v6, int32 v12)
>
> block0:
>     ldarg.s 'a_1'
>     ldarg.s 'b_1'
>     add
>     stloc.s 'v12'
>     ldloc.s 'v12'
>     stloc.s 'v6'
>     br.s block1
>
> block1:
>     ldloc.s 'v6'
>     ret
> }
>
> As you can see there are many unnecessary operations: the result of 
> 'add' is stored to v12, loaded from v12, stored in v6 and then loaded 
> from v6! The same is true for the branch instruction, which is unneeded.
> I think it should be fairly simple to translate from the SSA form to a 
> more "stack-friendly" form useful for stack-based machines; the 
> question is: where to put such code?
> Since it could be useful even for other backends, it would be nice to 
> put it in some place where it can be shared from several backends: one 
> option could be to write it as a backend optimization, but in this 
> case we have to introduce new low level operations for stack 
> manipulation, such as 'push', 'pop' or 'dup'.
>
> ciao Anto
> _______________________________________________
> pypy-dev at codespeak.net
> http://codespeak.net/mailman/listinfo/pypy-dev





More information about the Pypy-dev mailing list