From bfa29f17dac5450e26fa9ae2b2eaeeb19dedc546 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Fri, 25 Sep 2020 15:59:38 +0200 Subject: [PATCH] runtime: move/refactor some GC-related code Instead of putting tinygo_scanCurrentStack in scheduler_*.S files, put them in dedicated files. The function tinygo_scanCurrentStack has nothing to do with scheduling and so doesn't belong there. Additionally, while scheduling code is made specific for the Cortex-M, the tinygo_scanCurrentStack is generic to all ARM targets so this move removes some duplication there. Specifically: * tinygo_scanCurrentStack is moved out of scheduler_cortexm.S as it isn't really part of the scheduler. It is now gc_arm.S. * Same for the AVR target. * Same for the RISCV target. * scheduler_gba.S is removed, using gc_arm.S instead as it only contains tinygo_scanCurrentStack. --- src/runtime/{scheduler_gba.S => gc_arm.S} | 14 +++++ src/runtime/gc_avr.S | 52 ++++++++++++++++++ .../{scheduler_tinygoriscv.S => gc_riscv.S} | 0 src/runtime/scheduler_avr.S | 53 ------------------- src/runtime/scheduler_cortexm.S | 31 ----------- targets/avr.json | 1 + targets/cortex-m.json | 1 + targets/gameboy-advance.json | 2 +- targets/riscv.json | 2 +- 9 files changed, 70 insertions(+), 86 deletions(-) rename src/runtime/{scheduler_gba.S => gc_arm.S} (54%) create mode 100644 src/runtime/gc_avr.S rename src/runtime/{scheduler_tinygoriscv.S => gc_riscv.S} (100%) diff --git a/src/runtime/scheduler_gba.S b/src/runtime/gc_arm.S similarity index 54% rename from src/runtime/scheduler_gba.S rename to src/runtime/gc_arm.S index 6eea9826..5c6c58f1 100644 --- a/src/runtime/scheduler_gba.S +++ b/src/runtime/gc_arm.S @@ -1,14 +1,25 @@ +// Only generate .debug_frame, don't generate .eh_frame. +.cfi_sections .debug_frame + .section .text.tinygo_scanCurrentStack .global tinygo_scanCurrentStack .type tinygo_scanCurrentStack, %function tinygo_scanCurrentStack: + .cfi_startproc // Save callee-saved registers onto the stack. + #if defined(__thumb2__) + push {r4-r11, lr} + .cfi_def_cfa_offset 9*4 + #else mov r0, r8 mov r1, r9 mov r2, r10 mov r3, r11 push {r0-r3, lr} + .cfi_def_cfa_offset 5*4 push {r4-r7} + .cfi_def_cfa_offset 4*4 + #endif // Scan the stack. mov r0, sp @@ -16,4 +27,7 @@ tinygo_scanCurrentStack: // Restore stack state and return. add sp, #32 + .cfi_def_cfa_offset 1*4 pop {pc} + .cfi_endproc +.size tinygo_scanCurrentStack, .-tinygo_scanCurrentStack diff --git a/src/runtime/gc_avr.S b/src/runtime/gc_avr.S new file mode 100644 index 00000000..d0fcd3f5 --- /dev/null +++ b/src/runtime/gc_avr.S @@ -0,0 +1,52 @@ +.section .text.tinygo_scanCurrentStack +.global tinygo_scanCurrentStack +.type tinygo_scanCurrentStack, %function +tinygo_scanCurrentStack: + // Save callee-saved registers. + push r29 // Y + push r28 // Y + push r17 + push r16 + push r15 + push r14 + push r13 + push r12 + push r11 + push r10 + push r9 + push r8 + push r7 + push r6 + push r5 + push r4 + push r3 + push r2 + + // Scan the stack. + in r24, 0x3d; SPL + in r25, 0x3e; SPH +#if __AVR_ARCH__ == 2 || __AVR_ARCH__ == 25 + rcall tinygo_scanstack +#else + call tinygo_scanstack +#endif + + // Restore callee-saved registers. + pop r2 + pop r3 + pop r4 + pop r5 + pop r6 + pop r7 + pop r8 + pop r9 + pop r10 + pop r11 + pop r12 + pop r13 + pop r14 + pop r15 + pop r16 + pop r17 + pop r28 // Y + pop r29 // Y diff --git a/src/runtime/scheduler_tinygoriscv.S b/src/runtime/gc_riscv.S similarity index 100% rename from src/runtime/scheduler_tinygoriscv.S rename to src/runtime/gc_riscv.S diff --git a/src/runtime/scheduler_avr.S b/src/runtime/scheduler_avr.S index 4e65654d..83daf2e2 100644 --- a/src/runtime/scheduler_avr.S +++ b/src/runtime/scheduler_avr.S @@ -187,56 +187,3 @@ tinygo_switchToScheduler: // Return into the scheduler, as if tinygo_switchToTask was a regular call. ret - -.section .text.tinygo_scanCurrentStack -.global tinygo_scanCurrentStack -.type tinygo_scanCurrentStack, %function -tinygo_scanCurrentStack: - // Save callee-saved registers. - push r29 // Y - push r28 // Y - push r17 - push r16 - push r15 - push r14 - push r13 - push r12 - push r11 - push r10 - push r9 - push r8 - push r7 - push r6 - push r5 - push r4 - push r3 - push r2 - - // Scan the stack. - in r24, 0x3d; SPL - in r25, 0x3e; SPH -#if __AVR_ARCH__ == 2 || __AVR_ARCH__ == 25 - rcall tinygo_scanstack -#else - call tinygo_scanstack -#endif - - // Restore callee-saved registers. - pop r2 - pop r3 - pop r4 - pop r5 - pop r6 - pop r7 - pop r8 - pop r9 - pop r10 - pop r11 - pop r12 - pop r13 - pop r14 - pop r15 - pop r16 - pop r17 - pop r28 // Y - pop r29 // Y diff --git a/src/runtime/scheduler_cortexm.S b/src/runtime/scheduler_cortexm.S index 2053deca..fddddfde 100644 --- a/src/runtime/scheduler_cortexm.S +++ b/src/runtime/scheduler_cortexm.S @@ -136,34 +136,3 @@ tinygo_swapTask: #endif .cfi_endproc .size tinygo_swapTask, .-tinygo_swapTask - -.section .text.tinygo_scanCurrentStack -.global tinygo_scanCurrentStack -.type tinygo_scanCurrentStack, %function -tinygo_scanCurrentStack: - .cfi_startproc - // Save callee-saved registers onto the stack. - #if defined(__thumb2__) - push {r4-r11, lr} - .cfi_def_cfa_offset 9*4 - #else - mov r0, r8 - mov r1, r9 - mov r2, r10 - mov r3, r11 - push {r0-r3, lr} - .cfi_def_cfa_offset 5*4 - push {r4-r7} - .cfi_def_cfa_offset 4*4 - #endif - - // Scan the stack. - mov r0, sp - bl tinygo_scanstack - - // Restore stack state and return. - add sp, #32 - .cfi_def_cfa_offset 1*4 - pop {pc} - .cfi_endproc -.size tinygo_scanCurrentStack, .-tinygo_scanCurrentStack diff --git a/targets/avr.json b/targets/avr.json index 4aafbea9..75f3e175 100644 --- a/targets/avr.json +++ b/targets/avr.json @@ -13,6 +13,7 @@ "-Wl,--gc-sections" ], "extra-files": [ + "src/runtime/gc_avr.S", "src/runtime/scheduler_avr.S" ] } diff --git a/targets/cortex-m.json b/targets/cortex-m.json index 958d51ee..15be7d2d 100644 --- a/targets/cortex-m.json +++ b/targets/cortex-m.json @@ -25,6 +25,7 @@ ], "extra-files": [ "src/device/arm/cortexm.s", + "src/runtime/gc_arm.S", "src/runtime/scheduler_cortexm.S" ], "gdb": "gdb-multiarch" diff --git a/targets/gameboy-advance.json b/targets/gameboy-advance.json index 735d9229..0675622c 100644 --- a/targets/gameboy-advance.json +++ b/targets/gameboy-advance.json @@ -25,7 +25,7 @@ "linkerscript": "targets/gameboy-advance.ld", "extra-files": [ "targets/gameboy-advance.s", - "src/runtime/scheduler_gba.S" + "src/runtime/gc_arm.S" ], "gdb": "gdb-multiarch", "emulator": ["mgba", "-3"] diff --git a/targets/riscv.json b/targets/riscv.json index da40416b..3b727992 100644 --- a/targets/riscv.json +++ b/targets/riscv.json @@ -18,7 +18,7 @@ ], "extra-files": [ "src/device/riscv/start.S", - "src/runtime/scheduler_tinygoriscv.S", + "src/runtime/gc_riscv.S", "src/device/riscv/handleinterrupt.S" ], "gdb": "riscv64-unknown-elf-gdb"