gitweb on Svarog
projekti pod git sistemom za održavanje verzija -- projects under
the git version control system
2 Copyright (C) 2014 Doni Pracner
4 This file is part of mjc2wsl.
6 mjc2wsl is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 mjc2wsl is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with mjc2wsl. If not, see <http://www.gnu.org/licenses/>.
23 * This program converts file from compiled MicroJava bytecode to WSL language
24 * which is a part of the FermaT Transformation system. MicroJava is a subset
25 * used in Compiler Construction courses by Hanspeter Moessenboeck, not
28 * @author Doni Pracner, http://perun.dmi.rs/pracner http://quemaster.com
31 //default version name, used if the file is not found
32 private static String versionN
= "0.1.x";
34 private String versionFile
= "version.properties";
36 private TransMessages messages
= new TransMessages();
38 private boolean genPauseAfterEachAddress
=false,
39 genPrintForEachAddress
= false,
40 genPrintEStackOnChange
= false;
42 private boolean genPopPush
=false;
44 private boolean genInlinePrint
=false;
46 /** Constant used for marking a regular comment from the original file */
47 public static final char C_REG
= ' ';
49 * Constant used for marking when original code is inserted in the file,
50 * next to the translations
52 public static final char C_OC
= '#';
53 /** Constant used for marking special messages from the translator */
54 public static final char C_SPEC
= '&';
55 /** Constant used for marking error messages from the translator */
56 public static final char C_ERR
= '!';
58 /** instruction code in MicroJava bytecode. */
59 public static final int
118 private boolean originalInComments
= false;
120 private HashMap
<Integer
,String
> opMap
= null;
122 private String opCodeFile
= "mj-bytecodes.properties";
124 private HashMap
<Integer
, String
> getOpMap() {
126 opMap
= new HashMap
<Integer
, String
>(60, 0.98f
);
128 BufferedReader in
= new BufferedReader(new InputStreamReader(
129 getClass().getResourceAsStream(opCodeFile
)));
130 String str
= in
.readLine();
131 while (str
!= null) {
132 String
[] ss
= str
.split("=");
133 opMap
.put(Integer
.parseInt(ss
[0]), ss
[1]);
137 } catch (Exception ex
) {
138 ex
.printStackTrace();
144 private Properties versionData
;
146 private String
getVersion() {
147 if (versionData
== null) {
148 versionData
= new Properties();
150 versionData
.load(getClass().getResourceAsStream(versionFile
));
151 } catch (IOException e
) {
155 String ver
= versionData
.getProperty("version");
162 public String
getOpString(int op
) {
163 return getOpMap().get(op
);
166 public String
describeOpCode(int op
) {
167 return op
+ " (" + getOpString(op
) + ")";
170 private InputStream mainIn
;
171 private PrintWriter out
= null;
172 private int counter
= -1;
174 private void pr(int i
){
178 private void pr(char i
){
182 private void pr(String i
){
186 private void prl(String i
){
195 res
= res
<< 24 >>> 24;
196 } catch (IOException ex
) {
197 ex
.printStackTrace();
204 return (get() * 256 + get()) << 16 >> 16;
208 return (get2() << 16) + (get2() << 16 >>> 16);
211 public String
createStandardStart(){
212 return createStandardStart(10);
215 public String
createStandardStart(int numWords
){
216 StringBuilder ret
= new StringBuilder(
217 "C:\" This file automatically converted from microjava bytecode\";\n"
218 +"C:\" with mjc2wsl v "+getVersion()+"\";\n");
220 ret
.append("\nBEGIN");
221 ret
.append("\nVAR <\n\t");
222 ret
.append("mjvm_locals := ARRAY(1,0),");
223 ret
.append("\n\tmjvm_statics := ARRAY("+numWords
+",0),");
224 ret
.append("\n\tmjvm_arrays := < >,");
225 ret
.append("\n\tmjvm_flag_jump := 0,");
226 ret
.append("\n\tmjvm_objects := < >,");
227 ret
.append("\n\tmjvm_estack := < >, mjvm_mstack := < > > :");
229 return ret
.toString();
232 public String
createAsciiString(){
233 StringBuilder ret
= new StringBuilder("C:\"char array for ascii code conversions\";");
234 ret
.append("\nascii := \"????????????????????????????????\"++\n");
235 ret
.append("\" !\"++Quote++\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\";\n");
237 return ret
.toString();
240 public String
createStandardEnd(){
241 StringBuilder ret
= new StringBuilder("SKIP\nENDVAR\n");
242 ret
.append("\nWHERE\n");
244 ret
.append("\nFUNCT CHR(num) ==:\n");
245 ret
.append("\t@List_To_String(< num >)\n");
248 if (!genInlinePrint
) {
249 ret
.append("\nPROC Print_MJ(val, format VAR)==\n");
250 ret
.append(createComment("print spacing", C_SPEC
));
251 ret
.append("\n\tIF format>1 THEN\n\t\tFOR i:=2 TO ");
252 ret
.append("format STEP 1 DO PRINFLUSH(\" \") OD\n");
253 ret
.append("\tFI;\n\tPRINFLUSH(val)\nEND\n");
255 ret
.append("\nPROC Print_MJ_CHAR(val, format VAR)==\n");
256 ret
.append(createComment("print spacing", C_SPEC
));
257 ret
.append("\n\tIF format>1 THEN\n\t\tFOR i:=2 TO ");
258 ret
.append("format STEP 1 DO PRINFLUSH(\" \") OD\n");
259 ret
.append("\tFI;\n\tPRINFLUSH(CHR(val))\n");
264 return ret
.toString();
267 private String
createStartVar(String
... vars
){
268 StringBuilder ret
= new StringBuilder("VAR < ");
269 ret
.append(vars
[0] + " := 0");
270 for (int i
=1; i
<vars
.length
; i
++)
271 ret
.append(", "+ vars
[i
] +" := 0");
274 return ret
.toString();
277 private String
createEndVar(){
281 private String
createLocal(int i
) {
282 // arrays start at 1 in WSL, so we need an offset
283 return "mjvm_locals[" + (i
+ 1) + "]";
286 private String
createStatic(int i
) {
287 return "mjvm_statics[" + (i
+ 1) + "]";
290 private String
createArray(int i
) {
291 return "mjvm_arrays[" + i
+ "]";
294 private String
createArray(String i
) {
295 return "mjvm_arrays[" + i
+ "]";
298 private String
createObject(String i
) {
299 return "mjvm_objects[" + i
+ "]";
303 * Creates a WSL comment with care to quote chars.
305 public static String
createComment(String str
){
306 return createComment(str
, C_REG
);
310 * Creates a WSL comment with care to quote chars, of the
311 * given type. Types are given as char constants. They can be
312 * default comments, comments that contain the original code
313 * in them, or additional comments regarding the translation
316 public static String
createComment(String str
, char type
) {
317 return "C:\"" + type
+ str
.replace("\"", "''") + "\";";
320 // generalised stack operations
322 private String
createToStack(String stack
, String var
){
324 return "PUSH("+stack
+"," + var
+ ");";
326 return stack
+ " := <" + var
+ " > ++ " + stack
+";";
329 private String
createFromStack(String stack
, String var
){
331 return "POP("+ var
+ ", "+stack
+");";
333 return var
+ ":= HEAD("+stack
+"); "+stack
+" := TAIL("+stack
+");";
337 private String
createToEStack(int i
) {
338 return createToEStack(i
+"");
341 private String
createToEStack(String i
) {
342 String res
= createToStack("mjvm_estack", i
);
343 if (genPrintEStackOnChange
)
344 res
+= "PRINT(\"eStack\",mjvm_estack);";
348 private String
createFromEStack(String st
) {
349 String res
= createFromStack("mjvm_estack",st
);
350 if (genPrintEStackOnChange
)
351 res
+= "PRINT(\"eStack\",mjvm_estack);";
355 private String
createPopEStack() {
356 String res
= "mjvm_estack := TAIL(mjvm_estack);";
357 if (genPrintEStackOnChange
)
358 res
+= "PRINT(\"eStack\",mjvm_estack);";
362 private String
createTopTwoEStack() {
363 return createFromEStack("tempa") + "\n" + createFromEStack("tempb");
366 private String
createTopEStack() {
367 return createFromEStack("tempa");
372 private String
createToMStack(int i
) {
373 return createToMStack(i
+"");
376 private String
createToMStack(String i
) {
377 return createToStack("mjvm_mstack", i
);
380 private String
createFromMStack(String st
) {
381 return createFromStack("mjvm_mstack", st
);
384 private String
getRelationFor(int opcode
) throws Exception
{
386 case jeq
: return "=";
387 case jne
: return "<>";
388 case jlt
: return "<";
389 case jle
: return "<=";
390 case jgt
: return ">";
391 case jge
: return ">=";
393 throw new Exception("Wrong opcode for a relation");
396 private boolean isJumpCode(int opcode
) {
397 return (opcode
>= jmp
) && (opcode
<= jge
);
400 public void convertStream(InputStream ins
) throws Exception
{
403 byte m
= (byte) get();
404 byte j
= (byte) get();
405 if (m
!='M' || j
!='J')
406 throw new Exception("Wrong start of bytecode file");
407 int codesize
= get4();
408 int numberOfWords
= get4();
409 int mainAdr
= get4();
411 prl(createStandardStart(numberOfWords
));
412 prl("SKIP;\n ACTIONS a" + (14 + mainAdr
) + " :");
415 prl(" a" + counter
+ " ==");
416 if (originalInComments
)
417 prl(createComment(describeOpCode(op
), C_OC
));
418 if (genPrintForEachAddress
) {
419 prl("PRINT(\"a" + counter
+ "\");");
420 if (genPauseAfterEachAddress
)
421 prl("debug_disposable_string := @Read_Line(Standard_Input_Port);");
425 prl(createToEStack(createLocal(get())));
432 prl(createStartVar("tempa"));
433 prl("tempa :="+createLocal(op
- load_0
)+";");
434 prl(createToEStack("tempa"));
439 prl(createFromEStack(createLocal(get())));
446 prl(createStartVar("tempa"));
447 prl(createFromEStack("tempa"));
448 prl(createLocal(op
- store_0
)+" := tempa;");
454 prl(createToEStack(createStatic(get2())));
458 prl(createFromEStack(createStatic(get2())));
464 prl(createStartVar("tempa"));
465 prl(createTopEStack());
466 prl(createToEStack(createObject("tempa") + "[" + (f
+ 1) + "]"));
472 prl(createStartVar("tempa", "tempb"));
473 prl(createTopTwoEStack());
474 prl(createObject("tempb") + "[" + (f
+ 1) + "]:=tempa;");
480 prl(createToEStack(get4()));
485 prl(createToEStack(-1));
495 prl(createToEStack(op
- const_0
));
500 prl(createStartVar("tempa", "tempb", "tempres"));
501 prl(createTopTwoEStack());
502 prl("tempres := tempb + tempa;");
503 prl(createToEStack("tempres"));
508 prl(createStartVar("tempa", "tempb", "tempres"));
509 prl(createTopTwoEStack());
510 prl("tempres := tempb - tempa;");
511 prl(createToEStack("tempres"));
516 prl(createStartVar("tempa", "tempb", "tempres"));
517 prl(createTopTwoEStack());
518 prl("tempres := tempb * tempa;");
519 prl(createToEStack("tempres"));
524 prl(createStartVar("tempa", "tempb", "tempres"));
525 prl(createTopTwoEStack());
526 prl("IF tempa = 0 THEN ERROR(\"division by zero\") FI;");
527 prl("tempres := tempb DIV tempa;");
528 prl(createToEStack("tempres"));
533 prl(createStartVar("tempa", "tempb", "tempres"));
534 prl(createTopTwoEStack());
535 prl("IF tempa = 0 THEN ERROR(\"division by zero\") FI;");
536 prl("tempres := tempb MOD tempa;");
537 prl(createToEStack("tempres"));
543 prl(createStartVar("tempa"));
544 prl(createTopEStack());
545 prl(createToEStack("-tempa"));
551 prl(createStartVar("tempa", "tempb"));
552 prl(createTopTwoEStack());
553 prl("VAR <tempres :=tempb, i:=1 >:");
554 prl("\tFOR i:=1 TO tempa STEP 1 DO tempres := tempres * 2 OD;");
555 prl(createToEStack("tempres"));
561 prl(createStartVar("tempa", "tempb"));
562 prl(createTopTwoEStack());
563 prl("VAR <tempres :=tempb, i:=1 >:");
564 prl("\tFOR i:=1 TO tempa STEP 1 DO tempres := tempres DIV 2 OD;");
565 prl(createToEStack("tempres"));
572 int b1
= get(), b2
= get();
573 prl(createLocal(b1
) + " := " + createLocal(b1
) + " + " + b2
+ ";");
579 // TODO maybe objects and arrays should be in the same list?
580 prl("mjvm_objects := mjvm_objects ++ < ARRAY(" + size
582 prl(createToEStack("LENGTH(mjvm_objects)"));
586 get();// 0 - bytes, 1 - words; ignore for now
587 // TODO take into consideration 0/1
588 prl(createStartVar("tempa"));
589 prl(createTopEStack());
590 prl("mjvm_arrays := mjvm_arrays ++ < ARRAY(tempa,0) >;");
591 prl(createToEStack("LENGTH(mjvm_arrays)"));
598 prl(createStartVar("tempa", "tempb"));
599 prl(createTopTwoEStack());
600 prl(createToEStack(createArray("tempb") + "[tempa+1]"));
606 prl(createStartVar("tempa", "tempb", "tempres"));
607 prl(createFromEStack("tempres"));
608 prl(createTopTwoEStack());
609 prl("mjvm_arrays[tempb][tempa+1]:=tempres;");
614 prl(createStartVar("tempa", "tempb"));
615 prl(createTopEStack());
616 prl("tempb := LENGTH("+ createArray("tempa") + ");");
617 prl(createToEStack("tempb"));
623 prl(createStartVar("tempa", "tempb"));
624 prl(createTopEStack());
625 prl(createToEStack("tempa"));
626 prl(createToEStack("tempa"));
631 prl(createStartVar("tempa", "tempb"));
632 prl(createTopTwoEStack());
633 prl(createToEStack("tempb"));
634 prl(createToEStack("tempa"));
635 prl(createToEStack("tempb"));
636 prl(createToEStack("tempa"));
642 prl(createPopEStack());
647 prl("CALL a" + (counter
+ get2()) + ";");
657 prl(createStartVar("tempa", "tempb"));
658 prl(createTopTwoEStack());
659 prl("IF tempb " + getRelationFor(op
)
660 + " tempa THEN mjvm_flag_jump := 1"
661 + " ELSE mjvm_flag_jump := 0"
664 prl("IF mjvm_flag_jump = 1 THEN CALL a"
666 + " ELSE CALL a" + (counter
+ 1)
673 prl("CALL a" + (counter
+ get2()) + ";");
678 // we let the actions return
679 // there is nothing to clean up
680 prl("SKIP\n END\n b" + counter
+ " ==");
684 int parameters
= get();
687 prl(createToMStack("mjvm_locals"));
688 prl("mjvm_locals := ARRAY(" + locals
+ ",0);");
689 for (int i
= parameters
- 1; i
>= 0; i
--)
690 prl(createFromEStack(createLocal(i
)));
694 prl(createFromMStack("mjvm_locals"));
700 // TODO make it a char for read
701 messages
.message("char is read like a number", TransMessages
.M_WAR
);
702 prl(createComment("char is read like a number", C_SPEC
));
705 prl(createStartVar("tempa"));
706 prl("tempa := @String_To_Num(@Read_Line(Standard_Input_Port));");
707 prl(createToEStack("tempa"));
714 prl(createStartVar("tempa", "tempb"));
715 prl(createTopTwoEStack());
717 prl(createComment("print spacing and transformation",C_SPEC
));
718 prl("PRINFLUSH(SUBSTR(\" \", 0, MIN(10, MAX(0,tempa-1))), @List_To_String(< tempb >));");
720 prl("Print_MJ_CHAR(tempb,tempa);");
725 // TODO printing numbers needs different lengths of spacing
726 prl(createStartVar("tempa", "tempb"));
728 prl(createTopTwoEStack());
730 prl(createComment("print spacing",C_SPEC
));
731 prl("PRINFLUSH(SUBSTR(\" \", 0, MIN(10, MAX(0, tempa-1))), tempb);");
734 prl("Print_MJ(tempb,tempa);");
740 prl("ERROR(\"Runtime error: trap(" + get() + ")\");");
745 prl(createComment("unknown op error: " + op
, C_ERR
));
746 messages
.message("unknown op error: " + op
, TransMessages
.M_ERR
);
750 boolean wasJump
= isJumpCode(op
);
756 prl("CALL a" + counter
+ "\n END");
758 prl("SKIP\n END\nENDACTIONS;\n");
759 pr(createStandardEnd());
762 public void convertFile(File f
) {
764 convertStream(new FileInputStream(f
));
765 } catch (Exception ex
) {
766 ex
.printStackTrace();
770 public void printHelp() {
777 public void printLongHelp() {
780 System
.out
.println();
782 System
.out
.println();
783 printHelpDirectives();
784 System
.out
.println();
785 printHelpGenerating();
786 System
.out
.println();
790 public void printHelpOutput() {
791 System
.out
.println("Output options:");
792 System
.out
.println(" --screen print output to screen");
793 System
.out
.println(" -o --oc[+-] include original code in comments");
794 System
.out
.println(" -v verbose, print warning messages");
795 System
.out
.println(" -q quiet; don't print even the error messages");
796 System
.out
.println(" -d print detailed debug messages");
799 public void printHelpGenerating() {
800 System
.out
.println("Options for generating extra code for tracking code execution");
801 System
.out
.println(" --genEStackPrint generate print for all EStack changes");
802 System
.out
.println(" --genAddrPrint generate prints after every address of the original code ");
803 System
.out
.println(" --genAddrPause generate a pause after every address of the original code ");
804 System
.out
.println(" --genAddr short for --genAddrPrint and --genAddrPause");
805 System
.out
.println(" --genAll short for applying all code generation");
808 public void printHelpDirectives(){
809 System
.out
.println("Alternatives for code generation:");
810 System
.out
.println(" --genPopPush generate POP/PUSH instead of TAIL/HEAD");
811 System
.out
.println(" --genHeadTail generate TAIL/HEAD instead of POP/PUSH ");
812 System
.out
.println();
813 System
.out
.println(" --genInlinePrint generate prints directly instead of procedure calls");
814 System
.out
.println(" --genProcedurePrint generate prints as custom procedure calls");
817 public void printHelpHelp() {
818 System
.out
.println("Help and info options");
819 System
.out
.println(" -h basic help");
820 System
.out
.println(" --help print more detailed help");
821 System
.out
.println(" --version or -version print version and exit");
824 public void printUsage(){
825 System
.out
.println("usage:\n\t mjc2wsl {options} filename [outfile]");
828 public void printVersion() {
829 System
.out
.println("MicroJava bytecode to WSL converter. v " + getVersion()
830 + ", by Doni Pracner");
833 public String
makeDefaultOutName(String inname
){
835 if (inname
.endsWith(".obj"))
836 rez
= rez
.substring(0, rez
.length() - 4);
840 public void run(String
[] args
) {
841 if (args
.length
== 0) {
845 while (i
< args
.length
&& args
[i
].charAt(0) == '-') {
846 if (args
[i
].compareTo("-h") == 0) {
849 } else if (args
[i
].compareTo("--help") == 0) {
852 } else if (args
[i
].compareTo("--version") == 0
853 || args
[i
].compareTo("-version") == 0) {
856 } else if (args
[i
].compareTo("-o") == 0
857 || args
[i
].startsWith("--oc")) {
858 if (args
[i
].length() == 2)
859 originalInComments
= true;
860 else if (args
[i
].length() == 5)
861 originalInComments
= args
[i
].charAt(4) == '+';
863 originalInComments
= true;
864 } else if (args
[i
].compareTo("--screen") == 0) {
865 out
= new PrintWriter(System
.out
);
866 } else if (args
[i
].compareTo("-d") == 0) {
867 messages
.setPrintLevel(TransMessages
.M_DEB
);// print debug info
868 } else if (args
[i
].compareTo("-v") == 0) {
869 messages
.setPrintLevel(TransMessages
.M_WAR
);// print warnings
870 } else if (args
[i
].compareTo("-q") == 0) {
871 messages
.setPrintLevel(TransMessages
.M_QUIET
);// no printing
872 } else if (args
[i
].compareToIgnoreCase("--genEStackPrint") == 0) {
873 genPrintEStackOnChange
= true;
874 } else if (args
[i
].compareToIgnoreCase("--genAddrPause") == 0) {
875 genPauseAfterEachAddress
= true;
876 } else if (args
[i
].compareToIgnoreCase("--genAddrPrint") == 0) {
877 genPrintForEachAddress
= true;
878 } else if (args
[i
].compareToIgnoreCase("--genAddr") == 0) {
879 genPrintForEachAddress
= true;
880 genPauseAfterEachAddress
= true;
881 } else if (args
[i
].compareToIgnoreCase("--genAll") == 0) {
882 genPrintEStackOnChange
= true;
883 genPrintForEachAddress
= true;
884 genPauseAfterEachAddress
= true;
885 } else if (args
[i
].compareToIgnoreCase("--genPopPush") == 0) {
887 } else if (args
[i
].compareToIgnoreCase("--genInlinePrint") == 0) {
888 genInlinePrint
= true;
889 } else if (args
[i
].compareToIgnoreCase("--genHeadTail") == 0) {
891 } else if (args
[i
].compareToIgnoreCase("--genProcedurePrint") == 0) {
892 genInlinePrint
= false;
897 if (i
>= args
.length
) {
898 System
.out
.println("no filename supplied");
901 File f
= new File(args
[i
]);
903 if (i
+ 1 < args
.length
) {
905 out
= new PrintWriter(args
[i
+ 1]);
906 } catch (Exception e
) {
907 System
.err
.println("error in opening out file:");
912 // if not set to screen, or a file, make a default filename
914 out
= new PrintWriter(makeDefaultOutName(args
[i
]));
915 } catch (Exception e
) {
916 System
.err
.println("error in opening out file:");
921 Calendar now
= Calendar
.getInstance();
923 long mili
= Calendar
.getInstance().getTimeInMillis()
924 - now
.getTimeInMillis();
925 System
.out
.println("conversion time:" + mili
+ " ms");
926 messages
.printMessageCounters();
929 System
.out
.println("file does not exist");
933 public static void main(String
[] args
) {
934 new mjc2wsl().run(args
);
Svarog.pmf.uns.ac.rs/gitweb
maintanance
Doni Pracner