HANDBOOK / GENTLE PRIMER / Optimal Rule Selection /

# A Code-Generator Specification

Here is a specification of a code generator for the combined stack and accumulator computer:

       'choice' Encode(Stmnt)

          'rule' Encode(assign(V,X)):
             StackCode(X)
             POP(V)
             $ 10
          'rule' Encode(assign(V,X)):
             AccuCode(X)
             STOREACCU(V)
             $ 10

       'choice' StackCode(Expr)

          'rule' StackCode(plus(X,Y)):
             StackCode(X)
             StackCode(Y)
             PLUS
             $ 20
          'rule' StackCode(minus(X,Y)):
             StackCode(X)
             StackCode(Y)
             MINUS
             $ 20
          'rule' StackCode(var(V)):
             PUSH(V)
             $ 10
          'rule' StackCode(X):
             AccuCode(X)
             ACCUTOSTACK
             $ 10

       'choice' AccuCode(Expr)

          'rule' AccuCode(plus(X, var(V))):
             AccuCode(X)
             ACCUPLUS(V)
             $ 10
          'rule' AccuCode(minus(X, var(V))):
             AccuCode(X)
             ACCUMINUS(V)
             $ 10
          'rule' AccuCode(var(V))
             LOADACCU(V)
             $ 10
          'rule' AccuCode(X):
             StackCode(X)
             STACKTOACCU
             $ 20