gitweb on Svarog
projekti pod git sistemom za održavanje verzija -- projects under the git version control systemdiff --git a/src/com/quemaster/transformations/mjc2wsl/mjc2wsl.java b/src/com/quemaster/transformations/mjc2wsl/mjc2wsl.java
package com.quemaster.transformations.mjc2wsl;
/*
- Copyright (C) 2014 Doni Pracner
+ Copyright (C) 2014,2015, 2016, 2018 Doni Pracner
This file is part of mjc2wsl.
You should have received a copy of the GNU General Public License
along with mjc2wsl. If not, see <http://www.gnu.org/licenses/>.
*/
-import java.io.*;
-import java.util.*;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintWriter;
+import java.nio.file.FileSystems;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Calendar;
+import java.util.Properties;
import com.quemaster.transformations.TransMessages;
*/
public class mjc2wsl{
//default version name, used if the file is not found
- private static String versionN = "0.1.x";
+ private static String versionN = "v0.2.x";
private String versionFile = "version.properties";
private boolean genPopPush=false;
- private boolean genInlinePrint=false;
+ private boolean genInlinePrint = false;
+
+ 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 = ' ';
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");
return versionN;
}
- MicroJavaInput mjInput = new MicroJavaInput();
+ private MicroJavaInput mjInput;
private PrintWriter out = null;
public String createStandardStart(int numWords){
StringBuilder ret = new StringBuilder(
- "C:\" This file automatically converted from microjava bytecode\";\n"
- +"C:\" with mjc2wsl v "+getVersion()+"\";\n");
+ "C:\" This file was automatically converted from microjava bytecode\n"
+ +" using mjc2wsl "+getVersion()+"\n");
+
+ ret.append(" -options:");
+ ret.append("\n localsAsArrays:"+genLocalsAsArray);
+ ret.append("\n localVarBlocks:"+genLocalVars);
+ ret.append("\n popPush:"+genPopPush);
+ ret.append("\n inlinePrint:"+genInlinePrint);
+ ret.append("\n\";\n");
ret.append("\nBEGIN");
ret.append("\nVAR <\n\t");
- ret.append("mjvm_locals := ARRAY(1,0),");
+ if (!genLocalVars){
+ ret.append("\n\ttempa := 0, tempb :=0, tempres := 0,");
+ } else
+ ret.append("\n\tmjvm_flag_jump := 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_flag_jump := 0,");
ret.append("\n\tmjvm_objects := < >,");
ret.append("\n\tmjvm_estack := < >, mjvm_mstack := < > > :");
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) {
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("PRINFLUSH(@Format(format, val ))");
+ ret.append("\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("PRINFLUSH(@Format(format, CHR(val)))");
+ ret.append("\nEND\n");
}
ret.append("\nEND");
}
private String createStartVar(String... vars){
- StringBuilder ret = new StringBuilder("VAR < ");
- ret.append(vars[0] + " := 0");
- for (int i=1; i<vars.length; i++)
- ret.append(", "+ vars[i] +" := 0");
- ret.append(" > : ");
-
- return ret.toString();
+ if (genLocalVars) {
+ StringBuilder ret = new StringBuilder("VAR < ");
+ ret.append(vars[0] + " := 0");
+ for (int i = 1; i < vars.length; i++)
+ ret.append(", " + vars[i] + " := 0");
+ ret.append(" > : ");
+
+ return ret.toString();
+ }
+ return "";
}
private String createEndVar(){
- return "ENDVAR;";
+ if (genLocalVars)
+ return "ENDVAR;";
+ else
+ return "";
}
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) {
}
public void convertStream(InputStream ins) throws Exception{
- mjInput.setStream(ins);
- //process start
- mjInput.processHeader(this);
-
- prl(createStandardStart(mjInput.getNumberOfWords(this)));
- prl("SKIP;\n ACTIONS a" + (14 + mjInput.getMainAdr(this)) + " :");
+ mjInput = new MicroJavaInput(ins);
+
+ prl(createStandardStart(mjInput.getNumberOfWords()));
+ prl("SKIP;\n ACTIONS a" + (14 + mjInput.getMainAdr()) + " :");
+
+ // 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() + " ==");
if (genPrintForEachAddress) {
prl("PRINT(\"a" + mjInput.getCounter() + "\");");
if (genPauseAfterEachAddress)
- prl("debug_disposable_string := @Read_Line(Standard_Input_Port);");
+ prl("@Read_Line_Proc(VAR debug_disposable_string, Standard_Input_Port);");
}
switch (op) {
case load: {
case jle:
case jgt:
case jge: {
- prl(createStartVar("tempa", "tempb"));
- prl(createTopTwoEStack());
- prl("IF tempb " + mjInput.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"
- + (mjInput.getCounter() + mjInput.get2())
- + " ELSE CALL a" + (mjInput.getCounter() + 1)
- + " FI;");
-
+ if (genLocalVars) {
+ prl(createStartVar("tempa", "tempb"));
+ prl(createTopTwoEStack());
+ prl("IF tempb " + mjInput.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"
+ + (mjInput.getCounter() + mjInput.get2())
+ + " ELSE CALL a" + (mjInput.getCounter() + 1)
+ + " FI;");
+ } else {
+ prl(createTopTwoEStack());
+ prl("IF tempb " + mjInput.getRelationFor(op)
+ + " tempa THEN CALL a"
+ + (mjInput.getCounter() + mjInput.get2())
+ + " ELSE CALL a" + (mjInput.getCounter() + 1)
+ + " FI;");
+ }
break;
}
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;
}
// read, print
case bread: {
- // TODO make it a char for read
- messages.message("char is read like a number", TransMessages.M_WAR);
- prl(createComment("char is read like a number", C_SPEC));
+ // TODO maybe we'll need a buffer for multi chars!
+ prl(createStartVar("tempa"));
+ prl("@Read_Line_Proc(VAR tempa, Standard_Input_Port);");
+ prl("tempa := @String_To_List(tempa)[1];");
+ prl(createToEStack("tempa"));
+ prl(createEndVar());
+ break;
}
case read: {
prl(createStartVar("tempa"));
- prl("tempa := @String_To_Num(@Read_Line(Standard_Input_Port));");
+ prl("@Read_Line_Proc(VAR tempa, Standard_Input_Port);");
+ prl("tempa := @String_To_Num(tempa);");
prl(createToEStack("tempa"));
prl(createEndVar());
break;
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 >));");
+ prl("PRINFLUSH(@Format(tempa, @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());
if (genInlinePrint){
prl(createComment("print spacing",C_SPEC));
- prl("PRINFLUSH(SUBSTR(\" \", 0, MIN(10, MAX(0, tempa-SLENGTH(@String(tempb))))), tempb);");
+ prl("PRINFLUSH(@Format(tempa,tempb));");
}
else
prl("Print_MJ(tempb,tempa);");
}
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("Alternatives for code generation (* are the defaults):");
+ System.out.print(genPopPush?'*':' ');
+ System.out.println(" --genPopPush generate POP/PUSH instead of TAIL/HEAD");
+ System.out.print(!genPopPush?'*':' ');
+ System.out.println(" --genHeadTail generate TAIL/HEAD instead of POP/PUSH ");
+ System.out.println();
+ System.out.print(genInlinePrint?'*':' ');
+ System.out.println(" --genInlinePrint generate prints directly instead of procedure calls");
+ System.out.print(!genInlinePrint?'*':' ');
+ System.out.println(" --genProcedurePrint generate prints as custom procedure calls");
System.out.println();
- System.out.println(" --genInlinePrint generate prints directly instead of procedure calls");
- System.out.println(" --genProcedurePrint generate prints as custom procedure calls");
+ System.out.print(genLocalVars?'*':' ');
+ 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() {
}
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");
}
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;
genPopPush = false;
} else if (args[i].compareToIgnoreCase("--genProcedurePrint") == 0) {
genInlinePrint = false;
+ } else if (args[i].compareToIgnoreCase("--genLocalVars") == 0) {
+ 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, refusing to continue: "+args[i]);
+ System.exit(1);
}
i++;
}
if (i >= args.length) {
- System.out.println("no filename supplied");
+ System.err.println("no filename supplied");
System.exit(2);
}
- File f = new File(args[i]);
+
+ Path p = FileSystems.getDefault().getPath(args[i]);
+ if (!Files.isRegularFile(p)){
+ System.err.println("input file does not exist");
+ System.exit(1);
+ }
if (i + 1 < args.length) {
try {
e.printStackTrace();
}
}
- if (f.exists()) {
- Calendar now = Calendar.getInstance();
- convertFile(f);
- long mili = Calendar.getInstance().getTimeInMillis()
- - now.getTimeInMillis();
- System.out.println("conversion time:" + mili + " ms");
- messages.printMessageCounters();
- out.close();
- } else
- System.out.println("file does not exist");
+ Calendar now = Calendar.getInstance();
+ try {
+ convertStream(Files.newInputStream(p));
+ } catch (Exception e) {
+ System.err.println("Failed converting the stream");
+ e.printStackTrace();
+ }
+ long mili = Calendar.getInstance().getTimeInMillis()
+ - now.getTimeInMillis();
+ System.out.println("conversion time:" + mili + " ms");
+ messages.printMessageCounters();
+ out.close();
}
}