gitweb on Svarog

projekti pod git sistemom za održavanje verzija -- projects under the git version control system
mjc2wsl - object support - new, get/put field
[mjc2wsl.git] / src / mjc2wsl.java
index 05a8a32..274bb7e 100644 (file)
@@ -109,12 +109,19 @@ public class mjc2wsl{
                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");
                ret.append("mjvm_locals := ARRAY(1,0), ");
+               ret.append("\n\tmjvm_statics := ARRAY("+numWords+",0), ");
+               ret.append("\n\tmjvm_arrays := < >, ");
+               ret.append("\n\tmjvm_objects := < >, ");
                ret.append("\n  mjvm_estack := < >, mjvm_mstack := < >, "); 
                ret.append("\n  mjvm_fp := 0, mjvm_sp := 0,");
                ret.append("\n  t_e_m_p := 0 > :");
@@ -206,6 +213,22 @@ public class mjc2wsl{
                return "mjvm_locals[" + (i+1)+"]";
        }
        
+       private String genStatic(int i){
+                       return "mjvm_statics[" + (i+1)+"]";
+       }
+       
+       private String genArray(int i){
+                       return "mjvm_arrays["+ i +"]";
+       }
+       
+       private String genArray(String i){
+                       return "mjvm_arrays["+ i+"]";
+       }
+       
+       private String genObject(String i){
+                       return "mjvm_objects["+ i+"]";
+       }
+       
        /**
         * Creates a WSL comment with care to quote chars.
         */
@@ -244,6 +267,13 @@ public class mjc2wsl{
                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");
        }
@@ -293,7 +323,7 @@ public class mjc2wsl{
                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) {
@@ -329,8 +359,30 @@ public class mjc2wsl{
                                break;
                        }
                        
-                       //TODO getstatic, putstatic
-                       //TODO getfield, putfield
+                       case getstatic:{
+                               prl(cmdToEStack(genStatic(get2())));
+                               break;
+                       } 
+                       case putstatic: {
+                               prl(cmdFromEStack(genStatic(get2())));
+                               break;
+                       }
+
+                       case getfield:{
+                                       int f = get2();
+                                       prl(getTop());
+                                       prl(cmdToEStack(genObject("tempa")+"["+(f+1)+"]"));
+                                       break;
+                       }
+                       case putfield:{
+                                       int f = get2();
+                                       //we need to use a temparray as a pointer, WSL
+                                       //otherwise tries to access it as a list of lists and fails
+                                       prl(getTopTwo());
+                                       prl("VAR < tempArray := "+genObject("tempb")+" > :");
+                                       prl("tempArray["+(f+1)+"]:=tempa ENDVAR;");                             
+                                       break;
+                       }
                        
                        case const_: {
                                prl(cmdToEStack(get4()));
@@ -380,11 +432,80 @@ public class mjc2wsl{
                                break;
                        }
 
-                       //TODO neg, shl, shr, inc
-                       //TODO new_ newarray
-                       //TODO aload, asstore, baload, bastore
-                       //TODO arraylength
-                       //TODO pop, dup, dup2
+                       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;
+                       }
+
+                       case new_ :{
+                                       int size = get2();
+                                       //TODO maybe objects and arrays should be in the same list?
+                                       prl("mjvm_objects := mjvm_objects ++ < ARRAY("+size+",0) >;");
+                                       prl(cmdToEStack("LENGTH(mjvm_objects)"));
+                                       break;
+                       }
+                       case newarray :{
+                                       get();// 0 - bytes, 1 - words; ignore for now
+                                       //TODO take into consideration 0/1
+                                       prl(getTop());
+                                       prl("mjvm_arrays := mjvm_arrays ++ < ARRAY(tempa,0) >;");
+                                       prl(cmdToEStack("LENGTH(mjvm_arrays)"));
+                                       break;
+                       }
+                       
+                       case aload:
+                       case baload:{
+                               prl(getTopTwo());
+                               prl(cmdToEStack(genArray("tempb")+"[tempa+1]"));
+                               break;
+                       }
+                       case astore:
+                       case bastore:{
+                               prl(cmdFromEStack("tempres"));
+                               prl(getTopTwo());
+                               //we need to use a temparray as a pointer, WSL
+                               //otherwise tries to access it as a list of lists and fails
+                               prl("VAR < tempArray := "+genArray("tempb")+" > :");
+                               prl("tempArray[tempa+1]:=tempres ENDVAR;");
+                               break;
+                       }
+                       case arraylength :{
+                               prl(getTop());
+                               //TODO make an array length function of some sort!
+                               prl(createComment("array length not known - LENGTH not aplicable to arrays",C_ERR));
+                               message("array length not known - LENGTH not aplicable to arrays",M_ERR);
+                               prl(createComment("put 1 on the stack for consistency",C_SPEC));
+                               prl(cmdToEStack(1));
+                               break;
+                       }
+                       
+                       case dup : {
+                                       prl(getTop());
+                                       prl(cmdToEStack("tempa"));
+                                       prl(cmdToEStack("tempa"));
+                                       break;                                  
+                       }
+                       case dup2 : {
+                                       prl(getTopTwo());
+                                       prl(cmdToEStack("tempb"));
+                                       prl(cmdToEStack("tempa"));
+                                       prl(cmdToEStack("tempb"));
+                                       prl(cmdToEStack("tempa"));
+                                       break;                                  
+                       }
+                       
+                       case pop : {
+                                       prl(cmdPopEStack());
+                                       break;                                  
+                       }
                        
                        case jmp: {
                                prl("CALL a" + (counter + get2()) + ";");
@@ -406,17 +527,14 @@ public class mjc2wsl{
                        }
 
                        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+" ==");
+                               //we let the actions return
+                               // there is nothing to clean up
+                               prl("SKIP END b"+counter+" ==");
                                break;
                        }
                        case enter: {
@@ -435,18 +553,24 @@ public class mjc2wsl{
                        }
 
                        // read, print
-                       case bread:
-                       case read: {
+                       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"));
                                break;
                        }
 
                        // the prints
-                       case bprint: 
-                       case print: {
+                       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: {
                                // TODO printing numbers needs different lengths of spacing
                                prl(getTopTwo());
                                pr(createComment("print spacing",C_SPEC));
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner