1 marinov 1.1.2.1 // Start.java, created Fri Sep 25 02:24:11 1998 by marinov 2 cananian 1.1.2.5 // Copyright (C) 1998 Darko Marinov <marinov@lcs.mit.edu> 3 cananian 1.1.2.5 // Licensed under the terms of the GNU GPL; see COPYING for details. 4 marinov 1.1.2.1 package harpoon.Main; 5 marinov 1.1.2.1 6 cananian 1.1.2.3 import harpoon.ClassFile.CachingCodeFactory; 7 cananian 1.1.2.2 import harpoon.ClassFile.HClass; 8 cananian 1.1.2.2 import harpoon.ClassFile.HCode; 9 cananian 1.1.2.3 import harpoon.ClassFile.HCodeFactory; 10 cananian 1.1.2.2 import harpoon.ClassFile.HMethod; 11 cananian 1.1.2.9 import harpoon.ClassFile.Linker; 12 cananian 1.1.2.9 import harpoon.ClassFile.Loader; 13 cananian 1.1.2.2 import harpoon.IR.Quads.CALL; 14 cananian 1.1.2.2 import harpoon.IR.Quads.Quad; 15 cananian 1.1.2.7 import harpoon.Analysis.ClassHierarchy; 16 cananian 1.1.2.8 import harpoon.Analysis.Quads.QuadClassHierarchy; 17 cananian 1.1.2.2 import harpoon.Analysis.TypeInference.InterProc; 18 marinov 1.1.2.1 import harpoon.Util.Worklist; 19 cananian 1.1.2.12 import harpoon.Util.Collections.WorkSet; 20 cananian 1.1.2.2 21 cananian 1.1.2.9 import java.util.Collections; 22 cananian 1.1.2.10 import java.util.HashSet; 23 cananian 1.3 import java.util.Iterator; 24 cananian 1.1.2.10 import java.util.Set; 25 marinov 1.1.2.1 /** 26 marinov 1.1.2.1 * <code>PrintTypes</code> 27 marinov 1.1.2.1 * 28 marinov 1.1.2.1 * @author Darko Marinov <marinov@lcs.mit.edu> 29 cananian 1.3 * @version $Id: TypesMain.java,v 1.3 2002/09/03 15:18:34 cananian Exp $ 30 marinov 1.1.2.1 */ 31 marinov 1.1.2.1 public class TypesMain extends harpoon.IR.Registration { 32 marinov 1.1.2.1 public static void main(String args[]) { 33 cananian 1.1.2.9 Linker linker = Loader.systemLinker; 34 marinov 1.1.2.1 if (args.length >= 2) { 35 marinov 1.1.2.1 java.io.PrintWriter pw = new java.io.PrintWriter(System.out, true); 36 cananian 1.1.2.3 HCodeFactory hcf = 37 cananian 1.1.2.6 new CachingCodeFactory(harpoon.IR.Quads.QuadSSI.codeFactory()); 38 cananian 1.1.2.9 HClass hcl = linker.forName(args[0]); 39 marinov 1.1.2.1 HMethod hm[] = hcl.getDeclaredMethods(); 40 marinov 1.1.2.1 ClassHierarchy ch = null; 41 cananian 1.1.2.8 harpoon.Analysis.Quads.CallGraph cg = null; 42 marinov 1.1.2.1 InterProc ty = null; 43 marinov 1.1.2.1 boolean multiPass = args[args.length-1].equals("ADDITIONAL"); 44 marinov 1.1.2.1 for (int i=1; i<args.length; i++) { 45 marinov 1.1.2.1 if ((i==args.length-1)&&multiPass) continue; 46 marinov 1.1.2.1 int j; 47 marinov 1.1.2.1 for (j=0; j<hm.length; j++) if (args[i].equals(hm[j].getName())) break; 48 marinov 1.1.2.1 if (j<hm.length) { 49 cananian 1.1.2.3 HCode hco = hcf.convert(hm[j]); 50 marinov 1.1.2.1 if (i==1) { 51 marinov 1.1.2.1 System.out.println("CHA-like started: " + System.currentTimeMillis()); 52 cananian 1.1.2.9 ch = new QuadClassHierarchy(linker, Collections.singleton(hm[j]), hcf); 53 salcianu 1.1.2.11 cg = new harpoon.Analysis.Quads.CallGraphImpl(ch, hcf); 54 marinov 1.1.2.1 System.out.println("CHA-like finished: " + System.currentTimeMillis()); 55 marinov 1.1.2.1 if (multiPass) { 56 marinov 1.1.2.1 System.out.println("another CHA-like started: " + System.currentTimeMillis()); 57 cananian 1.1.2.9 ClassHierarchy ch1 = new QuadClassHierarchy(linker, Collections.singleton(hm[j]), hcf); 58 marinov 1.1.2.1 System.out.println("another CHA-like finished: " + System.currentTimeMillis()); 59 marinov 1.1.2.1 System.out.println("yet another CHA-like started: " + System.currentTimeMillis()); 60 cananian 1.1.2.9 ClassHierarchy ch2 = new QuadClassHierarchy(linker, Collections.singleton(hm[j]), hcf); 61 marinov 1.1.2.1 System.out.println("yet another CHA-like finished: " + System.currentTimeMillis()); 62 marinov 1.1.2.1 } 63 marinov 1.1.2.1 System.out.println("0-CFA-like started: " + System.currentTimeMillis()); 64 cananian 1.1.2.3 ty = new InterProc(hco, ch, hcf); 65 marinov 1.1.2.1 ty.analyze(); 66 marinov 1.1.2.1 System.out.println("0-CFA-like finished: " + System.currentTimeMillis()); 67 marinov 1.1.2.1 if (multiPass) { 68 marinov 1.1.2.1 System.out.println("another 0-CFA-like started: " + System.currentTimeMillis()); 69 cananian 1.1.2.3 InterProc ty1 = new InterProc(hco, ch, hcf); ty1.analyze(); 70 marinov 1.1.2.1 System.out.println("another 0-CFA-like finished: " + System.currentTimeMillis()); 71 marinov 1.1.2.1 } 72 marinov 1.1.2.1 } 73 marinov 1.1.2.1 for (int ana=0; ana<=1; ana++) { 74 marinov 1.1.2.1 if (ana==0) System.out.println("******** CHA-like"); 75 marinov 1.1.2.1 else System.out.println("******** 0-CFA-like"); 76 pnkfelix 1.1.2.4 Set fi = new HashSet(); 77 cananian 1.1.2.10 Worklist wl = new WorkSet(); 78 marinov 1.1.2.1 wl.push(hm[j]); 79 marinov 1.1.2.1 while (!wl.isEmpty()) { 80 marinov 1.1.2.1 HMethod m = (HMethod)wl.pull(); 81 marinov 1.1.2.1 System.out.print(m.getDeclaringClass() + " " + m.getName() + 82 marinov 1.1.2.1 " (" + m.getParameterTypes().length + ") calls: "); 83 cananian 1.1.2.3 HCode hc = hcf.convert(m); 84 marinov 1.1.2.1 if (hc!=null) { 85 marinov 1.1.2.1 Quad forLast = null; 86 cananian 1.3 Iterator e = hc.getElementsI(); 87 cananian 1.3 while (e.hasNext()) { 88 cananian 1.3 Quad q = (forLast==null) ? (Quad) e.next() : forLast; 89 marinov 1.1.2.1 if (!(q instanceof CALL)) continue; 90 cananian 1.3 while (e.hasNext()) { 91 cananian 1.3 forLast = (Quad) e.next(); 92 marinov 1.1.2.1 if (forLast instanceof CALL) break; 93 marinov 1.1.2.1 } 94 marinov 1.1.2.1 CALL cs = (CALL)q; 95 marinov 1.1.2.1 HMethod[] mm; 96 marinov 1.1.2.1 if (ana==0) mm = cg.calls(m, cs); 97 cananian 1.3 else mm = ty.calls(m, cs, !e.hasNext()); 98 marinov 1.1.2.1 System.out.print(mm.length + " "); 99 marinov 1.1.2.1 for (int k=0; k<mm.length; k++) 100 cananian 1.1.2.10 if (!fi.contains(mm[k])) { fi.add(mm[k]); wl.push(mm[k]); } 101 marinov 1.1.2.1 } 102 marinov 1.1.2.1 System.out.println(); 103 marinov 1.1.2.1 } else System.out.println("0 !"); 104 marinov 1.1.2.1 } 105 marinov 1.1.2.1 if (ana==0) System.out.println("******** \\CHA-like"); 106 marinov 1.1.2.1 else System.out.println("******** \\0-CFA-like"); 107 marinov 1.1.2.1 } 108 marinov 1.1.2.1 //SetHClass s = ty.getReturnType(hco); 109 marinov 1.1.2.1 //System.out.println("RETURN TYPE=" + s); 110 marinov 1.1.2.1 } else System.err.println("method not found"); 111 marinov 1.1.2.1 } 112 marinov 1.1.2.1 pw.close(); 113 marinov 1.1.2.1 } else System.err.println("usage: class method[s] [ADDITIONAL]"); 114 marinov 1.1.2.1 } 115 cananian 1.2 }