gitweb on Svarog
projekti pod git sistemom za održavanje verzija -- projects under
the git version control system
6813fbf9c351bf863aaf96d599a1d10ee565286f
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
{
273 //skip start TODO make better
274 for (int i
= 0; i
< 14; i
++)
277 prl(getStandardStart());
278 prl("SKIP;\n ACTIONS A_S_start:\n A_S_start == CALL a14 END");
281 if (originalInComments
)
282 prl(createComment(describeOpCode(op
), C_OC
));
283 prl("a" + counter
+ " == ");
286 prl(cmdToEStack(loc(get())));
293 prl(cmdToEStack(loc(op
- load_0
)));
297 prl(cmdFromEStack(loc(get())));
304 prl(cmdFromEStack(loc(op
- store_0
)));
308 //TODO getstatic, putstatic
309 //TODO getfield, putfield
312 prl(cmdToEStack(get4()));
322 prl(cmdToEStack(op
- const_0
));
328 prl("tempres := tempb + tempa;");
329 prl(cmdToEStack("tempres"));
334 prl("tempres := tempb - tempa;");
335 prl(cmdToEStack("tempres"));
340 prl("tempres := tempb * tempa;");
341 prl(cmdToEStack("tempres"));
346 prl("IF tempa = 0 THEN ERROR(\"division by zero\") FI;");
347 prl("tempres := tempb DIV tempa;");
348 prl(cmdToEStack("tempres"));
353 prl("IF tempa = 0 THEN ERROR(\"division by zero\") FI;");
354 prl("tempres := tempb MOD tempa;");
355 prl(cmdToEStack("tempres"));
359 //TODO neg, shl, shr, inc
361 //TODO aload, asstore, baload, bastore
363 //TODO pop, dup, dup2
366 prl("CALL a" + (counter
+ get2()) + ";");
377 prl("IF tempb "+ getRelationFor(op
)
378 +" tempa THEN CALL a" + (counter
+ get2())
384 prl(cmdToMStack(counter
+2));
385 prl("CALL a" + (counter
+ get2()) + ";");
390 prl("IF EMPTY?(mjvm_mstack) THEN CALL Z ELSE");
391 //else we let things return
392 prl(cmdFromMStack("tempa"));
394 prl("END b"+counter
+" ==");
398 prl(createComment("enter not fully procesed yet"));
399 message("enter not fully procesed yet", M_WAR
);
405 prl(createComment("exit not fully procesed yet"));
406 message("exit not fully procesed yet", M_WAR
);
412 prl("tempa := @String_To_Num(@Read_Line(Standard_Input_Port));");
413 prl(cmdToEStack("tempa"));
420 prl("PRINT(tempb);");
424 // TODO need to make it a char
426 prl("PRINT(tempb);");
430 prl(createComment("unknown op error: " + op
, C_ERR
));
431 message("unknown op error: "+ op
, M_ERR
);
440 prl("CALL a" + counter
+ " END");
442 prl("CALL Z;\nSKIP END\nENDACTIONS;\n");
443 prl(getStandardEnd());
447 public void convertFile(File f
) {
449 convertStream(new FileInputStream(f
));
450 } catch (Exception ex
) {
451 ex
.printStackTrace();
455 public void printHelp() {
456 System
.out
.println("MicroJava bytecode to WSL converter. v " + versionN
457 + ", by Doni Pracner");
458 System
.out
.println("usage:\n\t {options} mjc2wsl filename [outfile]");
459 System
.out
.println("options:\n\t--screen print output to screen");
460 System
.out
.println("\t-o --oc[+-] include original code in comments");
461 System
.out
.println("\t-v verbose, print warning messages");
462 System
.out
.println("\t-q don't print even the error messages");
463 System
.out
.println("\t-d print detailed debug messages");
466 public String
makeDefaultOutName(String inname
){
468 if (inname
.endsWith(".obj"))
469 rez
= rez
.substring(0, rez
.length() - 4);
473 public void run(String
[] args
) {
474 if (args
.length
== 0) {
478 while (i
< args
.length
&& args
[i
].charAt(0) == '-') {
479 if (args
[i
].compareTo("-h") == 0) {
482 } else if (args
[i
].compareTo("-o") == 0
483 || args
[i
].startsWith("--oc")) {
484 if (args
[i
].length() == 2)
485 originalInComments
= true;
486 else if (args
[i
].length() == 5)
487 originalInComments
= args
[i
].charAt(4) == '+';
489 originalInComments
= true;
490 } else if (args
[i
].startsWith("--screen")) {
491 out
= new PrintWriter(System
.out
);
492 } else if (args
[i
].compareTo("-d") == 0) {
493 printLevel
= M_DEB
;//print debug info
494 } else if (args
[i
].compareTo("-v") == 0) {
495 printLevel
= M_WAR
;//print warnings
496 } else if (args
[i
].compareTo("-q") == 0) {
497 printLevel
= M_ERR
+1;//no printing
502 if (i
>= args
.length
) {
503 System
.out
.println("no filename supplied");
506 File f
= new File(args
[i
]);
508 if (i
+ 1 < args
.length
) {
510 out
= new PrintWriter(args
[i
+ 1]);
511 } catch (Exception e
) {
512 System
.err
.println("error in opening out file:");
517 // if not set to screen, or a file, make a default filename
519 out
= new PrintWriter(makeDefaultOutName(args
[i
]));
520 } catch (Exception e
) {
521 System
.err
.println("error in opening out file:");
526 Calendar now
= Calendar
.getInstance();
528 long mili
= Calendar
.getInstance().getTimeInMillis()
529 - now
.getTimeInMillis();
530 System
.out
.println("conversion time:" + mili
+ " ms");
531 printMessageCounters();
534 System
.out
.println("file does not exist");
538 public static void main(String
[] args
) {
539 new mjc2wsl().run(args
);
Svarog.pmf.uns.ac.rs/gitweb
maintanance
Doni Pracner