@@ -569,17 +569,22 @@ struct _mp_obj_type_t {
569569 // Corresponds to __new__ and __init__ special methods, to make an instance of the type.
570570 mp_make_new_fun_t make_new ;
571571
572- #if MICROPY_OBJ_TYPE_REPR == MICROPY_OBJ_TYPE_REPR_FULL
572+ // Slots: For the rest of the fields, the slot index points to the
573+ // relevant function in the variable-length "slots" field. Ideally these
574+ // would be only 4 bits, but the extra overhead of accessing them adds
575+ // more code, and we also need to be able to take the address of them for
576+ // mp_obj_class_lookup.
577+
573578 // Corresponds to __repr__ and __str__ special methods.
574- mp_print_fun_t print ;
579+ uint8_t slot_index_print ;
575580
576581 // Corresponds to __call__ special method, ie T(...).
577- mp_call_fun_t call ;
582+ uint8_t slot_index_call ;
578583
579584 // Implements unary and binary operations.
580585 // Can return MP_OBJ_NULL if the operation is not supported.
581- mp_unary_op_fun_t unary_op ;
582- mp_binary_op_fun_t binary_op ;
586+ uint8_t slot_index_unary_op ;
587+ uint8_t slot_index_binary_op ;
583588
584589 // Implements load, store and delete attribute.
585590 //
@@ -593,60 +598,40 @@ struct _mp_obj_type_t {
593598 // dest[0,1] = {MP_OBJ_SENTINEL, object} means store
594599 // return: for fail, do nothing
595600 // for success set dest[0] = MP_OBJ_NULL
596- mp_attr_fun_t attr ;
601+ uint8_t slot_index_attr ;
597602
598603 // Implements load, store and delete subscripting:
599604 // - value = MP_OBJ_SENTINEL means load
600605 // - value = MP_OBJ_NULL means delete
601606 // - all other values mean store the value
602607 // Can return MP_OBJ_NULL if operation not supported.
603- mp_subscr_fun_t subscr ;
608+ uint8_t slot_index_subscr ;
604609
605610 // Corresponds to __iter__ special method.
606611 // Can use the given mp_obj_iter_buf_t to store iterator object,
607612 // otherwise can return a pointer to an object on the heap.
608- mp_getiter_fun_t getiter ;
613+ uint8_t slot_index_getiter ;
609614
610615 // Corresponds to __next__ special method. May return MP_OBJ_STOP_ITERATION
611616 // as an optimisation instead of raising StopIteration() with no args.
612- mp_fun_1_t iternext ;
617+ uint8_t slot_index_iternext ;
613618
614619 // Implements the buffer protocol if supported by this type.
615- mp_buffer_fun_t buffer ;
620+ uint8_t slot_index_buffer ;
616621
617622 // One of disjoint protocols (interfaces), like mp_stream_p_t, etc.
618- const void * protocol ;
623+ uint8_t slot_index_protocol ;
619624
620625 // A pointer to the parents of this type:
621626 // - 0 parents: pointer is NULL (object is implicitly the single parent)
622627 // - 1 parent: a pointer to the type of that parent
623628 // - 2 or more parents: pointer to a tuple object containing the parent types
624- const void * parent ;
629+ uint8_t slot_index_parent ;
625630
626631 // A dict mapping qstrs to objects local methods/constants/etc.
627- struct _mp_obj_dict_t * locals_dict ;
628-
629- #elif MICROPY_OBJ_TYPE_REPR == MICROPY_OBJ_TYPE_REPR_SLOT_INDEX
630-
631- // Ideally these would be only 4 bits, but the extra overhead of
632- // accessing them adds more code, and we also need to be able to
633- // take the address of them for mp_obj_class_lookup.
634- uint8_t slot_index_print ;
635- uint8_t slot_index_call ;
636- uint8_t slot_index_unary_op ;
637- uint8_t slot_index_binary_op ;
638- uint8_t slot_index_attr ;
639- uint8_t slot_index_subscr ;
640- uint8_t slot_index_getiter ;
641- uint8_t slot_index_iternext ;
642- uint8_t slot_index_buffer ;
643- uint8_t slot_index_protocol ;
644- uint8_t slot_index_parent ;
645632 uint8_t slot_index_locals_dict ;
646633
647634 const void * slots [];
648-
649- #endif
650635};
651636
652637// Non-variable sized version of mp_obj_type_t to be used as a member
@@ -659,23 +644,6 @@ typedef struct _mp_obj_full_type_t {
659644 uint16_t name ;
660645 mp_make_new_fun_t make_new ;
661646
662- #if MICROPY_OBJ_TYPE_REPR == MICROPY_OBJ_TYPE_REPR_FULL
663-
664- mp_print_fun_t print ;
665- mp_call_fun_t call ;
666- mp_unary_op_fun_t unary_op ;
667- mp_binary_op_fun_t binary_op ;
668- mp_attr_fun_t attr ;
669- mp_subscr_fun_t subscr ;
670- mp_getiter_fun_t getiter ;
671- mp_fun_1_t iternext ;
672- mp_buffer_fun_t buffer ;
673- const void * protocol ;
674- const void * parent ;
675- struct _mp_obj_dict_t * locals_dict ;
676-
677- #elif MICROPY_OBJ_TYPE_REPR == MICROPY_OBJ_TYPE_REPR_SLOT_INDEX
678-
679647 uint8_t slot_index_print ;
680648 uint8_t slot_index_call ;
681649 uint8_t slot_index_unary_op ;
@@ -688,41 +656,13 @@ typedef struct _mp_obj_full_type_t {
688656 uint8_t slot_index_protocol ;
689657 uint8_t slot_index_parent ;
690658 uint8_t slot_index_locals_dict ;
691- const void * slots [12 ];
692659
693- #endif
660+ // This is the only change from _mp_obj_type_t.
661+ const void * slots [12 ];
694662} mp_obj_full_type_t ;
695663
696664#define MP_TYPE_NULL_MAKE_NEW (NULL)
697665
698- #if MICROPY_OBJ_TYPE_REPR == MICROPY_OBJ_TYPE_REPR_FULL
699-
700- #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 }
1E02
tr>701- #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 }
702- #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 }
703- #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, .f1 = v1, .f2 = v2, .f3 = v3 }
704- #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, .f1 = v1, .f2 = v2, .f3 = v3, .f4 = v4 }
705- #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, .f1 = v1, .f2 = v2, .f3 = v3, .f4 = v4, .f5 = v5 }
706- #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, .f1 = v1, .f2 = v2, .f3 = v3, .f4 = v4, .f5 = v5, .f6 = v6 }
707- #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, .f1 = v1, .f2 = v2, .f3 = v3, .f4 = v4, .f5 = v5, .f6 = v6, .f7 = v7 }
708- #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, .f1 = v1, .f2 = v2, .f3 = v3, .f4 = v4, .f5 = v5, .f6 = v6, .f7 = v7, .f8 = v8 }
709- #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, .f1 = v1, .f2 = v2, .f3 = v3, .f4 = v4, .f5 = v5, .f6 = v6, .f7 = v7, .f8 = v8, .f9 = v9 }
710- #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, .f1 = v1, .f2 = v2, .f3 = v3, .f4 = v4, .f5 = v5, .f6 = v6, .f7 = v7, .f8 = v8, .f9 = v9, .f10 = v10 }
711- #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 }
712- #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 }
713-
714- // Always safe, checks if the type can and does have this slot.
715- #define MP_OBJ_TYPE_HAS_SLOT (t , f ) ((t)->f)
716- // Requires you know that this type can have this slot.
717- #define MP_OBJ_TYPE_GET_SLOT (t , f ) ((t)->f)
718- // Always safe, returns NULL if the type cannot have this slot.
719- #define MP_OBJ_TYPE_GET_SLOT_OR_NULL (t , f ) ((t)->f)
720- #define MP_OBJ_TYPE_SET_SLOT (t , f , v , n ) ((t)->f = v)
721- #define MP_OBJ_TYPE_OFFSETOF_SLOT (f ) (offsetof(mp_obj_type_t, f))
722- #define MP_OBJ_TYPE_HAS_SLOT_BY_OFFSET (t , offset ) (*(void **)((char *)(t) + (offset)) != NULL)
723-
724- #elif MICROPY_OBJ_TYPE_REPR == MICROPY_OBJ_TYPE_REPR_SLOT_INDEX
725-
726666#define _MP_OBJ_TYPE_SLOT_TYPE_print (mp_print_fun_t)
727667#define _MP_OBJ_TYPE_SLOT_TYPE_call (mp_call_fun_t)
728668#define _MP_OBJ_TYPE_SLOT_TYPE_unary_op (mp_unary_op_fun_t)
@@ -758,8 +698,6 @@ typedef struct _mp_obj_full_type_t {
758698// For everything except make_new, the offset is to the uint8_t index. For make_new, we need to check the pointer.
759699#define MP_OBJ_TYPE_HAS_SLOT_BY_OFFSET (t , offset ) (*(uint8_t *)((char *)(t) + (offset)) != 0 || (offset == offsetof(mp_obj_type_t, make_new) && t->make_new))
760700
761- #endif
762-
763701// Workaround for https://docs.microsoft.com/en-us/cpp/preprocessor/preprocessor-experimental-overview?view=msvc-160#macro-arguments-are-unpacked
764702#define _MP_DEFINE_CONST_OBJ_TYPE_EXPAND (x ) x
765703
0 commit comments