Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions .github/workflows/ports_m68kmac.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@ jobs:
build:
permissions: write-all
runs-on: ubuntu-latest
container: ghcr.io/autc04/retro68
container: ghcr.io/m68k-micropython/builder:latest
steps:
- uses: actions/checkout@v4
- name: Build
run: |
apt update
apt install -y python3-pip
pip install pyyaml click
git config --global --add safe.directory $(pwd)
make -C mpy-cross -j$(nproc)
make -C ports/m68kmac submodules
Expand Down
61 changes: 51 additions & 10 deletions extmod/moductypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,13 @@
#define IS_SCALAR_ARRAY_OF_BYTES(tuple_desc) (GET_TYPE(MP_OBJ_SMALL_INT_VALUE((tuple_desc)->items[1]), VAL_TYPE_BITS) == UINT8)

// "struct" in uctypes context means "structural", i.e. aggregate, type.
static const mp_obj_type_t uctypes_struct_type;
const mp_obj_type_t uctypes_struct_type;

// Get size of any type descriptor
static mp_uint_t uctypes_struct_size(mp_obj_t desc_in, int layout_type, mp_uint_t *max_field_size);

static mp_obj_t uctypes_struct_attr_op(mp_obj_t self_in, qstr attr, mp_obj_t set_val);

#define CTYPES_FLAGS_SIZE_BITS (2)
#define CTYPES_OFFSET_SIZE_BITS (8 * sizeof(uint32_t) - 2)

Expand Down Expand Up @@ -75,6 +77,11 @@ static bool is_struct_type(mp_obj_t obj_in) {
return make_new == uctypes_struct_type_make_new;
}

static bool is_struct_instance(mp_obj_t obj_in) {
const mp_obj_type_t *type = mp_obj_get_type(obj_in);
return MP_OBJ_TYPE_GET_SLOT_OR_NULL(type, subscr) == uctypes_struct_subscr;
}

