gitweb on Svarog

projekti pod git sistemom za održavanje verzija -- projects under the git version control system
TransMessages improvements
[mjc2wsl.git] / src / com / quemaster / transformations / mjc2wsl / Mjc2wsl.java
1 package com.quemaster.transformations.mjc2wsl;
2 /*
3 Copyright (C) 2014,2015, 2016, 2018 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 private MicroJavaInput mjInput;
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 = new MicroJavaInput(ins);
369 prl(createStandardStart(mjInput.getNumberOfWords()));
370 prl("SKIP;\n ACTIONS a" + (14 + mjInput.getMainAdr()) + " :");
372 // the number of Locals for procedures; need to remember it for exits
373 int numberOfLocals = 0;
375 int op = mjInput.get();
376 while (op >= 0) {
377 messages.message("Processing "+mjInput.describeOpCode(op)+ " at " + mjInput.getCounter(), TransMessages.M_DEB);
378 prl(" a" + mjInput.getCounter() + " ==");
379 if (originalInComments)
380 prl(createComment(mjInput.describeOpCode(op), C_OC));
381 if (genPrintForEachAddress) {
382 prl("PRINT(\"a" + mjInput.getCounter() + "\");");
383 if (genPauseAfterEachAddress)
384 prl("@Read_Line_Proc(VAR debug_disposable_string, Standard_Input_Port);");
386 switch (op) {
387 case load: {
388 prl(createToEStack(createLocal(mjInput.get())));
389 break;
391 case load_0:
392 case load_1:
393 case load_2:
394 case load_3: {
395 prl(createStartVar("tempa"));
396 prl("tempa :="+createLocal(op - load_0)+";");
397 prl(createToEStack("tempa"));
398 prl(createEndVar());
399 break;
401 case store: {
402 prl(createFromEStack(createLocal(mjInput.get())));
403 break;
405 case store_0:
406 case store_1:
407 case store_2:
408 case store_3: {
409 prl(createStartVar("tempa"));
410 prl(createFromEStack("tempa"));
411 prl(createLocal(op - store_0)+" := tempa;");
412 prl(createEndVar());
413 break;
416 case getstatic: {
417 prl(createToEStack(createStatic(mjInput.get2())));
418 break;
420 case putstatic: {
421 prl(createFromEStack(createStatic(mjInput.get2())));
422 break;
425 case getfield: {
426 int f = mjInput.get2();
427 prl(createStartVar("tempa"));
428 prl(createTopEStack());
429 prl(createToEStack(createObject("tempa") + "[" + (f + 1) + "]"));
430 prl(createEndVar());
431 break;
433 case putfield: {
434 int f = mjInput.get2();
435 prl(createStartVar("tempa", "tempb"));
436 prl(createTopTwoEStack());
437 prl(createObject("tempb") + "[" + (f + 1) + "]:=tempa;");
438 prl(createEndVar());
439 break;
442 case const_: {
443 prl(createToEStack(mjInput.get4()));
444 break;
447 case const_m1: {
448 prl(createToEStack(-1));
449 break;
452 case const_0:
453 case const_1:
454 case const_2:
455 case const_3:
456 case const_4:
457 case const_5: {
458 prl(createToEStack(op - const_0));
459 break;
462 case add: {
463 prl(createStartVar("tempa", "tempb", "tempres"));
464 prl(createTopTwoEStack());
465 prl("tempres := tempb + tempa;");
466 prl(createToEStack("tempres"));
467 prl(createEndVar());
468 break;
470 case sub: {
471 prl(createStartVar("tempa", "tempb", "tempres"));
472 prl(createTopTwoEStack());
473 prl("tempres := tempb - tempa;");
474 prl(createToEStack("tempres"));
475 prl(createEndVar());
476 break;
478 case mul: {
479 prl(createStartVar("tempa", "tempb", "tempres"));
480 prl(createTopTwoEStack());
481 prl("tempres := tempb * tempa;");
482 prl(createToEStack("tempres"));
483 prl(createEndVar());
484 break;
486 case div: {
487 prl(createStartVar("tempa", "tempb", "tempres"));
488 prl(createTopTwoEStack());
489 prl("IF tempa = 0 THEN ERROR(\"division by zero\") FI;");
490 prl("tempres := tempb DIV tempa;");
491 prl(createToEStack("tempres"));
492 prl(createEndVar());
493 break;
495 case rem: {
496 prl(createStartVar("tempa", "tempb", "tempres"));
497 prl(createTopTwoEStack());
498 prl("IF tempa = 0 THEN ERROR(\"division by zero\") FI;");
499 prl("tempres := tempb MOD tempa;");
500 prl(createToEStack("tempres"));
501 prl(createEndVar());
502 break;
505 case neg: {
506 prl(createStartVar("tempa"));
507 prl(createTopEStack());
508 prl(createToEStack("-tempa"));
509 prl(createEndVar());
510 break;
513 case shl: {
514 prl(createStartVar("tempa", "tempb"));
515 prl(createTopTwoEStack());
516 prl("VAR <tempres :=tempb, i:=1 >:");
517 prl("\tFOR i:=1 TO tempa STEP 1 DO tempres := tempres * 2 OD;");
518 prl(createToEStack("tempres"));
519 prl("ENDVAR;");
520 prl(createEndVar());
521 break;
523 case shr: {
524 prl(createStartVar("tempa", "tempb"));
525 prl(createTopTwoEStack());
526 prl("VAR <tempres :=tempb, i:=1 >:");
527 prl("\tFOR i:=1 TO tempa STEP 1 DO tempres := tempres DIV 2 OD;");
528 prl(createToEStack("tempres"));
529 prl("ENDVAR;");
530 prl(createEndVar());
531 break;
534 case inc: {
535 int b1 = mjInput.get(), b2 = mjInput.get();
536 prl(createLocal(b1) + " := " + createLocal(b1) + " + " + b2 + ";");
537 break;
540 case new_: {
541 int size = mjInput.get2();
542 // TODO maybe objects and arrays should be in the same list?
543 prl("mjvm_objects := mjvm_objects ++ < ARRAY(" + size
544 + ",0) >;");
545 prl(createToEStack("LENGTH(mjvm_objects)"));
546 break;
548 case newarray: {
549 mjInput.get();// 0 - bytes, 1 - words; ignore for now
550 // TODO take into consideration 0/1
551 prl(createStartVar("tempa"));
552 prl(createTopEStack());
553 prl("mjvm_arrays := mjvm_arrays ++ < ARRAY(tempa,0) >;");
554 prl(createToEStack("LENGTH(mjvm_arrays)"));
555 prl(createEndVar());
556 break;
559 case aload:
560 case baload: {
561 prl(createStartVar("tempa", "tempb"));
562 prl(createTopTwoEStack());
563 prl(createToEStack(createArray("tempb") + "[tempa+1]"));
564 prl(createEndVar());
565 break;
567 case astore:
568 case bastore: {
569 prl(createStartVar("tempa", "tempb", "tempres"));
570 prl(createFromEStack("tempres"));
571 prl(createTopTwoEStack());
572 prl("mjvm_arrays[tempb][tempa+1]:=tempres;");
573 prl(createEndVar());
574 break;
576 case arraylength: {
577 prl(createStartVar("tempa", "tempb"));
578 prl(createTopEStack());
579 prl("tempb := LENGTH("+ createArray("tempa") + ");");
580 prl(createToEStack("tempb"));
581 prl(createEndVar());
582 break;
585 case dup: {
586 prl(createStartVar("tempa", "tempb"));
587 prl(createTopEStack());
588 prl(createToEStack("tempa"));
589 prl(createToEStack("tempa"));
590 prl(createEndVar());
591 break;
593 case dup2: {
594 prl(createStartVar("tempa", "tempb"));
595 prl(createTopTwoEStack());
596 prl(createToEStack("tempb"));
597 prl(createToEStack("tempa"));
598 prl(createToEStack("tempb"));
599 prl(createToEStack("tempa"));
600 prl(createEndVar());
601 break;
604 case pop: {
605 prl(createPopEStack());
606 break;
609 case jmp: {
610 prl("CALL a" + (mjInput.getCounter() + mjInput.get2()) + ";");
611 break;
614 case jeq:
615 case jne:
616 case jlt:
617 case jle:
618 case jgt:
619 case jge: {
620 if (genLocalVars) {
621 prl(createStartVar("tempa", "tempb"));
622 prl(createTopTwoEStack());
623 prl("IF tempb " + mjInput.getRelationFor(op)
624 + " tempa THEN mjvm_flag_jump := 1"
625 + " ELSE mjvm_flag_jump := 0"
626 + " FI;");
627 prl(createEndVar());
628 prl("IF mjvm_flag_jump = 1 THEN CALL a"
629 + (mjInput.getCounter() + mjInput.get2())
630 + " ELSE CALL a" + (mjInput.getCounter() + 1)
631 + " FI;");
632 } else {
633 prl(createTopTwoEStack());
634 prl("IF tempb " + mjInput.getRelationFor(op)
635 + " tempa THEN CALL a"
636 + (mjInput.getCounter() + mjInput.get2())
637 + " ELSE CALL a" + (mjInput.getCounter() + 1)
638 + " FI;");
640 break;
643 case call: {
644 prl("CALL a" + (mjInput.getCounter() + mjInput.get2()) + ";");
645 break;
648 case return_: {
649 // we let the actions return
650 // there is nothing to clean up
651 prl("SKIP\n END\n b" + mjInput.getCounter() + " ==");
652 break;
654 case enter: {
655 int parameters = mjInput.get();
657 numberOfLocals = mjInput.get();
658 if (genLocalsAsArray) {
659 prl(createToMStack("mjvm_locals"));
660 prl("mjvm_locals := ARRAY(" + numberOfLocals + ",0);");
661 } else {
662 // TODO maybe we should generate VAR block for these somewhere
663 for (int i = 0; i < numberOfLocals; i++) {
664 prl(createToMStack(createLocal(i)));
668 for (int i = parameters - 1; i >= 0; i--)
669 prl(createFromEStack(createLocal(i)));
670 break;
672 case exit: {
673 if (genLocalsAsArray) {
674 prl(createFromMStack("mjvm_locals"));
675 } else {
676 // there are as many locals as defined in the last enter
677 for (int i = numberOfLocals -1; i >= 0; i--)
678 prl(createFromMStack(createLocal(i)));
680 break;
683 // read, print
684 case bread: {
685 // TODO maybe we'll need a buffer for multi chars!
686 prl(createStartVar("tempa"));
687 prl("@Read_Line_Proc(VAR tempa, Standard_Input_Port);");
688 prl("tempa := @String_To_List(tempa)[1];");
689 prl(createToEStack("tempa"));
690 prl(createEndVar());
691 break;
693 case read: {
694 prl(createStartVar("tempa"));
695 prl("@Read_Line_Proc(VAR tempa, Standard_Input_Port);");
696 prl("tempa := @String_To_Num(tempa);");
697 prl(createToEStack("tempa"));
698 prl(createEndVar());
699 break;
702 // the prints
703 case bprint: {
704 prl(createStartVar("tempa", "tempb"));
705 prl(createTopTwoEStack());
706 if (genInlinePrint){
707 prl(createComment("print spacing and transformation",C_SPEC));
708 prl("PRINFLUSH(@Format(tempa, @List_To_String(< tempb >)));");
709 } else
710 prl("Print_MJ_CHAR(tempb,tempa);");
711 prl(createEndVar());
712 break;
714 case print: {
715 prl(createStartVar("tempa", "tempb"));
717 prl(createTopTwoEStack());
718 if (genInlinePrint){
719 prl(createComment("print spacing",C_SPEC));
720 prl("PRINFLUSH(@Format(tempa,tempb));");
722 else
723 prl("Print_MJ(tempb,tempa);");
724 prl(createEndVar());
725 break;
728 case trap: {
729 prl("ERROR(\"Runtime error: trap(" + mjInput.get() + ")\");");
730 break;
733 default:
734 prl(createComment("unknown op error: " + op, C_ERR));
735 messages.message("unknown op error: " + op, TransMessages.M_ERR);
736 break;
739 boolean wasJump = mjInput.isJumpCode(op);
740 op = mjInput.get();
741 if (op >= 0)
742 if (wasJump)
743 prl("SKIP\n END");
744 else
745 prl("CALL a" + mjInput.getCounter() + "\n END");
747 prl("SKIP\n END\nENDACTIONS;\n");
748 pr(createStandardEnd());
751 public void convertFile(File f) {
752 try {
753 convertStream(new FileInputStream(f));
754 } catch (Exception ex) {
755 ex.printStackTrace();
759 public void printHelp() {
760 printVersion();
761 printUsage();
762 printHelpOutput();
763 printHelpHelp();
766 public void printLongHelp() {
767 printVersion();
768 printUsage();
769 System.out.println();
770 printHelpOutput();
771 System.out.println();
772 printHelpDirectives();
773 System.out.println();
774 printHelpGenerating();
775 System.out.println();
776 printHelpHelp();
779 public void printHelpOutput() {
780 System.out.println("Output options:");
781 System.out.println(" --screen print output to screen");
782 System.out.println(" -o --oc[+-] include original code in comments");
783 System.out.println(" -v verbose, print warning messages");
784 System.out.println(" -q quiet; don't print even the error messages");
785 System.out.println(" -d print detailed debug messages");
788 public void printHelpGenerating() {
789 System.out.println("Options for generating extra code for tracking code execution");
790 System.out.println(" --genEStackPrint generate print for all EStack changes");
791 System.out.println(" --genAddrPrint generate prints after every address of the original code ");
792 System.out.println(" --genAddrPause generate a pause after every address of the original code ");
793 System.out.println(" --genAddr short for --genAddrPrint and --genAddrPause");
794 System.out.println(" --genAll short for applying all code generation");
797 public void printHelpDirectives(){
798 System.out.println("Alternatives for code generation (* are the defaults):");
799 System.out.print(genPopPush?'*':' ');
800 System.out.println(" --genPopPush generate POP/PUSH instead of TAIL/HEAD");
801 System.out.print(!genPopPush?'*':' ');
802 System.out.println(" --genHeadTail generate TAIL/HEAD instead of POP/PUSH ");
803 System.out.println();
804 System.out.print(genInlinePrint?'*':' ');
805 System.out.println(" --genInlinePrint generate prints directly instead of procedure calls");
806 System.out.print(!genInlinePrint?'*':' ');
807 System.out.println(" --genProcedurePrint generate prints as custom procedure calls");
808 System.out.println();
809 System.out.print(genLocalVars?'*':' ');
810 System.out.println(" --genLocalVars generate local VAR block for temp variables");
811 System.out.print(!genLocalVars?'*':' ');
812 System.out.println(" --genGlobalVars do NOT generate local VAR block for temp variables");
813 System.out.println();
814 System.out.print(genLocalsAsArray?'*':' ');
815 System.out.println(" --genLocalsAsArray generate local variables as an array");
816 System.out.print(!genLocalsAsArray?'*':' ');
817 System.out.println(" --genLocalsSeparate generate local variables as separate entities");
820 public void printHelpHelp() {
821 System.out.println("Help and info options");
822 System.out.println(" -h basic help");
823 System.out.println(" --help print more detailed help");
824 System.out.println(" --version or -version print version and exit");
827 public void printUsage(){
828 System.out.println("usage:\n\t mjc2wsl {options} filename [outfile]");
831 public void printVersion() {
832 System.out.println("MicroJava bytecode to WSL converter " + getVersion()
833 + ", by Doni Pracner");
836 public String makeDefaultOutName(String inname){
837 String rez = inname;
838 if (inname.endsWith(".obj"))
839 rez = rez.substring(0, rez.length() - 4);
840 return rez + ".wsl";
843 public void run(String[] args) {
844 if (args.length == 0) {
845 printHelp();
846 } else {
847 int i = 0;
848 while (i < args.length && args[i].length() > 0 && args[i].charAt(0) == '-') {
849 if (args[i].compareTo("-h") == 0) {
850 printHelp();
851 return;
852 } else if (args[i].compareTo("--help") == 0) {
853 printLongHelp();
854 return;
855 } else if (args[i].compareTo("--version") == 0
856 || args[i].compareTo("-version") == 0) {
857 printVersion();
858 return;
859 } else if (args[i].compareTo("-o") == 0
860 || args[i].startsWith("--oc")) {
861 if (args[i].length() == 2)
862 originalInComments = true;
863 else if (args[i].length() == 5)
864 originalInComments = args[i].charAt(4) == '+';
865 else
866 originalInComments = true;
867 } else if (args[i].compareTo("--screen") == 0) {
868 out = new PrintWriter(System.out);
869 } else if (args[i].compareTo("-d") == 0) {
870 messages.setPrintLevel(TransMessages.M_DEB);// print debug info
871 } else if (args[i].compareTo("-v") == 0) {
872 messages.setPrintLevel(TransMessages.M_WAR);// print warnings
873 } else if (args[i].compareTo("-q") == 0) {
874 messages.setPrintLevel(TransMessages.M_QUIET);// no printing
875 } else if (args[i].compareToIgnoreCase("--genEStackPrint") == 0) {
876 genPrintEStackOnChange = true;
877 } else if (args[i].compareToIgnoreCase("--genAddrPause") == 0) {
878 genPauseAfterEachAddress = true;
879 } else if (args[i].compareToIgnoreCase("--genAddrPrint") == 0) {
880 genPrintForEachAddress = true;
881 } else if (args[i].compareToIgnoreCase("--genAddr") == 0) {
882 genPrintForEachAddress = true;
883 genPauseAfterEachAddress = true;
884 } else if (args[i].compareToIgnoreCase("--genAll") == 0) {
885 genPrintEStackOnChange = true;
886 genPrintForEachAddress = true;
887 genPauseAfterEachAddress = true;
888 } else if (args[i].compareToIgnoreCase("--genPopPush") == 0) {
889 genPopPush = true;
890 } else if (args[i].compareToIgnoreCase("--genInlinePrint") == 0) {
891 genInlinePrint = true;
892 } else if (args[i].compareToIgnoreCase("--genHeadTail") == 0) {
893 genPopPush = false;
894 } else if (args[i].compareToIgnoreCase("--genProcedurePrint") == 0) {
895 genInlinePrint = false;
896 } else if (args[i].compareToIgnoreCase("--genLocalVars") == 0) {
897 genLocalVars = true;
898 } else if (args[i].compareToIgnoreCase("--genGlobalVars") == 0) {
899 genLocalVars = false;
900 } else if (args[i].compareToIgnoreCase("--genLocalsAsArray") == 0) {
901 genLocalsAsArray = true;
902 } else if (args[i].compareToIgnoreCase("--genLocalsSeparate") == 0) {
903 genLocalsAsArray = false;
904 } else {
905 System.err.println("unknown option, refusing to continue: "+args[i]);
906 System.exit(1);
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();
929 if (i + 2 < args.length) {
930 messages.message("additional parameters ignored, starting from:'"+args[i+2]+"'", TransMessages.M_ERR);
933 if (out == null) {
934 // if not set to screen, or a file, make a default filename
935 try {
936 out = new PrintWriter(makeDefaultOutName(args[i]));
937 } catch (Exception e) {
938 System.err.println("error in opening out file:");
939 e.printStackTrace();
942 Calendar now = Calendar.getInstance();
943 try {
944 convertStream(Files.newInputStream(p));
945 } catch (Exception e) {
946 System.err.println("Failed converting the stream");
947 e.printStackTrace();
949 long mili = Calendar.getInstance().getTimeInMillis()
950 - now.getTimeInMillis();
951 messages.message("conversion time:" + mili + " ms");
952 messages.printMessageCounters();
953 out.close();
957 public static void main(String[] args) {
958 new Mjc2wsl().run(args);
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner