Next: , Previous: Structure Naming Convention, Up: Structures

5.22.4 Structure Implementation

The central idea in the implementation is to pass the data about the structure being built on the stack, not in some global variable. Everything else falls into place naturally once this design decision is made.

The type description on the stack is of the form align size. Keeping the size on the top-of-stack makes dealing with arrays very simple.

field is a defining word that uses Create and DOES>. The body of the field contains the offset of the field, and the normal DOES> action is simply:

     @ +

i.e., add the offset to the address, giving the stack effect addr1 – addr2 for a field.

This simple structure is slightly complicated by the optimization for fields with offset 0, which requires a different DOES>-part (because we cannot rely on there being something on the stack if such a field is invoked during compilation). Therefore, we put the different DOES>-parts in separate words, and decide which one to invoke based on the offset. For a zero offset, the field is basically a noop; it is immediate, and therefore no code is generated when it is compiled.