1 salcianu 1.1 // InstrumentedAllocationStrategy.java, created Fri Feb  7 11:33:53 2003 by salcianu
  2 salcianu 1.3 // Copyright (C) 2000 Alexandru Salcianu <salcianu@MIT.EDU>
  3 salcianu 1.1 // Licensed under the terms of the GNU GPL; see COPYING for details.
  4 salcianu 1.1 package harpoon.Instrumentation.AllocationStatistics;
  5 salcianu 1.1 
  6 salcianu 1.1 import harpoon.Analysis.Maps.AllocationInformation.AllocationProperties;
  7 salcianu 1.1 import harpoon.Backend.Generic.Frame;
  8 salcianu 1.1 import harpoon.Backend.Runtime1.MallocAllocationStrategy;
  9 salcianu 1.1 
 10 salcianu 1.1 import harpoon.IR.Tree.DerivationGenerator;
 11 salcianu 1.1 import harpoon.IR.Tree.TreeFactory;
 12 salcianu 1.1 
 13 salcianu 1.1 import harpoon.IR.Tree.Stm;
 14 salcianu 1.1 import harpoon.IR.Tree.StmList;
 15 salcianu 1.1 import harpoon.IR.Tree.ExpList;
 16 salcianu 1.1 import harpoon.IR.Tree.Exp;
 17 salcianu 1.1 import harpoon.IR.Tree.CALL;
 18 salcianu 1.1 import harpoon.IR.Tree.CONST;
 19 salcianu 1.1 import harpoon.IR.Tree.TEMP;
 20 salcianu 1.1 import harpoon.IR.Tree.MOVE;
 21 salcianu 1.1 import harpoon.IR.Tree.LABEL;
 22 salcianu 1.1 import harpoon.IR.Tree.NAME;
 23 salcianu 1.1 import harpoon.IR.Tree.SEQ;
 24 salcianu 1.1 import harpoon.IR.Tree.ESEQ;
 25 salcianu 1.1 import harpoon.IR.Tree.Typed;
 26 salcianu 1.1 
 27 salcianu 1.1 import harpoon.ClassFile.HClass;
 28 salcianu 1.3 import harpoon.ClassFile.HMethod;
 29 salcianu 1.1 import harpoon.ClassFile.HCodeElement;
 30 salcianu 1.1 import harpoon.Temp.Temp;
 31 salcianu 1.1 import harpoon.Temp.TempFactory;
 32 salcianu 1.1 import harpoon.Temp.Label;
 33 salcianu 1.1 
 34 salcianu 1.3 import harpoon.Backend.Maps.NameMap;
 35 salcianu 1.3 
 36 salcianu 1.1 import java.util.List;
 37 salcianu 1.1 import java.util.ArrayList;
 38 salcianu 1.1 
 39 salcianu 1.1 
 40 salcianu 1.1 /**
 41 salcianu 1.1  * <code>InstrumentedAllocationStrategy</code>
 42 salcianu 1.1  * 
 43 salcianu 1.3  * @author  Alexandru Salcianu <salcianu@MIT.EDU>
 44 salcianu 1.4  * @version $Id: InstrumentedAllocationStrategy.java,v 1.4 2003/02/11 20:16:01 salcianu Exp $
 45 salcianu 1.1  */
 46 salcianu 1.1 public class InstrumentedAllocationStrategy extends MallocAllocationStrategy {
 47 salcianu 1.1     
 48 salcianu 1.1     /** Creates a <code>InstrumentedAllocationStrategy</code>. */
 49 salcianu 1.1     public InstrumentedAllocationStrategy(Frame f) { 
 50 salcianu 1.1         super(f, "GC_malloc");
 51 salcianu 1.3         instrumMethod = InstrumentAllocs.getMethod
 52 salcianu 1.3             (f.getLinker(),
 53 salcianu 1.3              "harpoon.Runtime.CounterSupport",
 54 salcianu 1.3              "count2",
 55 salcianu 1.3              new HClass[]{HClass.Int, HClass.Int});
 56 salcianu 1.1     }
 57 salcianu 1.3 
 58 salcianu 1.3     private final HMethod instrumMethod;
 59 salcianu 1.3 
 60 salcianu 1.3     /* Generates the following sequence of instructions for each
 61 salcianu 1.3        allocation site:
 62 salcianu 1.3 
 63 salcianu 1.3          tlength = length;
 64 salcianu 1.3          call to instrumentation method(index, tlength);
 65 salcianu 1.3           (both normal and exceptional exits go to label)
 66 salcianu 1.3        label:
 67 salcianu 1.3          allocCall(..., tlength);
 68 salcianu 1.3     */
 69 salcianu 1.1     public Exp memAlloc(TreeFactory tf, HCodeElement source,
 70 salcianu 1.1                         DerivationGenerator dg,
 71 salcianu 1.1                         AllocationProperties ap,
 72 salcianu 1.1                         Exp length) {
 73 salcianu 1.1         int id = ap.getUniqueID();
 74 salcianu 1.4         // id == -1 corresponds to allocation sites that are part of
 75 salcianu 1.4         // our instrumenting code
 76 salcianu 1.4         if(id == -1) return super.memAlloc(tf, source, dg, ap, length);
 77 salcianu 1.1         
 78 salcianu 1.1         TempFactory tempFact = tf.tempFactory();
 79 salcianu 1.1         
 80 salcianu 1.3         TEMP tlength = new TEMP(tf, source, TEMP.INT, new Temp(tempFact));
 81 salcianu 1.3         dg.putType(tlength, HClass.Int);
 82 salcianu 1.3 
 83 salcianu 1.3         /* bogus variable to store the exception from the call */
 84 salcianu 1.3         TEMP texcp = new TEMP(tf, source, Typed.POINTER, new Temp(tempFact));
 85 salcianu 1.3         dg.putType(texcp, HClass.Void);
 86 salcianu 1.3 
 87 salcianu 1.3         MOVE move = new MOVE(tf, source, tlength, length);
 88 salcianu 1.1         
 89 salcianu 1.3         Label contLabel = new Label(getUniqueName());
 90 salcianu 1.3         NAME continuation = new NAME(tf, source, contLabel);
 91 salcianu 1.4 
 92 salcianu 1.3         CALL call = new CALL
 93 salcianu 1.3             (tf, source,
 94 salcianu 1.3              null,  /* no return value */
 95 salcianu 1.3              texcp, /* exceptions go to texcp (unused) */
 96 salcianu 1.3              /* method to call */
 97 salcianu 1.4              new NAME(tf, source,
 98 salcianu 1.4                       frame.getRuntime().getNameMap().label(instrumMethod)),
 99 salcianu 1.3              /* 2 arguments: */
100 salcianu 1.3              new ExpList(new CONST(tf, source, id), /* allocation ID */
101 salcianu 1.3                          new ExpList(tlength,       /* memory length */
102 salcianu 1.3                                      null)),
103 salcianu 1.3              /* exception handler = normal continuation */
104 salcianu 1.3              continuation,
105 salcianu 1.3              false /* not a tail call*/);
106 salcianu 1.4 
107 salcianu 1.3         LABEL label = new LABEL(tf, source, contLabel, false);
108 salcianu 1.3         Exp allocCall = super.memAlloc(tf, source, dg, ap, tlength);
109 salcianu 1.1 
110 salcianu 1.3         Exp wholeSequence =
111 salcianu 1.3             new ESEQ(new SEQ(move, new SEQ(call, label)), allocCall);
112 salcianu 1.1 
113 salcianu 1.4         /*
114 salcianu 1.3         System.out.println("IT's COMING!");
115 salcianu 1.3         System.out.println(wholeSequence);
116 salcianu 1.3         System.exit(1);
117 salcianu 1.4         */
118 salcianu 1.1 
119 salcianu 1.3         return wholeSequence;
120 salcianu 1.1     }
121 salcianu 1.1 
122 salcianu 1.1     protected static Exp DECLARE(DerivationGenerator dg, HClass hc, Exp exp) {
123 salcianu 1.3         if (dg != null) dg.putType(exp, hc);
124 salcianu 1.1         return exp;
125 salcianu 1.1     }
126 salcianu 1.1 
127 salcianu 1.1     private String getUniqueName() {
128 salcianu 1.1         return "cont" + uniqueNameCounter++;
129 salcianu 1.1     }
130 salcianu 1.1     private static int uniqueNameCounter = 0;
131 salcianu 1.1 }