1 /* 2 * Copyright (c) 2015, 2018, 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 org.graalvm.compiler.nodes.EncodedGraph; 34 import org.graalvm.compiler.nodes.GraphEncoder; 35 import org.graalvm.compiler.nodes.StructuredGraph; 36 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions; 37 import org.graalvm.compiler.phases.common.CanonicalizerPhase; 38 import org.graalvm.compiler.phases.tiers.PhaseContext; 39 import org.junit.Test; 40 41 import jdk.vm.ci.meta.ResolvedJavaMethod; 42 43 public class GraphEncoderTest extends GraalCompilerTest { 44 45 @Test 46 public void test01() { 47 testStringMethods(false); 48 } 49 50 @Test 51 public void test02() { 52 testStringMethods(true); 53 } 54 55 public void testStringMethods(boolean canonicalize) { 56 /* Encode and decode all methods of java.lang.String. */ 57 List<StructuredGraph> originalGraphs = new ArrayList<>(); 58 for (Method method : String.class.getDeclaredMethods()) { 59 ResolvedJavaMethod javaMethod = getMetaAccess().lookupJavaMethod(method); 60 if (javaMethod.hasBytecodes()) { 61 StructuredGraph originalGraph = parseEager(javaMethod, AllowAssumptions.YES); 62 if (canonicalize) { 63 PhaseContext context = new PhaseContext(getProviders()); 64 new CanonicalizerPhase().apply(originalGraph, context); 65 } 66 originalGraphs.add(originalGraph); 67 } 68 } 69 70 GraphEncoder encoder = new GraphEncoder(getTarget().arch); 71 for (StructuredGraph originalGraph : originalGraphs) { 72 encoder.prepare(originalGraph); 73 } 74 encoder.finishPrepare(); 75 Map<StructuredGraph, Integer> startOffsets = new HashMap<>(); 76 for (StructuredGraph originalGraph : originalGraphs) { 77 startOffsets.put(originalGraph, encoder.encode(originalGraph)); 78 } 79 80 for (StructuredGraph originalGraph : originalGraphs) { 81 EncodedGraph encodedGraph = new EncodedGraph(encoder.getEncoding(), startOffsets.get(originalGraph), encoder.getObjects(), encoder.getNodeClasses(), originalGraph); 82 encoder.verifyEncoding(originalGraph, encodedGraph); 83 } 84 } 85 }