From: Doni Pracner Date: Fri, 31 Aug 2012 00:19:26 +0000 (+0200) Subject: initial commit X-Git-Tag: v0.1.1~2 X-Git-Url: http://svarog.pmf.uns.ac.rs/gitweb/?p=mjc2wsl.git;a=commitdiff_plain;h=a24993cfe39a1fd5b7db6e2ffe9f656955fb73bd;ds=sidebyside initial commit --- a24993cfe39a1fd5b7db6e2ffe9f656955fb73bd diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7fbc92a --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.class +*.obj +*~ +*.bak \ No newline at end of file diff --git a/mjc2wsl.java b/mjc2wsl.java new file mode 100644 index 0000000..f43dfeb --- /dev/null +++ b/mjc2wsl.java @@ -0,0 +1,271 @@ +import java.io.*; +import java.util.*; + +/** + * This program converts file from compiled MicroJava bytecode (a subset used + * in Compiler Construction courses by H. Moessenboeck, not "Java ME") + * to WSL language which is a part of the FermaT Transformation system. + * + * @author Doni Pracner, http://perun.dmi.rs/pracner http://quemaster.com + */ +public class mjc2wsl{ + public static String versionN = "0.1"; + + //regular comments from the original file + //OC when original code is inserted in the file, next to the translations + //SPEC special messages from the translator + //ERR error messages from the transnlator + public static final char C_REG = ' ', C_OC = '#', C_SPEC = '&', C_ERR = '!'; + + + /** instruction code */ + public static final int + load = 1, + load_0 = 2, + load_1 = 3, + load_2 = 4, + load_3 = 5, + store = 6, + store_n = 7, + getstatic = 11, + putstatic = 12, + getfield = 13, + putfield = 14, + const_0 = 15, + const_1 = 16, + const_2 = 17, + const_3 = 18, + const_4 = 19, + const_5 = 20, + const_m1 = 21, + const_ = 22, + add = 23, + sub = 24, + mul = 25, + div = 26, + rem = 27, + neg = 28, + shl = 29, + shr = 30, + inc = 31, + new_ = 32, + newarray = 33, + aload = 34, + astore = 35, + baload = 36, + bastore = 37, + arraylength = 38, + pop = 39, + dup = 40, + dup2 = 41, + jmp = 42, + jcc = 43, + call = 49, + return_ = 50, + enter = 51, + exit = 52, + read = 53, + print = 54, + bread = 55, + bprint = 56, + trap = 57; + + public String getStandardStart(){ + String ret ="C:\" This file automatically converted from microjava bytecode\";\n" + +"C:\" with mjc2wsl v "+versionN+"\";\n"; + ret +="VAR < tempa := 0, tempb := 0, tempres :=0,\n"+ + " stack := < >, t_e_m_p := 0 > :\n"; + + return ret; + } + + public String getStandardEnd(){ + return "SKIP\nENDVAR"; + } + + private InputStream mainIn; + private PrintStream out = System.out; + + private void pr(int i){ + out.print(i); + } + + private void pr(char i){ + out.print(i); + } + + private void pr(String i){ + out.print(i); + } + + private void prl(String i){ + out.println(i); + } + + private int get() { + int res = -1; + try{ + res = mainIn.read(); + if (res>=0) res = res<<24>>>24; + }catch (IOException ex){ + ex.printStackTrace(); + } + return res; + } + + private int get2() { + return (get()*256 + get())<<16>>16; + } + + private int get4() { + return (get2()<<16) + (get2()<<16>>>16); + } + + /** + * Creates a WSL comment with care to quote chars. + */ + public static String createComment(String str){ + return createComment(str, C_REG); + } + + /** + * Creates a WSL comment with care to quote chars, of the + * given type. Types are given as char constants. They can be + * default comments, comments that contain the original code + * in them, or additional comments regarding the translation + * process. + */ + public static String createComment(String str, char type){ + return "C:\""+type+str.replace("\"", "''")+"\";"; + } + + private String cmdToStack(int i){ + return "stack := <"+i+" > ++ stack;"; + } + + private String cmdToStack(String i){ + return "stack := <"+i+" > ++ stack;"; + } + + private String cmdFromStack(String st){ + return st+" := HEAD(stack); stack := TAIL(stack);"; + } + + private String getTopTwo(){ + return cmdFromStack("tempa")+cmdFromStack("tempb"); + } + + private String getTop(){ + return cmdFromStack("tempa"); + } + + public void convertStream(InputStream ins){ + mainIn = ins; + //skip start TODO make better + for (int i=0;i<14;i++) get(); + + prl(getStandardStart()); + + int op = get(); + while (op>=0){ + switch(op) { + case load: { + prl(cmdToStack(get())); + break; + } + case load_0: { + prl(cmdToStack(0)); + break; + } + case const_: { + prl(cmdToStack(get4())); + break; + } + case const_0: { + prl(cmdToStack(0)); + break; + } + case const_1: { + prl(cmdToStack(1)); + break; + } + case const_2: { + prl(cmdToStack(2)); + break; + } + case const_3: { + prl(cmdToStack(3)); + break; + } + case const_4: { + prl(cmdToStack(4)); + break; + } + + case const_5: { + prl(cmdToStack(5)); + break; + } + + case div: { + prl(getTopTwo()); + prl("tempr = tempa / tempb;"); + prl(cmdToStack("tempr")); + break; + } + + case enter: { + prl(createComment("enter")); + get();get(); + break; + } + + //the prints + case bprint: { + prl(getTop()); + prl("PRINT(tempa);"); + break; + } + case print: { + //TODO need to make it a char + prl(getTop()); + prl("PRINT(tempa);"); + break; + } + + default: prl(createComment("unknown op error: "+op,C_ERR)); break; + } + op = get(); + } + prl(getStandardEnd()); + + } + + public void convertFile(File f){ + try{ + convertStream(new FileInputStream(f)); + }catch (Exception ex){ + ex.printStackTrace(); + } + } + + public void run(String[] args){ + if (args.length == 0){ + System.out.println("MicroJava bytecode to WSL converter. v "+versionN+", by Doni Pracner"); + System.out.println("usage:\n\t mjc2wsl filename"); + }else{ + File f = new File(args[0]); + if (f.exists()){ + Calendar now = Calendar.getInstance(); + convertFile(f); + long mili = Calendar.getInstance().getTimeInMillis() - now.getTimeInMillis(); + System.out.println("conversion time:"+mili+" ms"); + }else + System.out.println("file does not exist"); + } + } + + public static void main(String[] args){ + new mjc2wsl().run(args); + } +} \ No newline at end of file diff --git a/samples/chrtest.mj b/samples/chrtest.mj new file mode 100644 index 0000000..39a1531 --- /dev/null +++ b/samples/chrtest.mj @@ -0,0 +1,9 @@ +program P +{ + void main() + { + print(chr(10),15); + print(ord('a'),5); + print(len(null)); + } +} diff --git a/samples/sample-ext.mj b/samples/sample-ext.mj new file mode 100644 index 0000000..5bf3771 --- /dev/null +++ b/samples/sample-ext.mj @@ -0,0 +1,40 @@ +program P + final int size = 10; + class Table { + int[] pos; + int[] neg; + } + Table val; +{ + void main() + int x, i; + { //---------- Initialize val + val = new Table; + val.pos = new int[size]; val.neg = new int[size]; + i = 0; + while (i < size) { + val.pos[i] = 0; val.neg[i] = 0; + i++; + } + //---------- Read values + read(x); + while (x != 0) { + if (0 <= x && x < size) { + val.pos[x]++; + } else if (-size < x && x < 0) { + val.neg[-x]++; + } + read(x); + } + + // output everything + i = 0; + while (i < size) { + print(i,3); + print(val.pos[i],5); + print(val.neg[i],5); + print(chr(10)); + i++; + } + } +} diff --git a/samples/sample.mj b/samples/sample.mj new file mode 100644 index 0000000..8442ca8 --- /dev/null +++ b/samples/sample.mj @@ -0,0 +1,30 @@ +program P + final int size = 10; + class Table { + int[] pos; + int[] neg; + } + Table val; +{ + void main() + int x, i; + { //---------- Initialize val + val = new Table; + val.pos = new int[size]; val.neg = new int[size]; + i = 0; + while (i < size) { + val.pos[i] = 0; val.neg[i] = 0; + i++; + } + //---------- Read values + read(x); + while (x != 0) { + if (0 <= x && x < size) { + val.pos[x]++; + } else if (-size < x && x < 0) { + val.neg[-x]++; + } + read(x); + } + } +} diff --git a/samples/sample0.mj b/samples/sample0.mj new file mode 100644 index 0000000..cdc0851 --- /dev/null +++ b/samples/sample0.mj @@ -0,0 +1,12 @@ +program P +{ + void main() + int i; + { + i = 0; + while (i < 5) { + print(i); + i = i + 1; + } + } +} diff --git a/samples/sample1.mj b/samples/sample1.mj new file mode 100644 index 0000000..620bd2d --- /dev/null +++ b/samples/sample1.mj @@ -0,0 +1,38 @@ +program Eratos + + char[] sieve; + int max; // maximum prime to be found + int npp; // numbers per page + +{ + void put(int x) + { + if (npp == 10) {print(chr(13)); print(chr(10)); npp = 0;} + print(x, 5); + npp++; + } + + void found(int x) + int i; + { + put(x); + i = x; + while (i <= max) {sieve[i] = 'o'; i = i + x;} + } + + void main() + int i; + { + read(max); + npp = 0; + sieve = new char[max+1]; + i = 0; + while (i <= max) {sieve[i] = 'x'; i++;} + i = 2; + while (i <= max) { + found(i); + while(i <= max && sieve[i] == 'o') i++; + } + } + +}//test \ No newline at end of file diff --git a/samples/sample2.mj b/samples/sample2.mj new file mode 100644 index 0000000..d8fb689 --- /dev/null +++ b/samples/sample2.mj @@ -0,0 +1,10 @@ +program test +{ + void main() + int i, j; + { + i = 100; j = 0; + print(i/j); + } + +} \ No newline at end of file