Path: blob/master/test/hotspot/jtreg/compiler/stable/TestStableByte.java
41149 views
/*1* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test TestStableByte25* @summary tests on stable fields and arrays26* @library /test/lib /27* @modules java.base/jdk.internal.misc28* @modules java.base/jdk.internal.vm.annotation29* @build sun.hotspot.WhiteBox30*31* @run main/bootclasspath/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xcomp32* -XX:CompileOnly=::get,::get1,::get2,::get3,::get433* -XX:-TieredCompilation34* -XX:+FoldStableValues35* compiler.stable.TestStableByte36* @run main/bootclasspath/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xcomp37* -XX:CompileOnly=::get,::get1,::get2,::get3,::get438* -XX:-TieredCompilation39* -XX:-FoldStableValues40* compiler.stable.TestStableByte41*42* @run main/bootclasspath/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xcomp43* -XX:CompileOnly=::get,::get1,::get2,::get3,::get444* -XX:+TieredCompilation -XX:TieredStopAtLevel=145* -XX:+FoldStableValues46* compiler.stable.TestStableByte47* @run main/bootclasspath/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xcomp48* -XX:CompileOnly=::get,::get1,::get2,::get3,::get449* -XX:+TieredCompilation -XX:TieredStopAtLevel=150* -XX:-FoldStableValues51* compiler.stable.TestStableByte52*/5354package compiler.stable;5556import jdk.internal.vm.annotation.Stable;5758import java.lang.reflect.InvocationTargetException;5960public class TestStableByte {61static final boolean isStableEnabled = StableConfiguration.isStableEnabled;6263public static void main(String[] args) throws Exception {64run(DefaultValue.class);65run(ByteStable.class);66run(DefaultStaticValue.class);67run(StaticByteStable.class);68run(VolatileByteStable.class);6970// @Stable arrays: Dim 1-471run(ByteArrayDim1.class);72run(ByteArrayDim2.class);73run(ByteArrayDim3.class);74run(ByteArrayDim4.class);7576// @Stable Object field: dynamic arrays77run(ObjectArrayLowerDim0.class);78run(ObjectArrayLowerDim1.class);79run(ObjectArrayLowerDim2.class);8081// Nested @Stable fields82run(NestedStableField.class);83run(NestedStableField1.class);84run(NestedStableField2.class);85run(NestedStableField3.class);8687if (failed) {88throw new Error("TEST FAILED");89}90}9192/* ==================================================== */9394static class DefaultValue {95public @Stable byte v;9697public static final DefaultValue c = new DefaultValue();98public static byte get() { return c.v; }99public static void test() throws Exception {100byte val1 = get();101c.v = 1; byte val2 = get();102assertEquals(val1, 0);103assertEquals(val2, 1);104}105}106107/* ==================================================== */108109static class ByteStable {110public @Stable byte v;111112public static final ByteStable c = new ByteStable();113public static byte get() { return c.v; }114public static void test() throws Exception {115c.v = 5; byte val1 = get();116c.v = 127; byte val2 = get();117assertEquals(val1, 5);118assertEquals(val2, (isStableEnabled ? 5 : 127));119}120}121122/* ==================================================== */123124static class DefaultStaticValue {125public static @Stable byte v;126127public static final DefaultStaticValue c = new DefaultStaticValue();128public static byte get() { return c.v; }129public static void test() throws Exception {130byte val1 = get();131c.v = 1; byte val2 = get();132assertEquals(val1, 0);133assertEquals(val2, 1);134}135}136137/* ==================================================== */138139static class StaticByteStable {140public static @Stable byte v;141142public static final StaticByteStable c = new StaticByteStable();143public static byte get() { return c.v; }144public static void test() throws Exception {145c.v = 5; byte val1 = get();146c.v = 127; byte val2 = get();147assertEquals(val1, 5);148assertEquals(val2, (isStableEnabled ? 5 : 127));149}150}151152/* ==================================================== */153154static class VolatileByteStable {155public @Stable volatile byte v;156157public static final VolatileByteStable c = new VolatileByteStable();158public static byte get() { return c.v; }159public static void test() throws Exception {160c.v = 5; byte val1 = get();161c.v = 127; byte val2 = get();162assertEquals(val1, 5);163assertEquals(val2, (isStableEnabled ? 5 : 127));164}165}166167/* ==================================================== */168// @Stable array == field && all components are stable169170static class ByteArrayDim1 {171public @Stable byte[] v;172173public static final ByteArrayDim1 c = new ByteArrayDim1();174public static byte get() { return c.v[0]; }175public static byte get1() { return c.v[10]; }176public static byte[] get2() { return c.v; }177public static void test() throws Exception {178{179c.v = new byte[1]; c.v[0] = 1; byte val1 = get();180c.v[0] = 2; byte val2 = get();181assertEquals(val1, 1);182assertEquals(val2, (isStableEnabled ? 1 : 2));183184c.v = new byte[1]; c.v[0] = 3; byte val3 = get();185assertEquals(val3, (isStableEnabled ? (isStableEnabled ? 1 : 2)186: 3));187}188189{190c.v = new byte[20]; c.v[10] = 1; byte val1 = get1();191c.v[10] = 2; byte val2 = get1();192assertEquals(val1, 1);193assertEquals(val2, (isStableEnabled ? 1 : 2));194195c.v = new byte[20]; c.v[10] = 3; byte val3 = get1();196assertEquals(val3, (isStableEnabled ? (isStableEnabled ? 1 : 2)197: 3));198}199200{201c.v = new byte[1]; byte[] val1 = get2();202c.v = new byte[1]; byte[] val2 = get2();203assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));204}205}206}207208/* ==================================================== */209210static class ByteArrayDim2 {211public @Stable byte[][] v;212213public static final ByteArrayDim2 c = new ByteArrayDim2();214public static byte get() { return c.v[0][0]; }215public static byte[] get1() { return c.v[0]; }216public static byte[][] get2() { return c.v; }217public static void test() throws Exception {218{219c.v = new byte[1][1]; c.v[0][0] = 1; byte val1 = get();220c.v[0][0] = 2; byte val2 = get();221assertEquals(val1, 1);222assertEquals(val2, (isStableEnabled ? 1 : 2));223224c.v = new byte[1][1]; c.v[0][0] = 3; byte val3 = get();225assertEquals(val3, (isStableEnabled ? (isStableEnabled ? 1 : 2)226: 3));227228c.v[0] = new byte[1]; c.v[0][0] = 4; byte val4 = get();229assertEquals(val4, (isStableEnabled ? (isStableEnabled ? 1 : 2)230: 4));231}232233{234c.v = new byte[1][1]; byte[] val1 = get1();235c.v[0] = new byte[1]; byte[] val2 = get1();236assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));237}238239{240c.v = new byte[1][1]; byte[][] val1 = get2();241c.v = new byte[1][1]; byte[][] val2 = get2();242assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));243}244}245}246247/* ==================================================== */248249static class ByteArrayDim3 {250public @Stable byte[][][] v;251252public static final ByteArrayDim3 c = new ByteArrayDim3();253public static byte get() { return c.v[0][0][0]; }254public static byte[] get1() { return c.v[0][0]; }255public static byte[][] get2() { return c.v[0]; }256public static byte[][][] get3() { return c.v; }257public static void test() throws Exception {258{259c.v = new byte[1][1][1]; c.v[0][0][0] = 1; byte val1 = get();260c.v[0][0][0] = 2; byte val2 = get();261assertEquals(val1, 1);262assertEquals(val2, (isStableEnabled ? 1 : 2));263264c.v = new byte[1][1][1]; c.v[0][0][0] = 3; byte val3 = get();265assertEquals(val3, (isStableEnabled ? (isStableEnabled ? 1 : 2)266: 3));267268c.v[0] = new byte[1][1]; c.v[0][0][0] = 4; byte val4 = get();269assertEquals(val4, (isStableEnabled ? (isStableEnabled ? 1 : 2)270: 4));271272c.v[0][0] = new byte[1]; c.v[0][0][0] = 5; byte val5 = get();273assertEquals(val5, (isStableEnabled ? (isStableEnabled ? 1 : 2)274: 5));275}276277{278c.v = new byte[1][1][1]; byte[] val1 = get1();279c.v[0][0] = new byte[1]; byte[] val2 = get1();280assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));281}282283{284c.v = new byte[1][1][1]; byte[][] val1 = get2();285c.v[0] = new byte[1][1]; byte[][] val2 = get2();286assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));287}288289{290c.v = new byte[1][1][1]; byte[][][] val1 = get3();291c.v = new byte[1][1][1]; byte[][][] val2 = get3();292assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));293}294}295}296297/* ==================================================== */298299static class ByteArrayDim4 {300public @Stable byte[][][][] v;301302public static final ByteArrayDim4 c = new ByteArrayDim4();303public static byte get() { return c.v[0][0][0][0]; }304public static byte[] get1() { return c.v[0][0][0]; }305public static byte[][] get2() { return c.v[0][0]; }306public static byte[][][] get3() { return c.v[0]; }307public static byte[][][][] get4() { return c.v; }308public static void test() throws Exception {309{310c.v = new byte[1][1][1][1]; c.v[0][0][0][0] = 1; byte val1 = get();311c.v[0][0][0][0] = 2; byte val2 = get();312assertEquals(val1, 1);313assertEquals(val2, (isStableEnabled ? 1 : 2));314315c.v = new byte[1][1][1][1]; c.v[0][0][0][0] = 3; byte val3 = get();316assertEquals(val3, (isStableEnabled ? (isStableEnabled ? 1 : 2)317: 3));318319c.v[0] = new byte[1][1][1]; c.v[0][0][0][0] = 4; byte val4 = get();320assertEquals(val4, (isStableEnabled ? (isStableEnabled ? 1 : 2)321: 4));322323c.v[0][0] = new byte[1][1]; c.v[0][0][0][0] = 5; byte val5 = get();324assertEquals(val5, (isStableEnabled ? (isStableEnabled ? 1 : 2)325: 5));326327c.v[0][0][0] = new byte[1]; c.v[0][0][0][0] = 6; byte val6 = get();328assertEquals(val6, (isStableEnabled ? (isStableEnabled ? 1 : 2)329: 6));330}331332{333c.v = new byte[1][1][1][1]; byte[] val1 = get1();334c.v[0][0][0] = new byte[1]; byte[] val2 = get1();335assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));336}337338{339c.v = new byte[1][1][1][1]; byte[][] val1 = get2();340c.v[0][0] = new byte[1][1]; byte[][] val2 = get2();341assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));342}343344{345c.v = new byte[1][1][1][1]; byte[][][] val1 = get3();346c.v[0] = new byte[1][1][1]; byte[][][] val2 = get3();347assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));348}349350{351c.v = new byte[1][1][1][1]; byte[][][][] val1 = get4();352c.v = new byte[1][1][1][1]; byte[][][][] val2 = get4();353assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));354}355356}357}358359/* ==================================================== */360// Dynamic Dim is higher than static361362static class ObjectArrayLowerDim0 {363public @Stable Object v;364365public static final ObjectArrayLowerDim0 c = new ObjectArrayLowerDim0();366public static byte get() { return ((byte[])c.v)[0]; }367public static byte[] get1() { return (byte[])c.v; }368369public static void test() throws Exception {370{371c.v = new byte[1]; ((byte[])c.v)[0] = 1; byte val1 = get();372((byte[])c.v)[0] = 2; byte val2 = get();373374assertEquals(val1, 1);375assertEquals(val2, 2);376}377378{379c.v = new byte[1]; byte[] val1 = get1();380c.v = new byte[1]; byte[] val2 = get1();381assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));382}383}384}385386/* ==================================================== */387388static class ObjectArrayLowerDim1 {389public @Stable Object[] v;390391public static final ObjectArrayLowerDim1 c = new ObjectArrayLowerDim1();392public static byte get() { return ((byte[][])c.v)[0][0]; }393public static byte[] get1() { return (byte[])(c.v[0]); }394public static Object[] get2() { return c.v; }395396public static void test() throws Exception {397{398c.v = new byte[1][1]; ((byte[][])c.v)[0][0] = 1; byte val1 = get();399((byte[][])c.v)[0][0] = 2; byte val2 = get();400401assertEquals(val1, 1);402assertEquals(val2, 2);403}404405{406c.v = new byte[1][1]; c.v[0] = new byte[0]; byte[] val1 = get1();407c.v[0] = new byte[0]; byte[] val2 = get1();408409assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));410}411412{413c.v = new byte[0][0]; Object[] val1 = get2();414c.v = new byte[0][0]; Object[] val2 = get2();415416assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));417}418}419}420421/* ==================================================== */422423static class ObjectArrayLowerDim2 {424public @Stable Object[][] v;425426public static final ObjectArrayLowerDim2 c = new ObjectArrayLowerDim2();427public static byte get() { return ((byte[][][])c.v)[0][0][0]; }428public static byte[] get1() { return (byte[])(c.v[0][0]); }429public static byte[][] get2() { return (byte[][])(c.v[0]); }430public static Object[][] get3() { return c.v; }431432public static void test() throws Exception {433{434c.v = new byte[1][1][1]; ((byte[][][])c.v)[0][0][0] = 1; byte val1 = get();435((byte[][][])c.v)[0][0][0] = 2; byte val2 = get();436437assertEquals(val1, 1);438assertEquals(val2, 2);439}440441{442c.v = new byte[1][1][1]; c.v[0][0] = new byte[0]; byte[] val1 = get1();443c.v[0][0] = new byte[0]; byte[] val2 = get1();444445assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));446}447448{449c.v = new byte[1][1][1]; c.v[0] = new byte[0][0]; byte[][] val1 = get2();450c.v[0] = new byte[0][0]; byte[][] val2 = get2();451452assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));453}454455{456c.v = new byte[0][0][0]; Object[][] val1 = get3();457c.v = new byte[0][0][0]; Object[][] val2 = get3();458459assertTrue((isStableEnabled ? (val1 == val2) : (val1 != val2)));460}461}462}463464/* ==================================================== */465466static class NestedStableField {467static class A {468public @Stable byte a;469470}471public @Stable A v;472473public static final NestedStableField c = new NestedStableField();474public static A get() { return c.v; }475public static byte get1() { return get().a; }476477public static void test() throws Exception {478{479c.v = new A(); c.v.a = 1; A val1 = get();480c.v.a = 2; A val2 = get();481482assertEquals(val1.a, 2);483assertEquals(val2.a, 2);484}485486{487c.v = new A(); c.v.a = 1; byte val1 = get1();488c.v.a = 2; byte val2 = get1();489c.v = new A(); c.v.a = 3; byte val3 = get1();490491assertEquals(val1, 1);492assertEquals(val2, (isStableEnabled ? 1 : 2));493assertEquals(val3, (isStableEnabled ? 1 : 3));494}495}496}497498/* ==================================================== */499500static class NestedStableField1 {501static class A {502public @Stable byte a;503public @Stable A next;504}505public @Stable A v;506507public static final NestedStableField1 c = new NestedStableField1();508public static A get() { return c.v.next.next.next.next.next.next.next; }509public static byte get1() { return get().a; }510511public static void test() throws Exception {512{513c.v = new A(); c.v.next = new A(); c.v.next.next = c.v;514c.v.a = 1; c.v.next.a = 1; A val1 = get();515c.v.a = 2; c.v.next.a = 2; A val2 = get();516517assertEquals(val1.a, 2);518assertEquals(val2.a, 2);519}520521{522c.v = new A(); c.v.next = c.v;523c.v.a = 1; byte val1 = get1();524c.v.a = 2; byte val2 = get1();525c.v = new A(); c.v.next = c.v;526c.v.a = 3; byte val3 = get1();527528assertEquals(val1, 1);529assertEquals(val2, (isStableEnabled ? 1 : 2));530assertEquals(val3, (isStableEnabled ? 1 : 3));531}532}533}534/* ==================================================== */535536static class NestedStableField2 {537static class A {538public @Stable byte a;539public @Stable A left;540public A right;541}542543public @Stable A v;544545public static final NestedStableField2 c = new NestedStableField2();546public static byte get() { return c.v.left.left.left.a; }547public static byte get1() { return c.v.left.left.right.left.a; }548549public static void test() throws Exception {550{551c.v = new A(); c.v.left = c.v.right = c.v;552c.v.a = 1; byte val1 = get(); byte val2 = get1();553c.v.a = 2; byte val3 = get(); byte val4 = get1();554555assertEquals(val1, 1);556assertEquals(val3, (isStableEnabled ? 1 : 2));557558assertEquals(val2, 1);559assertEquals(val4, 2);560}561}562}563564/* ==================================================== */565566static class NestedStableField3 {567static class A {568public @Stable byte a;569public @Stable A[] left;570public A[] right;571}572573public @Stable A[] v;574575public static final NestedStableField3 c = new NestedStableField3();576public static byte get() { return c.v[0].left[1].left[0].left[1].a; }577public static byte get1() { return c.v[1].left[0].left[1].right[0].left[1].a; }578579public static void test() throws Exception {580{581A elem = new A();582c.v = new A[] { elem, elem }; c.v[0].left = c.v[0].right = c.v;583elem.a = 1; byte val1 = get(); byte val2 = get1();584elem.a = 2; byte val3 = get(); byte val4 = get1();585586assertEquals(val1, 1);587assertEquals(val3, (isStableEnabled ? 1 : 2));588589assertEquals(val2, 1);590assertEquals(val4, 2);591}592}593}594595/* ==================================================== */596// Auxiliary methods597static void assertEquals(int i, int j) { if (i != j) throw new AssertionError(i + " != " + j); }598static void assertTrue(boolean b) { if (!b) throw new AssertionError(); }599600static boolean failed = false;601602public static void run(Class<?> test) {603Throwable ex = null;604System.out.print(test.getName()+": ");605try {606test.getMethod("test").invoke(null);607} catch (InvocationTargetException e) {608ex = e.getCause();609} catch (Throwable e) {610ex = e;611} finally {612if (ex == null) {613System.out.println("PASSED");614} else {615failed = true;616System.out.println("FAILED");617ex.printStackTrace(System.out);618}619}620}621}622623624