X-Git-Url: http://svarog.pmf.uns.ac.rs/gitweb/?p=mjc2wsl.git;a=blobdiff_plain;f=src%2Fmjc2wsl.java;h=f05d271f4b8055b5fc4dc31220d2c3be48d50335;hp=86743cf4ce550065181487564ebba4ce00415dec;hb=9eb11724b95530a35e2a35a2f2ccb25cced8e511;hpb=faf53682246bcababbdd97eadd2da1db5ee2e49a diff --git a/src/mjc2wsl.java b/src/mjc2wsl.java index 86743cf..f05d271 100644 --- a/src/mjc2wsl.java +++ b/src/mjc2wsl.java @@ -10,7 +10,7 @@ import java.util.*; * @author Doni Pracner, http://perun.dmi.rs/pracner http://quemaster.com */ public class mjc2wsl{ - public static String versionN = "0.1.3"; + public static String versionN = "0.1.4"; public static final int M_ERR = 2, M_WAR = 1, M_DEB = 0; @@ -32,6 +32,9 @@ public class mjc2wsl{ out.println("total errors:"+messageCounters[M_ERR]+" warnings:"+messageCounters[M_WAR]); } + private boolean addPauseAfterEachAddress=false, + addPrintForEachAddress = false; + /** Constant used for marking a regular comment from the original file */ public static final char C_REG = ' '; /** @@ -125,6 +128,38 @@ public class mjc2wsl{ private boolean originalInComments = false; + private HashMap opMap = null; + + private String opCodeFile = "mj-bytecodes.properties"; + + private HashMap getOpMap() { + if (opMap==null) { + opMap = new HashMap (60, 0.98f); + try{ + BufferedReader in = new BufferedReader( + new InputStreamReader(getClass().getResourceAsStream(opCodeFile))); + String str = in.readLine(); + while (str != null) { + String[] ss = str.split("="); + opMap.put(Integer.parseInt(ss[0]),ss[1]); + str = in.readLine(); + } + in.close(); + }catch (Exception ex) { + ex.printStackTrace(); + } + } + return opMap; + } + + public String getOpString(int op) { + return getOpMap().get(op); + } + + public String describeOpCode(int op) { + return op + " (" + getOpString(op) + ")"; + } + private InputStream mainIn; private PrintWriter out = null; private int counter = -1; @@ -238,17 +273,27 @@ public class mjc2wsl{ public void convertStream(InputStream ins) throws Exception{ mainIn = ins; - //skip start TODO make better - for (int i = 0; i < 14; i++) - get(); + //process start + byte m = (byte) get(); + byte j = (byte) get(); + if (m!='M' || j !='J') + throw new Exception("Wrong start of bytecode file"); + int codesize = get4(); + int numberOfWords = get4(); + int mainAdr = get4(); prl(getStandardStart()); - prl("SKIP;\n ACTIONS A_S_start:\n A_S_start == CALL a14 END"); + prl("SKIP;\n ACTIONS A_S_start:\n A_S_start == CALL a"+(14+mainAdr)+" END"); int op = get(); while (op >= 0) { if (originalInComments) - prl(createComment("" + op, C_OC)); + prl(createComment(describeOpCode(op), C_OC)); prl("a" + counter + " == "); + if (addPrintForEachAddress) { + prl("PRINT(\"a"+counter+"\");"); + if (addPauseAfterEachAddress) + prl("debug_disposable_string := @Read_Line(Standard_Input_Port);"); + } switch (op) { case load: { prl(cmdToEStack(loc(get()))); @@ -355,15 +400,21 @@ public class mjc2wsl{ } case return_: { - prl(createComment("return not fully procesed yet")); - message("return not fully procesed yet", M_WAR); + prl("IF EMPTY?(mjvm_mstack) THEN CALL Z ELSE"); + //else we let things return + prl(cmdFromMStack("tempa")); + prl("SKIP FI"); + prl("END b"+counter+" =="); break; } case enter: { prl(createComment("enter not fully procesed yet")); message("enter not fully procesed yet", M_WAR); + int parameters = get(); + get(); - get(); + for (int i = parameters-1; i >= 0; i--) + prl(cmdFromEStack("mjvm_loc" + i)); break; } case exit: { @@ -373,6 +424,11 @@ public class mjc2wsl{ } //TODO read, print + case read: { + prl("tempa := @String_To_Num(@Read_Line(Standard_Input_Port));"); + prl(cmdToEStack("tempa")); + break; + } // the prints case bprint: { @@ -386,15 +442,22 @@ public class mjc2wsl{ prl("PRINT(tempb);"); break; } + + case trap: { + // TODO finish trap + prl(createComment("trap not fully procesed yet")); + message("trap not fully procesed yet", M_WAR); + get(); + break; + } + + default: prl(createComment("unknown op error: " + op, C_ERR)); message("unknown op error: "+ op, M_ERR); break; } - //TODO trap - - op = get(); if (op >= 0) prl("CALL a" + counter + " END");