diff --git a/make/common/modules/GensrcStreamPreProcessing.gmk b/make/common/modules/GensrcStreamPreProcessing.gmk index a48e3c98d4b..eb92ed99ed4 100644 --- a/make/common/modules/GensrcStreamPreProcessing.gmk +++ b/make/common/modules/GensrcStreamPreProcessing.gmk @@ -116,7 +116,7 @@ Conv_A = \ # Return integer type with same size as the type Conv_memtype = \ - $(if $(filter float, $1), int, $(if $(filter double, $1), long, $1)) + $(if $(filter float, $1), int, $(if $(filter double, $1), long, $(if $(filter boolean, $1), byte, $1))) # Return capitalized integer type with same size as the type Conv_Memtype = \ diff --git a/make/modules/java.base/gensrc/GensrcVarHandles.gmk b/make/modules/java.base/gensrc/GensrcVarHandles.gmk index 341a8c9dc2c..4b1697fd354 100644 --- a/make/modules/java.base/gensrc/GensrcVarHandles.gmk +++ b/make/modules/java.base/gensrc/GensrcVarHandles.gmk @@ -111,7 +111,7 @@ define GenerateVarHandleMemorySegment $1_KEYS += CAS endif ifneq ($$(filter boolean byte, $1),) - $1_KEYS += byte + $1_KEYS += ByteOrBoolean endif ifneq ($$(filter float double, $1),) $1_KEYS += floatingPoint diff --git a/src/java.base/share/classes/java/lang/invoke/X-VarHandleSegmentView.java.template b/src/java.base/share/classes/java/lang/invoke/X-VarHandleSegmentView.java.template index aa8c7b28617..0147810cb4e 100644 --- a/src/java.base/share/classes/java/lang/invoke/X-VarHandleSegmentView.java.template +++ b/src/java.base/share/classes/java/lang/invoke/X-VarHandleSegmentView.java.template @@ -33,20 +33,20 @@ import static java.lang.invoke.SegmentVarHandle.*; #warn -{#if[byte]?final:sealed} class VarHandleSegmentAs$Type$s { +{#if[ByteOrBoolean]?final:sealed} class VarHandleSegmentAs$Type$s { -#if[!byte] +#if[!ByteOrBoolean] static final int NON_PLAIN_ACCESS_MIN_ALIGN_MASK = $BoxType$.BYTES - 1; -#end[byte] +#end[ByteOrBoolean] static VarForm selectForm(long alignmentMask, boolean constantOffset) { -#if[byte] +#if[ByteOrBoolean] return constantOffset ? CONSTANT_OFFSET_FORM : VARIABLE_OFFSET_FORM; -#else[byte] +#else[ByteOrBoolean] return (alignmentMask & NON_PLAIN_ACCESS_MIN_ALIGN_MASK) != NON_PLAIN_ACCESS_MIN_ALIGN_MASK ? (constantOffset ? CONSTANT_OFFSET_FORM : VARIABLE_OFFSET_FORM) : (constantOffset ? VarHandleSegmentAs$Type$sAligned.CONSTANT_OFFSET_FORM : VarHandleSegmentAs$Type$sAligned.VARIABLE_OFFSET_FORM); -#end[byte] +#end[ByteOrBoolean] } static final VarForm CONSTANT_OFFSET_FORM = new VarForm(VarHandleSegmentAs$Type$s.class, MemorySegment.class, $type$.class, long.class); @@ -70,16 +70,16 @@ import static java.lang.invoke.SegmentVarHandle.*; handle.be); return $Type$.$rawType$BitsTo$Type$(rawValue); #else[floatingPoint] -#if[byte] - return SCOPED_MEMORY_ACCESS.get$Type$(bb.sessionImpl(), +#if[ByteOrBoolean] + return SCOPED_MEMORY_ACCESS.get$RawType$(bb.sessionImpl(), bb.unsafeGetBase(), - offset(bb, base, offset)); -#else[byte] + offset(bb, base, offset)){#if[boolean]? != 0}; +#else[ByteOrBoolean] return SCOPED_MEMORY_ACCESS.get$Type$Unaligned(bb.sessionImpl(), bb.unsafeGetBase(), offset(bb, base, offset), handle.be); -#end[byte] +#end[ByteOrBoolean] #end[floatingPoint] } @@ -99,21 +99,21 @@ import static java.lang.invoke.SegmentVarHandle.*; $Type$.$type$ToRaw$RawType$Bits(value), handle.be); #else[floatingPoint] -#if[byte] +#if[ByteOrBoolean] SCOPED_MEMORY_ACCESS.put$Type$(bb.sessionImpl(), bb.unsafeGetBase(), offset(bb, base, offset), value); -#else[byte] +#else[ByteOrBoolean] SCOPED_MEMORY_ACCESS.put$Type$Unaligned(bb.sessionImpl(), bb.unsafeGetBase(), offset(bb, base, offset), value, handle.be); -#end[byte] +#end[ByteOrBoolean] #end[floatingPoint] } -#if[!byte] +#if[!ByteOrBoolean] } // This class must be accessed through non-aligned VarHandleSegmentAs$Type$s @@ -123,7 +123,7 @@ final class VarHandleSegmentAs$Type$sAligned extends VarHandleSegmentAs$Type$s { static final VarForm VARIABLE_OFFSET_FORM = new VarForm(VarHandleSegmentAs$Type$sAligned.class, VarHandleSegmentAs$Type$s.VARIABLE_OFFSET_FORM); VarHandleSegmentAs$Type$sAligned() { throw new AssertionError(); } -#end[byte] +#end[ByteOrBoolean] #if[floatingPoint] @ForceInline @@ -138,17 +138,29 @@ final class VarHandleSegmentAs$Type$sAligned extends VarHandleSegmentAs$Type$s { return $Type$.$rawType$BitsTo$Type$(rv); } #else[floatingPoint] -#if[byte] +#if[ByteOrBoolean] +#if[boolean] + @ForceInline + static $rawType$ convEndian(boolean big, $type$ v) { + return (byte) (v ? 1 : 0); + } + + @ForceInline + static $type$ convEndian(boolean big, $rawType$ n) { + return n != 0; + } +#else[boolean] @ForceInline static $type$ convEndian(boolean big, $type$ n) { return n; } -#else[byte] +#end[boolean] +#else[ByteOrBoolean] @ForceInline static $type$ convEndian(boolean big, $type$ n) { return big == BE ? n : $BoxType$.reverseBytes(n); } -#end[byte] +#end[ByteOrBoolean] #end[floatingPoint] @ForceInline @@ -424,18 +436,18 @@ final class VarHandleSegmentAs$Type$sAligned extends VarHandleSegmentAs$Type$s { static $type$ getAndAdd(VarHandle ob, Object obb, long base, long offset, $type$ delta) { SegmentVarHandle handle = (SegmentVarHandle)ob; AbstractMemorySegmentImpl bb = handle.checkSegment(obb, base, false); -#if[!byte] +#if[!ByteOrBoolean] if (handle.be == BE) { -#end[byte] +#end[ByteOrBoolean] return SCOPED_MEMORY_ACCESS.getAndAdd$RawType$(bb.sessionImpl(), bb.unsafeGetBase(), offset(bb, base, offset), delta); -#if[!byte] +#if[!ByteOrBoolean] } else { return getAndAddConvEndianWithCAS(bb, offset(bb, base, offset), delta); } -#end[byte] +#end[ByteOrBoolean] } @ForceInline @@ -447,18 +459,18 @@ final class VarHandleSegmentAs$Type$sAligned extends VarHandleSegmentAs$Type$s { static $type$ getAndAddAcquire(VarHandle ob, Object obb, long base, long offset, $type$ delta) { SegmentVarHandle handle = (SegmentVarHandle)ob; AbstractMemorySegmentImpl bb = handle.checkSegment(obb, base, false); -#if[!byte] +#if[!ByteOrBoolean] if (handle.be == BE) { -#end[byte] +#end[ByteOrBoolean] return SCOPED_MEMORY_ACCESS.getAndAdd$RawType$Acquire(bb.sessionImpl(), bb.unsafeGetBase(), offset(bb, base, offset), delta); -#if[!byte] +#if[!ByteOrBoolean] } else { return getAndAddConvEndianWithCAS(bb, offset(bb, base, offset), delta); } -#end[byte] +#end[ByteOrBoolean] } @ForceInline @@ -470,20 +482,20 @@ final class VarHandleSegmentAs$Type$sAligned extends VarHandleSegmentAs$Type$s { static $type$ getAndAddRelease(VarHandle ob, Object obb, long base, long offset, $type$ delta) { SegmentVarHandle handle = (SegmentVarHandle)ob; AbstractMemorySegmentImpl bb = handle.checkSegment(obb, base, false); -#if[!byte] +#if[!ByteOrBoolean] if (handle.be == BE) { -#end[byte] +#end[ByteOrBoolean] return SCOPED_MEMORY_ACCESS.getAndAdd$RawType$Release(bb.sessionImpl(), bb.unsafeGetBase(), offset(bb, base, offset), delta); -#if[!byte] +#if[!ByteOrBoolean] } else { return getAndAddConvEndianWithCAS(bb, offset(bb, base, offset), delta); } -#end[byte] +#end[ByteOrBoolean] } -#if[!byte] +#if[!ByteOrBoolean] @ForceInline static $type$ getAndAddConvEndianWithCAS(AbstractMemorySegmentImpl bb, long offset, $type$ delta) { @@ -496,7 +508,7 @@ final class VarHandleSegmentAs$Type$sAligned extends VarHandleSegmentAs$Type$s { nativeExpectedValue, $RawBoxType$.reverseBytes({#if[ShorterThanInt]?($type$) }(expectedValue + delta)))); return expectedValue; } -#end[byte] +#end[ByteOrBoolean] #end[AtomicAdd] #if[Bitwise] @@ -509,18 +521,18 @@ final class VarHandleSegmentAs$Type$sAligned extends VarHandleSegmentAs$Type$s { static $type$ getAndBitwiseOr(VarHandle ob, Object obb, long base, long offset, $type$ value) { SegmentVarHandle handle = (SegmentVarHandle)ob; AbstractMemorySegmentImpl bb = handle.checkSegment(obb, base, false); -#if[!byte] +#if[!ByteOrBoolean] if (handle.be == BE) { -#end[byte] +#end[ByteOrBoolean] return SCOPED_MEMORY_ACCESS.getAndBitwiseOr$RawType$(bb.sessionImpl(), bb.unsafeGetBase(), offset(bb, base, offset), value); -#if[!byte] +#if[!ByteOrBoolean] } else { return getAndBitwiseOrConvEndianWithCAS(bb, offset(bb, base, offset), value); } -#end[byte] +#end[ByteOrBoolean] } @ForceInline @@ -532,18 +544,18 @@ final class VarHandleSegmentAs$Type$sAligned extends VarHandleSegmentAs$Type$s { static $type$ getAndBitwiseOrRelease(VarHandle ob, Object obb, long base, long offset, $type$ value) { SegmentVarHandle handle = (SegmentVarHandle)ob; AbstractMemorySegmentImpl bb = handle.checkSegment(obb, base, false); -#if[!byte] +#if[!ByteOrBoolean] if (handle.be == BE) { -#end[byte] +#end[ByteOrBoolean] return SCOPED_MEMORY_ACCESS.getAndBitwiseOr$RawType$Release(bb.sessionImpl(), bb.unsafeGetBase(), offset(bb, base, offset), value); -#if[!byte] +#if[!ByteOrBoolean] } else { return getAndBitwiseOrConvEndianWithCAS(bb, offset(bb, base, offset), value); } -#end[byte] +#end[ByteOrBoolean] } @ForceInline @@ -555,20 +567,20 @@ final class VarHandleSegmentAs$Type$sAligned extends VarHandleSegmentAs$Type$s { static $type$ getAndBitwiseOrAcquire(VarHandle ob, Object obb, long base, long offset, $type$ value) { SegmentVarHandle handle = (SegmentVarHandle)ob; AbstractMemorySegmentImpl bb = handle.checkSegment(obb, base, false); -#if[!byte] +#if[!ByteOrBoolean] if (handle.be == BE) { -#end[byte] +#end[ByteOrBoolean] return SCOPED_MEMORY_ACCESS.getAndBitwiseOr$RawType$Acquire(bb.sessionImpl(), bb.unsafeGetBase(), offset(bb, base, offset), value); -#if[!byte] +#if[!ByteOrBoolean] } else { return getAndBitwiseOrConvEndianWithCAS(bb, offset(bb, base, offset), value); } -#end[byte] +#end[ByteOrBoolean] } -#if[!byte] +#if[!ByteOrBoolean] @ForceInline static $type$ getAndBitwiseOrConvEndianWithCAS(AbstractMemorySegmentImpl bb, long offset, $type$ value) { @@ -581,7 +593,7 @@ final class VarHandleSegmentAs$Type$sAligned extends VarHandleSegmentAs$Type$s { nativeExpectedValue, $RawBoxType$.reverseBytes({#if[ShorterThanInt]?($type$) }(expectedValue | value)))); return expectedValue; } -#end[byte] +#end[ByteOrBoolean] @ForceInline static $type$ getAndBitwiseAnd(VarHandle ob, Object obb, long base, $type$ value) { @@ -592,18 +604,18 @@ final class VarHandleSegmentAs$Type$sAligned extends VarHandleSegmentAs$Type$s { static $type$ getAndBitwiseAnd(VarHandle ob, Object obb, long base, long offset, $type$ value) { SegmentVarHandle handle = (SegmentVarHandle)ob; AbstractMemorySegmentImpl bb = handle.checkSegment(obb, base, false); -#if[!byte] +#if[!ByteOrBoolean] if (handle.be == BE) { -#end[byte] +#end[ByteOrBoolean] return SCOPED_MEMORY_ACCESS.getAndBitwiseAnd$RawType$(bb.sessionImpl(), bb.unsafeGetBase(), offset(bb, base, offset), value); -#if[!byte] +#if[!ByteOrBoolean] } else { return getAndBitwiseAndConvEndianWithCAS(bb, offset(bb, base, offset), value); } -#end[byte] +#end[ByteOrBoolean] } @ForceInline @@ -615,18 +627,18 @@ final class VarHandleSegmentAs$Type$sAligned extends VarHandleSegmentAs$Type$s { static $type$ getAndBitwiseAndRelease(VarHandle ob, Object obb, long base, long offset, $type$ value) { SegmentVarHandle handle = (SegmentVarHandle)ob; AbstractMemorySegmentImpl bb = handle.checkSegment(obb, base, false); -#if[!byte] +#if[!ByteOrBoolean] if (handle.be == BE) { -#end[byte] +#end[ByteOrBoolean] return SCOPED_MEMORY_ACCESS.getAndBitwiseAnd$RawType$Release(bb.sessionImpl(), bb.unsafeGetBase(), offset(bb, base, offset), value); -#if[!byte] +#if[!ByteOrBoolean] } else { return getAndBitwiseAndConvEndianWithCAS(bb, offset(bb, base, offset), value); } -#end[byte] +#end[ByteOrBoolean] } @ForceInline @@ -639,20 +651,20 @@ final class VarHandleSegmentAs$Type$sAligned extends VarHandleSegmentAs$Type$s { static $type$ getAndBitwiseAndAcquire(VarHandle ob, Object obb, long base, long offset, $type$ value) { SegmentVarHandle handle = (SegmentVarHandle)ob; AbstractMemorySegmentImpl bb = handle.checkSegment(obb, base, false); -#if[!byte] +#if[!ByteOrBoolean] if (handle.be == BE) { -#end[byte] +#end[ByteOrBoolean] return SCOPED_MEMORY_ACCESS.getAndBitwiseAnd$RawType$Acquire(bb.sessionImpl(), bb.unsafeGetBase(), offset(bb, base, offset), value); -#if[!byte] +#if[!ByteOrBoolean] } else { return getAndBitwiseAndConvEndianWithCAS(bb, offset(bb, base, offset), value); } -#end[byte] +#end[ByteOrBoolean] } -#if[!byte] +#if[!ByteOrBoolean] @ForceInline static $type$ getAndBitwiseAndConvEndianWithCAS(AbstractMemorySegmentImpl bb, long offset, $type$ value) { @@ -665,7 +677,7 @@ final class VarHandleSegmentAs$Type$sAligned extends VarHandleSegmentAs$Type$s { nativeExpectedValue, $RawBoxType$.reverseBytes({#if[ShorterThanInt]?($type$) }(expectedValue & value)))); return expectedValue; } -#end[byte] +#end[ByteOrBoolean] @ForceInline static $type$ getAndBitwiseXor(VarHandle ob, Object obb, long base, $type$ value) { @@ -676,18 +688,18 @@ final class VarHandleSegmentAs$Type$sAligned extends VarHandleSegmentAs$Type$s { static $type$ getAndBitwiseXor(VarHandle ob, Object obb, long base, long offset, $type$ value) { SegmentVarHandle handle = (SegmentVarHandle)ob; AbstractMemorySegmentImpl bb = handle.checkSegment(obb, base, false); -#if[!byte] +#if[!ByteOrBoolean] if (handle.be == BE) { -#end[byte] +#end[ByteOrBoolean] return SCOPED_MEMORY_ACCESS.getAndBitwiseXor$RawType$(bb.sessionImpl(), bb.unsafeGetBase(), offset(bb, base, offset), value); -#if[!byte] +#if[!ByteOrBoolean] } else { return getAndBitwiseXorConvEndianWithCAS(bb, offset(bb, base, offset), value); } -#end[byte] +#end[ByteOrBoolean] } @ForceInline @@ -699,18 +711,18 @@ final class VarHandleSegmentAs$Type$sAligned extends VarHandleSegmentAs$Type$s { static $type$ getAndBitwiseXorRelease(VarHandle ob, Object obb, long base, long offset, $type$ value) { SegmentVarHandle handle = (SegmentVarHandle)ob; AbstractMemorySegmentImpl bb = handle.checkSegment(obb, base, false); -#if[!byte] +#if[!ByteOrBoolean] if (handle.be == BE) { -#end[byte] +#end[ByteOrBoolean] return SCOPED_MEMORY_ACCESS.getAndBitwiseXor$RawType$Release(bb.sessionImpl(), bb.unsafeGetBase(), offset(bb, base, offset), value); -#if[!byte] +#if[!ByteOrBoolean] } else { return getAndBitwiseXorConvEndianWithCAS(bb, offset(bb, base, offset), value); } -#end[byte] +#end[ByteOrBoolean] } @ForceInline @@ -722,20 +734,20 @@ final class VarHandleSegmentAs$Type$sAligned extends VarHandleSegmentAs$Type$s { static $type$ getAndBitwiseXorAcquire(VarHandle ob, Object obb, long base, long offset, $type$ value) { SegmentVarHandle handle = (SegmentVarHandle)ob; AbstractMemorySegmentImpl bb = handle.checkSegment(obb, base, false); -#if[!byte] +#if[!ByteOrBoolean] if (handle.be == BE) { -#end[byte] +#end[ByteOrBoolean] return SCOPED_MEMORY_ACCESS.getAndBitwiseXor$RawType$Acquire(bb.sessionImpl(), bb.unsafeGetBase(), offset(bb, base, offset), value); -#if[!byte] +#if[!ByteOrBoolean] } else { return getAndBitwiseXorConvEndianWithCAS(bb, offset(bb, base, offset), value); } -#end[byte] +#end[ByteOrBoolean] } -#if[!byte] +#if[!ByteOrBoolean] @ForceInline static $type$ getAndBitwiseXorConvEndianWithCAS(AbstractMemorySegmentImpl bb, long offset, $type$ value) { @@ -748,6 +760,6 @@ final class VarHandleSegmentAs$Type$sAligned extends VarHandleSegmentAs$Type$s { nativeExpectedValue, $RawBoxType$.reverseBytes({#if[ShorterThanInt]?($type$) }(expectedValue ^ value)))); return expectedValue; } -#end[byte] +#end[ByteOrBoolean] #end[Bitwise] } diff --git a/test/jdk/ProblemList.txt b/test/jdk/ProblemList.txt index 5e730af92b0..fcde1d9c01d 100644 --- a/test/jdk/ProblemList.txt +++ b/test/jdk/ProblemList.txt @@ -648,8 +648,6 @@ jdk/jfr/event/oldobject/TestZ.java 8375615 generic- # jdk_foreign -java/foreign/normalize/TestNormalize.java 8386848 generic-all - ############################################################################ # Client manual tests diff --git a/test/jdk/java/foreign/normalize/TestNormalizeBooleanVarHandle.java b/test/jdk/java/foreign/normalize/TestNormalizeBooleanVarHandle.java new file mode 100644 index 00000000000..acca0d095c3 --- /dev/null +++ b/test/jdk/java/foreign/normalize/TestNormalizeBooleanVarHandle.java @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/* + * @test + * @run testng TestNormalizeBooleanVarHandle + */ + +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +import java.lang.foreign.Arena; +import java.lang.foreign.MemorySegment; +import java.lang.invoke.VarHandle; +import java.util.ArrayList; +import java.util.List; +import java.util.function.Function; +import java.util.function.Predicate; + +import static java.lang.foreign.ValueLayout.*; +import static org.testng.Assert.*; + +// test normalization of smaller than int primitive types +public class TestNormalizeBooleanVarHandle { + + static final VarHandle VH = JAVA_BOOLEAN.varHandle(); + + @Test(dataProvider = "bools") + public void testBool(Function segmentFactory, Predicate accessor, + byte testValue, boolean expected) { + try (Arena arena = Arena.ofConfined()) { + MemorySegment ms = segmentFactory.apply(arena); + ms.set(JAVA_BYTE, 0L, testValue); + + boolean b = accessor.test(ms); + assertEquals(b, expected); + } + } + + @DataProvider + public static Object[][] bools() { + List cases = new ArrayList<>(); + for (Function segmentFactory : factories()) { + for (Predicate accessor : accessors()) { + cases.add(new Object[]{ segmentFactory, accessor, + (byte) 0b0 , false }); // canonical false + cases.add(new Object[]{ segmentFactory, accessor, + (byte) 0b01, true }); // canonical true + cases.add(new Object[]{ segmentFactory, accessor, + (byte) 0b10, true }); // zero least significant bit, but non-zero first byte + } + } + + return cases.toArray(Object[][]::new); + } + + private static List> factories() { + return List.of( + a -> a.allocate(JAVA_BYTE), + _ -> MemorySegment.ofArray(new byte[1]) + ); + } + + private static List> accessors() { + return List.of( + ms -> ms.get(JAVA_BOOLEAN, 0L), + ms -> (boolean) VH.get(ms, 0L), + ms -> (boolean) VH.getVolatile(ms, 0L), + ms -> (boolean) VH.getAcquire(ms, 0L), + ms -> (boolean) VH.getOpaque(ms, 0L) + ); + } +}