8000 py/obj.h: Add slot-index mp_obj_type_t representation. · micropython/micropython@6f9b29e · GitHub
[go: up one dir, main page]

Skip to content

Commit 6f9b29e

Browse files
committed
py/obj.h: Add slot-index mp_obj_type_t representation.
1 parent 9b501ce commit 6f9b29e

File tree

3 files changed

+85
-9
lines changed

3 files changed

+85
-9
lines changed

py/mpconfig.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,17 @@
120120
#define MICROPY_OBJ_IMMEDIATE_OBJS (MICROPY_OBJ_REPR != MICROPY_OBJ_REPR_D)
121121
#endif
122122

123+
#define MICROPY_OBJ_TYPE_REPR_FULL (0)
124+
#define MICROPY_OBJ_TYPE_REPR_SLOT_INDEX (1)
125+
126+
#ifndef MICROPY_OBJ_TYPE_REPR
127+
#define MICROPY_OBJ_TYPE_REPR (MICROPY_OBJ_TYPE_REPR_FULL)
128+
#endif
129+
130+
#ifndef MICROPY_OBJ_TYPE_SLOT_INDEX_BITS
131+
#define MICROPY_OBJ_TYPE_SLOT_INDEX_BITS (8)
132+
#endif
133+
123134
/*****************************************************************************/
124135
/* Memory allocation policy */
125136

