X-Git-Url: http://svarog.pmf.uns.ac.rs/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Fmjc2wsl.java;h=dec76fe6b66d64c19d3d707e6dfa63c010d9c613;hb=eef0ddbd915d0f1d2df423a3265266b5c6651537;hp=e23b10e439ba4ea93b2f2c7df0d1743dcbfdb582;hpb=3b31cbbe8fa685918d254c4106cfef524518367e;p=mjc2wsl.git
diff --git a/src/mjc2wsl.java b/src/mjc2wsl.java
index e23b10e..dec76fe 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,14 +28,21 @@ import java.util.*;
* @author Doni Pracner, http://perun.dmi.rs/pracner http://quemaster.com
*/
public class mjc2wsl{
- public static String versionN = "0.1.4";
+ //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,
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 = ' ';
/**
@@ -116,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);
}
@@ -172,24 +215,69 @@ 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");
-
- 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 > :");
-
+ +"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_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 "SKIP\nENDVAR";
+ StringBuilder ret = new StringBuilder("SKIP\nENDVAR\n");
+ ret.append("\nWHERE\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();
}
+ 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) + "]";
@@ -228,26 +316,37 @@ public class mjc2wsl{
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) {
- String res = "mjvm_estack := <" + i + " > ++ mjvm_estack;";
- if (genPrintEStackOnChange)
- res += "PRINT(\"eStack\",mjvm_estack);";
- return res;
+ return createToEStack(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) {
- 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;
@@ -271,15 +370,15 @@ public class mjc2wsl{
//Method stack
private String createToMStack(int i) {
- return "mjvm_mstack := <" + i + " > ++ mjvm_mstack;";
+ return createToMStack(i+"");
}
private String createToMStack(String i) {
- return "mjvm_mstack := <" + i + " > ++ mjvm_mstack;";
+ return createToStack("mjvm_mstack", i);
}
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 {
@@ -310,13 +409,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) {
+ prl(" a" + counter + " ==");
if (originalInComments)
prl(createComment(describeOpCode(op), C_OC));
- prl("a" + counter + " == ");
if (genPrintForEachAddress) {
prl("PRINT(\"a" + counter + "\");");
if (genPauseAfterEachAddress)
@@ -331,7 +429,10 @@ public class mjc2wsl{
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: {
@@ -342,7 +443,10 @@ public class mjc2wsl{
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;
}
@@ -357,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;
}
@@ -376,6 +481,11 @@ public class mjc2wsl{
break;
}
+ case const_m1: {
+ prl(createToEStack(-1));
+ break;
+ }
+
case const_0:
case const_1:
case const_2:
@@ -387,58 +497,74 @@ public class mjc2wsl{
}
case add: {
+ prl(createStartVar("tempa", "tempb", "tempres"));
prl(createTopTwoEStack());
prl("tempres := tempb + tempa;");
prl(createToEStack("tempres"));
+ prl(createEndVar());
break;
}
case sub: {
+ prl(createStartVar("tempa", "tempb", "tempres"));
prl(createTopTwoEStack());
prl("tempres := tempb - tempa;");
prl(createToEStack("tempres"));
+ prl(createEndVar());
break;
}
case mul: {
+ prl(createStartVar("tempa", "tempb", "tempres"));
prl(createTopTwoEStack());
prl("tempres := tempb * tempa;");
prl(createToEStack("tempres"));
+ prl(createEndVar());
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;
}
case neg: {
+ prl(createStartVar("tempa"));
prl(createTopEStack());
prl(createToEStack("-tempa"));
+ prl(createEndVar());
break;
}
case shl: {
+ prl(createStartVar("tempa", "tempb"));
prl(createTopTwoEStack());
prl("VAR :");
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 :");
prl("\tFOR i:=1 TO tempa STEP 1 DO tempres := tempres DIV 2 OD;");
prl(createToEStack("tempres"));
prl("ENDVAR;");
+ prl(createEndVar());
break;
}
@@ -459,52 +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());
- // TODO make an array length function of some sort!
- prl(createComment(
- "array length not known - LENGTH not aplicable to arrays",
- C_ERR));
- messages.message("array length not known - LENGTH not aplicable to arrays", TransMessages.M_ERR);
- prl(createComment("put 1 on the stack for consistency", C_SPEC));
- prl(createToEStack(1));
+ 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;
}
@@ -524,10 +654,18 @@ public class mjc2wsl{
case jle:
case jgt:
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;
}
@@ -539,7 +677,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: {
@@ -564,24 +702,37 @@ public class mjc2wsl{
prl(createComment("char is read like a number", C_SPEC));
}
case read: {
+ prl(createStartVar("tempa"));
prl("tempa := @String_To_Num(@Read_Line(Standard_Input_Port));");
prl(createToEStack("tempa"));
+ prl(createEndVar());
break;
}
// 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());
- 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);");
+ 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;
}
@@ -600,12 +751,12 @@ 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(createStandardEnd());
+ prl("SKIP\n END\nENDACTIONS;\n");
+ pr(createStandardEnd());
}
public void convertFile(File f) {
@@ -617,14 +768,66 @@ public class mjc2wsl{
}
public void printHelp() {
- System.out.println("MicroJava bytecode to WSL converter. v " + versionN
- + ", by Doni Pracner");
+ printVersion();
+ printUsage();
+ printHelpOutput();
+ printHelpHelp();
+ }
+
+ public void printLongHelp() {
+ printVersion();
+ printUsage();
+ System.out.println();
+ printHelpOutput();
+ System.out.println();
+ printHelpDirectives();
+ 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 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");
+ 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]");
- 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 void printVersion() {
+ System.out.println("MicroJava bytecode to WSL converter. v " + getVersion()
+ + ", by Doni Pracner");
}
public String makeDefaultOutName(String inname){
@@ -643,6 +846,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)
@@ -672,6 +882,14 @@ public class mjc2wsl{
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++;
}