pforth
pforth copied to clipboard
Inconsistent handling of stack-underflow
Different words handle stack-underflows differently. There is inconsistency between words, and also inconsistency within certain words, in that some words throw on some but not all underflow conditions.
A few examples:
-
.
detects when the stack is empty and throws - The basic binary arithmetic words (
+
,-
,*
,/
) throw if the stack is empty, but fail to throw when there's 1 value on the stack. The result of running one of these words when 1 value is on the stack, is an empty stack. -
CELLS
does not throw when the stack is empty (results in an empty stack) -
DUP
and2DUP
do not detect when the stack is empty, and push garbage values in that circumstance -
SWAP
does not detect if the stack is empty. If run when there is 1 value on the stack, the result is that the stack holds 1 value (of garbage). -
2SWAP
does something similar toSWAP
In most of these cases, it was considered too costly to do the stack checking on these words. Words like +
and *
are meant to be optimized. The .
prints so adding a stack check there was not too expensive.
When running the outer interpreter, I check at the end of the line. But not in the middle. I could add that.
2 3 drop drop / no error
2 drop drop 3 / does not throw error but should because of temporary underflow
@MaxBarraclough - Would it help to add a compiler flag so that the primitives in pf_inner.c did underflow checking? I could put some checks in the M_POP() and M_DROP() macros.
@MaxBarraclough - Would it help to add a compiler flag so that the primitives in pf_inner.c did underflow checking? I could put some checks in the M_POP() and M_DROP() macros.
That sounds like a great solution. Are you thinking of 2 options (something like CHECKED
/UNCHECKED
) or 3 options (something like FULLY_CHECKED
/PARTIALLY_CHECKED
/UNCHECKED
)?
If the performance penalty of full checking is significant, it might make good sense to default to partial checking, with the option of no checks at all for people looking for maximum performance.