python bytecode grammar

Bengt Richter bokr at oz.net
Sun Jun 26 15:52:07 EDT 2005


On Fri, 10 Jun 2005 16:46:32 +0200, "Diez B. Roggisch" <deets at web.de> wrote:

>M1st0 wrote:
>> Ops yes is BNF :P Bacus Normal Form if I am not wrong...
>> 
>> However......
>> 
>> I'am tryng to recognizing patterns in a bytecoded file in orderd to
>> optimize...
>> 
>> But I would like to "parse"  i.e reconstruct it in something like a
>> tree..
>> in order to apply rules on a tree recursively.
>
>But bytecode is like assembly - there is no tree-structure. A opcode is 
>followed by a number of arguments, and oopcodes are in a seqence.
>
>However the bytecode is _generated_ from the AST somehow. Maybe you can 
>work on that. Also take a look at psyco, it already does optimizations 
>for numeric calculations.
>
>Besides: I serously doubt you can do much optimization on the 
>bytecodelevel itself, as it is very highlevel. The optimization efforts 
>like in psyco don't alter bytecode - they replace it....
>
Well, Raymond Hettinger wrote a decorator to optimize functions in various ways,
rewriting the byte code, and I wrote another decorator inspired by that.
His is in the recipes at
   http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/277940
and mine is posted  at
   http://groups-beta.google.com/group/comp.lang.python/msg/7d27a53385e89924?hl=en
(I think that's the last version ;-) Mine was to preset local variables without
using the default-argument hack. This led to a currying version of that, which
lets you modify the signature of an existing function by setting a local variable
to a constant and eliminating that name from the signature, so there is one level
of call as before, not the double call level from wrapping that you will get from some
other currying recipes.

I think you can see how byte code mods work from the code, and where the byte code definitions
come from etc. If you want to generate optimized code by replacing current code generation,
you will have to look into AST stuff as Diez mentions above. The sources are all there,
and there is pure python equivalents for the python run-time compiler (which is (all?) in C I think).

Import compiler and parser etc. and poke around. You'll find interesting things ;-)

Regards,
Bengt Richter



More information about the Python-list mailing list