2014-06-27 20:03:47 -04:00
|
|
|
/*
|
2017-06-30 03:22:17 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2014-06-27 20:03:47 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2018-08-25 15:54:27 -04:00
|
|
|
* Copyright (c) 2014-2018 Paul Sokolovsky
|
2014-06-27 20:03:47 -04:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdint.h>
|
2015-01-01 15:35:21 -05:00
|
|
|
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/objtuple.h"
|
|
|
|
#include "py/binary.h"
|
2014-06-27 20:03:47 -04:00
|
|
|
|
|
|
|
#if MICROPY_PY_UCTYPES
|
|
|
|
|
2021-05-05 22:11:51 -04:00
|
|
|
// The uctypes module allows defining the layout of a raw data structure (using
|
|
|
|
// terms of the C language), and then access memory buffers using this definition.
|
|
|
|
// The module also provides convenience functions to access memory buffers
|
|
|
|
// contained in Python objects or wrap memory buffers in Python objects.
|
2014-06-27 20:03:47 -04:00
|
|
|
|
|
|
|
#define LAYOUT_LITTLE_ENDIAN (0)
|
|
|
|
#define LAYOUT_BIG_ENDIAN (1)
|
|
|
|
#define LAYOUT_NATIVE (2)
|
|
|
|
|
|
|
|
#define VAL_TYPE_BITS 4
|
|
|
|
#define BITF_LEN_BITS 5
|
|
|
|
#define BITF_OFF_BITS 5
|
|
|
|
#define OFFSET_BITS 17
|
2021-05-05 22:40:53 -04:00
|
|
|
#define LEN_BITS (OFFSET_BITS + BITF_OFF_BITS)
|
2014-06-27 20:03:47 -04:00
|
|
|
#if VAL_TYPE_BITS + BITF_LEN_BITS + BITF_OFF_BITS + OFFSET_BITS != 31
|
|
|
|
#error Invalid encoding field length
|
|
|
|
#endif
|
|
|
|
|
|
|
|
enum {
|
|
|
|
UINT8, INT8, UINT16, INT16,
|
|
|
|
UINT32, INT32, UINT64, INT64,
|
|
|
|
|
|
|
|
BFUINT8, BFINT8, BFUINT16, BFINT16,
|
|
|
|
BFUINT32, BFINT32,
|
|
|
|
|
|
|
|
FLOAT32, FLOAT64,
|
|
|
|
};
|
|
|
|
|
|
|
|
#define AGG_TYPE_BITS 2
|
|
|
|
|
|
|
|
enum {
|
2018-09-29 11:01:58 -04:00
|
|
|
STRUCT, PTR, ARRAY,
|
2014-06-27 20:03:47 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Here we need to set sign bit right
|
|
|
|
#define TYPE2SMALLINT(x, nbits) ((((int)x) << (32 - nbits)) >> 1)
|
2014-10-29 21:32:54 -04:00
|
|
|
#define GET_TYPE(x, nbits) (((x) >> (31 - nbits)) & ((1 << nbits) - 1))
|
2014-06-27 20:03:47 -04:00
|
|
|
// Bit 0 is "is_signed"
|
|
|
|
#define GET_SCALAR_SIZE(val_type) (1 << ((val_type) >> 1))
|
|
|
|
#define VALUE_MASK(type_nbits) ~((int)0x80000000 >> type_nbits)
|
|
|
|
|
2014-10-29 21:32:54 -04:00
|
|
|
#define IS_SCALAR_ARRAY(tuple_desc) ((tuple_desc)->len == 2)
|
|
|
|
// We cannot apply the below to INT8, as their range [-128, 127]
|
|
|
|
#define IS_SCALAR_ARRAY_OF_BYTES(tuple_desc) (GET_TYPE(MP_OBJ_SMALL_INT_VALUE((tuple_desc)->items[1]), VAL_TYPE_BITS) == UINT8)
|
|
|
|
|
2014-10-29 20:43:53 -04:00
|
|
|
// "struct" in uctypes context means "structural", i.e. aggregate, type.
|
2014-06-27 20:03:47 -04:00
|
|
|
STATIC const mp_obj_type_t uctypes_struct_type;
|
|
|
|
|
|
|
|
typedef struct _mp_obj_uctypes_struct_t {
|
|
|
|
mp_obj_base_t base;
|
|
|
|
mp_obj_t desc;
|
|
|
|
byte *addr;
|
|
|
|
uint32_t flags;
|
|
|
|
} mp_obj_uctypes_struct_t;
|
|
|
|
|
2015-01-12 17:34:38 -05:00
|
|
|
STATIC NORETURN void syntax_error(void) {
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_TypeError(MP_ERROR_TEXT("syntax error in uctypes descriptor"));
|
2014-06-27 20:03:47 -04:00
|
|
|
}
|
|
|
|
|
2016-01-03 10:55:55 -05:00
|
|
|
STATIC 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) {
|
2015-06-06 15:57:31 -04:00
|
|
|
mp_arg_check_num(n_args, n_kw, 2, 3, false);
|
2022-04-22 03:09:15 -04:00
|
|
|
mp_obj_uctypes_struct_t *o = mp_obj_malloc(mp_obj_uctypes_struct_t, type);
|
2016-09-21 02:50:06 -04:00
|
|
|
o->addr = (void *)(uintptr_t)mp_obj_int_get_truncated(args[0]);
|
2015-06-06 15:57:31 -04:00
|
|
|
o->desc = args[1];
|
2014-06-27 20:03:47 -04:00
|
|
|
o->flags = LAYOUT_NATIVE;
|
|
|
|
if (n_args == 3) {
|
|
|
|
o->flags = mp_obj_get_int(args[2]);
|
|
|
|
}
|
2015-11-27 12:01:44 -05:00
|
|
|
return MP_OBJ_FROM_PTR(o);
|
2014-06-27 20:03:47 -04:00
|
|
|
}
|
|
|
|
|
2015-04-09 18:56:15 -04:00
|
|
|
STATIC void uctypes_struct_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
|
2015-01-20 07:47:20 -05:00
|
|
|
(void)kind;
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_uctypes_struct_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-06-27 20:03:47 -04:00
|
|
|
const char *typen = "unk";
|
2020-06-21 03:14:18 -04:00
|
|
|
if (mp_obj_is_dict_or_ordereddict(self->desc)) {
|
2014-06-27 20:03:47 -04:00
|
|
|
typen = "STRUCT";
|
2019-01-30 06:04:54 -05:00
|
|
|
} else if (mp_obj_is_type(self->desc, &mp_type_tuple)) {
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(self->desc);
|
2014-06-27 20:03:47 -04:00
|
|
|
mp_int_t offset = MP_OBJ_SMALL_INT_VALUE(t->items[0]);
|
|
|
|
uint agg_type = GET_TYPE(offset, AGG_TYPE_BITS);
|
|
|
|
switch (agg_type) {
|
|
|
|
case PTR:
|
|
|
|
typen = "PTR";
|
|
|
|
break;
|
|
|
|
case ARRAY:
|
|
|
|
typen = "ARRAY";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
typen = "ERROR";
|
|
|
|
}
|
2015-04-09 18:56:15 -04:00
|
|
|
mp_printf(print, "<struct %s %p>", typen, self->addr);
|
2014-06-27 20:03:47 -04:00
|
|
|
}
|
|
|
|
|
2014-10-29 20:43:53 -04:00
|
|
|
// Get size of any type descriptor
|
2015-12-09 14:39:36 -05:00
|
|
|
STATIC mp_uint_t uctypes_struct_size(mp_obj_t desc_in, int layout_type, mp_uint_t *max_field_size);
|
2014-10-29 20:43:53 -04:00
|
|
|
|
|
|
|
// Get size of scalar type descriptor
|
2014-06-27 20:03:47 -04:00
|
|
|
static inline mp_uint_t uctypes_struct_scalar_size(int val_type) {
|
|
|
|
if (val_type == FLOAT32) {
|
|
|
|
return 4;
|
|
|
|
} else {
|
|
|
|
return GET_SCALAR_SIZE(val_type & 7);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-29 20:43:53 -04:00
|
|
|
// Get size of aggregate type descriptor
|
2015-12-09 14:39:36 -05:00
|
|
|
STATIC mp_uint_t uctypes_struct_agg_size(mp_obj_tuple_t *t, int layout_type, mp_uint_t *max_field_size) {
|
2014-10-29 20:43:53 -04:00
|
|
|
mp_uint_t total_size = 0;
|
|
|
|
|
|
|
|
mp_int_t offset_ = MP_OBJ_SMALL_INT_VALUE(t->items[0]);
|
|
|
|
mp_uint_t agg_type = GET_TYPE(offset_, AGG_TYPE_BITS);
|
|
|
|
|
|
|
|
switch (agg_type) {
|
|
|
|
case STRUCT:
|
2015-12-09 14:39:36 -05:00
|
|
|
return uctypes_struct_size(t->items[1], layout_type, max_field_size);
|
2014-10-29 20:43:53 -04:00
|
|
|
case PTR:
|
|
|
|
if (sizeof(void *) > *max_field_size) {
|
|
|
|
*max_field_size = sizeof(void *);
|
|
|
|
}
|
|
|
|
return sizeof(void *);
|
|
|
|
case ARRAY: {
|
|
|
|
mp_int_t arr_sz = MP_OBJ_SMALL_INT_VALUE(t->items[1]);
|
|
|
|
uint val_type = GET_TYPE(arr_sz, VAL_TYPE_BITS);
|
|
|
|
arr_sz &= VALUE_MASK(VAL_TYPE_BITS);
|
|
|
|
mp_uint_t item_s;
|
|
|
|
if (t->len == 2) {
|
|
|
|
// Elements of array are scalar
|
2021-05-05 23:11:33 -04:00
|
|
|
item_s = uctypes_struct_scalar_size(val_type);
|
2014-10-29 20:43:53 -04:00
|
|
|
if (item_s > *max_field_size) {
|
|
|
|
*max_field_size = item_s;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Elements of array are aggregates
|
2015-12-09 14:39:36 -05:00
|
|
|
item_s = uctypes_struct_size(t->items[2], layout_type, max_field_size);
|
2014-10-29 20:43:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return item_s * arr_sz;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return total_size;
|
|
|
|
}
|
|
|
|
|
2015-12-09 14:39:36 -05:00
|
|
|
STATIC mp_uint_t uctypes_struct_size(mp_obj_t desc_in, int layout_type, mp_uint_t *max_field_size) {
|
2020-06-21 03:14:18 -04:00
|
|
|
if (!mp_obj_is_dict_or_ordereddict(desc_in)) {
|
2019-01-30 06:04:54 -05:00
|
|
|
if (mp_obj_is_type(desc_in, &mp_type_tuple)) {
|
2015-12-09 14:39:36 -05:00
|
|
|
return uctypes_struct_agg_size((mp_obj_tuple_t *)MP_OBJ_TO_PTR(desc_in), layout_type, max_field_size);
|
2019-01-30 06:04:54 -05:00
|
|
|
} else if (mp_obj_is_small_int(desc_in)) {
|
2014-10-29 20:43:53 -04:00
|
|
|
// We allow sizeof on both type definitions and structures/structure fields,
|
|
|
|
// but scalar structure field is lowered into native Python int, so all
|
|
|
|
// type info is lost. So, we cannot say if it's scalar type description,
|
|
|
|
// or such lowered scalar.
|
2020-04-09 03:22:25 -04:00
|
|
|
mp_raise_TypeError(MP_ERROR_TEXT("can't unambiguously get sizeof scalar"));
|
2014-10-29 20:43:53 -04:00
|
|
|
}
|
2014-06-27 20:03:47 -04:00
|
|
|
syntax_error();
|
|
|
|
}
|
|
|
|
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_dict_t *d = MP_OBJ_TO_PTR(desc_in);
|
|
|
|
mp_uint_t total_size = 0;
|
|
|
|
|
2014-06-27 20:03:47 -04:00
|
|
|
for (mp_uint_t i = 0; i < d->map.alloc; i++) {
|
2019-01-30 06:04:54 -05:00
|
|
|
if (mp_map_slot_is_filled(&d->map, i)) {
|
2014-06-27 20:03:47 -04:00
|
|
|
mp_obj_t v = d->map.table[i].value;
|
2019-01-30 06:04:54 -05:00
|
|
|
if (mp_obj_is_small_int(v)) {
|
2014-06-27 20:03:47 -04:00
|
|
|
mp_uint_t offset = MP_OBJ_SMALL_INT_VALUE(v);
|
|
|
|
mp_uint_t val_type = GET_TYPE(offset, VAL_TYPE_BITS);
|
|
|
|
offset &= VALUE_MASK(VAL_TYPE_BITS);
|
2016-01-03 13:38:36 -05:00
|
|
|
if (val_type >= BFUINT8 && val_type <= BFINT32) {
|
|
|
|
offset &= (1 << OFFSET_BITS) - 1;
|
|
|
|
}
|
2014-06-27 20:03:47 -04:00
|
|
|
mp_uint_t s = uctypes_struct_scalar_size(val_type);
|
|
|
|
if (s > *max_field_size) {
|
|
|
|
*max_field_size = s;
|
|
|
|
}
|
|
|
|
if (offset + s > total_size) {
|
|
|
|
total_size = offset + s;
|
|
|
|
}
|
|
|
|
} else {
|
2019-01-30 06:04:54 -05:00
|
|
|
if (!mp_obj_is_type(v, &mp_type_tuple)) {
|
2014-06-27 20:03:47 -04:00
|
|
|
syntax_error();
|
|
|
|
}
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(v);
|
2014-06-27 20:03:47 -04:00
|
|
|
mp_int_t offset = MP_OBJ_SMALL_INT_VALUE(t->items[0]);
|
|
|
|
offset &= VALUE_MASK(AGG_TYPE_BITS);
|
2015-12-09 14:39:36 -05:00
|
|
|
mp_uint_t s = uctypes_struct_agg_size(t, layout_type, max_field_size);
|
2014-10-29 20:43:53 -04:00
|
|
|
if (offset + s > total_size) {
|
|
|
|
total_size = offset + s;
|
2014-06-27 20:03:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Round size up to alignment of biggest field
|
2015-12-09 14:39:36 -05:00
|
|
|
if (layout_type == LAYOUT_NATIVE) {
|
|
|
|
total_size = (total_size + *max_field_size - 1) & ~(*max_field_size - 1);
|
|
|
|
}
|
2014-06-27 20:03:47 -04:00
|
|
|
return total_size;
|
|
|
|
}
|
|
|
|
|
2018-08-25 18:55:43 -04:00
|
|
|
STATIC mp_obj_t uctypes_struct_sizeof(size_t n_args, const mp_obj_t *args) {
|
|
|
|
mp_obj_t obj_in = args[0];
|
2014-06-27 20:03:47 -04:00
|
|
|
mp_uint_t max_field_size = 0;
|
2019-01-30 06:04:54 -05:00
|
|
|
if (mp_obj_is_type(obj_in, &mp_type_bytearray)) {
|
2014-10-29 21:48:50 -04:00
|
|
|
return mp_obj_len(obj_in);
|
|
|
|
}
|
2015-12-09 14:39:36 -05:00
|
|
|
int layout_type = LAYOUT_NATIVE;
|
2014-10-29 20:43:53 -04:00
|
|
|
// We can apply sizeof either to structure definition (a dict)
|
|
|
|
// or to instantiated structure
|
2019-01-30 06:04:54 -05:00
|
|
|
if (mp_obj_is_type(obj_in, &uctypes_struct_type)) {
|
2018-08-25 18:55:43 -04:00
|
|
|
if (n_args != 1) {
|
|
|
|
mp_raise_TypeError(NULL);
|
|
|
|
}
|
2014-10-29 20:43:53 -04:00
|
|
|
// Extract structure definition
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_uctypes_struct_t *obj = MP_OBJ_TO_PTR(obj_in);
|
2014-06-27 20:03:47 -04:00
|
|
|
obj_in = obj->desc;
|
2015-12-09 14:39:36 -05:00
|
|
|
layout_type = obj->flags;
|
2018-08-25 18:55:43 -04:00
|
|
|
} else {
|
|
|
|
if (n_args == 2) {
|
|
|
|
layout_type = mp_obj_get_int(args[1]);
|
|
|
|
}
|
2014-06-27 20:03:47 -04:00
|
|
|
}
|
2015-12-09 14:39:36 -05:00
|
|
|
mp_uint_t size = uctypes_struct_size(obj_in, layout_type, &max_field_size);
|
2014-06-27 20:03:47 -04:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(size);
|
|
|
|
}
|
2018-08-25 18:55:43 -04:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(uctypes_struct_sizeof_obj, 1, 2, uctypes_struct_sizeof);
|
2014-06-27 20:03:47 -04:00
|
|
|
|
2017-08-01 23:42:34 -04:00
|
|
|
static inline mp_obj_t get_unaligned(uint val_type, byte *p, int big_endian) {
|
2016-03-19 17:41:01 -04:00
|
|
|
char struct_type = big_endian ? '>' : '<';
|
2016-03-19 17:59:42 -04:00
|
|
|
static const char type2char[16] = "BbHhIiQq------fd";
|
2019-08-05 01:15:28 -04:00
|
|
|
return mp_binary_get_val(struct_type, type2char[val_type], p, &p);
|
2014-06-27 20:03:47 -04:00
|
|
|
}
|
|
|
|
|
2017-08-01 23:42:34 -04:00
|
|
|
static inline void set_unaligned(uint val_type, byte *p, int big_endian, mp_obj_t val) {
|
2014-06-27 20:03:47 -04:00
|
|
|
char struct_type = big_endian ? '>' : '<';
|
2016-03-19 17:59:42 -04:00
|
|
|
static const char type2char[16] = "BbHhIiQq------fd";
|
2019-09-01 22:57:51 -04:00
|
|
|
mp_binary_set_val(struct_type, type2char[val_type], val, p, &p);
|
2014-06-27 20:03:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline mp_uint_t get_aligned_basic(uint val_type, void *p) {
|
|
|
|
switch (val_type) {
|
|
|
|
case UINT8:
|
|
|
|
return *(uint8_t *)p;
|
|
|
|
case UINT16:
|
|
|
|
return *(uint16_t *)p;
|
|
|
|
case UINT32:
|
|
|
|
return *(uint32_t *)p;
|
|
|
|
}
|
|
|
|
assert(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void set_aligned_basic(uint val_type, void *p, mp_uint_t v) {
|
|
|
|
switch (val_type) {
|
|
|
|
case UINT8:
|
|
|
|
*(uint8_t *)p = (uint8_t)v;
|
|
|
|
return;
|
|
|
|
case UINT16:
|
|
|
|
*(uint16_t *)p = (uint16_t)v;
|
|
|
|
return;
|
|
|
|
case UINT32:
|
|
|
|
*(uint32_t *)p = (uint32_t)v;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_obj_t get_aligned(uint val_type, void *p, mp_int_t index) {
|
|
|
|
switch (val_type) {
|
|
|
|
case UINT8:
|
2014-07-31 05:49:14 -04:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(((uint8_t *)p)[index]);
|
2014-06-27 20:03:47 -04:00
|
|
|
case INT8:
|
2014-07-31 05:49:14 -04:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(((int8_t *)p)[index]);
|
2014-06-27 20:03:47 -04:00
|
|
|
case UINT16:
|
2014-07-31 05:49:14 -04:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(((uint16_t *)p)[index]);
|
2014-06-27 20:03:47 -04:00
|
|
|
case INT16:
|
2014-07-31 05:49:14 -04:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(((int16_t *)p)[index]);
|
2014-06-27 20:03:47 -04:00
|
|
|
case UINT32:
|
|
|
|
return mp_obj_new_int_from_uint(((uint32_t *)p)[index]);
|
|
|
|
case INT32:
|
|
|
|
return mp_obj_new_int(((int32_t *)p)[index]);
|
|
|
|
case UINT64:
|
2015-10-27 17:21:28 -04:00
|
|
|
return mp_obj_new_int_from_ull(((uint64_t *)p)[index]);
|
2014-06-27 20:03:47 -04:00
|
|
|
case INT64:
|
|
|
|
return mp_obj_new_int_from_ll(((int64_t *)p)[index]);
|
2014-09-26 12:04:05 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2014-06-27 20:03:47 -04:00
|
|
|
case FLOAT32:
|
2020-04-09 03:05:48 -04:00
|
|
|
return mp_obj_new_float_from_f(((float *)p)[index]);
|
2014-06-27 20:03:47 -04:00
|
|
|
case FLOAT64:
|
2020-03-31 08:48:08 -04:00
|
|
|
return mp_obj_new_float_from_d(((double *)p)[index]);
|
2014-09-26 12:04:05 -04:00
|
|
|
#endif
|
2014-06-27 20:03:47 -04:00
|
|
|
default:
|
|
|
|
assert(0);
|
2014-08-12 15:02:26 -04:00
|
|
|
return MP_OBJ_NULL;
|
2014-06-27 20:03:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void set_aligned(uint val_type, void *p, mp_int_t index, mp_obj_t val) {
|
2016-03-19 17:59:42 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
|
|
|
if (val_type == FLOAT32 || val_type == FLOAT64) {
|
|
|
|
if (val_type == FLOAT32) {
|
2020-04-09 03:05:48 -04:00
|
|
|
((float *)p)[index] = mp_obj_get_float_to_f(val);
|
2016-03-19 17:59:42 -04:00
|
|
|
} else {
|
2020-04-09 03:05:48 -04:00
|
|
|
((double *)p)[index] = mp_obj_get_float_to_d(val);
|
2016-03-19 17:59:42 -04:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2017-04-21 09:40:57 -04:00
|
|
|
mp_int_t v = mp_obj_get_int_truncated(val);
|
2014-06-27 20:03:47 -04:00
|
|
|
switch (val_type) {
|
|
|
|
case UINT8:
|
|
|
|
((uint8_t *)p)[index] = (uint8_t)v;
|
|
|
|
return;
|
|
|
|
case INT8:
|
|
|
|
((int8_t *)p)[index] = (int8_t)v;
|
|
|
|
return;
|
|
|
|
case UINT16:
|
|
|
|
((uint16_t *)p)[index] = (uint16_t)v;
|
|
|
|
return;
|
|
|
|
case INT16:
|
|
|
|
((int16_t *)p)[index] = (int16_t)v;
|
|
|
|
return;
|
|
|
|
case UINT32:
|
|
|
|
((uint32_t *)p)[index] = (uint32_t)v;
|
|
|
|
return;
|
|
|
|
case INT32:
|
|
|
|
((int32_t *)p)[index] = (int32_t)v;
|
|
|
|
return;
|
2015-12-03 17:58:46 -05:00
|
|
|
case INT64:
|
|
|
|
case UINT64:
|
|
|
|
if (sizeof(mp_int_t) == 8) {
|
|
|
|
((uint64_t *)p)[index] = (uint64_t)v;
|
|
|
|
} else {
|
|
|
|
// TODO: Doesn't offer atomic store semantics, but should at least try
|
2020-11-05 06:39:54 -05:00
|
|
|
set_unaligned(val_type, (void *)&((uint64_t *)p)[index], MP_ENDIANNESS_BIG, val);
|
2015-12-03 17:58:46 -05:00
|
|
|
}
|
|
|
|
return;
|
2014-06-27 20:03:47 -04:00
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_obj_t uctypes_struct_attr_op(mp_obj_t self_in, qstr attr, mp_obj_t set_val) {
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_uctypes_struct_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-06-27 20:03:47 -04:00
|
|
|
|
2020-06-21 03:14:18 -04:00
|
|
|
if (!mp_obj_is_dict_or_ordereddict(self->desc)) {
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_TypeError(MP_ERROR_TEXT("struct: no fields"));
|
2014-06-27 20:03:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t deref = mp_obj_dict_get(self->desc, MP_OBJ_NEW_QSTR(attr));
|
2019-01-30 06:04:54 -05:00
|
|
|
if (mp_obj_is_small_int(deref)) {
|
2014-06-27 20:03:47 -04:00
|
|
|
mp_int_t offset = MP_OBJ_SMALL_INT_VALUE(deref);
|
|
|
|
mp_uint_t val_type = GET_TYPE(offset, VAL_TYPE_BITS);
|
|
|
|
offset &= VALUE_MASK(VAL_TYPE_BITS);
|
|
|
|
|
2016-03-19 17:59:42 -04:00
|
|
|
if (val_type <= INT64 || val_type == FLOAT32 || val_type == FLOAT64) {
|
2014-06-27 20:03:47 -04:00
|
|
|
if (self->flags == LAYOUT_NATIVE) {
|
|
|
|
if (set_val == MP_OBJ_NULL) {
|
|
|
|
return get_aligned(val_type, self->addr + offset, 0);
|
|
|
|
} else {
|
|
|
|
set_aligned(val_type, self->addr + offset, 0, set_val);
|
|
|
|
return set_val; // just !MP_OBJ_NULL
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (set_val == MP_OBJ_NULL) {
|
|
|
|
return get_unaligned(val_type, self->addr + offset, self->flags);
|
|
|
|
} else {
|
|
|
|
set_unaligned(val_type, self->addr + offset, self->flags, set_val);
|
|
|
|
return set_val; // just !MP_OBJ_NULL
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (val_type >= BFUINT8 && val_type <= BFINT32) {
|
2021-05-05 22:40:53 -04:00
|
|
|
uint bit_offset = (offset >> OFFSET_BITS) & 31;
|
|
|
|
uint bit_len = (offset >> LEN_BITS) & 31;
|
|
|
|
offset &= (1 << OFFSET_BITS) - 1;
|
2014-06-27 20:03:47 -04:00
|
|
|
mp_uint_t val;
|
|
|
|
if (self->flags == LAYOUT_NATIVE) {
|
|
|
|
val = get_aligned_basic(val_type & 6, self->addr + offset);
|
|
|
|
} else {
|
|
|
|
val = mp_binary_get_int(GET_SCALAR_SIZE(val_type & 7), val_type & 1, self->flags, self->addr + offset);
|
|
|
|
}
|
|
|
|
if (set_val == MP_OBJ_NULL) {
|
|
|
|
val >>= bit_offset;
|
|
|
|
val &= (1 << bit_len) - 1;
|
|
|
|
// TODO: signed
|
|
|
|
assert((val_type & 1) == 0);
|
|
|
|
return mp_obj_new_int(val);
|
|
|
|
} else {
|
|
|
|
mp_uint_t set_val_int = (mp_uint_t)mp_obj_get_int(set_val);
|
|
|
|
mp_uint_t mask = (1 << bit_len) - 1;
|
|
|
|
set_val_int &= mask;
|
|
|
|
set_val_int <<= bit_offset;
|
|
|
|
mask <<= bit_offset;
|
|
|
|
val = (val & ~mask) | set_val_int;
|
|
|
|
|
|
|
|
if (self->flags == LAYOUT_NATIVE) {
|
|
|
|
set_aligned_basic(val_type & 6, self->addr + offset, val);
|
|
|
|
} else {
|
|
|
|
mp_binary_set_int(GET_SCALAR_SIZE(val_type & 7), self->flags == LAYOUT_BIG_ENDIAN,
|
2014-10-06 11:05:35 -04:00
|
|
|
self->addr + offset, val);
|
2014-06-27 20:03:47 -04:00
|
|
|
}
|
|
|
|
return set_val; // just !MP_OBJ_NULL
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(0);
|
|
|
|
return MP_OBJ_NULL;
|
|
|
|
}
|
|
|
|
|
2019-01-30 06:04:54 -05:00
|
|
|
if (!mp_obj_is_type(deref, &mp_type_tuple)) {
|
2014-06-27 20:03:47 -04:00
|
|
|
syntax_error();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (set_val != MP_OBJ_NULL) {
|
|
|
|
// Cannot assign to aggregate
|
|
|
|
syntax_error();
|
|
|
|
}
|
|
|
|
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_tuple_t *sub = MP_OBJ_TO_PTR(deref);
|
2014-06-27 20:03:47 -04:00
|
|
|
mp_int_t offset = MP_OBJ_SMALL_INT_VALUE(sub->items[0]);
|
|
|
|
mp_uint_t agg_type = GET_TYPE(offset, AGG_TYPE_BITS);
|
|
|
|
offset &= VALUE_MASK(AGG_TYPE_BITS);
|
|
|
|
|
|
|
|
switch (agg_type) {
|
|
|
|
case STRUCT: {
|
2022-04-22 03:09:15 -04:00
|
|
|
mp_obj_uctypes_struct_t *o = mp_obj_malloc(mp_obj_uctypes_struct_t, &uctypes_struct_type);
|
2014-06-27 20:03:47 -04:00
|
|
|
o->desc = sub->items[1];
|
|
|
|
o->addr = self->addr + offset;
|
|
|
|
o->flags = self->flags;
|
2015-11-27 12:01:44 -05:00
|
|
|
return MP_OBJ_FROM_PTR(o);
|
2014-06-27 20:03:47 -04:00
|
|
|
}
|
2014-10-29 21:32:54 -04:00
|
|
|
case ARRAY: {
|
|
|
|
mp_uint_t dummy;
|
|
|
|
if (IS_SCALAR_ARRAY(sub) && IS_SCALAR_ARRAY_OF_BYTES(sub)) {
|
2015-12-09 14:39:36 -05:00
|
|
|
return mp_obj_new_bytearray_by_ref(uctypes_struct_agg_size(sub, self->flags, &dummy), self->addr + offset);
|
2014-10-29 21:32:54 -04:00
|
|
|
}
|
|
|
|
// Fall thru to return uctypes struct object
|
2019-11-28 06:47:21 -05:00
|
|
|
MP_FALLTHROUGH
|
2014-10-29 21:32:54 -04:00
|
|
|
}
|
|
|
|
case PTR: {
|
2022-04-22 03:09:15 -04:00
|
|
|
mp_obj_uctypes_struct_t *o = mp_obj_malloc(mp_obj_uctypes_struct_t, &uctypes_struct_type);
|
2015-11-27 12:01:44 -05:00
|
|
|
o->desc = MP_OBJ_FROM_PTR(sub);
|
2014-06-27 20:03:47 -04:00
|
|
|
o->addr = self->addr + offset;
|
|
|
|
o->flags = self->flags;
|
2015-11-27 12:01:44 -05:00
|
|
|
return MP_OBJ_FROM_PTR(o);
|
2014-06-27 20:03:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should be unreachable once all cases are handled
|
|
|
|
return MP_OBJ_NULL;
|
|
|
|
}
|
|
|
|
|
2015-04-01 10:10:50 -04:00
|
|
|
STATIC void uctypes_struct_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
|
|
|
|
if (dest[0] == MP_OBJ_NULL) {
|
|
|
|
// load attribute
|
|
|
|
mp_obj_t val = uctypes_struct_attr_op(self_in, attr, MP_OBJ_NULL);
|
|
|
|
dest[0] = val;
|
|
|
|
} else {
|
|
|
|
// delete/store attribute
|
|
|
|
if (uctypes_struct_attr_op(self_in, attr, dest[1]) != MP_OBJ_NULL) {
|
|
|
|
dest[0] = MP_OBJ_NULL; // indicate success
|
|
|
|
}
|
|
|
|
}
|
2014-06-27 20:03:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
STATIC mp_obj_t uctypes_struct_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value) {
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_uctypes_struct_t *self = MP_OBJ_TO_PTR(self_in);
|
2014-06-27 20:03:47 -04:00
|
|
|
|
|
|
|
if (value == MP_OBJ_NULL) {
|
|
|
|
// delete
|
|
|
|
return MP_OBJ_NULL; // op not supported
|
2015-12-27 14:16:45 -05:00
|
|
|
} else {
|
|
|
|
// load / store
|
2019-01-30 06:04:54 -05:00
|
|
|
if (!mp_obj_is_type(self->desc, &mp_type_tuple)) {
|
2020-04-09 03:22:25 -04:00
|
|
|
mp_raise_TypeError(MP_ERROR_TEXT("struct: can't index"));
|
2014-06-27 20:03:47 -04:00
|
|
|
}
|
|
|
|
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(self->desc);
|
2014-06-27 20:03:47 -04:00
|
|
|
mp_int_t offset = MP_OBJ_SMALL_INT_VALUE(t->items[0]);
|
|
|
|
uint agg_type = GET_TYPE(offset, AGG_TYPE_BITS);
|
|
|
|
|
|
|
|
mp_int_t index = MP_OBJ_SMALL_INT_VALUE(index_in);
|
|
|
|
|
|
|
|
if (agg_type == ARRAY) {
|
|
|
|
mp_int_t arr_sz = MP_OBJ_SMALL_INT_VALUE(t->items[1]);
|
|
|
|
uint val_type = GET_TYPE(arr_sz, VAL_TYPE_BITS);
|
|
|
|
arr_sz &= VALUE_MASK(VAL_TYPE_BITS);
|
|
|
|
if (index >= arr_sz) {
|
2020-03-02 06:35:22 -05:00
|
|
|
mp_raise_msg(&mp_type_IndexError, MP_ERROR_TEXT("struct: index out of range"));
|
2014-06-27 20:03:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (t->len == 2) {
|
2015-12-27 14:16:45 -05:00
|
|
|
// array of scalars
|
|
|
|
if (self->flags == LAYOUT_NATIVE) {
|
|
|
|
if (value == MP_OBJ_SENTINEL) {
|
|
|
|
return get_aligned(val_type, self->addr, index);
|
|
|
|
} else {
|
|
|
|
set_aligned(val_type, self->addr, index, value);
|
|
|
|
return value; // just !MP_OBJ_NULL
|
|
|
|
}
|
|
|
|
} else {
|
2021-05-05 23:11:33 -04:00
|
|
|
byte *p = self->addr + uctypes_struct_scalar_size(val_type) * index;
|
2015-12-27 14:16:45 -05:00
|
|
|
if (value == MP_OBJ_SENTINEL) {
|
|
|
|
return get_unaligned(val_type, p, self->flags);
|
|
|
|
} else {
|
|
|
|
set_unaligned(val_type, p, self->flags, value);
|
|
|
|
return value; // just !MP_OBJ_NULL
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (value == MP_OBJ_SENTINEL) {
|
2014-06-27 20:03:47 -04:00
|
|
|
mp_uint_t dummy = 0;
|
2015-12-09 14:39:36 -05:00
|
|
|
mp_uint_t size = uctypes_struct_size(t->items[2], self->flags, &dummy);
|
2022-04-22 03:09:15 -04:00
|
|
|
mp_obj_uctypes_struct_t *o = mp_obj_malloc(mp_obj_uctypes_struct_t, &uctypes_struct_type);
|
2014-06-27 20:03:47 -04:00
|
|
|
o->desc = t->items[2];
|
|
|
|
o->addr = self->addr + size * index;
|
|
|
|
o->flags = self->flags;
|
2015-11-27 12:01:44 -05:00
|
|
|
return MP_OBJ_FROM_PTR(o);
|
2015-12-27 14:16:45 -05:00
|
|
|
} else {
|
|
|
|
return MP_OBJ_NULL; // op not supported
|
2014-06-27 20:03:47 -04:00
|
|
|
}
|
2015-12-27 14:16:45 -05:00
|
|
|
|
2014-06-27 20:03:47 -04:00
|
|
|
} else if (agg_type == PTR) {
|
|
|
|
byte *p = *(void **)self->addr;
|
2019-01-30 06:04:54 -05:00
|
|
|
if (mp_obj_is_small_int(t->items[1])) {
|
2014-06-27 20:03:47 -04:00
|
|
|
uint val_type = GET_TYPE(MP_OBJ_SMALL_INT_VALUE(t->items[1]), VAL_TYPE_BITS);
|
|
|
|
return get_aligned(val_type, p, index);
|
|
|
|
} else {
|
|
|
|
mp_uint_t dummy = 0;
|
2015-12-09 14:39:36 -05:00
|
|
|
mp_uint_t size = uctypes_struct_size(t->items[1], self->flags, &dummy);
|
2022-04-22 03:09:15 -04:00
|
|
|
mp_obj_uctypes_struct_t *o = mp_obj_malloc(mp_obj_uctypes_struct_t, &uctypes_struct_type);
|
2014-06-27 20:03:47 -04:00
|
|
|
o->desc = t->items[1];
|
|
|
|
o->addr = p + size * index;
|
|
|
|
o->flags = self->flags;
|
2015-11-27 12:01:44 -05:00
|
|
|
return MP_OBJ_FROM_PTR(o);
|
2014-06-27 20:03:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(0);
|
2014-08-12 15:02:26 -04:00
|
|
|
return MP_OBJ_NULL;
|
2014-06-27 20:03:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-29 12:37:03 -04:00
|
|
|
STATIC mp_obj_t uctypes_struct_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
|
|
|
|
mp_obj_uctypes_struct_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
switch (op) {
|
|
|
|
case MP_UNARY_OP_INT:
|
2019-01-30 06:04:54 -05:00
|
|
|
if (mp_obj_is_type(self->desc, &mp_type_tuple)) {
|
2018-08-29 12:37:03 -04:00
|
|
|
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(self->desc);
|
|
|
|
mp_int_t offset = MP_OBJ_SMALL_INT_VALUE(t->items[0]);
|
|
|
|
uint agg_type = GET_TYPE(offset, AGG_TYPE_BITS);
|
|
|
|
if (agg_type == PTR) {
|
|
|
|
byte *p = *(void **)self->addr;
|
|
|
|
return mp_obj_new_int((mp_int_t)(uintptr_t)p);
|
|
|
|
}
|
|
|
|
}
|
2019-11-28 06:47:21 -05:00
|
|
|
MP_FALLTHROUGH
|
2018-08-29 12:37:03 -04:00
|
|
|
|
|
|
|
default:
|
|
|
|
return MP_OBJ_NULL; // op not supported
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-25 18:02:27 -04:00
|
|
|
STATIC mp_int_t uctypes_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
|
|
|
|
(void)flags;
|
2015-11-27 12:01:44 -05:00
|
|
|
mp_obj_uctypes_struct_t *self = MP_OBJ_TO_PTR(self_in);
|
2015-10-25 18:02:27 -04:00
|
|
|
mp_uint_t max_field_size = 0;
|
2015-12-09 14:39:36 -05:00
|
|
|
mp_uint_t size = uctypes_struct_size(self->desc, self->flags, &max_field_size);
|
2015-10-25 18:02:27 -04:00
|
|
|
|
|
|
|
bufinfo->buf = self->addr;
|
|
|
|
bufinfo->len = size;
|
|
|
|
bufinfo->typecode = BYTEARRAY_TYPECODE;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-05-05 22:11:51 -04:00
|
|
|
// addressof()
|
|
|
|
// Return address of object's data (applies to objects providing the buffer interface).
|
2014-12-10 17:07:04 -05:00
|
|
|
STATIC mp_obj_t uctypes_struct_addressof(mp_obj_t buf) {
|
2014-06-27 20:03:47 -04:00
|
|
|
mp_buffer_info_t bufinfo;
|
|
|
|
mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ);
|
2015-11-27 12:01:44 -05:00
|
|
|
return mp_obj_new_int((mp_int_t)(uintptr_t)bufinfo.buf);
|
2014-06-27 20:03:47 -04:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(uctypes_struct_addressof_obj, uctypes_struct_addressof);
|
|
|
|
|
2021-05-05 22:11:51 -04:00
|
|
|
// bytearray_at()
|
|
|
|
// Capture memory at given address of given size as bytearray.
|
2014-12-10 17:07:04 -05:00
|
|
|
STATIC mp_obj_t uctypes_struct_bytearray_at(mp_obj_t ptr, mp_obj_t size) {
|
2015-11-27 12:01:44 -05:00
|
|
|
return mp_obj_new_bytearray_by_ref(mp_obj_int_get_truncated(size), (void *)(uintptr_t)mp_obj_int_get_truncated(ptr));
|
2014-06-27 20:03:47 -04:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(uctypes_struct_bytearray_at_obj, uctypes_struct_bytearray_at);
|
|
|
|
|
2021-05-05 22:11:51 -04:00
|
|
|
// bytes_at()
|
|
|
|
// Capture memory at given address of given size as bytes.
|
2014-12-10 17:07:04 -05:00
|
|
|
STATIC mp_obj_t uctypes_struct_bytes_at(mp_obj_t ptr, mp_obj_t size) {
|
2015-11-27 12:01:44 -05:00
|
|
|
return mp_obj_new_bytes((void *)(uintptr_t)mp_obj_int_get_truncated(ptr), mp_obj_int_get_truncated(size));
|
2014-06-27 20:03:47 -04:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(uctypes_struct_bytes_at_obj, uctypes_struct_bytes_at);
|
|
|
|
|
2021-07-14 00:38:38 -04:00
|
|
|
STATIC MP_DEFINE_CONST_OBJ_TYPE(
|
|
|
|
uctypes_struct_type,
|
|
|
|
MP_QSTR_struct,
|
|
|
|
MP_TYPE_FLAG_NONE,
|
2022-09-16 10:31:23 -04:00
|
|
|
make_new, uctypes_struct_make_new,
|
2021-07-14 00:38:38 -04:00
|
|
|
print, uctypes_struct_print,
|
|
|
|
attr, uctypes_struct_attr,
|
|
|
|
subscr, uctypes_struct_subscr,
|
|
|
|
unary_op, uctypes_struct_unary_op,
|
|
|
|
buffer, uctypes_get_buffer
|
|
|
|
);
|
2014-06-27 20:03:47 -04:00
|
|
|
|
2015-11-27 08:38:15 -05:00
|
|
|
STATIC const mp_rom_map_elem_t mp_module_uctypes_globals_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_uctypes) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_struct), MP_ROM_PTR(&uctypes_struct_type) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_sizeof), MP_ROM_PTR(&uctypes_struct_sizeof_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_addressof), MP_ROM_PTR(&uctypes_struct_addressof_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_bytes_at), MP_ROM_PTR(&uctypes_struct_bytes_at_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_bytearray_at), MP_ROM_PTR(&uctypes_struct_bytearray_at_obj) },
|
2014-06-27 20:03:47 -04:00
|
|
|
|
2015-11-27 08:38:15 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_NATIVE), MP_ROM_INT(LAYOUT_NATIVE) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_LITTLE_ENDIAN), MP_ROM_INT(LAYOUT_LITTLE_ENDIAN) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_BIG_ENDIAN), MP_ROM_INT(LAYOUT_BIG_ENDIAN) },
|
2014-06-27 20:03:47 -04:00
|
|
|
|
2015-11-27 08:38:15 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_VOID), MP_ROM_INT(TYPE2SMALLINT(UINT8, VAL_TYPE_BITS)) },
|
2014-06-27 20:03:47 -04:00
|
|
|
|
2021-05-05 22:40:53 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_UINT8), MP_ROM_INT(TYPE2SMALLINT(UINT8, VAL_TYPE_BITS)) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_INT8), MP_ROM_INT(TYPE2SMALLINT(INT8, VAL_TYPE_BITS)) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_UINT16), MP_ROM_INT(TYPE2SMALLINT(UINT16, VAL_TYPE_BITS)) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_INT16), MP_ROM_INT(TYPE2SMALLINT(INT16, VAL_TYPE_BITS)) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_UINT32), MP_ROM_INT(TYPE2SMALLINT(UINT32, VAL_TYPE_BITS)) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_INT32), MP_ROM_INT(TYPE2SMALLINT(INT32, VAL_TYPE_BITS)) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_UINT64), MP_ROM_INT(TYPE2SMALLINT(UINT64, VAL_TYPE_BITS)) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_INT64), MP_ROM_INT(TYPE2SMALLINT(INT64, VAL_TYPE_BITS)) },
|
|
|
|
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_BFUINT8), MP_ROM_INT(TYPE2SMALLINT(BFUINT8, VAL_TYPE_BITS)) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_BFINT8), MP_ROM_INT(TYPE2SMALLINT(BFINT8, VAL_TYPE_BITS)) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_BFUINT16), MP_ROM_INT(TYPE2SMALLINT(BFUINT16, VAL_TYPE_BITS)) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_BFINT16), MP_ROM_INT(TYPE2SMALLINT(BFINT16, VAL_TYPE_BITS)) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_BFUINT32), MP_ROM_INT(TYPE2SMALLINT(BFUINT32, VAL_TYPE_BITS)) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_BFINT32), MP_ROM_INT(TYPE2SMALLINT(BFINT32, VAL_TYPE_BITS)) },
|
|
|
|
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_BF_POS), MP_ROM_INT(OFFSET_BITS) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_BF_LEN), MP_ROM_INT(LEN_BITS) },
|
2014-07-10 17:06:36 -04:00
|
|
|
|
2016-03-19 17:59:42 -04:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2021-05-05 22:40:53 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_FLOAT32), MP_ROM_INT(TYPE2SMALLINT(FLOAT32, VAL_TYPE_BITS)) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_FLOAT64), MP_ROM_INT(TYPE2SMALLINT(FLOAT64, VAL_TYPE_BITS)) },
|
2016-03-19 17:59:42 -04:00
|
|
|
#endif
|
|
|
|
|
2018-10-06 16:34:58 -04:00
|
|
|
#if MICROPY_PY_UCTYPES_NATIVE_C_TYPES
|
|
|
|
// C native type aliases. These depend on GCC-compatible predefined
|
|
|
|
// preprocessor macros.
|
|
|
|
#if __SIZEOF_SHORT__ == 2
|
2021-05-05 22:40:53 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_SHORT), MP_ROM_INT(TYPE2SMALLINT(INT16, VAL_TYPE_BITS)) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_USHORT), MP_ROM_INT(TYPE2SMALLINT(UINT16, VAL_TYPE_BITS)) },
|
2018-10-06 16:34:58 -04:00
|
|
|
#endif
|
|
|
|
#if __SIZEOF_INT__ == 4
|
2021-05-05 22:40:53 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_INT), MP_ROM_INT(TYPE2SMALLINT(INT32, VAL_TYPE_BITS)) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_UINT), MP_ROM_INT(TYPE2SMALLINT(UINT32, VAL_TYPE_BITS)) },
|
2018-10-06 16:34:58 -04:00
|
|
|
#endif
|
|
|
|
#if __SIZEOF_LONG__ == 4
|
2021-05-05 22:40:53 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_LONG), MP_ROM_INT(TYPE2SMALLINT(INT32, VAL_TYPE_BITS)) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_ULONG), MP_ROM_INT(TYPE2SMALLINT(UINT32, VAL_TYPE_BITS)) },
|
2018-10-06 16:34:58 -04:00
|
|
|
#elif __SIZEOF_LONG__ == 8
|
2021-05-05 22:40:53 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_LONG), MP_ROM_INT(TYPE2SMALLINT(INT64, VAL_TYPE_BITS)) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_ULONG), MP_ROM_INT(TYPE2SMALLINT(UINT64, VAL_TYPE_BITS)) },
|
2018-10-06 16:34:58 -04:00
|
|
|
#endif
|
|
|
|
#if __SIZEOF_LONG_LONG__ == 8
|
2021-05-05 22:40:53 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_LONGLONG), MP_ROM_INT(TYPE2SMALLINT(INT64, VAL_TYPE_BITS)) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_ULONGLONG), MP_ROM_INT(TYPE2SMALLINT(UINT64, VAL_TYPE_BITS)) },
|
2018-10-06 16:34:58 -04:00
|
|
|
#endif
|
|
|
|
#endif // MICROPY_PY_UCTYPES_NATIVE_C_TYPES
|
|
|
|
|
2015-11-27 08:38:15 -05:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_PTR), MP_ROM_INT(TYPE2SMALLINT(PTR, AGG_TYPE_BITS)) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_ARRAY), MP_ROM_INT(TYPE2SMALLINT(ARRAY, AGG_TYPE_BITS)) },
|
2014-06-27 20:03:47 -04:00
|
|
|
};
|
2014-11-29 09:39:27 -05:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(mp_module_uctypes_globals, mp_module_uctypes_globals_table);
|
2014-06-27 20:03:47 -04:00
|
|
|
|
|
|
|
const mp_obj_module_t mp_module_uctypes = {
|
|
|
|
.base = { &mp_type_module },
|
|
|
|
.globals = (mp_obj_dict_t *)&mp_module_uctypes_globals,
|
|
|
|
};
|
|
|
|
|
2022-05-31 08:56:11 -04:00
|
|
|
MP_REGISTER_MODULE(MP_QSTR_uctypes, mp_module_uctypes);
|
2022-04-20 02:06:22 -04:00
|
|
|
|
2014-06-27 20:03:47 -04:00
|
|
|
#endif
|