[NEXT] [PREV]    HANDBOOK / GENTLE PRIMER / Optimal Rule Selection /

# A Combined Stack and Accumulator Computer

Gentle
Applications
Concepts
Examples
Handbook
Support

We show cost driven-rule selection by a computer combining stack and accumulator processing (following Professor Ulrich Grude, who coined this example for his lectures on Gentle).

The combined computer provides both sets of instructions, i.e. one can switch between stack- and accumulator-based computation. In addition, it offers two further instructions that move data from stack to accumulator and vice versa:

```
'action' STACKTOACCU
'action' ACCUTOSTACK
```
STACKTOACCU pops the top element from the stack and stores it in the accumulator. ACCUTOSTACK pushes the current value of the accumulator onto the stack.

Since the specification for the stack computer covers all cases, we can simply copy these rules. Hence, we already have a correct code generator for the combined computer.

Because the rules for the accumulator-based computer are also correct, we copy them as well.

We add a rule that computes stack values in the accumulator:

```
'rule' StackCode(X):
AccuCode(X)
ACCUTOSTACK
```
and a rule that computes accumulator values using the stack:
```
'rule' AccuCode(X):
StackCode(X)
STACKTOACCU
```

[NEXT] [PREV]