forth: check PS everywhere

It turns out we have to...
This commit is contained in:
Virgil Dupras 2020-03-17 14:56:08 -04:00
parent 6314c60ede
commit a40926d710
2 changed files with 42 additions and 12 deletions

View File

@ -18,7 +18,8 @@
; IP, but we also take care of increasing it my 2 before jumping ; IP, but we also take care of increasing it my 2 before jumping
next: next:
; Before we continue: are stacks within bounds? ; Before we continue: are stacks within bounds?
call chkPSRS call chkPS
call chkRS
ld de, (IP) ld de, (IP)
ld h, d ld h, d
ld l, e ld l, e
@ -158,6 +159,12 @@ abortUnknownWord:
.msg: .msg:
.db "unknown word", 0 .db "unknown word", 0
abortUnderflow:
ld hl, .msg
jr abortMsg
.msg:
.db "stack underflow", 0
.db "BYE" .db "BYE"
.fill 4 .fill 4
.dw ABORT .dw ABORT
@ -180,6 +187,7 @@ BYE:
EMIT: EMIT:
.dw nativeWord .dw nativeWord
pop hl pop hl
call chkPS
ld a, l ld a, l
call stdioPutC call stdioPutC
jp next jp next
@ -193,6 +201,7 @@ PSTORE:
.dw nativeWord .dw nativeWord
pop bc pop bc
pop hl pop hl
call chkPS
out (c), l out (c), l
jp next jp next
@ -204,6 +213,7 @@ PSTORE:
PFETCH: PFETCH:
.dw nativeWord .dw nativeWord
pop bc pop bc
call chkPS
ld h, 0 ld h, 0
in l, (c) in l, (c)
push hl push hl
@ -216,6 +226,7 @@ PFETCH:
EXECUTE: EXECUTE:
.dw nativeWord .dw nativeWord
pop iy ; is a wordref pop iy ; is a wordref
call chkPS
ld l, (iy) ld l, (iy)
ld h, (iy+1) ld h, (iy+1)
; HL points to code pointer ; HL points to code pointer
@ -354,6 +365,7 @@ LITN:
ld de, NUMBER ld de, NUMBER
call DEinHL call DEinHL
pop de ; number from stack pop de ; number from stack
call chkPS
call DEinHL call DEinHL
ld (HERE), hl ld (HERE), hl
jp next jp next
@ -485,6 +497,7 @@ STORE:
.dw nativeWord .dw nativeWord
pop iy pop iy
pop hl pop hl
call chkPS
ld (iy), l ld (iy), l
ld (iy+1), h ld (iy+1), h
jp next jp next
@ -498,6 +511,7 @@ CSTORE:
.dw nativeWord .dw nativeWord
pop hl pop hl
pop de pop de
call chkPS
ld (hl), e ld (hl), e
jp next jp next
@ -509,6 +523,7 @@ CSTORE:
FETCH: FETCH:
.dw nativeWord .dw nativeWord
pop hl pop hl
call chkPS
call intoHL call intoHL
push hl push hl
jp next jp next
@ -521,6 +536,7 @@ FETCH:
CFETCH: CFETCH:
.dw nativeWord .dw nativeWord
pop hl pop hl
call chkPS
ld l, (hl) ld l, (hl)
ld h, 0 ld h, 0
push hl push hl
@ -544,6 +560,7 @@ DROP:
SWAP: SWAP:
.dw nativeWord .dw nativeWord
pop hl pop hl
call chkPS
ex (sp), hl ex (sp), hl
push hl push hl
jp next jp next
@ -558,6 +575,7 @@ SWAP2:
pop de ; D pop de ; D
pop hl ; C pop hl ; C
pop bc ; B pop bc ; B
call chkPS
ex (sp), hl ; A in HL ex (sp), hl ; A in HL
push de ; D push de ; D
@ -573,6 +591,7 @@ SWAP2:
DUP: DUP:
.dw nativeWord .dw nativeWord
pop hl pop hl
call chkPS
push hl push hl
push hl push hl
jp next jp next
@ -586,6 +605,7 @@ DUP2:
.dw nativeWord .dw nativeWord
pop hl ; B pop hl ; B
pop de ; A pop de ; A
call chkPS
push de push de
push hl push hl
push de push de
@ -601,6 +621,7 @@ OVER:
.dw nativeWord .dw nativeWord
pop hl ; B pop hl ; B
pop de ; A pop de ; A
call chkPS
push de push de
push hl push hl
push de push de
@ -617,6 +638,7 @@ OVER2:
pop de ; C pop de ; C
pop bc ; B pop bc ; B
pop iy ; A pop iy ; A
call chkPS
push iy ; A push iy ; A
push bc ; B push bc ; B
push de ; C push de ; C
@ -632,6 +654,7 @@ OVER2:
P2R: P2R:
.dw nativeWord .dw nativeWord
pop hl pop hl
call chkPS
call pushRS call pushRS
jp next jp next
@ -687,6 +710,7 @@ PLUS:
.dw nativeWord .dw nativeWord
pop hl pop hl
pop de pop de
call chkPS
add hl, de add hl, de
push hl push hl
jp next jp next
@ -700,6 +724,7 @@ MINUS:
.dw nativeWord .dw nativeWord
pop de ; B pop de ; B
pop hl ; A pop hl ; A
call chkPS
or a ; reset carry or a ; reset carry
sbc hl, de sbc hl, de
push hl push hl
@ -714,6 +739,7 @@ MULT:
.dw nativeWord .dw nativeWord
pop de pop de
pop bc pop bc
call chkPS
call multDEBC call multDEBC
push hl push hl
jp next jp next
@ -727,6 +753,7 @@ DIVMOD:
.dw nativeWord .dw nativeWord
pop de pop de
pop hl pop hl
call chkPS
call divide call divide
push hl push hl
push bc push bc
@ -741,6 +768,7 @@ SCMP:
.dw nativeWord .dw nativeWord
pop de pop de
pop hl pop hl
call chkPS
call strcmp call strcmp
call flagsToBC call flagsToBC
push bc push bc
@ -755,6 +783,7 @@ CMP:
.dw nativeWord .dw nativeWord
pop hl pop hl
pop de pop de
call chkPS
or a ; clear carry or a ; clear carry
sbc hl, de sbc hl, de
call flagsToBC call flagsToBC
@ -768,6 +797,7 @@ CMP:
CSKIP: CSKIP:
.dw nativeWord .dw nativeWord
pop hl pop hl
call chkPS
ld a, h ld a, h
or l or l
jp z, next ; False, do nothing. jp z, next ; False, do nothing.

