Clarification on stack usage in standards doc

The Standards Document 1.1 says this about the stack:


The absolute minimum standard for stack size is defined as:

let the ‘usage’ of a routine call be 4 plus the number of local variables it has. During a game the total of the usages for each routine in the recursive chain of routines being called, plus the game’s own stack usage, can be assumed to never reach 1024.

I find the usage of “the stack” in this section of the doc to be a little ambiguous. I’m assuming it’s referring to what Quetzal would term the per-call “evaluation stack”?

I’m not clear on how the “usage” of 4 (words?) is arrived at. It matches what’s set aside in Quetzal, but I can’t find a definition in the standard stating what it intends those 4 words to be used for. Is it meant to be left for the implementer to decide?

The stack is described in the preface:

The stack is a second bank of memory, quite separate from the main one, which has variable size: initially it is empty. From time to time values are added to, or taken from, the top of the stack. As well as being used to keep return details, the stack is also used to store local variables (values needed only by a particular routine) and, for short periods only, the partial results of calculations.

6.3.3 is referring to the total stack space. Yes, it’s in words.

The Quetzal doc describes two stacks (function call frames and the evaluation stack), but notes that it’s possible to store these in a single contiguous array. When you’re writing out a Quetzal file, you write them as a single array, regardless of how the interpreter handles them internally.

Thanks @zarf.

I’m still not 100% clear on how the “let the ‘usage’ of a routine call be 4” statement is arrived at.

Does the standard specify what those 4 words are used for?

I think it says that if you implement the stack in such a way that it uses four words to store the necessary state information for a stack frame, not counting the local variables (or pushed data, I presume), you can expect a stack size of 1024 words to always be enough.

If you choose a more wasteful scheme, using say six words per stack frame, you should preferrably have a bigger stack.

Yeah. I’m sure that “four words” was arrived at by looking at Zip and Frotz and observing that they both stored four words’ worth of info per stack frame (plus local variables and the eval stack), and that was sufficient.

There are many different ways to implement the z-machine stack but as zarf pointed out: 8 bytes + locals + eval is sufficient and commonly used.

I use something different, but it boils down to the same end result.

With regard to the 1024 words threshold mentioned in the standard: Someone correct me if I’m wrong, but I believe there have been stories that won’t fit in that…

In fact, because of the variety of possible implementations, it is maddeningly difficult to pin down a minimum stack size with any precision, e.g. number of frames, number of locals, number of evals may all be combined or separate depending on design.