MP_ROM_QSTR, MP_OBJ_NEW_QSTR lower 2 bits?

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
jickster
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_ROM_QSTR(q) MP_OBJ_NEW_QSTR(q)
#define MP_OBJ_NEW_QSTR(qst) ((mp_obj_t)((((mp_uint_t)(qst)) << 2) | 2))

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

Re: MP_ROM_QSTR, MP_OBJ_NEW_QSTR lower 2 bits?

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

This header file: https://github.com/micropython/micropyt ... .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