X-Git-Url: http://svarog.pmf.uns.ac.rs/gitweb/?p=mjc2wsl.git;a=blobdiff_plain;f=src%2Fmjc2wsl.java;h=87177112908a7923d7a282be0825afdec10b0659;hp=9eff3add933badbc57bfa5fd18639bfabb8f0318;hb=3163f812ec467db6a4af65840c52133c77fd9eb6;hpb=786d08226c4b4749d7004aff32276c612feef110 diff --git a/src/mjc2wsl.java b/src/mjc2wsl.java index 9eff3ad..8717711 100644 --- a/src/mjc2wsl.java +++ b/src/mjc2wsl.java @@ -28,7 +28,7 @@ import java.util.*; * @author Doni Pracner, http://perun.dmi.rs/pracner http://quemaster.com */ public class mjc2wsl{ - public static String versionN = "0.1.5"; + public static String versionN = "0.1.6"; private TransMessages messages = new TransMessages(); @@ -192,22 +192,47 @@ public class mjc2wsl{ "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("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("\n mjvm_fp := 0, mjvm_sp := 0,"); - ret.append("\n t_e_m_p := 0 > :"); + ret.append("\n mjvm_estack := < >, mjvm_mstack := < > > : "); return ret.toString(); } public String createStandardEnd(){ - return "SKIP\nENDVAR"; + 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) + "]"; @@ -246,26 +271,31 @@ 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){ + return stack + " := <" + var + " > ++ " + stack +";"; + } - //Expression stack + private String createFromStack(String stack, String var){ + 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; @@ -289,15 +319,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 { @@ -328,13 +358,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) @@ -405,41 +434,53 @@ 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; } @@ -501,13 +542,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)); - 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")); break; } @@ -542,10 +578,13 @@ 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) + " FI;"); + prl(createEndVar()); + break; } @@ -557,7 +596,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: { @@ -582,8 +621,10 @@ 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; } @@ -596,10 +637,11 @@ public class mjc2wsl{ } 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);"); + prl("Print_MJ(tempb,tempa);"); + prl(createEndVar()); break; } @@ -618,11 +660,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()); } @@ -671,9 +713,10 @@ public class mjc2wsl{ } public void printHelpHelp() { - System.out.println("Help options"); + 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(){ @@ -704,6 +747,10 @@ public class mjc2wsl{ } 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)