> There is actually a much better approach, which was done in Algol 68 > and seems now to be done only in Ada. As far as the compiler > implementation goes, it is a trivial variation on what you have done, > but there is a little more work in the run-time system.
Obviously this depends on the compiler. GNAT allocates all local arrays on the primary stack for example. > That is to use primary and secondary stacks. The primary is for the > linkage, scalars, array descriptors and very small, fixed-size arrays. > The secondary is for all other arrays with automatic scope. You get > all of the benefits that you mention, because both are true stacks, > and the scope of the secondary stack is controlled by that of the > primary. GNAT has a secondary stack, but it is only used to return objects whose size is self-referential, i.e. isn't given by the return type of the function. > You need a single secondary stack pointer (and preferably limit, for > checking), which can be global variables. If a procedure uses the > secondary stack, it needs to restore it on leaving, or when leaving > a scope including an allocation - but you have already implemented > that! Not exactly, managing a secondary stack isn't supported in the GENERIC IL, you need to do it explicitly by inserting calls to the runtime at relevant points. As a consequence, this isn't an efficient mechanism, hence the restricted use. -- Eric Botcazou