MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B

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

MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B

Post by jickster » Thu Aug 16, 2018 6:50 pm

Code: Select all

// A MicroPython object is a machine word having the following form:
//  - xxxx...xx01 : a small int, bits 2 and above are the value
//  - xxxx...xx11 : a qstr, bits 2 and above are the value
//  - xxxx...xxx0 : a pointer to an mp_obj_base_t (unless a fake object)
#define MICROPY_OBJ_REPR_B (1)
Does this mean pointers must have 2-byte alignment?

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

Re: MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B

Post by dhylands » Thu Aug 16, 2018 8:18 pm

Pointers to micropython objects always have at least 4-byte alignment. Note that this representation is not for pointers in general, just pointers to micropython objects.

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B

Post by jickster » Fri Aug 17, 2018 1:38 am

dhylands wrote:Pointers to micropython objects always have at least 4-byte alignment. Note that this representation is not for pointers in general, just pointers to micropython objects.
I don’t see how this is enforced. The REPR macros are not used in any of allocation functions

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

Re: MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_B

Post by dhylands » Fri Aug 17, 2018 4:34 pm

For the pyboard, the heap is aligned to 4 byte boundary, and the blocks are 16-bytes each. This causes all allocated objects to automatically have 4-byte alignment.

In C, any structure which contains a pointer is also pointer-size aligned. Since all python objects declared in C have an mp_obj_type_t object as their first member and it contains a pointer, this ensures that all C declared python objects have 4-byte alignment.

The stack is also setup to have 4-byte alignment.

Post Reply