gitweb on Svarog
projekti pod git sistemom za održavanje verzija -- projects under
the git version control system
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 /** Constant used for marking a regular comment from the original file */
36 public static final char C_REG
= ' ';
38 * Constant used for marking when original code is inserted in the file,
39 * next to the translations
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
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\t");
113 for (int i
= 0; i
<= 3; i
++)
114 ret
.append("mjvm_loc" + i
+ " := 0, ");
115 ret
.append("\n mjvm_estack := < >, mjvm_mstack := < >, ");
116 ret
.append("\n mjvm_fp := 0, mjvm_sp := 0,");
117 ret
.append("\n t_e_m_p := 0 > :");
119 return ret
.toString();
122 public String
getStandardEnd(){
123 return "SKIP\nENDVAR";
126 private boolean originalInComments
= false;
128 private HashMap
<Integer
,String
> opMap
= null;
130 private String opCodeFile
= "mj-bytecodes.properties";
132 private HashMap
<Integer
,String
> getOpMap() {
134 opMap
= new HashMap
<Integer
, String
> (60, 0.98f
);
136 BufferedReader in
= new BufferedReader(
137 new InputStreamReader(getClass().getResourceAsStream(opCodeFile
)));
138 String str
= in
.readLine();
139 while (str
!= null) {
140 String
[] ss
= str
.split("=");
141 opMap
.put(Integer
.parseInt(ss
[0]),ss
[1]);
145 }catch (Exception ex
) {
146 ex
.printStackTrace();
152 public String
getOpString(int op
) {
153 return getOpMap().get(op
);
156 public String
describeOpCode(int op
) {
157 return op
+ " (" + getOpString(op
) + ")";
160 private InputStream mainIn
;
161 private PrintWriter out
= null;
162 private int counter
= -1;
164 private void pr(int i
){
168 private void pr(char i
){
172 private void pr(String i
){
176 private void prl(String i
){
185 res
= res
<< 24 >>> 24;
186 } catch (IOException ex
) {
187 ex
.printStackTrace();
194 return (get() * 256 + get()) << 16 >> 16;
198 return (get2() << 16) + (get2() << 16 >>> 16);
201 private String
loc(int i
){
202 return "mjvm_loc" + i
;
206 * Creates a WSL comment with care to quote chars.
208 public static String
createComment(String str
){
209 return createComment(str
, C_REG
);
213 * Creates a WSL comment with care to quote chars, of the
214 * given type. Types are given as char constants. They can be
215 * default comments, comments that contain the original code
216 * in them, or additional comments regarding the translation
219 public static String
createComment(String str
, char type
) {
220 return "C:\"" + type
+ str
.replace("\"", "''") + "\";";
225 private String
cmdToEStack(int i
) {
226 return "mjvm_estack := <" + i
+ " > ++ mjvm_estack;";
229 private String
cmdToEStack(String i
) {
230 return "mjvm_estack := <" + i
+ " > ++ mjvm_estack;";
233 private String
cmdFromEStack(String st
) {
234 return st
+ " := HEAD(mjvm_estack); mjvm_estack := TAIL(mjvm_estack);";
237 private String
getTopTwo(){
238 return cmdFromEStack("tempa") + "\n" + cmdFromEStack("tempb");
241 private String
getTop() {
242 return cmdFromEStack("tempa");
247 private String
cmdToMStack(int i
) {
248 return "mjvm_mstack := <" + i
+ " > ++ mjvm_mstack;";
251 private String
cmdToMStack(String i
) {
252 return "mjvm_mstack := <" + i
+ " > ++ mjvm_mstack;";
255 private String
cmdFromMStack(String st
) {
256 return st
+ " := HEAD(mjvm_mstack); mjvm_mstack := TAIL(mjvm_mstack);";
259 private String
getRelationFor(int opcode
) throws Exception
{
261 case jeq
: return "=";
262 case jne
: return "<>";
263 case jlt
: return "<";
264 case jle
: return "<=";
265 case jgt
: return ">";
266 case jge
: return ">=";
268 throw new Exception("Wrong opcode for a relation");
271 public void convertStream(InputStream ins
) throws Exception
{
274 byte m
= (byte) get();
275 byte j
= (byte) get();
276 if (m
!='M' || j
!='J')
277 throw new Exception("Wrong start of bytecode file");
278 int codesize
= get4();
279 int numberOfWords
= get4();
280 int mainAdr
= get4();
282 prl(getStandardStart());
283 prl("SKIP;\n ACTIONS A_S_start:\n A_S_start == CALL a"+(14+mainAdr
)+" END");
286 if (originalInComments
)
287 prl(createComment(describeOpCode(op
), C_OC
));
288 prl("a" + counter
+ " == ");
291 prl(cmdToEStack(loc(get())));
298 prl(cmdToEStack(loc(op
- load_0
)));
302 prl(cmdFromEStack(loc(get())));
309 prl(cmdFromEStack(loc(op
- store_0
)));
313 //TODO getstatic, putstatic
314 //TODO getfield, putfield
317 prl(cmdToEStack(get4()));
327 prl(cmdToEStack(op
- const_0
));
333 prl("tempres := tempb + tempa;");
334 prl(cmdToEStack("tempres"));
339 prl("tempres := tempb - tempa;");
340 prl(cmdToEStack("tempres"));
345 prl("tempres := tempb * tempa;");
346 prl(cmdToEStack("tempres"));
351 prl("IF tempa = 0 THEN ERROR(\"division by zero\") FI;");
352 prl("tempres := tempb DIV tempa;");
353 prl(cmdToEStack("tempres"));
358 prl("IF tempa = 0 THEN ERROR(\"division by zero\") FI;");
359 prl("tempres := tempb MOD tempa;");
360 prl(cmdToEStack("tempres"));
364 //TODO neg, shl, shr, inc
366 //TODO aload, asstore, baload, bastore
368 //TODO pop, dup, dup2
371 prl("CALL a" + (counter
+ get2()) + ";");
382 prl("IF tempb "+ getRelationFor(op
)
383 +" tempa THEN CALL a" + (counter
+ get2())
389 prl(cmdToMStack(counter
+2));
390 prl("CALL a" + (counter
+ get2()) + ";");
395 prl("IF EMPTY?(mjvm_mstack) THEN CALL Z ELSE");
396 //else we let things return
397 prl(cmdFromMStack("tempa"));
399 prl("END b"+counter
+" ==");
403 prl(createComment("enter not fully procesed yet"));
404 message("enter not fully procesed yet", M_WAR
);
410 prl(createComment("exit not fully procesed yet"));
411 message("exit not fully procesed yet", M_WAR
);
417 prl("tempa := @String_To_Num(@Read_Line(Standard_Input_Port));");
418 prl(cmdToEStack("tempa"));
425 prl("PRINT(tempb);");
429 // TODO need to make it a char
431 prl("PRINT(tempb);");
437 prl(createComment("trap not fully procesed yet"));
438 message("trap not fully procesed yet", M_WAR
);
445 prl(createComment("unknown op error: " + op
, C_ERR
));
446 message("unknown op error: "+ op
, M_ERR
);
452 prl("CALL a" + counter
+ " END");
454 prl("CALL Z;\nSKIP END\nENDACTIONS;\n");
455 prl(getStandardEnd());
459 public void convertFile(File f
) {
461 convertStream(new FileInputStream(f
));
462 } catch (Exception ex
) {
463 ex
.printStackTrace();
467 public void printHelp() {
468 System
.out
.println("MicroJava bytecode to WSL converter. v " + versionN
469 + ", by Doni Pracner");
470 System
.out
.println("usage:\n\t {options} mjc2wsl filename [outfile]");
471 System
.out
.println("options:\n\t--screen print output to screen");
472 System
.out
.println("\t-o --oc[+-] include original code in comments");
473 System
.out
.println("\t-v verbose, print warning messages");
474 System
.out
.println("\t-q don't print even the error messages");
475 System
.out
.println("\t-d print detailed debug messages");
478 public String
makeDefaultOutName(String inname
){
480 if (inname
.endsWith(".obj"))
481 rez
= rez
.substring(0, rez
.length() - 4);
485 public void run(String
[] args
) {
486 if (args
.length
== 0) {
490 while (i
< args
.length
&& args
[i
].charAt(0) == '-') {
491 if (args
[i
].compareTo("-h") == 0) {
494 } else if (args
[i
].compareTo("-o") == 0
495 || args
[i
].startsWith("--oc")) {
496 if (args
[i
].length() == 2)
497 originalInComments
= true;
498 else if (args
[i
].length() == 5)
499 originalInComments
= args
[i
].charAt(4) == '+';
501 originalInComments
= true;
502 } else if (args
[i
].startsWith("--screen")) {
503 out
= new PrintWriter(System
.out
);
504 } else if (args
[i
].compareTo("-d") == 0) {
505 printLevel
= M_DEB
;//print debug info
506 } else if (args
[i
].compareTo("-v") == 0) {
507 printLevel
= M_WAR
;//print warnings
508 } else if (args
[i
].compareTo("-q") == 0) {
509 printLevel
= M_ERR
+1;//no printing
514 if (i
>= args
.length
) {
515 System
.out
.println("no filename supplied");
518 File f
= new File(args
[i
]);
520 if (i
+ 1 < args
.length
) {
522 out
= new PrintWriter(args
[i
+ 1]);
523 } catch (Exception e
) {
524 System
.err
.println("error in opening out file:");
529 // if not set to screen, or a file, make a default filename
531 out
= new PrintWriter(makeDefaultOutName(args
[i
]));
532 } catch (Exception e
) {
533 System
.err
.println("error in opening out file:");
538 Calendar now
= Calendar
.getInstance();
540 long mili
= Calendar
.getInstance().getTimeInMillis()
541 - now
.getTimeInMillis();
542 System
.out
.println("conversion time:" + mili
+ " ms");
543 printMessageCounters();
546 System
.out
.println("file does not exist");
550 public static void main(String
[] args
) {
551 new mjc2wsl().run(args
);
Svarog.pmf.uns.ac.rs/gitweb
maintanance
Doni Pracner