View File

@ -37,25 +37,25 @@ skipRS:
ret ret
; Verifies that SP and RS are within bounds. If it's not, call ABORT ; Verifies that SP and RS are within bounds. If it's not, call ABORT
chkPSRS: chkRS:
push ix \ pop hl push ix \ pop hl
push de ; --> lvl 1 push de ; --> lvl 1
ld de, RS_ADDR ld de, RS_ADDR
or a ; clear carry or a ; clear carry
sbc hl, de sbc hl, de
pop de ; <-- lvl 1 pop de ; <-- lvl 1
jr c, .underflow jp c, abortUnderflow
ret
chkPS:
push hl
ld hl, (INITIAL_SP) ld hl, (INITIAL_SP)
; We have the return address for this very call on the stack. Let's ; We have the return address for this very call on the stack and
; compensate ; protected registers. Let's compensate
dec hl \ dec hl
dec hl \ dec hl dec hl \ dec hl
or a ; clear carry or a ; clear carry
sbc hl, sp sbc hl, sp
pop hl
ret nc ; (INITIAL_SP) >= SP? good ret nc ; (INITIAL_SP) >= SP? good
.underflow: jp abortUnderflow
; underflow
ld hl, .msg
call printstr
jp abort
.msg:
.db "stack underflow", 0