gitweb on Svarog

projekti pod git sistemom za održavanje verzija -- projects under the git version control system
mjc2wsl - start from the proper address
[mjc2wsl.git] / src / mjc2wsl.java
index 69287f6..917978c 100644 (file)
@@ -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;
        
@@ -125,6 +125,38 @@ public class mjc2wsl{
        
        private boolean originalInComments = false;     
        
+       private HashMap<Integer,String> opMap = null;
+       
+       private String opCodeFile = "mj-bytecodes.properties";
+       
+       private HashMap<Integer,String> getOpMap() {
+                       if (opMap==null) {
+                                       opMap = new HashMap<Integer, String> (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,16 +270,21 @@ 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 + " == ");
                        switch (op) {
                        case load: {
@@ -376,6 +413,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: {
@@ -389,15 +431,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");
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner