gitweb on Svarog
projekti pod git sistemom za održavanje verzija -- projects under
the git version control system
65b6348cf19b08e6e239f10510daf14464b3a69a
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(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 private boolean isJumpCode(int opcode
) {
275 return (opcode
>=jmp
) && (opcode
<=jge
);
278 public void convertStream(InputStream ins
) throws Exception
{
281 byte m
= (byte) get();
282 byte j
= (byte) get();
283 if (m
!='M' || j
!='J')
284 throw new Exception("Wrong start of bytecode file");
285 int codesize
= get4();
286 int numberOfWords
= get4();
287 int mainAdr
= get4();
289 prl(getStandardStart());
290 prl("SKIP;\n ACTIONS A_S_start:\n A_S_start == CALL a"+(14+mainAdr
)+" END");
293 if (originalInComments
)
294 prl(createComment(describeOpCode(op
), C_OC
));
295 prl("a" + counter
+ " == ");
296 if (addPrintForEachAddress
) {
297 prl("PRINT(\"a"+counter
+"\");");
298 if (addPauseAfterEachAddress
)
299 prl("debug_disposable_string := @Read_Line(Standard_Input_Port);");
303 prl(cmdToEStack(loc(get())));
310 prl(cmdToEStack(loc(op
- load_0
)));
314 prl(cmdFromEStack(loc(get())));
321 prl(cmdFromEStack(loc(op
- store_0
)));
325 //TODO getstatic, putstatic
326 //TODO getfield, putfield
329 prl(cmdToEStack(get4()));
339 prl(cmdToEStack(op
- const_0
));
345 prl("tempres := tempb + tempa;");
346 prl(cmdToEStack("tempres"));
351 prl("tempres := tempb - tempa;");
352 prl(cmdToEStack("tempres"));
357 prl("tempres := tempb * tempa;");
358 prl(cmdToEStack("tempres"));
363 prl("IF tempa = 0 THEN ERROR(\"division by zero\") FI;");
364 prl("tempres := tempb DIV tempa;");
365 prl(cmdToEStack("tempres"));
370 prl("IF tempa = 0 THEN ERROR(\"division by zero\") FI;");
371 prl("tempres := tempb MOD tempa;");
372 prl(cmdToEStack("tempres"));
376 //TODO neg, shl, shr, inc
378 //TODO aload, asstore, baload, bastore
380 //TODO pop, dup, dup2
383 prl("CALL a" + (counter
+ get2()) + ";");
394 prl("IF tempb "+ getRelationFor(op
)
395 +" tempa THEN CALL a" + (counter
+ get2())
396 +" ELSE CALL a" + (counter
+1)
402 prl(cmdToMStack(counter
+2));
403 prl("CALL a" + (counter
+ get2()) + ";");
408 prl("IF EMPTY?(mjvm_mstack) THEN CALL Z ELSE");
409 //else we let things return
410 prl(cmdFromMStack("tempa"));
412 prl("END b"+counter
+" ==");
416 prl(createComment("enter not fully procesed yet"));
417 message("enter not fully procesed yet", M_WAR
);
418 int parameters
= get();
421 for (int i
= parameters
-1; i
>= 0; i
--)
422 prl(cmdFromEStack(loc(i
)));
426 prl(createComment("exit not fully procesed yet"));
427 message("exit not fully procesed yet", M_WAR
);
433 prl("tempa := @String_To_Num(@Read_Line(Standard_Input_Port));");
434 prl(cmdToEStack("tempa"));
441 prl("PRINT(tempb);");
445 // TODO need to make it a char
447 prl("PRINT(tempb);");
452 prl("ERROR(\"Runtime error: trap("+get()+")\");");
458 prl(createComment("unknown op error: " + op
, C_ERR
));
459 message("unknown op error: "+ op
, M_ERR
);
463 boolean wasJump
= isJumpCode(op
);
469 prl("CALL a" + counter
+ " END");
471 prl("CALL Z;\nSKIP END\nENDACTIONS;\n");
472 prl(getStandardEnd());
476 public void convertFile(File f
) {
478 convertStream(new FileInputStream(f
));
479 } catch (Exception ex
) {
480 ex
.printStackTrace();
484 public void printHelp() {
485 System
.out
.println("MicroJava bytecode to WSL converter. v " + versionN
486 + ", by Doni Pracner");
487 System
.out
.println("usage:\n\t {options} mjc2wsl filename [outfile]");
488 System
.out
.println("options:\n\t--screen print output to screen");
489 System
.out
.println("\t-o --oc[+-] include original code in comments");
490 System
.out
.println("\t-v verbose, print warning messages");
491 System
.out
.println("\t-q don't print even the error messages");
492 System
.out
.println("\t-d print detailed debug messages");
495 public String
makeDefaultOutName(String inname
){
497 if (inname
.endsWith(".obj"))
498 rez
= rez
.substring(0, rez
.length() - 4);
502 public void run(String
[] args
) {
503 if (args
.length
== 0) {
507 while (i
< args
.length
&& args
[i
].charAt(0) == '-') {
508 if (args
[i
].compareTo("-h") == 0) {
511 } else if (args
[i
].compareTo("-o") == 0
512 || args
[i
].startsWith("--oc")) {
513 if (args
[i
].length() == 2)
514 originalInComments
= true;
515 else if (args
[i
].length() == 5)
516 originalInComments
= args
[i
].charAt(4) == '+';
518 originalInComments
= true;
519 } else if (args
[i
].startsWith("--screen")) {
520 out
= new PrintWriter(System
.out
);
521 } else if (args
[i
].compareTo("-d") == 0) {
522 printLevel
= M_DEB
;//print debug info
523 } else if (args
[i
].compareTo("-v") == 0) {
524 printLevel
= M_WAR
;//print warnings
525 } else if (args
[i
].compareTo("-q") == 0) {
526 printLevel
= M_ERR
+1;//no printing
531 if (i
>= args
.length
) {
532 System
.out
.println("no filename supplied");
535 File f
= new File(args
[i
]);
537 if (i
+ 1 < args
.length
) {
539 out
= new PrintWriter(args
[i
+ 1]);
540 } catch (Exception e
) {
541 System
.err
.println("error in opening out file:");
546 // if not set to screen, or a file, make a default filename
548 out
= new PrintWriter(makeDefaultOutName(args
[i
]));
549 } catch (Exception e
) {
550 System
.err
.println("error in opening out file:");
555 Calendar now
= Calendar
.getInstance();
557 long mili
= Calendar
.getInstance().getTimeInMillis()
558 - now
.getTimeInMillis();
559 System
.out
.println("conversion time:" + mili
+ " ms");
560 printMessageCounters();
563 System
.out
.println("file does not exist");
567 public static void main(String
[] args
) {
568 new mjc2wsl().run(args
);
Svarog.pmf.uns.ac.rs/gitweb
maintanance
Doni Pracner