mp_obj_t uctypes_struct_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 2, 3, false);
mp_obj_t desc = args[1];
Expand Down Expand Up @@ -149,6 +156,9 @@ mp_obj_t uctypes_struct_type_make_new(const mp_obj_type_t *type_in, size_t n_arg
} else if (agg_type != PTR) {
syntax_error();
}
if (n_kw) {
mp_raise_TypeError(MP_ERROR_TEXT("struct: no fields"));
}
} else {
mp_obj_dict_t *d = MP_OBJ_TO_PTR(desc);
// only for packed ROM tables..
Expand All @@ -161,10 +171,17 @@ mp_obj_t uctypes_struct_type_make_new(const mp_obj_type_t *type_in, size_t n_arg
for (size_t i = 0; i < n_args; i++) {
mp_store_attr(result, mp_obj_str_get_qstr(d->map.table[i].key), args[i]);
}
}
args += n_args;
for (size_t i = 0; i < 2 * n_kw; i += 2) {
mp_store_attr(result, mp_obj_str_get_qstr(args[i]), args[i + 1]);
args += n_args;
for (size_t i = 0; i < 2 * n_kw; i += 2) {
qstr q = mp_obj_str_get_qstr(args[i]);
for (size_t j = 0; j < n_args; j++) {
if (mp_obj_str_get_qstr(d->map.table[j].key) == q) {
mp_raise_msg_varg(&mp_type_TypeError,
MP_ERROR_TEXT("function got multiple values for argument '%q'"), q);
}
}
mp_store_attr(result, q, args[i + 1]);
}
}
return result;
}
Expand All @@ -173,8 +190,10 @@ mp_obj_t uctypes_struct_type_make_new(const mp_obj_type_t *type_in, size_t n_arg
void uctypes_struct_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
(void)kind;
mp_obj_uctypes_struct_t *self = MP_OBJ_TO_PTR(self_in);
mp_obj_dict_t *d = 0;
const char *typen = "unk";
if (mp_obj_is_dict_or_ordereddict(self->desc)) {
d = MP_OBJ_TO_PTR(self->desc);
typen = "STRUCT";
} else if (mp_obj_is_type(self->desc, &mp_type_tuple)) {
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(self->desc);
Expand All @@ -192,7 +211,17 @@ void uctypes_struct_print(const mp_print_t *print, mp_obj_t self_in, mp_print_ki
typen = "ERROR";
}

mp_printf(print, "<%q %s %p>", (qstr)mp_obj_get_type_qstr(self_in), typen, struct_addr(self));
mp_printf(print, "<%q %s %p", (qstr)mp_obj_get_type_qstr(self_in), typen, struct_addr(self));
if (kind == PRINT_REPR && d) {
for (mp_uint_t i = 0; i < d->map.alloc; i++) {
if (mp_map_slot_is_filled(&d->map, i)) {
qstr k = mp_obj_str_get_qstr(d->map.table[i].key);
mp_obj_t attr = uctypes_struct_attr_op(self_in, k, MP_OBJ_NULL);
mp_printf(print, "\n %q=%r", k, attr);
}
}
}
mp_printf(print, ">");
}

// Get size of scalar type descriptor
Expand Down Expand Up @@ -346,13 +375,25 @@ static mp_obj_t uctypes_struct_sizeof(size_t n_args, const mp_obj_t *args) {
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(uctypes_struct_sizeof_obj, 1, 2, uctypes_struct_sizeof);

mp_obj_t uctypes_get_struct_desc(mp_obj_t arg) {
if (is_struct_instance(arg)) {
mp_obj_uctypes_struct_t *struct_ = MP_OBJ_TO_PTR(arg);
return struct_->desc;
}
if (is_struct_type(arg)) {
mp_obj_ctypes_struct_type_t *struct_type = MP_OBJ_TO_PTR(arg);
return struct_type->desc;
}
return MP_OBJ_NULL;
}
static mp_obj_t uctypes_struct_desc(mp_obj_t arg) {
if (!is_struct_type(arg)) {
mp_obj_t result = uctypes_get_struct_desc(arg);
if (result == MP_OBJ_NULL) {
mp_raise_TypeError(NULL);
}
mp_obj_ctypes_struct_type_t *struct_type = MP_OBJ_TO_PTR(arg);
return struct_type->desc;
return result;
}

MP_DEFINE_CONST_FUN_OBJ_1(uctypes_struct_desc_obj, uctypes_struct_desc);

static const char type2char[16] = {
Expand Down Expand Up @@ -757,7 +798,7 @@ static mp_obj_t uctypes_struct_bytes_at(mp_obj_t ptr, mp_obj_t size) {
}
MP_DEFINE_CONST_FUN_OBJ_2(uctypes_struct_bytes_at_obj, uctypes_struct_bytes_at);

static MP_DEFINE_CONST_OBJ_TYPE(
MP_DEFINE_CONST_OBJ_TYPE(
uctypes_struct_type,
MP_QSTR_struct,
MP_TYPE_FLAG_NONE,
Expand Down
8 changes: 6 additions & 2 deletions extmod/moductypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#if MICROPY_PY_UCTYPES
#include "py/obj.h"

extern const mp_obj_type_t uctypes_struct_type;

typedef struct _mp_obj_ctypes_struct_type_t {
// This is a mp_obj_type_t with six slots.
mp_obj_empty_type_t base;
Expand All @@ -45,12 +47,14 @@ mp_obj_t uctypes_struct_unary_op(mp_unary_op_t op, mp_obj_t self_in);
mp_int_t uctypes_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags);
mp_obj_t uctypes_struct_type_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args);
mp_obj_t uctypes_struct_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args);
// may call on an instance or named struct type
mp_obj_t uctypes_get_struct_desc(const mp_obj_t obj);

#define MP_DECLARE_CTYPES_STRUCT(type_name) \
extern mp_obj_ctypes_struct_type_t type_name;
extern const mp_obj_ctypes_struct_type_t type_name;

#define MP_DEFINE_CTYPES_STRUCT(type_name, name_, desc_, flags_) \
mp_obj_ctypes_struct_type_t type_name = { \
const mp_obj_ctypes_struct_type_t type_name = { \
.base = { \
.base = { &mp_type_type }, \
.flags = MP_TYPE_FLAG_NONE, \
Expand Down
14 changes: 14 additions & 0 deletions extmod/multiversal.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
MKAPIFORMAT ?=
# $(eval $(call multiversal_module, module name, defs files))
define multiversal_module
$$(BUILD)/mod$(1).c: $(TOP)/tools/mkapi.py $(filter-out -t,$(2)) $$(DEFS)
$$(ECHO) "MKAPI $(1)"
$$(Q)$$(MKDIR) -p $$(BUILD)
$$(Q)$$(PYTHON) $(TOP)/tools/mkapi.py $(MKAPIFORMAT) -o $$@ -m $(1) $$(TDEFS) $(2)
SRC_C += $(BUILD)/mod$(1).c
SRC_QSTR += $(BUILD)/mod$(1).c
mkapi:: $$(BUILD)/mod$(1).c
endef

SRC_C += extmod/multiverse_support.c
SRC_QSTR += extmod/multiverse_support.c
Loading
Loading