/** Run a job up to the <code>goal</code> pass. */
  public boolean runToPass(Job job, Pass goal) {
    if (Report.should_report(Report.frontend, 1))
      Report.report(1, "Running " + job + " to pass " + goal);

    while (!job.pendingPasses().isEmpty()) {
      Pass pass = (Pass) job.pendingPasses().get(0);

      try {
        runPass(job, pass);
      } catch (CyclicDependencyException e) {
        // cause the pass to fail.
        job.finishPass(pass, false);
      }

      if (pass == goal) {
        break;
      }
    }

    if (job.completed()) {
      if (Report.should_report(Report.frontend, 1)) Report.report(1, "Job " + job + " completed");
    }

    return job.status();
  }
  /**
   * Run the pass <code>pass</code> on the job. Before running the pass on the job, if the job is a
   * <code>SourceJob</code>, then this method will ensure that the scheduling invariants are
   * enforced by calling <code>enforceInvariants</code>.
   */
  protected void runPass(Job job, Pass pass) throws CyclicDependencyException {
    // make sure that all scheduling invariants are satisfied before running
    // the next pass. We may thus execute some other passes on other
    // jobs running the given pass.
    try {
      enforceInvariants(job, pass);
    } catch (CyclicDependencyException e) {
      // A job that depends on this job is still running
      // an earlier pass.  We cannot continue this pass,
      // but we can just silently fail since the job we're
      // that depends on this one will eventually try
      // to run this pass again when it reaches a barrier.
      return;
    }

    if (getOptions().disable_passes.contains(pass.name())) {
      if (Report.should_report(Report.frontend, 1)) Report.report(1, "Skipping pass " + pass);
      job.finishPass(pass, true);
      return;
    }

    if (Report.should_report(Report.frontend, 1))
      Report.report(1, "Trying to run pass " + pass + " in " + job);

    if (job.isRunning()) {
      // We're currently running.  We can't reach the goal.
      throw new CyclicDependencyException(job + " cannot reach pass " + pass);
    }

    pass.resetTimers();

    boolean result = false;
    if (job.status()) {
      Job oldCurrentJob = this.currentJob;
      this.currentJob = job;
      Report.should_report.push(pass.name());

      // Stop the timer on the old pass. */
      Pass oldPass = oldCurrentJob != null ? oldCurrentJob.runningPass() : null;

      if (oldPass != null) {
        oldPass.toggleTimers(true);
      }

      job.setRunningPass(pass);
      pass.toggleTimers(false);

      result = pass.run();

      pass.toggleTimers(false);
      job.setRunningPass(null);

      Report.should_report.pop();
      this.currentJob = oldCurrentJob;

      // Restart the timer on the old pass. */
      if (oldPass != null) {
        oldPass.toggleTimers(true);
      }

      // pretty-print this pass if we need to.
      if (getOptions().print_ast.contains(pass.name())) {
        System.err.println("--------------------------------" + "--------------------------------");
        System.err.println("Pretty-printing AST for " + job + " after " + pass.name());

        PrettyPrinter pp = new PrettyPrinter();
        pp.printAst(job.ast(), new CodeWriter(System.err, 78));
      }

      // dump this pass if we need to.
      if (getOptions().dump_ast.contains(pass.name())) {
        System.err.println("--------------------------------" + "--------------------------------");
        System.err.println("Dumping AST for " + job + " after " + pass.name());

        NodeVisitor dumper = new DumpAst(new CodeWriter(System.err, 78));
        dumper = dumper.begin();
        job.ast().visit(dumper);
        dumper.finish();
      }

      // This seems to work around a VM bug on linux with JDK
      // 1.4.0.  The mark-sweep collector will sometimes crash.
      // Running the GC explicitly here makes the bug go away.
      // If this fails, maybe run with bigger heap.

      // System.gc();
    }

    Stats stats = getStats();
    stats.accumPassTimes(pass.id(), pass.inclusiveTime(), pass.exclusiveTime());

    if (Report.should_report(Report.time, 2)) {
      Report.report(
          2,
          "Finished "
              + pass
              + " status="
              + str(result)
              + " inclusive_time="
              + pass.inclusiveTime()
              + " exclusive_time="
              + pass.exclusiveTime());
    } else if (Report.should_report(Report.frontend, 1)) {
      Report.report(1, "Finished " + pass + " status=" + str(result));
    }

    job.finishPass(pass, result);
  }