diff options
Diffstat (limited to 'py/obj.h')
-rw-r--r-- | py/obj.h | 57 |
1 files changed, 56 insertions, 1 deletions
@@ -32,10 +32,15 @@ #include "py/mpprint.h" // All Micro Python objects are at least this type -// It must be of pointer size +// The bit-size must be at least pointer size +#if MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D +typedef uint64_t mp_obj_t; +typedef uint64_t mp_const_obj_t; +#else typedef machine_ptr_t mp_obj_t; typedef machine_const_ptr_t mp_const_obj_t; +#endif // Anything that wants to be a Micro Python object must have // mp_obj_base_t as its first member (except small ints and qstrs) @@ -158,6 +163,56 @@ static inline bool MP_OBJ_IS_QSTR(mp_const_obj_t o) static inline bool MP_OBJ_IS_OBJ(mp_const_obj_t o) { return ((((mp_int_t)(o)) & 3) == 0); } +#elif MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D + +static inline bool MP_OBJ_IS_SMALL_INT(mp_const_obj_t o) + { return ((((mp_int_t)(o)) & 0xffff000000000000) == 0x0001000000000000); } +#define MP_OBJ_SMALL_INT_VALUE(o) (((intptr_t)(o)) >> 1) +#define MP_OBJ_NEW_SMALL_INT(small_int) ((mp_obj_t)(((uintptr_t)(small_int)) << 1) | 0x0001000000000001) + +static inline bool MP_OBJ_IS_QSTR(mp_const_obj_t o) + { return ((((mp_int_t)(o)) & 0xffff000000000000) == 0x0002000000000000); } +#define MP_OBJ_QSTR_VALUE(o) ((((mp_uint_t)(o)) >> 1) & 0xffffffff) +#define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)((((mp_uint_t)(qst)) << 1) | 0x0002000000000001)) + +#if MICROPY_PY_BUILTINS_FLOAT +#define mp_const_float_e {((mp_obj_t)((uint64_t)0x4005bf0a8b125769 + 0x8004000000000000))} +#define mp_const_float_pi {((mp_obj_t)((uint64_t)0x400921fb54442d18 + 0x8004000000000000))} + +static inline bool mp_obj_is_float(mp_const_obj_t o) { + return ((uint64_t)(o) & 0xfffc000000000000) != 0; +} +static inline mp_float_t mp_obj_float_get(mp_const_obj_t o) { + union { + mp_float_t f; + uint64_t r; + } num = {.r = o - 0x8004000000000000}; + return num.f; +} +static inline mp_obj_t mp_obj_new_float(mp_float_t f) { + union { + mp_float_t f; + uint64_t r; + } num = {.f = f}; + return num.r + 0x8004000000000000; +} +#endif + +static inline bool MP_OBJ_IS_OBJ(mp_const_obj_t o) + { return ((((uint64_t)(o)) & 0xffff000000000000) == 0x0000000000000000); } +#define MP_OBJ_TO_PTR(o) ((void*)(uintptr_t)(o)) +#define MP_OBJ_FROM_PTR(p) ((mp_obj_t)((uintptr_t)(p))) + +// rom object storage needs special handling to widen 32-bit pointer to 64-bits +typedef union _mp_rom_obj_t { uint64_t u64; struct { const void *lo, *hi; } u32; } mp_rom_obj_t; +#define MP_ROM_INT(i) {MP_OBJ_NEW_SMALL_INT(i)} +#define MP_ROM_QSTR(q) {MP_OBJ_NEW_QSTR(q)} +#if MP_ENDIANNESS_LITTLE +#define MP_ROM_PTR(p) {.u32 = {.lo = (p), .hi = NULL}} +#else +#define MP_ROM_PTR(p) {.u32 = {.lo = NULL, .hi = (p)}} +#endif + #endif // Macros to convert between mp_obj_t and concrete object types. |