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

Skip to content

Commit 1fe8dbd

Browse files
committed
py/obj.h: Add slot-index mp_obj_type_t representation.
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
1 parent 5df326c commit 1fe8dbd<
10000
div>

File tree

2 files changed

+94
-3
lines changed

2 files changed

+94
-3
lines changed

py/mpconfig.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,13 @@
145145
#define MICROPY_OBJ_IMMEDIATE_OBJS (MICROPY_OBJ_REPR != MICROPY_OBJ_REPR_D)
146146
#endif
147147

148+
#define MICROPY_OBJ_TYPE_REPR_FULL (0)
149+
#define MICROPY_OBJ_TYPE_REPR_SLOT_INDEX (1)
150+
151+
#ifndef MICROPY_OBJ_TYPE_REPR
152+
#define MICROPY_OBJ_TYPE_REPR (MICROPY_OBJ_TYPE_REPR_FULL)
153+
#endif
154+
148155
/*****************************************************************************/
149156
/* Memory allocation policy */
150157

py/obj.h

Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -566,12 +566,13 @@ struct _mp_obj_type_t {
566566
// The name of this type, a qstr.
567567
uint16_t name;
568568

569-
// Corresponds to __repr__ and __str__ special methods.
570-
mp_print_fun_t print;
571-
572569
// Corresponds to __new__ and __init__ special methods, to make an instance of the type.
573570
mp_make_new_fun_t make_new;
574571

572+
#if MICROPY_OBJ_TYPE_REPR == MICROPY_OBJ_TYPE_REPR_FULL
573+
// Corresponds to __repr__ and __str__ special methods.
574+
mp_print_fun_t print;
575+
575576
// Corresponds to __call__ special method, ie T(...).
576577
mp_call_fun_t call;
577578

@@ -624,6 +625,28 @@ struct _mp_obj_type_t {
624625

625626
// A dict mapping qstrs to objects local methods/constants/etc.
626627
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;
645+
uint8_t slot_index_locals_dict;
646+
647+
const void *slots[];
648+
649+
#endif
627650
};
628651

629652
// Non-variable sized version of mp_obj_type_t to be used as a member
@@ -636,6 +659,8 @@ typedef struct _mp_obj_full_type_t {
636659
uint16_t name;
637660
mp_make_new_fun_t make_new;
638661

662+
#if MICROPY_OBJ_TYPE_REPR == MICROPY_OBJ_TYPE_REPR_FULL
663+
639664
mp_print_fun_t print;
640665
mp_call_fun_t call;
641666
mp_unary_op_fun_t unary_op;
@@ -648,10 +673,30 @@ typedef struct _mp_obj_full_type_t {
648673
const void *protocol;
649674
const void *parent;
650675
struct _mp_obj_dict_t *locals_dict;
676+
677+
#elif MICROPY_OBJ_TYPE_REPR == MICROPY_OBJ_TYPE_REPR_SLOT_INDEX
678+
679+
uint8_t slot_index_print;
680+
uint8_t slot_index_call;
681+
uint8_t slot_index_unary_op;
682+
uint8_t slot_index_binary_op;
683+
uint8_t slot_index_attr;
684+
uint8_t slot_index_subscr;
685+
uint8_t slot_index_getiter;
686+
uint8_t slot_index_iternext;
687+
uint8_t slot_index_buffer;
688+
uint8_t slot_index_protocol;
689+
uint8_t slot_index_parent;
690+
uint8_t slot_index_locals_dict;
691+
const void *slots[12];
692+
693+
#endif
651694
} mp_obj_full_type_t;
652695

653696
#define MP_TYPE_NULL_MAKE_NEW (NULL)
654697

698+
#if MICROPY_OBJ_TYPE_REPR == MICROPY_OBJ_TYPE_REPR_FULL
699+
655700
#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 }
656701
#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 }
657702
#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 }
@@ -676,6 +721,45 @@ typedef struct _mp_obj_full_type_t {
676721
#define MP_OBJ_TYPE_OFFSETOF_SLOT(f) (offsetof(mp_obj_type_t, f))
677722
#define MP_OBJ_TYPE_HAS_SLOT_BY_OFFSET(t, offset) (*(void **)((char *)(t) + (offset)) != NULL)
678723

724+
#elif MICROPY_OBJ_TYPE_REPR == MICROPY_OBJ_TYPE_REPR_SLOT_INDEX
725+
726+
#define _MP_OBJ_TYPE_SLOT_TYPE_print (mp_print_fun_t)
727+
#define _MP_OBJ_TYPE_SLOT_TYPE_call (mp_call_fun_t)
728+
#define _MP_OBJ_TYPE_SLOT_TYPE_unary_op (mp_unary_op_fun_t)
729+
#define _MP_OBJ_TYPE_SLOT_TYPE_binary_op (mp_binary_op_fun_t)
730+
#define _MP_OBJ_TYPE_SLOT_TYPE_attr (mp_attr_fun_t)
731+
#define _MP_OBJ_TYPE_SLOT_TYPE_subscr (mp_subscr_fun_t)
732+
#define _MP_OBJ_TYPE_SLOT_TYPE_getiter (mp_getiter_fun_t)
733+
#define _MP_OBJ_TYPE_SLOT_TYPE_iternext (mp_fun_1_t)
734+
#define _MP_OBJ_TYPE_SLOT_TYPE_buffer (mp_buffer_fun_t)
735+
#define _MP_OBJ_TYPE_SLOT_TYPE_protocol (const void *)
736+
#define _MP_OBJ_TYPE_SLOT_TYPE_parent (const void *)
737+
#define _MP_OBJ_TYPE_SLOT_TYPE_locals_dict (struct _mp_obj_dict_t *)
738+
739+
#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 = {} }
740+
#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, } }
741+
#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, } }
742+
#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, } }
743+
#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, } }
744+
#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, } }
745+
#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, } }
746+
#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, } }
747+
#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, } }
748+
#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, } }
749+
#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, } }
750+
#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, } }
751+
#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, } }
752+
753+
#define MP_OBJ_TYPE_HAS_SLOT(t, f) ((t)->slot_index_##f)
754+
#define MP_OBJ_TYPE_GET_SLOT(t, f) (_MP_OBJ_TYPE_SLOT_TYPE_##f(t)->slots[(t)->slot_index_##f - 1])
755+
#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))
756+
#define MP_OBJ_TYPE_SET_SLOT(t, f, v, n) ((t)->slot_index_##f = n, (t)->slots[(t)->slot_index_##f - 1] = (void *)v)
757+
#define MP_OBJ_TYPE_OFFSETOF_SLOT(f) (offsetof(mp_obj_type_t, slot_index_##f))
758+
// For everything except make_new, the offset is to the uint8_t index. For make_new, we need to check the pointer.
759+
#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))
760+
761+
#endif
762+
679763
// Workaround for https://docs.microsoft.com/en-us/cpp/preprocessor/preprocessor-experimental-overview?view=msvc-160#macro-arguments-are-unpacked
680764
#define _MP_DEFINE_CONST_OBJ_TYPE_EXPAND(x) x
681765

0 commit comments

Comments
 (0)
0