1 salcianu 1.1 // AllocationNumbering.java, created Wed Nov 8 19:06:08 2000 by cananian 2 salcianu 1.1 // Copyright (C) 2000 C. Scott Ananian <cananian@alumni.princeton.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.ClassHierarchy; 7 salcianu 1.1 import harpoon.ClassFile.CachingCodeFactory; 8 salcianu 1.1 import harpoon.ClassFile.HCode; 9 salcianu 1.1 import harpoon.ClassFile.HClass; 10 salcianu 1.1 import harpoon.ClassFile.HCodeFactory; 11 salcianu 1.1 import harpoon.ClassFile.HMethod; 12 salcianu 1.1 import harpoon.IR.Quads.ANEW; 13 salcianu 1.1 import harpoon.IR.Quads.CALL; 14 salcianu 1.1 import harpoon.IR.Quads.NEW; 15 salcianu 1.1 import harpoon.IR.Quads.Quad; 16 salcianu 1.1 import harpoon.IR.Quads.Code; 17 salcianu 1.1 18 salcianu 1.5 import harpoon.Util.Util; 19 salcianu 1.3 20 salcianu 1.1 import java.util.HashMap; 21 salcianu 1.1 import java.util.Iterator; 22 salcianu 1.1 import java.util.Map; 23 salcianu 1.1 import java.util.Set; 24 salcianu 1.1 import java.util.HashSet; 25 salcianu 1.1 import java.util.Collection; 26 salcianu 1.1 import java.io.PrintWriter; 27 salcianu 1.1 import java.io.BufferedWriter; 28 salcianu 1.1 import java.io.FileWriter; 29 salcianu 1.1 import java.io.IOException; 30 salcianu 1.1 31 salcianu 1.1 /** 32 salcianu 1.1 * An <code>AllocationNumbering</code> object assigns unique numbers 33 salcianu 1.1 * to each allocations site from a program and (possibly) to each call 34 salcianu 1.1 * sites. Later, these numbers can be used in the instrumenting code 35 salcianu 1.1 * (e.g. <code>InstrumentAllocs</code>). 36 salcianu 1.1 * 37 salcianu 1.1 * @author C. Scott Ananian <cananian@alumni.princeton.edu> 38 salcianu 1.6 * @version $Id: AllocationNumbering.java,v 1.6 2003/06/04 18:46:09 salcianu Exp $ */ 39 salcianu 1.6 public class AllocationNumbering 40 salcianu 1.6 implements AllocationNumberingInterf, java.io.Serializable { 41 salcianu 1.6 42 salcianu 1.6 public final Map alloc2int; 43 salcianu 1.6 public final Map call2int; 44 salcianu 1.1 45 salcianu 1.1 /** Creates an <code>AllocationNumbering</code> object. 46 salcianu 1.1 47 salcianu 1.2 @param ccf <code>CachingCodeFactory</code> giving the code 48 salcianu 1.2 that will later be instrumented 49 salcianu 1.1 @param ch <code>ClassHierarchy</code> for the code from hcf 50 salcianu 1.1 @param callSites if true, instrument the call sites too */ 51 salcianu 1.2 public AllocationNumbering(CachingCodeFactory ccf, ClassHierarchy ch, 52 salcianu 1.1 boolean callSites) { 53 salcianu 1.1 this.alloc2int = new HashMap(); 54 salcianu 1.1 this.call2int = callSites ? new HashMap() : null; 55 salcianu 1.1 for (Iterator it = ch.callableMethods().iterator(); it.hasNext(); ) 56 salcianu 1.2 number(ccf.convert((HMethod) it.next()), callSites); 57 salcianu 1.1 58 salcianu 1.1 System.out.println("alloc_count = " + alloc_count + 59 salcianu 1.1 "\tcall_count = " + call_count); 60 salcianu 1.1 } 61 salcianu 1.1 62 salcianu 1.4 /** @return an integer identifying the allocation site <code>q</code> */ 63 salcianu 1.1 public int allocID(Quad q) { 64 salcianu 1.4 if (!alloc2int.containsKey(q)) 65 salcianu 1.4 throw new UnknownAllocationSiteError 66 salcianu 1.5 ("Quad unknown: " + Util.code2str(q)); 67 salcianu 1.1 return ((Integer) alloc2int.get(q)).intValue(); 68 salcianu 1.1 } 69 salcianu 1.4 70 salcianu 1.1 71 salcianu 1.1 /** Return the set of instrumented allocation sites. */ 72 salcianu 1.1 public Set getAllocs() { 73 salcianu 1.1 return alloc2int.keySet(); 74 salcianu 1.1 } 75 salcianu 1.1 76 salcianu 1.1 77 salcianu 1.1 /** Return an integer identifying the CALL quad <code>q</code>. */ 78 salcianu 1.1 public int callID(Quad q) { 79 salcianu 1.1 if (!call2int.containsKey(q)) throw new Error("Quad unknown: "+q); 80 salcianu 1.1 return ((Integer) call2int.get(q)).intValue(); 81 salcianu 1.1 } 82 salcianu 1.1 83 salcianu 1.1 /** Return the set of instrumented CALLs. */ 84 salcianu 1.1 public Set getCalls() { 85 salcianu 1.1 return call2int.keySet(); 86 salcianu 1.1 } 87 salcianu 1.1 88 salcianu 1.1 89 salcianu 1.1 /* hard part: the numbering */ 90 salcianu 1.1 private void number(HCode hc, boolean callSites) { 91 salcianu 1.1 if (hc != null) 92 salcianu 1.1 for (Iterator it = hc.getElementsI(); it.hasNext(); ) { 93 salcianu 1.1 Quad q = (Quad) it.next(); 94 salcianu 1.1 if ((q instanceof ANEW) || (q instanceof NEW)) 95 salcianu 1.1 alloc2int.put(q, new Integer(alloc_count++)); 96 salcianu 1.1 else if (callSites && (q instanceof CALL)) 97 salcianu 1.1 call2int.put(q, new Integer(call_count++)); 98 salcianu 1.1 } 99 salcianu 1.1 } 100 salcianu 1.1 101 salcianu 1.1 private int alloc_count = 0; 102 salcianu 1.1 private int call_count = 0; 103 salcianu 1.1 }