1/*
2 * Copyright (c) 2014, 2016, 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 * @test ConstantGettersTransitionsTest
26 * @summary Test the correctness of compilation level transitions for constant getters methods
27 * @library /test/lib /
28 * @modules java.base/jdk.internal.misc
29 *          java.management
30 *
31 * @build sun.hotspot.WhiteBox
32 *        compiler.tiered.ConstantGettersTransitionsTest
33 * @run driver ClassFileInstaller sun.hotspot.WhiteBox sun.hotspot.WhiteBox$WhiteBoxPermission
34 * @run main/othervm/timeout=240 -Xmixed -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions
35 *      -XX:+WhiteBoxAPI -XX:+TieredCompilation -XX:-UseCounterDecay
36 *      -XX:CompileCommand=compileonly,compiler.tiered.ConstantGettersTransitionsTest$ConstantGettersTestCase$TrivialMethods::*
37 *      compiler.tiered.TransitionsTestExecutor
38 *      compiler.tiered.ConstantGettersTransitionsTest
39 */
40
41package compiler.tiered;
42
43import compiler.whitebox.CompilerWhiteBoxTest;
44
45import java.lang.reflect.Executable;
46import java.util.concurrent.Callable;
47
48public class ConstantGettersTransitionsTest extends LevelTransitionTest {
49    public static void main(String[] args) {
50        assert (!CompilerWhiteBoxTest.skipOnTieredCompilation(false));
51
52        // run test cases
53        for (TestCase testCase : ConstantGettersTestCase.values()) {
54            new ConstantGettersTransitionsTest(testCase).runTest();
55        }
56    }
57
58    @Override
59    protected boolean isTrivial() {
60        return true;
61    }
62
63    private ConstantGettersTransitionsTest(TestCase testCase) {
64        super(testCase);
65    }
66
67    private static enum ConstantGettersTestCase implements CompilerWhiteBoxTest.TestCase {
68        ICONST_M1,
69        ICONST_0,
70        ICONST_1,
71        ICONST_2,
72        ICONST_3,
73        ICONST_4,
74        ICONST_5,
75        LCONST_0,
76        LCONST_1,
77        FCONST_0,
78        FCONST_1,
79        FCONST_2,
80        DCONST_0,
81        DCONST_1,
82        DCONST_W,
83        BYTE,
84        SHORT,
85        CHAR;
86
87        private final Executable executable;
88        private final Callable<Integer> callable;
89
90        @Override
91        public Executable getExecutable() {
92            return executable;
93        }
94
95        @Override
96        public Callable<Integer> getCallable() {
97            return callable;
98        }
99
100        @Override
101        public boolean isOsr() {
102            return false;
103        }
104
105        private ConstantGettersTestCase() {
106            String name = "make" + this.name();
107            this.executable = LevelTransitionTest.Helper.getMethod(TrivialMethods.class, name);
108            this.callable = LevelTransitionTest.Helper.getCallable(new TrivialMethods(), name);
109        }
110
111        /**
112         * Contains methods that load constants with certain types of bytecodes
113         * See JVMS 2.11.2. Load and Store Instructions
114         * Note that it doesn't have a method for ldc_w instruction
115         */
116        private static class TrivialMethods {
117            public static int makeICONST_M1() {
118                return -1;
119            }
120
121            public static int makeICONST_0() {
122                return 0;
123            }
124
125            public static int makeICONST_1() {
126                return 1;
127            }
128
129            public static int makeICONST_2() {
130                return 2;
131            }
132
133            public static int makeICONST_3() {
134                return 3;
135            }
136
137            public static int makeICONST_4() {
138                return 4;
139            }
140
141            public static int makeICONST_5() {
142                return 5;
143            }
144
145            public static long makeLCONST_0() {
146                return 0L;
147            }
148
149            public static long makeLCONST_1() {
150                return 1L;
151            }
152
153            public static float makeFCONST_0() {
154                return 0F;
155            }
156
157            public static float makeFCONST_1() {
158                return 1F;
159            }
160
161            public static float makeFCONST_2() {
162                return 2F;
163            }
164
165            public static double makeDCONST_0() {
166                return 0D;
167            }
168
169            public static double makeDCONST_1() {
170                return 1D;
171            }
172
173            public static double makeDCONST_W() {
174                // ldc2_w
175                return Double.MAX_VALUE;
176            }
177
178            public static Object makeOBJECT() {
179                // aconst_null
180                return null;
181            }
182
183            public static byte makeBYTE() {
184                // bipush
185                return (byte) 0x7F;
186            }
187
188            public static short makeSHORT() {
189                // sipush
190                return (short) 0x7FFF;
191            }
192
193            public static char makeCHAR() {
194                // ldc
195                return (char) 0xFFFF;
196            }
197
198            public static boolean makeBOOLEAN() {
199                return true;
200            }
201        }
202    }
203}