gitweb on Svarog

projekti pod git sistemom za održavanje verzija -- projects under the git version control system
mjc2wsl - directives to print Estack on each change
[mjc2wsl.git] / src / mjc2wsl.java
index c269f20..886cd7d 100644 (file)
@@ -10,8 +10,32 @@ import java.util.*;
  * @author Doni Pracner, http://perun.dmi.rs/pracner http://quemaster.com
  */
 public class mjc2wsl{
-       public static String versionN = "0.1.2";
+       public static String versionN = "0.1.4";
 
+       public static final int M_ERR = 2, M_WAR = 1, M_DEB = 0;
+       
+       private int printLevel = M_ERR;
+       
+       private int[] messageCounters = new int[M_ERR+1];
+       
+       private void message(String mes, int level){
+                       if (level>=printLevel)
+                                       System.out.println(mes);
+                       messageCounters[level]++;
+       }
+       
+       private void printMessageCounters(){
+                       printMessageCounters(System.out);
+       }
+       
+       private void printMessageCounters(PrintStream out){
+                       out.println("total errors:"+messageCounters[M_ERR]+" warnings:"+messageCounters[M_WAR]);
+       }
+       
+       private boolean addPauseAfterEachAddress=false, 
+               addPrintForEachAddress = false,
+               genPrintEStackOnChange = false;
+       
        /** Constant used for marking a regular comment from the original file */
        public static final char C_REG = ' ';
        /**
@@ -89,10 +113,12 @@ public class mjc2wsl{
                        "C:\" This file automatically converted from microjava bytecode\";\n"
                        +"C:\" with mjc2wsl v "+versionN+"\";\n");
 
-               ret.append("VAR < tempa := 0, tempb := 0, tempres :=0,\n");
+               ret.append("VAR < tempa := 0, tempb := 0, tempres :=0,\n\t");
                for (int i = 0; i <= 3; i++)
-                       ret.append("loc" + i + " := 0, ");
-               ret.append("\n  estack := < >, t_e_m_p := 0 > :");
+                       ret.append(loc(i) + " := 0, ");
+               ret.append("\n  mjvm_estack := < >, mjvm_mstack := < >, "); 
+               ret.append("\n  mjvm_fp := 0, mjvm_sp := 0,");
+               ret.append("\n  t_e_m_p := 0 > :");
 
                return ret.toString();
        }
@@ -103,6 +129,38 @@ public class mjc2wsl{
        
        private boolean originalInComments = false;     
        
+       private HashMap<Integer,String> opMap = null;
+       
+       private String opCodeFile = "mj-bytecodes.properties";
+       
+       private HashMap<Integer,String> getOpMap() {
+                       if (opMap==null) {
+                                       opMap = new HashMap<Integer, String> (60, 0.98f);
+                                       try{
+                                       BufferedReader in = new BufferedReader(
+                                                       new InputStreamReader(getClass().getResourceAsStream(opCodeFile)));
+                                       String str = in.readLine();
+                                       while (str != null) {
+                                                       String[] ss = str.split("=");
+                                                       opMap.put(Integer.parseInt(ss[0]),ss[1]);
+                                                       str = in.readLine();
+                                       }
+                                       in.close();
+                                       }catch (Exception ex) {
+                                               ex.printStackTrace();
+                                       }
+                       }
+                       return opMap;
+       }
+       
+       public String getOpString(int op) {
+                       return getOpMap().get(op);
+       }
+       
+       public String describeOpCode(int op) {
+                       return op + " (" + getOpString(op) + ")";
+       }
+       
        private InputStream mainIn;
        private PrintWriter out = null;
        private int counter = -1;
@@ -145,7 +203,7 @@ public class mjc2wsl{
        }
        
        private String loc(int i){
-               return "loc" + i;
+               return "mjvm_loc" + i;
        }
        
        /**
@@ -166,16 +224,24 @@ public class mjc2wsl{
                return "C:\"" + type + str.replace("\"", "''") + "\";";
        }
 
+       //Expression stack
+       
        private String cmdToEStack(int i) {
-               return "estack := <" + i + " > ++ estack;";
+               String res = "mjvm_estack := <" + i + " > ++ mjvm_estack;";
+               if (genPrintEStackOnChange) res +="PRINT(\"eStack\",mjvm_estack);";
+               return res;
        }
 
        private String cmdToEStack(String i) {
-               return "estack := <" + i + " > ++ estack;";
+               String res = "mjvm_estack := <" + i + " > ++ mjvm_estack;";
+               if (genPrintEStackOnChange) res +="PRINT(\"eStack\",mjvm_estack);";
+               return res;
        }
 
        private String cmdFromEStack(String st) {
-               return st + " := HEAD(estack); estack := TAIL(estack);";
+               String res = st + " := HEAD(mjvm_estack); mjvm_estack := TAIL(mjvm_estack);";
+               if (genPrintEStackOnChange) res +="PRINT(\"eStack\",mjvm_estack);";
+               return res;
        }
        
        private String getTopTwo(){
@@ -186,10 +252,24 @@ public class mjc2wsl{
                return cmdFromEStack("tempa");
        }
        
+       //Method stack
+       
+       private String cmdToMStack(int i) {
+               return "mjvm_mstack := <" + i + " > ++ mjvm_mstack;";
+       }
+
+       private String cmdToMStack(String i) {
+               return "mjvm_mstack := <" + i + " > ++ mjvm_mstack;";
+       }
+
+       private String cmdFromMStack(String st) {
+               return st + " := HEAD(mjvm_mstack); mjvm_mstack := TAIL(mjvm_mstack);";
+       }
+       
        private String getRelationFor(int opcode) throws Exception {
                        switch (opcode) {
                                        case jeq: return "=";
-                                       case jne: return "#";
+                                       case jne: return "<>";
                                        case jlt: return "<";
                                        case jle: return "<=";
                                        case jgt: return ">";
@@ -197,20 +277,34 @@ public class mjc2wsl{
                        }
                        throw new Exception("Wrong opcode for a relation");
        }
+       
+       private boolean isJumpCode(int opcode) {
+                       return (opcode>=jmp) && (opcode<=jge);
+       }
 
        public void convertStream(InputStream ins) throws Exception{
                mainIn = ins;
-               //skip start TODO make better
-               for (int i = 0; i < 14; i++)
-                       get();
+               //process start 
+               byte m = (byte) get();
+               byte j = (byte) get();
+               if (m!='M' || j !='J') 
+                               throw new Exception("Wrong start of bytecode file");
+               int codesize = get4();
+               int numberOfWords = get4();
+               int mainAdr = get4();
                
                prl(getStandardStart());
-               prl("SKIP;\n ACTIONS A_S_start:\n A_S_start == CALL a14 END");
+               prl("SKIP;\n ACTIONS A_S_start:\n A_S_start == CALL a"+(14+mainAdr)+" END");
                int op = get();
                while (op >= 0) {
                        if (originalInComments)
-                               prl(createComment("" + op, C_OC));
+                               prl(createComment(describeOpCode(op), C_OC));
                        prl("a" + counter + " == ");
+                       if (addPrintForEachAddress) {
+                                       prl("PRINT(\"a"+counter+"\");");
+                                       if (addPauseAfterEachAddress)
+                                                       prl("debug_disposable_string := @Read_Line(Standard_Input_Port);");
+                       }
                        switch (op) {
                        case load: {
                                prl(cmdToEStack(loc(get())));
@@ -234,6 +328,10 @@ public class mjc2wsl{
                                prl(cmdFromEStack(loc(op - store_0)));
                                break;
                        }
+                       
+                       //TODO getstatic, putstatic
+                       //TODO getfield, putfield
+                       
                        case const_: {
                                prl(cmdToEStack(get4()));
                                break;
@@ -249,24 +347,6 @@ public class mjc2wsl{
                                break;
                        }
 
-                       case jmp: {
-                               prl("CALL a" + (counter + get2()) + ";");
-                               break;
-                       }
-
-                       case jeq:
-                       case jne:
-                       case jlt:
-                       case jle:
-                       case jgt:
-                       case jge: {
-                               prl(getTopTwo());
-                               prl("IF tempb "+ getRelationFor(op)
-                                               +" tempa THEN CALL a" + (counter + get2())
-                                               + " FI;");
-                               break;
-                       }
-
                        case add: {
                                prl(getTopTwo());
                                prl("tempres := tempb + tempa;");
@@ -287,23 +367,78 @@ public class mjc2wsl{
                        }
                        case div: {
                                prl(getTopTwo());
-                               prl("tempres := tempb / tempa;");
+                               prl("IF tempa = 0 THEN ERROR(\"division by zero\") FI;");
+                               prl("tempres := tempb DIV tempa;");
+                               prl(cmdToEStack("tempres"));
+                               break;
+                       }
+                       case rem: {
+                               prl(getTopTwo());
+                               prl("IF tempa = 0 THEN ERROR(\"division by zero\") FI;");
+                               prl("tempres := tempb MOD tempa;");
                                prl(cmdToEStack("tempres"));
                                break;
                        }
 
-                       case enter: {
-                               prl(createComment("enter not fully procesed yet"));
-                               get();
-                               get();
+                       //TODO neg, shl, shr, inc
+                       //TODO new_ newarray
+                       //TODO aload, asstore, baload, bastore
+                       //TODO arraylength
+                       //TODO pop, dup, dup2
+                       
+                       case jmp: {
+                               prl("CALL a" + (counter + get2()) + ";");
+                               break;
+                       }
+
+                       case jeq:
+                       case jne:
+                       case jlt:
+                       case jle:
+                       case jgt:
+                       case jge: {
+                               prl(getTopTwo());
+                               prl("IF tempb "+ getRelationFor(op)
+                                               +" tempa THEN CALL a" + (counter + get2())
+                                               +" ELSE CALL a" + (counter+1)
+                                               + " FI;");
+                               break;
+                       }
+
+                       case call: {
+                               prl(cmdToMStack(counter+2));
+                               prl("CALL a" + (counter + get2()) + ";");
                                break;
                        }
+
                        case return_: {
-                               prl(createComment("return not fully procesed yet"));
+                               prl("IF EMPTY?(mjvm_mstack) THEN CALL Z ELSE");
+                               //else we let things return             
+                               prl(cmdFromMStack("tempa"));
+                               prl("SKIP FI");
+                               prl("END b"+counter+" ==");
+                               break;
+                       }
+                       case enter: {
+                               prl(createComment("enter not fully procesed yet"));
+                               message("enter not fully procesed yet", M_WAR);
+                               int parameters = get();
+                               
+                               get();
+                               for (int i = parameters-1; i >= 0; i--)
+                                               prl(cmdFromEStack(loc(i)));
                                break;
                        }
                        case exit: {
                                prl(createComment("exit not fully procesed yet"));
+                               message("exit not fully procesed yet", M_WAR);
+                               break;
+                       }
+
+                       //TODO read, print
+                       case read: {
+                               prl("tempa := @String_To_Num(@Read_Line(Standard_Input_Port));");
+                               prl(cmdToEStack("tempa"));
                                break;
                        }
 
@@ -319,14 +454,26 @@ public class mjc2wsl{
                                prl("PRINT(tempb);");
                                break;
                        }
+
+                       case trap: {
+                               prl("ERROR(\"Runtime error: trap("+get()+")\");");
+                               break;
+                       }
+                       
+       
                        default:
                                prl(createComment("unknown op error: " + op, C_ERR));
+                               message("unknown op error: "+ op, M_ERR);
                                break;
                        }
 
+                       boolean wasJump = isJumpCode(op);
                        op = get();
                        if (op >= 0)
-                               prl("CALL a" + counter + " END");
+                               if (wasJump)
+                                               prl("SKIP END");
+                               else
+                                               prl("CALL a" + counter + " END");
                }
                prl("CALL Z;\nSKIP END\nENDACTIONS;\n");
                prl(getStandardEnd());
@@ -346,7 +493,10 @@ public class mjc2wsl{
                                + ", by Doni Pracner");
                System.out.println("usage:\n\t {options} mjc2wsl  filename [outfile]");
                System.out.println("options:\n\t--screen print output to screen");
-               System.out.println("\t-o --oc include original code in comments");
+               System.out.println("\t-o --oc[+-] include original code in comments");
+               System.out.println("\t-v verbose, print warning messages");
+               System.out.println("\t-q don't print even the error messages");
+               System.out.println("\t-d print detailed debug messages");
        }
        
        public String makeDefaultOutName(String inname){
@@ -373,10 +523,21 @@ public class mjc2wsl{
                                                originalInComments = args[i].charAt(4) == '+';
                                        else
                                                originalInComments = true;
-                               } else if (args[i].startsWith("--screen")) {
+                               } else if (args[i].compareTo("--screen") == 0) {
                                        out = new PrintWriter(System.out);
-                               }
-                               i++;
+                               } else if (args[i].compareTo("-d") == 0) {
+                                       printLevel = M_DEB;//print debug info
+                               } else if (args[i].compareTo("-v") == 0) {
+                                       printLevel = M_WAR;//print warnings
+                               } else if (args[i].compareTo("-q") == 0) {
+                                       printLevel = M_ERR+1;//no printing                              
+                               } else if (args[i].compareToIgnoreCase("--genEStackPrint") == 0) {
+                                       genPrintEStackOnChange = true;
+                               } else if (args[i].compareToIgnoreCase("--genAll") == 0) {
+                                       genPrintEStackOnChange = true;
+                                       addPrintForEachAddress = true;
+                                       addPauseAfterEachAddress = true;
+                               }i++;
                        }
 
                        if (i >= args.length) {
@@ -408,6 +569,7 @@ public class mjc2wsl{
                                long mili = Calendar.getInstance().getTimeInMillis()
                                                - now.getTimeInMillis();
                                System.out.println("conversion time:" + mili + " ms");
+                               printMessageCounters();
                                out.close();
                        } else
                                System.out.println("file does not exist");
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner