X-Git-Url: http://svarog.pmf.uns.ac.rs/gitweb/?p=mjc2wsl.git;a=blobdiff_plain;f=src%2Fcom%2Fquemaster%2Ftransformations%2Fmjc2wsl%2FMicroJavaInput.java;fp=src%2Fcom%2Fquemaster%2Ftransformations%2Fmjc2wsl%2FMicroJavaInput.java;h=4f5e45accea689d7ddd85ab8a1bda57965e8d0a7;hp=0000000000000000000000000000000000000000;hb=215cc953e6fd7896f0876c66d0de1428d8d1ba7b;hpb=c7a64b0d1297957012f49406eb5d918f7591244a diff --git a/src/com/quemaster/transformations/mjc2wsl/MicroJavaInput.java b/src/com/quemaster/transformations/mjc2wsl/MicroJavaInput.java new file mode 100644 index 0000000..4f5e45a --- /dev/null +++ b/src/com/quemaster/transformations/mjc2wsl/MicroJavaInput.java @@ -0,0 +1,151 @@ +package com.quemaster.transformations.mjc2wsl; + +/* + Copyright (C) 2014 Doni Pracner + + This file is part of mjc2wsl. + + mjc2wsl is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + mjc2wsl is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with mjc2wsl. If not, see . + */ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.HashMap; + +public class MicroJavaInput { + private HashMap opMap; + public String opCodeFile = "mj-bytecodes.properties"; + private InputStream mainIn; + int counter = -1; + + int mainAdr; + + int numberOfWords; + private int codesize; + + public MicroJavaInput() { + + } + + public int get() { + int res = -1; + try { + res = mainIn.read(); + if (res >= 0) + res = res << 24 >>> 24; + } catch (IOException ex) { + ex.printStackTrace(); + } + counter++; + return res; + } + + public int get2() { + return (get() * 256 + get()) << 16 >> 16; + } + + public int get4() { + return (get2() << 16) + (get2() << 16 >>> 16); + } + + public void processHeader(mjc2wsl mjc2wsl) throws Exception { + byte m = (byte) get(); + byte j = (byte) get(); + if (m != 'M' || j != 'J') + throw new Exception("Wrong start of bytecode file"); + codesize = get4(); + setNumberOfWords(get4()); + setMainAdr(get4()); + } + + public void setStream(InputStream ins) { + mainIn = ins; + } + + public int getCounter() { + return counter; + } + + public int getCodesize() { + return codesize; + } + + public int getMainAdr(mjc2wsl mjc2wsl) { + return mainAdr; + } + + public int getNumberOfWords(mjc2wsl mjc2wsl) { + return numberOfWords; + } + + void setNumberOfWords(int numberOfWords) { + this.numberOfWords = numberOfWords; + } + + void setMainAdr(int mainAdr) { + this.mainAdr = mainAdr; + } + + String getRelationFor(int opcode) throws Exception { + switch (opcode) { + case mjc2wsl.jeq: + return "="; + case mjc2wsl.jne: + return "<>"; + case mjc2wsl.jlt: + return "<"; + case mjc2wsl.jle: + return "<="; + case mjc2wsl.jgt: + return ">"; + case mjc2wsl.jge: + return ">="; + } + throw new Exception("Wrong opcode for a relation"); + } + + boolean isJumpCode(int opcode) { + return (opcode >= mjc2wsl.jmp) && (opcode <= mjc2wsl.jge); + } + + private HashMap getOpMap() { + if (opMap == null) { + opMap = new HashMap(60, 0.98f); + try { + BufferedReader in = new BufferedReader(new InputStreamReader( + getClass().getResourceAsStream(opCodeFile))); + String str = in.readLine(); + while (str != null) { + String[] ss = str.split("="); + opMap.put(Integer.parseInt(ss[0]), ss[1]); + str = in.readLine(); + } + in.close(); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + return opMap; + } + + public String getOpString(int op) { + return getOpMap().get(op); + } + + public String describeOpCode(int op) { + return op + " (" + getOpString(op) + ")"; + } + +} \ No newline at end of file