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