A virtual machine intended as a target for functional programming language compilers.

The SECD machine is defined with a set of transitions between its four components:


Like all internal data-structures, the stack is a list, with the S register pointing at the list's head or beginning. Due to the list structure, the stack need not be a continuous block of memory, so stack space is available as long as there is a single free memory cell. Even when all cells have been used, garbage collection may yield additional free memory. Obviously, specific implementations of the SECD structure can implement the stack as a canonical stack structure, thus improving the overall efficiency of the virtual machine, provided that a strict bound be put on the dimension of the stack.

The C register points at the head of the code or instruction list that will be evaluated. Once the instruction there has been executed, the C is pointed at the next instruction in the list—it is similar to an instruction pointer (or program counter) in conventional machines, except that subsequent instructions are always specified during execution and are not by default contained in subsequent memory locations, as is the case with the conventional machines.

The current variable environment is managed by the E register, which points at a list of lists. Each individual list represents one environment level: the parameters of the current function are in the head of the list, variables that are free in the current function, but bound by a surrounding function, are in other elements of E.

The dump, at whose head the D register points, is used as temporary storage for values of the other registers, for example during function calls. It can be likened to the return stack of other machines.


Basic Instructions
NILpushes a nil pointer onto the stack
LDpushes the value of a variable onto the stack. The variable is indicated by the argument, a pair.
LDCpushes a constant argument onto the stack
Branching instructions
SELexpects two list arguments, and pops a value from the stack. The first list is executed if the popped value was non-nil, the second list otherwise. Before one of these list pointers is made the new C, a pointer to the instruction following s e l sel is saved on the dump.
JOINpops a list reference from the dump and makes this the new value of C. This instruction occurs at the end of both alternatives of a sel.
Non-recursive function instructions
LDFtakes one list argument representing a function. It constructs a closure (a pair containing the function and the current environment) and pushes that onto the stack.
APpops a closure and a list of parameter values from the stack. The closure is applied to the parameters by installing its environment as the current one, pushing the parameter list in front of that, clearing the stack, and setting C to the closure's function pointer. The previous values of S, E, and the next value of C are saved on the dump.
RTNpops one return value from the stack, restores S, E, and C from the dump, and pushes the return value onto the now-current stack.
Recursive function instructions
RAPworks like a p ap, only that it replaces an occurrence of a dummy environment with the current one, thus making recursive functions possible
DUMpushes a "dummy", an empty list, in front of the environment list.

A. Push Objects to Stack

Compilation Rules:

Stack Operations:

"Locate" is an auxiliary function. It returns the jth element of the ith sublist in e.

Note: roughly, e is a list of sublists each of which is a list of actual parameters. Thus, e corresponds to the value list in our interpreter. There will be no name list here, as each occurrence of a formal parameter will be compiled to LD (i.j) and by locate(i.j) the corresponding actual parameter is found.

"The Mechanical Evaluation of Expressions" by PeterLandin is the original paper that describes the SECD machine.