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