1 cananian 1.1.4.1 // Translation.java, created Wed Sep 29 18:37:28 1999 by cananian
  2 cananian 1.1.4.1 // Copyright (C) 1999 C. Scott Ananian <cananian@alumni.princeton.edu>
  3 cananian 1.1.4.1 // Licensed under the terms of the GNU GPL; see COPYING for details.
  4 cananian 1.1.4.1 package harpoon.IR.Tree;
  5 cananian 1.1.4.1 
  6 cananian 1.1.4.1 import harpoon.Temp.Label;
  7 cananian 1.1.4.1 import harpoon.Temp.Temp;
  8 cananian 1.1.4.3 import harpoon.Util.Util;
  9 cananian 1.1.4.1 /**
 10 cananian 1.1.4.1  * <code>Translation</code> is an empty class wrapper for various
 11 cananian 1.1.4.1  * special context-sensitive <code>Tree.Exp</code> wrappers.
 12 cananian 1.1.4.1  * Boolean expressions are often best compiled different ways
 13 cananian 1.1.4.1  * depending on whether they are used in a direct assignment or
 14 cananian 1.1.4.1  * as the argument to a conditional branch. 
 15 cananian 1.1.4.1  * <code>Translation.Exp</code> is a closure object that allows
 16 cananian 1.1.4.1  * this sort of conditional context-sensitive expression resolution.
 17 cananian 1.1.4.1  * 
 18 cananian 1.1.4.1  * @author  C. Scott Ananian <cananian@alumni.princeton.edu>
 19 cananian 1.4      * @version $Id: Translation.java,v 1.4 2002/04/10 03:05:46 cananian Exp $
 20 cananian 1.1.4.1  */
 21 cananian 1.1.4.1 public abstract class Translation {
 22 cananian 1.1.4.1     /** The <code>Translation.Exp</code> class represents an expression
 23 cananian 1.1.4.1      *  that might be used in several different ways: as a value,
 24 cananian 1.1.4.1      *  as a branch condition, or as code to be executed for side-effects
 25 cananian 1.1.4.1      *  only.
 26 pnkfelix 1.1.4.2      *
 27 pnkfelix 1.1.4.2      *  For each instance of this class, only one of these methods
 28 pnkfelix 1.1.4.2      *  should be called, and it should only be called once.
 29 cananian 1.1.4.1      */
 30 cananian 1.1.4.1     public static abstract class Exp {
 31 cananian 1.1.4.3         private boolean once=false;
 32 pnkfelix 1.1.4.2         public final harpoon.IR.Tree.Exp unEx(TreeFactory tf) {
 33 cananian 1.3.2.1             assert !once; once=true;
 34 pnkfelix 1.1.4.2             return unExImpl(tf);
 35 pnkfelix 1.1.4.2         }
 36 pnkfelix 1.1.4.2         protected abstract harpoon.IR.Tree.Exp unExImpl(TreeFactory tf);
 37 pnkfelix 1.1.4.2         
 38 pnkfelix 1.1.4.2         public final harpoon.IR.Tree.Stm unNx(TreeFactory tf) {
 39 cananian 1.3.2.1             assert !once; once=true;
 40 pnkfelix 1.1.4.2             return unNxImpl(tf);
 41 pnkfelix 1.1.4.2         }
 42 pnkfelix 1.1.4.2         protected abstract harpoon.IR.Tree.Stm unNxImpl(TreeFactory tf);
 43 pnkfelix 1.1.4.2         public final harpoon.IR.Tree.Stm unCx(TreeFactory tf,
 44 pnkfelix 1.1.4.2                                               Label iftrue, 
 45 pnkfelix 1.1.4.2                                               Label iffalse) {
 46 cananian 1.3.2.1             assert !once; once=true;
 47 pnkfelix 1.1.4.2             return unCxImpl(tf, iftrue, iffalse);
 48 pnkfelix 1.1.4.2         }
 49 pnkfelix 1.1.4.2         protected abstract harpoon.IR.Tree.Stm unCxImpl(TreeFactory tf,
 50 pnkfelix 1.1.4.2                                                         Label iftrue, 
 51 pnkfelix 1.1.4.2                                                         Label iffalse);
 52 cananian 1.1.4.1     }
 53 cananian 1.1.4.6     /** The <code>Translation.Ex</code> class is a <code>Translation.Exp</code>
 54 cananian 1.1.4.6      *  representing a value expression.  It can be evaluated as an
 55 cananian 1.1.4.6      *  expression, a conditional, or a side-effects-only statement. */
 56 cananian 1.1.4.1     public static class Ex extends Exp {
 57 cananian 1.1.4.1         final harpoon.IR.Tree.Exp exp;
 58 cananian 1.1.4.1         public Ex(harpoon.IR.Tree.Exp exp) { this.exp = exp; }
 59 pnkfelix 1.1.4.2         protected harpoon.IR.Tree.Exp unExImpl(TreeFactory tf) { return exp; }
 60 pnkfelix 1.1.4.2         protected harpoon.IR.Tree.Stm unNxImpl(TreeFactory tf) {
 61 jwhaley  1.1.4.5             return new EXPR(tf, exp, exp);
 62 cananian 1.1.4.1         }
 63 pnkfelix 1.1.4.2         protected harpoon.IR.Tree.Stm unCxImpl(TreeFactory tf,
 64 cananian 1.1.4.1                                         Label iftrue, Label iffalse) {
 65 cananian 1.1.4.4             // special cases for CONST 0 and CONST 1 are done in
 66 cananian 1.1.4.4             // a separate subclass of Ex (ExCONST, in ToTree)
 67 cananian 1.1.4.1             return new CJUMP(tf, exp, exp, iftrue, iffalse);
 68 cananian 1.1.4.1         }
 69 cananian 1.1.4.1     }
 70 cananian 1.1.4.6     /** The <code>Translation.Nx</code> class is a <code>Translation.Exp</code>
 71 cananian 1.1.4.6      *  representing a statement.  It can only be evaluated for side-effects;
 72 cananian 1.1.4.6      *  any attempt to evaluate it as a value expression or conditional
 73 cananian 1.1.4.6      *  will throw an <code>Error</code>. */
 74 cananian 1.1.4.1     public static class Nx extends Exp {
 75 cananian 1.1.4.1         final harpoon.IR.Tree.Stm stm;
 76 cananian 1.1.4.1         public Nx(harpoon.IR.Tree.Stm stm) { this.stm = stm; }
 77 pnkfelix 1.1.4.2         protected harpoon.IR.Tree.Exp unExImpl(TreeFactory tf) {
 78 cananian 1.1.4.1             throw new Error("Nx cannot be converted to Ex");
 79 cananian 1.1.4.1         }
 80 pnkfelix 1.1.4.2         protected harpoon.IR.Tree.Stm unNxImpl(TreeFactory tf) { return stm; }
 81 pnkfelix 1.1.4.2         protected harpoon.IR.Tree.Stm unCxImpl(TreeFactory tf,
 82 cananian 1.1.4.1                                         Label iftrue, Label iffalse) {
 83 cananian 1.1.4.1             throw new Error("Nx cannot be converted to Cx");
 84 cananian 1.1.4.1         }
 85 cananian 1.1.4.1     }
 86 cananian 1.1.4.6     /** The <code>Translation.Cx</code> abstract class specifies how to
 87 cananian 1.1.4.6      *  evaluate a conditional as an expression or as a side-effects-only
 88 cananian 1.1.4.6      *  statement. */
 89 cananian 1.1.4.1     public static abstract class Cx extends Exp {
 90 pnkfelix 1.1.4.2         protected harpoon.IR.Tree.Exp unExImpl(TreeFactory tf) {
 91 cananian 1.1.4.1             Temp  Tr = new Temp(tf.tempFactory(), "cx");
 92 cananian 1.1.4.1             Label Lt = new Label();
 93 cananian 1.1.4.1             Label Lf = new Label();
 94 cananian 1.1.4.3             Stm s = unCxImpl(tf, Lt, Lf);
 95 cananian 1.1.4.1             return new ESEQ
 96 cananian 1.1.4.1                 (tf, s,
 97 cananian 1.1.4.1                  new SEQ
 98 cananian 1.1.4.1                  (tf, s,
 99 cananian 1.1.4.1                   new MOVE(tf, s,
100 cananian 1.1.4.1                            new TEMP(tf, s, Type.INT, Tr),
101 cananian 1.1.4.1                            new CONST(tf, s, 1)),
102 cananian 1.1.4.1                   new SEQ
103 cananian 1.1.4.1                   (tf, s,
104 cananian 1.1.4.1                    s,
105 cananian 1.1.4.1                    new SEQ
106 cananian 1.1.4.1                    (tf, s,
107 cananian 1.1.4.1                     new LABEL(tf, s, Lf, false),
108 cananian 1.1.4.1                     new SEQ
109 cananian 1.1.4.1                     (tf, s,
110 cananian 1.1.4.1                      new MOVE(tf, s,
111 cananian 1.1.4.1                               new TEMP(tf, s, Type.INT, Tr),
112 cananian 1.1.4.1                               new CONST(tf, s, 0)),
113 cananian 1.1.4.1                      new LABEL(tf, s, Lt, false))))),
114 cananian 1.1.4.1                  new TEMP(tf, s, Type.INT, Tr));
115 cananian 1.1.4.1         }
116 pnkfelix 1.1.4.2         protected harpoon.IR.Tree.Stm unNxImpl(TreeFactory tf) {
117 cananian 1.1.4.1             Label l = new Label();
118 cananian 1.1.4.3             Stm s = unCxImpl(tf, l, l);
119 cananian 1.1.4.1             return new SEQ(tf, s, s, new LABEL(tf, s, l, false));
120 cananian 1.1.4.1         }
121 cananian 1.1.4.1     }
122 cananian 1.2     }