gitweb on Svarog

projekti pod git sistemom za održavanje verzija -- projects under the git version control system
mjc2wsl - new option to toggle locals as array or separate
[mjc2wsl.git] / src / com / quemaster / transformations / mjc2wsl / mjc2wsl.java
index dab321b..233d5ae 100644 (file)
@@ -1,6 +1,6 @@
 package com.quemaster.transformations.mjc2wsl;
 /*
-   Copyright (C) 2014  Doni Pracner
+   Copyright (C) 2014,2015, 2016  Doni Pracner
    
     This file is part of mjc2wsl.
 
@@ -40,9 +40,9 @@ import com.quemaster.transformations.TransMessages;
  */
 public class mjc2wsl{
        //default version name, used if the file is not found
-       private static String versionN = "0.2.x";
+       private static String versionN = "v0.2.x";
 
-       private String versionFile = "/version.properties";
+       private String versionFile = "version.properties";
        
        private TransMessages messages = new TransMessages();
 
@@ -56,6 +56,9 @@ public class mjc2wsl{
        
        private boolean genLocalVars = true;
 
+       // make an array for all local variables OTHERWISE make them separate
+       private boolean genLocalsAsArray = true;
+
        /** Constant used for marking a regular comment from the original file */
        public static final char C_REG = ' ';
        /**
@@ -137,8 +140,9 @@ public class mjc2wsl{
                        versionData = new Properties();
                        try {
                                versionData.load(getClass().getResourceAsStream(versionFile));
-                       } catch (IOException e) {
-                               e.printStackTrace();
+                       } catch (IOException | NullPointerException e) {
+                               // it doesn't matter
+                               //e.printStackTrace();
                        }
                }
                String ver = versionData.getProperty("version");
@@ -176,7 +180,7 @@ public class mjc2wsl{
        public String createStandardStart(int numWords){
                StringBuilder ret = new StringBuilder(
                        "C:\" This file automatically converted from microjava bytecode\";\n"
-                       +"C:\" with mjc2wsl "+getVersion()+"\";\n");
+                       +"C:\" with mjc2wsl "+getVersion()+"\";\n");
 
                ret.append("\nBEGIN");
                ret.append("\nVAR <\n\t");
@@ -184,7 +188,10 @@ public class mjc2wsl{
                        ret.append("\n\ttempa := 0, tempb :=0, tempres := 0,");
                } else
                        ret.append("\n\tmjvm_flag_jump := 0,");
-               ret.append("mjvm_locals := ARRAY(1,0),");
+
+               if (genLocalsAsArray)
+                       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 := < >,");
@@ -206,7 +213,7 @@ public class mjc2wsl{
                ret.append("\nWHERE\n");
                
                ret.append("\nFUNCT CHR(num) ==:\n");
-               ret.append("\t@List_To_String(< num >)\n");
+               ret.append("\t(@List_To_String(< num >))\n");
                ret.append("END\n");
 
                if (!genInlinePrint) {
@@ -245,7 +252,10 @@ public class mjc2wsl{
        
        private String createLocal(int i) {
                // arrays start at 1 in WSL, so we need an offset
-               return "mjvm_locals[" + (i + 1) + "]";
+               if (genLocalsAsArray)
+                       return "mjvm_locals[" + (i + 1) + "]";
+               else
+                       return "mjvm_locals_" + i;
        }
 
        private String createStatic(int i) {
@@ -353,6 +363,10 @@ public class mjc2wsl{
                
                prl(createStandardStart(mjInput.getNumberOfWords(this)));
                prl("SKIP;\n ACTIONS a" + (14 + mjInput.getMainAdr(this)) + " :");
+
+               // the number of Locals for procedures; need to remember it for exits
+               int numberOfLocals = 0;
+
                int op = mjInput.get();
                while (op >= 0) {
                        prl(" a" + mjInput.getCounter() + " ==");
@@ -634,15 +648,29 @@ public class mjc2wsl{
                        case enter: {
                                int parameters = mjInput.get();
 
-                               int locals = mjInput.get();
-                               prl(createToMStack("mjvm_locals"));
-                               prl("mjvm_locals := ARRAY(" + locals + ",0);");
+                               numberOfLocals = mjInput.get();
+                               if (genLocalsAsArray) {
+                                       prl(createToMStack("mjvm_locals"));
+                                       prl("mjvm_locals := ARRAY(" + numberOfLocals + ",0);");
+                               } else {
+                                       // TODO maybe we should generate VAR block for these somewhere
+                                       for (int i = 0; i < numberOfLocals; i++) {
+                                               prl(createToMStack(createLocal(i)));
+                                       }
+                               }
+
                                for (int i = parameters - 1; i >= 0; i--)
                                        prl(createFromEStack(createLocal(i)));
                                break;
                        }
                        case exit: {
-                               prl(createFromMStack("mjvm_locals"));
+                               if (genLocalsAsArray) {
+                                       prl(createFromMStack("mjvm_locals"));
+                               } else {
+                                       // there are as many locals as defined in the last enter
+                                       for (int i = numberOfLocals -1; i >= 0; i--)
+                                               prl(createFromMStack(createLocal(i)));
+                               }
                                break;
                        }
 
@@ -776,6 +804,11 @@ public class mjc2wsl{
                System.out.println(" --genLocalVars generate local VAR block for temp variables");
                System.out.print(!genLocalVars?'*':' ');
                System.out.println(" --genGlobalVars do NOT generate local VAR block for temp variables");
+               System.out.println();
+               System.out.print(genLocalsAsArray?'*':' ');
+               System.out.println(" --genLocalsAsArray generate local variables as an array");
+               System.out.print(!genLocalsAsArray?'*':' ');
+               System.out.println(" --genLocalsSeparate generate local variables as separate entities");
        }
 
        public void printHelpHelp() {
@@ -790,7 +823,7 @@ public class mjc2wsl{
        }
 
        public void printVersion() {
-               System.out.println("MicroJava bytecode to WSL converter. v " + getVersion()
+               System.out.println("MicroJava bytecode to WSL converter " + getVersion()
                                + ", by Doni Pracner");
        }
        
@@ -806,7 +839,7 @@ public class mjc2wsl{
                        printHelp();
                } else {
                        int i = 0;
-                       while (i < args.length && args[i].charAt(0) == '-') {
+                       while (i < args.length && args[i].length() > 0 && args[i].charAt(0) == '-') {
                                if (args[i].compareTo("-h") == 0) {
                                        printHelp();
                                        return;
@@ -858,6 +891,10 @@ public class mjc2wsl{
                                        genLocalVars = true;
                                } else if (args[i].compareToIgnoreCase("--genGlobalVars") == 0) {
                                        genLocalVars = false;
+                               } else if (args[i].compareToIgnoreCase("--genLocalsAsArray") == 0) {
+                                       genLocalsAsArray = true;
+                               } else if (args[i].compareToIgnoreCase("--genLocalsSeparate") == 0) {
+                                       genLocalsAsArray = false;
                                } else {
                                        System.err.println("unknown option: "+args[i]);
                                }
@@ -870,7 +907,7 @@ public class mjc2wsl{
                        }
 
                        Path p = FileSystems.getDefault().getPath(args[i]);
-                       if (!Files.exists(p)){
+                       if (!Files.isRegularFile(p)){
                                System.err.println("input file does not exist");
                                System.exit(1);
                        }
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner