8000 py/obj.h: Remove basic mp_obj_type_t representation. · micropython/micropython@2f82ed1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2f82ed1

Browse files
committed
py/obj.h: Remove basic mp_obj_type_t representation.
This makes the slots-based representation the only option. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
1 parent 5c8fa23 commit 2f82ed1

File tree

2 files changed

+17
-74
lines changed

2 files changed

+17
-74
lines changed

py/mpconfig.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,6 @@
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-
155148
/*****************************************************************************/
156149
/* Memory allocation policy */
157150

py/obj.h

Lines changed: 17 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -578,17 +578,22 @@ struct _mp_obj_type_t {
578578
// Corresponds to __new__ and __init__ special methods, to make an instance of the type.
579579
mp_make_new_fun_t make_new;
580580

581-
#if MICROPY_OBJ_TYPE_REPR == MICROPY_OBJ_TYPE_REPR_FULL
581+
// Slots: For the rest of the fields, the slot index points to the
582+
// relevant function in the variable-length "slots" field. Ideally these
583+
// would be only 4 bits, but the extra overhead of accessing them adds
584+
// more code, and we also need to be able to take the address of them for
585+
// mp_obj_class_lookup.
586+
582587
// Corresponds to __repr__ and __str__ special methods.
583-
mp_print_fun_t print;
588+
uint8_t slot_index_print;
584589

585590
// Corresponds to __call__ special method, ie T(...).
586-
mp_call_fun_t call;
591+
uint8_t slot_index_call;
587592

588593
// Implements unary and binary operations.
589594
// Can return MP_OBJ_NULL if the operation is not supported.
590-
mp_unary_op_fun_t unary_op;
591-
mp_binary_op_fun_t binary_op;
595+
uint8_t slot_index_unary_op;
596+
uint8_t slot_index_binary_op;
592597

593598
// Implements load, store and delete attribute.
594599
//
@@ -602,70 +607,46 @@ struct _mp_obj_type_t {
602607
// dest[0,1] = {MP_OBJ_SENTINEL, object} means store
603608
// return: for fail, do nothing
604609
// for success set dest[0] = MP_OBJ_NULL
605-
mp_attr_fun_t attr;
610+
uint8_t slot_index_attr;
606611

607612
// Implements load, store and delete subscripting:
608613
// - value = MP_OBJ_SENTINEL means load
609614
// - value = MP_OBJ_NULL means delete
610615
// - all other values mean store the value
611616
// Can return MP_OBJ_NULL if operation not supported.
612-
mp_subscr_fun_t subscr;
617+
uint8_t slot_index_subscr;
613618

614619
// Corresponds to __iter__ special method.
615620
// Can use the given mp_obj_iter_buf_t to store iterator object,
616621
// otherwise can return a pointer to an object on the heap.
617-
mp_getiter_fun_t getiter;
622+
uint8_t slot_index_getiter;
618623

619624
// Corresponds to __next__ special method. May return MP_OBJ_STOP_ITERATION
620625
// as an optimisation instead of raising StopIteration() with no args.
621-
mp_fun_1_t iternext;
626+
uint8_t slot_index_iternext;
622627

623628
// Implements the buffer protocol if supported by this type.
624-
mp_buffer_fun_t buffer;
629+
uint8_t slot_index_buffer;
625630

626631
// One of disjoint protocols (interfaces), like mp_stream_p_t, etc.
627-
const void *protocol;
632+
uint8_t slot_index_protocol;
628633

629634
// A pointer to the parents of this type:
630635
// - 0 parents: pointer is NULL (object is implicitly the single parent)
631636
// - 1 parent: a pointer to the type of that parent
632637
// - 2 or more parents: pointer to a tuple object containing the parent types
633-
const void *parent;
638+
uint8_t slot_index_parent;
634639

635640
// A dict mapping qstrs to objects local methods/constants/etc.
636-
struct _mp_obj_dict_t *locals_dict;
637-
638-
#elif MICROPY_OBJ_TYPE_REPR == MICROPY_OBJ_TYPE_REPR_SLOT_INDEX
639-
640-
// Ideally these would be only 4 bits, but the extra overhead of
641-
// accessing them adds more code, and we also need to be able to
642-
// take the address of them for mp_obj_class_lookup.
643-
uint8_t slot_index_print;
644-
uint8_t slot_index_call;
645-
uint8_t slot_index_unary_op;
646-
uint8_t slot_index_binary_op;
647-
uint8_t slot_index_attr;
648-
uint8_t slot_index_subscr;
649-
uint8_t slot_index_getiter;
650-
uint8_t slot_index_iternext;
651-
uint8_t slot_index_buffer;
652-
uint8_t slot_index_protocol;
653-
uint8_t slot_index_parent;
654641
uint8_t slot_index_locals_dict;
655642

656643
const void *slots[];
657-
658-
#endif
659644
};
660645

661646
// Non-variable sized versions of mp_obj_type_t to be used as a member
662647
// in other structs or for dynamic allocation. The fields are exactly
663648
// as in mp_obj_type_t, but with a fixed size for the flexible array
664649
// members.
665-
#if MICROPY_OBJ_TYPE_REPR == MICROPY_OBJ_TYPE_REPR_FULL
666-
typedef mp_obj_type_t mp_obj_empty_type_t;
667-
typedef mp_obj_type_t mp_obj_full_type_t;
668-
#else
669650
typedef struct _mp_obj_empty_type_t {
670651
mp_obj_base_t base;
671652
uint16_t flags;
@@ -710,38 +691,9 @@ typedef struct _mp_obj_full_type_t {
710691
// Explicitly add 12 slots.
711692
const void *slots[12];
712693
} mp_obj_full_type_t;
713-
#endif
714694

715695
#define MP_TYPE_NULL_MAKE_NEW (NULL)
716696

717-
#if MICROPY_OBJ_TYPE_REPR == MICROPY_OBJ_TYPE_REPR_FULL
718-
719-
#define _MP_DEFINE_CONST_OBJ_TYPE_0(_struct_type, _typename, _name, _flags, _make_new) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new }
720-
#define _MP_DEFINE_CONST_OBJ_TYPE_1(_struct_type, _typename, _name, _flags, _make_new, f1, v1) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1 }
721-
#define _MP_DEFINE_CONST_OBJ_TYPE_2(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1, .f2 = v2 }
722-
#define _MP_DEFINE_CONST_OBJ_TYPE_3(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1, .f2 = v2, .f3 = v3 }
723-
#define _MP_DEFINE_CONST_OBJ_TYPE_4(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1, .f2 = v2, .f3 = v3, .f4 = v4 }
724-
#define _MP_DEFINE_CONST_OBJ_TYPE_5(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5) const _struct_type _typename = { .base = { &mp_type_type }, .name = _name, .flags = _flags, .make_new = _make_new, .f1 = v1, .f2 = v2, .f3 = v3, .f4 = v4, .f5 = v5 }
725-
#define _MP_DEFINE_CONST_OBJ_TYPE_6(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6) const _struct_type _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 }
726-
#define _MP_DEFINE_CONST_OBJ_TYPE_7(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7) const _struct_type _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 }
727-
#define _MP_DEFINE_CONST_OBJ_TYPE_8(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8) const _struct_type _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 }
728-
#define _MP_DEFINE_CONST_OBJ_TYPE_9(_struct_type, _typename, _name, _flags, _make_new, f1, v1, f2, v2, f3, v3, f4, v4, f5, v5, f6, v6, f7, v7, f8, v8, f9, v9) const _struct_type _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 }
729-
#define _MP_DEFINE_CONST_OBJ_TYPE_10(_struct_type, _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 _struct_type _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 }
730-
#define _MP_DEFINE_CONST_OBJ_TYPE_11(_struct_type, _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 _struct_type _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 }
731-
#define _MP_DEFINE_CONST_OBJ_TYPE_12(_struct_type, _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 _struct_type _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 }
732-
733-
// Always safe, checks if the type can and does have this slot.
734-
#define MP_OBJ_TYPE_HAS_SLOT(t, f) ((t)->f)
735-
// Requires you know that this type can have this slot.
736-
#define MP_OBJ_TYPE_GET_SLOT(t, f) ((t)->f)
737-
// Always safe, returns NULL if the type cannot have this slot.
738-
#define MP_OBJ_TYPE_GET_SLOT_OR_NULL(t, f) ((t)->f)
739-
#define MP_OBJ_TYPE_SET_SLOT(t, f, v, n) ((t)->f = v)
740-
#define MP_OBJ_TYPE_OFFSETOF_SLOT(f) (offsetof(mp_obj_type_t, f))
741-
#define MP_OBJ_TYPE_HAS_SLOT_BY_OFFSET(t, offset) (*(void **)((char *)(t) + (offset)) != NULL)
742-
743-
#elif MICROPY_OBJ_TYPE_REPR == MICROPY_OBJ_TYPE_REPR_SLOT_INDEX
744-
745697
#define _MP_OBJ_TYPE_SLOT_TYPE_print (mp_print_fun_t)
746698
#define _MP_OBJ_TYPE_SLOT_TYPE_call (mp_call_fun_t)
747699
#define _MP_OBJ_TYPE_SLOT_TYPE_unary_op (mp_unary_op_fun_t)
@@ -779,8 +731,6 @@ typedef struct _mp_obj_full_type_t {
779731
// For everything except make_new, the offset is to the uint8_t index. For make_new, we need to check the pointer.
780732
#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))
781733

782-
#endif
783-
784734
// Workaround for https://docs.microsoft.com/en-us/cpp/preprocessor/preprocessor-experimental-overview?view=msvc-160#macro-arguments-are-unpacked
785735
#define _MP_DEFINE_CONST_OBJ_TYPE_EXPAND(x) x
786736

0 commit comments

Comments
 (0)
0