<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div class="moz-cite-prefix">On 11/10/12 7:50 AM, Devin Jeanpierre
wrote:<br>
</div>
<blockquote
cite="mid:CABicbJJ0PdSQv==UqEgc+FzaT2xZbmui26T=PBwo48_3b2K5DQ@mail.gmail.com"
type="cite">
<pre wrap="">On Sat, Nov 10, 2012 at 6:57 AM, Laurens Van Houtven <a class="moz-txt-link-rfc2396E" href="mailto:_@lvh.cc"><_@lvh.cc></a> wrote:
</pre>
<blockquote type="cite">
<pre wrap="">The README suggests that doing this optimization on a bytecode level may be
better than doing it on a source/AST level. Can you explain why?
</pre>
</blockquote>
<pre wrap="">
[...]
In particular there's a directly analogous CPS form for bytecode,
where every bytecode instruction in a bytecode string is considered to
be equivalent to a CPS function, and continuations represent returns
from functions and not expression evaluation, and the CPS functions
just navigate through the bytecode stream using tail calls, while
keeping track of the expression evaluation stack and the call stack
and globals dict.
</pre>
</blockquote>
<br>
Right... looking at an example output from the transform:<br>
<br>
<meta charset="utf-8">
<pre style="margin: 15px 0px; padding: 6px 10px; border: 1px solid rgb(204, 204, 204); font-size: 13px; font-family: Consolas, 'Liberation Mono', Courier, monospace; background-color: rgb(248, 248, 248); line-height: 19px; overflow: auto; border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; color: rgb(51, 51, 51); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;"> v13 = n
v14 = 1
v4 = v13 == v14
if v4:
...
</pre>
<br class="Apple-interchange-newline">
Viewed in CPS this might look like:<br>
<br>
(VAR, [v13, (INT, [v14, ((EQ, v13, v14), TEST, ...)])])<br>
<br>
Where each node is (EXP, CONT). In this case the result of each
operation is put into a variable/register (e.g., 'v13'), but
python's bytecodes actually operate on the frame stack. So if there
were some way to change this to<br>
<br>
(VAR, [PUSH, (INT, [PUSH, ((EQ 0 1)), TEST, ...)])])<br>
<br>
Where (EQ 0 1) means 'apply EQ to the top two items on the stack'.<br>
<br>
The code above puts each value into a local variable, which gets
pushed onto the stack anyway by the compiled bytecode.<br>
<br>
Another advantage to generating bytecode directly would be support
for python 2, since I think 'nonlocal' can be done at the bytecode
level.<br>
<br>
-Sam<br>
<br>
</body>
</html>