KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jbet > cmd > regencode


1 /*
2  * JBET - Java Binary Enhancement Tool
3  * Copyright (c) 2003 Networks Associates Technology, Inc.
4  *
5  * This software was developed under DARPA/SPAWAR contract
6  * N66001-00-C-8602 "SPMA" as part of the
7  * DARPA OASIS research program.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */

30
31 /*
32    Rewrite a method using the CodeGenerator. As the current implementation
33    is not very good, this command's only purpose is for testing the code generator
34    and basic block finder.
35 */

36
37 package jbet.cmd;
38 import jbet.*;
39 import java.io.*;
40 import java.util.*;
41
42 public class regencode implements Command
43 {
44     public int helplevel() { return 3; }
45     public String JavaDoc shorthelp() { return "regen code for <method>"; }
46     public String JavaDoc longhelp() {
47     return
48         "jbet regencode <class>.<method>\t\tRegen code for <method>\n"; }
49
50     public void run (Lexer lexer) throws Exception JavaDoc {
51
52     boolean runcodegen = true;
53     boolean runanal = false;
54
55     loop:
56     while(true) {
57         switch(lexer.read().type) {
58         case 'g': runcodegen = true; break;
59         case 'a': runanal = true; break;
60         case Token.END_OF_OPTS: break loop;
61         default: lexer.unexpected(lexer.justread());
62         }
63     }
64
65     Object JavaDoc thing = lexer.getThing(Jbet.default_class);
66     lexer.term();
67     
68     if (thing instanceof FieldInfo) throw new
69         GlobalException("can't regencode a field");
70     
71     Vector methods = new Vector();
72     if (thing instanceof ClassInfo) {
73         ClassInfo cr = (ClassInfo) thing;
74         for (int i = 0; i < cr.numMethods(); i++)
75         methods.addElement (cr.methodAt (i));
76     }
77     else
78         methods.addElement (thing);
79     
80     for (int i = 0; i < methods.size(); i++) {
81         MethodInfo mi = (MethodInfo) methods.elementAt (i);
82
83         if (mi.name.equals ("<init>")) continue;
84         if (mi.code == null) continue;
85         try {
86         DagMethodInfo method = new DagMethodInfo (new DagClassInfo (mi.cr), mi, true);
87         DagSnippit dags = method.dags();
88
89         SimpleCodeGen ra = new SimpleCodeGen ();
90
91         Snippit outcode = ra.codegen (dags);
92
93         mi.cr.setDirty();
94         mi.code = outcode;
95         mi.maxStack = -1;
96         mi.maxLocals = -1;
97         mi.code.addLinesAsPCs(mi.cr.constantPool());
98
99         DataFlow df = new DataFlow (mi);
100         df.run();
101
102         mi.maxStack = df.maxStackDetected;
103         mi.maxLocals = df.maxLocalsDetected;
104
105         mi.removeDataFlow();
106
107         Jbet.info.println("regencode " + mi.qualifiedName());
108         
109         } catch (Exception JavaDoc e) {
110         Jbet.output.println ("Warning : couldn't do method " +
111                      mi.qualifiedName() + " " + e.getMessage());
112         if (Main.verboseErrors)
113             e.printStackTrace(Jbet.output);
114         }
115     }
116
117     }
118 }
119
Popular Tags