From ee1b7e606da9ab4bdc01b22777176c3192b15e50 Mon Sep 17 00:00:00 2001 From: Stephen Heumann Date: Thu, 23 Jan 2020 18:43:20 -0600 Subject: [PATCH] Implement quick_exit() and at_quick_exit() from C11. Also, make all the exit functions quit via RTL if #pragma rtl was used. This fixes #19. --- cc.asm | 70 +++++++++++++++++++++++++++++++++++++++++++++++++ stdlib.asm | 76 ++++++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 138 insertions(+), 8 deletions(-) diff --git a/cc.asm b/cc.asm index 74c490f..09e9bf6 100644 --- a/cc.asm +++ b/cc.asm @@ -184,6 +184,8 @@ TAB equ 9 TAB key code stz ~ExitList no exit routines, yet stz ~ExitList+2 + stz ~QuickExitList + stz ~QuickExitList+2 case on jsl ~InitIO reset standard I/O case off @@ -359,6 +361,10 @@ start ds 2 start of the command line string stz ~ExitList no exit routines, yet stz ~ExitList+2 + stz ~QuickExitList + stz ~QuickExitList+2 + lda #~RTL set up so exit(), etc. call ~RTL + sta ~C_Quit+1 stz targv argv[0] = NULL stz targv+2 @@ -439,14 +445,78 @@ lb4 pld return rts end +**************************************************************** +* +* ~QuickExit - call quick exit routines +* +* Inputs: +* ~QuickExitList - list of quick exit routines +* +**************************************************************** +* +~QuickExit start +ptr equ 3 pointer to exit routines +; +; Set up our stack frame +; + phb + phk + plb + ph4 ~QuickExitList set up our stack frame + phd + tsc + tcd +; +; Call the quick exit functions +; +lb1 lda ptr if the pointer is non-nil then + ora ptr+2 + beq lb3 + pea +(lb2-1)|-8 call the function + pea +(lb2-1)|8 + phb + pla + ldy #5 + lda [ptr],Y + pha + dey + dey + lda [ptr],Y + pha + phb + pla + rtl +lb2 ldy #2 dereference the pointer + lda [ptr],Y + tax + lda [ptr] + sta ptr + stx ptr+2 + bra lb1 +; +; return +; +lb3 pld return + pla + pla + plb + rts + end + **************************************************************** * * ~ExitList - list of exit routines +* ~QuickExitList - list of quick exit routines +* ~C_Quit - call to quit (may be changed to call ~RTL) * **************************************************************** * ~ExitList start ds 4 +~QuickExitList entry + ds 4 +~C_Quit entry + jmp ~QUIT end **************************************************************** diff --git a/stdlib.asm b/stdlib.asm index d3a5e26..4f18eb1 100644 --- a/stdlib.asm +++ b/stdlib.asm @@ -37,7 +37,7 @@ abort start ph2 #SIGABRT jsl raise lda #-1 - jmp ~QUIT + jmp ~C_QUIT end **************************************************************** @@ -159,6 +159,60 @@ rval equ 5 return value lb1 creturn 2:rval end +**************************************************************** +* +* int at_quick_exit(func) +* void (*func)(); +* +* This function is used to build a list of functions that will +* be called as part of the quick exit processing. +* +* Inputs: +* func - address of the function to call on quick exit +* +* Outputs: +* Returns 0 if successful, -1 if not. +* +**************************************************************** +* +at_quick_exit start +ptr equ 1 work pointer +rval equ 5 return value + + csubroutine (4:func),6 + + lda #-1 assume we will fail + sta rval assume we will fail + dec4 func we need the addr-1, not the addr + ph4 #8 get space for the record + jsl malloc + stx ptr+2 + sta ptr + ora ptr+2 quit now if we failed + beq lb1 + ldy #2 place the record in the exit list + lda >~QUICKEXITLIST + sta [ptr] + lda >~QUICKEXITLIST+2 + sta [ptr],Y + lda ptr + sta >~QUICKEXITLIST + lda ptr+2 + sta >~QUICKEXITLIST+2 + iny place the function address in the record + iny + lda func + sta [ptr],Y + iny + iny + lda func+2 + sta [ptr],Y + inc rval success... + +lb1 creturn 2:rval + end + + **************************************************************** * * atof - convert a string to a float @@ -331,14 +385,13 @@ div_t ds 4 **************************************************************** * -* void exit(status) -* int status; +* void exit(int status); * -* void _exit(status) -* int status; +* void _exit(int status); * -* void _Exit(status) -* int status; +* void _Exit(int status); +* +* void quick_exit(int status); * * Stop the program. Exit cleans up, _exit does not. Status * is the status returned to the shell. @@ -354,7 +407,14 @@ exit start _exit entry _Exit entry lda 4,S - jmp ~QUIT + jmp ~C_QUIT + end + +quick_exit start + + jsr ~QUICKEXIT + lda 4,S + jmp ~C_QUIT end ****************************************************************