001/*
002 * Copyright (c) 2015, 2015, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.compiler.test;
024
025import java.lang.reflect.*;
026import java.util.*;
027
028import jdk.internal.jvmci.meta.*;
029
030import org.junit.*;
031
032import com.oracle.graal.nodes.*;
033import com.oracle.graal.nodes.StructuredGraph.AllowAssumptions;
034import com.oracle.graal.phases.common.*;
035import com.oracle.graal.phases.tiers.*;
036
037public class GraphEncoderTest extends GraalCompilerTest {
038
039    @Test
040    public void test01() {
041        testStringMethods(false);
042    }
043
044    @Test
045    public void test02() {
046        testStringMethods(true);
047    }
048
049    public void testStringMethods(boolean canonicalize) {
050        /* Encode and decode all methods of java.lang.String. */
051        List<StructuredGraph> originalGraphs = new ArrayList<>();
052        for (Method method : String.class.getDeclaredMethods()) {
053            ResolvedJavaMethod javaMethod = getMetaAccess().lookupJavaMethod(method);
054            if (javaMethod.hasBytecodes()) {
055                StructuredGraph originalGraph = parseEager(javaMethod, AllowAssumptions.YES);
056                if (canonicalize) {
057                    PhaseContext context = new PhaseContext(getProviders());
058                    new CanonicalizerPhase().apply(originalGraph, context);
059                }
060                originalGraphs.add(originalGraph);
061            }
062        }
063
064        GraphEncoder encoder = new GraphEncoder(getTarget().arch);
065        for (StructuredGraph originalGraph : originalGraphs) {
066            encoder.prepare(originalGraph);
067        }
068        encoder.finishPrepare();
069        Map<StructuredGraph, Long> startOffsets = new HashMap<>();
070        for (StructuredGraph originalGraph : originalGraphs) {
071            startOffsets.put(originalGraph, encoder.encode(originalGraph));
072        }
073
074        for (StructuredGraph originalGraph : originalGraphs) {
075            EncodedGraph encodedGraph = new EncodedGraph(encoder.getEncoding(), startOffsets.get(originalGraph), encoder.getObjects(), encoder.getNodeClasses(), originalGraph.getAssumptions(),
076                            originalGraph.getInlinedMethods());
077            GraphEncoder.verifyEncoding(originalGraph, encodedGraph, getTarget().arch);
078        }
079    }
080}