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