[Python-Dev] JUMP_IF_X opcodes

Damien Morton newsgroups1@bitfurnace.com
Mon, 3 Mar 2003 18:55:47 -0500


I have been reviewing the compile.c module with respect to the use of
JUMP_IF_XXX opcodes, and the frequency with which these opcodes are followed
by a POP_TOP instruction.

It seems to me that there are two kinds of uses cases for these opcodes,

The first use case could be expressed as POP_THEN_JUMP_IF_XXXX
The second use case could be expressed as JUMP_IF_XXX_ELSE_POP

Listed below are the use cases for these instructions, and the functions in
compile.c that they apear in.

The form is JUMP_IF_XXX(top-of-stack-if-no-jump, top-of-stack-if-jump)


com_assert_stmt - JUMP_IF_TRUE(-,-)
com_if_stmt - JUMP_IF_FALSE(-,-)
com_while_stmt - JUMP_IF_FALSE(-,-)
com_try_except - JUMP_IF_FALSE(-,-)
com_list_if - JUMP_IF_FALSE(-, -)

com_comparison - JUMP_IF_FALSE(-, 0)
com_and_test - JUMP_IF_FALSE(-, 0)
com_test - JUMP_IF_TRUE(-, 1)

Below is a minimally intrusive implementation of the expansion of
JUMP_IF_FALSE into two opcodes for handling the two use cases.


case JUMP_IF_FALSE_ELSE_POP:
case POP_THEN_JUMP_IF_FALSE:
 NEXTARG(oparg);
 err = PyObject_IsTrue(TOP());
 if (err > 0) {
  err = 0;
  POP();
  }
 else if (err == 0) {
  if (opcode == POP_THEN_JUMP_IF_FALSE) POP();
  JUMPBY(oparg);
  }
 else
  break;
 continue;


Comments, suggestions, etc, appreciated.