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.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 }