gitweb on Svarog

projekti pod git sistemom za održavanje verzija -- projects under the git version control system
ant - added build task for versioning by using 'git describe'
[mjc2wsl.git] / src / mjc2wsl.java
index 388f1ad..14cc9e4 100644 (file)
@@ -28,14 +28,18 @@ import java.util.*;
  * @author Doni Pracner, http://perun.dmi.rs/pracner http://quemaster.com
  */
 public class mjc2wsl{
  * @author Doni Pracner, http://perun.dmi.rs/pracner http://quemaster.com
  */
 public class mjc2wsl{
-       public static String versionN = "0.1.6";
+       public static String versionN = "0.1.7";
 
        private TransMessages messages = new TransMessages();
 
        private boolean genPauseAfterEachAddress=false, 
                genPrintForEachAddress = false,
                genPrintEStackOnChange = false;
 
        private TransMessages messages = new TransMessages();
 
        private boolean genPauseAfterEachAddress=false, 
                genPrintForEachAddress = false,
                genPrintEStackOnChange = false;
+               
+       private boolean genPopPush=false;
        
        
+       private boolean genInlinePrint=false;
+
        /** Constant used for marking a regular comment from the original file */
        public static final char C_REG = ' ';
        /**
        /** Constant used for marking a regular comment from the original file */
        public static final char C_REG = ' ';
        /**
@@ -191,31 +195,51 @@ public class mjc2wsl{
                StringBuilder ret = new StringBuilder(
                        "C:\" This file automatically converted from microjava bytecode\";\n"
                        +"C:\" with mjc2wsl v "+versionN+"\";\n");
                StringBuilder ret = new StringBuilder(
                        "C:\" This file automatically converted from microjava bytecode\";\n"
                        +"C:\" with mjc2wsl v "+versionN+"\";\n");
-       
-               ret.append("BEGIN ");
-               ret.append("VAR < \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("\nBEGIN");
+               ret.append("\nVAR <\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_flag_jump := 0,");
+               ret.append("\n\tmjvm_objects := < >,");
+               ret.append("\n\tmjvm_estack := < >, mjvm_mstack := < > > :");
+
+               return ret.toString();
+       }
+
+       public String createAsciiString(){
+               StringBuilder ret = new StringBuilder("C:\"char array for ascii code conversions\";");
+               ret.append("\nascii := \"????????????????????????????????\"++\n");
+               ret.append("\" !\"++Quote++\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\";\n");
+
                return ret.toString();
        }
 
        public String createStandardEnd(){
                return ret.toString();
        }
 
        public String createStandardEnd(){
-               StringBuilder ret = new StringBuilder("SKIP\nENDVAR");
+               StringBuilder ret = new StringBuilder("SKIP\nENDVAR\n");
                ret.append("\nWHERE\n");
                
                ret.append("\nWHERE\n");
                
-               ret.append("\nPROC Print_MJ(val, format VAR)==\n");
-               ret.append(createComment("print spacing", C_SPEC));
-                               
-               ret.append("\n\tIF format>1 THEN\n\t\tFOR i:=2 TO ");
-               ret.append("format STEP 1 DO PRINFLUSH(\" \") OD\n");
-               ret.append("\tFI;\n\tPRINFLUSH(val)\nEND\n");
-               
-               ret.append("\nEND\n");
-               
+               ret.append("\nFUNCT CHR(num) ==:\n");
+               ret.append("\t@List_To_String(< num >)\n");
+               ret.append("END\n");
+
+               if (!genInlinePrint) {
+                               ret.append("\nPROC Print_MJ(val, format VAR)==\n");
+                               ret.append(createComment("print spacing", C_SPEC));
+                               ret.append("\n\tIF format>1 THEN\n\t\tFOR i:=2 TO ");
+                               ret.append("format STEP 1 DO PRINFLUSH(\" \") OD\n");
+                               ret.append("\tFI;\n\tPRINFLUSH(val)\nEND\n");
+
+                               ret.append("\nPROC Print_MJ_CHAR(val, format VAR)==\n");
+                               ret.append(createComment("print spacing", C_SPEC));
+                               ret.append("\n\tIF format>1 THEN\n\t\tFOR i:=2 TO ");
+                               ret.append("format STEP 1 DO PRINFLUSH(\" \") OD\n");
+                               ret.append("\tFI;\n\tPRINFLUSH(CHR(val))\n");
+                               ret.append("END\n");
+               }
+
+               ret.append("\nEND");
                return ret.toString();
        }
 
                return ret.toString();
        }
 
@@ -271,26 +295,37 @@ public class mjc2wsl{
        public static String createComment(String str, char type) {
                return "C:\"" + type + str.replace("\"", "''") + "\";";
        }
        public static String createComment(String str, char type) {
                return "C:\"" + type + str.replace("\"", "''") + "\";";
        }
+       
+       // generalised stack operations
+       
+       private String createToStack(String stack, String var){
+               if (genPopPush)
+                               return  "PUSH("+stack+"," + var + ");";
+               else
+                               return stack + " := <" + var + " > ++ " + stack +";";
+       }
 
 
-       //Expression stack
+       private String createFromStack(String stack, String var){
+               if (genPopPush)
+                       return "POP("+ var + ", "+stack+");";
+               else
+                       return var + ":= HEAD("+stack+"); "+stack+" := TAIL("+stack+");";
+       }
+//Expression stack
        
        private String createToEStack(int i) {
        
        private String createToEStack(int i) {
-               String res = "mjvm_estack := <" + i + " > ++ mjvm_estack;";
-               if (genPrintEStackOnChange)
-                       res += "PRINT(\"eStack\",mjvm_estack);";
-               return res;
+               return createToEStack(i+"");
        }
 
        private String createToEStack(String i) {
        }
 
        private String createToEStack(String i) {
-               String res = "mjvm_estack := <" + i + " > ++ mjvm_estack;";
+               String res = createToStack("mjvm_estack", i);
                if (genPrintEStackOnChange)
                        res += "PRINT(\"eStack\",mjvm_estack);";
                return res;
        }
 
        private String createFromEStack(String st) {
                if (genPrintEStackOnChange)
                        res += "PRINT(\"eStack\",mjvm_estack);";
                return res;
        }
 
        private String createFromEStack(String st) {
-               String res = st
-                               + " := HEAD(mjvm_estack); mjvm_estack := TAIL(mjvm_estack);";
+               String res = createFromStack("mjvm_estack",st);
                if (genPrintEStackOnChange)
                        res += "PRINT(\"eStack\",mjvm_estack);";
                return res;
                if (genPrintEStackOnChange)
                        res += "PRINT(\"eStack\",mjvm_estack);";
                return res;
@@ -314,15 +349,15 @@ public class mjc2wsl{
        //Method stack
 
        private String createToMStack(int i) {
        //Method stack
 
        private String createToMStack(int i) {
-               return "mjvm_mstack := <" + i + " > ++ mjvm_mstack;";
+               return createToMStack(i+"");
        }
 
        private String createToMStack(String i) {
        }
 
        private String createToMStack(String i) {
-               return "mjvm_mstack := <" + i + " > ++ mjvm_mstack;";
+               return createToStack("mjvm_mstack", i);
        }
 
        private String createFromMStack(String st) {
        }
 
        private String createFromMStack(String st) {
-               return st + " := HEAD(mjvm_mstack); mjvm_mstack := TAIL(mjvm_mstack);";
+               return createFromStack("mjvm_mstack", st);
        }
 
        private String getRelationFor(int opcode) throws Exception {
        }
 
        private String getRelationFor(int opcode) throws Exception {
@@ -356,9 +391,9 @@ public class mjc2wsl{
                prl("SKIP;\n ACTIONS a" + (14 + mainAdr) + " :");
                int op = get();
                while (op >= 0) {
                prl("SKIP;\n ACTIONS a" + (14 + mainAdr) + " :");
                int op = get();
                while (op >= 0) {
+                       prl(" a" + counter + " ==");
                        if (originalInComments)
                                prl(createComment(describeOpCode(op), C_OC));
                        if (originalInComments)
                                prl(createComment(describeOpCode(op), C_OC));
-                       prl(" a" + counter + " == ");
                        if (genPrintForEachAddress) {
                                prl("PRINT(\"a" + counter + "\");");
                                if (genPauseAfterEachAddress)
                        if (genPrintForEachAddress) {
                                prl("PRINT(\"a" + counter + "\");");
                                if (genPauseAfterEachAddress)
@@ -373,7 +408,10 @@ public class mjc2wsl{
                        case load_1:
                        case load_2:
                        case load_3: {
                        case load_1:
                        case load_2:
                        case load_3: {
-                               prl(createToEStack(createLocal(op - load_0)));
+                               prl(createStartVar("tempa"));
+                               prl("tempa :="+createLocal(op - load_0)+";");
+                               prl(createToEStack("tempa"));
+                               prl(createEndVar());
                                break;
                        }
                        case store: {
                                break;
                        }
                        case store: {
@@ -384,7 +422,10 @@ public class mjc2wsl{
                        case store_1:
                        case store_2:
                        case store_3: {
                        case store_1:
                        case store_2:
                        case store_3: {
-                               prl(createFromEStack(createLocal(op - store_0)));
+                               prl(createStartVar("tempa"));
+                               prl(createFromEStack("tempa"));
+                               prl(createLocal(op - store_0)+" := tempa;");
+                               prl(createEndVar());
                                break;
                        }
 
                                break;
                        }
 
@@ -399,17 +440,18 @@ public class mjc2wsl{
 
                        case getfield: {
                                int f = get2();
 
                        case getfield: {
                                int f = get2();
+                               prl(createStartVar("tempa"));
                                prl(createTopEStack());
                                prl(createToEStack(createObject("tempa") + "[" + (f + 1) + "]"));
                                prl(createTopEStack());
                                prl(createToEStack(createObject("tempa") + "[" + (f + 1) + "]"));
+                               prl(createEndVar());
                                break;
                        }
                        case putfield: {
                                int f = get2();
                                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;");
+                               prl(createStartVar("tempa", "tempb"));
+                               prl(createTopTwoEStack());                              
+                               prl(createObject("tempb") + "[" + (f + 1) + "]:=tempa;");
+                               prl(createEndVar());
                                break;
                        }
 
                                break;
                        }
 
@@ -418,6 +460,11 @@ public class mjc2wsl{
                                break;
                        }
 
                                break;
                        }
 
+                       case const_m1: {
+                               prl(createToEStack(-1));
+                               break;
+                       }
+
                        case const_0:
                        case const_1:
                        case const_2:
                        case const_0:
                        case const_1:
                        case const_2:
@@ -480,19 +527,23 @@ public class mjc2wsl{
                        }
 
                        case shl: {
                        }
 
                        case shl: {
+                               prl(createStartVar("tempa", "tempb"));
                                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;");
                                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;");
+                               prl(createEndVar());
                                break;
                        }
                        case shr: {
                                break;
                        }
                        case shr: {
+                               prl(createStartVar("tempa", "tempb"));
                                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;");
                                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;");
+                               prl(createEndVar());
                                break;
                        }
 
                                break;
                        }
 
@@ -513,47 +564,56 @@ public class mjc2wsl{
                        case newarray: {
                                get();// 0 - bytes, 1 - words; ignore for now
                                // TODO take into consideration 0/1
                        case newarray: {
                                get();// 0 - bytes, 1 - words; ignore for now
                                // TODO take into consideration 0/1
+                               prl(createStartVar("tempa"));
                                prl(createTopEStack());
                                prl("mjvm_arrays := mjvm_arrays ++ < ARRAY(tempa,0) >;");
                                prl(createToEStack("LENGTH(mjvm_arrays)"));
                                prl(createTopEStack());
                                prl("mjvm_arrays := mjvm_arrays ++ < ARRAY(tempa,0) >;");
                                prl(createToEStack("LENGTH(mjvm_arrays)"));
+                               prl(createEndVar());
                                break;
                        }
 
                        case aload:
                        case baload: {
                                break;
                        }
 
                        case aload:
                        case baload: {
+                               prl(createStartVar("tempa", "tempb"));
                                prl(createTopTwoEStack());
                                prl(createToEStack(createArray("tempb") + "[tempa+1]"));
                                prl(createTopTwoEStack());
                                prl(createToEStack(createArray("tempb") + "[tempa+1]"));
+                               prl(createEndVar());
                                break;
                        }
                        case astore:
                        case bastore: {
                                break;
                        }
                        case astore:
                        case bastore: {
+                               prl(createStartVar("tempa", "tempb", "tempres"));
                                prl(createFromEStack("tempres"));
                                prl(createTopTwoEStack());
                                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;");
+                               prl("mjvm_arrays[tempb][tempa+1]:=tempres;");
+                               prl(createEndVar());
                                break;
                        }
                        case arraylength: {
                                break;
                        }
                        case arraylength: {
+                               prl(createStartVar("tempa", "tempb"));
                                prl(createTopEStack());
                                prl("tempb := LENGTH("+ createArray("tempa") + ");");
                                prl(createToEStack("tempb"));
                                prl(createTopEStack());
                                prl("tempb := LENGTH("+ createArray("tempa") + ");");
                                prl(createToEStack("tempb"));
+                               prl(createEndVar());
                                break;
                        }
 
                        case dup: {
                                break;
                        }
 
                        case dup: {
+                               prl(createStartVar("tempa", "tempb"));
                                prl(createTopEStack());
                                prl(createToEStack("tempa"));
                                prl(createToEStack("tempa"));
                                prl(createTopEStack());
                                prl(createToEStack("tempa"));
                                prl(createToEStack("tempa"));
+                               prl(createEndVar());
                                break;
                        }
                        case dup2: {
                                break;
                        }
                        case dup2: {
+                               prl(createStartVar("tempa", "tempb"));
                                prl(createTopTwoEStack());
                                prl(createToEStack("tempb"));
                                prl(createToEStack("tempa"));
                                prl(createToEStack("tempb"));
                                prl(createToEStack("tempa"));
                                prl(createTopTwoEStack());
                                prl(createToEStack("tempb"));
                                prl(createToEStack("tempa"));
                                prl(createToEStack("tempb"));
                                prl(createToEStack("tempa"));
+                               prl(createEndVar());
                                break;
                        }
 
                                break;
                        }
 
@@ -575,10 +635,15 @@ public class mjc2wsl{
                        case jge: {
                                prl(createStartVar("tempa", "tempb"));
                                prl(createTopTwoEStack());
                        case jge: {
                                prl(createStartVar("tempa", "tempb"));
                                prl(createTopTwoEStack());
-                               prl("IF tempb " + getRelationFor(op) + " tempa THEN CALL a"
-                                               + (counter + get2()) + " ELSE CALL a" + (counter + 1)
+                               prl("IF tempb " + getRelationFor(op) 
+                                               + " tempa THEN mjvm_flag_jump := 1"
+                                               + " ELSE mjvm_flag_jump := 0" 
                                                + " FI;");
                                prl(createEndVar());
                                                + " FI;");
                                prl(createEndVar());
+                               prl("IF mjvm_flag_jump = 1 THEN CALL a"
+                                               + (counter + get2()) 
+                                               + " ELSE CALL a" + (counter + 1)
+                                               + " FI;");
                                
                                break;
                        }
                                
                                break;
                        }
@@ -625,17 +690,27 @@ public class mjc2wsl{
 
                        // the prints
                        case bprint: {
 
                        // the prints
                        case bprint: {
-                               // TODO need to make it a char on print
-                               messages.message("chars will be printed as number codes", TransMessages.M_WAR);
-                               prl(createComment("char will be printed as a number code",
-                                               C_SPEC));
+                               prl(createStartVar("tempa", "tempb"));
+                               prl(createTopTwoEStack());
+                               if (genInlinePrint){
+                                       prl(createComment("print spacing and transformation",C_SPEC));
+                                       prl("PRINFLUSH(SUBSTR(\"          \", 0, MIN(10, MAX(0,tempa-1))), @List_To_String(< tempb >));");
+                               } else
+                                       prl("Print_MJ_CHAR(tempb,tempa);");
+                               prl(createEndVar());
+                               break;
                        }
                        case print: {
                                // TODO printing numbers needs different lengths of spacing
                                prl(createStartVar("tempa", "tempb"));
 
                                prl(createTopTwoEStack());
                        }
                        case print: {
                                // TODO printing numbers needs different lengths of spacing
                                prl(createStartVar("tempa", "tempb"));
 
                                prl(createTopTwoEStack());
-                               prl("Print_MJ(tempb,tempa);");
+                               if (genInlinePrint){
+                                       prl(createComment("print spacing",C_SPEC));
+                                       prl("PRINFLUSH(SUBSTR(\"          \", 0, MIN(10, MAX(0, tempa-1))), tempb);");
+                               }
+                               else
+                                       prl("Print_MJ(tempb,tempa);");
                                prl(createEndVar());
                                break;
                        }
                                prl(createEndVar());
                                break;
                        }
@@ -660,7 +735,7 @@ public class mjc2wsl{
                                        prl("CALL a" + counter + "\n END");
                }
                prl("SKIP\n END\nENDACTIONS;\n");
                                        prl("CALL a" + counter + "\n END");
                }
                prl("SKIP\n END\nENDACTIONS;\n");
-               prl(createStandardEnd());
+               pr(createStandardEnd());
        }
 
        public void convertFile(File f) {
        }
 
        public void convertFile(File f) {
@@ -684,6 +759,8 @@ public class mjc2wsl{
                System.out.println();
                printHelpOutput();
                System.out.println();
                System.out.println();
                printHelpOutput();
                System.out.println();
+               printHelpDirectives();
+               System.out.println();
                printHelpGenerating();
                System.out.println();
                printHelpHelp();
                printHelpGenerating();
                System.out.println();
                printHelpHelp();
@@ -707,6 +784,15 @@ public class mjc2wsl{
                System.out.println("  --genAll   short for applying all code generation");
        }
 
                System.out.println("  --genAll   short for applying all code generation");
        }
 
+       public void printHelpDirectives(){
+               System.out.println("Alternatives for code generation:");
+               System.out.println("  --genPopPush generate POP/PUSH instead of TAIL/HEAD");
+               System.out.println("  --genHeadTail generate TAIL/HEAD instead of POP/PUSH ");
+               System.out.println();
+               System.out.println("  --genInlinePrint generate prints directly instead of procedure calls");
+               System.out.println("  --genProcedurePrint generate prints as custom procedure calls");
+       }
+
        public void printHelpHelp() {
                System.out.println("Help and info options");
                System.out.println("  -h basic help");
        public void printHelpHelp() {
                System.out.println("Help and info options");
                System.out.println("  -h basic help");
@@ -775,6 +861,14 @@ public class mjc2wsl{
                                        genPrintEStackOnChange = true;
                                        genPrintForEachAddress = true;
                                        genPauseAfterEachAddress = true;
                                        genPrintEStackOnChange = true;
                                        genPrintForEachAddress = true;
                                        genPauseAfterEachAddress = true;
+                               } else if (args[i].compareToIgnoreCase("--genPopPush") == 0) {
+                                       genPopPush = true;
+                               } else if (args[i].compareToIgnoreCase("--genInlinePrint") == 0) {
+                                       genInlinePrint = true;
+                               } else if (args[i].compareToIgnoreCase("--genHeadTail") == 0) {
+                                       genPopPush = false;
+                               } else if (args[i].compareToIgnoreCase("--genProcedurePrint") == 0) {
+                                       genInlinePrint = false;
                                }
                                i++;
                        }
                                }
                                i++;
                        }
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner