/** * 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; }
/** 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); }
JLCInfo getJLCInfo(String typeSystemKey) { // Check if already set. JLCInfo jlc = (JLCInfo) jlcInfoCache.get(typeSystemKey); if (jlc != null) { return jlc; } jlc = new JLCInfo(); jlcInfoCache.put(typeSystemKey, jlc); try { int mask = 0; for (int i = 0; i < fields.length; i++) { if (fields[i].name().equals("jlc$SourceLastModified$" + typeSystemKey)) { jlc.sourceLastModified = fields[i].getLong(); mask |= 1; } else if (fields[i].name().equals("jlc$CompilerVersion$" + typeSystemKey)) { jlc.compilerVersion = fields[i].getString(); mask |= 2; } else if (fields[i].name().equals("jlc$ClassType$" + typeSystemKey)) { // there is encoded class type information. StringBuffer encodedClassTypeInfo = new StringBuffer(fields[i].getString()); // check to see if there are more fields. int seeking = 1; boolean found; do { found = false; String suffix = ("$" + seeking); String seekingFieldName = "jlc$ClassType$" + typeSystemKey + suffix; for (int j = 0; j < fields.length; j++) { if (fields[j].name().equals(seekingFieldName)) { encodedClassTypeInfo.append(fields[j].getString()); found = true; seeking++; break; } } } while (found); jlc.encodedClassType = encodedClassTypeInfo.toString(); mask |= 4; } } if (mask != 7) { // Not all the information is there. Reset to default. jlc.sourceLastModified = 0; jlc.compilerVersion = null; jlc.encodedClassType = null; } } catch (SemanticException e) { jlc.sourceLastModified = 0; jlc.compilerVersion = null; jlc.encodedClassType = null; } return jlc; }
/** Adds a dependency from the current job to the given Source. */ public void addDependencyToCurrentJob(Source s) { if (s == null) return; if (currentJob != null) { Object o = jobs.get(s); if (o != COMPLETED_JOB) { if (Report.should_report(Report.frontend, 2)) { Report.report(2, "Adding dependency from " + currentJob.source() + " to " + s); } currentJob.sourceJob().addDependency(s); } } else { throw new InternalCompilerError("No current job!"); } }
/** 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; }
/** * Before running <code>Pass pass</code> on <code>SourceJob job</code> make sure that all * appropriate scheduling invariants are satisfied, to ensure that all passes of other jobs that * <code>job</code> depends on will have already been done. */ protected void enforceInvariants(Job job, Pass pass) throws CyclicDependencyException { SourceJob srcJob = job.sourceJob(); if (srcJob == null) { return; } BarrierPass lastBarrier = srcJob.lastBarrier(); if (lastBarrier != null) { // make sure that _all_ dependent jobs have completed at least up to // the last barrier (not just children). // // Ideally the invariant should be that only the source jobs that // job _depends on_ should be brought up to the last barrier. // This is work to be done in the future... List allDependentSrcs = new ArrayList(srcJob.dependencies()); Iterator i = allDependentSrcs.iterator(); while (i.hasNext()) { Source s = (Source) i.next(); Object o = jobs.get(s); if (o == COMPLETED_JOB) continue; if (o == null) { throw new InternalCompilerError("Unknown source " + s); } SourceJob sj = (SourceJob) o; if (sj.pending(lastBarrier.id())) { // Make the job run up to the last barrier. // We ignore the return result, since even if the job // fails, we will keep on going and see // how far we get... if (Report.should_report(Report.frontend, 3)) { Report.report(3, "Running " + sj + " to " + lastBarrier.id() + " for " + srcJob); } runToPass(sj, lastBarrier.id()); } } } if (pass instanceof GlobalBarrierPass) { // need to make sure that _all_ jobs have completed just up to // this global barrier. // If we hit a cyclic dependency, ignore it and run the other // jobs up to that pass. Then try again to run the cyclic // pass. If we hit the cycle again for the same job, stop. LinkedList barrierWorklist = new LinkedList(jobs.values()); while (!barrierWorklist.isEmpty()) { Object o = barrierWorklist.removeFirst(); if (o == COMPLETED_JOB) continue; SourceJob sj = (SourceJob) o; if (sj.completed(pass.id()) || sj.nextPass() == sj.passByID(pass.id())) { // the source job has either done this global pass // (which is possible if the job was loaded late in the // game), or is right up to the global barrier. continue; } // Make the job run up to just before the global barrier. // We ignore the return result, since even if the job // fails, we will keep on going and see // how far we get... Pass beforeGlobal = sj.getPreviousTo(pass.id()); if (Report.should_report(Report.frontend, 3)) { Report.report(3, "Running " + sj + " to " + beforeGlobal.id() + " for " + srcJob); } // Don't use runToPass, since that catches the // CyclicDependencyException that we should report // back to the caller. while (!sj.pendingPasses().isEmpty()) { Pass p = (Pass) sj.pendingPasses().get(0); runPass(sj, p); if (p == beforeGlobal) { break; } } } } }