[SOLVED]gc_helper_get_regs_and_sp(regs) - why is "regs" is never used

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

[SOLVED]gc_helper_get_regs_and_sp(regs) - why is "regs" is never used

Post by jickster » Sat Apr 14, 2018 11:54 pm

In all of the usages of

Code: Select all

mp_uint_t gc_helper_get_regs_and_sp(mp_uint_t *regs);
regs is never used. Ever.

This means that if the registers contain malloc'd memory, it won't be cleaned up.
Last edited by jickster on Mon Apr 16, 2018 7:01 pm, edited 1 time in total.

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

Re: gc_helper_get_regs_and_sp(regs) - why is "regs" is never used

Post by dhylands » Sun Apr 15, 2018 1:22 am

Can you point to a specific example?

I could only find 3 definitions of the function gc_helper_get_regs_and_sp (in the stm32, esp8266 and cc3200 ports) and those versions of gc_helper_get_regs_and_sp were all implemented in assembler and all used the regs argument.

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

Re: gc_helper_get_regs_and_sp(regs) - why is "regs" is never used

Post by jickster » Mon Apr 16, 2018 3:22 pm

dhylands wrote:
Sun Apr 15, 2018 1:22 am
Can you point to a specific example?

I could only find 3 definitions of the function gc_helper_get_regs_and_sp (in the stm32, esp8266 and cc3200 ports) and those versions of gc_helper_get_regs_and_sp were all implemented in assembler and all used the regs argument.
I mean in the caller code.

Here's cc3200 gccollect.c

Code: Select all

void gc_collect(void) {
    // start the GC
    gc_collect_start();

    // get the registers and the sp
    mp_uint_t regs[10];
    mp_uint_t sp = gc_helper_get_regs_and_sp(regs);

    // trace the stack, including the registers (since they live on the stack in this function)
    gc_collect_root((void**)sp, ((mp_uint_t)MP_STATE_THREAD(stack_top) - sp) / sizeof(uint32_t));

    // trace root pointers from any threads
    #if MICROPY_PY_THREAD
    mp_thread_gc_others();
    #endif

    // end the GC
    gc_collect_end();
}
"regs" is not used i.e. they're not checked for root pointers.

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

Re: gc_helper_get_regs_and_sp(regs) - why is "regs" is never used

Post by dhylands » Mon Apr 16, 2018 3:34 pm

They are actually checked as root pointers by the virtue that they're stored on the stack.

Right after the call to gc_helper_get_regs_and_sp, you'll see a call to gc_collect_root where it treats everything currently on the stack as root pointers.

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

Re: gc_helper_get_regs_and_sp(regs) - why is "regs" is never used

Post by jickster » Mon Apr 16, 2018 6:57 pm

dhylands wrote:
Mon Apr 16, 2018 3:34 pm
They are actually checked as root pointers by the virtue that they're stored on the stack.

Right after the call to gc_helper_get_regs_and_sp, you'll see a call to gc_collect_root where it treats everything currently on the stack as root pointers.
I totally agree the registers that are saved to the stack are checked.

But the values that are in the registers i.e. r1, r2, r3, ... are not checked according to that code.

The whole point of "returning" the array mp_uint_t regs[10]; is to get the values in the registers and treat them as root pointers and garbage collect them just like the stack is.

UPDATE: nevermind I finally got it. Those registers, by virtue of the "regs" array, ARE on the stack.

Post Reply