gitweb on Svarog

projekti pod git sistemom za održavanje verzija -- projects under the git version control system
mjc2wsl - refactoring, renamed all the methods that generate pieces of WSL to start...
[mjc2wsl.git] / src / mjc2wsl.java
index 4c7618f..dafe6e4 100644 (file)
@@ -108,11 +108,11 @@ public class mjc2wsl{
                bprint      = 56,
                trap            = 57;
 
-       public String getStandardStart(){
-                       return getStandardStart(10);
+       public String createStandardStart(){
+                       return createStandardStart(10);
        }
        
-       public String getStandardStart(int numWords){
+       public String createStandardStart(int numWords){
                StringBuilder ret = new StringBuilder(
                        "C:\" This file automatically converted from microjava bytecode\";\n"
                        +"C:\" with mjc2wsl v "+versionN+"\";\n");
@@ -120,6 +120,8 @@ public class mjc2wsl{
                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 > :");
@@ -127,7 +129,7 @@ public class mjc2wsl{
                return ret.toString();
        }
 
-       public String getStandardEnd(){
+       public String createStandardEnd(){
                return "SKIP\nENDVAR";
        }
        
@@ -137,32 +139,32 @@ public class mjc2wsl{
        
        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;
+       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);
+               return getOpMap().get(op);
        }
-       
+
        public String describeOpCode(int op) {
-                       return op + " (" + getOpString(op) + ")";
+               return op + " (" + getOpString(op) + ")";
        }
        
        private InputStream mainIn;
@@ -206,13 +208,25 @@ public class mjc2wsl{
                return (get2() << 16) + (get2() << 16 >>> 16);
        }
        
-       private String loc(int i){
-               //arrays start at 1 in WSL, so we need an offset
-               return "mjvm_locals[" + (i+1)+"]";
+       private String createLocal(int 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)+"]";
+
+       private String createStatic(int i) {
+               return "mjvm_statics[" + (i + 1) + "]";
+       }
+
+       private String createArray(int i) {
+               return "mjvm_arrays[" + i + "]";
+       }
+
+       private String createArray(String i) {
+               return "mjvm_arrays[" + i + "]";
+       }
+
+       private String createObject(String i) {
+               return "mjvm_objects[" + i + "]";
        }
        
        /**
@@ -235,67 +249,71 @@ public class mjc2wsl{
 
        //Expression stack
        
-       private String cmdToEStack(int i) {
+       private String createToEStack(int i) {
                String res = "mjvm_estack := <" + i + " > ++ mjvm_estack;";
-               if (genPrintEStackOnChange) res +="PRINT(\"eStack\",mjvm_estack);";
+               if (genPrintEStackOnChange)
+                       res += "PRINT(\"eStack\",mjvm_estack);";
                return res;
        }
 
-       private String cmdToEStack(String i) {
+       private String createToEStack(String i) {
                String res = "mjvm_estack := <" + i + " > ++ mjvm_estack;";
-               if (genPrintEStackOnChange) res +="PRINT(\"eStack\",mjvm_estack);";
+               if (genPrintEStackOnChange)
+                       res += "PRINT(\"eStack\",mjvm_estack);";
                return res;
        }
 
-       private String cmdFromEStack(String st) {
-               String res = st + " := HEAD(mjvm_estack); mjvm_estack := TAIL(mjvm_estack);";
-               if (genPrintEStackOnChange) res +="PRINT(\"eStack\",mjvm_estack);";
+       private String createFromEStack(String st) {
+               String res = st
+                               + " := HEAD(mjvm_estack); mjvm_estack := TAIL(mjvm_estack);";
+               if (genPrintEStackOnChange)
+                       res += "PRINT(\"eStack\",mjvm_estack);";
                return res;
        }
-       
-       private String cmdPopEStack() {
+
+       private String createPopEStack() {
                String res = "mjvm_estack := TAIL(mjvm_estack);";
-               if (genPrintEStackOnChange) res +="PRINT(\"eStack\",mjvm_estack);";
+               if (genPrintEStackOnChange)
+                       res += "PRINT(\"eStack\",mjvm_estack);";
                return res;
        }
-       
-       
-       private String getTopTwo(){
-               return cmdFromEStack("tempa") + "\n" + cmdFromEStack("tempb");
+
+       private String createTopTwoEStack() {
+               return createFromEStack("tempa") + "\n" + createFromEStack("tempb");
        }
 
-       private String getTop() {
-               return cmdFromEStack("tempa");
+       private String createTopEStack() {
+               return createFromEStack("tempa");
        }
        
        //Method stack
-       
-       private String cmdToMStack(int i) {
+
+       private String createToMStack(int i) {
                return "mjvm_mstack := <" + i + " > ++ mjvm_mstack;";
        }
 
-       private String cmdToMStack(String i) {
+       private String createToMStack(String i) {
                return "mjvm_mstack := <" + i + " > ++ mjvm_mstack;";
        }
 
-       private String cmdFromMStack(String st) {
+       private String createFromMStack(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 jlt: return "<";
-                                       case jle: return "<=";
-                                       case jgt: return ">";
-                                       case jge: return ">=";
-                       }
-                       throw new Exception("Wrong opcode for a relation");
+               switch (opcode) {
+                               case jeq: return "=";
+                               case jne: return "<>";
+                               case jlt: return "<";
+                               case jle: return "<=";
+                               case jgt: return ">";
+                               case jge: return ">=";
+               }
+               throw new Exception("Wrong opcode for a relation");
        }
-       
+
        private boolean isJumpCode(int opcode) {
-                       return (opcode>=jmp) && (opcode<=jge);
+               return (opcode >= jmp) && (opcode <= jge);
        }
 
        public void convertStream(InputStream ins) throws Exception{
@@ -309,61 +327,70 @@ public class mjc2wsl{
                int numberOfWords = get4();
                int mainAdr = get4();
                
-               prl(getStandardStart(numberOfWords));
-               prl("SKIP;\n ACTIONS A_S_start:\n A_S_start == CALL a"+(14+mainAdr)+" END");
+               prl(createStandardStart(numberOfWords));
+               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(describeOpCode(op), C_OC));
                        prl("a" + counter + " == ");
                        if (genPrintForEachAddress) {
-                                       prl("PRINT(\"a"+counter+"\");");
-                                       if (genPauseAfterEachAddress)
-                                                       prl("debug_disposable_string := @Read_Line(Standard_Input_Port);");
+                               prl("PRINT(\"a" + counter + "\");");
+                               if (genPauseAfterEachAddress)
+                                       prl("debug_disposable_string := @Read_Line(Standard_Input_Port);");
                        }
                        switch (op) {
                        case load: {
-                               prl(cmdToEStack(loc(get())));
+                               prl(createToEStack(createLocal(get())));
                                break;
                        }
                        case load_0:
                        case load_1:
                        case load_2:
                        case load_3: {
-                               prl(cmdToEStack(loc(op - load_0)));
+                               prl(createToEStack(createLocal(op - load_0)));
                                break;
                        }
                        case store: {
-                               prl(cmdFromEStack(loc(get())));
+                               prl(createFromEStack(createLocal(get())));
                                break;
                        }
                        case store_0:
                        case store_1:
                        case store_2:
                        case store_3: {
-                               prl(cmdFromEStack(loc(op - store_0)));
+                               prl(createFromEStack(createLocal(op - store_0)));
                                break;
                        }
-                       
-                       case getstatic:{
-                               prl(cmdToEStack(genStatic(get2())));
+
+                       case getstatic: {
+                               prl(createToEStack(createStatic(get2())));
                                break;
-                       } 
+                       }
                        case putstatic: {
-                               prl(cmdFromEStack(genStatic(get2())));
+                               prl(createFromEStack(createStatic(get2())));
                                break;
                        }
-                       //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 getfield: {
+                               int f = get2();
+                               prl(createTopEStack());
+                               prl(createToEStack(createObject("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(createTopTwoEStack());
+                               prl("VAR < tempArray := " + createObject("tempb") + " > :");
+                               prl("tempArray[" + (f + 1) + "]:=tempa ENDVAR;");
+                               break;
                        }
-                       
+
                        case const_: {
-                               prl(cmdToEStack(get4()));
+                               prl(createToEStack(get4()));
                                break;
                        }
 
@@ -373,68 +400,138 @@ public class mjc2wsl{
                        case const_3:
                        case const_4:
                        case const_5: {
-                               prl(cmdToEStack(op - const_0));
+                               prl(createToEStack(op - const_0));
                                break;
                        }
 
                        case add: {
-                               prl(getTopTwo());
+                               prl(createTopTwoEStack());
                                prl("tempres := tempb + tempa;");
-                               prl(cmdToEStack("tempres"));
+                               prl(createToEStack("tempres"));
                                break;
                        }
                        case sub: {
-                               prl(getTopTwo());
+                               prl(createTopTwoEStack());
                                prl("tempres := tempb - tempa;");
-                               prl(cmdToEStack("tempres"));
+                               prl(createToEStack("tempres"));
                                break;
                        }
                        case mul: {
-                               prl(getTopTwo());
+                               prl(createTopTwoEStack());
                                prl("tempres := tempb * tempa;");
-                               prl(cmdToEStack("tempres"));
+                               prl(createToEStack("tempres"));
                                break;
                        }
                        case div: {
-                               prl(getTopTwo());
+                               prl(createTopTwoEStack());
                                prl("IF tempa = 0 THEN ERROR(\"division by zero\") FI;");
                                prl("tempres := tempb DIV tempa;");
-                               prl(cmdToEStack("tempres"));
+                               prl(createToEStack("tempres"));
                                break;
                        }
                        case rem: {
-                               prl(getTopTwo());
+                               prl(createTopTwoEStack());
                                prl("IF tempa = 0 THEN ERROR(\"division by zero\") FI;");
                                prl("tempres := tempb MOD tempa;");
-                               prl(cmdToEStack("tempres"));
+                               prl(createToEStack("tempres"));
                                break;
                        }
 
-                       //TODO neg, shl, shr, inc
-                       
-                       case inc  : {
+                       case neg: {
+                               prl(createTopEStack());
+                               prl(createToEStack("-tempa"));
+                               break;
+                       }
+
+                       case shl: {
+                               prl(createTopTwoEStack());
+                               prl("VAR <tempres :=tempb, i:=1 >:");
+                               prl("\tFOR i:=1 TO tempa STEP 1 DO tempres := tempres * 2 OD;");
+                               prl(createToEStack("tempres"));
+                               prl("ENDVAR;");
+                               break;
+                       }
+                       case shr: {
+                               prl(createTopTwoEStack());
+                               prl("VAR <tempres :=tempb, i:=1 >:");
+                               prl("\tFOR i:=1 TO tempa STEP 1 DO tempres := tempres DIV 2 OD;");
+                               prl(createToEStack("tempres"));
+                               prl("ENDVAR;");
+                               break;
+                       }
+
+                       case inc: {
                                int b1 = get(), b2 = get();
-                               prl(loc(b1)+" := " +loc(b1)+" + "+b2);
-                               break;
-                       }
-                       //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 dup, dup2
-                       
-                       case pop : {
-                                       prl(cmdPopEStack());
-                                       break;                                  
-                       }
-                       
+                               prl(createLocal(b1) + " := " + createLocal(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(createToEStack("LENGTH(mjvm_objects)"));
+                               break;
+                       }
+                       case newarray: {
+                               get();// 0 - bytes, 1 - words; ignore for now
+                               // TODO take into consideration 0/1
+                               prl(createTopEStack());
+                               prl("mjvm_arrays := mjvm_arrays ++ < ARRAY(tempa,0) >;");
+                               prl(createToEStack("LENGTH(mjvm_arrays)"));
+                               break;
+                       }
+
+                       case aload:
+                       case baload: {
+                               prl(createTopTwoEStack());
+                               prl(createToEStack(createArray("tempb") + "[tempa+1]"));
+                               break;
+                       }
+                       case astore:
+                       case bastore: {
+                               prl(createFromEStack("tempres"));
+                               prl(createTopTwoEStack());
+                               // 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 := " + createArray("tempb") + " > :");
+                               prl("tempArray[tempa+1]:=tempres ENDVAR;");
+                               break;
+                       }
+                       case arraylength: {
+                               prl(createTopEStack());
+                               // 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(createToEStack(1));
+                               break;
+                       }
+
+                       case dup: {
+                               prl(createTopEStack());
+                               prl(createToEStack("tempa"));
+                               prl(createToEStack("tempa"));
+                               break;
+                       }
+                       case dup2: {
+                               prl(createTopTwoEStack());
+                               prl(createToEStack("tempb"));
+                               prl(createToEStack("tempa"));
+                               prl(createToEStack("tempb"));
+                               prl(createToEStack("tempa"));
+                               break;
+                       }
+
+                       case pop: {
+                               prl(createPopEStack());
+                               break;
+                       }
+
                        case jmp: {
                                prl("CALL a" + (counter + get2()) + ";");
                                break;
@@ -446,10 +543,9 @@ public class mjc2wsl{
                        case jle:
                        case jgt:
                        case jge: {
-                               prl(getTopTwo());
-                               prl("IF tempb "+ getRelationFor(op)
-                                               +" tempa THEN CALL a" + (counter + get2())
-                                               +" ELSE CALL a" + (counter+1)
+                               prl(createTopTwoEStack());
+                               prl("IF tempb " + getRelationFor(op) + " tempa THEN CALL a"
+                                               + (counter + get2()) + " ELSE CALL a" + (counter + 1)
                                                + " FI;");
                                break;
                        }
@@ -460,62 +556,62 @@ public class mjc2wsl{
                        }
 
                        case return_: {
-                               prl("IF EMPTY?(mjvm_mstack) THEN CALL Z FI");
-                               //else we let things return             
-                               prl("END b"+counter+" ==");
+                               // we let the actions return
+                               // there is nothing to clean up
+                               prl("SKIP END b" + counter + " ==");
                                break;
                        }
                        case enter: {
                                int parameters = 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)));
+                               prl(createToMStack("mjvm_locals"));
+                               prl("mjvm_locals := ARRAY(" + locals + ",0);");
+                               for (int i = parameters - 1; i >= 0; i--)
+                                       prl(createFromEStack(createLocal(i)));
                                break;
                        }
                        case exit: {
-                               prl(cmdFromMStack("mjvm_locals"));
+                               prl(createFromMStack("mjvm_locals"));
                                break;
                        }
 
                        // read, print
-                       case read:{
-                               //TODO make it a char for 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));
+                               prl(createComment("char is read like a number", C_SPEC));
                        }
-                       case bread: {
+                       case read: {
                                prl("tempa := @String_To_Num(@Read_Line(Standard_Input_Port));");
-                               prl(cmdToEStack("tempa"));
+                               prl(createToEStack("tempa"));
                                break;
                        }
 
                        // the prints
-                       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));
+                               prl(createComment("char will be printed as a number code",
+                                               C_SPEC));
                        }
-                       case bprint: {
+                       case print: {
                                // TODO printing numbers needs different lengths of spacing
-                               prl(getTopTwo());
-                               pr(createComment("print spacing",C_SPEC));
+                               prl(createTopTwoEStack());
+                               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;
                        }
 
                        case trap: {
-                               prl("ERROR(\"Runtime error: trap("+get()+")\");");
+                               prl("ERROR(\"Runtime error: trap(" + get() + ")\");");
                                break;
                        }
-                       
-       
+
                        default:
                                prl(createComment("unknown op error: " + op, C_ERR));
-                               message("unknown op error: "+ op, M_ERR);
+                               message("unknown op error: " + op, M_ERR);
                                break;
                        }
 
@@ -523,15 +619,14 @@ public class mjc2wsl{
                        op = get();
                        if (op >= 0)
                                if (wasJump)
-                                               prl("SKIP END");
+                                       prl("SKIP END");
                                else
-                                               prl("CALL a" + counter + " END");
+                                       prl("CALL a" + counter + " END");
                }
                prl("CALL Z;\nSKIP END\nENDACTIONS;\n");
-               prl(getStandardEnd());
-
+               prl(createStandardEnd());
        }
-               
+
        public void convertFile(File f) {
                try {
                        convertStream(new FileInputStream(f));
@@ -578,11 +673,11 @@ public class mjc2wsl{
                                } else if (args[i].compareTo("--screen") == 0) {
                                        out = new PrintWriter(System.out);
                                } else if (args[i].compareTo("-d") == 0) {
-                                       printLevel = M_DEB;//print debug info
+                                       printLevel = M_DEB;// print debug info
                                } else if (args[i].compareTo("-v") == 0) {
-                                       printLevel = M_WAR;//print warnings
+                                       printLevel = M_WAR;// print warnings
                                } else if (args[i].compareTo("-q") == 0) {
-                                       printLevel = M_ERR+1;//no printing                              
+                                       printLevel = M_ERR + 1;// no printing
                                } else if (args[i].compareToIgnoreCase("--genEStackPrint") == 0) {
                                        genPrintEStackOnChange = true;
                                } else if (args[i].compareToIgnoreCase("--genAddrPause") == 0) {
@@ -596,7 +691,8 @@ public class mjc2wsl{
                                        genPrintEStackOnChange = true;
                                        genPrintForEachAddress = true;
                                        genPauseAfterEachAddress = true;
-                               }i++;
+                               }
+                               i++;
                        }
 
                        if (i >= args.length) {
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner