1 /*
 2  * Copyright (c) 2020, 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 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 }