Skip to content

Commit 0864a69

Browse files
committed
py: Clean up unary and binary enum list to keep groups together.
2 non-bytecode binary ops (NOT_IN and IN_NOT) are moved out of the bytecode group, so this change will change the bytecode format.
1 parent f869d6b commit 0864a69

5 files changed

Lines changed: 34 additions & 28 deletions

File tree

py/bc0.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113
#define MP_BC_LOAD_CONST_SMALL_INT_MULTI (0x70) // + N(64)
114114
#define MP_BC_LOAD_FAST_MULTI (0xb0) // + N(16)
115115
#define MP_BC_STORE_FAST_MULTI (0xc0) // + N(16)
116-
#define MP_BC_UNARY_OP_MULTI (0xd0) // + op(<MP_UNARY_OP_NON_BYTECODE)
117-
#define MP_BC_BINARY_OP_MULTI (0xd7) // + op(36)
116+
#define MP_BC_UNARY_OP_MULTI (0xd0) // + op(<MP_UNARY_OP_NUM_BYTECODE)
117+
#define MP_BC_BINARY_OP_MULTI (0xd7) // + op(<MP_BINARY_OP_NUM_BYTECODE)
118118

119119
#endif // MICROPY_INCLUDED_PY_BC0_H

py/runtime0.h

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,55 +51,52 @@ typedef enum {
5151
MP_UNARY_OP_NOT,
5252

5353
// Following ops cannot appear in the bytecode
54-
MP_UNARY_OP_NON_BYTECODE,
54+
MP_UNARY_OP_NUM_BYTECODE,
5555

56-
MP_UNARY_OP_BOOL = MP_UNARY_OP_NON_BYTECODE, // __bool__
56+
MP_UNARY_OP_BOOL = MP_UNARY_OP_NUM_BYTECODE, // __bool__
5757
MP_UNARY_OP_LEN, // __len__
5858
MP_UNARY_OP_HASH, // __hash__; must return a small int
5959
MP_UNARY_OP_ABS, // __abs__
6060
MP_UNARY_OP_SIZEOF, // for sys.getsizeof()
61+
62+
MP_UNARY_OP_NUM_RUNTIME,
6163
} mp_unary_op_t;
6264

63-
// Note: the first 35 of these are used in bytecode and changing
65+
// Note: the first 9+12+12 of these are used in bytecode and changing
6466
// them requires changing the bytecode version.
6567
typedef enum {
66-
// Relational operations, should return a bool
68+
// 9 relational operations, should return a bool
6769
MP_BINARY_OP_LESS,
6870
MP_BINARY_OP_MORE,
6971
MP_BINARY_OP_EQUAL,
7072
MP_BINARY_OP_LESS_EQUAL,
7173
MP_BINARY_OP_MORE_EQUAL,
7274
MP_BINARY_OP_NOT_EQUAL,
73-
7475
MP_BINARY_OP_IN,
7576
MP_BINARY_OP_IS,
7677
MP_BINARY_OP_EXCEPTION_MATCH,
77-
// these are not supported by the runtime and must be synthesised by the emitter
78-
MP_BINARY_OP_NOT_IN,
79-
MP_BINARY_OP_IS_NOT,
8078

81-
// Arithmetic operations
79+
// 12 inplace arithmetic operations
8280
MP_BINARY_OP_INPLACE_OR,
8381
MP_BINARY_OP_INPLACE_XOR,
8482
MP_BINARY_OP_INPLACE_AND,
8583
MP_BINARY_OP_INPLACE_LSHIFT,
8684
MP_BINARY_OP_INPLACE_RSHIFT,
8785
MP_BINARY_OP_INPLACE_ADD,
88-
8986
MP_BINARY_OP_INPLACE_SUBTRACT,
9087
MP_BINARY_OP_INPLACE_MULTIPLY,
9188
MP_BINARY_OP_INPLACE_FLOOR_DIVIDE,
9289
MP_BINARY_OP_INPLACE_TRUE_DIVIDE,
9390
MP_BINARY_OP_INPLACE_MODULO,
9491
MP_BINARY_OP_INPLACE_POWER,
9592

93+
// 12 normal arithmetic operations
9694
MP_BINARY_OP_OR,
9795
MP_BINARY_OP_XOR,
9896
MP_BINARY_OP_AND,
9997
MP_BINARY_OP_LSHIFT,
10098
MP_BINARY_OP_RSHIFT,
10199
MP_BINARY_OP_ADD,
102-
103100
MP_BINARY_OP_SUBTRACT,
104101
MP_BINARY_OP_MULTIPLY,
105102
MP_BINARY_OP_FLOOR_DIVIDE,
@@ -109,16 +106,16 @@ typedef enum {
109106

110107
// Operations below this line don't appear in bytecode, they
111108
// just identify special methods.
109+
MP_BINARY_OP_NUM_BYTECODE,
112110

113111
// MP_BINARY_OP_REVERSE_* must follow immediately after MP_BINARY_OP_*
114112
#if MICROPY_PY_REVERSE_SPECIAL_METHODS
115-
MP_BINARY_OP_REVERSE_OR,
113+
MP_BINARY_OP_REVERSE_OR = MP_BINARY_OP_NUM_BYTECODE,
116114
MP_BINARY_OP_REVERSE_XOR,
117115
MP_BINARY_OP_REVERSE_AND,
118116
MP_BINARY_OP_REVERSE_LSHIFT,
119117
MP_BINARY_OP_REVERSE_RSHIFT,
120118
MP_BINARY_OP_REVERSE_ADD,
121-
122119
MP_BINARY_OP_REVERSE_SUBTRACT,
123120
MP_BINARY_OP_REVERSE_MULTIPLY,
124121
MP_BINARY_OP_REVERSE_FLOOR_DIVIDE,
@@ -127,9 +124,18 @@ typedef enum {
127124
MP_BINARY_OP_REVERSE_POWER,
128125
#endif
129126

130-
MP_BINARY_OP_DIVMOD, // not emitted by the compiler but supported by the runtime
127+
// This is not emitted by the compiler but is supported by the runtime
128+
MP_BINARY_OP_DIVMOD
129+
#if !MICROPY_PY_REVERSE_SPECIAL_METHODS
130+
= MP_BINARY_OP_NUM_BYTECODE
131+
#endif
132+
,
133+
134+
MP_BINARY_OP_NUM_RUNTIME,
131135

132-
MP_BINARY_OP_LAST,
136+
// These 2 are not supported by the runtime and must be synthesised by the emitter
137+
MP_BINARY_OP_NOT_IN,
138+
MP_BINARY_OP_IS_NOT,
133139
} mp_binary_op_t;
134140

135141
typedef enum {

py/showbc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,9 +539,9 @@ const byte *mp_bytecode_print_str(const byte *ip) {
539539
printf("LOAD_FAST " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_LOAD_FAST_MULTI);
540540
} else if (ip[-1] < MP_BC_STORE_FAST_MULTI + 16) {
541541
printf("STORE_FAST " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_STORE_FAST_MULTI);
542-
} else if (ip[-1] < MP_BC_UNARY_OP_MULTI + MP_UNARY_OP_NON_BYTECODE) {
542+
} else if (ip[-1] < MP_BC_UNARY_OP_MULTI + MP_UNARY_OP_NUM_BYTECODE) {
543543
printf("UNARY_OP " UINT_FMT, (mp_uint_t)ip[-1] - MP_BC_UNARY_OP_MULTI);
544-
} else if (ip[-1] < MP_BC_BINARY_OP_MULTI + 36) {
544+
} else if (ip[-1] < MP_BC_BINARY_OP_MULTI + MP_BINARY_OP_NUM_BYTECODE) {
545545
mp_uint_t op = ip[-1] - MP_BC_BINARY_OP_MULTI;
546546
printf("BINARY_OP " UINT_FMT " %s", op, qstr_str(mp_binary_op_method_name[op]));
547547
} else {

py/vmentrytable.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ static const void *const entry_table[256] = {
109109
[MP_BC_LOAD_CONST_SMALL_INT_MULTI ... MP_BC_LOAD_CONST_SMALL_INT_MULTI + 63] = &&entry_MP_BC_LOAD_CONST_SMALL_INT_MULTI,
110110
[MP_BC_LOAD_FAST_MULTI ... MP_BC_LOAD_FAST_MULTI + 15] = &&entry_MP_BC_LOAD_FAST_MULTI,
111111
[MP_BC_STORE_FAST_MULTI ... MP_BC_STORE_FAST_MULTI + 15] = &&entry_MP_BC_STORE_FAST_MULTI,
112-
[MP_BC_UNARY_OP_MULTI ... MP_BC_UNARY_OP_MULTI + MP_UNARY_OP_NON_BYTECODE - 1] = &&entry_MP_BC_UNARY_OP_MULTI,
113-
[MP_BC_BINARY_OP_MULTI ... MP_BC_BINARY_OP_MULTI + 35] = &&entry_MP_BC_BINARY_OP_MULTI,
112+
[MP_BC_UNARY_OP_MULTI ... MP_BC_UNARY_OP_MULTI + MP_UNARY_OP_NUM_BYTECODE - 1] = &&entry_MP_BC_UNARY_OP_MULTI,
113+
[MP_BC_BINARY_OP_MULTI ... MP_BC_BINARY_OP_MULTI + MP_BINARY_OP_NUM_BYTECODE - 1] = &&entry_MP_BC_BINARY_OP_MULTI,
114114
};
115115

116116
#if __clang__

tests/cmdline/cmd_showbc.py.exp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ Raw bytecode (code_info_size=\\d\+, bytecode_size=\\d\+):
4343
bc=\\d\+ line=126
4444
00 LOAD_CONST_NONE
4545
01 LOAD_CONST_FALSE
46-
02 BINARY_OP 28 __add__
46+
02 BINARY_OP 26 __add__
4747
03 LOAD_CONST_TRUE
48-
04 BINARY_OP 28 __add__
48+
04 BINARY_OP 26 __add__
4949
05 STORE_FAST 0
5050
06 LOAD_CONST_SMALL_INT 0
5151
07 STORE_FAST 0
@@ -84,7 +84,7 @@ Raw bytecode (code_info_size=\\d\+, bytecode_size=\\d\+):
8484
\\d\+ STORE_FAST 7
8585
\\d\+ LOAD_FAST 0
8686
\\d\+ LOAD_DEREF 14
87-
\\d\+ BINARY_OP 28 __add__
87+
\\d\+ BINARY_OP 26 __add__
8888
\\d\+ STORE_FAST 8
8989
\\d\+ LOAD_FAST 0
9090
\\d\+ UNARY_OP 1
@@ -132,7 +132,7 @@ Raw bytecode (code_info_size=\\d\+, bytecode_size=\\d\+):
132132
\\d\+ DUP_TOP_TWO
133133
\\d\+ LOAD_SUBSCR
134134
\\d\+ LOAD_FAST 12
135-
\\d\+ BINARY_OP 16 __iadd__
135+
\\d\+ BINARY_OP 14 __iadd__
136136
\\d\+ ROT_THREE
137137
\\d\+ STORE_SUBSCR
138138
\\d\+ LOAD_DEREF 14
@@ -369,7 +369,7 @@ Raw bytecode (code_info_size=\\d\+, bytecode_size=\\d\+):
369369
42 STORE_FAST_N 19
370370
44 LOAD_FAST 9
371371
45 LOAD_FAST_N 19
372-
47 BINARY_OP 28 __add__
372+
47 BINARY_OP 26 __add__
373373
48 POP_TOP
374374
49 LOAD_CONST_NONE
375375
50 RETURN_VALUE
@@ -521,7 +521,7 @@ arg names: *
521521
bc=\\d\+ line=113
522522
00 LOAD_DEREF 0
523523
02 LOAD_CONST_SMALL_INT 1
524-
03 BINARY_OP 28 __add__
524+
03 BINARY_OP 26 __add__
525525
04 STORE_FAST 1
526526
05 LOAD_CONST_SMALL_INT 1
527527
06 STORE_DEREF 0
@@ -540,7 +540,7 @@ arg names: * b
540540
bc=\\d\+ line=139
541541
00 LOAD_FAST 1
542542
01 LOAD_DEREF 0
543-
03 BINARY_OP 28 __add__
543+
03 BINARY_OP 26 __add__
544544
04 RETURN_VALUE
545545
mem: total=\\d\+, current=\\d\+, peak=\\d\+
546546
stack: \\d\+ out of \\d\+

0 commit comments

Comments
 (0)