[Python-ideas] Standard (portable) bytecode "assembly" format

Victor Stinner victor.stinner at gmail.com
Fri Feb 26 12:19:14 EST 2016


2016-02-26 17:51 GMT+01:00 Brett Cannon <brett at python.org>:
>>     bytecode.extend([Instr("LOAD_NAME", 'print'),
>>                      Instr("LOAD_CONST", 'Hello World!'),
>>                      Instr("CALL_FUNCTION", 1),
>>                      Instr("POP_TOP"),
>>                      Instr("LOAD_CONST", None),
>>                      Instr("RETURN_VALUE")])
>
> Any reason you went with string constants instead of enums?

My API is still a work-in-progress :-) Maybe the API can be changed to:

LOAD_CONST('Hello World!')
POP_TOP()

But it means that your code will probably starts with "from bytecode
import *" or "from bytecode import LOAD_CONST, POP_TOP". There are
something like 155 opcodes, so I would prefer to not have to write the
exhaustive list of imports.

Another option is something like:

Instr.LOAD_CONST('Hello World!')
Instr.POP_TOP()

or

whatever.LOAD_CONST('Hello World!')
whatever.POP_TOP()

I don't know what is the best. codetransformers uses
instructions.LOAD_CONST("Hello World!") and instructions.LOAD_FAST is
a type (it used for pattern matching).

Victor


More information about the Python-ideas mailing list