Page 1 of 1

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

Posted: Sat Apr 14, 2018 11:54 pm
by jickster
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.

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

Posted: Sun Apr 15, 2018 1:22 am
by dhylands
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.

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

Posted: Mon Apr 16, 2018 3:22 pm
by jickster
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.

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

Posted: Mon Apr 16, 2018 3:34 pm
by dhylands
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.

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

Posted: Mon Apr 16, 2018 6:57 pm
by jickster
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.