inline function call

Michael Spencer mahs at telcopartners.com
Thu Jan 5 21:34:53 EST 2006


Bengt Richter wrote:
...
> 
> This could be achieved by a custom import function that would capture the AST
> and e.g. recognize a declaration like __inline__ = foo, bar followed by defs
> of foo and bar, and extracting that from the AST and modifying the rest of the
> AST wherever foo and bar calls occur, and generating suitable substitutions of
> suitable AST subtrees generated from the ASTs of the foo and bar ASTs and rules
> for renaming and guaranteeing safe temporary names etc. The modified AST would
> then pass through the rest of the process to compilation and execution for the
> creation of a module. 

I've thought about a similar approach, but I suspect that pure AST 
transformations (i.e., source-code inlining) would work only in a few special 
cases.  General inlining would require, I guess:

* Munging the names of the in-lined local function variables, e.g., prepending 
them with the name of the inlined function, and perhaps a sequence number
* Inserting a set of assignments to replicate the parameter-passing
* Replacing the return statement (there would have to be only one) with another 
assignment

In order for the assignment operations to take place in an expression list, the 
modifications would have to happen at the byte-code level.  It may be possible 
to modify compiler.pycodegen.ModuleCodeGenerator to do most of the work.  Or 
perhaps the new pypy compiler might be more amenable to experimenting - I 
haven't looked at it.

> 
> I've been meaning to do a proof of concept, but I've hinted at these things before,
> and no one seems much interested. And besides it's really a distraction from more radical
> stuff I'd like to try ;-)
> 
> Regards,
> Bengt Richter

Might be fun to try

Michael




More information about the Python-list mailing list