The return of the Forth

In which I discover the return stack

The return stack is used in many Forth systems to record return addresses when executing defined words. We currently don’t need this because the return stack is implicit in the Swift stack. However, the return stack can be used for temporary storage. Furthermore, at some point, we would like to replace the Swift stack with a return stack of our making.

So far, we have identified two types of object that we would like to stop on the return stack:

  • return addresses which will consist of a WordList and an offset – but aren’t needed just yet.
  • data stack items

We will also need words for manipulating the return stack. For example >R, R>, R@.

At the moment the return stack is implemented as a stack of an enumeration defined as follows:

fileprivate enum ReturnStackCell
{
	case `return`(WordList, Int)
	case data(Int)
}

This makes it very simple to implement the words that manipulate it. Currently, we don’t cache the top word of the return stack, although that is an obvious optimisation.

The code is tagged blog-1113

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.