gitweb on Svarog

projekti pod git sistemom za održavanje verzija -- projects under the git version control system
e65bd1dbbc33fd527f200677a2aad4addbedb9a6
[mjc2wsl.git] / mjc2wsl.java
1 import java.io.*;
2 import java.util.*;
4 /**
5 * This program converts file from compiled MicroJava bytecode (a subset used
6 * in Compiler Construction courses by H. Moessenboeck, not "Java ME")
7 * to WSL language which is a part of the FermaT Transformation system.
8 *
9 * @author Doni Pracner, http://perun.dmi.rs/pracner http://quemaster.com
10 */
11 public class mjc2wsl{
12 public static String versionN = "0.1";
14 //regular comments from the original file
15 //OC when original code is inserted in the file, next to the translations
16 //SPEC special messages from the translator
17 //ERR error messages from the transnlator
18 public static final char C_REG = ' ', C_OC = '#', C_SPEC = '&', C_ERR = '!';
21 /** instruction code */
22 public static final int
23 load = 1,
24 load_0 = 2,
25 load_1 = 3,
26 load_2 = 4,
27 load_3 = 5,
28 store = 6,
29 store_0 = 7,
30 store_1 = 8,
31 store_2 = 9,
32 store_3 = 10,
33 getstatic = 11,
34 putstatic = 12,
35 getfield = 13,
36 putfield = 14,
37 const_0 = 15,
38 const_1 = 16,
39 const_2 = 17,
40 const_3 = 18,
41 const_4 = 19,
42 const_5 = 20,
43 const_m1 = 21,
44 const_ = 22,
45 add = 23,
46 sub = 24,
47 mul = 25,
48 div = 26,
49 rem = 27,
50 neg = 28,
51 shl = 29,
52 shr = 30,
53 inc = 31,
54 new_ = 32,
55 newarray = 33,
56 aload = 34,
57 astore = 35,
58 baload = 36,
59 bastore = 37,
60 arraylength = 38,
61 pop = 39,
62 dup = 40,
63 dup2 = 41,
64 jmp = 42,
65 jeq = 43,
66 jne = 44,
67 jlt = 45,
68 jle = 46,
69 jgt = 47,
70 jge = 48,
71 call = 49,
72 return_ = 50,
73 enter = 51,
74 exit = 52,
75 read = 53,
76 print = 54,
77 bread = 55,
78 bprint = 56,
79 trap = 57;
81 public String getStandardStart(){
82 StringBuilder ret = new StringBuilder("C:\" This file automatically converted from microjava bytecode\";\n"
83 +"C:\" with mjc2wsl v "+versionN+"\";\n");
85 ret.append("VAR < tempa := 0, tempb := 0, tempres :=0,\n");
86 for (int i=0;i<=3;i++) ret.append("loc"+i+" := 0, ");
87 ret.append("\n estack := < >, t_e_m_p := 0 > :");
89 return ret.toString();
90 }
92 public String getStandardEnd(){
93 return "SKIP\nENDVAR";
94 }
96 private boolean originalInComments = false;
98 private InputStream mainIn;
99 private PrintWriter out = null;
101 private void pr(int i){
102 out.print(i);
105 private void pr(char i){
106 out.print(i);
109 private void pr(String i){
110 out.print(i);
113 private void prl(String i){
114 out.println(i);
117 private int get() {
118 int res = -1;
119 try{
120 res = mainIn.read();
121 if (res>=0) res = res<<24>>>24;
122 }catch (IOException ex){
123 ex.printStackTrace();
125 return res;
128 private int get2() {
129 return (get()*256 + get())<<16>>16;
132 private int get4() {
133 return (get2()<<16) + (get2()<<16>>>16);
136 private String loc(int i){
137 return "loc"+i;
140 /**
141 * Creates a WSL comment with care to quote chars.
142 */
143 public static String createComment(String str){
144 return createComment(str, C_REG);
147 /**
148 * Creates a WSL comment with care to quote chars, of the
149 * given type. Types are given as char constants. They can be
150 * default comments, comments that contain the original code
151 * in them, or additional comments regarding the translation
152 * process.
153 */
154 public static String createComment(String str, char type){
155 return "C:\""+type+str.replace("\"", "''")+"\";";
158 private String cmdToEStack(int i){
159 return "estack := <"+i+" > ++ estack;";
162 private String cmdToEStack(String i){
163 return "estack := <"+i+" > ++ estack;";
166 private String cmdFromEStack(String st){
167 return st+" := HEAD(estack); estack := TAIL(estack);";
170 private String getTopTwo(){
171 return cmdFromEStack("tempa")+"\n"+cmdFromEStack("tempb");
174 private String getTop(){
175 return cmdFromEStack("tempa");
178 public void convertStream(InputStream ins){
179 mainIn = ins;
180 //skip start TODO make better
181 for (int i=0;i<14;i++) get();
183 prl(getStandardStart());
185 int op = get();
186 while (op>=0){
187 if (originalInComments) prl(createComment(""+op,C_OC));
188 switch(op) {
189 case load: {
190 prl(cmdToEStack(loc(get())));
191 break;
193 case load_0: case load_1: case load_2: case load_3: {
194 prl(cmdToEStack(loc(op-load_0)));
195 break;
197 case store: {
198 prl(cmdFromEStack(loc(get())));
199 break;
201 case store_0: case store_1: case store_2: case store_3: {
202 prl(cmdFromEStack(loc(op-store_0)));
203 break;
205 case const_: {
206 prl(cmdToEStack(get4()));
207 break;
210 case const_0: case const_1: case const_2:
211 case const_3: case const_4: case const_5:{
212 prl(cmdToEStack(op-const_0));
213 break;
216 case jmp: {
217 prl(createComment("CALL "+get2()));
218 break;
221 case jeq: case jne: case jlt:
222 case jle: case jgt: case jge: {
223 prl(getTopTwo());
224 prl(createComment("IF CALL "+get2()));
225 break;
228 case add: {
229 prl(getTopTwo());
230 prl("tempres := tempb + tempa;");
231 prl(cmdToEStack("tempres"));
232 break;
233 }
234 case div: {
235 prl(getTopTwo());
236 prl("tempres := tempb / tempa;");
237 prl(cmdToEStack("tempres"));
238 break;
241 case enter: {
242 prl(createComment("enter not fully procesed yet"));
243 get();get();
244 break;
246 case return_: {
247 prl(createComment("return not fully procesed yet"));
248 break;
250 case exit: {
251 prl(createComment("exit not fully procesed yet"));
252 break;
255 //the prints
256 case bprint: {
257 prl(getTopTwo());
258 prl("PRINT(tempb);");
259 break;
261 case print: {
262 //TODO need to make it a char
263 prl(getTopTwo());
264 prl("PRINT(tempb);");
265 break;
268 default: prl(createComment("unknown op error: "+op,C_ERR)); break;
270 op = get();
272 prl(getStandardEnd());
276 public void convertFile(File f){
277 try{
278 convertStream(new FileInputStream(f));
279 }catch (Exception ex){
280 ex.printStackTrace();
284 public void printHelp(){
285 System.out.println("MicroJava bytecode to WSL converter. v "+versionN+", by Doni Pracner");
286 System.out.println("usage:\n\t {options} mjc2wsl filename [outfile]");
287 System.out.println("options:\n\t--screen print output to screen");
288 System.out.println("\t-o --oc include original code in comments");
291 public String makeDefaultOutName(String inname){
292 String rez = inname;
293 if (inname.endsWith(".obj"))
294 rez = rez.substring(0,rez.length()-4);
295 return rez+".wsl";
298 public void run(String[] args){
299 if (args.length == 0){
300 printHelp();
301 }else{
302 int i=0;
303 while (i<args.length && args[i].charAt(0)=='-'){
304 if (args[i].compareTo("-h")==0){
305 printHelp();return;
306 }else
307 if (args[i].compareTo("-o")==0 || args[i].startsWith("--oc")){
308 if (args[i].length()==2) originalInComments = true;
309 else if (args[i].length()==5)
310 originalInComments = args[i].charAt(4)=='+';
311 else originalInComments = true;
312 }else
313 if (args[i].startsWith("--screen")){
314 out = new PrintWriter(System.out);
316 i++;
319 if (i>=args.length) {System.out.println("no filename supplied");System.exit(2);}
320 File f = new File(args[i]);
322 if (i+1<args.length) {
323 try{out = new PrintWriter(args[i+1]);}catch (Exception e) {
324 System.err.println("error in opening out file:");
325 e.printStackTrace();
328 if (out == null){
329 //if not set to screen, or a file, make a default filename
330 try{out = new PrintWriter(makeDefaultOutName(args[i]));}catch (Exception e) {
331 System.err.println("error in opening out file:");
332 e.printStackTrace();
335 if (f.exists()){
336 Calendar now = Calendar.getInstance();
337 convertFile(f);
338 long mili = Calendar.getInstance().getTimeInMillis() - now.getTimeInMillis();
339 System.out.println("conversion time:"+mili+" ms");
340 out.close();
341 }else
342 System.out.println("file does not exist");
346 public static void main(String[] args){
347 new mjc2wsl().run(args);
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner