1 cananian 1.1.2.1 // DerivationGenerator.java, created Mon Feb 28 16:01:34 2000 by cananian
 2 cananian 1.1.2.1 // Copyright (C) 1999 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.Backend.CSAHack.RegAlloc;
 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 pnkfelix 1.1.2.2 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.IR.Assem.Instr;
12 cananian 1.1.2.1 import harpoon.Temp.Temp;
13 cananian 1.1.2.1 import harpoon.Temp.TempMap;
14 cananian 1.5     import net.cscott.jutil.Default;
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.Iterator;
19 cananian 1.1.2.1 import java.util.Map;
20 cananian 1.1.2.1 /**
21 cananian 1.1.2.1  * <code>DerivationGenerator</code> helps maintain the accuracy of
22 cananian 1.1.2.1  * the <code>Derivation</code> while the register allocator creates
23 cananian 1.1.2.1  * spills.
24 cananian 1.1.2.1  * 
25 cananian 1.1.2.1  * @author  C. Scott Ananian <cananian@alumni.princeton.edu>
26 cananian 1.6      * @version $Id: DerivationGenerator.java,v 1.6 2004/02/08 03:20:48 cananian Exp $
27 cananian 1.1.2.1  */
28 cananian 1.1.2.1 public class DerivationGenerator implements Derivation {
29 cananian 1.1.2.1     private Map dtM = new HashMap();
30 cananian 1.1.2.1     
31 cananian 1.1.2.1     /** Creates a <code>DerivationGenerator</code>. */
32 cananian 1.1.2.1     public DerivationGenerator(Instr instrs, Derivation deriv) {
33 cananian 1.3.2.1         assert deriv!=null;
34 cananian 1.1.2.1         for (Instr in=instrs; in!=null; in=in.getNext()) {
35 cananian 1.6                 for (Object dO : in.defC()) {
36 cananian 1.6                     Temp d = (Temp) dO;
37 cananian 1.1.2.1                 HClass hc = deriv.typeMap(in, d);
38 cananian 1.1.2.1                 DList dl  = deriv.derivation(in, d);
39 cananian 1.1.2.1                 if (hc!=null)
40 cananian 1.1.2.1                     dtM.put(Default.pair(in, d), new TypeAndDerivation(hc));
41 cananian 1.1.2.1                 else
42 cananian 1.1.2.1                     dtM.put(Default.pair(in, d), new TypeAndDerivation(dl));
43 cananian 1.1.2.1             }
44 cananian 1.1.2.1         }
45 cananian 1.1.2.1     }
46 cananian 1.1.2.1     
47 cananian 1.1.2.1     // public interface
48 cananian 1.1.2.1     public HClass typeMap(HCodeElement hce, Temp t)
49 cananian 1.1.2.1         throws TypeNotKnownException {
50 cananian 1.1.2.1         TypeAndDerivation tad =
51 cananian 1.1.2.1             (TypeAndDerivation) dtM.get(Default.pair(hce, t));
52 cananian 1.1.2.1         if (tad==null) throw new TypeNotKnownException(hce, t);
53 cananian 1.1.2.1         return tad.type;
54 cananian 1.1.2.1     }
55 cananian 1.1.2.1     public DList  derivation(HCodeElement hce, Temp t)
56 cananian 1.1.2.1         throws TypeNotKnownException {
57 cananian 1.1.2.1         TypeAndDerivation tad =
58 cananian 1.1.2.1             (TypeAndDerivation) dtM.get(Default.pair(hce, t));
59 cananian 1.1.2.1         if (tad==null) throw new TypeNotKnownException(hce, t);
60 cananian 1.1.2.1         return tad.derivation;
61 cananian 1.1.2.1     }
62 cananian 1.1.2.1     // private interface
63 cananian 1.1.2.1     /** replace old instr with new instr, using specified temp map for defs */
64 cananian 1.1.2.1     void update(Instr oldi, Instr newi, TempMap defmap) {
65 cananian 1.6             for (Object dO : oldi.defC()) {
66 cananian 1.6                 Temp d = (Temp) dO;
67 cananian 1.1.2.1             TypeAndDerivation tad = 
68 cananian 1.1.2.1                 (TypeAndDerivation) dtM.remove(Default.pair(oldi, d));
69 cananian 1.1.2.1             dtM.put(Default.pair(newi, defmap.tempMap(d)), tad);
70 cananian 1.1.2.1         }
71 cananian 1.1.2.1     }
72 cananian 1.1.2.1     void copy(Instr oldi, Temp olduse, Instr newi, Temp newuse) {
73 cananian 1.1.2.1         // XXX HACK
74 cananian 1.1.2.1         dtM.put(Default.pair(newi, newuse),
75 cananian 1.1.2.1                 new TypeAndDerivation(HClass.Void));
76 cananian 1.1.2.1     }
77 cananian 1.1.2.1 
78 cananian 1.1.2.1     /** internal structure of type/derivation information */
79 cananian 1.1.2.1     private static class TypeAndDerivation {
80 cananian 1.1.2.1         /** non-null for base pointers */
81 cananian 1.1.2.1         public final HClass type;
82 cananian 1.1.2.1         /** non-null for derived pointers */ 
83 cananian 1.1.2.1         public final DList derivation;
84 cananian 1.1.2.1         // public constructors
85 cananian 1.1.2.1         TypeAndDerivation(HClass type) { this(type, null); }
86 cananian 1.1.2.1         TypeAndDerivation(DList deriv) { this(null, deriv); }
87 cananian 1.1.2.1         /** private constructor */
88 cananian 1.1.2.1         private TypeAndDerivation(HClass type, DList derivation) {
89 cananian 1.3.2.1             assert type!=null ^ derivation!=null;
90 cananian 1.1.2.1             this.type = type;
91 cananian 1.1.2.1             this.derivation = derivation;
92 cananian 1.1.2.1         }
93 cananian 1.1.2.1     }
94 cananian 1.2     }