From d1de99d8c9c36c976e583f8c9e668b9338566574 Mon Sep 17 00:00:00 2001 From: Virgil Dupras Date: Sat, 8 Aug 2020 08:08:05 -0400 Subject: [PATCH] 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. --- blk/076 | 17 +++++++++++------ blk/284 | 1 - blk/286 | 16 ++++++++-------- blk/287 | 2 +- 4 files changed, 20 insertions(+), 16 deletions(-) diff --git a/blk/076 b/blk/076 index a662b31..f6d0098 100644 --- a/blk/076 +++ b/blk/076 @@ -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. diff --git a/blk/284 b/blk/284 index 6e54c41..a9c4ca1 100644 --- a/blk/284 +++ b/blk/284 @@ -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 diff --git a/blk/286 b/blk/286 index 72cc6e4..60e8d3e 100644 --- a/blk/286 +++ b/blk/286 @@ -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 ) diff --git a/blk/287 b/blk/287 index f372223..3ff378d 100644 --- a/blk/287 +++ b/blk/287 @@ -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. )