示例#1
0
  CFG createCFG(String className) throws ClassNotFoundException {
    CFG cfg = new CFG();
    JavaClass jc = Repository.lookupClass(className);
    ClassGen cg = new ClassGen(jc);
    ConstantPoolGen cpg = cg.getConstantPool();
    for (Method m : cg.getMethods()) {
      MethodGen mg = new MethodGen(m, cg.getClassName(), cpg);
      InstructionList il = mg.getInstructionList();
      InstructionHandle[] handles = il.getInstructionHandles();
      int prev = 0;
      for (InstructionHandle ih : handles) {
        int position = ih.getPosition();
        cfg.addNode(position, m, jc);
        Instruction inst = ih.getInstruction();

        boolean br = inst.getName().contains("if") || inst.getName().contains("goto");
        boolean ret = inst.getName().contains("return");
        boolean stat = inst.getName().contains("invokestatic");
        int len = inst.getLength();

        if (stat) {
          int index = inst.toString(true).indexOf(" ");
          String name = inst.toString(true).substring(index + 1);
          int tar = Integer.valueOf(name);
          INVOKESTATIC inv = new INVOKESTATIC(tar);
          name = inv.getMethodName(cpg);

          Method m2 = null;
          Method[] tm = cg.getMethods();
          for (int i = 0; i < tm.length; i++) {
            if (tm[i].getName().equals(name)) {
              m2 = tm[i];
            }
          }
          cfg.addEdge(position, m, jc, 0, m2, jc);
          cfg.addEdge(-1, m2, jc, position + len, m, jc);
        }

        if (!ret && !stat) {
          cfg.addEdge(position, position + len, m, jc);
        }
        if (br) {
          cfg.addEdge(position, position + len, m, jc);
          IF_ICMPGE comp = new IF_ICMPGE(ih);
          String name = comp.getTarget().toString(false);
          int index = name.indexOf(">");
          name = name.substring(index + 2);
          int tar = Integer.valueOf(name);
          cfg.addEdge(position, tar, m, jc);
        }
        if (ret) {
          cfg.addEdge(position, -1, m, jc);
        }

        prev = position;
      }
      System.out.println(cfg.toString());
    }
    return cfg;
  }
示例#2
0
  public static void main(String args[]) throws IOException {
    List<Instruction> insns = Instructions.getInstructions();
    Collections.sort(insns);

    List<Character> startLetters = new java.util.ArrayList<Character>();
    char last = 0;
    for (int i = 0; i < insns.size(); i++) {
      Instruction insn = (Instruction) insns.get(i);
      char leadChar = insn.getName().charAt(0);
      if (last != leadChar) {
        startLetters.add(leadChar);
        last = leadChar;
      }
    }

    File docdir = new File("SquawkBytecodeSpec");
    Build.mkdir(docdir);

    PrintWriter out = null;
    out = new PrintWriter(new FileWriter(new File(docdir, "Instructions.doc.html")));

    out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2//EN\">");
    out.println("<html>");
    out.println("<head>");
    out.println("<title>Squawk Bytecode Instruction Set</title>");
    out.println("</head>");
    out.println("<body BGCOLOR=#eeeeff text=#000000 LINK=#0000ff VLINK=#000077 ALINK=#ff0000>");
    out.println("<table width=100%><tr>");
    out.print(
        "<td>Prev | <a href=\"Instructions2.doc1.html\">Next</a> | <a href=\"Instructions2.index.html\">Index</a> </td>");
    out.println("<td align=right><i><i>Squawk Bytecode Instruction Set</i></i></td>");
    out.println("</tr></table>");
    out.println();
    out.println("<hr><br>");
    out.println();
    for (int i = 0; i < startLetters.size(); i++) {
      Character c = (Character) startLetters.get(i);
      out.println(
          "<a href=\"Instructions2.doc"
              + (i + 1)
              + ".html\">"
              + Character.toUpperCase(c.charValue())
              + "</a>");
    }
    out.println();
    out.println("<hr><br>");
    out.println();
    out.println("<h1>Squawk Bytecode Instruction Set</h1>");
    out.println("<hr><p>");
    out.println("A Squawk bytecode instruction consists of an opcode specifying the operation");
    out.println(
        "to be performed, followed by zero or more operands embodying values to be operated");
    out.println("upon. This chapter gives details about the format of each Squawk bytecode");
    out.println("instruction and the operation it performs.</p>");
    out.println("<hr>");

    out.println("<p>Prev | <a href=\"Instructions2.doc1.html\">Next</a></p>");
    out.println("</body></html>");

    out.close();

    PrintWriter indexPage =
        new PrintWriter(new FileWriter(new File(docdir, "Instructions2.index.html")));
    indexPage.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2//EN\">");
    indexPage.println("<html>");
    indexPage.println("<head>");
    indexPage.println("<title>Squawk Bytecode Instruction Set</title>");
    indexPage.println("</head>");
    indexPage.println(
        "<body BGCOLOR=#eeeeff text=#000000 LINK=#0000ff VLINK=#000077 ALINK=#ff0000>");
    indexPage.println("<table width=100%><tr>");
    indexPage.println("<td><a href=\"Instructions.doc.html\">Contents</a></td>");
    indexPage.println("<td align=right><i><i>Squawk Bytecode Instruction Set</i></i></td>");
    indexPage.println("</tr></table>");
    indexPage.println();
    indexPage.println("<hr><br>");
    indexPage.println();

    Iterator<Instruction> iter = insns.iterator();
    Instruction insn = (iter.hasNext()) ? iter.next() : null;
    for (int i = 0; i < startLetters.size(); i++) {
      String thisPage = "Instructions2.doc" + (i + 1) + ".html";
      String prevPage = "Instructions2.doc" + i + ".html";
      String nextPage = "Instructions2.doc" + (i + 2) + ".html";

      out = new PrintWriter(new FileWriter(new File(docdir, thisPage)));
      out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2//EN\">");
      out.println("<html>");
      out.println("<head>");
      out.println("<title>Squawk Bytecode Instruction Descriptions</title>");
      out.println("</head>");
      out.println("<body BGCOLOR=#eeeeff text=#000000 LINK=#0000ff VLINK=#000077 ALINK=#ff0000>");
      out.println("<table width=100%><tr>");
      out.print("<td>");
      out.print(" <a href=\"Instructions.doc.html\">Contents</a> | ");
      if (i > 0) out.print("<a href=\"" + prevPage + "\">Prev</a>");
      else out.print("<a href=\"Instructions.doc.html\">Prev</a>");
      out.print(" | ");
      if (i < startLetters.size() - 1) out.print("<a href=\"" + nextPage + "\">Next</a>");
      else out.print("Next");
      out.println(" | <a href=\"Instructions2.index.html\">Index</a> </td>");
      out.println("<td align=right><i><i>Squawk Bytecode Instruction Set</i></i></td>");
      out.println("</tr></table>");
      out.println();
      out.println("<hr><br>");
      out.println();
      for (int j = 0; j < startLetters.size(); j++) {
        Character c = (Character) startLetters.get(j);
        out.println(
            "<a href=\"Instructions2.doc"
                + (j + 1)
                + ".html\">"
                + Character.toUpperCase(c.charValue())
                + "</a>");
      }
      out.println();

      while (insn != null
          && Character.toUpperCase(insn.getName().charAt(0))
              == Character.toUpperCase(((Character) startLetters.get(i)).charValue())) {

        String name = insn.getName();
        indexPage.println("<a href=\"" + thisPage + "#" + name + "\">" + name + "</a><br>");

        out.println("<hr><a name=\"" + name + "\"><h2>" + name + "</h2>");

        out.println("<p><b>Operation</b></p>");
        out.println("<blockquote>");
        insn.printOperation(out);
        out.println("</blockquote>");

        out.println("<p><b>Format</b></p>");
        out.println("<blockquote>");
        insn.printFormat(out);
        out.println("</blockquote>");

        out.println("<p><b>Forms</b></p>");
        out.println("<blockquote>");
        insn.printForms(out);
        out.println("</blockquote>");

        out.println("<p><b>Operand Stack</b></p>");
        out.println("<blockquote>");
        insn.printOperandStack(out);
        out.println("</blockquote>");

        out.println("<p><b>Description</b></p>");
        out.println("<blockquote>");
        insn.printDescription(out);
        out.println("</blockquote>");

        if (insn.hasNotes()) {
          out.println("<p><b>Notes</b></p>");
          out.println("<blockquote>");
          insn.printNotes(out);
          out.println("</blockquote>");
        }

        insn = (iter.hasNext()) ? (Instruction) iter.next() : null;
      }

      out.println("<hr>");
      if (i > 0) out.print("<a href=\"Instructions2.doc" + i + ".html\">Prev</a>");
      else out.print("<a href=\"Instructions.doc.html\">Prev</a>");
      out.print(" | ");
      if (i < startLetters.size() - 1)
        out.print("<a href=\"Instructions2.doc" + (i + 2) + ".html\">Next</a>");
      else out.print("Next");
      out.println();
      out.println("</body></html>");

      out.flush();
      out.close();
    }

    indexPage.println("</body></html>");
    indexPage.close();

    for (int i = 0; i < 256; i++)
      if (!opcodes.contains(new Integer(i))) System.out.println("missing opcode: " + i);
  }