Understanding definition of mp_uint_t and UINT_FMT in mpconfigport.h
Posted: Thu Mar 02, 2017 10:16 pm
I've been working on a port of MicroPython as a component of existing framework for an ARM-based platform using an IAR compiler, and I'm trying to understand the relationship between mp_uint_t and UINT_FMT as defined in mpconfigport.h. The plan is to have this code work on other hardware platforms supported by this framework, so I'm trying to use existing macros/types for portability.
I'm seeing that bare-arm and qemu-arm platforms use uint32_t and "%lu" which would only make sense if uint32_t is defined as an unsigned long. I thought the Cortex-M was a 32-bit platform (and therefore uses 32-bit integers), which would mean that "%lu" is potentially the wrong format to use when printing the value of an mp_uint_t variable. Unless both "unsigned" and "unsigned long" are 32 bits, in which case "%u" would work just as well as "%lu".
I would expect to see "%lu" for a 16-bit platform where unsigned is 16 bits, pointers are 32 bits and mp_uint_t is therefore an unsigned long.
I was planning to just use macros and types from inttypes.h and stdint.h as follows (and which should work for POSIX platforms). Is this safe, or have I misunderstood the purpose of these macros/types?
I'm seeing that bare-arm and qemu-arm platforms use uint32_t and "%lu" which would only make sense if uint32_t is defined as an unsigned long. I thought the Cortex-M was a 32-bit platform (and therefore uses 32-bit integers), which would mean that "%lu" is potentially the wrong format to use when printing the value of an mp_uint_t variable. Unless both "unsigned" and "unsigned long" are 32 bits, in which case "%u" would work just as well as "%lu".
I would expect to see "%lu" for a 16-bit platform where unsigned is 16 bits, pointers are 32 bits and mp_uint_t is therefore an unsigned long.
I was planning to just use macros and types from inttypes.h and stdint.h as follows (and which should work for POSIX platforms). Is this safe, or have I misunderstood the purpose of these macros/types?
Code: Select all
#define UINT_FMT "%" PRIuPTR // printf specifier for mp_uint_t
#define INT_FMT "%" PRIdPTR // printf specifier for mp_int_t
typedef intptr_t mp_int_t; // signed integer same size as a pointer
typedef uintptr_t mp_uint_t; // unsigned integer same size as a pointer