gitweb on Svarog

projekti pod git sistemom za održavanje verzija -- projects under the git version control system
mjc2wsl - nicer version number outputs
[mjc2wsl.git] / src / com / quemaster / transformations / mjc2wsl / MicroJavaInput.java
1 package com.quemaster.transformations.mjc2wsl;
3 /*
4 Copyright (C) 2014 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() {
40 }
42 public int get() {
43 int res = -1;
44 try {
45 res = mainIn.read();
46 if (res >= 0)
47 res = res << 24 >>> 24;
48 } catch (IOException ex) {
49 ex.printStackTrace();
50 }
51 counter++;
52 return res;
53 }
55 public int get2() {
56 return (get() * 256 + get()) << 16 >> 16;
57 }
59 public int get4() {
60 return (get2() << 16) + (get2() << 16 >>> 16);
61 }
63 public void processHeader(mjc2wsl mjc2wsl) throws Exception {
64 byte m = (byte) get();
65 byte j = (byte) get();
66 if (m != 'M' || j != 'J')
67 throw new Exception("Wrong start of bytecode file");
68 codesize = get4();
69 setNumberOfWords(get4());
70 setMainAdr(get4());
71 }
73 public void setStream(InputStream ins) {
74 mainIn = ins;
75 }
77 public int getCounter() {
78 return counter;
79 }
81 public int getCodesize() {
82 return codesize;
83 }
85 public int getMainAdr(mjc2wsl mjc2wsl) {
86 return mainAdr;
87 }
89 public int getNumberOfWords(mjc2wsl mjc2wsl) {
90 return numberOfWords;
91 }
93 void setNumberOfWords(int numberOfWords) {
94 this.numberOfWords = numberOfWords;
95 }
97 void setMainAdr(int mainAdr) {
98 this.mainAdr = mainAdr;
99 }
101 String getRelationFor(int opcode) throws Exception {
102 switch (opcode) {
103 case mjc2wsl.jeq:
104 return "=";
105 case mjc2wsl.jne:
106 return "<>";
107 case mjc2wsl.jlt:
108 return "<";
109 case mjc2wsl.jle:
110 return "<=";
111 case mjc2wsl.jgt:
112 return ">";
113 case mjc2wsl.jge:
114 return ">=";
116 throw new Exception("Wrong opcode for a relation");
119 boolean isJumpCode(int opcode) {
120 return (opcode >= mjc2wsl.jmp) && (opcode <= mjc2wsl.jge);
123 private HashMap<Integer, String> getOpMap() {
124 if (opMap == null) {
125 opMap = new HashMap<Integer, String>(60, 0.98f);
126 try {
127 BufferedReader in = new BufferedReader(new InputStreamReader(
128 getClass().getResourceAsStream(opCodeFile)));
129 String str = in.readLine();
130 while (str != null) {
131 String[] ss = str.split("=");
132 opMap.put(Integer.parseInt(ss[0]), ss[1]);
133 str = in.readLine();
135 in.close();
136 } catch (Exception ex) {
137 ex.printStackTrace();
140 return opMap;
143 public String getOpString(int op) {
144 return getOpMap().get(op);
147 public String describeOpCode(int op) {
148 return op + " (" + getOpString(op) + ")";
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner