Beispiel #1
0
  /**
   * Does a human-friendly dump of this instance.
   *
   * @param out {@code non-null;} where to dump
   * @param prefix {@code non-null;} prefix to attach to each line of output
   * @param verbose whether to be verbose; verbose output includes lines for zero-size instructions
   *     and explicit constant pool indices
   */
  public void debugPrint(Writer out, String prefix, boolean verbose) {
    IndentingWriter iw = new IndentingWriter(out, 0, prefix);
    int sz = size();

    try {
      for (int i = 0; i < sz; i++) {
        DalvInsn insn = (DalvInsn) get0(i);
        String s;

        if ((insn.codeSize() != 0) || verbose) {
          s = insn.listingString("", 0, verbose);
        } else {
          s = null;
        }

        if (s != null) {
          iw.write(s);
        }
      }

      iw.flush();
    } catch (IOException ex) {
      throw new RuntimeException(ex);
    }
  }
Beispiel #2
0
  /**
   * Writes all the instructions in this instance to the given output destination.
   *
   * @param out {@code non-null;} where to write to
   */
  public void writeTo(AnnotatedOutput out) {
    int startCursor = out.getCursor();
    int sz = size();

    if (out.annotates()) {
      boolean verbose = out.isVerbose();

      for (int i = 0; i < sz; i++) {
        DalvInsn insn = (DalvInsn) get0(i);
        int codeBytes = insn.codeSize() * 2;
        String s;

        if ((codeBytes != 0) || verbose) {
          s = insn.listingString("  ", out.getAnnotationWidth(), true);
        } else {
          s = null;
        }

        if (s != null) {
          out.annotate(codeBytes, s);
        } else if (codeBytes != 0) {
          out.annotate(codeBytes, "");
        }
      }
    }

    for (int i = 0; i < sz; i++) {
      DalvInsn insn = (DalvInsn) get0(i);
      try {
        insn.writeTo(out);
      } catch (RuntimeException ex) {
        throw ExceptionWithContext.withContext(ex, "...while writing " + insn);
      }
    }

    // Sanity check of the amount written.
    int written = (out.getCursor() - startCursor) / 2;
    if (written != codeSize()) {
      throw new RuntimeException(
          "write length mismatch; expected " + codeSize() + " but actually wrote " + written);
    }
  }