z80: implement stack overflow error condition

Also, remove underflow checks from next: it's taxing and useless
because underflow check already takes place in each relevant native
word.
This commit is contained in:
Virgil Dupras 2020-08-08 08:08:05 -04:00
parent 78d4d15fcf
commit d1de99d8c9
4 changed files with 20 additions and 16 deletions

17
blk/076
View File

@ -1,6 +1,11 @@
STACK OVERFLOW PROTECTION: To avoid having to check for stack
underflow after each pop operation (which can end up being
prohibitive in terms of costs), PS_ADDR should be set to
at least 6 bytes before its actual limit. 6 bytes because we
seldom have words requiring more than 3 items from the stack.
Then, at each "exit" call we check for stack underflow.
Stack underflow and overflow: In each native word involving
PSP popping, we check whether the stack is big enough. If it's
not we go in "uflw" (underflow) error condition, then abort.
We don't check RSP for underflow because the cost of the check
is significant and its usefulness is dubious: if RSP isn't
tightly in control, we're screwed anyways, and that, well
before we reach underflow.
Overflow condition happen when RSP and PSP meet somewhere in
the middle. That check is made at each "next" call.

View File

@ -1,5 +1,4 @@
PC ORG @ 1 + ! ( main )
( STACK OVERFLOW PROTECTION: See B76 )
SP PS_ADDR LDdn, IX RS_ADDR LDdn,
( LATEST is a label to the latest entry of the dict. It is
written at offset 0x08 by the process or person building

16
blk/286
View File

@ -2,14 +2,14 @@ lblnext BSET PC ORG @ 0x1b + ! ( next )
( This routine is jumped to at the end of every word. In it,
we jump to current IP, but we also take care of increasing
it by 2 before jumping. )
( Before we continue: are stacks within bounds? )
( PS ) HL PS_ADDR LDdn,
SP SUBHLd,
JRC, lblofl BWR ( abortUnderflow-B285 )
( RS ) IX PUSH, HL POP,
DE RS_ADDR LDdn,
DE SUBHLd,
JRC, lblofl BWR ( IX < RS_ADDR? abortUnderflow-B285 )
( Before we continue: are we overflowing? )
IX PUSH, EX(SP)HL, ( do EX to count the IX push in SP )
SP SUBHLd, HL POP,
IFNC, ( SP <= IX? overflow )
SP PS_ADDR LDdn, IX RS_ADDR LDdn,
DE BIN( @ 0x13 ( oflw ) + LDdd(n),
JR, L3 FWR ( execute, B287 )
THEN,
LDA(BC), E A LDrr, BC INCd,
LDA(BC), D A LDrr, BC INCd,
( continue to execute )

View File

@ -1,4 +1,4 @@
lblexec BSET L1 FSET ( B284 ) L2 FSET ( B285 )
lblexec BSET L1 FSET ( B284 ) L2 FSET ( B285 ) L3 FSET ( B286 )
( DE -> wordref )
( When we have a BIN( offset, we need to adjust stable
ABI offsets. )