Skip to content

Commit 28e45f9

Browse files
committed
Super can be reformulated as macro with a special op
1 parent 34aa393 commit 28e45f9

File tree

2 files changed

+107
-48
lines changed

2 files changed

+107
-48
lines changed

Python/bytecodes.c

+20-11
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ do { \
7373
#define op(name, ...) /* NAME is ignored */
7474
#define macro(name) static int MACRO_##name
7575
#define super(name) static int SUPER_##name
76-
#define family(name, ...) static int family_##name
76+
#define family(name, ...) static int family_##name[]
7777

7878
#define NAME_ERROR_MSG \
7979
"name '%.200s' is not defined"
@@ -86,6 +86,7 @@ static PyObject *exit_func, *lasti, *val, *retval, *obj, *iter;
8686
static size_t jump;
8787
// Dummy variables for cache effects
8888
static _Py_CODEUNIT when_to_jump_mask, invert, counter, index, hint;
89+
static _Py_CODEUNIT word;
8990
static uint32_t type_version;
9091
// Dummy opcode names for 'op' opcodes
9192
#define _BINARY_OP_INPLACE_ADD_UNICODE_PART_1 1001
@@ -94,6 +95,7 @@ static uint32_t type_version;
9495
#define _COMPARE_OP_INT 1004
9596
#define _COMPARE_OP_STR 1005
9697
#define _JUMP_ON_SIGN 1006
98+
#define JOIN 0
9799

98100
static PyObject *
99101
dummy_func(
@@ -156,11 +158,18 @@ dummy_func(
156158
SETLOCAL(oparg, value);
157159
}
158160

159-
super(LOAD_FAST__LOAD_FAST) = LOAD_FAST + LOAD_FAST;
160-
super(LOAD_FAST__LOAD_CONST) = LOAD_FAST + LOAD_CONST;
161-
super(STORE_FAST__LOAD_FAST) = STORE_FAST + LOAD_FAST;
162-
super(STORE_FAST__STORE_FAST) = STORE_FAST + STORE_FAST;
163-
super(LOAD_CONST__LOAD_FAST) = LOAD_CONST + LOAD_FAST;
161+
op(JOIN, (word/1 --)) {
162+
#ifndef NDEBUG
163+
opcode = _Py_OPCODE(word);
164+
#endif
165+
oparg = _Py_OPARG(word);
166+
}
167+
168+
macro(LOAD_FAST__LOAD_FAST) = LOAD_FAST + JOIN + LOAD_FAST;
169+
macro(LOAD_FAST__LOAD_CONST) = LOAD_FAST + JOIN + LOAD_CONST;
170+
macro(STORE_FAST__LOAD_FAST) = STORE_FAST + JOIN + LOAD_FAST;
171+
macro(STORE_FAST__STORE_FAST) = STORE_FAST + JOIN + STORE_FAST;
172+
macro(LOAD_CONST__LOAD_FAST) = LOAD_CONST + JOIN + LOAD_FAST;
164173

165174
inst(POP_TOP, (value --)) {
166175
Py_DECREF(value);
@@ -307,8 +316,8 @@ dummy_func(
307316
op(_BINARY_OP_INPLACE_ADD_UNICODE_PART_2, (unused --)) {
308317
// The STORE_FAST is already done; oparg is dead.
309318
}
310-
super(BINARY_OP_INPLACE_ADD_UNICODE) =
311-
_BINARY_OP_INPLACE_ADD_UNICODE_PART_1 + _BINARY_OP_INPLACE_ADD_UNICODE_PART_2;
319+
macro(BINARY_OP_INPLACE_ADD_UNICODE) =
320+
_BINARY_OP_INPLACE_ADD_UNICODE_PART_1 + JOIN + _BINARY_OP_INPLACE_ADD_UNICODE_PART_2;
312321

313322
inst(BINARY_OP_ADD_FLOAT, (left, right, unused/1 -- sum)) {
314323
assert(cframe.use_tracing == 0);
@@ -2042,7 +2051,7 @@ dummy_func(
20422051
}
20432052
}
20442053
// We're praying that the compiler optimizes the flags manipuations.
2045-
super(COMPARE_OP_FLOAT_JUMP) = _COMPARE_OP_FLOAT + _JUMP_ON_SIGN;
2054+
macro(COMPARE_OP_FLOAT_JUMP) = _COMPARE_OP_FLOAT + JOIN + _JUMP_ON_SIGN;
20462055

20472056
// Similar to COMPARE_OP_FLOAT
20482057
op(_COMPARE_OP_INT, (unused/1, left, right, when_to_jump_mask/1 -- jump: size_t)) {
@@ -2062,7 +2071,7 @@ dummy_func(
20622071
_Py_DECREF_SPECIALIZED(right, (destructor)PyObject_Free);
20632072
jump = sign_ish & when_to_jump_mask;
20642073
}
2065-
super(COMPARE_OP_INT_JUMP) = _COMPARE_OP_INT + _JUMP_ON_SIGN;
2074+
macro(COMPARE_OP_INT_JUMP) = _COMPARE_OP_INT + JOIN + _JUMP_ON_SIGN;
20662075

20672076
// Similar to COMPARE_OP_FLOAT, but for ==, != only
20682077
op(_COMPARE_OP_STR, (unused/1, left, right, invert/1 -- jump: size_t)) {
@@ -2079,7 +2088,7 @@ dummy_func(
20792088
assert(invert == 0 || invert == 1);
20802089
jump = res ^ invert;
20812090
}
2082-
super(COMPARE_OP_STR_JUMP) = _COMPARE_OP_STR + _JUMP_ON_SIGN;
2091+
macro(COMPARE_OP_STR_JUMP) = _COMPARE_OP_STR + JOIN + _JUMP_ON_SIGN;
20832092

20842093
// stack effect: (__0 -- )
20852094
inst(IS_OP) {

Python/generated_cases.c.h

+87-37
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)