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 private boolean addPauseAfterEachAddress
=false,
36 addPrintForEachAddress
= false;
38 /** Constant used for marking a regular comment from the original file */
39 public static final char C_REG
= ' ';
41 * Constant used for marking when original code is inserted in the file,
42 * next to the translations
44 public static final char C_OC
= '#';
45 /** Constant used for marking special messages from the translator */
46 public static final char C_SPEC
= '&';
47 /** Constant used for marking error messages from the translator */
48 public static final char C_ERR
= '!';
50 /** instruction code in MicroJava bytecode. */
51 public static final int
110 public String
getStandardStart(){
111 StringBuilder ret
= new StringBuilder(
112 "C:\" This file automatically converted from microjava bytecode\";\n"
113 +"C:\" with mjc2wsl v "+versionN
+"\";\n");
115 ret
.append("VAR < tempa := 0, tempb := 0, tempres :=0,\n\t");
116 for (int i
= 0; i
<= 3; i
++)
117 ret
.append("mjvm_loc" + i
+ " := 0, ");
118 ret
.append("\n mjvm_estack := < >, mjvm_mstack := < >, ");
119 ret
.append("\n mjvm_fp := 0, mjvm_sp := 0,");
120 ret
.append("\n t_e_m_p := 0 > :");
122 return ret
.toString();
125 public String
getStandardEnd(){
126 return "SKIP\nENDVAR";
129 private boolean originalInComments
= false;
131 private HashMap
<Integer
,String
> opMap
= null;
133 private String opCodeFile
= "mj-bytecodes.properties";
135 private HashMap
<Integer
,String
> getOpMap() {
137 opMap
= new HashMap
<Integer
, String
> (60, 0.98f
);
139 BufferedReader in
= new BufferedReader(
140 new InputStreamReader(getClass().getResourceAsStream(opCodeFile
)));
141 String str
= in
.readLine();
142 while (str
!= null) {
143 String
[] ss
= str
.split("=");
144 opMap
.put(Integer
.parseInt(ss
[0]),ss
[1]);
148 }catch (Exception ex
) {
149 ex
.printStackTrace();
155 public String
getOpString(int op
) {
156 return getOpMap().get(op
);
159 public String
describeOpCode(int op
) {
160 return op
+ " (" + getOpString(op
) + ")";
163 private InputStream mainIn
;
164 private PrintWriter out
= null;
165 private int counter
= -1;
167 private void pr(int i
){
171 private void pr(char i
){
175 private void pr(String i
){
179 private void prl(String i
){
188 res
= res
<< 24 >>> 24;
189 } catch (IOException ex
) {
190 ex
.printStackTrace();
197 return (get() * 256 + get()) << 16 >> 16;
201 return (get2() << 16) + (get2() << 16 >>> 16);
204 private String
loc(int i
){
205 return "mjvm_loc" + i
;
209 * Creates a WSL comment with care to quote chars.
211 public static String
createComment(String str
){
212 return createComment(str
, C_REG
);
216 * Creates a WSL comment with care to quote chars, of the
217 * given type. Types are given as char constants. They can be
218 * default comments, comments that contain the original code
219 * in them, or additional comments regarding the translation
222 public static String
createComment(String str
, char type
) {
223 return "C:\"" + type
+ str
.replace("\"", "''") + "\";";
228 private String
cmdToEStack(int i
) {
229 return "mjvm_estack := <" + i
+ " > ++ mjvm_estack;";
232 private String
cmdToEStack(String i
) {
233 return "mjvm_estack := <" + i
+ " > ++ mjvm_estack;";
236 private String
cmdFromEStack(String st
) {
237 return st
+ " := HEAD(mjvm_estack); mjvm_estack := TAIL(mjvm_estack);";
240 private String
getTopTwo(){
241 return cmdFromEStack("tempa") + "\n" + cmdFromEStack("tempb");
244 private String
getTop() {
245 return cmdFromEStack("tempa");
250 private String
cmdToMStack(int i
) {
251 return "mjvm_mstack := <" + i
+ " > ++ mjvm_mstack;";
254 private String
cmdToMStack(String i
) {
255 return "mjvm_mstack := <" + i
+ " > ++ mjvm_mstack;";
258 private String
cmdFromMStack(String st
) {
259 return st
+ " := HEAD(mjvm_mstack); mjvm_mstack := TAIL(mjvm_mstack);";
262 private String
getRelationFor(int opcode
) throws Exception
{
264 case jeq
: return "=";
265 case jne
: return "<>";
266 case jlt
: return "<";
267 case jle
: return "<=";
268 case jgt
: return ">";
269 case jge
: return ">=";
271 throw new Exception("Wrong opcode for a relation");
274 public void convertStream(InputStream ins
) throws Exception
{
277 byte m
= (byte) get();
278 byte j
= (byte) get();
279 if (m
!='M' || j
!='J')
280 throw new Exception("Wrong start of bytecode file");
281 int codesize
= get4();
282 int numberOfWords
= get4();
283 int mainAdr
= get4();
285 prl(getStandardStart());
286 prl("SKIP;\n ACTIONS A_S_start:\n A_S_start == CALL a"+(14+mainAdr
)+" END");
289 if (originalInComments
)
290 prl(createComment(describeOpCode(op
), C_OC
));
291 prl("a" + counter
+ " == ");
292 if (addPrintForEachAddress
) {
293 prl("PRINT(\"a"+counter
+"\");");
294 if (addPauseAfterEachAddress
)
295 prl("debug_disposable_string := @Read_Line(Standard_Input_Port);");
299 prl(cmdToEStack(loc(get())));
306 prl(cmdToEStack(loc(op
- load_0
)));
310 prl(cmdFromEStack(loc(get())));
317 prl(cmdFromEStack(loc(op
- store_0
)));
321 //TODO getstatic, putstatic
322 //TODO getfield, putfield
325 prl(cmdToEStack(get4()));
335 prl(cmdToEStack(op
- const_0
));
341 prl("tempres := tempb + tempa;");
342 prl(cmdToEStack("tempres"));
347 prl("tempres := tempb - tempa;");
348 prl(cmdToEStack("tempres"));
353 prl("tempres := tempb * tempa;");
354 prl(cmdToEStack("tempres"));
359 prl("IF tempa = 0 THEN ERROR(\"division by zero\") FI;");
360 prl("tempres := tempb DIV tempa;");
361 prl(cmdToEStack("tempres"));
366 prl("IF tempa = 0 THEN ERROR(\"division by zero\") FI;");
367 prl("tempres := tempb MOD tempa;");
368 prl(cmdToEStack("tempres"));
372 //TODO neg, shl, shr, inc
374 //TODO aload, asstore, baload, bastore
376 //TODO pop, dup, dup2
379 prl("CALL a" + (counter
+ get2()) + ";");
390 prl("IF tempb "+ getRelationFor(op
)
391 +" tempa THEN CALL a" + (counter
+ get2())
397 prl(cmdToMStack(counter
+2));
398 prl("CALL a" + (counter
+ get2()) + ";");
403 prl("IF EMPTY?(mjvm_mstack) THEN CALL Z ELSE");
404 //else we let things return
405 prl(cmdFromMStack("tempa"));
407 prl("END b"+counter
+" ==");
411 prl(createComment("enter not fully procesed yet"));
412 message("enter not fully procesed yet", M_WAR
);
413 int parameters
= get();
416 for (int i
= parameters
-1; i
>= 0; i
--)
417 prl(cmdFromEStack("mjvm_loc" + i
));
421 prl(createComment("exit not fully procesed yet"));
422 message("exit not fully procesed yet", M_WAR
);
428 prl("tempa := @String_To_Num(@Read_Line(Standard_Input_Port));");
429 prl(cmdToEStack("tempa"));
436 prl("PRINT(tempb);");
440 // TODO need to make it a char
442 prl("PRINT(tempb);");
448 prl(createComment("trap not fully procesed yet"));
449 message("trap not fully procesed yet", M_WAR
);
456 prl(createComment("unknown op error: " + op
, C_ERR
));
457 message("unknown op error: "+ op
, M_ERR
);
463 prl("CALL a" + counter
+ " END");
465 prl("CALL Z;\nSKIP END\nENDACTIONS;\n");
466 prl(getStandardEnd());
470 public void convertFile(File f
) {
472 convertStream(new FileInputStream(f
));
473 } catch (Exception ex
) {
474 ex
.printStackTrace();
478 public void printHelp() {
479 System
.out
.println("MicroJava bytecode to WSL converter. v " + versionN
480 + ", by Doni Pracner");
481 System
.out
.println("usage:\n\t {options} mjc2wsl filename [outfile]");
482 System
.out
.println("options:\n\t--screen print output to screen");
483 System
.out
.println("\t-o --oc[+-] include original code in comments");
484 System
.out
.println("\t-v verbose, print warning messages");
485 System
.out
.println("\t-q don't print even the error messages");
486 System
.out
.println("\t-d print detailed debug messages");
489 public String
makeDefaultOutName(String inname
){
491 if (inname
.endsWith(".obj"))
492 rez
= rez
.substring(0, rez
.length() - 4);
496 public void run(String
[] args
) {
497 if (args
.length
== 0) {
501 while (i
< args
.length
&& args
[i
].charAt(0) == '-') {
502 if (args
[i
].compareTo("-h") == 0) {
505 } else if (args
[i
].compareTo("-o") == 0
506 || args
[i
].startsWith("--oc")) {
507 if (args
[i
].length() == 2)
508 originalInComments
= true;
509 else if (args
[i
].length() == 5)
510 originalInComments
= args
[i
].charAt(4) == '+';
512 originalInComments
= true;
513 } else if (args
[i
].startsWith("--screen")) {
514 out
= new PrintWriter(System
.out
);
515 } else if (args
[i
].compareTo("-d") == 0) {
516 printLevel
= M_DEB
;//print debug info
517 } else if (args
[i
].compareTo("-v") == 0) {
518 printLevel
= M_WAR
;//print warnings
519 } else if (args
[i
].compareTo("-q") == 0) {
520 printLevel
= M_ERR
+1;//no printing
525 if (i
>= args
.length
) {
526 System
.out
.println("no filename supplied");
529 File f
= new File(args
[i
]);
531 if (i
+ 1 < args
.length
) {
533 out
= new PrintWriter(args
[i
+ 1]);
534 } catch (Exception e
) {
535 System
.err
.println("error in opening out file:");
540 // if not set to screen, or a file, make a default filename
542 out
= new PrintWriter(makeDefaultOutName(args
[i
]));
543 } catch (Exception e
) {
544 System
.err
.println("error in opening out file:");
549 Calendar now
= Calendar
.getInstance();
551 long mili
= Calendar
.getInstance().getTimeInMillis()
552 - now
.getTimeInMillis();
553 System
.out
.println("conversion time:" + mili
+ " ms");
554 printMessageCounters();
557 System
.out
.println("file does not exist");
561 public static void main(String
[] args
) {
562 new mjc2wsl().run(args
);
Svarog.pmf.uns.ac.rs/gitweb
maintanance
Doni Pracner