This paper discusses a two-item stack model and its six fundamental operations called stack quarks. These fundamental operations are like quarks of matter, truly fundamental. They may not be factored any further. Combinations of these stack quarks can be used in a language for describing data flow. When building compilers for stack based languages, phrases of stack macros may be broken down into stack quarks and simple optimizing rules may be applied. At the end of this paper, some optimization rules are modeled in Timbre.
Particle scientists in the search of the fundamental pieces of matter, have found six elementary particles which when combined, form matter (basically). These elementary particles are called quarks. Given a limited number of particles, there should be a particular set of rules which describes their combinations that produce the physical world we live in.
The same factoring can be applied to a model. If we look at a two-item stack model which has a stack, a next register and a top register, we find that there are six fundamental operations. These six operations are like the matter quarks as they can be combined to form more complex stack operations for stack based algorithms. Interestingly, they can be grouped in pairs and they carry a charge.
Stacks are fundamental to us as children. As we play, we learn how many blocks we can stack up before we get an overflow, and crash. We build and operate the stack with two hands. Therefore it is natural for us to think of a two item stack model.
Top and next may be picked off the top of the stack with both hands. They may be set back as they were, (Uncertainty Principle: now what the heck is on the stack? or, what was I doing?), swapped or coact with the stack. For instance, if we pick up top and next, put next down (store it somewhere in RAM) and then pick up an item off the stack, we have a new next and the number of items in the model has decreased by one. Same for top. Top, next, and the stack interact to execute stack-based algorithms and computations.
The stack quarks are the singular operations which describe all the operations between the stack, top and next. In considering the simplest operation, it can only be a single data movement. For certainly anything less than this would be no action.
DRIP and DIP have no charge (S0) since there is no net gain or loss in the number of items kept. The other four operations carry a charge which reflects whether there is an increase (S+) or decrease (S-) in the overall numbers of items. The overall charge in each pair is zero. DRIP and DIP are the operations which may be used to copy items between top and next. NIP and NUP pop or push the stack to and from next. TAKE and TUCK pop and push the stack to and from top.
Top and next are available as input and output registers to a computing unit which can perform functions like add, subtract, multiply, shift, logic, etc.. The stack is available to top and next for keeping intermediate values in computing unit sequences.
The following rules are part of a Timbre1 script which will translate stack macros into fundamental quarks of the two item stack model and then do some peephole optimization.
Script 1. These Timbre rules are the definitions of the four stack macros in terms of the stack quarks. The superscript number signifies the quarks needed to define the macro. The quote signifies that they run in parallel. Some rules are redundant but are there for interest to describe other ways of creating the same macro.
Script 2. These Timbre rules are some optimizations to improve the code production. The numbers at the end of the line represent how many quarks are used by the initial phrase and what it is reduced to.
When running source code through the translation rules, the stack macros are reduced to quarks and then any quark sequences which appear in the optimization rules, executes, further simplifying the source code. Finally, the quarks will have rules to translate them into target code, whether it is logic, symbolic, binary or another programming language.
If we take the sequence DROP DUP, and expand it into its quark sequence, we will see how it can be reduced to something simpler (if we only had the stack macros, then we would not be able to reduce the phrase any further since this simplification ends up as a stack quark).
FIGURE 4. Phrase reduction of DROP DUP produces DIP. The shaded ones are in identical stack situations. Pictorially, it is easy to see what to factor out.
The rules in script 1 and 2 are used to illustrate the optimization of the phrase DROP DUP. Rule 2 executes to translate the DROP into DIP NIP. Rule 31 execute to translate NIP DUP into DRIP and we are left with DIP DRIP. Then rule 14 executes to reduce the phrase to just DIP.
This paper sparked a working group on finding other quarks for the rest of the computing unit. It was felt that these new quarks held the same power in terms of defining higher order operations and allowing for efficient compilation as the ones in this paper.
A PDF version of the original paper is also available.