MP_ROM_QSTR, MP_OBJ_NEW_QSTR lower 2 bits?

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

MP_ROM_QSTR, MP_OBJ_NEW_QSTR lower 2 bits?

Post by jickster » Thu Dec 14, 2017 4:56 pm

Why is this type's lower two bits shifted reserved?

Code: Select all

#define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)((((mp_uint_t)(qst)) << 2) | 2))

User avatar
Posts: 3195
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada

Re: MP_ROM_QSTR, MP_OBJ_NEW_QSTR lower 2 bits?

Post by dhylands » Mon Dec 18, 2017 8:39 pm

This header file: ... .h#L51-L93 describes the various in-memory representations of the objects in MicroPython.

MicroPython was designed to run on a 32-bit MCU and assumes that pointers to objects will be 4-byte aligned. Which, in turn, means that the bottom 2 bits of an object pointer will always be 00. Changing the value of the bottom 2 bits allows integers and qstrs to be encoded in the same space as a pointer (instead of having to have a pointer point to an object containing the int or qstr).

Post Reply