pythonbytecode

Python bytecode, does compare_op pop the stack?


I'm working with the following bytecode:

  6          10 LOAD_FAST                0 (x)
             12 LOAD_CONST               3 (255)
             14 COMPARE_OP               2 (<)
             18 POP_JUMP_IF_FALSE       27 (to 74)

LOAD_FAST pushes the value for x (zero in this case) onto the stack. LOAD_CONST pushes the value of 255 onto the stack. I cannot find documentation regarding what the less than COMPARE_OP does with the stack although the Documentation for the POP_JUMP_IF_FALSE states that

If STACK[-1] is false, increments the bytecode counter by delta. STACK[-1] is popped.

What happens with the stack when the COMPARE_OP is executed?


Solution

  • @jasonharper is correct. Bench testing strongly suggests that the COMPARE_OP pops the top two values and pushes a boolean as the result. The POP_JUMP_IF_FALSE pops the boolean and then executes the conditional jump. If the value popped is TRUE then no operation is performed. After those four lines execute the stack is in the same condition as it was before those lines were executed. Thanks.