From 19a2110ba2354ceae476e885c430518279fc78bd Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Thu, 4 Jan 2024 00:30:20 +0100 Subject: [PATCH] fix exit() now actually correctly setting the return code in A also, moved some cleanup stuff such as CLRCHN from exit() to the cleanup routine that is always called. finally, also call the cleanup routine when %option no_sysinit is used --- .../codegen/cpu6502/ProgramAndVarsGen.kt | 11 ++------- compiler/res/prog8lib/atari/syslib.p8 | 4 ++-- compiler/res/prog8lib/c128/syslib.p8 | 17 +++++++------ compiler/res/prog8lib/c64/syslib.p8 | 17 +++++++------ compiler/res/prog8lib/cx16/syslib.p8 | 7 ++++-- compiler/res/prog8lib/pet32/syslib.p8 | 7 ++++-- docs/source/todo.rst | 2 +- examples/test.p8 | 24 +++++++++++-------- 8 files changed, 49 insertions(+), 40 deletions(-) diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt index 109dc0645..770906daf 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/ProgramAndVarsGen.kt @@ -133,7 +133,6 @@ internal class ProgramAndVarsGen( pha""") } - // make sure that on the cx16 and c64, basic rom is banked in again when we exit the program when(compTarget.name) { "cx16" -> { if(options.floats) @@ -143,17 +142,11 @@ internal class ProgramAndVarsGen( } "c64" -> { asmgen.out(" jsr p8b_main.p8s_start | lda #31 | sta $01") - if(!options.noSysInit) - asmgen.out(" jmp sys.cleanup_at_exit") - else - asmgen.out(" rts") + asmgen.out(" jmp sys.cleanup_at_exit") } "c128" -> { asmgen.out(" jsr p8b_main.p8s_start | lda #0 | sta ${"$"}ff00") - if(!options.noSysInit) - asmgen.out(" jmp sys.cleanup_at_exit") - else - asmgen.out(" rts") + asmgen.out(" jmp sys.cleanup_at_exit") } else -> asmgen.jmp("p8b_main.p8s_start") } diff --git a/compiler/res/prog8lib/atari/syslib.p8 b/compiler/res/prog8lib/atari/syslib.p8 index 9e9b3b899..5a52c5898 100644 --- a/compiler/res/prog8lib/atari/syslib.p8 +++ b/compiler/res/prog8lib/atari/syslib.p8 @@ -252,9 +252,9 @@ save_SCRATCH_ZPWORD2 .word 0 }} } - inline asmsub exit(ubyte returnvalue @A) { + asmsub exit(ubyte returnvalue @A) { ; -- immediately exit the program with a return code in the A register - ; TODO + ; TODO where to store A as exit code? %asm {{ ldx prog8_lib.orig_stackpointer txs diff --git a/compiler/res/prog8lib/c128/syslib.p8 b/compiler/res/prog8lib/c128/syslib.p8 index 24e2b92c4..19fa3e750 100644 --- a/compiler/res/prog8lib/c128/syslib.p8 +++ b/compiler/res/prog8lib/c128/syslib.p8 @@ -373,7 +373,13 @@ asmsub init_system_phase2() { asmsub cleanup_at_exit() { ; executed when the main subroutine does rts %asm {{ - jmp sys.enable_runstop_and_charsetswitch + lda #0 + sta $ff00 ; default bank 15 + jsr cbm.CLRCHN ; reset i/o channels + jsr enable_runstop_and_charsetswitch +_exitcode = *+1 + lda #0 ; exit code possibly modified in exit() + rts }} } @@ -754,16 +760,13 @@ _longcopy }} } - inline asmsub exit(ubyte returnvalue @A) { + asmsub exit(ubyte returnvalue @A) { ; -- immediately exit the program with a return code in the A register %asm {{ - lda #0 - sta $ff00 ; default bank 15 - jsr cbm.CLRCHN ; reset i/o channels - jsr sys.enable_runstop_and_charsetswitch + sta cleanup_at_exit._exitcode ldx prog8_lib.orig_stackpointer txs - rts ; return to original caller + jmp cleanup_at_exit }} } diff --git a/compiler/res/prog8lib/c64/syslib.p8 b/compiler/res/prog8lib/c64/syslib.p8 index 8ca06d5bd..f471ef361 100644 --- a/compiler/res/prog8lib/c64/syslib.p8 +++ b/compiler/res/prog8lib/c64/syslib.p8 @@ -372,7 +372,13 @@ asmsub init_system_phase2() { asmsub cleanup_at_exit() { ; executed when the main subroutine does rts %asm {{ - jmp sys.enable_runstop_and_charsetswitch + lda #31 + sta $01 ; bank the kernal in + jsr cbm.CLRCHN ; reset i/o channels + jsr enable_runstop_and_charsetswitch +_exitcode = *+1 + lda #0 ; exit code possibly modified in exit() + rts }} } @@ -752,16 +758,13 @@ _longcopy }} } - inline asmsub exit(ubyte returnvalue @A) { + asmsub exit(ubyte returnvalue @A) { ; -- immediately exit the program with a return code in the A register %asm {{ - lda #31 - sta $01 ; bank the kernal in - jsr cbm.CLRCHN ; reset i/o channels - jsr sys.enable_runstop_and_charsetswitch + sta cleanup_at_exit._exitcode ldx prog8_lib.orig_stackpointer txs - rts ; return to original caller + jmp cleanup_at_exit }} } diff --git a/compiler/res/prog8lib/cx16/syslib.p8 b/compiler/res/prog8lib/cx16/syslib.p8 index 97e121568..dc37c3b53 100644 --- a/compiler/res/prog8lib/cx16/syslib.p8 +++ b/compiler/res/prog8lib/cx16/syslib.p8 @@ -1239,6 +1239,9 @@ asmsub cleanup_at_exit() { lda #4 sta $01 ; rom bank 4 (basic) stz $2d ; hack to reset machine code monitor bank to 0 + jsr cbm.CLRCHN ; reset i/o channels +_exitcode = *+1 + lda #0 ; exit code possibly modified in exit() rts }} } @@ -1602,10 +1605,10 @@ save_SCRATCH_ZPWORD2 .word 0 asmsub exit(ubyte returnvalue @A) { ; -- immediately exit the program with a return code in the A register %asm {{ - jsr cbm.CLRCHN ; reset i/o channels + sta cleanup_at_exit._exitcode ldx prog8_lib.orig_stackpointer txs - rts ; return to original caller + jmp cleanup_at_exit }} } diff --git a/compiler/res/prog8lib/pet32/syslib.p8 b/compiler/res/prog8lib/pet32/syslib.p8 index d4a93fb06..26f327a43 100644 --- a/compiler/res/prog8lib/pet32/syslib.p8 +++ b/compiler/res/prog8lib/pet32/syslib.p8 @@ -110,6 +110,8 @@ asmsub init_system_phase2() { asmsub cleanup_at_exit() { ; executed when the main subroutine does rts %asm {{ +_exitcode = *+1 + lda #0 ; exit code possibly modified in exit() rts }} } @@ -345,12 +347,13 @@ save_SCRATCH_ZPWORD2 .word 0 }} } - inline asmsub exit(ubyte returnvalue @A) { + asmsub exit(ubyte returnvalue @A) { ; -- immediately exit the program with a return code in the A register %asm {{ + sta cleanup_at_exit._exitcode ldx prog8_lib.orig_stackpointer txs - rts ; return to original caller + jmp cleanup_at_exit }} } diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 3e1469021..b9020fe3e 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -1,7 +1,7 @@ TODO ==== -keep Bool alive longer until codegen? (don't replace by UBYTE so quickly?) +[on branch 'booleans']: keep Bool alive longer until codegen? (don't replace by UBYTE so quickly?) ... diff --git a/examples/test.p8 b/examples/test.p8 index 4f0b24036..1bedafc00 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,17 +1,21 @@ -%import textio %zeropage basicsafe +%option no_sysinit main { sub start() { + sub1() + } - cx16.r0 = 500 - if cx16.r0 in 127 to 5555 - cx16.r0++ - - - cx16.r0 = 50 - if cx16.r0 in 5555 downto 127 - cx16.r0++ - + sub sub1() { + cx16.r0++ + sub2() + } + sub sub2() { + cx16.r0++ + sub3() + } + sub sub3() { + cx16.r0++ + sys.exit(42) } }