Skip to content

Commit bf5f8b4

Browse files
Various fixes for @cconstant, @cfield, @RawField, @cenum, @CBitfield, and CConstantValueSupport.
1 parent 324b856 commit bf5f8b4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1349
-1219
lines changed

sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/c/constant/CConstant.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -89,8 +89,7 @@ private ValueAccess() {
8989
* This method is useful during native image generation, when the annotated method cannot be
9090
* called.
9191
*
92-
* @param declaringClass The class that contains the method annotated with {@link CConstant}
93-
* .
92+
* @param declaringClass The class that contains the method.
9493
* @param methodName The name of the method annotated with {@link CConstant}.
9594
* @param returnType The desired type of the returned value. For integer-kind constants, the
9695
* supported types are {@link Long}, {@link Integer}, and {@link Boolean}. For

sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/c/constant/CEnum.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -63,9 +63,13 @@
6363
* enumeration value. This annotation is optional and only needed if an attribute has a non-default
6464
* value.
6565
* <p>
66-
* Note that C enumeration are merely a type-safe way to specify integer constants in C. Therefore,
67-
* C enumeration values can be imported to Java as a regular {@link CConstant}; and {@link CEnum}
68-
* can be used to import regular integer C constants as a Java enumeration.
66+
* Note that in C, the keyword "enum" is merely a type-safe way to specify integer constants.
67+
* Therefore, it is also possible to import C enumeration values as regular {@link CConstant}s into
68+
* Java.
69+
* <p>
70+
* {@link CEnum} can also be used to import multiple (potentially unrelated) regular integer C
71+
* constants into a single Java enumeration. However, please note that each C constant will be
72+
* converted to the C type that {@link CEnum} specifies.
6973
* <p>
7074
* The annotated class, or an outer class that contains the class, must be annotated with
7175
* {@link CContext}.
@@ -77,8 +81,8 @@
7781
public @interface CEnum {
7882

7983
/**
80-
* Specifies the name of the imported C enum type. If no name is provided, <code>int</code> is
81-
* used instead.
84+
* Specifies the name of the imported C enum type. If no name is provided, the C data type
85+
* <code>int</code> is used instead.
8286
*
8387
* @since 19.0
8488
*/

sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/c/struct/CBitfield.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -65,7 +65,7 @@
6565
* The receiver is the pointer to the struct that is accessed, i.e., the base address of the memory
6666
* access.
6767
* <p>
68-
* The {@code FieldType} must be must be a primitive integer type or a {@link WordBase word type}.
68+
* The {@code FieldType} must be a primitive integer type or a {@link WordBase word type}.
6969
* <p>
7070
* The optional parameter {@code locationIdentity} specifies the {@link LocationIdentity} to be used
7171
* for the memory access. Two memory accesses with two different location identities are guaranteed

sdk/src/org.graalvm.nativeimage/src/org/graalvm/nativeimage/c/struct/CTypedefOfInfo.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -46,12 +46,13 @@
4646
import java.lang.annotation.Target;
4747

4848
/**
49-
* Informational only. Used to provide information about the base type of a type mentioned in a
50-
* System Java annotation. Currently use with {@link CPointerTo} to inform about the base primitive
51-
* type of the type pointed to.
49+
* Informational only. Used to document the base type of a type mentioned in a System Java
50+
* annotation. Currently use with {@link CPointerTo} to inform about the base primitive type of the
51+
* type pointed to.
5252
*
5353
* @since 19.0
5454
*/
55+
@Deprecated(since = "24.1", forRemoval = true)
5556
@Retention(RetentionPolicy.SOURCE)
5657
@Target({ElementType.TYPE})
5758
public @interface CTypedefOfInfo {

substratevm/src/com.oracle.svm.core.posix/src/com/oracle/svm/core/posix/headers/Dlfcn.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,25 @@
2424
*/
2525
package com.oracle.svm.core.posix.headers;
2626

27-
import com.oracle.svm.core.c.libc.GLibC;
28-
import com.oracle.svm.core.c.libc.LibCSpecific;
2927
import org.graalvm.nativeimage.Platform;
3028
import org.graalvm.nativeimage.Platforms;
3129
import org.graalvm.nativeimage.c.CContext;
3230
import org.graalvm.nativeimage.c.constant.CConstant;
3331
import org.graalvm.nativeimage.c.function.CFunction;
34-
import org.graalvm.nativeimage.c.function.CLibrary;
3532
import org.graalvm.nativeimage.c.function.CFunction.Transition;
33+
import org.graalvm.nativeimage.c.function.CLibrary;
3634
import org.graalvm.nativeimage.c.struct.CField;
3735
import org.graalvm.nativeimage.c.struct.CPointerTo;
3836
import org.graalvm.nativeimage.c.struct.CStruct;
39-
import org.graalvm.nativeimage.c.struct.CTypedefOfInfo;
4037
import org.graalvm.nativeimage.c.type.CCharPointer;
4138
import org.graalvm.word.Pointer;
4239
import org.graalvm.word.PointerBase;
4340
import org.graalvm.word.SignedWord;
4441
import org.graalvm.word.WordBase;
4542

43+
import com.oracle.svm.core.c.libc.GLibC;
44+
import com.oracle.svm.core.c.libc.LibCSpecific;
45+
4646
// Checkstyle: stop
4747

4848
/**
@@ -106,7 +106,6 @@ public static class GNUExtensions {
106106
public interface Lmid_t extends SignedWord {
107107
}
108108

109-
@CTypedefOfInfo("long int")
110109
@CPointerTo(nameOfCType = "Lmid_t")
111110
public interface Lmid_tPointer extends PointerBase {
112111
Lmid_t read();

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/c/enums/EnumArrayLookup.java renamed to substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/c/enums/CEnumArrayLookup.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,19 @@
2626

2727
import jdk.graal.compiler.core.common.calc.UnsignedMath;
2828

29-
public class EnumArrayLookup extends EnumRuntimeData {
30-
29+
public class CEnumArrayLookup extends CEnumRuntimeData {
3130
private final long cOffset;
32-
private Enum<?>[] cToJava;
31+
private final Enum<?>[] cToJava;
3332

34-
public EnumArrayLookup(long[] javaToC, long cOffset, Enum<?>[] cToJava) {
35-
super(javaToC);
33+
public CEnumArrayLookup(long[] javaToC, int bytesInC, boolean isCValueUnsigned, long cOffset, Enum<?>[] cToJava) {
34+
super(javaToC, bytesInC, isCValueUnsigned);
3635
this.cOffset = cOffset;
3736
this.cToJava = cToJava;
3837
}
3938

4039
@Override
41-
protected Enum<?> convertCToJava(long cValue) {
42-
long arrayIdx = cValue - cOffset;
40+
protected Enum<?> lookupEnum(long value) {
41+
long arrayIdx = value - cOffset;
4342
if (UnsignedMath.aboveOrEqual(arrayIdx, cToJava.length)) {
4443
return null;
4544
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/c/enums/EnumMapLookup.java renamed to substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/c/enums/CEnumMapLookup.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,16 @@
2626

2727
import java.util.Map;
2828

29-
public class EnumMapLookup extends EnumRuntimeData {
29+
public class CEnumMapLookup extends CEnumRuntimeData {
30+
private final Map<Long, Enum<?>> cToJava;
3031

31-
private Map<Long, Enum<?>> cToJava;
32-
33-
public EnumMapLookup(long[] javaToC, Map<Long, Enum<?>> cToJava) {
34-
super(javaToC);
32+
public CEnumMapLookup(long[] javaToC, int bytesInC, boolean isCValueUnsigned, Map<Long, Enum<?>> cToJava) {
33+
super(javaToC, bytesInC, isCValueUnsigned);
3534
this.cToJava = cToJava;
3635
}
3736

3837
@Override
39-
protected Enum<?> convertCToJava(long cValue) {
40-
return cToJava.get(cValue);
38+
protected Enum<?> lookupEnum(long value) {
39+
return cToJava.get(value);
4140
}
4241
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/c/enums/EnumNoLookup.java renamed to substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/c/enums/CEnumNoLookup.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,13 @@
2424
*/
2525
package com.oracle.svm.core.c.enums;
2626

27-
public class EnumNoLookup extends EnumRuntimeData {
28-
29-
public EnumNoLookup(long[] javaToC) {
30-
super(javaToC);
27+
public class CEnumNoLookup extends CEnumRuntimeData {
28+
public CEnumNoLookup(long[] javaToC, int bytesInC, boolean isCValueUnsigned) {
29+
super(javaToC, bytesInC, isCValueUnsigned);
3130
}
3231

3332
@Override
34-
protected Enum<?> convertCToJava(long cValue) {
33+
protected Enum<?> lookupEnum(long value) {
3534
return null;
3635
}
3736
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright (c) 2014, 2024, 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. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package com.oracle.svm.core.c.enums;
26+
27+
import org.graalvm.word.SignedWord;
28+
import org.graalvm.word.UnsignedWord;
29+
import org.graalvm.word.WordFactory;
30+
31+
import com.oracle.svm.core.Uninterruptible;
32+
import com.oracle.svm.core.jdk.UninterruptibleUtils.CodeUtil;
33+
34+
public abstract class CEnumRuntimeData {
35+
private static final NullPointerException CACHED_NULL_EXCEPTION = new NullPointerException(
36+
"null enum object cannot be converted to C enum integer (typically for automatic conversions on return to C code)");
37+
38+
/** Stores the sign- or zero-extended C values (depending on the signedness of the C enum). */
39+
private final long[] javaToC;
40+
private final int bytesInC;
41+
private final boolean isCEnumTypeUnsigned;
42+
43+
protected CEnumRuntimeData(long[] javaToC, int bytesInC, boolean isCEnumTypeUnsigned) {
44+
this.javaToC = javaToC;
45+
this.bytesInC = bytesInC;
46+
this.isCEnumTypeUnsigned = isCEnumTypeUnsigned;
47+
}
48+
49+
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
50+
public final boolean enumToBoolean(Enum<?> value) {
51+
return enumToLong(value) != 0L;
52+
}
53+
54+
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
55+
public final byte enumToByte(Enum<?> value) {
56+
return (byte) enumToLong(value);
57+
}
58+
59+
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
60+
public final int enumToShort(Enum<?> value) {
61+
return (short) enumToLong(value);
62+
}
63+
64+
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
65+
public final char enumToChar(Enum<?> value) {
66+
return (char) CodeUtil.zeroExtend(enumToLong(value), bytesInC * Byte.SIZE);
67+
}
68+
69+
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
70+
public final int enumToInt(Enum<?> value) {
71+
return (int) enumToLong(value);
72+
}
73+
74+
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
75+
public final long enumToLong(Enum<?> value) {
76+
if (value == null) {
77+
throw CACHED_NULL_EXCEPTION;
78+
}
79+
return javaToC[value.ordinal()];
80+
}
81+
82+
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
83+
public final SignedWord enumToSignedWord(Enum<?> value) {
84+
long result = enumToLong(value);
85+
return WordFactory.signed(result);
86+
}
87+
88+
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
89+
public final UnsignedWord enumToUnsignedWord(Enum<?> value) {
90+
long result = CodeUtil.zeroExtend(enumToLong(value), bytesInC * Byte.SIZE);
91+
return WordFactory.unsigned(result);
92+
}
93+
94+
/**
95+
* Reduce the C value to the bits that fit into the C enum type. Then, sign- or zero-extend that
96+
* value to 64-bit so that it can be compared to the data in the image heap.
97+
*/
98+
public final Enum<?> longToEnum(long rawValue) {
99+
long lookupValue;
100+
if (isCEnumTypeUnsigned) {
101+
lookupValue = CodeUtil.zeroExtend(rawValue, bytesInC * Byte.SIZE);
102+
} else {
103+
lookupValue = CodeUtil.signExtend(rawValue, bytesInC * Byte.SIZE);
104+
}
105+
return lookupEnum(lookupValue);
106+
}
107+
108+
protected abstract Enum<?> lookupEnum(long value);
109+
}

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/c/enums/EnumRuntimeData.java

Lines changed: 0 additions & 60 deletions
This file was deleted.

0 commit comments

Comments
 (0)