< prev index next >

src/jdk.incubator.foreign/share/classes/jdk/internal/foreign/abi/x64/windows/TypeClass.java

Print this page

 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 jdk.internal.foreign.abi.x64.windows;
26 
27 import jdk.incubator.foreign.GroupLayout;

28 import jdk.incubator.foreign.MemoryLayout;
29 import jdk.incubator.foreign.ValueLayout;
30 import jdk.internal.foreign.PlatformLayouts;
31 
32 import static jdk.internal.foreign.PlatformLayouts.Win64.VARARGS_ATTRIBUTE_NAME;
33 
34 enum TypeClass {
35     STRUCT_REGISTER,
36     STRUCT_REFERENCE,
37     POINTER,
38     INTEGER,
39     FLOAT,
40     VARARG_FLOAT;
41 
42     private static TypeClass classifyValueType(ValueLayout type) {
43         // No 128 bit integers in the Windows C ABI. There are __m128(i|d) intrinsic types but they act just
44         // like a struct when passing as an argument (passed by pointer).
45         // https://docs.microsoft.com/en-us/cpp/cpp/m128?view=vs-2019
46 
47         // x87 is ignored on Windows:
48         // "The x87 register stack is unused, and may be used by the callee,
49         // but must be considered volatile across function calls."
50         // https://docs.microsoft.com/en-us/cpp/build/x64-calling-convention?view=vs-2019
51 
52         return switch (PlatformLayouts.getKind(type)) {
53             case CHAR, SHORT, INT, LONG, LONG_LONG -> INTEGER;
54             case POINTER -> POINTER;
55             case FLOAT, DOUBLE -> {
56                  if (type.attribute(VARARGS_ATTRIBUTE_NAME)
57                         .map(Boolean.class::cast).orElse(false)) {
58                     yield VARARG_FLOAT;
59                 }
60                 yield FLOAT;
61             }
62         };




63     }
64 
65     static boolean isRegisterAggregate(MemoryLayout type) {
66         long size = type.byteSize();
67         return size == 1
68             || size == 2
69             || size == 4
70             || size == 8;
71     }
72 
73     private static TypeClass classifyStructType(MemoryLayout layout) {
74         if (isRegisterAggregate(layout)) {
75             return STRUCT_REGISTER;
76         }
77         return STRUCT_REFERENCE;
78     }
79 
80     static TypeClass typeClassFor(MemoryLayout type) {
81         if (type instanceof ValueLayout) {
82             return classifyValueType((ValueLayout) type);
83         } else if (type instanceof GroupLayout) {
84             return classifyStructType(type);
85         } else {
86             throw new IllegalArgumentException("Unhandled type " + type);
87         }
88     }
89 }

 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 jdk.internal.foreign.abi.x64.windows;
26 
27 import jdk.incubator.foreign.GroupLayout;
28 import jdk.incubator.foreign.MemoryAddress;
29 import jdk.incubator.foreign.MemoryLayout;
30 import jdk.incubator.foreign.ValueLayout;



31 
32 enum TypeClass {
33     STRUCT_REGISTER,
34     STRUCT_REFERENCE,
35     POINTER,
36     INTEGER,
37     FLOAT,
38     VARARG_FLOAT;
39 
40     private static TypeClass classifyValueType(ValueLayout type, boolean isVararg) {
41         // No 128 bit integers in the Windows C ABI. There are __m128(i|d) intrinsic types but they act just
42         // like a struct when passing as an argument (passed by pointer).
43         // https://docs.microsoft.com/en-us/cpp/cpp/m128?view=vs-2019
44 
45         // x87 is ignored on Windows:
46         // "The x87 register stack is unused, and may be used by the callee,
47         // but must be considered volatile across function calls."
48         // https://docs.microsoft.com/en-us/cpp/build/x64-calling-convention?view=vs-2019
49 
50         Class<?> carrier = type.carrier();
51         if (carrier == boolean.class || carrier == byte.class || carrier == char.class ||
52                 carrier == short.class || carrier == int.class || carrier == long.class) {
53             return INTEGER;
54         } else if (carrier == float.class || carrier == double.class) {
55             if (isVararg) {
56                 return VARARG_FLOAT;
57             } else {
58                 return FLOAT;
59             }
60         } else if (carrier == MemoryAddress.class) {
61             return POINTER;
62         } else {
63             throw new IllegalStateException("Cannot get here: " + carrier.getName());
64         }
65     }
66 
67     static boolean isRegisterAggregate(MemoryLayout type) {
68         long size = type.byteSize();
69         return size == 1
70             || size == 2
71             || size == 4
72             || size == 8;
73     }
74 
75     private static TypeClass classifyStructType(MemoryLayout layout) {
76         if (isRegisterAggregate(layout)) {
77             return STRUCT_REGISTER;
78         }
79         return STRUCT_REFERENCE;
80     }
81 
82     static TypeClass typeClassFor(MemoryLayout type, boolean isVararg) {
83         if (type instanceof ValueLayout) {
84             return classifyValueType((ValueLayout) type, isVararg);
85         } else if (type instanceof GroupLayout) {
86             return classifyStructType(type);
87         } else {
88             throw new IllegalArgumentException("Unhandled type " + type);
89         }
90     }
91 }
< prev index next >