X-Git-Url: http://svarog.pmf.uns.ac.rs/gitweb/?p=mjc2wsl.git;a=blobdiff_plain;f=src%2Fmjc2wsl.java;h=bc6f4656427485f057a9bfe8e4b4676113b3d616;hp=dafe6e441026954abe0e8b4c6271f5884e810b87;hb=dd51d4504d3c15dd2c58bf9ece80cd182b02d5d5;hpb=2bb2844b53ef2fdee27da8e235f070c5b741f28c;ds=sidebyside diff --git a/src/mjc2wsl.java b/src/mjc2wsl.java index dafe6e4..bc6f465 100644 --- a/src/mjc2wsl.java +++ b/src/mjc2wsl.java @@ -1,3 +1,21 @@ +/* + Copyright (C) 2014 Doni Pracner + + This file is part of mjc2wsl. + + mjc2wsl is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + mjc2wsl is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with mjc2wsl. If not, see . +*/ import java.io.*; import java.util.*; @@ -10,28 +28,10 @@ import java.util.*; * @author Doni Pracner, http://perun.dmi.rs/pracner http://quemaster.com */ public class mjc2wsl{ - public static String versionN = "0.1.4"; + public static String versionN = "0.1.5"; + + private TransMessages messages = new TransMessages(); - public static final int M_ERR = 2, M_WAR = 1, M_DEB = 0; - - private int printLevel = M_ERR; - - private int[] messageCounters = new int[M_ERR+1]; - - private void message(String mes, int level){ - if (level>=printLevel) - System.out.println(mes); - messageCounters[level]++; - } - - private void printMessageCounters(){ - printMessageCounters(System.out); - } - - private void printMessageCounters(PrintStream out){ - out.println("total errors:"+messageCounters[M_ERR]+" warnings:"+messageCounters[M_WAR]); - } - private boolean genPauseAfterEachAddress=false, genPrintForEachAddress = false, genPrintEStackOnChange = false; @@ -108,31 +108,6 @@ public class mjc2wsl{ bprint = 56, trap = 57; - public String createStandardStart(){ - return createStandardStart(10); - } - - public String createStandardStart(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 > :"); - - return ret.toString(); - } - - public String createStandardEnd(){ - return "SKIP\nENDVAR"; - } - private boolean originalInComments = false; private HashMap opMap = null; @@ -208,6 +183,56 @@ public class mjc2wsl{ return (get2() << 16) + (get2() << 16 >>> 16); } + public String createStandardStart(){ + return createStandardStart(10); + } + + public String createStandardStart(int numWords){ + 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 < 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 := < > > : "); + + return ret.toString(); + } + + public String createStandardEnd(){ + StringBuilder ret = new StringBuilder("SKIP\nENDVAR"); + 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"); + + return ret.toString(); + } + + private String createStartVar(String... vars){ + StringBuilder ret = new StringBuilder("VAR < "); + ret.append(vars[0] + " := 0"); + for (int i=1; i : "); + + return ret.toString(); + } + + private String createEndVar(){ + return "ENDVAR;"; + } + private String createLocal(int i) { // arrays start at 1 in WSL, so we need an offset return "mjvm_locals[" + (i + 1) + "]"; @@ -328,13 +353,12 @@ public class mjc2wsl{ int mainAdr = get4(); prl(createStandardStart(numberOfWords)); - prl("SKIP;\n ACTIONS A_S_start:\n A_S_start == CALL a" + (14 + mainAdr) - + " END"); + prl("SKIP;\n ACTIONS a" + (14 + mainAdr) + " :"); int op = get(); while (op >= 0) { if (originalInComments) prl(createComment(describeOpCode(op), C_OC)); - prl("a" + counter + " == "); + prl(" a" + counter + " == "); if (genPrintForEachAddress) { prl("PRINT(\"a" + counter + "\");"); if (genPauseAfterEachAddress) @@ -423,17 +447,21 @@ public class mjc2wsl{ break; } case div: { + prl(createStartVar("tempa", "tempb", "tempres")); prl(createTopTwoEStack()); prl("IF tempa = 0 THEN ERROR(\"division by zero\") FI;"); prl("tempres := tempb DIV tempa;"); prl(createToEStack("tempres")); + prl(createEndVar()); break; } case rem: { + prl(createStartVar("tempa", "tempb", "tempres")); prl(createTopTwoEStack()); prl("IF tempa = 0 THEN ERROR(\"division by zero\") FI;"); prl("tempres := tempb MOD tempa;"); prl(createToEStack("tempres")); + prl(createEndVar()); break; } @@ -501,14 +529,8 @@ public class mjc2wsl{ } 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)); + prl("tempb := LENGTH("+ createArray("tempa") + ");"); + prl(createToEStack("tempb")); break; } @@ -558,7 +580,7 @@ public class mjc2wsl{ case return_: { // we let the actions return // there is nothing to clean up - prl("SKIP END b" + counter + " =="); + prl("SKIP\n END\n b" + counter + " =="); break; } case enter: { @@ -579,7 +601,7 @@ public class mjc2wsl{ // read, print case bread: { // TODO make it a char for read - message("char is read like a number", M_WAR); + messages.message("char is read like a number", TransMessages.M_WAR); prl(createComment("char is read like a number", C_SPEC)); } case read: { @@ -591,16 +613,14 @@ public class mjc2wsl{ // the prints case bprint: { // TODO need to make it a char on print - message("chars will be printed as number codes", M_WAR); + messages.message("chars will be printed as number codes", TransMessages.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(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);"); + prl("Print_MJ(tempb,tempa);"); break; } @@ -611,7 +631,7 @@ public class mjc2wsl{ default: prl(createComment("unknown op error: " + op, C_ERR)); - message("unknown op error: " + op, M_ERR); + messages.message("unknown op error: " + op, TransMessages.M_ERR); break; } @@ -619,11 +639,11 @@ public class mjc2wsl{ op = get(); if (op >= 0) if (wasJump) - prl("SKIP END"); + prl("SKIP\n END"); else - prl("CALL a" + counter + " END"); + prl("CALL a" + counter + "\n END"); } - prl("CALL Z;\nSKIP END\nENDACTIONS;\n"); + prl("SKIP\n END\nENDACTIONS;\n"); prl(createStandardEnd()); } @@ -636,14 +656,55 @@ public class mjc2wsl{ } public void printHelp() { + printVersion(); + printUsage(); + printHelpOutput(); + printHelpHelp(); + } + + public void printLongHelp() { + printVersion(); + printUsage(); + System.out.println(); + printHelpOutput(); + System.out.println(); + printHelpGenerating(); + System.out.println(); + printHelpHelp(); + } + + public void printHelpOutput() { + System.out.println("Output options:"); + System.out.println(" --screen print output to screen"); + System.out.println(" -o --oc[+-] include original code in comments"); + System.out.println(" -v verbose, print warning messages"); + System.out.println(" -q quiet; don't print even the error messages"); + System.out.println(" -d print detailed debug messages"); + } + + public void printHelpGenerating() { + System.out.println("Options for generating extra code for tracking code execution"); + System.out.println(" --genEStackPrint generate print for all EStack changes"); + System.out.println(" --genAddrPrint generate prints after every address of the original code "); + System.out.println(" --genAddrPause generate a pause after every address of the original code "); + System.out.println(" --genAddr short for --genAddrPrint and --genAddrPause"); + System.out.println(" --genAll short for applying all code generation"); + } + + public void printHelpHelp() { + System.out.println("Help and info options"); + System.out.println(" -h basic help"); + System.out.println(" --help print more detailed help"); + System.out.println(" --version or -version print version and exit"); + } + + public void printUsage(){ + System.out.println("usage:\n\t mjc2wsl {options} filename [outfile]"); + } + + public void printVersion() { System.out.println("MicroJava bytecode to WSL converter. v " + versionN + ", by Doni Pracner"); - System.out.println("usage:\n\t mjc2wsl {options} filename [outfile]"); - System.out.println("options:\n\t--screen print output to screen"); - System.out.println("\t-o --oc[+-] include original code in comments"); - System.out.println("\t-v verbose, print warning messages"); - System.out.println("\t-q don't print even the error messages"); - System.out.println("\t-d print detailed debug messages"); } public String makeDefaultOutName(String inname){ @@ -662,6 +723,13 @@ public class mjc2wsl{ if (args[i].compareTo("-h") == 0) { printHelp(); return; + } else if (args[i].compareTo("--help") == 0) { + printLongHelp(); + return; + } else if (args[i].compareTo("--version") == 0 + || args[i].compareTo("-version") == 0) { + printVersion(); + return; } else if (args[i].compareTo("-o") == 0 || args[i].startsWith("--oc")) { if (args[i].length() == 2) @@ -673,11 +741,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 + messages.setPrintLevel(TransMessages.M_DEB);// print debug info } else if (args[i].compareTo("-v") == 0) { - printLevel = M_WAR;// print warnings + messages.setPrintLevel(TransMessages.M_WAR);// print warnings } else if (args[i].compareTo("-q") == 0) { - printLevel = M_ERR + 1;// no printing + messages.setPrintLevel(TransMessages.M_QUIET);// no printing } else if (args[i].compareToIgnoreCase("--genEStackPrint") == 0) { genPrintEStackOnChange = true; } else if (args[i].compareToIgnoreCase("--genAddrPause") == 0) { @@ -724,7 +792,7 @@ public class mjc2wsl{ long mili = Calendar.getInstance().getTimeInMillis() - now.getTimeInMillis(); System.out.println("conversion time:" + mili + " ms"); - printMessageCounters(); + messages.printMessageCounters(); out.close(); } else System.out.println("file does not exist");