]> git.sur5r.net Git - cc65/blob - libsrc/runtime/stkchk.s
Comment additions and changes.
[cc65] / libsrc / runtime / stkchk.s
1 ;
2 ; Ullrich von Bassewitz, 19.03.2001
3 ;
4 ; Stack checking code. These are actually two routines, one to check the C
5 ; stack, and the other one to check the 6502 hardware stack.
6 ; For performance reasons (to avoid having to pass a parameter), the compiler
7 ; calls the cstkchk routine *after* allocating space on the stack. So the
8 ; stackpointer may already be invalid if this routine is called. In addition
9 ; to that, pushs and pops that are needed for expression evaluation are not
10 ; checked (this would be way too much overhead). As a consequence we will
11 ; operate using a safety area at the stack bottom. Once the stack reaches this
12 ; safety area, we consider it an overflow, even if the stack is still inside
13 ; its' bounds.
14 ;
15
16         .export         stkchk, cstkchk
17         .constructor    initstkchk, 25
18         .import         __STACKSIZE__                   ; Linker defined
19         .import         pusha0, _exit
20         .importzp       sp
21
22         ; Use macros for better readability
23         .macpack        generic
24
25
26 ; ----------------------------------------------------------------------------
27 ; Initialization code. This is a constructor, so it is called on startup if
28 ; the linker has detected references to this module.
29
30 .segment        "INIT"
31
32 .proc   initstkchk
33
34         lda     sp
35         sta     initialsp
36         sub     #<__STACKSIZE__
37         sta     lowwater
38         lda     sp+1
39         sta     initialsp+1
40         sbc     #>__STACKSIZE__
41         add     #1                      ; Add 256 bytes safety area
42         sta     lowwater+1
43         rts
44
45 .endproc
46
47 ; ----------------------------------------------------------------------------
48 ; 6502 stack checking routine. Does not need to save any registers.
49 ; Safety zone for the hardware stack is 12 bytes.
50
51 .code
52
53 stkchk: tsx
54         cpx     #12
55         bcc     Fail            ; Jump on stack overflow
56         rts                     ; Return if ok
57
58 ; ----------------------------------------------------------------------------
59 ; C stack checking routine. Does not need to save any registers.
60
61 .code
62
63 cstkchk:
64
65 ; Check the high byte of the software stack
66
67 @L0:    lda     lowwater+1
68         cmp     sp+1
69         bcs     @L1
70         rts
71
72 ; Check low byte
73
74 @L1:    bne     CStackOverflow
75         lda     lowwater
76         cmp     sp
77         bcs     CStackOverflow
78 Done:   rts
79
80 ; We have a C stack overflow. Set the stack pointer to the initial value, so
81 ; we can continue without worrying about stack issues.
82
83 CStackOverflow:
84         lda     initialsp
85         sta     sp
86         lda     initialsp+1
87         sta     sp+1
88
89 ; Generic abort entry. We should output a diagnostic here, but this is
90 ; difficult, since we're operating at a lower level here.
91
92 Fail:   lda     #4
93         ldx     #0
94         jmp     _exit
95
96 ; ----------------------------------------------------------------------------
97 ; Data
98
99 .bss
100
101 ; Initial stack pointer value. Stack is reset to this in case of overflows to
102 ; allow program exit processing.
103 initialsp:      .word   0
104
105 ; Stack low water mark.
106 lowwater:       .word   0
107
108
109