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