gitweb on Svarog

projekti pod git sistemom za održavanje verzija -- projects under the git version control system
mjc2wsl - added output of options used in the generated files
[mjc2wsl.git] / src / com / quemaster / transformations / mjc2wsl / mjc2wsl.java
1 package com.quemaster.transformations.mjc2wsl;
2 /*
3 Copyright (C) 2014,2015, 2016 Doni Pracner
5 This file is part of mjc2wsl.
7 mjc2wsl is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 mjc2wsl is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with mjc2wsl. If not, see <http://www.gnu.org/licenses/>.
19 */
20 import java.io.File;
21 import java.io.FileInputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.PrintWriter;
25 import java.nio.file.FileSystems;
26 import java.nio.file.Files;
27 import java.nio.file.Path;
28 import java.util.Calendar;
29 import java.util.Properties;
31 import com.quemaster.transformations.TransMessages;
33 /**
34 * This program converts file from compiled MicroJava bytecode to WSL language
35 * which is a part of the FermaT Transformation system. MicroJava is a subset
36 * used in Compiler Construction courses by Hanspeter Moessenboeck, not
37 * "Java ME".
38 *
39 * @author Doni Pracner, http://perun.dmi.rs/pracner http://quemaster.com
40 */
41 public class mjc2wsl{
42 //default version name, used if the file is not found
43 private static String versionN = "v0.2.x";
45 private String versionFile = "version.properties";
47 private TransMessages messages = new TransMessages();
49 private boolean genPauseAfterEachAddress=false,
50 genPrintForEachAddress = false,
51 genPrintEStackOnChange = false;
53 private boolean genPopPush=false;
55 private boolean genInlinePrint = false;
57 private boolean genLocalVars = true;
59 // make an array for all local variables OTHERWISE make them separate
60 private boolean genLocalsAsArray = true;
62 /** Constant used for marking a regular comment from the original file */
63 public static final char C_REG = ' ';
64 /**
65 * Constant used for marking when original code is inserted in the file,
66 * next to the translations
67 */
68 public static final char C_OC = '#';
69 /** Constant used for marking special messages from the translator */
70 public static final char C_SPEC = '&';
71 /** Constant used for marking error messages from the translator */
72 public static final char C_ERR = '!';
74 /** instruction code in MicroJava bytecode. */
75 public static final int
76 load = 1,
77 load_0 = 2,
78 load_1 = 3,
79 load_2 = 4,
80 load_3 = 5,
81 store = 6,
82 store_0 = 7,
83 store_1 = 8,
84 store_2 = 9,
85 store_3 = 10,
86 getstatic = 11,
87 putstatic = 12,
88 getfield = 13,
89 putfield = 14,
90 const_0 = 15,
91 const_1 = 16,
92 const_2 = 17,
93 const_3 = 18,
94 const_4 = 19,
95 const_5 = 20,
96 const_m1 = 21,
97 const_ = 22,
98 add = 23,
99 sub = 24,
100 mul = 25,
101 div = 26,
102 rem = 27,
103 neg = 28,
104 shl = 29,
105 shr = 30,
106 inc = 31,
107 new_ = 32,
108 newarray = 33,
109 aload = 34,
110 astore = 35,
111 baload = 36,
112 bastore = 37,
113 arraylength = 38,
114 pop = 39,
115 dup = 40,
116 dup2 = 41,
117 jmp = 42,
118 jeq = 43,
119 jne = 44,
120 jlt = 45,
121 jle = 46,
122 jgt = 47,
123 jge = 48,
124 call = 49,
125 return_ = 50,
126 enter = 51,
127 exit = 52,
128 read = 53,
129 print = 54,
130 bread = 55,
131 bprint = 56,
132 trap = 57;
134 private boolean originalInComments = false;
136 private Properties versionData;
138 private String getVersion() {
139 if (versionData == null) {
140 versionData = new Properties();
141 try {
142 versionData.load(getClass().getResourceAsStream(versionFile));
143 } catch (IOException | NullPointerException e) {
144 // it doesn't matter
145 //e.printStackTrace();
148 String ver = versionData.getProperty("version");
149 if (ver != null)
150 return ver;
151 else
152 return versionN;
155 MicroJavaInput mjInput = new MicroJavaInput();
157 private PrintWriter out = null;
160 private void pr(int i){
161 out.print(i);
164 private void pr(char i){
165 out.print(i);
168 private void pr(String i){
169 out.print(i);
172 private void prl(String i){
173 out.println(i);
176 public String createStandardStart(){
177 return createStandardStart(10);
180 public String createStandardStart(int numWords){
181 StringBuilder ret = new StringBuilder(
182 "C:\" This file was automatically converted from microjava bytecode\n"
183 +" using mjc2wsl "+getVersion()+"\n");
185 ret.append(" -options:");
186 ret.append("\n localsAsArrays:"+genLocalsAsArray);
187 ret.append("\n localVarBlocks:"+genLocalVars);
188 ret.append("\n popPush:"+genPopPush);
189 ret.append("\n inlinePrint:"+genInlinePrint);
190 ret.append("\n\";\n");
192 ret.append("\nBEGIN");
193 ret.append("\nVAR <\n\t");
194 if (!genLocalVars){
195 ret.append("\n\ttempa := 0, tempb :=0, tempres := 0,");
196 } else
197 ret.append("\n\tmjvm_flag_jump := 0,");
199 if (genLocalsAsArray)
200 ret.append("mjvm_locals := ARRAY(1,0),");
202 ret.append("\n\tmjvm_statics := ARRAY("+numWords+",0),");
203 ret.append("\n\tmjvm_arrays := < >,");
204 ret.append("\n\tmjvm_objects := < >,");
205 ret.append("\n\tmjvm_estack := < >, mjvm_mstack := < > > :");
207 return ret.toString();
210 public String createAsciiString(){
211 StringBuilder ret = new StringBuilder("C:\"char array for ascii code conversions\";");
212 ret.append("\nascii := \"????????????????????????????????\"++\n");
213 ret.append("\" !\"++Quote++\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\";\n");
215 return ret.toString();
218 public String createStandardEnd(){
219 StringBuilder ret = new StringBuilder("SKIP\nENDVAR\n");
220 ret.append("\nWHERE\n");
222 ret.append("\nFUNCT CHR(num) ==:\n");
223 ret.append("\t(@List_To_String(< num >))\n");
224 ret.append("END\n");
226 if (!genInlinePrint) {
227 ret.append("\nPROC Print_MJ(val, format VAR)==\n");
228 ret.append("PRINFLUSH(@Format(format, val ))");
229 ret.append("\nEND\n");
231 ret.append("\nPROC Print_MJ_CHAR(val, format VAR)==\n");
232 ret.append("PRINFLUSH(@Format(format, CHR(val)))");
233 ret.append("\nEND\n");
236 ret.append("\nEND");
237 return ret.toString();
240 private String createStartVar(String... vars){
241 if (genLocalVars) {
242 StringBuilder ret = new StringBuilder("VAR < ");
243 ret.append(vars[0] + " := 0");
244 for (int i = 1; i < vars.length; i++)
245 ret.append(", " + vars[i] + " := 0");
246 ret.append(" > : ");
248 return ret.toString();
250 return "";
253 private String createEndVar(){
254 if (genLocalVars)
255 return "ENDVAR;";
256 else
257 return "";
260 private String createLocal(int i) {
261 // arrays start at 1 in WSL, so we need an offset
262 if (genLocalsAsArray)
263 return "mjvm_locals[" + (i + 1) + "]";
264 else
265 return "mjvm_locals_" + i;
268 private String createStatic(int i) {
269 return "mjvm_statics[" + (i + 1) + "]";
272 private String createArray(int i) {
273 return "mjvm_arrays[" + i + "]";
276 private String createArray(String i) {
277 return "mjvm_arrays[" + i + "]";
280 private String createObject(String i) {
281 return "mjvm_objects[" + i + "]";
284 /**
285 * Creates a WSL comment with care to quote chars.
286 */
287 public static String createComment(String str){
288 return createComment(str, C_REG);
291 /**
292 * Creates a WSL comment with care to quote chars, of the
293 * given type. Types are given as char constants. They can be
294 * default comments, comments that contain the original code
295 * in them, or additional comments regarding the translation
296 * process.
297 */
298 public static String createComment(String str, char type) {
299 return "C:\"" + type + str.replace("\"", "''") + "\";";
302 // generalised stack operations
304 private String createToStack(String stack, String var){
305 if (genPopPush)
306 return "PUSH("+stack+"," + var + ");";
307 else
308 return stack + " := <" + var + " > ++ " + stack +";";
311 private String createFromStack(String stack, String var){
312 if (genPopPush)
313 return "POP("+ var + ", "+stack+");";
314 else
315 return var + ":= HEAD("+stack+"); "+stack+" := TAIL("+stack+");";
317 //Expression stack
319 private String createToEStack(int i) {
320 return createToEStack(i+"");
323 private String createToEStack(String i) {
324 String res = createToStack("mjvm_estack", i);
325 if (genPrintEStackOnChange)
326 res += "PRINT(\"eStack\",mjvm_estack);";
327 return res;
330 private String createFromEStack(String st) {
331 String res = createFromStack("mjvm_estack",st);
332 if (genPrintEStackOnChange)
333 res += "PRINT(\"eStack\",mjvm_estack);";
334 return res;
337 private String createPopEStack() {
338 String res = "mjvm_estack := TAIL(mjvm_estack);";
339 if (genPrintEStackOnChange)
340 res += "PRINT(\"eStack\",mjvm_estack);";
341 return res;
344 private String createTopTwoEStack() {
345 return createFromEStack("tempa") + "\n" + createFromEStack("tempb");
348 private String createTopEStack() {
349 return createFromEStack("tempa");
352 //Method stack
354 private String createToMStack(int i) {
355 return createToMStack(i+"");
358 private String createToMStack(String i) {
359 return createToStack("mjvm_mstack", i);
362 private String createFromMStack(String st) {
363 return createFromStack("mjvm_mstack", st);
366 public void convertStream(InputStream ins) throws Exception{
367 mjInput.setStream(ins);
368 //process start
369 mjInput.processHeader(this);
371 prl(createStandardStart(mjInput.getNumberOfWords(this)));
372 prl("SKIP;\n ACTIONS a" + (14 + mjInput.getMainAdr(this)) + " :");
374 // the number of Locals for procedures; need to remember it for exits
375 int numberOfLocals = 0;
377 int op = mjInput.get();
378 while (op >= 0) {
379 prl(" a" + mjInput.getCounter() + " ==");
380 if (originalInComments)
381 prl(createComment(mjInput.describeOpCode(op), C_OC));
382 if (genPrintForEachAddress) {
383 prl("PRINT(\"a" + mjInput.getCounter() + "\");");
384 if (genPauseAfterEachAddress)
385 prl("@Read_Line_Proc(VAR debug_disposable_string, Standard_Input_Port);");
387 switch (op) {
388 case load: {
389 prl(createToEStack(createLocal(mjInput.get())));
390 break;
392 case load_0:
393 case load_1:
394 case load_2:
395 case load_3: {
396 prl(createStartVar("tempa"));
397 prl("tempa :="+createLocal(op - load_0)+";");
398 prl(createToEStack("tempa"));
399 prl(createEndVar());
400 break;
402 case store: {
403 prl(createFromEStack(createLocal(mjInput.get())));
404 break;
406 case store_0:
407 case store_1:
408 case store_2:
409 case store_3: {
410 prl(createStartVar("tempa"));
411 prl(createFromEStack("tempa"));
412 prl(createLocal(op - store_0)+" := tempa;");
413 prl(createEndVar());
414 break;
417 case getstatic: {
418 prl(createToEStack(createStatic(mjInput.get2())));
419 break;
421 case putstatic: {
422 prl(createFromEStack(createStatic(mjInput.get2())));
423 break;
426 case getfield: {
427 int f = mjInput.get2();
428 prl(createStartVar("tempa"));
429 prl(createTopEStack());
430 prl(createToEStack(createObject("tempa") + "[" + (f + 1) + "]"));
431 prl(createEndVar());
432 break;
434 case putfield: {
435 int f = mjInput.get2();
436 prl(createStartVar("tempa", "tempb"));
437 prl(createTopTwoEStack());
438 prl(createObject("tempb") + "[" + (f + 1) + "]:=tempa;");
439 prl(createEndVar());
440 break;
443 case const_: {
444 prl(createToEStack(mjInput.get4()));
445 break;
448 case const_m1: {
449 prl(createToEStack(-1));
450 break;
453 case const_0:
454 case const_1:
455 case const_2:
456 case const_3:
457 case const_4:
458 case const_5: {
459 prl(createToEStack(op - const_0));
460 break;
463 case add: {
464 prl(createStartVar("tempa", "tempb", "tempres"));
465 prl(createTopTwoEStack());
466 prl("tempres := tempb + tempa;");
467 prl(createToEStack("tempres"));
468 prl(createEndVar());
469 break;
471 case sub: {
472 prl(createStartVar("tempa", "tempb", "tempres"));
473 prl(createTopTwoEStack());
474 prl("tempres := tempb - tempa;");
475 prl(createToEStack("tempres"));
476 prl(createEndVar());
477 break;
479 case mul: {
480 prl(createStartVar("tempa", "tempb", "tempres"));
481 prl(createTopTwoEStack());
482 prl("tempres := tempb * tempa;");
483 prl(createToEStack("tempres"));
484 prl(createEndVar());
485 break;
487 case div: {
488 prl(createStartVar("tempa", "tempb", "tempres"));
489 prl(createTopTwoEStack());
490 prl("IF tempa = 0 THEN ERROR(\"division by zero\") FI;");
491 prl("tempres := tempb DIV tempa;");
492 prl(createToEStack("tempres"));
493 prl(createEndVar());
494 break;
496 case rem: {
497 prl(createStartVar("tempa", "tempb", "tempres"));
498 prl(createTopTwoEStack());
499 prl("IF tempa = 0 THEN ERROR(\"division by zero\") FI;");
500 prl("tempres := tempb MOD tempa;");
501 prl(createToEStack("tempres"));
502 prl(createEndVar());
503 break;
506 case neg: {
507 prl(createStartVar("tempa"));
508 prl(createTopEStack());
509 prl(createToEStack("-tempa"));
510 prl(createEndVar());
511 break;
514 case shl: {
515 prl(createStartVar("tempa", "tempb"));
516 prl(createTopTwoEStack());
517 prl("VAR <tempres :=tempb, i:=1 >:");
518 prl("\tFOR i:=1 TO tempa STEP 1 DO tempres := tempres * 2 OD;");
519 prl(createToEStack("tempres"));
520 prl("ENDVAR;");
521 prl(createEndVar());
522 break;
524 case shr: {
525 prl(createStartVar("tempa", "tempb"));
526 prl(createTopTwoEStack());
527 prl("VAR <tempres :=tempb, i:=1 >:");
528 prl("\tFOR i:=1 TO tempa STEP 1 DO tempres := tempres DIV 2 OD;");
529 prl(createToEStack("tempres"));
530 prl("ENDVAR;");
531 prl(createEndVar());
532 break;
535 case inc: {
536 int b1 = mjInput.get(), b2 = mjInput.get();
537 prl(createLocal(b1) + " := " + createLocal(b1) + " + " + b2 + ";");
538 break;
541 case new_: {
542 int size = mjInput.get2();
543 // TODO maybe objects and arrays should be in the same list?
544 prl("mjvm_objects := mjvm_objects ++ < ARRAY(" + size
545 + ",0) >;");
546 prl(createToEStack("LENGTH(mjvm_objects)"));
547 break;
549 case newarray: {
550 mjInput.get();// 0 - bytes, 1 - words; ignore for now
551 // TODO take into consideration 0/1
552 prl(createStartVar("tempa"));
553 prl(createTopEStack());
554 prl("mjvm_arrays := mjvm_arrays ++ < ARRAY(tempa,0) >;");
555 prl(createToEStack("LENGTH(mjvm_arrays)"));
556 prl(createEndVar());
557 break;
560 case aload:
561 case baload: {
562 prl(createStartVar("tempa", "tempb"));
563 prl(createTopTwoEStack());
564 prl(createToEStack(createArray("tempb") + "[tempa+1]"));
565 prl(createEndVar());
566 break;
568 case astore:
569 case bastore: {
570 prl(createStartVar("tempa", "tempb", "tempres"));
571 prl(createFromEStack("tempres"));
572 prl(createTopTwoEStack());
573 prl("mjvm_arrays[tempb][tempa+1]:=tempres;");
574 prl(createEndVar());
575 break;
577 case arraylength: {
578 prl(createStartVar("tempa", "tempb"));
579 prl(createTopEStack());
580 prl("tempb := LENGTH("+ createArray("tempa") + ");");
581 prl(createToEStack("tempb"));
582 prl(createEndVar());
583 break;
586 case dup: {
587 prl(createStartVar("tempa", "tempb"));
588 prl(createTopEStack());
589 prl(createToEStack("tempa"));
590 prl(createToEStack("tempa"));
591 prl(createEndVar());
592 break;
594 case dup2: {
595 prl(createStartVar("tempa", "tempb"));
596 prl(createTopTwoEStack());
597 prl(createToEStack("tempb"));
598 prl(createToEStack("tempa"));
599 prl(createToEStack("tempb"));
600 prl(createToEStack("tempa"));
601 prl(createEndVar());
602 break;
605 case pop: {
606 prl(createPopEStack());
607 break;
610 case jmp: {
611 prl("CALL a" + (mjInput.getCounter() + mjInput.get2()) + ";");
612 break;
615 case jeq:
616 case jne:
617 case jlt:
618 case jle:
619 case jgt:
620 case jge: {
621 if (genLocalVars) {
622 prl(createStartVar("tempa", "tempb"));
623 prl(createTopTwoEStack());
624 prl("IF tempb " + mjInput.getRelationFor(op)
625 + " tempa THEN mjvm_flag_jump := 1"
626 + " ELSE mjvm_flag_jump := 0"
627 + " FI;");
628 prl(createEndVar());
629 prl("IF mjvm_flag_jump = 1 THEN CALL a"
630 + (mjInput.getCounter() + mjInput.get2())
631 + " ELSE CALL a" + (mjInput.getCounter() + 1)
632 + " FI;");
633 } else {
634 prl(createTopTwoEStack());
635 prl("IF tempb " + mjInput.getRelationFor(op)
636 + " tempa THEN CALL a"
637 + (mjInput.getCounter() + mjInput.get2())
638 + " ELSE CALL a" + (mjInput.getCounter() + 1)
639 + " FI;");
641 break;
644 case call: {
645 prl("CALL a" + (mjInput.getCounter() + mjInput.get2()) + ";");
646 break;
649 case return_: {
650 // we let the actions return
651 // there is nothing to clean up
652 prl("SKIP\n END\n b" + mjInput.getCounter() + " ==");
653 break;
655 case enter: {
656 int parameters = mjInput.get();
658 numberOfLocals = mjInput.get();
659 if (genLocalsAsArray) {
660 prl(createToMStack("mjvm_locals"));
661 prl("mjvm_locals := ARRAY(" + numberOfLocals + ",0);");
662 } else {
663 // TODO maybe we should generate VAR block for these somewhere
664 for (int i = 0; i < numberOfLocals; i++) {
665 prl(createToMStack(createLocal(i)));
669 for (int i = parameters - 1; i >= 0; i--)
670 prl(createFromEStack(createLocal(i)));
671 break;
673 case exit: {
674 if (genLocalsAsArray) {
675 prl(createFromMStack("mjvm_locals"));
676 } else {
677 // there are as many locals as defined in the last enter
678 for (int i = numberOfLocals -1; i >= 0; i--)
679 prl(createFromMStack(createLocal(i)));
681 break;
684 // read, print
685 case bread: {
686 // TODO maybe we'll need a bufer for multi chars!
687 prl(createStartVar("tempa"));
688 prl("@Read_Line_Proc(VAR tempa, Standard_Input_Port);");
689 prl("tempa := @String_To_List(tempa)[1];");
690 prl(createToEStack("tempa"));
691 prl(createEndVar());
692 break;
694 case read: {
695 prl(createStartVar("tempa"));
696 prl("@Read_Line_Proc(VAR tempa, Standard_Input_Port);");
697 prl("tempa := @String_To_Num(tempa);");
698 prl(createToEStack("tempa"));
699 prl(createEndVar());
700 break;
703 // the prints
704 case bprint: {
705 prl(createStartVar("tempa", "tempb"));
706 prl(createTopTwoEStack());
707 if (genInlinePrint){
708 prl(createComment("print spacing and transformation",C_SPEC));
709 prl("PRINFLUSH(@Format(tempa, @List_To_String(< tempb >)));");
710 } else
711 prl("Print_MJ_CHAR(tempb,tempa);");
712 prl(createEndVar());
713 break;
715 case print: {
716 prl(createStartVar("tempa", "tempb"));
718 prl(createTopTwoEStack());
719 if (genInlinePrint){
720 prl(createComment("print spacing",C_SPEC));
721 prl("PRINFLUSH(@Format(tempa,tempb));");
723 else
724 prl("Print_MJ(tempb,tempa);");
725 prl(createEndVar());
726 break;
729 case trap: {
730 prl("ERROR(\"Runtime error: trap(" + mjInput.get() + ")\");");
731 break;
734 default:
735 prl(createComment("unknown op error: " + op, C_ERR));
736 messages.message("unknown op error: " + op, TransMessages.M_ERR);
737 break;
740 boolean wasJump = mjInput.isJumpCode(op);
741 op = mjInput.get();
742 if (op >= 0)
743 if (wasJump)
744 prl("SKIP\n END");
745 else
746 prl("CALL a" + mjInput.getCounter() + "\n END");
748 prl("SKIP\n END\nENDACTIONS;\n");
749 pr(createStandardEnd());
752 public void convertFile(File f) {
753 try {
754 convertStream(new FileInputStream(f));
755 } catch (Exception ex) {
756 ex.printStackTrace();
760 public void printHelp() {
761 printVersion();
762 printUsage();
763 printHelpOutput();
764 printHelpHelp();
767 public void printLongHelp() {
768 printVersion();
769 printUsage();
770 System.out.println();
771 printHelpOutput();
772 System.out.println();
773 printHelpDirectives();
774 System.out.println();
775 printHelpGenerating();
776 System.out.println();
777 printHelpHelp();
780 public void printHelpOutput() {
781 System.out.println("Output options:");
782 System.out.println(" --screen print output to screen");
783 System.out.println(" -o --oc[+-] include original code in comments");
784 System.out.println(" -v verbose, print warning messages");
785 System.out.println(" -q quiet; don't print even the error messages");
786 System.out.println(" -d print detailed debug messages");
789 public void printHelpGenerating() {
790 System.out.println("Options for generating extra code for tracking code execution");
791 System.out.println(" --genEStackPrint generate print for all EStack changes");
792 System.out.println(" --genAddrPrint generate prints after every address of the original code ");
793 System.out.println(" --genAddrPause generate a pause after every address of the original code ");
794 System.out.println(" --genAddr short for --genAddrPrint and --genAddrPause");
795 System.out.println(" --genAll short for applying all code generation");
798 public void printHelpDirectives(){
799 System.out.println("Alternatives for code generation (* are the defaults):");
800 System.out.print(genPopPush?'*':' ');
801 System.out.println(" --genPopPush generate POP/PUSH instead of TAIL/HEAD");
802 System.out.print(!genPopPush?'*':' ');
803 System.out.println(" --genHeadTail generate TAIL/HEAD instead of POP/PUSH ");
804 System.out.println();
805 System.out.print(genInlinePrint?'*':' ');
806 System.out.println(" --genInlinePrint generate prints directly instead of procedure calls");
807 System.out.print(!genInlinePrint?'*':' ');
808 System.out.println(" --genProcedurePrint generate prints as custom procedure calls");
809 System.out.println();
810 System.out.print(genLocalVars?'*':' ');
811 System.out.println(" --genLocalVars generate local VAR block for temp variables");
812 System.out.print(!genLocalVars?'*':' ');
813 System.out.println(" --genGlobalVars do NOT generate local VAR block for temp variables");
814 System.out.println();
815 System.out.print(genLocalsAsArray?'*':' ');
816 System.out.println(" --genLocalsAsArray generate local variables as an array");
817 System.out.print(!genLocalsAsArray?'*':' ');
818 System.out.println(" --genLocalsSeparate generate local variables as separate entities");
821 public void printHelpHelp() {
822 System.out.println("Help and info options");
823 System.out.println(" -h basic help");
824 System.out.println(" --help print more detailed help");
825 System.out.println(" --version or -version print version and exit");
828 public void printUsage(){
829 System.out.println("usage:\n\t mjc2wsl {options} filename [outfile]");
832 public void printVersion() {
833 System.out.println("MicroJava bytecode to WSL converter " + getVersion()
834 + ", by Doni Pracner");
837 public String makeDefaultOutName(String inname){
838 String rez = inname;
839 if (inname.endsWith(".obj"))
840 rez = rez.substring(0, rez.length() - 4);
841 return rez + ".wsl";
844 public void run(String[] args) {
845 if (args.length == 0) {
846 printHelp();
847 } else {
848 int i = 0;
849 while (i < args.length && args[i].length() > 0 && args[i].charAt(0) == '-') {
850 if (args[i].compareTo("-h") == 0) {
851 printHelp();
852 return;
853 } else if (args[i].compareTo("--help") == 0) {
854 printLongHelp();
855 return;
856 } else if (args[i].compareTo("--version") == 0
857 || args[i].compareTo("-version") == 0) {
858 printVersion();
859 return;
860 } else if (args[i].compareTo("-o") == 0
861 || args[i].startsWith("--oc")) {
862 if (args[i].length() == 2)
863 originalInComments = true;
864 else if (args[i].length() == 5)
865 originalInComments = args[i].charAt(4) == '+';
866 else
867 originalInComments = true;
868 } else if (args[i].compareTo("--screen") == 0) {
869 out = new PrintWriter(System.out);
870 } else if (args[i].compareTo("-d") == 0) {
871 messages.setPrintLevel(TransMessages.M_DEB);// print debug info
872 } else if (args[i].compareTo("-v") == 0) {
873 messages.setPrintLevel(TransMessages.M_WAR);// print warnings
874 } else if (args[i].compareTo("-q") == 0) {
875 messages.setPrintLevel(TransMessages.M_QUIET);// no printing
876 } else if (args[i].compareToIgnoreCase("--genEStackPrint") == 0) {
877 genPrintEStackOnChange = true;
878 } else if (args[i].compareToIgnoreCase("--genAddrPause") == 0) {
879 genPauseAfterEachAddress = true;
880 } else if (args[i].compareToIgnoreCase("--genAddrPrint") == 0) {
881 genPrintForEachAddress = true;
882 } else if (args[i].compareToIgnoreCase("--genAddr") == 0) {
883 genPrintForEachAddress = true;
884 genPauseAfterEachAddress = true;
885 } else if (args[i].compareToIgnoreCase("--genAll") == 0) {
886 genPrintEStackOnChange = true;
887 genPrintForEachAddress = true;
888 genPauseAfterEachAddress = true;
889 } else if (args[i].compareToIgnoreCase("--genPopPush") == 0) {
890 genPopPush = true;
891 } else if (args[i].compareToIgnoreCase("--genInlinePrint") == 0) {
892 genInlinePrint = true;
893 } else if (args[i].compareToIgnoreCase("--genHeadTail") == 0) {
894 genPopPush = false;
895 } else if (args[i].compareToIgnoreCase("--genProcedurePrint") == 0) {
896 genInlinePrint = false;
897 } else if (args[i].compareToIgnoreCase("--genLocalVars") == 0) {
898 genLocalVars = true;
899 } else if (args[i].compareToIgnoreCase("--genGlobalVars") == 0) {
900 genLocalVars = false;
901 } else if (args[i].compareToIgnoreCase("--genLocalsAsArray") == 0) {
902 genLocalsAsArray = true;
903 } else if (args[i].compareToIgnoreCase("--genLocalsSeparate") == 0) {
904 genLocalsAsArray = false;
905 } else {
906 System.err.println("unknown option: "+args[i]);
908 i++;
911 if (i >= args.length) {
912 System.err.println("no filename supplied");
913 System.exit(2);
916 Path p = FileSystems.getDefault().getPath(args[i]);
917 if (!Files.isRegularFile(p)){
918 System.err.println("input file does not exist");
919 System.exit(1);
922 if (i + 1 < args.length) {
923 try {
924 out = new PrintWriter(args[i + 1]);
925 } catch (Exception e) {
926 System.err.println("error in opening out file:");
927 e.printStackTrace();
930 if (out == null) {
931 // if not set to screen, or a file, make a default filename
932 try {
933 out = new PrintWriter(makeDefaultOutName(args[i]));
934 } catch (Exception e) {
935 System.err.println("error in opening out file:");
936 e.printStackTrace();
939 Calendar now = Calendar.getInstance();
940 try {
941 convertStream(Files.newInputStream(p));
942 } catch (Exception e) {
943 // TODO Auto-generated catch block
944 e.printStackTrace();
946 long mili = Calendar.getInstance().getTimeInMillis()
947 - now.getTimeInMillis();
948 System.out.println("conversion time:" + mili + " ms");
949 messages.printMessageCounters();
950 out.close();
954 public static void main(String[] args) {
955 new mjc2wsl().run(args);
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner