Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 74a69fa

Browse files
committed
Issue #9225: Remove the ROT_FOUR and DUP_TOPX opcode, the latter replaced
by the new (and simpler) DUP_TOP_TWO. Performance isn't changed, but our bytecode is a bit simplified. Patch by Demur Rumed.
1 parent ef0e6c3 commit 74a69fa

9 files changed

Lines changed: 30 additions & 61 deletions

File tree

Doc/library/dis.rst

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,15 @@ The Python compiler currently generates the following bytecode instructions.
184184
three.
185185

186186

187-
.. opcode:: ROT_FOUR
187+
.. opcode:: DUP_TOP
188188

189-
Lifts second, third and forth stack item one position up, moves top down to
190-
position four.
189+
Duplicates the reference on top of the stack.
191190

192191

193-
.. opcode:: DUP_TOP
192+
.. opcode:: DUP_TOP_TWO
194193

195-
Duplicates the reference on top of the stack.
194+
Duplicates the two references on top of the stack, leaving them in the
195+
same order.
196196

197197

198198
**Unary operations**
@@ -531,12 +531,6 @@ the more significant byte last.
531531
are put onto the stack right-to-left.
532532

533533

534-
.. opcode:: DUP_TOPX (count)
535-
536-
Duplicate *count* items, keeping them in the same order. Due to implementation
537-
limits, *count* should be between 1 and 5 inclusive.
538-
539-
540534
.. opcode:: STORE_ATTR (namei)
541535

542536
Implements ``TOS.name = TOS1``, where *namei* is the index of name in

Include/opcode.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ extern "C" {
1212
#define ROT_TWO 2
1313
#define ROT_THREE 3
1414
#define DUP_TOP 4
15-
#define ROT_FOUR 5
15+
#define DUP_TOP_TWO 5
1616
#define NOP 9
1717

1818
#define UNARY_POSITIVE 10
@@ -83,7 +83,7 @@ extern "C" {
8383
#define DELETE_ATTR 96 /* "" */
8484
#define STORE_GLOBAL 97 /* "" */
8585
#define DELETE_GLOBAL 98 /* "" */
86-
#define DUP_TOPX 99 /* number of items to duplicate */
86+
8787
#define LOAD_CONST 100 /* Index in const list */
8888
#define LOAD_NAME 101 /* Index in name list */
8989
#define BUILD_TUPLE 102 /* Number of tuple items */

Lib/opcode.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def jabs_op(name, op):
4848
def_op('ROT_TWO', 2)
4949
def_op('ROT_THREE', 3)
5050
def_op('DUP_TOP', 4)
51-
def_op('ROT_FOUR', 5)
51+
def_op('DUP_TOP_TWO', 5)
5252

5353
def_op('NOP', 9)
5454
def_op('UNARY_POSITIVE', 10)
@@ -116,7 +116,6 @@ def jabs_op(name, op):
116116
name_op('DELETE_ATTR', 96) # ""
117117
name_op('STORE_GLOBAL', 97) # ""
118118
name_op('DELETE_GLOBAL', 98) # ""
119-
def_op('DUP_TOPX', 99) # number of items to duplicate
120119
def_op('LOAD_CONST', 100) # Index in const list
121120
hasconst.append(100)
122121
name_op('LOAD_NAME', 101) # Index in name list

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,7 @@ Craig Rowland
700700
Clinton Roy
701701
Paul Rubin
702702
Sam Ruby
703+
Demur Rumed
703704
Audun S. Runde
704705
Rauli Ruohonen
705706
Jeff Rush

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ What's New in Python 3.2 Alpha 2?
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #9225: Remove the ROT_FOUR and DUP_TOPX opcode, the latter replaced
16+
by the new (and simpler) DUP_TOP_TWO. Performance isn't changed, but
17+
our bytecode is a bit simplified. Patch by Demur Rumed.
18+
1519
- Issue #9766: Rename poorly named variables exposed by _warnings to prevent
1620
confusion with the proper variables names from 'warnings' itself.
1721

Python/ceval.c

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,50 +1420,21 @@ PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
14201420
SET_THIRD(v);
14211421
FAST_DISPATCH();
14221422

1423-
TARGET(ROT_FOUR)
1424-
u = TOP();
1425-
v = SECOND();
1426-
w = THIRD();
1427-
x = FOURTH();
1428-
SET_TOP(v);
1429-
SET_SECOND(w);
1430-
SET_THIRD(x);
1431-
SET_FOURTH(u);
1432-
FAST_DISPATCH();
1433-
14341423
TARGET(DUP_TOP)
14351424
v = TOP();
14361425
Py_INCREF(v);
14371426
PUSH(v);
14381427
FAST_DISPATCH();
14391428

1440-
TARGET(DUP_TOPX)
1441-
if (oparg == 2) {
1442-
x = TOP();
1443-
Py_INCREF(x);
1444-
w = SECOND();
1445-
Py_INCREF(w);
1446-
STACKADJ(2);
1447-
SET_TOP(x);
1448-
SET_SECOND(w);
1449-
FAST_DISPATCH();
1450-
} else if (oparg == 3) {
1451-
x = TOP();
1452-
Py_INCREF(x);
1453-
w = SECOND();
1454-
Py_INCREF(w);
1455-
v = THIRD();
1456-
Py_INCREF(v);
1457-
STACKADJ(3);
1458-
SET_TOP(x);
1459-
SET_SECOND(w);
1460-
SET_THIRD(v);
1461-
FAST_DISPATCH();
1462-
}
1463-
Py_FatalError("invalid argument to DUP_TOPX"
1464-
" (bytecode corruption?)");
1465-
/* Never returns, so don't bother to set why. */
1466-
break;
1429+
TARGET(DUP_TOP_TWO)
1430+
x = TOP();
1431+
Py_INCREF(x);
1432+
w = SECOND();
1433+
Py_INCREF(w);
1434+
STACKADJ(2);
1435+
SET_TOP(x);
1436+
SET_SECOND(w);
1437+
FAST_DISPATCH();
14671438

14681439
TARGET(UNARY_POSITIVE)
14691440
v = TOP();

Python/compile.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -680,8 +680,8 @@ opcode_stack_effect(int opcode, int oparg)
680680
return 0;
681681
case DUP_TOP:
682682
return 1;
683-
case ROT_FOUR:
684-
return 0;
683+
case DUP_TOP_TWO:
684+
return 2;
685685

686686
case UNARY_POSITIVE:
687687
case UNARY_NEGATIVE:
@@ -782,8 +782,6 @@ opcode_stack_effect(int opcode, int oparg)
782782
return -1;
783783
case DELETE_GLOBAL:
784784
return 0;
785-
case DUP_TOPX:
786-
return oparg;
787785
case LOAD_CONST:
788786
return 1;
789787
case LOAD_NAME:
@@ -3404,7 +3402,7 @@ compiler_handle_subscr(struct compiler *c, const char *kind,
34043402
return 0;
34053403
}
34063404
if (ctx == AugLoad) {
3407-
ADDOP_I(c, DUP_TOPX, 2);
3405+
ADDOP(c, DUP_TOP_TWO);
34083406
}
34093407
else if (ctx == AugStore) {
34103408
ADDOP(c, ROT_THREE);

Python/import.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,14 @@ typedef unsigned short mode_t;
101101
introduce POP_JUMP_IF_FALSE and POP_JUMP_IF_TRUE)
102102
Python 3.2a0: 3160 (add SETUP_WITH)
103103
tag: cpython-32
104+
Python 3.2a1: 3170 (add DUP_TOP_TWO, remove DUP_TOPX and ROT_FOUR)
105+
tag: cpython-32
104106
*/
105107

106108
/* If you change MAGIC, you must change TAG and you must insert the old value
107109
into _PyMagicNumberTags below.
108110
*/
109-
#define MAGIC (3160 | ((long)'\r'<<16) | ((long)'\n'<<24))
111+
#define MAGIC (3170 | ((long)'\r'<<16) | ((long)'\n'<<24))
110112
#define TAG "cpython-32"
111113
#define CACHEDIR "__pycache__"
112114
/* Current magic word and string tag as globals. */

Python/opcode_targets.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ static void *opcode_targets[256] = {
44
&&TARGET_ROT_TWO,
55
&&TARGET_ROT_THREE,
66
&&TARGET_DUP_TOP,
7-
&&TARGET_ROT_FOUR,
7+
&&TARGET_DUP_TOP_TWO,
88
&&_unknown_opcode,
99
&&_unknown_opcode,
1010
&&_unknown_opcode,
@@ -98,7 +98,7 @@ static void *opcode_targets[256] = {
9898
&&TARGET_DELETE_ATTR,
9999
&&TARGET_STORE_GLOBAL,
100100
&&TARGET_DELETE_GLOBAL,
101-
&&TARGET_DUP_TOPX,
101+
&&_unknown_opcode,
102102
&&TARGET_LOAD_CONST,
103103
&&TARGET_LOAD_NAME,
104104
&&TARGET_BUILD_TUPLE,

0 commit comments

Comments
 (0)