gitweb on Svarog
projekti pod git sistemom za održavanje verzija -- projects under
the git version control system
1682978fd688923fab5f6962073fe3dc93bc377f
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
10 * @author Doni Pracner, http://perun.dmi.rs/pracner http://quemaster.com
13 public static String versionN
= "0.1.4";
15 public static final int M_ERR
= 2, M_WAR
= 1, M_DEB
= 0;
17 private int printLevel
= M_ERR
;
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
]++;
27 private void printMessageCounters(){
28 printMessageCounters(System
.out
);
31 private void printMessageCounters(PrintStream out
){
32 out
.println("total errors:"+messageCounters
[M_ERR
]+" warnings:"+messageCounters
[M_WAR
]);
35 private boolean genPauseAfterEachAddress
=false,
36 genPrintForEachAddress
= false,
37 genPrintEStackOnChange
= false;
39 /** Constant used for marking a regular comment from the original file */
40 public static final char C_REG
= ' ';
42 * Constant used for marking when original code is inserted in the file,
43 * next to the translations
45 public static final char C_OC
= '#';
46 /** Constant used for marking special messages from the translator */
47 public static final char C_SPEC
= '&';
48 /** Constant used for marking error messages from the translator */
49 public static final char C_ERR
= '!';
51 /** instruction code in MicroJava bytecode. */
52 public static final int
111 public String
getStandardStart(){
112 return getStandardStart(10);
115 public String
getStandardStart(int numWords
){
116 StringBuilder ret
= new StringBuilder(
117 "C:\" This file automatically converted from microjava bytecode\";\n"
118 +"C:\" with mjc2wsl v "+versionN
+"\";\n");
120 ret
.append("VAR < tempa := 0, tempb := 0, tempres :=0,\n\t");
121 ret
.append("mjvm_locals := ARRAY(1,0), ");
122 ret
.append("\n\tmjvm_statics := ARRAY("+numWords
+",0), ");
123 ret
.append("\n\tmjvm_arrays := < >, ");
124 ret
.append("\n\tmjvm_objects := < >, ");
125 ret
.append("\n mjvm_estack := < >, mjvm_mstack := < >, ");
126 ret
.append("\n mjvm_fp := 0, mjvm_sp := 0,");
127 ret
.append("\n t_e_m_p := 0 > :");
129 return ret
.toString();
132 public String
getStandardEnd(){
133 return "SKIP\nENDVAR";
136 private boolean originalInComments
= false;
138 private HashMap
<Integer
,String
> opMap
= null;
140 private String opCodeFile
= "mj-bytecodes.properties";
142 private HashMap
<Integer
,String
> getOpMap() {
144 opMap
= new HashMap
<Integer
, String
> (60, 0.98f
);
146 BufferedReader in
= new BufferedReader(
147 new InputStreamReader(getClass().getResourceAsStream(opCodeFile
)));
148 String str
= in
.readLine();
149 while (str
!= null) {
150 String
[] ss
= str
.split("=");
151 opMap
.put(Integer
.parseInt(ss
[0]),ss
[1]);
155 }catch (Exception ex
) {
156 ex
.printStackTrace();
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 private String
loc(int i
){
212 //arrays start at 1 in WSL, so we need an offset
213 return "mjvm_locals[" + (i
+1)+"]";
216 private String
genStatic(int i
){
217 return "mjvm_statics[" + (i
+1)+"]";
220 private String
genArray(int i
){
221 return "mjvm_arrays["+ i
+"]";
224 private String
genArray(String i
){
225 return "mjvm_arrays["+ i
+"]";
228 private String
genObject(String i
){
229 return "mjvm_objects["+ i
+"]";
233 * Creates a WSL comment with care to quote chars.
235 public static String
createComment(String str
){
236 return createComment(str
, C_REG
);
240 * Creates a WSL comment with care to quote chars, of the
241 * given type. Types are given as char constants. They can be
242 * default comments, comments that contain the original code
243 * in them, or additional comments regarding the translation
246 public static String
createComment(String str
, char type
) {
247 return "C:\"" + type
+ str
.replace("\"", "''") + "\";";
252 private String
cmdToEStack(int i
) {
253 String res
= "mjvm_estack := <" + i
+ " > ++ mjvm_estack;";
254 if (genPrintEStackOnChange
) res
+="PRINT(\"eStack\",mjvm_estack);";
258 private String
cmdToEStack(String i
) {
259 String res
= "mjvm_estack := <" + i
+ " > ++ mjvm_estack;";
260 if (genPrintEStackOnChange
) res
+="PRINT(\"eStack\",mjvm_estack);";
264 private String
cmdFromEStack(String st
) {
265 String res
= st
+ " := HEAD(mjvm_estack); mjvm_estack := TAIL(mjvm_estack);";
266 if (genPrintEStackOnChange
) res
+="PRINT(\"eStack\",mjvm_estack);";
270 private String
cmdPopEStack() {
271 String res
= "mjvm_estack := TAIL(mjvm_estack);";
272 if (genPrintEStackOnChange
) res
+="PRINT(\"eStack\",mjvm_estack);";
277 private String
getTopTwo(){
278 return cmdFromEStack("tempa") + "\n" + cmdFromEStack("tempb");
281 private String
getTop() {
282 return cmdFromEStack("tempa");
287 private String
cmdToMStack(int i
) {
288 return "mjvm_mstack := <" + i
+ " > ++ mjvm_mstack;";
291 private String
cmdToMStack(String i
) {
292 return "mjvm_mstack := <" + i
+ " > ++ mjvm_mstack;";
295 private String
cmdFromMStack(String st
) {
296 return st
+ " := HEAD(mjvm_mstack); mjvm_mstack := TAIL(mjvm_mstack);";
299 private String
getRelationFor(int opcode
) throws Exception
{
301 case jeq
: return "=";
302 case jne
: return "<>";
303 case jlt
: return "<";
304 case jle
: return "<=";
305 case jgt
: return ">";
306 case jge
: return ">=";
308 throw new Exception("Wrong opcode for a relation");
311 private boolean isJumpCode(int opcode
) {
312 return (opcode
>=jmp
) && (opcode
<=jge
);
315 public void convertStream(InputStream ins
) throws Exception
{
318 byte m
= (byte) get();
319 byte j
= (byte) get();
320 if (m
!='M' || j
!='J')
321 throw new Exception("Wrong start of bytecode file");
322 int codesize
= get4();
323 int numberOfWords
= get4();
324 int mainAdr
= get4();
326 prl(getStandardStart(numberOfWords
));
327 prl("SKIP;\n ACTIONS A_S_start:\n A_S_start == CALL a"+(14+mainAdr
)+" END");
330 if (originalInComments
)
331 prl(createComment(describeOpCode(op
), C_OC
));
332 prl("a" + counter
+ " == ");
333 if (genPrintForEachAddress
) {
334 prl("PRINT(\"a"+counter
+"\");");
335 if (genPauseAfterEachAddress
)
336 prl("debug_disposable_string := @Read_Line(Standard_Input_Port);");
340 prl(cmdToEStack(loc(get())));
347 prl(cmdToEStack(loc(op
- load_0
)));
351 prl(cmdFromEStack(loc(get())));
358 prl(cmdFromEStack(loc(op
- store_0
)));
363 prl(cmdToEStack(genStatic(get2())));
367 prl(cmdFromEStack(genStatic(get2())));
374 prl(cmdToEStack(genObject("tempa")+"["+(f
+1)+"]"));
379 //we need to use a temparray as a pointer, WSL
380 //otherwise tries to access it as a list of lists and fails
382 prl("VAR < tempArray := "+genObject("tempb")+" > :");
383 prl("tempArray["+(f
+1)+"]:=tempa ENDVAR;");
388 prl(cmdToEStack(get4()));
398 prl(cmdToEStack(op
- const_0
));
404 prl("tempres := tempb + tempa;");
405 prl(cmdToEStack("tempres"));
410 prl("tempres := tempb - tempa;");
411 prl(cmdToEStack("tempres"));
416 prl("tempres := tempb * tempa;");
417 prl(cmdToEStack("tempres"));
422 prl("IF tempa = 0 THEN ERROR(\"division by zero\") FI;");
423 prl("tempres := tempb DIV tempa;");
424 prl(cmdToEStack("tempres"));
429 prl("IF tempa = 0 THEN ERROR(\"division by zero\") FI;");
430 prl("tempres := tempb MOD tempa;");
431 prl(cmdToEStack("tempres"));
437 prl(cmdToEStack("-tempa"));
443 prl("VAR <tempres :=tempb, i:=1 >:");
444 prl("\tFOR i:=1 TO tempa STEP 1 DO tempres := tempres * 2 OD;");
445 prl(cmdToEStack("tempres"));
451 prl("VAR <tempres :=tempb, i:=1 >:");
452 prl("\tFOR i:=1 TO tempa STEP 1 DO tempres := tempres DIV 2 OD;");
453 prl(cmdToEStack("tempres"));
459 int b1
= get(), b2
= get();
460 prl(loc(b1
)+" := " +loc(b1
)+" + "+b2
+";");
466 //TODO maybe objects and arrays should be in the same list?
467 prl("mjvm_objects := mjvm_objects ++ < ARRAY("+size
+",0) >;");
468 prl(cmdToEStack("LENGTH(mjvm_objects)"));
472 get();// 0 - bytes, 1 - words; ignore for now
473 //TODO take into consideration 0/1
475 prl("mjvm_arrays := mjvm_arrays ++ < ARRAY(tempa,0) >;");
476 prl(cmdToEStack("LENGTH(mjvm_arrays)"));
483 prl(cmdToEStack(genArray("tempb")+"[tempa+1]"));
488 prl(cmdFromEStack("tempres"));
490 //we need to use a temparray as a pointer, WSL
491 //otherwise tries to access it as a list of lists and fails
492 prl("VAR < tempArray := "+genArray("tempb")+" > :");
493 prl("tempArray[tempa+1]:=tempres ENDVAR;");
498 //TODO make an array length function of some sort!
499 prl(createComment("array length not known - LENGTH not aplicable to arrays",C_ERR
));
500 message("array length not known - LENGTH not aplicable to arrays",M_ERR
);
501 prl(createComment("put 1 on the stack for consistency",C_SPEC
));
508 prl(cmdToEStack("tempa"));
509 prl(cmdToEStack("tempa"));
514 prl(cmdToEStack("tempb"));
515 prl(cmdToEStack("tempa"));
516 prl(cmdToEStack("tempb"));
517 prl(cmdToEStack("tempa"));
527 prl("CALL a" + (counter
+ get2()) + ";");
538 prl("IF tempb "+ getRelationFor(op
)
539 +" tempa THEN CALL a" + (counter
+ get2())
540 +" ELSE CALL a" + (counter
+1)
546 prl("CALL a" + (counter
+ get2()) + ";");
551 //we let the actions return
552 // there is nothing to clean up
553 prl("SKIP END b"+counter
+" ==");
557 int parameters
= get();
560 prl(cmdToMStack("mjvm_locals"));
561 prl("mjvm_locals := ARRAY("+locals
+",0);");
562 for (int i
= parameters
-1; i
>= 0; i
--)
563 prl(cmdFromEStack(loc(i
)));
567 prl(cmdFromMStack("mjvm_locals"));
573 //TODO make it a char for read
574 message("char is read like a number", M_WAR
);
575 prl(createComment("char is read like a number",C_SPEC
));
578 prl("tempa := @String_To_Num(@Read_Line(Standard_Input_Port));");
579 prl(cmdToEStack("tempa"));
585 // TODO need to make it a char on print
586 message("chars will be printed as number codes", M_WAR
);
587 prl(createComment("char will be printed as a number code",C_SPEC
));
590 // TODO printing numbers needs different lengths of spacing
592 pr(createComment("print spacing",C_SPEC
));
593 prl("IF tempa>1 THEN FOR i:=2 TO tempa STEP 1 DO PRINFLUSH(\" \") OD FI;");
594 prl("PRINFLUSH(tempb);");
599 prl("ERROR(\"Runtime error: trap("+get()+")\");");
605 prl(createComment("unknown op error: " + op
, C_ERR
));
606 message("unknown op error: "+ op
, M_ERR
);
610 boolean wasJump
= isJumpCode(op
);
616 prl("CALL a" + counter
+ " END");
618 prl("CALL Z;\nSKIP END\nENDACTIONS;\n");
619 prl(getStandardEnd());
623 public void convertFile(File f
) {
625 convertStream(new FileInputStream(f
));
626 } catch (Exception ex
) {
627 ex
.printStackTrace();
631 public void printHelp() {
632 System
.out
.println("MicroJava bytecode to WSL converter. v " + versionN
633 + ", by Doni Pracner");
634 System
.out
.println("usage:\n\t mjc2wsl {options} filename [outfile]");
635 System
.out
.println("options:\n\t--screen print output to screen");
636 System
.out
.println("\t-o --oc[+-] include original code in comments");
637 System
.out
.println("\t-v verbose, print warning messages");
638 System
.out
.println("\t-q don't print even the error messages");
639 System
.out
.println("\t-d print detailed debug messages");
642 public String
makeDefaultOutName(String inname
){
644 if (inname
.endsWith(".obj"))
645 rez
= rez
.substring(0, rez
.length() - 4);
649 public void run(String
[] args
) {
650 if (args
.length
== 0) {
654 while (i
< args
.length
&& args
[i
].charAt(0) == '-') {
655 if (args
[i
].compareTo("-h") == 0) {
658 } else if (args
[i
].compareTo("-o") == 0
659 || args
[i
].startsWith("--oc")) {
660 if (args
[i
].length() == 2)
661 originalInComments
= true;
662 else if (args
[i
].length() == 5)
663 originalInComments
= args
[i
].charAt(4) == '+';
665 originalInComments
= true;
666 } else if (args
[i
].compareTo("--screen") == 0) {
667 out
= new PrintWriter(System
.out
);
668 } else if (args
[i
].compareTo("-d") == 0) {
669 printLevel
= M_DEB
;//print debug info
670 } else if (args
[i
].compareTo("-v") == 0) {
671 printLevel
= M_WAR
;//print warnings
672 } else if (args
[i
].compareTo("-q") == 0) {
673 printLevel
= M_ERR
+1;//no printing
674 } else if (args
[i
].compareToIgnoreCase("--genEStackPrint") == 0) {
675 genPrintEStackOnChange
= true;
676 } else if (args
[i
].compareToIgnoreCase("--genAddrPause") == 0) {
677 genPauseAfterEachAddress
= true;
678 } else if (args
[i
].compareToIgnoreCase("--genAddrPrint") == 0) {
679 genPrintForEachAddress
= true;
680 } else if (args
[i
].compareToIgnoreCase("--genAddr") == 0) {
681 genPrintForEachAddress
= true;
682 genPauseAfterEachAddress
= true;
683 } else if (args
[i
].compareToIgnoreCase("--genAll") == 0) {
684 genPrintEStackOnChange
= true;
685 genPrintForEachAddress
= true;
686 genPauseAfterEachAddress
= true;
690 if (i
>= args
.length
) {
691 System
.out
.println("no filename supplied");
694 File f
= new File(args
[i
]);
696 if (i
+ 1 < args
.length
) {
698 out
= new PrintWriter(args
[i
+ 1]);
699 } catch (Exception e
) {
700 System
.err
.println("error in opening out file:");
705 // if not set to screen, or a file, make a default filename
707 out
= new PrintWriter(makeDefaultOutName(args
[i
]));
708 } catch (Exception e
) {
709 System
.err
.println("error in opening out file:");
714 Calendar now
= Calendar
.getInstance();
716 long mili
= Calendar
.getInstance().getTimeInMillis()
717 - now
.getTimeInMillis();
718 System
.out
.println("conversion time:" + mili
+ " ms");
719 printMessageCounters();
722 System
.out
.println("file does not exist");
726 public static void main(String
[] args
) {
727 new mjc2wsl().run(args
);
Svarog.pmf.uns.ac.rs/gitweb
maintanance
Doni Pracner