예제 #1
0
  /** Eliminate instructions that are not reached */
  boolean eliminate() {
    boolean change = false;
    Instruction prev = first;

    for (Instruction inst = first.next; inst != null; inst = inst.next) {
      if (inst.pc != NOTREACHED) {
        prev.next = inst;
        prev = inst;
        inst.pc = NOTREACHED;
      } else {
        change = true;
      }
    }
    first.pc = NOTREACHED;
    prev.next = null;
    return change;
  }
예제 #2
0
  /** Optimize instructions and mark those that can be reached */
  void optimize(Environment env, Label lbl) {
    lbl.pc = REACHED;

    for (Instruction inst = lbl.next; inst != null; inst = inst.next) {
      switch (inst.pc) {
        case NOTREACHED:
          inst.optimize(env);
          inst.pc = REACHED;
          break;
        case REACHED:
          return;
        case NEEDED:
          break;
      }

      switch (inst.opc) {
        case opc_label:
        case opc_dead:
          if (inst.pc == REACHED) {
            inst.pc = NOTREACHED;
          }
          break;

        case opc_ifeq:
        case opc_ifne:
        case opc_ifgt:
        case opc_ifge:
        case opc_iflt:
        case opc_ifle:
        case opc_if_icmpeq:
        case opc_if_icmpne:
        case opc_if_icmpgt:
        case opc_if_icmpge:
        case opc_if_icmplt:
        case opc_if_icmple:
        case opc_if_acmpeq:
        case opc_if_acmpne:
        case opc_ifnull:
        case opc_ifnonnull:
          optimize(env, (Label) inst.value);
          break;

        case opc_goto:
          optimize(env, (Label) inst.value);
          return;

        case opc_jsr:
          optimize(env, (Label) inst.value);
          break;

        case opc_ret:
        case opc_return:
        case opc_ireturn:
        case opc_lreturn:
        case opc_freturn:
        case opc_dreturn:
        case opc_areturn:
        case opc_athrow:
          return;

        case opc_tableswitch:
        case opc_lookupswitch:
          {
            SwitchData sw = (SwitchData) inst.value;
            optimize(env, sw.defaultLabel);
            for (Enumeration e = sw.tab.elements(); e.hasMoreElements(); ) {
              optimize(env, (Label) e.nextElement());
            }
            return;
          }

        case opc_try:
          {
            TryData td = (TryData) inst.value;
            td.getEndLabel().pc = NEEDED;
            for (Enumeration e = td.catches.elements(); e.hasMoreElements(); ) {
              CatchData cd = (CatchData) e.nextElement();
              optimize(env, cd.getLabel());
            }
            break;
          }
      }
    }
  }
예제 #3
0
  /** Determine stack size, count local variables */
  void balance(Label lbl, int depth) {
    for (Instruction inst = lbl; inst != null; inst = inst.next) {
      // Environment.debugOutput(inst.toString() + ": " + depth + " => " +
      //                                 (depth + inst.balance()));
      depth += inst.balance();
      if (depth < 0) {
        throw new CompilerError("stack under flow: " + inst.toString() + " = " + depth);
      }
      if (depth > maxdepth) {
        maxdepth = depth;
      }
      switch (inst.opc) {
        case opc_label:
          lbl = (Label) inst;
          if (inst.pc == REACHED) {
            if (lbl.depth != depth) {
              throw new CompilerError(
                  "stack depth error " + depth + "/" + lbl.depth + ": " + inst.toString());
            }
            return;
          }
          lbl.pc = REACHED;
          lbl.depth = depth;
          break;

        case opc_ifeq:
        case opc_ifne:
        case opc_ifgt:
        case opc_ifge:
        case opc_iflt:
        case opc_ifle:
        case opc_if_icmpeq:
        case opc_if_icmpne:
        case opc_if_icmpgt:
        case opc_if_icmpge:
        case opc_if_icmplt:
        case opc_if_icmple:
        case opc_if_acmpeq:
        case opc_if_acmpne:
        case opc_ifnull:
        case opc_ifnonnull:
          balance((Label) inst.value, depth);
          break;

        case opc_goto:
          balance((Label) inst.value, depth);
          return;

        case opc_jsr:
          balance((Label) inst.value, depth + 1);
          break;

        case opc_ret:
        case opc_return:
        case opc_ireturn:
        case opc_lreturn:
        case opc_freturn:
        case opc_dreturn:
        case opc_areturn:
        case opc_athrow:
          return;

        case opc_iload:
        case opc_fload:
        case opc_aload:
        case opc_istore:
        case opc_fstore:
        case opc_astore:
          {
            int v =
                ((inst.value instanceof Number)
                        ? ((Number) inst.value).intValue()
                        : ((LocalVariable) inst.value).slot)
                    + 1;
            if (v > maxvar) maxvar = v;
            break;
          }

        case opc_lload:
        case opc_dload:
        case opc_lstore:
        case opc_dstore:
          {
            int v =
                ((inst.value instanceof Number)
                        ? ((Number) inst.value).intValue()
                        : ((LocalVariable) inst.value).slot)
                    + 2;
            if (v > maxvar) maxvar = v;
            break;
          }

        case opc_iinc:
          {
            int v = ((int[]) inst.value)[0] + 1;
            if (v > maxvar) maxvar = v + 1;
            break;
          }

        case opc_tableswitch:
        case opc_lookupswitch:
          {
            SwitchData sw = (SwitchData) inst.value;
            balance(sw.defaultLabel, depth);
            for (Enumeration e = sw.tab.elements(); e.hasMoreElements(); ) {
              balance((Label) e.nextElement(), depth);
            }
            return;
          }

        case opc_try:
          {
            TryData td = (TryData) inst.value;
            for (Enumeration e = td.catches.elements(); e.hasMoreElements(); ) {
              CatchData cd = (CatchData) e.nextElement();
              balance(cd.getLabel(), depth + 1);
            }
            break;
          }
      }
    }
  }