gitweb on Svarog

projekti pod git sistemom za održavanje verzija -- projects under the git version control system
version string in file to 0.1.8
[mjc2wsl.git] / src / mjc2wsl.java
index a140ced..dec76fe 100644 (file)
@@ -28,8 +28,11 @@ import java.util.*;
  * @author Doni Pracner, http://perun.dmi.rs/pracner http://quemaster.com
  */
 public class mjc2wsl{
-       public static String versionN = "0.1.6";
+       //default version name, used if the file is not found
+       private static String versionN = "0.1.x";
 
+       private String versionFile = "version.properties";
+       
        private TransMessages messages = new TransMessages();
 
        private boolean genPauseAfterEachAddress=false, 
@@ -38,6 +41,8 @@ public class mjc2wsl{
                
        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 = ' ';
        /**
@@ -136,6 +141,24 @@ public class mjc2wsl{
                return opMap;
        }
        
+       private Properties versionData;
+
+       private String getVersion() {
+               if (versionData == null) {
+                       versionData = new Properties();
+                       try {
+                               versionData.load(getClass().getResourceAsStream(versionFile));
+                       } catch (IOException e) {
+                               e.printStackTrace();
+                       }
+               }
+               String ver = versionData.getProperty("version");
+               if (ver != null)
+                       return ver;
+               else
+                       return versionN;
+       }
+       
        public String getOpString(int op) {
                return getOpMap().get(op);
        }
@@ -192,17 +215,16 @@ public class mjc2wsl{
        public String createStandardStart(int numWords){
                StringBuilder ret = new StringBuilder(
                        "C:\" This file automatically converted from microjava bytecode\";\n"
-                       +"C:\" with mjc2wsl v "+versionN+"\";\n\n");
-
-               ret.append(createAsciiString());
+                       +"C:\" with mjc2wsl v "+getVersion()+"\";\n");
 
-               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_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();
        }
@@ -216,31 +238,29 @@ public class mjc2wsl{
        }
 
        public String createStandardEnd(){
-               StringBuilder ret = new StringBuilder("SKIP\nENDVAR");
+               StringBuilder ret = new StringBuilder("SKIP\nENDVAR\n");
                ret.append("\nWHERE\n");
                
                ret.append("\nFUNCT CHR(num) ==:\n");
-               ret.append("\tSUBSTR(ascii,num,1)\n");
+               ret.append("\t@List_To_String(< num >)\n");
                ret.append("END\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("\nPROC Print_MJ_CHAR(val, format VAR)==\n");
-               ret.append(createComment("print spacing", C_SPEC));
-               ret.append("\n\tIF val=10 OR val=13 THEN\n");
-               ret.append("\t\tPRINT(\"\");");
-               ret.append("\tELSE\n");
-               ret.append("\t\tIF format>1 THEN\n\t\t\tFOR i:=2 TO ");
-               ret.append("format STEP 1 DO PRINFLUSH(\" \") OD\n");
-               ret.append("\t\tFI;\n\t\tPRINFLUSH(CHR(val))\n");
-               ret.append("\tFI\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\n");
+               ret.append("\nEND");
                return ret.toString();
        }
 
@@ -392,7 +412,7 @@ public class mjc2wsl{
                prl("SKIP;\n ACTIONS a" + (14 + mainAdr) + " :");
                int op = get();
                while (op >= 0) {
-                       prl(" a" + counter + " == ");
+                       prl(" a" + counter + " ==");
                        if (originalInComments)
                                prl(createComment(describeOpCode(op), C_OC));
                        if (genPrintForEachAddress) {
@@ -441,17 +461,18 @@ public class mjc2wsl{
 
                        case getfield: {
                                int f = get2();
+                               prl(createStartVar("tempa"));
                                prl(createTopEStack());
                                prl(createToEStack(createObject("tempa") + "[" + (f + 1) + "]"));
+                               prl(createEndVar());
                                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;
                        }
 
@@ -460,6 +481,11 @@ public class mjc2wsl{
                                break;
                        }
 
+                       case const_m1: {
+                               prl(createToEStack(-1));
+                               break;
+                       }
+
                        case const_0:
                        case const_1:
                        case const_2:
@@ -522,19 +548,23 @@ public class mjc2wsl{
                        }
 
                        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(createEndVar());
                                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(createEndVar());
                                break;
                        }
 
@@ -555,47 +585,56 @@ public class mjc2wsl{
                        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(createEndVar());
                                break;
                        }
 
                        case aload:
                        case baload: {
+                               prl(createStartVar("tempa", "tempb"));
                                prl(createTopTwoEStack());
                                prl(createToEStack(createArray("tempb") + "[tempa+1]"));
+                               prl(createEndVar());
                                break;
                        }
                        case astore:
                        case bastore: {
+                               prl(createStartVar("tempa", "tempb", "tempres"));
                                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: {
+                               prl(createStartVar("tempa", "tempb"));
                                prl(createTopEStack());
                                prl("tempb := LENGTH("+ createArray("tempa") + ");");
                                prl(createToEStack("tempb"));
+                               prl(createEndVar());
                                break;
                        }
 
                        case dup: {
+                               prl(createStartVar("tempa", "tempb"));
                                prl(createTopEStack());
                                prl(createToEStack("tempa"));
                                prl(createToEStack("tempa"));
+                               prl(createEndVar());
                                break;
                        }
                        case dup2: {
+                               prl(createStartVar("tempa", "tempb"));
                                prl(createTopTwoEStack());
                                prl(createToEStack("tempb"));
                                prl(createToEStack("tempa"));
                                prl(createToEStack("tempb"));
                                prl(createToEStack("tempa"));
+                               prl(createEndVar());
                                break;
                        }
 
@@ -617,10 +656,15 @@ public class mjc2wsl{
                        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());
+                               prl("IF mjvm_flag_jump = 1 THEN CALL a"
+                                               + (counter + get2()) 
+                                               + " ELSE CALL a" + (counter + 1)
+                                               + " FI;");
                                
                                break;
                        }
@@ -669,7 +713,11 @@ public class mjc2wsl{
                        case bprint: {
                                prl(createStartVar("tempa", "tempb"));
                                prl(createTopTwoEStack());
-                               prl("Print_MJ_CHAR(tempb,tempa);");
+                               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;
                        }
@@ -678,7 +726,12 @@ public class mjc2wsl{
                                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-SLENGTH(@String(tempb))))), tempb);");
+                               }
+                               else
+                                       prl("Print_MJ(tempb,tempa);");
                                prl(createEndVar());
                                break;
                        }
@@ -703,7 +756,7 @@ public class mjc2wsl{
                                        prl("CALL a" + counter + "\n END");
                }
                prl("SKIP\n END\nENDACTIONS;\n");
-               prl(createStandardEnd());
+               pr(createStandardEnd());
        }
 
        public void convertFile(File f) {
@@ -755,6 +808,10 @@ public class mjc2wsl{
        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() {
@@ -769,7 +826,7 @@ public class mjc2wsl{
        }
 
        public void printVersion() {
-               System.out.println("MicroJava bytecode to WSL converter. v " + versionN
+               System.out.println("MicroJava bytecode to WSL converter. v " + getVersion()
                                + ", by Doni Pracner");
        }
        
@@ -827,6 +884,12 @@ public class mjc2wsl{
                                        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++;
                        }
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner