Skip to content

Commit 58321dd

Browse files
committed
all: Convert mp_uint_t to mp_unary_op_t/mp_binary_op_t where appropriate
The unary-op/binary-op enums are already defined, and there are no arithmetic tricks used with these types, so it makes sense to use the correct enum type for arguments that take these values. It also reduces code size quite a bit for nan-boxing builds.
1 parent be8e574 commit 58321dd

25 files changed

+54
-52
lines changed

extmod/modbtree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ STATIC mp_obj_t btree_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
281281
}
282282
}
283283

284-
STATIC mp_obj_t btree_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
284+
STATIC mp_obj_t btree_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
285285
mp_obj_btree_t *self = MP_OBJ_TO_PTR(lhs_in);
286286
switch (op) {
287287
case MP_BINARY_OP_IN: {

extmod/modutimeq.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ STATIC mp_obj_t mod_utimeq_dump(mp_obj_t heap_in) {
189189
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_utimeq_dump_obj, mod_utimeq_dump);
190190
#endif
191191

192-
STATIC mp_obj_t utimeq_unary_op(mp_uint_t op, mp_obj_t self_in) {
192+
STATIC mp_obj_t utimeq_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
193193
mp_obj_utimeq_t *self = MP_OBJ_TO_PTR(self_in);
194194
switch (op) {
195195
case MP_UNARY_OP_BOOL: return mp_obj_new_bool(self->len != 0);

py/obj.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ void mp_get_buffer_raise(mp_obj_t obj, mp_buffer_info_t *bufinfo, mp_uint_t flag
505505
}
506506
}
507507

508-
mp_obj_t mp_generic_unary_op(mp_uint_t op, mp_obj_t o_in) {
508+
mp_obj_t mp_generic_unary_op(mp_unary_op_t op, mp_obj_t o_in) {
509509
switch (op) {
510510
case MP_UNARY_OP_HASH: return MP_OBJ_NEW_SMALL_INT((mp_uint_t)o_in);
511511
default: return MP_OBJ_NULL; // op not supported

py/obj.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "py/misc.h"
3131
#include "py/qstr.h"
3232
#include "py/mpprint.h"
33+
#include "py/runtime0.h"
3334

3435
// This is the definition of the opaque MicroPython object type.
3536
// All concrete objects have an encoding within this type and the
@@ -429,8 +430,8 @@ typedef struct _mp_obj_iter_buf_t {
429430
typedef void (*mp_print_fun_t)(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind);
430431
typedef mp_obj_t (*mp_make_new_fun_t)(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args);
431432
typedef mp_obj_t (*mp_call_fun_t)(mp_obj_t fun, size_t n_args, size_t n_kw, const mp_obj_t *args);
432-
typedef mp_obj_t (*mp_unary_op_fun_t)(mp_uint_t op, mp_obj_t);
433-
typedef mp_obj_t (*mp_binary_op_fun_t)(mp_uint_t op, mp_obj_t, mp_obj_t);
433+
typedef mp_obj_t (*mp_unary_op_fun_t)(mp_unary_op_t op, mp_obj_t);
434+
typedef mp_obj_t (*mp_binary_op_fun_t)(mp_binary_op_t op, mp_obj_t, mp_obj_t);
434435
typedef void (*mp_attr_fun_t)(mp_obj_t self_in, qstr attr, mp_obj_t *dest);
435436
typedef mp_obj_t (*mp_subscr_fun_t)(mp_obj_t self_in, mp_obj_t index, mp_obj_t value);
436437
typedef mp_obj_t (*mp_getiter_fun_t)(mp_obj_t self_in, mp_obj_iter_buf_t *iter_buf);
@@ -694,7 +695,7 @@ mp_obj_t mp_obj_id(mp_obj_t o_in);
694695
mp_obj_t mp_obj_len(mp_obj_t o_in);
695696
mp_obj_t mp_obj_len_maybe(mp_obj_t o_in); // may return MP_OBJ_NULL
696697
mp_obj_t mp_obj_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t val);
697-
mp_obj_t mp_generic_unary_op(mp_uint_t op, mp_obj_t o_in);
698+
mp_obj_t mp_generic_unary_op(mp_unary_op_t op, mp_obj_t o_in);
698699

699700
// cell
700701
mp_obj_t mp_obj_cell_get(mp_obj_t self_in);
@@ -734,11 +735,11 @@ mp_int_t mp_float_hash(mp_float_t val);
734735
#else
735736
static inline mp_int_t mp_float_hash(mp_float_t val) { return (mp_int_t)val; }
736737
#endif
737-
mp_obj_t mp_obj_float_binary_op(mp_uint_t op, mp_float_t lhs_val, mp_obj_t rhs); // can return MP_OBJ_NULL if op not supported
738+
mp_obj_t mp_obj_float_binary_op(mp_binary_op_t op, mp_float_t lhs_val, mp_obj_t rhs); // can return MP_OBJ_NULL if op not supported
738739

739740
// complex
740741
void mp_obj_complex_get(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag);
741-
mp_obj_t mp_obj_complex_binary_op(mp_uint_t op, mp_float_t lhs_real, mp_float_t lhs_imag, mp_obj_t rhs_in); // can return MP_OBJ_NULL if op not supported
742+
mp_obj_t mp_obj_complex_binary_op(mp_binary_op_t op, mp_float_t lhs_real, mp_float_t lhs_imag, mp_obj_t rhs_in); // can return MP_OBJ_NULL if op not supported
742743
#else
743744
#define mp_obj_is_float(o) (false)
744745
#endif

py/objarray.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ STATIC mp_obj_t memoryview_make_new(const mp_obj_type_t *type_in, size_t n_args,
231231
}
232232
#endif
233233

234-
STATIC mp_obj_t array_unary_op(mp_uint_t op, mp_obj_t o_in) {
234+
STATIC mp_obj_t array_unary_op(mp_unary_op_t op, mp_obj_t o_in) {
235235
mp_obj_array_t *o = MP_OBJ_TO_PTR(o_in);
236236
switch (op) {
237237
case MP_UNARY_OP_BOOL: return mp_obj_new_bool(o->len != 0);
@@ -240,7 +240,7 @@ STATIC mp_obj_t array_unary_op(mp_uint_t op, mp_obj_t o_in) {
240240
}
241241
}
242242

243-
STATIC mp_obj_t array_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
243+
STATIC mp_obj_t array_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
244244
mp_obj_array_t *lhs = MP_OBJ_TO_PTR(lhs_in);
245245
switch (op) {
246246
case MP_BINARY_OP_ADD: {

py/objbool.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@ STATIC mp_obj_t bool_make_new(const mp_obj_type_t *type_in, size_t n_args, size_
6363
}
6464
}
6565

66-
STATIC mp_obj_t bool_unary_op(mp_uint_t op, mp_obj_t o_in) {
66+
STATIC mp_obj_t bool_unary_op(mp_unary_op_t op, mp_obj_t o_in) {
6767
if (op == MP_UNARY_OP_LEN) {
6868
return MP_OBJ_NULL;
6969
}
7070
mp_obj_bool_t *self = MP_OBJ_TO_PTR(o_in);
7171
return mp_unary_op(op, MP_OBJ_NEW_SMALL_INT(self->value));
7272
}
7373

74-
STATIC mp_obj_t bool_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
74+
STATIC mp_obj_t bool_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
7575
mp_obj_bool_t *self = MP_OBJ_TO_PTR(lhs_in);
7676
return mp_binary_op(op, MP_OBJ_NEW_SMALL_INT(self->value), rhs_in);
7777
}

py/objcomplex.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ STATIC mp_obj_t complex_make_new(const mp_obj_type_t *type_in, size_t n_args, si
117117
}
118118
}
119119

120-
STATIC mp_obj_t complex_unary_op(mp_uint_t op, mp_obj_t o_in) {
120+
STATIC mp_obj_t complex_unary_op(mp_unary_op_t op, mp_obj_t o_in) {
121121
mp_obj_complex_t *o = MP_OBJ_TO_PTR(o_in);
122122
switch (op) {
123123
case MP_UNARY_OP_BOOL: return mp_obj_new_bool(o->real != 0 || o->imag != 0);
@@ -128,7 +128,7 @@ STATIC mp_obj_t complex_unary_op(mp_uint_t op, mp_obj_t o_in) {
128128
}
129129
}
130130

131-
STATIC mp_obj_t complex_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
131+
STATIC mp_obj_t complex_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
132132
mp_obj_complex_t *lhs = MP_OBJ_TO_PTR(lhs_in);
133133
return mp_obj_complex_binary_op(op, lhs->real, lhs->imag, rhs_in);
134134
}
@@ -171,7 +171,7 @@ void mp_obj_complex_get(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag) {
171171
*imag = self->imag;
172172
}
173173

174-
mp_obj_t mp_obj_complex_binary_op(mp_uint_t op, mp_float_t lhs_real, mp_float_t lhs_imag, mp_obj_t rhs_in) {
174+
mp_obj_t mp_obj_complex_binary_op(mp_binary_op_t op, mp_float_t lhs_real, mp_float_t lhs_imag, mp_obj_t rhs_in) {
175175
mp_float_t rhs_real, rhs_imag;
176176
mp_obj_get_complex(rhs_in, &rhs_real, &rhs_imag); // can be any type, this function will convert to float (if possible)
177177
switch (op) {

py/objdict.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ STATIC mp_obj_t dict_make_new(const mp_obj_type_t *type, size_t n_args, size_t n
100100
return dict_out;
101101
}
102102

103-
STATIC mp_obj_t dict_unary_op(mp_uint_t op, mp_obj_t self_in) {
103+
STATIC mp_obj_t dict_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
104104
mp_obj_dict_t *self = MP_OBJ_TO_PTR(self_in);
105105
switch (op) {
106106
case MP_UNARY_OP_BOOL: return mp_obj_new_bool(self->map.used != 0);
@@ -115,7 +115,7 @@ STATIC mp_obj_t dict_unary_op(mp_uint_t op, mp_obj_t self_in) {
115115
}
116116
}
117117

118-
STATIC mp_obj_t dict_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
118+
STATIC mp_obj_t dict_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
119119
mp_obj_dict_t *o = MP_OBJ_TO_PTR(lhs_in);
120120
switch (op) {
121121
case MP_BINARY_OP_IN: {
@@ -482,7 +482,7 @@ STATIC void dict_view_print(const mp_print_t *print, mp_obj_t self_in, mp_print_
482482
mp_print_str(print, "])");
483483
}
484484

485-
STATIC mp_obj_t dict_view_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
485+
STATIC mp_obj_t dict_view_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
486486
// only supported for the 'keys' kind until sets and dicts are refactored
487487
mp_obj_dict_view_t *o = MP_OBJ_TO_PTR(lhs_in);
488488
if (o->kind != MP_DICT_VIEW_KEYS) {

py/objfloat.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ STATIC mp_obj_t float_make_new(const mp_obj_type_t *type_in, size_t n_args, size
155155
}
156156
}
157157

158-
STATIC mp_obj_t float_unary_op(mp_uint_t op, mp_obj_t o_in) {
158+
STATIC mp_obj_t float_unary_op(mp_unary_op_t op, mp_obj_t o_in) {
159159
mp_float_t val = mp_obj_float_get(o_in);
160160
switch (op) {
161161
case MP_UNARY_OP_BOOL: return mp_obj_new_bool(val != 0);
@@ -166,7 +166,7 @@ STATIC mp_obj_t float_unary_op(mp_uint_t op, mp_obj_t o_in) {
166166
}
167167
}
168168

169-
STATIC mp_obj_t float_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
169+
STATIC mp_obj_t float_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
170170
mp_float_t lhs_val = mp_obj_float_get(lhs_in);
171171
#if MICROPY_PY_BUILTINS_COMPLEX
172172
if (MP_OBJ_IS_TYPE(rhs_in, &mp_type_complex)) {
@@ -239,7 +239,7 @@ STATIC void mp_obj_float_divmod(mp_float_t *x, mp_float_t *y) {
239239
*y = mod;
240240
}
241241

242-
mp_obj_t mp_obj_float_binary_op(mp_uint_t op, mp_float_t lhs_val, mp_obj_t rhs_in) {
242+
mp_obj_t mp_obj_float_binary_op(mp_binary_op_t op, mp_float_t lhs_val, mp_obj_t rhs_in) {
243243
mp_float_t rhs_val = mp_obj_get_float(rhs_in); // can be any type, this function will convert to float (if possible)
244244
switch (op) {
245245
case MP_BINARY_OP_ADD:

py/objint.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -325,12 +325,12 @@ mp_obj_t mp_obj_int_abs(mp_obj_t self_in) {
325325
}
326326

327327
// This is called for operations on SMALL_INT that are not handled by mp_unary_op
328-
mp_obj_t mp_obj_int_unary_op(mp_uint_t op, mp_obj_t o_in) {
328+
mp_obj_t mp_obj_int_unary_op(mp_unary_op_t op, mp_obj_t o_in) {
329329
return MP_OBJ_NULL; // op not supported
330330
}
331331

332332
// This is called for operations on SMALL_INT that are not handled by mp_binary_op
333-
mp_obj_t mp_obj_int_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
333+
mp_obj_t mp_obj_int_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
334334
return mp_obj_int_binary_op_extra_cases(op, lhs_in, rhs_in);
335335
}
336336

@@ -382,7 +382,7 @@ mp_int_t mp_obj_int_get_checked(mp_const_obj_t self_in) {
382382

383383
// This dispatcher function is expected to be independent of the implementation of long int
384384
// It handles the extra cases for integer-like arithmetic
385-
mp_obj_t mp_obj_int_binary_op_extra_cases(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
385+
mp_obj_t mp_obj_int_binary_op_extra_cases(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
386386
if (rhs_in == mp_const_false) {
387387
// false acts as 0
388388
return mp_binary_op(op, lhs_in, MP_OBJ_NEW_SMALL_INT(0));

0 commit comments

Comments
 (0)