BaseDuplicateScopeTreesVisitor.java

Go to the documentation of this file.
00001 package edu.rice.cs.hpc.data.experiment.scope.visitors;
00002 
00003 import java.util.Stack;
00004 
00005 import edu.rice.cs.hpc.data.experiment.scope.CallSiteScope;
00006 import edu.rice.cs.hpc.data.experiment.scope.FileScope;
00007 import edu.rice.cs.hpc.data.experiment.scope.GroupScope;
00008 import edu.rice.cs.hpc.data.experiment.scope.LineScope;
00009 import edu.rice.cs.hpc.data.experiment.scope.LoadModuleScope;
00010 import edu.rice.cs.hpc.data.experiment.scope.LoopScope;
00011 import edu.rice.cs.hpc.data.experiment.scope.ProcedureScope;
00012 import edu.rice.cs.hpc.data.experiment.scope.RootScope;
00013 import edu.rice.cs.hpc.data.experiment.scope.RootScopeType;
00014 import edu.rice.cs.hpc.data.experiment.scope.Scope;
00015 import edu.rice.cs.hpc.data.experiment.scope.ScopeVisitType;
00016 import edu.rice.cs.hpc.data.experiment.scope.StatementRangeScope;
00017 
00018 public abstract class BaseDuplicateScopeTreesVisitor implements IScopeVisitor {
00019     protected Stack<Scope> scopeStack;
00020     protected int offsetMetric;
00021     
00022     public BaseDuplicateScopeTreesVisitor(Scope newRoot, int offset) {
00023         scopeStack = new Stack<Scope>();
00024         scopeStack.push(newRoot);
00025         offsetMetric = offset;
00026     }
00027 
00028     
00029     //----------------------------------------------------
00030     // visitor pattern instantiations for each Scope type
00031     //----------------------------------------------------
00032     public void visit(RootScope scope,              ScopeVisitType vt) { 
00033         if (scope.getType() != RootScopeType.Invisible) 
00034             mergeInsert(scope, vt);
00035     }
00036     public void visit(LoadModuleScope scope,        ScopeVisitType vt) { mergeInsert(scope, vt); }
00037     public void visit(FileScope scope,              ScopeVisitType vt) { mergeInsert(scope, vt); }
00038     public void visit(GroupScope scope,             ScopeVisitType vt) { mergeInsert(scope, vt); }
00039     public void visit(Scope scope,                  ScopeVisitType vt) { mergeInsert(scope, vt); }
00040     public void visit(CallSiteScope scope,          ScopeVisitType vt) { mergeInsert(scope, vt); }
00041     public void visit(ProcedureScope scope,         ScopeVisitType vt) { mergeInsert(scope, vt); }
00042     public void visit(LoopScope scope,              ScopeVisitType vt) { mergeInsert(scope, vt); }
00043     public void visit(StatementRangeScope scope,    ScopeVisitType vt) { mergeInsert(scope, vt); }
00044     public void visit(LineScope scope,              ScopeVisitType vt) { mergeInsert(scope, vt); }
00045 
00046     
00047     private void mergeInsert(Scope scope, ScopeVisitType vt) {
00048         if (!scope.isCounterZero())
00049             return;
00050         
00051         if (vt == ScopeVisitType.PreVisit) {
00052             Scope newParent = scopeStack.peek();
00053             
00054             Scope kid = findMatch(newParent, scope);
00055             
00056             Scope newKid = this.addMetricColumns(newParent, kid, scope);
00057             
00058             scopeStack.push(newKid);
00059             
00060         } else { // PostVisit
00061             scopeStack.pop();
00062         }
00063     }
00064 
00065     
00066     /*****
00067      * add child and its metric values if needed
00068      * 
00069      * @param parent
00070      * @param target
00071      * @param source
00072      * @return
00073      */
00074     protected Scope addMetricColumns(Scope parent, Scope target, Scope source) {
00075         
00076         if (target == null) {
00077             // no target scope; create it under parent, and copy over source metrics
00078             target = source.duplicate();
00079             parent.addSubscope(target);
00080             target.setParentScope(parent);
00081             
00082             target.setExperiment(parent.getExperiment());
00083             
00084             if (target instanceof CallSiteScope) {
00085                 ((CallSiteScope)target).getLineScope().setExperiment(parent.getExperiment());
00086                 ((CallSiteScope)target).getProcedureScope().setExperiment(parent.getExperiment());
00087             }
00088         } // else match! just copy source's metrics over to target
00089         
00090         accumulateMetrics(target, source, offsetMetric);
00091 
00092         if (source instanceof CallSiteScope && target instanceof CallSiteScope) {
00093             accumulateMetrics(  ((CallSiteScope)target).getLineScope(),
00094                                 ((CallSiteScope)source).getLineScope(), offsetMetric);
00095         }
00096         
00097         return target;
00098     }
00099     
00100     protected void accumulateMetrics(Scope target, Scope source, int offset) {
00101                 
00102         source.copyMetrics(target, offset);
00103     }
00104 
00105     /****
00106      * Try to find a scope kid that matches with another scope
00107      * to be implemented in the child class: returning null if it doesn't match,
00108      *   or the kid if it matches
00109      * 
00110      * @param parent
00111      * @param toMatch
00112      * @return the kid
00113      */
00114     abstract protected Scope findMatch(Scope parent, Scope toMatch);
00115 
00116 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1