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
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012-2015 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     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.
     8  *
     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.
    13  *
    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.
    17  */
    18 package org.apidesign.bck2brwsr.flow;
    19 
    20 import java.util.Arrays;
    21 import org.apidesign.vm4brwsr.Bck2Brwsr;
    22 
    23 /** Poweful flow analyzer. Based on internals used by Graal JVM
    24  * compiler.
    25  *
    26  * @author Jaroslav Tulach
    27  */
    28 public final class GraalFlowAnalyzer {
    29     private GraalFlowAnalyzer() {
    30     }
    31     
    32     public static Bck2Brwsr.Flow.Analyzer getDefault() {
    33         return Impl.DEFAULT;
    34     }
    35 
    36     private static class Impl implements Bck2Brwsr.Flow.Analyzer {
    37         static Impl DEFAULT = new Impl();
    38         
    39         private Impl() {
    40         }
    41 
    42         @Override
    43         public boolean analyze(final Bck2Brwsr.Flow result) {
    44             /*
    45             // Delete the previous line and fix pom.xml, David.
    46             try {
    47                 new com.oracle.graal.tag.codegen.BCCodeGenerator(
    48                     com.oracle.graal.tag.BCTagDriver.tagGraph(result.getMethodByteCode())
    49                 ) {
    50                     @Override
    51                     protected void emitIfStart(int bci, String txt) {
    52                         System.err.println("if { // " + bci);
    53                         result.beginIfAt(bci);
    54                     }
    55                     
    56                     
    57                     @Override
    58                     protected void emitEnd(int bci) {
    59                         System.err.println("} // " + bci);
    60                         result.endAt(bci);
    61                     }
    62 
    63                     @Override
    64                     protected void emitElseHeader(int bci) {
    65                         System.err.println("else { //" + bci);
    66                         result.beginElseAt(bci);
    67                     }
    68 
    69                     @Override
    70                     protected void emitLoopHeader(int bci) {
    71                         System.err.println("for (;;) { // " + bci);
    72                         result.beginLoopAt(bci);
    73                     }
    74 
    75                     @Override
    76                     protected void emitBreak(int bci) {
    77                         System.err.println("break; // " + bci);
    78                         result.breakAt(bci);
    79                     }
    80 
    81                     @Override
    82                     protected void emit(String txt) {
    83                     }
    84                     
    85                 }.GenBCCode();
    86                 return true;
    87             } catch (Exception e) {
    88                 e.printStackTrace();
    89             }
    90             // */
    91             
    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 };
    93             if (Arrays.equals(
    94                 simpleLoopTestMock,
    95                 result.getMethodByteCode()
    96             )) {
    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
   104                 return true;
   105             }
   106             
   107             return false;
   108         }
   109     }
   110 }