1 cananian 1.1.2.2 // QuantaChecker.java, created Fri Jun 8 14:02:02 2001 by wingman 2 cananian 1.1.2.2 // Copyright (C) 2001 Bryan Fink <wingman@mit.edu> 3 cananian 1.1.2.2 // Licensed under the terms of the GNU GPL; see COPYING for details. 4 wingman 1.1.2.1 package harpoon.Analysis.Realtime; 5 wingman 1.1.2.1 6 wingman 1.1.2.1 import harpoon.Analysis.Transformation.MethodMutator; 7 wingman 1.1.2.1 import harpoon.Backend.Generic.Frame; 8 wingman 1.1.2.1 import harpoon.ClassFile.HCodeFactory; 9 wingman 1.1.2.1 import harpoon.ClassFile.HCodeElement; 10 wingman 1.1.2.1 import harpoon.ClassFile.HCode; 11 wingman 1.1.2.1 import harpoon.ClassFile.HCodeAndMaps; 12 wingman 1.1.2.1 import harpoon.ClassFile.HClass; 13 wingman 1.1.2.1 import harpoon.ClassFile.Linker; 14 wingman 1.1.2.1 import harpoon.ClassFile.HField; 15 wingman 1.1.2.1 import harpoon.ClassFile.HMethod; 16 wingman 1.1.2.1 17 wbeebee 1.6 import harpoon.IR.Tree.CanonicalTreeCode; 18 wingman 1.1.2.1 import harpoon.IR.Tree.TreeVisitor; 19 wingman 1.1.2.1 import harpoon.IR.Tree.TreeFactory; 20 wingman 1.1.2.1 import harpoon.IR.Tree.DerivationGenerator; 21 wingman 1.1.2.1 import harpoon.IR.Tree.METHOD; 22 wingman 1.1.2.1 import harpoon.IR.Tree.CJUMP; 23 wingman 1.1.2.1 import harpoon.IR.Tree.MEM; 24 wingman 1.1.2.1 import harpoon.IR.Tree.NAME; 25 wingman 1.1.2.1 import harpoon.IR.Tree.LABEL; 26 wingman 1.1.2.1 import harpoon.IR.Tree.ESEQ; 27 wingman 1.1.2.1 import harpoon.IR.Tree.NATIVECALL; 28 wingman 1.1.2.1 import harpoon.IR.Tree.CONST; 29 wingman 1.1.2.1 import harpoon.IR.Tree.SEQ; 30 wingman 1.1.2.1 import harpoon.IR.Tree.CALL; 31 wingman 1.1.2.1 import harpoon.IR.Tree.TEMP; 32 wingman 1.1.2.1 import harpoon.IR.Tree.Tree; 33 wingman 1.1.2.1 import harpoon.IR.Tree.Stm; 34 wingman 1.1.2.1 import harpoon.IR.Tree.Exp; 35 dumi 1.5 import harpoon.IR.Tree.ExpList; 36 wbeebee 1.1.2.3 import harpoon.IR.Tree.TreeCode; 37 wingman 1.1.2.1 import harpoon.IR.Tree.Typed; 38 wingman 1.1.2.1 import harpoon.IR.Tree.Type; 39 wingman 1.1.2.1 40 wingman 1.1.2.1 import harpoon.Temp.Label; 41 wingman 1.1.2.1 import harpoon.Temp.Temp; 42 wingman 1.1.2.1 43 cananian 1.10 import net.cscott.jutil.SnapshotIterator; 44 cananian 1.8 45 wbeebee 1.1.2.3 import harpoon.Util.Util; 46 wbeebee 1.1.2.3 47 wbeebee 1.7 import java.util.Iterator; 48 wingman 1.1.2.1 import java.util.List; 49 wingman 1.1.2.1 import java.util.ArrayList; 50 wingman 1.1.2.1 import java.io.PrintWriter; 51 wingman 1.1.2.1 52 cananian 1.1.2.2 /** 53 cananian 1.1.2.2 * @author Bryan Fink <wingman@mit.edu> 54 cananian 1.10 * @version $Id: QuantaChecker.java,v 1.10 2004/02/08 01:53:47 cananian Exp $ 55 cananian 1.1.2.2 */ 56 wingman 1.1.2.1 public class QuantaChecker extends MethodMutator 57 wingman 1.1.2.1 { 58 wbeebee 1.1.2.3 public QuantaChecker(HCodeFactory hcf) 59 wingman 1.1.2.1 { 60 wingman 1.1.2.1 super(hcf); 61 wbeebee 1.6 assert hcf.getCodeName().equals(CanonicalTreeCode.codename) : 62 wbeebee 1.6 "QuantaChecker only works on CanonicalTree form"; 63 wingman 1.1.2.1 } 64 wingman 1.1.2.1 65 wingman 1.1.2.1 protected HCode mutateHCode(HCodeAndMaps input) { 66 wingman 1.1.2.1 final HCode hc = input.hcode(); 67 wingman 1.1.2.1 HClass hclass = hc.getMethod().getDeclaringClass(); 68 wingman 1.1.2.1 if (hc == null) { 69 wingman 1.1.2.1 return hc; 70 wingman 1.1.2.1 } 71 wbeebee 1.1.2.3 final Linker linker = hclass.getLinker(); 72 dumi 1.5 73 wingman 1.1.2.1 TreeVisitor visitor = new TreeVisitor() { 74 wingman 1.1.2.1 public void visit(METHOD e) 75 wingman 1.1.2.1 { 76 wingman 1.1.2.1 TreeFactory tf = e.getFactory(); 77 wingman 1.1.2.1 Stm stuff = addJump(linker, hc, tf, e); 78 wingman 1.1.2.1 //make placebo label to hold parent of e while we mess with it 79 wingman 1.1.2.1 LABEL newLabel = new LABEL(tf, e, new Label("hackLabel"), true); 80 wingman 1.1.2.1 //replace e in the tree with label 81 wingman 1.1.2.1 e.replace(newLabel); 82 wingman 1.1.2.1 //make a new sequence of e and "stuff" (the test and jump) 83 wingman 1.1.2.1 SEQ newSeq = new SEQ(tf, e, e, stuff); 84 wingman 1.1.2.1 //replace the label with e to put it back in the tree 85 wingman 1.1.2.1 newLabel.replace(newSeq); 86 wingman 1.1.2.1 } 87 wingman 1.1.2.1 88 wingman 1.1.2.1 //put in loops later 89 wingman 1.1.2.1 90 wingman 1.1.2.1 public void visit(Tree e) 91 wingman 1.1.2.1 {} 92 wingman 1.1.2.1 }; 93 wingman 1.1.2.1 94 cananian 1.8 // note that we use a SnapshotIterator so that ti is 'static'; 95 cananian 1.8 // i.e. the underlying list of elements to iterate over does not 96 cananian 1.8 // change as the visitor mutates the hcode. 97 cananian 1.8 Iterator ti = new SnapshotIterator(hc.getElementsI()); 98 wbeebee 1.7 while (ti.hasNext()) ((Tree)(ti.next())).accept(visitor); 99 wingman 1.1.2.1 100 wingman 1.1.2.1 return hc; 101 wingman 1.1.2.1 } 102 wingman 1.1.2.1 103 wingman 1.1.2.1 protected Stm addJump(Linker linker, HCode hc, TreeFactory tf, Stm e) 104 wingman 1.1.2.1 { 105 wingman 1.1.2.1 106 wingman 1.1.2.1 harpoon.IR.Tree.Code code = (harpoon.IR.Tree.Code) hc; //cast to Code 107 wingman 1.1.2.1 DerivationGenerator dg = (DerivationGenerator) code.getTreeDerivation(); //get DerivationGenerator 108 wingman 1.1.2.1 109 wingman 1.1.2.1 NATIVECALL handleFlag = addCheck(tf, e, dg); //get call to C function 110 wingman 1.1.2.1 111 wingman 1.1.2.1 List stmlist = new ArrayList(); //create stmt list 112 wingman 1.1.2.1 stmlist.add(handleFlag); //add c func call 113 wingman 1.1.2.1 return Stm.toStm(stmlist); //create final stmt 114 wingman 1.1.2.1 } 115 wingman 1.1.2.1 116 wingman 1.1.2.1 protected NATIVECALL addCheck(TreeFactory tf, HCodeElement source, 117 wingman 1.1.2.1 DerivationGenerator dg) 118 wingman 1.1.2.1 { 119 cananian 1.1.2.4 Label func = new Label(tf.getFrame().getRuntime().getNameMap() 120 wbeebee 1.9 .c_function_name("CheckTimeSwitch")); 121 wingman 1.1.2.1 return new NATIVECALL 122 wingman 1.1.2.1 (tf, source, 123 wingman 1.1.2.1 null, 124 wingman 1.1.2.1 (NAME) 125 wbeebee 1.9 DECLARE(dg, HClass.Void, 126 wingman 1.1.2.1 new NAME(tf, source, func)), 127 wbeebee 1.9 null); 128 wingman 1.1.2.1 } 129 wingman 1.1.2.1 protected static Exp DECLARE(DerivationGenerator dg, HClass hc, Exp exp) { 130 wingman 1.1.2.1 if (dg!=null) dg.putType(exp, hc); 131 wingman 1.1.2.1 return exp; 132 wingman 1.1.2.1 } 133 wingman 1.1.2.1 protected static Exp DECLARE(DerivationGenerator dg, HClass hc, Temp t, 134 wingman 1.1.2.1 Exp exp) { 135 wingman 1.1.2.1 if (dg!=null) dg.putTypeAndTemp(exp, hc, t); 136 wingman 1.1.2.1 return exp; 137 wingman 1.1.2.1 } 138 wingman 1.1.2.1 139 wingman 1.1.2.1 140 cananian 1.2 } 141 dumi 1.5