The return stack
The return stack is forth's method for managing control flow throughout words. In general, a compiled forth program is a list of addresses in memory that correspond to other words. (1)
Consider this piece of forth code:
: duplicate dup ;
: square duplicate * ;
2 square
Once the word 'square' is executed, forth needs to execute the word 'duplicate' but also needs to remember its location inside 'square' so it can come back and execute *. Forth uses the return stack for this.
In the following diagram, the program counter (pc) is pointing to the location '5' in memory. Forth recognises that it needs to execute the code at memory location 12, but has to be able to resume execution at location 6 after the called word is done executing. It does this by pushing the location after the program counter onto the return stack, setting the program counter to 12, and then resuming execution. At some point, the called word will execute the word 'exit' which will simply pop a value from the top of the return stack, place it into the program counter, and then resume execution.