More about stack effects
Wednesday, August 16, 2006
The see
word shows the stack effect now. Also – this patch isn’t
pushed yet – but HELP:
syntax is now different. Before when
documenting words, you would write:
HELP: my-word "( a b -- c )" { $values { "a" "..." } { "b" "..." } } { $description "Foo" }
Now, the new style is either:
HELP: my-word { $values ...
or:
HELP: my-word ( a b -- c ) { $values ...
That is, the stack effect comment is no longer given as a string, but
written out literally, and it is optional. If no stack effect comment is
specified in the help, the stack effect declared for the colon
definition is used.
One thing about stack effect declarations I forgot to mention in my
previous post is that if a word unconditionally throws an error, you
need to declare this for the word to compile:
: foo-error ( -- * ) "Foo!!!" throw ;
If the output values list consists of a single *
, then in fact the
word can be compiled in a context where any number of outputs are
expected; since it throws an error and execution does not continue past
throw
, the stack height is irrelevant by the time control flow reaches
the word.
You cannot lie to the compiler, at least in a way which breaks stack effect inference and the optimizer. If you specify an incorrect stack effect for a recursive word, inference will fail with an unbalanced branches error. For non-recursive words, a stack effect check is performed at the end to ensure the inferred effect matches the declaration.
What you can do is declare an effect which is too deep:
: foobar ( a b c d -- a b c ) + ;
This word claims to touch 4 stack elements but it only touches 2.
However the change in stack height for both the declared effect and the
inferred effect is the same, and the word does not touch any values it
does not declare, so the compiler lets this one slide. All it does is
reduce potential for optimization in places where the word is called,
since certain values will be assumed to have changed when in fact they
have not.
Factor is still evolving quickly. However it feels like the core of the language is slowly coming together in a way I can be truly happy with, and this was true even before the big cons cell removal/first class quotations change in 0.83. I’ve mostly been concentrating on the compiler and UI lately and this is where most of the work remains.