Is CONTINUE_LOOP still a thing?
Adam Preble
adam.preble at gmail.com
Tue Jun 9 03:26:43 EDT 2020
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_FUNCTION 3
16 GET_ITER
>> 18 FOR_ITER 22 (to 42)
20 STORE_FAST 1 (i)
4 22 LOAD_FAST 1 (i)
24 LOAD_CONST 4 (2)
26 COMPARE_OP 2 (==)
28 POP_JUMP_IF_FALSE 32
5 30 JUMP_ABSOLUTE 18
6 >> 32 LOAD_FAST 0 (a)
34 LOAD_FAST 1 (i)
36 INPLACE_ADD
38 STORE_FAST 0 (a)
40 JUMP_ABSOLUTE 18
>> 42 POP_BLOCK
8 44 LOAD_FAST 0 (a)
46 LOAD_CONST 5 (10)
48 INPLACE_ADD
50 STORE_FAST 0 (a)
9 >> 52 LOAD_FAST 0 (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.
More information about the Python-list
mailing list