1 cananian 1.1.2.1 // DerivationMap.java, created Tue May 16 22:03:20 2000 by cananian
  2 cananian 1.1.2.1 // Copyright (C) 2000 C. Scott Ananian <cananian@alumni.princeton.edu>
  3 cananian 1.1.2.1 // Licensed under the terms of the GNU GPL; see COPYING for details.
  4 cananian 1.1.2.1 package harpoon.IR.LowQuad;
  5 cananian 1.1.2.1 
  6 cananian 1.1.2.1 import harpoon.Analysis.Maps.Derivation;
  7 cananian 1.1.2.1 import harpoon.Analysis.Maps.Derivation.DList;
  8 cananian 1.1.2.1 import harpoon.Analysis.Maps.TypeMap.TypeNotKnownException;
  9 cananian 1.1.2.1 import harpoon.ClassFile.HClass;
 10 cananian 1.1.2.1 import harpoon.ClassFile.HCodeElement;
 11 cananian 1.1.2.1 import harpoon.Temp.Temp;
 12 cananian 1.1.2.1 import harpoon.Temp.TempMap;
 13 cananian 1.6     import net.cscott.jutil.Default;
 14 cananian 1.6     import net.cscott.jutil.Default.PairList;
 15 cananian 1.1.2.1 import harpoon.Util.Util;
 16 cananian 1.1.2.1 
 17 cananian 1.1.2.1 import java.util.HashMap;
 18 cananian 1.1.2.1 import java.util.List;
 19 cananian 1.1.2.1 import java.util.Map;
 20 cananian 1.1.2.1 /**
 21 cananian 1.1.2.1  * <code>DerivationMap</code> is a simple map-based implementation of
 22 cananian 1.1.2.1  * common <code>Derivation</code> functionality.
 23 cananian 1.1.2.1  * 
 24 cananian 1.1.2.1  * @author  C. Scott Ananian <cananian@alumni.princeton.edu>
 25 cananian 1.6      * @version $Id: DerivationMap.java,v 1.6 2004/02/08 01:55:17 cananian Exp $
 26 cananian 1.1.2.1  * @see harpoon.IR.Tree.DerivationGenerator
 27 cananian 1.1.2.1  */
 28 cananian 1.5     public class DerivationMap<HCE extends HCodeElement>
 29 cananian 1.5         implements Derivation<HCE> {
 30 cananian 1.1.2.1     /** private type and derivation map */
 31 cananian 1.5         private Map<PairList<HCE,Temp>,TypeAndDerivation> dtM =
 32 cananian 1.5             new HashMap<PairList<HCE,Temp>,TypeAndDerivation>();
 33 cananian 1.1.2.1     
 34 cananian 1.1.2.1     /** Creates a <code>DerivationMap</code>. */
 35 cananian 1.1.2.1     public DerivationMap() { }
 36 cananian 1.1.2.1 
 37 cananian 1.1.2.1     /** internal structure of type/derivation information. */
 38 cananian 1.1.2.1     private static class TypeAndDerivation {
 39 cananian 1.1.2.1         /** non-null for base pointers */
 40 cananian 1.1.2.1         public final HClass type;
 41 cananian 1.1.2.1         /** non-null for derived pointers */ 
 42 cananian 1.1.2.1         public final DList derivation;
 43 cananian 1.1.2.1         // public constructors
 44 cananian 1.1.2.1         TypeAndDerivation(HClass type) { this(type, null); }
 45 cananian 1.1.2.1         TypeAndDerivation(DList deriv) { this(null, deriv); }
 46 cananian 1.1.2.1         /** private constructor */
 47 cananian 1.1.2.1         private TypeAndDerivation(HClass type, DList derivation) {
 48 cananian 1.3.2.1             assert type!=null ^ derivation!=null;
 49 cananian 1.1.2.1             this.type = type;
 50 cananian 1.1.2.1             this.derivation = derivation;
 51 cananian 1.1.2.1         }
 52 cananian 1.1.2.1         TypeAndDerivation rename(TempMap tm) {
 53 cananian 1.1.2.1             if (this.derivation!=null && tm!=null)
 54 cananian 1.1.2.1                 return new TypeAndDerivation(DList.rename(this.derivation,tm));
 55 cananian 1.1.2.1             // no need to create a new object, as tad's are immutable.
 56 cananian 1.1.2.1             return this;
 57 cananian 1.1.2.1         }
 58 cananian 1.1.2.1     }
 59 cananian 1.1.2.1     // public interface
 60 cananian 1.5         public HClass typeMap(HCE hce, Temp t)
 61 cananian 1.1.2.1         throws TypeNotKnownException {
 62 cananian 1.1.2.1         return getDT(hce, t).type;
 63 cananian 1.1.2.1     }
 64 cananian 1.5         public DList  derivation(HCE hce, Temp t)
 65 cananian 1.1.2.1         throws TypeNotKnownException {
 66 cananian 1.1.2.1         return getDT(hce, t).derivation;
 67 cananian 1.1.2.1     }
 68 cananian 1.1.2.1 
 69 cananian 1.1.2.1     // allow implementations to add explicit type/derivation information
 70 cananian 1.1.2.1     /** Add a mapping from the given <code>Temp</code> <code>t</code>
 71 cananian 1.1.2.1      *  defined at the given <code>HCodeElement</code> <code>hce</code>
 72 cananian 1.1.2.1      *  to the given <code>HClass</code> <code>type</code> to this
 73 cananian 1.1.2.1      *  <code>DerivationMap</code>. */
 74 cananian 1.5         public void putType(HCE hce, Temp t, HClass type) {
 75 cananian 1.3.2.1         assert hce!=null && t!=null && type!=null;
 76 cananian 1.1.2.1         putDT(hce, t, new TypeAndDerivation(type));
 77 cananian 1.1.2.1     }
 78 cananian 1.1.2.1     /** Add a mapping from the given <code>Temp</code> <code>t</code>
 79 cananian 1.1.2.1      *  defined at the given <code>HCodeElement</code> <code>hce</code>
 80 cananian 1.1.2.1      *  to the given <code>Derivation.DList</code> <code>derivation</code>
 81 cananian 1.1.2.1      *  to this <code>DerivationMap</code>. */
 82 cananian 1.5         public void putDerivation(HCE hce, Temp t, DList derivation) {
 83 cananian 1.3.2.1         assert hce!=null && t!=null && derivation!=null;
 84 cananian 1.1.2.1         putDT(hce, t, new TypeAndDerivation(derivation));
 85 cananian 1.1.2.1     }
 86 cananian 1.1.2.1     /** Transfer typing from one place to another. */
 87 cananian 1.5         public void update(HCE old_hce, Temp old_t,
 88 cananian 1.5                            HCE new_hce, Temp new_t) {
 89 cananian 1.1.2.1         TypeAndDerivation tad = getDT(old_hce, old_t);
 90 cananian 1.1.2.1         removeDT(old_hce, old_t);
 91 cananian 1.1.2.1         putDT(new_hce, new_t, tad);
 92 cananian 1.1.2.1     }
 93 cananian 1.1.2.1     /** Transfer typings from one <code>Derivation</code> to another, using
 94 cananian 1.1.2.1      *  an appropriate <code>TempMap</code>. */
 95 cananian 1.5         public <HCE2 extends HCodeElement> void transfer
 96 cananian 1.5                              (HCE new_hce,
 97 cananian 1.5                               HCE2 old_hce, Temp[] old_defs,
 98 cananian 1.5                               TempMap old2new, Derivation<HCE2> old_deriv) {
 99 cananian 1.1.2.1         for (int i=0; i<old_defs.length; i++) {
100 cananian 1.1.2.1             Temp old_def = old_defs[i];
101 cananian 1.1.2.1             Temp new_def = old2new==null ? old_def : old2new.tempMap(old_def);
102 cananian 1.1.2.1             DList dl = old_deriv.derivation(old_hce, old_def);
103 cananian 1.1.2.1             if (dl==null)
104 cananian 1.1.2.1                 putType(new_hce, new_def, old_deriv.typeMap(old_hce,old_def));
105 cananian 1.1.2.1             else
106 cananian 1.1.2.1                 putDerivation(new_hce, new_def,
107 cananian 1.1.2.1                               old2new==null ? dl : DList.rename(dl, old2new));
108 cananian 1.1.2.1         }
109 cananian 1.1.2.1     }
110 cananian 1.1.2.1     // allow implementations to flush old data from the derivation generator
111 cananian 1.1.2.1     /** Remove all type and derivation mappings for the given
112 cananian 1.1.2.1      *  <code>Temp</code> defined at the given <code>HCodeElement</code>.
113 cananian 1.1.2.1      *  Used for memory management purposes. */
114 cananian 1.5         public void remove(HCE hce, Temp t) {
115 cananian 1.3.2.1         assert hce!=null && t!=null;
116 cananian 1.1.2.1         removeDT(hce, t);
117 cananian 1.1.2.1     }
118 cananian 1.1.2.1 
119 cananian 1.1.2.1     // private interface
120 cananian 1.5         private TypeAndDerivation getDT(HCE hce, Temp t)
121 cananian 1.1.2.1         throws TypeNotKnownException {
122 cananian 1.5             PairList<HCE,Temp> pair = Default.pair(hce, t);
123 cananian 1.1.2.1         if (!dtM.containsKey(pair))
124 cananian 1.1.2.1             throw new TypeNotKnownException(hce, t);
125 cananian 1.5             return dtM.get(pair);
126 cananian 1.1.2.1     }
127 cananian 1.5         private void putDT(HCE hce, Temp t, TypeAndDerivation tad) {
128 cananian 1.5             PairList<HCE,Temp> pair = Default.pair(hce, t);
129 cananian 1.3.2.1         assert !dtM.containsKey(pair);
130 cananian 1.3.2.1         assert tad!=null;
131 cananian 1.1.2.1         dtM.put(pair, tad);
132 cananian 1.1.2.1     }
133 cananian 1.5         private void removeDT(HCE hce, Temp t) {
134 cananian 1.5             PairList<HCE,Temp> pair = Default.pair(hce, t);
135 cananian 1.1.2.1         dtM.remove(pair);
136 cananian 1.1.2.1     }
137 cananian 1.2     }