Stackmachines are hidden register machines


A look into the problem of context switching on stackmachines have results into a remarkable fact. In reality, all so called stackmachines are using a stack buffer. That is an area in memory which is referenced by a pointer. If the operating system switches between the tasks, so called context-switching, the pointer to the stackbuffer is adjusted. So the new task gets his own datastack and return stack. What does that mean for reality? It mean, that real Forth CPUs have a way more than only a datastack and a return stack, they have a cache witch many stacks.

Sometimes in literature it was told, that stackmachines are in reality normal register machines. And here is the reason why. If a stackmachine really provides two stacks for data and return addresses, the machine is not able for context switch. Let us investigate the problem on real Forth implementations. Nearly all x86 based Forth Virtual machines are implementing the PAUSE command. Pause is using the capabilities of a register cpu for context switches. It saves the current stack to memory and retrieves the new one. That is one of the reason why Forth is slow.

If Forth is used not an x86 machines but on Stackmachines, the context switching works different. There are two options. The first one is, that the programmer is not recognizing the problem, because he only runs one program at the same time. Or he needs context switching, than the underlying cpu must support it. How exactly this is done is a bit like voodoo magic. But in reality, Forth CPUs like the GA144 have such feature integrated, which means it is not really a stackcomputer but a registermachine which calls themself a stackmachine.

Apart from real machines we can research the problem in theory. Suppose we are constructing a two stackpushdown automaton with pen & paper. .This machine is able to run Forth, but the machine can’t switch between tasks. For doing so, we must first extend the pushdown automaton to a Von-neuman-architecture, which is equal that it is no longer a Forth CPU. Only then we can switch between the tasks.

What I want to explain is, that stackmachines are an illusion. It is not possible to build them in reality.

Advertisements

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.