Skip to content

Commit a5c82a8

Browse files
committed
py: Convert some macros to inline functions (in obj.h).
Also convert mp_obj_is_integer to an inline function. Overall this decreased code size (at least on 32-bit x86 machine).
1 parent e22d76e commit a5c82a8

2 files changed

Lines changed: 23 additions & 17 deletions

File tree

py/obj.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,6 @@ int mp_obj_is_true(mp_obj_t arg) {
9999
}
100100
}
101101

102-
// returns true if o_in is bool, small int, or long int
103-
bool mp_obj_is_integer(mp_obj_t o_in) {
104-
return MP_OBJ_IS_INT(o_in) || MP_OBJ_IS_TYPE(o_in, &mp_type_bool);
105-
}
106-
107102
bool mp_obj_is_callable(mp_obj_t o_in) {
108103
return mp_obj_get_type(o_in)->call != NULL;
109104
}

py/obj.h

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ typedef struct _mp_obj_base_t mp_obj_base_t;
2121
// The NULL object is used to indicate the absence of an object
2222
// It *cannot* be used when an mp_obj_t is expected, except where explicitly allowed
2323

24-
#define MP_OBJ_NULL ((mp_obj_t)NULL)
24+
#define MP_OBJ_NULL ((mp_obj_t)0)
2525

2626
// The SENTINEL object is used for various internal purposes where one needs
2727
// an object which is unique from all other objects, including MP_OBJ_NULL.
@@ -37,12 +37,13 @@ typedef struct _mp_obj_base_t mp_obj_base_t;
3737
#define MP_SMALL_INT_MIN ((mp_small_int_t)(((machine_int_t)WORD_MSBIT_HIGH) >> 1))
3838
#define MP_SMALL_INT_MAX ((mp_small_int_t)(~(MP_SMALL_INT_MIN)))
3939
#define MP_OBJ_FITS_SMALL_INT(n) ((((n) ^ ((n) << 1)) & WORD_MSBIT_HIGH) == 0)
40-
#define MP_OBJ_IS_SMALL_INT(o) ((((mp_small_int_t)(o)) & 1) != 0)
41-
#define MP_OBJ_IS_QSTR(o) ((((mp_small_int_t)(o)) & 3) == 2)
42-
#define MP_OBJ_IS_OBJ(o) ((((mp_small_int_t)(o)) & 3) == 0)
43-
#define MP_OBJ_IS_TYPE(o, t) (MP_OBJ_IS_OBJ(o) && (((mp_obj_base_t*)(o))->type == (t))) // this does not work for checking a string, use below macro for that
44-
#define MP_OBJ_IS_INT(o) (MP_OBJ_IS_SMALL_INT(o) || MP_OBJ_IS_TYPE(o, &mp_type_int))
45-
#define MP_OBJ_IS_STR(o) (MP_OBJ_IS_QSTR(o) || MP_OBJ_IS_TYPE(o, &mp_type_str))
40+
// these macros have now become inline functions; see below
41+
//#define MP_OBJ_IS_SMALL_INT(o) ((((mp_small_int_t)(o)) & 1) != 0)
42+
//#define MP_OBJ_IS_QSTR(o) ((((mp_small_int_t)(o)) & 3) == 2)
43+
//#define MP_OBJ_IS_OBJ(o) ((((mp_small_int_t)(o)) & 3) == 0)
44+
//#define MP_OBJ_IS_TYPE(o, t) (MP_OBJ_IS_OBJ(o) && (((mp_obj_base_t*)(o))->type == (t))) // this does not work for checking a string, use below macro for that
45+
//#define MP_OBJ_IS_INT(o) (MP_OBJ_IS_SMALL_INT(o) || MP_OBJ_IS_TYPE(o, &mp_type_int))
46+
//#define MP_OBJ_IS_STR(o) (MP_OBJ_IS_QSTR(o) || MP_OBJ_IS_TYPE(o, &mp_type_str))
4647

4748
#define MP_OBJ_SMALL_INT_VALUE(o) (((mp_small_int_t)(o)) >> 1)
4849
#define MP_OBJ_NEW_SMALL_INT(small_int) ((mp_obj_t)(((small_int) << 1) | 1))
@@ -115,7 +116,7 @@ typedef enum _mp_map_lookup_kind_t {
115116
MP_MAP_LOOKUP_REMOVE_IF_FOUND, // 2
116117
} mp_map_lookup_kind_t;
117118

