[Python-Dev] An obscene computed goto bytecode hack for "switch" :)
Phillip J. Eby
pje at telecommunity.com
Sat Jun 17 18:38:19 CEST 2006
At 01:18 PM 6/17/2006 +0200, Armin Rigo wrote:
>Psyco cheats here and emulates a behavior where there is
>always exactly one object instead (which can be a tuple), so if a
>END_FINALLY sees values not put there in the "official" way it will just
>crash. PyPy works similarily but always expect three values.
>(Hum, Psyco could easily be fixed to support your use case... For PyPy
>it would be harder without performance hit)
I suppose if the code knew it was running under PyPy or Psyco, the code
could push three items or a tuple? It's the knowing whether that's the
case that would be difficult. :)
I'm a bit surprised, though, since I thought PyPy was supposed to be an
interpreter of CPython bytecode. That is, that it runs unmodified Python
Or are you guys just changing POP_BLOCK's semantics so it puts two extra
None's on the stack when popping a SETUP_FINALLY block? [Looks at the code]
Ah, yes. But you're not emulating the control mechanism. I see why you're
saying it would be harder without a performance hit. I could change the
bytecode so it would work under PyPy as far as stack levels go, but I'd
need to also be able to put wrapped unrollers on the stack (which seems
impossible from within the interpreter), or else PyPy would have to check
whether the unroller is an integer.
I guess it would probably make better sense to have a JUMP_TOP operation to
implement the switch statement, and to use that under PyPy, keeping the
hack only for implementing jump tables in older Python versions.
Anyway, if I do use this for older Python versions, would you accept a
patch for Psyco to support it? That would let us have JIT-compiled
predicate dispatch for older Pythons, which sounds rather
exciting. :) The current version of RuleDispatch is an interpreter that
follows a tree data structure, but I am working on a new package,
PEAK-Rules, that is planned to be able to translate dispatch trees directly
into bytecode, thus removing one level of interpretation.
I do have some other questions, but I suppose this is getting off-topic for
python-dev now, so I'll jump over to psyco-devel once I've gotten a bit
further along. Right now, I've only just got BytecodeAssembler up to
building simple expression trees, and the "computed goto" demo.
More information about the Python-Dev