rev 52889 : 8214023: Update Graal

   1 /*
   2  * Copyright (c) 2015, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 
  25 package org.graalvm.compiler.core.test;
  26 
  27 import java.lang.reflect.Method;
  28 import java.util.ArrayList;
  29 import java.util.HashMap;
  30 import java.util.List;
  31 import java.util.Map;
  32 
  33 import jdk.vm.ci.meta.ResolvedJavaMethod;
  34 
  35 import org.junit.Test;
  36 
  37 import org.graalvm.compiler.nodes.EncodedGraph;
  38 import org.graalvm.compiler.nodes.GraphEncoder;
  39 import org.graalvm.compiler.nodes.StructuredGraph;
  40 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  41 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  42 import org.graalvm.compiler.phases.tiers.PhaseContext;
  43 
  44 public class GraphEncoderTest extends GraalCompilerTest {
  45 
  46     @Test
  47     public void test01() {
  48         testStringMethods(false);
  49     }
  50 
  51     @Test
  52     public void test02() {
  53         testStringMethods(true);
  54     }
  55 
  56     public void testStringMethods(boolean canonicalize) {
  57         /* Encode and decode all methods of java.lang.String. */
  58         List<StructuredGraph> originalGraphs = new ArrayList<>();
  59         for (Method method : String.class.getDeclaredMethods()) {
  60             ResolvedJavaMethod javaMethod = getMetaAccess().lookupJavaMethod(method);
  61             if (javaMethod.hasBytecodes()) {
  62                 StructuredGraph originalGraph = parseEager(javaMethod, AllowAssumptions.YES);
  63                 if (canonicalize) {
  64                     PhaseContext context = new PhaseContext(getProviders());
  65                     new CanonicalizerPhase().apply(originalGraph, context);
  66                 }
  67                 originalGraphs.add(originalGraph);
  68             }
  69         }
  70 
  71         GraphEncoder encoder = new GraphEncoder(getTarget().arch);
  72         for (StructuredGraph originalGraph : originalGraphs) {
  73             encoder.prepare(originalGraph);
  74         }
  75         encoder.finishPrepare();
  76         Map<StructuredGraph, Integer> startOffsets = new HashMap<>();
  77         for (StructuredGraph originalGraph : originalGraphs) {
  78             startOffsets.put(originalGraph, encoder.encode(originalGraph));
  79         }
  80 
  81         for (StructuredGraph originalGraph : originalGraphs) {
  82             EncodedGraph encodedGraph = new EncodedGraph(encoder.getEncoding(), startOffsets.get(originalGraph), encoder.getObjects(), encoder.getNodeClasses(), originalGraph);
  83             GraphEncoder.verifyEncoding(originalGraph, encodedGraph, getTarget().arch);
  84         }
  85     }
  86 }
--- EOF ---