pvvx / THB2

Custom firmware for Tuya devices on the PHY622x2 chipset
Other
92 stars 13 forks source link

Небольшая оптимизация функции wakeupProcess1() #4

Closed froloffw7 closed 5 months ago

froloffw7 commented 5 months ago

Function wakeupProcess1() не требует сохранения/восстановления регистров (prolog/epilog) и не возвращается в точку вызова.
Для сравнения, код, сгенерированный до изменений:

Пролог

1fff2ca4:   b5f7        push    {r0, r1, r2, r4, r5, r6, r7, lr}
    __set_MSP(pGlobal_config[INITIAL_STACK_PTR]);
1fff2ca6:   4e6b        ldr r6, [pc, #428]  @ (1fff2e54 <wakeupProcess1+0x1b0>)
{
1fff2ca8:   af00        add r7, sp, #0
    __set_MSP(pGlobal_config[INITIAL_STACK_PTR]);
1fff2caa:   6833        ldr r3, [r6, #0]
1fff2cac:   3388        adds    r3, #136    @ 0x88
    \param [in]    topOfMainStack  Main Stack Pointer value to set
*/
__attribute__( ( always_inline ) ) __STATIC_INLINE void __set_MSP(uint32_t topOfMainStack)
{
    __ASM volatile ("MSR msp, %0\n" : : "r" (topOfMainStack) : "sp");
1fff2cae:   681b        ldr r3, [r3, #0]
1fff2cb0:   f383 8808   msr MSP, r3
...

Эпилог

    osal_start_system();
1fff2e26:   f001 f8a7   bl  1fff3f78 <__osal_start_system_veneer>
}
1fff2e2a:   46bd        mov sp, r7
1fff2e2c:   bdf7        pop {r0, r1, r2, r4, r5, r6, r7, pc}
...

и после

Пролог

1fff2ca4 <wakeupProcess1>:
    __set_MSP(pGlobal_config[INITIAL_STACK_PTR]);
1fff2ca4:   4e66        ldr r6, [pc, #408]  @ (1fff2e40 <wakeupProcess1+0x19c>)
1fff2ca6:   6833        ldr r3, [r6, #0]
1fff2ca8:   3388        adds    r3, #136    @ 0x88
    \param [in]    topOfMainStack  Main Stack Pointer value to set
*/
__attribute__( ( always_inline ) ) __STATIC_INLINE void __set_MSP(uint32_t topOfMainStack)
{
    __ASM volatile ("MSR msp, %0\n" : : "r" (topOfMainStack));
1fff2caa:   681b        ldr r3, [r3, #0]
1fff2cac:   f383 8808   msr MSP, r3
...

Эпилог

    osal_start_system();
1fff2e18:   f001 f8a2   bl  1fff3f60 <__osal_start_system_veneer>
...

Это также избавляет от предупреждения от gcc:

./SDK/misc/CMSIS/include/core_cmFunc.h:463:5: note: in expansion of macro '__ASM'
./SDK/misc/CMSIS/include/core_cm0.h:90:26: note: the value of the stack pointer after an 'asm' statement must
 be the same as it was before the statement