Re: Is CONTINUE_LOOP still a thing?

2020-06-09 Thread Terry Reedy

On 6/9/2020 3:26 AM, Adam Preble wrote:


Well, I found CONTINUE_LOOP


In opcode.py for 3.6.8 with value 119, annotated as an absolute jump.

Everything in this file is use-at-your-own-risk internal cpython detail, 
subject to change with any release.



3.6.8 sure doesn't emit it for pretty basic stuff:


It is gone in 3.8.3, along with others.  I believe a core developer who 
added new opcodes removed unused opcodes after counting emissions of 
opcodes in compiler code.  As with Python, deletion of unused items is 
not guaranteed to be immediate.


SETUP_LOOP, which was used in 3.6, is gone also.

--
Terry Jan Reedy

--
https://mail.python.org/mailman/listinfo/python-list


Is CONTINUE_LOOP still a thing?

2020-06-09 Thread Adam Preble
I got to the point of trying to implement continue in my own interpreter 
project and was surprised when my for-loop just used some jumps to manage its 
control flow. Actually, I hoped for something else; I don't have logic in my 
code generation to track jump positions. I kind of hoped there was some 
CONTINUE opcode with some extra logic I could add at run time to just kind of 
do it.

(that is my own problem and I know there is no such thing as a free lunch, but 
it's 2AM and I want to hope!)

Well, I found CONTINUE_LOOP, which applies for for-loops, but 3.6.8 sure 
doesn't emit it for pretty basic stuff:

>>> def for_continue():
...   a = 0
...   for i in range(0, 3, 1):
... if i == 2:
...   continue
... a += i
...   else:
... a += 10
...   return a
...
>>> for_continue()
11
>>> dis(for_continue)
  2   0 LOAD_CONST   1 (0)
  2 STORE_FAST   0 (a)

  3   4 SETUP_LOOP  46 (to 52)
  6 LOAD_GLOBAL  0 (range)
  8 LOAD_CONST   1 (0)
 10 LOAD_CONST   2 (3)
 12 LOAD_CONST   3 (1)
 14 CALL_FUNCTION3
 16 GET_ITER
>>   18 FOR_ITER22 (to 42)
 20 STORE_FAST   1 (i)

  4  22 LOAD_FAST1 (i)
 24 LOAD_CONST   4 (2)
 26 COMPARE_OP   2 (==)
 28 POP_JUMP_IF_FALSE   32

  5  30 JUMP_ABSOLUTE   18

  6 >>   32 LOAD_FAST0 (a)
 34 LOAD_FAST1 (i)
 36 INPLACE_ADD
 38 STORE_FAST   0 (a)
 40 JUMP_ABSOLUTE   18
>>   42 POP_BLOCK

  8  44 LOAD_FAST0 (a)
 46 LOAD_CONST   5 (10)
 48 INPLACE_ADD
 50 STORE_FAST   0 (a)

  9 >>   52 LOAD_FAST0 (a)
 54 RETURN_VALUE

The place where a CONTINUE_LOOP could have made sense would be at address 30 
for that JUMP_ABSOLUTE. That'll go back to a FOR_ITER, as CONTINUE_LOOP implies 
it *must* do. I'm just guessing that at some point, somebody concluded there 
wasn't anything special about having that opcode over absolute jumps and it got 
abandoned. I wanted to check if my notions were correct or if there's some 
gotcha where having that over other things makes sense.
-- 
https://mail.python.org/mailman/listinfo/python-list