jaroslav@1812: /** jaroslav@1812: * Back 2 Browser Bytecode Translator jaroslav@1812: * Copyright (C) 2012-2015 Jaroslav Tulach jaroslav@1812: * jaroslav@1812: * This program is free software: you can redistribute it and/or modify jaroslav@1812: * it under the terms of the GNU General Public License as published by jaroslav@1812: * the Free Software Foundation, version 2 of the License. jaroslav@1812: * jaroslav@1812: * This program is distributed in the hope that it will be useful, jaroslav@1812: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@1812: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@1812: * GNU General Public License for more details. jaroslav@1812: * jaroslav@1812: * You should have received a copy of the GNU General Public License jaroslav@1812: * along with this program. Look for COPYING file in the top folder. jaroslav@1812: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@1812: */ jaroslav@1812: package org.apidesign.bck2brwsr.flow; jaroslav@1812: jaroslav@1818: import java.util.Arrays; jaroslav@1812: import org.apidesign.vm4brwsr.Bck2Brwsr; jaroslav@1812: jaroslav@1812: /** Poweful flow analyzer. Based on internals used by Graal JVM jaroslav@1812: * compiler. jaroslav@1812: * jaroslav@1812: * @author Jaroslav Tulach jaroslav@1812: */ jaroslav@1812: public final class GraalFlowAnalyzer { jaroslav@1812: private GraalFlowAnalyzer() { jaroslav@1812: } jaroslav@1812: jaroslav@1812: public static Bck2Brwsr.Flow.Analyzer getDefault() { jaroslav@1812: return Impl.DEFAULT; jaroslav@1812: } jaroslav@1812: jaroslav@1812: private static class Impl implements Bck2Brwsr.Flow.Analyzer { jaroslav@1812: static Impl DEFAULT = new Impl(); jaroslav@1812: jaroslav@1812: private Impl() { jaroslav@1812: } jaroslav@1812: jaroslav@1812: @Override jaroslav@1818: public boolean analyze(final Bck2Brwsr.Flow result) { jaroslav@1818: /* jaroslav@1818: // Delete the previous line and fix pom.xml, David. jaroslav@1818: try { jaroslav@1818: new com.oracle.graal.tag.codegen.BCCodeGenerator( jaroslav@1818: com.oracle.graal.tag.BCTagDriver.tagGraph(result.getMethodByteCode()) jaroslav@1818: ) { jaroslav@1818: @Override jaroslav@1818: protected void emitIfStart(int bci, String txt) { jaroslav@1818: System.err.println("if { // " + bci); jaroslav@1818: result.beginIfAt(bci); jaroslav@1818: } jaroslav@1818: jaroslav@1818: jaroslav@1818: @Override jaroslav@1818: protected void emitEnd(int bci) { jaroslav@1818: System.err.println("} // " + bci); jaroslav@1818: result.endAt(bci); jaroslav@1818: } jaroslav@1818: jaroslav@1818: @Override jaroslav@1818: protected void emitElseHeader(int bci) { jaroslav@1818: System.err.println("else { //" + bci); jaroslav@1818: result.beginElseAt(bci); jaroslav@1818: } jaroslav@1818: jaroslav@1818: @Override jaroslav@1818: protected void emitLoopHeader(int bci) { jaroslav@1818: System.err.println("for (;;) { // " + bci); jaroslav@1818: result.beginLoopAt(bci); jaroslav@1818: } jaroslav@1818: jaroslav@1818: @Override jaroslav@1818: protected void emitBreak(int bci) { jaroslav@1818: System.err.println("break; // " + bci); jaroslav@1818: result.breakAt(bci); jaroslav@1818: } jaroslav@1818: jaroslav@1818: @Override jaroslav@1818: protected void emit(String txt) { jaroslav@1818: } jaroslav@1818: jaroslav@1818: }.GenBCCode(); jaroslav@1818: return true; jaroslav@1818: } catch (Exception e) { jaroslav@1818: e.printStackTrace(); jaroslav@1818: } jaroslav@1818: // */ jaroslav@1818: jaroslav@1818: byte[] simpleLoopTestMock = { 3, 60, 3, 61, 28, 26, -94, 0, 13, 27, 28, 96, 60, -124, 2, 1, -89, -1, -12, 27, 26, 104, -84 }; jaroslav@1818: if (Arrays.equals( jaroslav@1818: simpleLoopTestMock, jaroslav@1818: result.getMethodByteCode() jaroslav@1818: )) { jaroslav@1818: result.beginLoopAt(4); // for (;;) { // 4 jaroslav@1818: result.beginIfAt(6); // if { // 6 jaroslav@1818: result.breakAt(9); // ;break; // 9 jaroslav@1818: result.endAt(9); // ;} // 9 jaroslav@1818: result.beginElseAt(9); // else { //9 jaroslav@1818: result.endAt(19); // } // 19 jaroslav@1818: result.endAt(19); // } // 19 jaroslav@1818: return true; jaroslav@1818: } jaroslav@1818: jaroslav@1812: return false; jaroslav@1812: } jaroslav@1812: } jaroslav@1812: }