gitweb on Svarog

projekti pod git sistemom za održavanje verzija -- projects under the git version control system
mjc2wsl - added a messageing system with levels of messages and counters for warnings...
[mjc2wsl.git] / src / mjc2wsl.java
1 import java.io.*;
2 import java.util.*;
4 /**
5 * This program converts file from compiled MicroJava bytecode to WSL language
6 * which is a part of the FermaT Transformation system. MicroJava is a subset
7 * used in Compiler Construction courses by Hanspeter Moessenboeck, not
8 * "Java ME".
9 *
10 * @author Doni Pracner, http://perun.dmi.rs/pracner http://quemaster.com
11 */
12 public class mjc2wsl{
13 public static String versionN = "0.1.2";
15 public static final int M_ERR = 2, M_WAR = 1;
17 private int printLevel = 0;
19 private int[] messageCounters = new int[M_ERR+1];
21 private void message(String mes, int level){
22 if (level>printLevel)
23 System.out.println(mes);
24 messageCounters[level]++;
25 }
27 private void printMessageCounters(){
28 printMessageCounters(System.out);
29 }
31 private void printMessageCounters(PrintStream out){
32 out.println("total errors:"+messageCounters[M_ERR]+" warnings:"+messageCounters[M_WAR]);
33 }
35 /** Constant used for marking a regular comment from the original file */
36 public static final char C_REG = ' ';
37 /**
38 * Constant used for marking when original code is inserted in the file,
39 * next to the translations
40 */
41 public static final char C_OC = '#';
42 /** Constant used for marking special messages from the translator */
43 public static final char C_SPEC = '&';
44 /** Constant used for marking error messages from the translator */
45 public static final char C_ERR = '!';
47 /** instruction code in MicroJava bytecode. */
48 public static final int
49 load = 1,
50 load_0 = 2,
51 load_1 = 3,
52 load_2 = 4,
53 load_3 = 5,
54 store = 6,
55 store_0 = 7,
56 store_1 = 8,
57 store_2 = 9,
58 store_3 = 10,
59 getstatic = 11,
60 putstatic = 12,
61 getfield = 13,
62 putfield = 14,
63 const_0 = 15,
64 const_1 = 16,
65 const_2 = 17,
66 const_3 = 18,
67 const_4 = 19,
68 const_5 = 20,
69 const_m1 = 21,
70 const_ = 22,
71 add = 23,
72 sub = 24,
73 mul = 25,
74 div = 26,
75 rem = 27,
76 neg = 28,
77 shl = 29,
78 shr = 30,
79 inc = 31,
80 new_ = 32,
81 newarray = 33,
82 aload = 34,
83 astore = 35,
84 baload = 36,
85 bastore = 37,
86 arraylength = 38,
87 pop = 39,
88 dup = 40,
89 dup2 = 41,
90 jmp = 42,
91 jeq = 43,
92 jne = 44,
93 jlt = 45,
94 jle = 46,
95 jgt = 47,
96 jge = 48,
97 call = 49,
98 return_ = 50,
99 enter = 51,
100 exit = 52,
101 read = 53,
102 print = 54,
103 bread = 55,
104 bprint = 56,
105 trap = 57;
107 public String getStandardStart(){
108 StringBuilder ret = new StringBuilder(
109 "C:\" This file automatically converted from microjava bytecode\";\n"
110 +"C:\" with mjc2wsl v "+versionN+"\";\n");
112 ret.append("VAR < tempa := 0, tempb := 0, tempres :=0,\n");
113 for (int i = 0; i <= 3; i++)
114 ret.append("loc" + i + " := 0, ");
115 ret.append("\n estack := < >, t_e_m_p := 0 > :");
117 return ret.toString();
120 public String getStandardEnd(){
121 return "SKIP\nENDVAR";
124 private boolean originalInComments = false;
126 private InputStream mainIn;
127 private PrintWriter out = null;
128 private int counter = -1;
130 private void pr(int i){
131 out.print(i);
134 private void pr(char i){
135 out.print(i);
138 private void pr(String i){
139 out.print(i);
142 private void prl(String i){
143 out.println(i);
146 private int get() {
147 int res = -1;
148 try {
149 res = mainIn.read();
150 if (res >= 0)
151 res = res << 24 >>> 24;
152 } catch (IOException ex) {
153 ex.printStackTrace();
155 counter++;
156 return res;
159 private int get2() {
160 return (get() * 256 + get()) << 16 >> 16;
163 private int get4() {
164 return (get2() << 16) + (get2() << 16 >>> 16);
167 private String loc(int i){
168 return "loc" + i;
171 /**
172 * Creates a WSL comment with care to quote chars.
173 */
174 public static String createComment(String str){
175 return createComment(str, C_REG);
178 /**
179 * Creates a WSL comment with care to quote chars, of the
180 * given type. Types are given as char constants. They can be
181 * default comments, comments that contain the original code
182 * in them, or additional comments regarding the translation
183 * process.
184 */
185 public static String createComment(String str, char type) {
186 return "C:\"" + type + str.replace("\"", "''") + "\";";
189 private String cmdToEStack(int i) {
190 return "estack := <" + i + " > ++ estack;";
193 private String cmdToEStack(String i) {
194 return "estack := <" + i + " > ++ estack;";
197 private String cmdFromEStack(String st) {
198 return st + " := HEAD(estack); estack := TAIL(estack);";
201 private String getTopTwo(){
202 return cmdFromEStack("tempa") + "\n" + cmdFromEStack("tempb");
205 private String getTop() {
206 return cmdFromEStack("tempa");
209 private String getRelationFor(int opcode) throws Exception {
210 switch (opcode) {
211 case jeq: return "=";
212 case jne: return "<>";
213 case jlt: return "<";
214 case jle: return "<=";
215 case jgt: return ">";
216 case jge: return ">=";
218 throw new Exception("Wrong opcode for a relation");
221 public void convertStream(InputStream ins) throws Exception{
222 mainIn = ins;
223 //skip start TODO make better
224 for (int i = 0; i < 14; i++)
225 get();
227 prl(getStandardStart());
228 prl("SKIP;\n ACTIONS A_S_start:\n A_S_start == CALL a14 END");
229 int op = get();
230 while (op >= 0) {
231 if (originalInComments)
232 prl(createComment("" + op, C_OC));
233 prl("a" + counter + " == ");
234 switch (op) {
235 case load: {
236 prl(cmdToEStack(loc(get())));
237 break;
239 case load_0:
240 case load_1:
241 case load_2:
242 case load_3: {
243 prl(cmdToEStack(loc(op - load_0)));
244 break;
246 case store: {
247 prl(cmdFromEStack(loc(get())));
248 break;
250 case store_0:
251 case store_1:
252 case store_2:
253 case store_3: {
254 prl(cmdFromEStack(loc(op - store_0)));
255 break;
257 case const_: {
258 prl(cmdToEStack(get4()));
259 break;
262 case const_0:
263 case const_1:
264 case const_2:
265 case const_3:
266 case const_4:
267 case const_5: {
268 prl(cmdToEStack(op - const_0));
269 break;
272 case jmp: {
273 prl("CALL a" + (counter + get2()) + ";");
274 break;
277 case jeq:
278 case jne:
279 case jlt:
280 case jle:
281 case jgt:
282 case jge: {
283 prl(getTopTwo());
284 prl("IF tempb "+ getRelationFor(op)
285 +" tempa THEN CALL a" + (counter + get2())
286 + " FI;");
287 break;
290 case add: {
291 prl(getTopTwo());
292 prl("tempres := tempb + tempa;");
293 prl(cmdToEStack("tempres"));
294 break;
296 case sub: {
297 prl(getTopTwo());
298 prl("tempres := tempb - tempa;");
299 prl(cmdToEStack("tempres"));
300 break;
302 case mul: {
303 prl(getTopTwo());
304 prl("tempres := tempb * tempa;");
305 prl(cmdToEStack("tempres"));
306 break;
308 case div: {
309 prl(getTopTwo());
310 prl("IF tempa = 0 THEN ERROR(\"division by zero\") FI;");
311 prl("tempres := tempb DIV tempa;");
312 prl(cmdToEStack("tempres"));
313 break;
315 case rem: {
316 prl(getTopTwo());
317 prl("IF tempa = 0 THEN ERROR(\"division by zero\") FI;");
318 prl("tempres := tempb MOD tempa;");
319 prl(cmdToEStack("tempres"));
320 break;
323 case enter: {
324 prl(createComment("enter not fully procesed yet"));
325 message("enter not fully procesed yet", M_WAR);
326 get();
327 get();
328 break;
330 case return_: {
331 prl(createComment("return not fully procesed yet"));
332 message("return not fully procesed yet", M_WAR);
333 break;
335 case exit: {
336 prl(createComment("exit not fully procesed yet"));
337 message("exit not fully procesed yet", M_WAR);
338 break;
341 // the prints
342 case bprint: {
343 prl(getTopTwo());
344 prl("PRINT(tempb);");
345 break;
347 case print: {
348 // TODO need to make it a char
349 prl(getTopTwo());
350 prl("PRINT(tempb);");
351 break;
353 default:
354 prl(createComment("unknown op error: " + op, C_ERR));
355 message("unknown op error: "+ op, M_ERR);
356 break;
359 op = get();
360 if (op >= 0)
361 prl("CALL a" + counter + " END");
363 prl("CALL Z;\nSKIP END\nENDACTIONS;\n");
364 prl(getStandardEnd());
368 public void convertFile(File f) {
369 try {
370 convertStream(new FileInputStream(f));
371 } catch (Exception ex) {
372 ex.printStackTrace();
376 public void printHelp() {
377 System.out.println("MicroJava bytecode to WSL converter. v " + versionN
378 + ", by Doni Pracner");
379 System.out.println("usage:\n\t {options} mjc2wsl filename [outfile]");
380 System.out.println("options:\n\t--screen print output to screen");
381 System.out.println("\t-o --oc include original code in comments");
384 public String makeDefaultOutName(String inname){
385 String rez = inname;
386 if (inname.endsWith(".obj"))
387 rez = rez.substring(0, rez.length() - 4);
388 return rez + ".wsl";
391 public void run(String[] args) {
392 if (args.length == 0) {
393 printHelp();
394 } else {
395 int i = 0;
396 while (i < args.length && args[i].charAt(0) == '-') {
397 if (args[i].compareTo("-h") == 0) {
398 printHelp();
399 return;
400 } else if (args[i].compareTo("-o") == 0
401 || args[i].startsWith("--oc")) {
402 if (args[i].length() == 2)
403 originalInComments = true;
404 else if (args[i].length() == 5)
405 originalInComments = args[i].charAt(4) == '+';
406 else
407 originalInComments = true;
408 } else if (args[i].startsWith("--screen")) {
409 out = new PrintWriter(System.out);
411 i++;
414 if (i >= args.length) {
415 System.out.println("no filename supplied");
416 System.exit(2);
418 File f = new File(args[i]);
420 if (i + 1 < args.length) {
421 try {
422 out = new PrintWriter(args[i + 1]);
423 } catch (Exception e) {
424 System.err.println("error in opening out file:");
425 e.printStackTrace();
428 if (out == null) {
429 // if not set to screen, or a file, make a default filename
430 try {
431 out = new PrintWriter(makeDefaultOutName(args[i]));
432 } catch (Exception e) {
433 System.err.println("error in opening out file:");
434 e.printStackTrace();
437 if (f.exists()) {
438 Calendar now = Calendar.getInstance();
439 printLevel=10;
440 convertFile(f);
441 long mili = Calendar.getInstance().getTimeInMillis()
442 - now.getTimeInMillis();
443 System.out.println("conversion time:" + mili + " ms");
444 printMessageCounters();
445 out.close();
446 } else
447 System.out.println("file does not exist");
451 public static void main(String[] args) {
452 new mjc2wsl().run(args);
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner