2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.bck2brwsr.flow;
20 import java.util.Arrays;
21 import org.apidesign.vm4brwsr.Bck2Brwsr;
23 /** Poweful flow analyzer. Based on internals used by Graal JVM
26 * @author Jaroslav Tulach
28 public final class GraalFlowAnalyzer {
29 private GraalFlowAnalyzer() {
32 public static Bck2Brwsr.Flow.Analyzer getDefault() {
36 private static class Impl implements Bck2Brwsr.Flow.Analyzer {
37 static Impl DEFAULT = new Impl();
43 public boolean analyze(final Bck2Brwsr.Flow result) {
45 // Delete the previous line and fix pom.xml, David.
47 new com.oracle.graal.tag.codegen.BCCodeGenerator(
48 com.oracle.graal.tag.BCTagDriver.tagGraph(result.getMethodByteCode())
51 protected void emitIfStart(int bci, String txt) {
52 System.err.println("if { // " + bci);
53 result.beginIfAt(bci);
58 protected void emitEnd(int bci) {
59 System.err.println("} // " + bci);
64 protected void emitElseHeader(int bci) {
65 System.err.println("else { //" + bci);
66 result.beginElseAt(bci);
70 protected void emitLoopHeader(int bci) {
71 System.err.println("for (;;) { // " + bci);
72 result.beginLoopAt(bci);
76 protected void emitBreak(int bci) {
77 System.err.println("break; // " + bci);
82 protected void emit(String txt) {
87 } catch (Exception e) {
92 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 };
95 result.getMethodByteCode()
97 result.beginLoopAt(4); // for (;;) { // 4
98 result.beginIfAt(6); // if { // 6
99 result.breakAt(9); // ;break; // 9
100 result.endAt(9); // ;} // 9
101 result.beginElseAt(9); // else { //9
102 result.endAt(19); // } // 19
103 result.endAt(19); // } // 19