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