[Python-Dev] Expose stack effects to Python? (original) (raw)

Victor Stinner victor.stinner at gmail.com
Wed Jul 3 12:06:08 CEST 2013


Hi,

For my registervm project (fork of CPython using register-based bytecode, instead of stack-based bytecode), I implemented a Instruction.use_stack() method which just checks if the stack is "used": read the stack, exchange values in the stack (like "ROT" instruction), push or pop a value.

Instruction.use_stack(): http://hg.python.org/sandbox/registervm/file/ff24dfecc27d/Lib/registervm.py#l546

The method uses a dummy heuristic, just because it was quick to implement it, and it's enough for my use case.

To answer your question: yes, I would like a opcode_stack_effect() function.

registervm has its own disassembler which creates objects, rather than just generating a text representation of the bytecode. I'm using objects because I rewrite most instructions and I need more information and functions:

Would it be useful to have such high-level API in Python?

Victor

2013/7/3 Larry Hastings <larry at hastings.org>:

I wrote my own assembler for Python bytecode called "Maynard". I had to statically compute the stack effects for each bytecode instruction by hand; what I did was copied and pasted opcodestackeffect() (which is static) out of Python/compile.c and into my own driver program, then I probed it with test values to produce a table. I then coded up a function using that table, but hand-calculating the value sometimes as there are some opcodes whose stack effect varies based on the oparg. It sure would be nice if this information was simply available to the Python interpreter; theoretically it can change between point releases. Would anybody mind if I added it somewhere? I'd probably just expose opcodestackeffect to Python, then add all this other junk to the dis module and make it available there. /arry


Python-Dev mailing list Python-Dev at python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/victor.stinner%40gmail.com



More information about the Python-Dev mailing list