gitweb on Svarog

projekti pod git sistemom za održavanje verzija -- projects under the git version control system
mjc2wsl - FIX inc, forgot a ;
[mjc2wsl.git] / src / mjc2wsl.java
index 1b31bba..10e9c2c 100644 (file)
@@ -109,13 +109,17 @@ public class mjc2wsl{
                trap            = 57;
 
        public String getStandardStart(){
                trap            = 57;
 
        public String getStandardStart(){
+                       return getStandardStart(10);
+       }
+       
+       public String getStandardStart(int numWords){
                StringBuilder ret = new StringBuilder(
                        "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\t");
                StringBuilder ret = new StringBuilder(
                        "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\t");
-               for (int i = 0; i <= 3; i++)
-                       ret.append(loc(i) + " := 0, ");
+               ret.append("mjvm_locals := ARRAY(1,0), ");
+               ret.append("\n\tmjvm_statics := ARRAY("+numWords+",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 > :");
                ret.append("\n  mjvm_estack := < >, mjvm_mstack := < >, "); 
                ret.append("\n  mjvm_fp := 0, mjvm_sp := 0,");
                ret.append("\n  t_e_m_p := 0 > :");
@@ -203,7 +207,12 @@ public class mjc2wsl{
        }
        
        private String loc(int i){
        }
        
        private String loc(int i){
-               return "mjvm_loc" + i;
+               //arrays start at 1 in WSL, so we need an offset
+               return "mjvm_locals[" + (i+1)+"]";
+       }
+       
+       private String genStatic(int i){
+                       return "mjvm_statics[" + (i+1)+"]";
        }
        
        /**
        }
        
        /**
@@ -244,6 +253,13 @@ public class mjc2wsl{
                return res;
        }
        
                return res;
        }
        
+       private String cmdPopEStack() {
+               String res = "mjvm_estack := TAIL(mjvm_estack);";
+               if (genPrintEStackOnChange) res +="PRINT(\"eStack\",mjvm_estack);";
+               return res;
+       }
+       
+       
        private String getTopTwo(){
                return cmdFromEStack("tempa") + "\n" + cmdFromEStack("tempb");
        }
        private String getTopTwo(){
                return cmdFromEStack("tempa") + "\n" + cmdFromEStack("tempb");
        }
@@ -293,7 +309,7 @@ public class mjc2wsl{
                int numberOfWords = get4();
                int mainAdr = get4();
                
                int numberOfWords = get4();
                int mainAdr = get4();
                
-               prl(getStandardStart());
+               prl(getStandardStart(numberOfWords));
                prl("SKIP;\n ACTIONS A_S_start:\n A_S_start == CALL a"+(14+mainAdr)+" END");
                int op = get();
                while (op >= 0) {
                prl("SKIP;\n ACTIONS A_S_start:\n A_S_start == CALL a"+(14+mainAdr)+" END");
                int op = get();
                while (op >= 0) {
@@ -329,8 +345,22 @@ public class mjc2wsl{
                                break;
                        }
                        
                                break;
                        }
                        
-                       //TODO getstatic, putstatic
+                       case getstatic:{
+                               prl(cmdToEStack(genStatic(get2())));
+                               break;
+                       } 
+                       case putstatic: {
+                               prl(cmdFromEStack(genStatic(get2())));
+                               break;
+                       }
                        //TODO getfield, putfield
                        //TODO getfield, putfield
+                       case getfield:
+                       case putfield:{
+                                       prl(createComment("fields are not processed properly", C_ERR));
+                                       message("fields are not processed properly", M_ERR);
+                                       get2();
+                                       break;
+                       }
                        
                        case const_: {
                                prl(cmdToEStack(get4()));
                        
                        case const_: {
                                prl(cmdToEStack(get4()));
@@ -380,11 +410,35 @@ public class mjc2wsl{
                                break;
                        }
 
                                break;
                        }
 
-                       //TODO neg, shl, shr, inc
+                       case neg :{
+                               prl(getTop());
+                               prl(cmdToEStack("-tempa"));
+                               break;
+                       }                       
+                       //TODO shl, shr
+                       
+                       case inc  : {
+                               int b1 = get(), b2 = get();
+                               prl(loc(b1)+" := " +loc(b1)+" + "+b2+";");
+                               break;
+                       }
                        //TODO new_ newarray
                        //TODO new_ newarray
+                       case new_ :
+                                       get();//needs a short, but a byte will be taken bellow as well
+                       case newarray :{
+                                       prl(createComment("memory allocation not processed properly", C_ERR));
+                                       message("memory allocation  not processed properly", M_ERR);
+                                       get();
+                                       break;
+                       }
                        //TODO aload, asstore, baload, bastore
                        //TODO arraylength
                        //TODO aload, asstore, baload, bastore
                        //TODO arraylength
-                       //TODO pop, dup, dup2
+                       //TODO dup, dup2
+                       
+                       case pop : {
+                                       prl(cmdPopEStack());
+                                       break;                                  
+                       }
                        
                        case jmp: {
                                prl("CALL a" + (counter + get2()) + ";");
                        
                        case jmp: {
                                prl("CALL a" + (counter + get2()) + ";");
@@ -406,36 +460,37 @@ public class mjc2wsl{
                        }
 
                        case call: {
                        }
 
                        case call: {
-                               prl(cmdToMStack(counter+2));
                                prl("CALL a" + (counter + get2()) + ";");
                                break;
                        }
 
                        case return_: {
                                prl("CALL a" + (counter + get2()) + ";");
                                break;
                        }
 
                        case return_: {
-                               prl("IF EMPTY?(mjvm_mstack) THEN CALL Z ELSE");
+                               prl("IF EMPTY?(mjvm_mstack) THEN CALL Z FI");
                                //else we let things return             
                                //else we let things return             
-                               prl(cmdFromMStack("tempa"));
-                               prl("SKIP FI");
                                prl("END b"+counter+" ==");
                                break;
                        }
                        case enter: {
                                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();
                                
                                int parameters = get();
                                
-                               get();
+                               int locals = get();
+                               prl(cmdToMStack("mjvm_locals"));
+                               prl("mjvm_locals := ARRAY("+locals+",0);");
                                for (int i = parameters-1; i >= 0; i--)
                                                prl(cmdFromEStack(loc(i)));
                                break;
                        }
                        case exit: {
                                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);
+                               prl(cmdFromMStack("mjvm_locals"));
                                break;
                        }
 
                                break;
                        }
 
-                       //TODO read, print
+                       // read, print
+                       case bread:{
+                               //TODO make it a char for read
+                               message("char is read like a number", M_WAR);
+                               prl(createComment("char is read like a number",C_SPEC));
+                       }
                        case read: {
                                prl("tempa := @String_To_Num(@Read_Line(Standard_Input_Port));");
                                prl(cmdToEStack("tempa"));
                        case read: {
                                prl("tempa := @String_To_Num(@Read_Line(Standard_Input_Port));");
                                prl(cmdToEStack("tempa"));
@@ -443,15 +498,17 @@ public class mjc2wsl{
                        }
 
                        // the prints
                        }
 
                        // the prints
-                       case bprint: {
-                               prl(getTopTwo());
-                               prl("PRINT(tempb);");
-                               break;
+                       case bprint:{
+                               // TODO need to make it a char on print
+                               message("chars will be printed as number codes", M_WAR);
+                               prl(createComment("char will be printed as a number code",C_SPEC));
                        }
                        case print: {
                        }
                        case print: {
-                               // TODO need to make it a char
+                               // TODO printing numbers needs different lengths of spacing
                                prl(getTopTwo());
                                prl(getTopTwo());
-                               prl("PRINT(tempb);");
+                               pr(createComment("print spacing",C_SPEC));
+                               prl("IF tempa>1 THEN FOR i:=2 TO tempa STEP 1 DO PRINFLUSH(\" \") OD FI;");
+                               prl("PRINFLUSH(tempb);");
                                break;
                        }
 
                                break;
                        }
 
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner