예제 #1
0
 /** Print the byte codes */
 public void listing(PrintStream out) {
   out.println("-- listing --");
   for (Instruction inst = first; inst != null; inst = inst.next) {
     out.println(inst.toString());
   }
 }
예제 #2
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;
          }
      }
    }
  }