py/obj.h

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -532,12 +532,13 @@ struct _mp_obj_type_t {
532532
// The name of this type, a qstr.
533533
uint16_t name;
534534

535-
// Corresponds to __repr__ and __str__ special methods.
536-
mp_print_fun_t print;
537-
538535
// Corresponds to __new__ and __init__ special methods, to make an instance of the type.
539536
mp_make_new_fun_t make_new;
540537

538+
#if MICROPY_OBJ_TYPE_REPR == MICROPY_OBJ_TYPE_REPR_FULL
539+
// Corresponds to __repr__ and __str__ special methods.
540+
mp_print_fun_t print;
541+
541542
// Corresponds to __call__ special method, ie T(...).
542543
mp_call_fun_t call;
543544

@@ -589,8 +590,33 @@ struct _mp_obj_type_t {
589590

590591
// A dict mapping qstrs to objects local methods/constants/etc.
591592
struct _mp_obj_dict_t *locals_dict;
593+
594+
#elif MICROPY_OBJ_TYPE_REPR == MICROPY_OBJ_TYPE_REPR_SLOT_INDEX
595+
596+
unsigned slot_index_print : MICROPY_OBJ_TYPE_SLOT_INDEX_BITS;
597+
unsigned slot_index_call : MICROPY_OBJ_TYPE_SLOT_INDEX_BITS;
598+
unsigned slot_index_unary_op : MICROPY_OBJ_TYPE_SLOT_INDEX_BITS;
599+
unsigned slot_index_binary_op : MICROPY_OBJ_TYPE_SLOT_INDEX_BITS;
600+
unsigned slot_index_attr : MICROPY_OBJ_TYPE_SLOT_INDEX_BITS;
601+
unsigned slot_index_subscr : MICROPY_OBJ_TYPE_SLOT_INDEX_BITS;
602+
unsigned slot_index_getiter : MICROPY_OBJ_TYPE_SLOT_INDEX_BITS;
603+
unsigned slot_index_iternext : MICROPY_OBJ_TYPE_SLOT_INDEX_BITS;
604+
unsigned slot_index_buffer : MICROPY_OBJ_TYPE_SLOT_INDEX_BITS;
605+
unsigned slot_index_protocol : MICROPY_OBJ_TYPE_SLOT_INDEX_BITS;
606+
unsigned slot_index_parent : MICROPY_OBJ_TYPE_SLOT_INDEX_BITS;
607+
unsigned slot_index_locals_dict : MICROPY_OBJ_TYPE_SLOT_INDEX_BITS;
608+
609+
const void *slots[];
610+
611+
#endif
592612
};
593613

614+
#define MP_TYPE_NULL_MAKE_NEW (NULL)
615+
616+
#if MICROPY_OBJ_TYPE_REPR == MICROPY_OBJ_TYPE_REPR_FULL
617+
618+
#define MICROPY_OBJ_TYPE_EXTRA_ALLOC (0)
619+
594620
#define MP_DEFINE_CONST_OBJ_TYPE_0(_typename, _name, _flags, _make_new) const mp_obj_type_t _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new }
595621
#define MP_DEFINE_CONST_OBJ_TYPE_1(_typename, _name, _flags, _make_new, f1, v1) const mp_obj_type_t _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1 }
596622
#define MP_DEFINE_CONST_OBJ_TYPE_2(_typename, _name, _flags, _make_new, f1, v1, f2, v2) const mp_obj_type_t _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1, .f2 = v2 }
@@ -605,18 +631,57 @@ struct _mp_obj_type_t {
605631
#define MP_DEFINE_CONST_OBJ_TYPE_11(_typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8, f9, v9, f10, v10, f11, v11) const mp_obj_type_t _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1, .f2 = v2, .f3 = v3, .f4 = v4, .f5 = v5, .f6 = v6, .f7 = v7, .f8 = v8, .f9 = v9, .f10 = v10, .f11 = v11 }
606632
#define MP_DEFINE_CONST_OBJ_TYPE_12(_typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8, f9, v9, f10, v10, f11, v11, f12, v12) const mp_obj_type_t _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1, .f2 = v2, .f3 = v3, .f4 = v4, .f5 = v5, .f6 = v6, .f7 = v7, .f8 = v8, .f9 = v9, .f10 = v10, .f11 = v11, .f12 = v12 }
607633

608-
#define MP_DEFINE_CONST_OBJ_TYPE_IMPL(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, N, ...) MP_DEFINE_CONST_OBJ_TYPE_##N
609-
#define MP_DEFINE_CONST_OBJ_TYPE(...) MP_DEFINE_CONST_OBJ_TYPE_IMPL(__VA_ARGS__, _INV, 12, _INV, 11, _INV, 10, _INV, 9, _INV, 8, _INV, 7, _INV, 6, _INV, 5, _INV, 4, _INV, 3, _INV, 2, _INV, 1, _INV, 0, _INV, _INV, _INV)(__VA_ARGS__)
610-
611634
// Always safe, checks if the type can and does have this slot.
612635
#define MP_OBJ_TYPE_HAS_SLOT(t, f) ((t)->f)
613636
// Requires you know that this type can have this slot.
614637
#define MP_OBJ_TYPE_GET_SLOT(t, f) ((t)->f)
615638
// Always safe, returns NULL if the type cannot have this slot.
616639
#define MP_OBJ_TYPE_GET_SLOT_OR_NULL(t, f) ((t)->f)
617-
#define MP_OBJ_TYPE_SET_SLOT(t, f, v) ((t)->f = v)
640+
#define MP_OBJ_TYPE_SET_SLOT(t, f, v, n) ((t)->f = v)
618641
#define MP_OBJ_TYPE_OFFSETOF_SLOT(t, f) (offsetof(mp_obj_type_t, f))
619642

643+
#elif MICROPY_OBJ_TYPE_REPR == MICROPY_OBJ_TYPE_REPR_SLOT_INDEX
644+
645+
#define MICROPY_OBJ_TYPE_EXTRA_ALLOC (12 * sizeof(void *))
646+
647+
#define MP_OBJ_TYPE_SLOT_TYPE_print (mp_print_fun_t)
648+
#define MP_OBJ_TYPE_SLOT_TYPE_call (mp_call_fun_t)
649+
#define MP_OBJ_TYPE_SLOT_TYPE_unary_op (mp_unary_op_fun_t)
650+
#define MP_OBJ_TYPE_SLOT_TYPE_binary_op (mp_binary_op_fun_t)
651+
#define MP_OBJ_TYPE_SLOT_TYPE_attr (mp_attr_fun_t)
652+
#define MP_OBJ_TYPE_SLOT_TYPE_subscr (mp_subscr_fun_t)
653+
#define MP_OBJ_TYPE_SLOT_TYPE_getiter (mp_getiter_fun_t)
654+
#define MP_OBJ_TYPE_SLOT_TYPE_iternext (mp_fun_1_t)
655+
#define MP_OBJ_TYPE_SLOT_TYPE_buffer (mp_buffer_fun_t)
656+
#define MP_OBJ_TYPE_SLOT_TYPE_protocol (const void *)
657+
#define MP_OBJ_TYPE_SLOT_TYPE_parent (const void *)
658+
#define MP_OBJ_TYPE_SLOT_TYPE_locals_dict (struct _mp_obj_dict_t *)
659+
660+
#define MP_DEFINE_CONST_OBJ_TYPE_0(_typename, _name, _flags, _make_new) const mp_obj_type_t _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .slots = {} }
661+
#define MP_DEFINE_CONST_OBJ_TYPE_1(_typename, _name, _flags, _make_new, f1, v1) const mp_obj_type_t _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .slot_index_##f1 = 1, .slots = {v1, } }
662+
#define MP_DEFINE_CONST_OBJ_TYPE_2(_typename, _name, _flags, _make_new, f1, v1, f2, v2) const mp_obj_type_t _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slots = {v1, v2, } }
663+
#define MP_DEFINE_CONST_OBJ_TYPE_3(_typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3) const mp_obj_type_t _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slots = {v1, v2, v3, } }
664+
#define MP_DEFINE_CONST_OBJ_TYPE_4(_typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4) const mp_obj_type_t _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slots = {v1, v2, v3, v4, } }
665+
#define MP_DEFINE_CONST_OBJ_TYPE_5(_typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5) const mp_obj_type_t _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slots = {v1, v2, v3, v4, v5, } }
666+
#define MP_DEFINE_CONST_OBJ_TYPE_6(_typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6) const mp_obj_type_t _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slots = {v1, v2, v3, v4, v5, v6, } }
667+
#define MP_DEFINE_CONST_OBJ_TYPE_7(_typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7) const mp_obj_type_t _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slot_index_##f7 = 7, .slots = {v1, v2, v3, v4, v5, v6, v7, } }
668+
#define MP_DEFINE_CONST_OBJ_TYPE_8(_typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8) const mp_obj_type_t _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slot_index_##f7 = 7, .slot_index_##f8 = 8, .slots = {v1, v2, v3, v4, v5, v6, v7, v8, } }
669+
#define MP_DEFINE_CONST_OBJ_TYPE_9(_typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8, f9, v9) const mp_obj_type_t _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slot_index_##f7 = 7, .slot_index_##f8 = 8, .slot_index_##f9 = 9, .slots = {v1, v2, v3, v4, v5, v6, v7, v8, v9, } }
670+
#define MP_DEFINE_CONST_OBJ_TYPE_10(_typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8, f9, v9, f10, v10) const mp_obj_type_t _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slot_index_##f7 = 7, .slot_index_##f8 = 8, .slot_index_##f9 = 9, .slot_index_##f10 = 10, .slots = {v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, } }
671+
#define MP_DEFINE_CONST_OBJ_TYPE_11(_typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8, f9, v9, f10, v10, f11, v11) const mp_obj_type_t _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slot_index_##f7 = 7, .slot_index_##f8 = 8, .slot_index_##f9 = 9, .slot_index_##f10 = 10, .slot_index_##f11 = 11, .slots = {v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, } }
672+
#define MP_DEFINE_CONST_OBJ_TYPE_12(_typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8, f9, v9, f10, v10, f11, v11, f12, v12) const mp_obj_type_t _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .slot_index_##f1 = 1, .slot_index_##f2 = 2, .slot_index_##f3 = 3, .slot_index_##f4 = 4, .slot_index_##f5 = 5, .slot_index_##f6 = 6, .slot_index_##f7 = 7, .slot_index_##f8 = 8, .slot_index_##f9 = 9, .slot_index_##f10 = 10, .slot_index_##f11 = 11, .slot_index_##f12 = 12, .slots = {v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, } }
673+
674+
#define MP_OBJ_TYPE_HAS_SLOT(t, f) ((t)->slot_index_##f)
675+
#define MP_OBJ_TYPE_GET_SLOT(t, f) (MP_OBJ_TYPE_SLOT_TYPE_##f(t)->slots[(t)->slot_index_##f - 1])
676+
#define MP_OBJ_TYPE_GET_SLOT_OR_NULL(t, f) (MP_OBJ_TYPE_SLOT_TYPE_##f(MP_OBJ_TYPE_HAS_SLOT(t, f) ? MP_OBJ_TYPE_GET_SLOT(t, f) : NULL))
677+
#define MP_OBJ_TYPE_SET_SLOT(t, f, v, n) ((t)->slot_index_##f = n, (t)->slots[(t)->slot_index_##f - 1] = (void *)v)
678+
#define MP_OBJ_TYPE_OFFSETOF_SLOT(t, f) (offsetof(mp_obj_type_t, slots) + (t)->slot_index_##f * sizeof(void *))
679+
680+
#endif
681+
682+
#define MP_DEFINE_CONST_OBJ_TYPE_IMPL(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21, _22, _23, _24, _25, _26, _27, _28, _29, N, ...) MP_DEFINE_CONST_OBJ_TYPE_##N
683+
#define MP_DEFINE_CONST_OBJ_TYPE(...) MP_DEFINE_CONST_OBJ_TYPE_IMPL(__VA_ARGS__, _INV, 12, _INV, 11, _INV, 10, _INV, 9, _INV, 8, _INV, 7, _INV, 6, _INV, 5, _INV, 4, _INV, 3, _INV, 2, _INV, 1, _INV, 0, _INV, _INV, _INV)(__VA_ARGS__)
684+
620685
// Constant types, globally accessible
621686
extern const mp_obj_type_t mp_type_type;
622687
extern const mp_obj_type_t mp_type_object;

py/objtype.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,9 +1138,9 @@ mp_obj_t mp_obj_new_type(qstr name, mp_obj_t bases_tuple, mp_obj_t locals_dict)
11381138
#endif
11391139
}
11401140

1141-
mp_obj_type_t *o = m_new0(mp_obj_type_t, 1);
1141+
mp_obj_type_t *o = (mp_obj_type_t *)m_malloc0(sizeof(mp_obj_type_t) + MICROPY_OBJ_TYPE_EXTRA_ALLOC);
11421142
o->base.type = &mp_type_type;
1143-
o->flags = base_flags;
1143+
o->flags = base_flags
11441144
o->name = name;
11451145
o->make_new = mp_obj_instance_make_new;
11461146
MP_OBJ_TYPE_SET_SLOT(o, print, instance_print, 1);

0 commit comments

Comments
 (0)
0