/** * Add a new <code>SourceJob</code> for the <code>Source source</code>, with AST <code>ast</code>. * A new job will be created if needed. If the <code>Source source</code> has already been * processed, and its job discarded to release resources, then <code>null</code> will be returned. */ public SourceJob addJob(Source source, Node ast) { Object o = jobs.get(source); SourceJob job = null; if (o == COMPLETED_JOB) { // the job has already been completed. // We don't need to add a job return null; } else if (o == null) { // No appropriate job yet exists, we will create one. job = this.createSourceJob(source, ast); // record the job in the map and the worklist. jobs.put(source, job); worklist.addLast(job); if (Report.should_report(Report.frontend, 3)) { Report.report(3, "Adding job for " + source + " at the " + "request of job " + currentJob); } } else { job = (SourceJob) o; } // if the current source job caused this job to load, record the // dependency. if (currentJob instanceof SourceJob) { ((SourceJob) currentJob).addDependency(source); } return job; }
/** * Run all jobs in the work list (and any children they have) to completion. This method returns * <code>true</code> if all jobs were successfully completed. If all jobs were successfully * completed, then the worklist will be empty. * * <p>The scheduling of <code>Job</code>s uses two methods to maintain scheduling invariants: * <code>selectJobFromWorklist</code> selects a <code>SourceJob</code> from <code>worklist</code> * (a list of jobs that still need to be processed); <code>enforceInvariants</code> is called * before a pass is performed on a <code>SourceJob</code> and is responsible for ensuring all * dependencies are satisfied before the pass proceeds, i.e. enforcing any scheduling invariants. */ public boolean runToCompletion() { boolean okay = true; while (okay && !worklist.isEmpty()) { SourceJob job = selectJobFromWorklist(); if (Report.should_report(Report.frontend, 1)) { Report.report(1, "Running job " + job); } okay &= runAllPasses(job); if (job.completed()) { // the job has finished. Let's remove it from the map so it // can be garbage collected, and free up the AST. jobs.put(job.source(), COMPLETED_JOB); if (Report.should_report(Report.frontend, 1)) { Report.report(1, "Completed job " + job); } } else { // the job is not yet completed (although, it really // should be...) if (Report.should_report(Report.frontend, 1)) { Report.report(1, "Failed to complete job " + job); } worklist.add(job); } } if (Report.should_report(Report.frontend, 1)) Report.report(1, "Finished all passes -- " + (okay ? "okay" : "failed")); return okay; }
/** Perform the appropriate flow operations for declaration of a local variable */ protected Map flowLocalDecl( DataFlowItem inItem, FlowGraph graph, LocalDecl ld, Set succEdgeKeys) { Map m = new HashMap(inItem.initStatus); MinMaxInitCount initCount = (MinMaxInitCount) m.get(ld.localInstance()); // if (initCount == null) { if (ld.init() != null) { // declaration of local var with initialization. initCount = new MinMaxInitCount(InitCount.ONE, InitCount.ONE); } else { // declaration of local var with no initialization. initCount = new MinMaxInitCount(InitCount.ZERO, InitCount.ZERO); } m.put(ld.localInstance(), initCount); // } // else { // the initCount is not null. We now have a problem. Why is the // initCount not null? Has this variable been assigned in its own // initialization, or is this a declaration inside a loop body? // XXX@@@ THIS IS A BUG THAT NEEDS TO BE FIXED. // Currently, the declaration "final int i = (i=5);" will // not be rejected, as we cannot distinguish between that and // "while (true) {final int i = 4;}" // } // record the fact that we have seen a local declaration currCBI.localDeclarations.add(ld.localInstance()); return itemToMap(new DataFlowItem(m), succEdgeKeys); }
/** * The confluence operator is essentially the union of all of the inItems. However, if two or more * of the initCount maps from the inItems each have a MinMaxInitCounts entry for the same * VarInstance, the conflict must be resolved, by using the minimum of all mins and the maximum of * all maxs. */ public Item confluence(List inItems, Term node, FlowGraph graph) { // Resolve any conflicts pairwise. Iterator iter = inItems.iterator(); Map m = null; while (iter.hasNext()) { Item itm = (Item) iter.next(); if (itm == BOTTOM) continue; if (m == null) { m = new HashMap(((DataFlowItem) itm).initStatus); } else { Map n = ((DataFlowItem) itm).initStatus; for (Iterator iter2 = n.entrySet().iterator(); iter2.hasNext(); ) { Map.Entry entry = (Map.Entry) iter2.next(); VarInstance v = (VarInstance) entry.getKey(); MinMaxInitCount initCount1 = (MinMaxInitCount) m.get(v); MinMaxInitCount initCount2 = (MinMaxInitCount) entry.getValue(); m.put(v, MinMaxInitCount.join(initCount1, initCount2)); } } } if (m == null) return BOTTOM; return new DataFlowItem(m); }
/** Initialize the <code>passes</code> field and the <code>passMap</code> field. */ protected void init() { passes = new ArrayList(getPasses()); passMap = new HashMap(); for (int i = 0; i < passes.size(); i++) { Pass pass = (Pass) passes.get(i); passMap.put(pass.id(), new Integer(i)); } }
/** Perform the appropriate flow operations for declaration of a formal parameter */ protected Map flowFormal(DataFlowItem inItem, FlowGraph graph, Formal f, Set succEdgeKeys) { Map m = new HashMap(inItem.initStatus); // a formal argument is always defined. m.put(f.localInstance(), new MinMaxInitCount(InitCount.ONE, InitCount.ONE)); // record the fact that we have seen the formal declaration currCBI.localDeclarations.add(f.localInstance()); return itemToMap(new DataFlowItem(m), succEdgeKeys); }
public Map flow(Item in, FlowGraph graph, Term n, Set succEdgeKeys) { if (in == DataFlowItem.NOT_REACHABLE) { return itemToMap(in, succEdgeKeys); } // in is either REACHABLE or REACHABLE_EX_ONLY. // return a map where all exception edges are REACHABLE_EX_ONLY, // and all non-exception edges are REACHABLE. Map m = itemToMap(DataFlowItem.REACHABLE_EX_ONLY, succEdgeKeys); if (succEdgeKeys.contains(FlowGraph.EDGE_KEY_OTHER)) { m.put(FlowGraph.EDGE_KEY_OTHER, DataFlowItem.REACHABLE); } if (succEdgeKeys.contains(FlowGraph.EDGE_KEY_TRUE)) { m.put(FlowGraph.EDGE_KEY_TRUE, DataFlowItem.REACHABLE); } if (succEdgeKeys.contains(FlowGraph.EDGE_KEY_FALSE)) { m.put(FlowGraph.EDGE_KEY_FALSE, DataFlowItem.REACHABLE); } return m; }
/** Perform the appropriate flow operations for assignment to a local variable */ protected Map flowLocalAssign( DataFlowItem inItem, FlowGraph graph, LocalAssign a, Set succEdgeKeys) { Local l = (Local) a.left(); Map m = new HashMap(inItem.initStatus); MinMaxInitCount initCount = (MinMaxInitCount) m.get(l.localInstance()); // initcount could be null if the local is defined in the outer // class, or if we have not yet seen its declaration (i.e. the // local is used in its own initialization) if (initCount == null) { initCount = new MinMaxInitCount(InitCount.ZERO, InitCount.ZERO); } initCount = new MinMaxInitCount(initCount.getMin().increment(), initCount.getMax().increment()); m.put(l.localInstance(), initCount); return itemToMap(new DataFlowItem(m), succEdgeKeys); }
/** Perform the appropriate flow operations for assignment to a field */ protected Map flowFieldAssign( DataFlowItem inItem, FlowGraph graph, FieldAssign a, Set succEdgeKeys) { Field f = (Field) a.left(); FieldInstance fi = f.fieldInstance(); if (fi.flags().isFinal() && isFieldsTargetAppropriate(f)) { // this field is final and the target for this field is // appropriate for what we are interested in. Map m = new HashMap(inItem.initStatus); MinMaxInitCount initCount = (MinMaxInitCount) m.get(fi); // initCount may be null if the field is defined in an // outer class. if (initCount != null) { initCount = new MinMaxInitCount(initCount.getMin().increment(), initCount.getMax().increment()); m.put(fi, initCount); return itemToMap(new DataFlowItem(m), succEdgeKeys); } } return null; }