/*
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */

package compiler.jvmci.CompilerToVM;

import jdk.internal.jvmci.hotspot.*;
import jdk.internal.jvmci.hotspot.CompilerToVMHelper;
import jdk.test.lib.Asserts;

import java.lang.reflect.Executable;
import java.util.HashMap;
import java.util.Map;

/**
 * @test
 * @bug 8134994
 * @library /testlibrary /../../test/lib /
 * @compile -g GetLocalVariableTableTest.java
 * @build jdk.internal.jvmci.hotspot.CompilerToVMHelper GetLocalVariableTableTest
 * @run main ClassFileInstaller jdk.internal.jvmci.hotspot.CompilerToVMHelper
 * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+EnableJVMCI
 *      -Xbootclasspath/a:.
 *      compiler.jvmci.CompilerToVM.GetLocalVariableTableTest
 */

public class GetLocalVariableTableTest {

    public static final int MAIN_LOCALS_COUNT = 2;
    public static final int INSTANCE_LOCALS_COUNT = 5;
    public static final int EMPTY_LOCALS_COUNT = 0;
    public static final int ABSTRACT_INHERIT_LOCALS_COUNT = 2;
    public static final int DEFAULTFUNC_LOCALS_COUNT = 4;

    public static void main(String[] args) {
        Map<Executable, Integer> testCases = createTestCases();
        testCases.forEach(GetLocalVariableTableTest::runSanityTest);
    }

    private static Map<Executable, Integer> createTestCases() {
        HashMap<Executable, Integer> methods = new HashMap<>();
        try {
            Class<?> aClass;

            aClass = GetLocalVariableTableTest.class;
            methods.put(aClass.getMethod("main", String[].class),
                    MAIN_LOCALS_COUNT);

            aClass = GetLocalVariableTableTest.DummyClass.class;
            methods.put(aClass.getMethod("dummyInstanceFunction"),
                    INSTANCE_LOCALS_COUNT);
            methods.put(aClass.getMethod("dummyEmptyInstanceFunction"),
                    EMPTY_LOCALS_COUNT);
            methods.put(aClass.getMethod("dummyEmptyStaticFunction"),
                    EMPTY_LOCALS_COUNT);
            methods.put(aClass.getMethod("dummyFunction"),
                    EMPTY_LOCALS_COUNT);
            methods.put(aClass.getMethod("dummyAbstractFunction"),
                    ABSTRACT_INHERIT_LOCALS_COUNT);

            aClass = GetLocalVariableTableTest.DummyInterface.class;
            methods.put(aClass.getMethod("dummyFunction"), EMPTY_LOCALS_COUNT);
            methods.put(aClass.getMethod("dummyDefaultFunction", int.class,
                    int.class), DEFAULTFUNC_LOCALS_COUNT);

            aClass = GetLocalVariableTableTest.DummyAbstractClass.class;
            methods.put(aClass.getMethod("dummyAbstractFunction"), 0);
        } catch (NoSuchMethodException e) {
            throw new Error("TEST BUG", e);
        }
        return methods;
    }

    private static void runSanityTest(Executable aMethod,
                                      Integer expectedTableLength) {
        String name = getFormattedMethodName(aMethod);
        HotSpotResolvedJavaMethodImpl method = ResolvedMethodGetter
                .getResolvedMethod(aMethod);
        long tblStart = CompilerToVMHelper.getLocalVariableTableStart(method);
        Asserts.assertNE(tblStart, 0L, name + " local variable table starts "
                + "at 0.");

        int tblLength = CompilerToVMHelper.getLocalVariableTableLength(method);
        Asserts.assertEQ(tblLength, expectedTableLength, name + " incorrect "
                + "local variable table length.");
    }

    private static String getFormattedMethodName(Executable aMethod) {
        return String.format("'%s::%s'", aMethod.getDeclaringClass().getName(),
                aMethod.getName());
    }

    private interface DummyInterface {
        void dummyFunction();
        default int dummyDefaultFunction(int x, int y) {
            int z = x * y;
            return (int)(Math.cos(x - y + z) * 100);
        }
    }

    private static abstract class DummyAbstractClass implements DummyInterface {
        public abstract int dummyAbstractFunction();
    }

    private static class DummyClass extends DummyAbstractClass {
        int p1 = 5;
        int p2 = 6;
        public int dummyInstanceFunction() {
            String str1 = "123123123";
            double x = 3.14;
            int y = Integer.parseInt(str1);

            return y / (int) x;
        }
        public int dummyEmptyInstanceFunction() {
            return 42;
        }
        public static int dummyEmptyStaticFunction() {
            return -42;
        }

        @Override
        public int dummyAbstractFunction() {
            int z = p1 * p2;
            return (int)(Math.cos(p2 - p1 + z) * 100);
        }

        @Override
        public void dummyFunction() {
            ;
        }
    }
}