118-
#define MP_MAP_SLOT_IS_FILLED(map, pos) ((map)->table[pos].key != MP_OBJ_NULL && (map)->table[pos].key != MP_OBJ_SENTINEL)
119+
inline bool MP_MAP_SLOT_IS_FILLED(mp_map_t *map, machine_uint_t pos) { return ((map)->table[pos].key != MP_OBJ_NULL && (map)->table[pos].key != MP_OBJ_SENTINEL); }
119120

120121
void mp_map_init(mp_map_t *map, int n);
121122
void mp_map_init_fixed_table(mp_map_t *map, int n, const mp_obj_t *table);
@@ -134,7 +135,7 @@ typedef struct _mp_set_t {
134135
mp_obj_t *table;
135136
} mp_set_t;
136137

137-
#define MP_SET_SLOT_IS_FILLED(set, pos) ((set)->table[pos] != MP_OBJ_NULL && (set)->table[pos] != MP_OBJ_SENTINEL)
138+
inline bool MP_SET_SLOT_IS_FILLED(mp_set_t *set, machine_uint_t pos) { return ((set)->table[pos] != MP_OBJ_NULL && (set)->table[pos] != MP_OBJ_SENTINEL); }
138139

139140
void mp_set_init(mp_set_t *set, int n);
140141
mp_obj_t mp_set_lookup(mp_set_t *set, mp_obj_t index, mp_map_lookup_kind_t lookup_kind);
@@ -369,7 +370,16 @@ void mp_obj_print(mp_obj_t o, mp_print_kind_t kind);
369370
void mp_obj_print_exception(mp_obj_t exc);
370371

371372
int mp_obj_is_true(mp_obj_t arg);
372-
bool mp_obj_is_integer(mp_obj_t o_in); // returns true if o_in is bool, small int, or long int
373+
374+
// TODO make these all lower case when they have proven themselves
375+
inline bool MP_OBJ_IS_OBJ(mp_const_obj_t o) { return ((((mp_small_int_t)(o)) & 3) == 0); }
376+
inline bool MP_OBJ_IS_TYPE(mp_const_obj_t o, const mp_obj_type_t *t) { return (MP_OBJ_IS_OBJ(o) && (((mp_obj_base_t*)(o))->type == (t))); } // this does not work for checking a string, use below macro for that
377+
inline bool MP_OBJ_IS_SMALL_INT(mp_const_obj_t o) { return ((((mp_small_int_t)(o)) & 1) != 0); }
378+
inline bool MP_OBJ_IS_INT(mp_const_obj_t o) { return (MP_OBJ_IS_SMALL_INT(o) || MP_OBJ_IS_TYPE(o, &mp_type_int)); } // returns true if o is a small int or long int
379+
inline bool mp_obj_is_integer(mp_const_obj_t o) { return MP_OBJ_IS_INT(o) || MP_OBJ_IS_TYPE(o, &mp_type_bool); } // returns true if o is bool, small int or long int
380+
inline bool MP_OBJ_IS_QSTR(mp_const_obj_t o) { return ((((mp_small_int_t)(o)) & 3) == 2); }
381+
inline bool MP_OBJ_IS_STR(mp_const_obj_t o) { return (MP_OBJ_IS_QSTR(o) || MP_OBJ_IS_TYPE(o, &mp_type_str)); }
382+
373383
bool mp_obj_is_callable(mp_obj_t o_in);
374384
machine_int_t mp_obj_hash(mp_obj_t o_in);
375385
bool mp_obj_equal(mp_obj_t o1, mp_obj_t o2);
@@ -384,10 +394,11 @@ void mp_obj_get_complex(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag);
384394
void mp_obj_get_array(mp_obj_t o, uint *len, mp_obj_t **items);
385395
void mp_obj_get_array_fixed_n(mp_obj_t o, uint len, mp_obj_t **items);
386396
uint mp_get_index(const mp_obj_type_t *type, machine_uint_t len, mp_obj_t index, bool is_slice);
387-
mp_obj_t mp_obj_len_maybe(mp_obj_t o_in); /* may return NULL */
397+
mp_obj_t mp_obj_len_maybe(mp_obj_t o_in); /* may return MP_OBJ_NULL */
388398

389399
// bool
390-
#define MP_BOOL(x) (x ? mp_const_true : mp_const_false)
400+
// TODO make lower case when it has proven itself
401+
inline mp_obj_t MP_BOOL(machine_int_t x) { return x ? mp_const_true : mp_const_false; }
391402

392403
// cell
393404
mp_obj_t mp_obj_cell_get(mp_obj_t self_in);

0 commit comments

Comments
 (0)