rt/flow/src/main/java/org/apidesign/bck2brwsr/flow/GraalFlowAnalyzer.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 13 Mar 2015 11:59:26 +0100
branchflow
changeset 1818 21089a85f02b
parent 1812 4fef6b767f61
child 1841 e38cdcd3c997
permissions -rw-r--r--
First method with flow analyser generated
jaroslav@1812
     1
/**
jaroslav@1812
     2
 * Back 2 Browser Bytecode Translator
jaroslav@1812
     3
 * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@1812
     4
 *
jaroslav@1812
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@1812
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@1812
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@1812
     8
 *
jaroslav@1812
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@1812
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@1812
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@1812
    12
 * GNU General Public License for more details.
jaroslav@1812
    13
 *
jaroslav@1812
    14
 * You should have received a copy of the GNU General Public License
jaroslav@1812
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@1812
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@1812
    17
 */
jaroslav@1812
    18
package org.apidesign.bck2brwsr.flow;
jaroslav@1812
    19
jaroslav@1818
    20
import java.util.Arrays;
jaroslav@1812
    21
import org.apidesign.vm4brwsr.Bck2Brwsr;
jaroslav@1812
    22
jaroslav@1812
    23
/** Poweful flow analyzer. Based on internals used by Graal JVM
jaroslav@1812
    24
 * compiler.
jaroslav@1812
    25
 *
jaroslav@1812
    26
 * @author Jaroslav Tulach
jaroslav@1812
    27
 */
jaroslav@1812
    28
public final class GraalFlowAnalyzer {
jaroslav@1812
    29
    private GraalFlowAnalyzer() {
jaroslav@1812
    30
    }
jaroslav@1812
    31
    
jaroslav@1812
    32
    public static Bck2Brwsr.Flow.Analyzer getDefault() {
jaroslav@1812
    33
        return Impl.DEFAULT;
jaroslav@1812
    34
    }
jaroslav@1812
    35
jaroslav@1812
    36
    private static class Impl implements Bck2Brwsr.Flow.Analyzer {
jaroslav@1812
    37
        static Impl DEFAULT = new Impl();
jaroslav@1812
    38
        
jaroslav@1812
    39
        private Impl() {
jaroslav@1812
    40
        }
jaroslav@1812
    41
jaroslav@1812
    42
        @Override
jaroslav@1818
    43
        public boolean analyze(final Bck2Brwsr.Flow result) {
jaroslav@1818
    44
            /*
jaroslav@1818
    45
            // Delete the previous line and fix pom.xml, David.
jaroslav@1818
    46
            try {
jaroslav@1818
    47
                new com.oracle.graal.tag.codegen.BCCodeGenerator(
jaroslav@1818
    48
                    com.oracle.graal.tag.BCTagDriver.tagGraph(result.getMethodByteCode())
jaroslav@1818
    49
                ) {
jaroslav@1818
    50
                    @Override
jaroslav@1818
    51
                    protected void emitIfStart(int bci, String txt) {
jaroslav@1818
    52
                        System.err.println("if { // " + bci);
jaroslav@1818
    53
                        result.beginIfAt(bci);
jaroslav@1818
    54
                    }
jaroslav@1818
    55
                    
jaroslav@1818
    56
                    
jaroslav@1818
    57
                    @Override
jaroslav@1818
    58
                    protected void emitEnd(int bci) {
jaroslav@1818
    59
                        System.err.println("} // " + bci);
jaroslav@1818
    60
                        result.endAt(bci);
jaroslav@1818
    61
                    }
jaroslav@1818
    62
jaroslav@1818
    63
                    @Override
jaroslav@1818
    64
                    protected void emitElseHeader(int bci) {
jaroslav@1818
    65
                        System.err.println("else { //" + bci);
jaroslav@1818
    66
                        result.beginElseAt(bci);
jaroslav@1818
    67
                    }
jaroslav@1818
    68
jaroslav@1818
    69
                    @Override
jaroslav@1818
    70
                    protected void emitLoopHeader(int bci) {
jaroslav@1818
    71
                        System.err.println("for (;;) { // " + bci);
jaroslav@1818
    72
                        result.beginLoopAt(bci);
jaroslav@1818
    73
                    }
jaroslav@1818
    74
jaroslav@1818
    75
                    @Override
jaroslav@1818
    76
                    protected void emitBreak(int bci) {
jaroslav@1818
    77
                        System.err.println("break; // " + bci);
jaroslav@1818
    78
                        result.breakAt(bci);
jaroslav@1818
    79
                    }
jaroslav@1818
    80
jaroslav@1818
    81
                    @Override
jaroslav@1818
    82
                    protected void emit(String txt) {
jaroslav@1818
    83
                    }
jaroslav@1818
    84
                    
jaroslav@1818
    85
                }.GenBCCode();
jaroslav@1818
    86
                return true;
jaroslav@1818
    87
            } catch (Exception e) {
jaroslav@1818
    88
                e.printStackTrace();
jaroslav@1818
    89
            }
jaroslav@1818
    90
            // */
jaroslav@1818
    91
            
jaroslav@1818
    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 };
jaroslav@1818
    93
            if (Arrays.equals(
jaroslav@1818
    94
                simpleLoopTestMock,
jaroslav@1818
    95
                result.getMethodByteCode()
jaroslav@1818
    96
            )) {
jaroslav@1818
    97
                result.beginLoopAt(4); // for (;;) { // 4
jaroslav@1818
    98
                result.beginIfAt(6); // if { // 6
jaroslav@1818
    99
                result.breakAt(9); // ;break; // 9
jaroslav@1818
   100
                result.endAt(9); // ;} // 9
jaroslav@1818
   101
                result.beginElseAt(9); // else { //9
jaroslav@1818
   102
                result.endAt(19); // } // 19
jaroslav@1818
   103
                result.endAt(19); // } // 19
jaroslav@1818
   104
                return true;
jaroslav@1818
   105
            }
jaroslav@1818
   106
            
jaroslav@1812
   107
            return false;
jaroslav@1812
   108
        }
jaroslav@1812
   109
    }
jaroslav@1812
   110
}