gitweb on Svarog

projekti pod git sistemom za održavanje verzija -- projects under the git version control system
version 0.1.4
[mjc2wsl.git] / src / mjc2wsl.java
index 574adcc..b7edf0b 100644 (file)
@@ -10,7 +10,7 @@ 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;
        
@@ -109,10 +109,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("mjvm_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();
        }
@@ -165,7 +167,7 @@ public class mjc2wsl{
        }
        
        private String loc(int i){
-               return "loc" + i;
+               return "mjvm_loc" + i;
        }
        
        /**
@@ -186,16 +188,18 @@ public class mjc2wsl{
                return "C:\"" + type + str.replace("\"", "''") + "\";";
        }
 
+       //Expression stack
+       
        private String cmdToEStack(int i) {
-               return "estack := <" + i + " > ++ estack;";
+               return "mjvm_estack := <" + i + " > ++ mjvm_estack;";
        }
 
        private String cmdToEStack(String i) {
-               return "estack := <" + i + " > ++ estack;";
+               return "mjvm_estack := <" + i + " > ++ mjvm_estack;";
        }
 
        private String cmdFromEStack(String st) {
-               return st + " := HEAD(estack); estack := TAIL(estack);";
+               return st + " := HEAD(mjvm_estack); mjvm_estack := TAIL(mjvm_estack);";
        }
        
        private String getTopTwo(){
@@ -206,6 +210,20 @@ 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 "=";
@@ -254,6 +272,10 @@ public class mjc2wsl{
                                prl(cmdFromEStack(loc(op - store_0)));
                                break;
                        }
+                       
+                       //TODO getstatic, putstatic
+                       //TODO getfield, putfield
+                       
                        case const_: {
                                prl(cmdToEStack(get4()));
                                break;
@@ -269,24 +291,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;");
@@ -320,6 +324,44 @@ public class mjc2wsl{
                                break;
                        }
 
+                       //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())
+                                               + " FI;");
+                               break;
+                       }
+
+                       case call: {
+                               prl(cmdToMStack(counter+2));
+                               prl("CALL a" + (counter + get2()) + ";");
+                               break;
+                       }
+
+                       case return_: {
+                               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);
@@ -327,17 +369,14 @@ public class mjc2wsl{
                                get();
                                break;
                        }
-                       case return_: {
-                               prl(createComment("return not fully procesed yet"));
-                               message("return not fully procesed yet", M_WAR);
-                               break;
-                       }
                        case exit: {
                                prl(createComment("exit not fully procesed yet"));
                                message("exit not fully procesed yet", M_WAR);
                                break;
                        }
 
+                       //TODO read, print
+
                        // the prints
                        case bprint: {
                                prl(getTopTwo());
@@ -356,6 +395,9 @@ public class mjc2wsl{
                                break;
                        }
 
+                       //TODO trap
+
+
                        op = get();
                        if (op >= 0)
                                prl("CALL a" + counter + " END");
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner