gitweb on Svarog

projekti pod git sistemom za održavanje verzija -- projects under the git version control system
mjc2wsl, default to PushPop, change some of the information output
[mjc2wsl.git] / src / com / quemaster / transformations / mjc2wsl / MicroJavaInput.java
1 package com.quemaster.transformations.mjc2wsl;
3 /*
4 Copyright (C) 2014,2015,2018 Doni Pracner
6 This file is part of mjc2wsl.
8 mjc2wsl is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 mjc2wsl is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with mjc2wsl. If not, see <http://www.gnu.org/licenses/>.
20 */
21 import java.io.BufferedReader;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
25 import java.util.HashMap;
27 public class MicroJavaInput {
28 private HashMap<Integer, String> opMap;
29 public String opCodeFile = "mj-bytecodes.properties";
30 private InputStream mainIn;
31 int counter = -1;
33 int mainAdr;
35 int numberOfWords;
36 private int codesize;
38 public MicroJavaInput(InputStream input) throws Exception {
39 mainIn = input;
40 processHeader();
41 }
43 public int get() {
44 int res = -1;
45 try {
46 res = mainIn.read();
47 if (res >= 0)
48 res = res << 24 >>> 24;
49 } catch (IOException ex) {
50 ex.printStackTrace();
51 }
52 counter++;
53 return res;
54 }
56 public int get2() {
57 return (get() * 256 + get()) << 16 >> 16;
58 }
60 public int get4() {
61 return (get2() << 16) + (get2() << 16 >>> 16);
62 }
64 public void processHeader() throws Exception {
65 byte m = (byte) get();
66 byte j = (byte) get();
67 if (m != 'M' || j != 'J')
68 throw new Exception("Wrong start of bytecode file");
69 codesize = get4();
70 this.numberOfWords = get4();
71 this.mainAdr = get4();
72 }
75 public int getCounter() {
76 return counter;
77 }
79 public int getCodesize() {
80 return codesize;
81 }
83 public int getMainAdr() {
84 return mainAdr;
85 }
87 public int getNumberOfWords() {
88 return numberOfWords;
89 }
91 String getRelationFor(int opcode) throws Exception {
92 switch (opcode) {
93 case Mjc2wsl.jeq:
94 return "=";
95 case Mjc2wsl.jne:
96 return "<>";
97 case Mjc2wsl.jlt:
98 return "<";
99 case Mjc2wsl.jle:
100 return "<=";
101 case Mjc2wsl.jgt:
102 return ">";
103 case Mjc2wsl.jge:
104 return ">=";
106 throw new Exception("Wrong opcode for a relation");
109 boolean isJumpCode(int opcode) {
110 return (opcode >= Mjc2wsl.jmp) && (opcode <= Mjc2wsl.jge);
113 private HashMap<Integer, String> getOpMap() {
114 if (opMap == null) {
115 opMap = new HashMap<Integer, String>(60, 0.98f);
116 try {
117 BufferedReader in = new BufferedReader(new InputStreamReader(
118 getClass().getResourceAsStream(opCodeFile)));
119 String str = in.readLine();
120 while (str != null) {
121 String[] ss = str.split("=");
122 opMap.put(Integer.parseInt(ss[0]), ss[1]);
123 str = in.readLine();
125 in.close();
126 } catch (Exception ex) {
127 ex.printStackTrace();
130 return opMap;
133 public String getOpString(int op) {
134 return getOpMap().get(op);
137 public String describeOpCode(int op) {
138 return op + " (" + getOpString(op) + ")";
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner