On 2021-03-17 22:10, Skip Montanaro wrote:
Consider this little session from the tip of the spear:

 >>> sys.version
'3.10.0a6+ (heads/master:0ab152c6b5, Mar 15 2021, 17:24:38) [GCC 10.2.0]'
 >>> def while2(a):
...     while a >= 0:
...         a -= 1
...     return a
...
 >>> dis.dis(while2)
   2           0 LOAD_FAST                0 (a)
               2 LOAD_CONST               1 (0)
               4 COMPARE_OP               5 (>=)
               6 POP_JUMP_IF_FALSE       24

   3     >>    8 LOAD_FAST                0 (a)
              10 LOAD_CONST               2 (1)
              12 INPLACE_SUBTRACT
              14 STORE_FAST               0 (a)

   2          16 LOAD_FAST                0 (a)
              18 LOAD_CONST               1 (0)
              20 COMPARE_OP               5 (>=)
              22 POP_JUMP_IF_TRUE         8

   4     >>   24 LOAD_FAST                0 (a)
              26 RETURN_VALUE
 >>> list(dis.findlinestarts(while2.__code__))
[(0, 2), (8, 3), (16, 2), (24, 4)]

I get that somewhere along the way the compiler has been optimized to duplicate the loop's testso as to eliminate some jumps (perhaps). It's not clear to me that the line number should be "2" in the duplicated test though.Shouldn't it be "3"?

The condition is on line 2. The fact that it has been duplicated n the bytecode is irrelevant; it's still on line 2 in the source.

Line 3 has the decrement and line 4 has the return.

I stumbled on this while trying to generate a line number table in my side project register VM. As I understand it, the line number delta in the output table is supposed to always be >= 0. In my code I'm using dis.findlinestarts() to determine the line numbers for each block. Perhaps I should be modifying its results. OTOH, maybe it's a bug. (If that's the consensus, I will create an issue.)

_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/X52UUKO22IHCKCW7N6GNBFAHBRZTMMMO/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to