1 /*
  2  *  Copyright (c) 2020, 2021, 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  */
 26 package jdk.internal.foreign;
 27 
 28 import jdk.incubator.foreign.MemoryLayout;
 29 import jdk.incubator.foreign.ValueLayout;
 30 
 31 public class PlatformLayouts {
 32     public static <Z extends MemoryLayout> Z pick(Z sysv, Z win64, Z aarch64) {
 33         return switch (CABI.current()) {
 34             case SysV -> sysv;
 35             case Win64 -> win64;
 36             case LinuxAArch64, MacOsAArch64 -> aarch64;
 37         };
 38     }
 39 
 40     /**
 41      * This class defines layout constants modelling standard primitive types supported by the x64 SystemV ABI.
 42      */
 43     public static final class SysV {
 44         private SysV() {
 45             //just the one
 46         }
 47 
 48         /**
 49          * The {@code bool} native type.
 50          */
 51         public static final ValueLayout.OfBoolean C_BOOL = ValueLayout.JAVA_BOOLEAN;
 52 
 53         /**
 54          * The {@code char} native type.
 55          */
 56         public static final ValueLayout.OfByte C_CHAR = ValueLayout.JAVA_BYTE;
 57 
 58         /**
 59          * The {@code short} native type.
 60          */
 61         public static final ValueLayout.OfShort C_SHORT = ValueLayout.JAVA_SHORT.withBitAlignment(16);
 62 
 63         /**
 64          * The {@code int} native type.
 65          */
 66         public static final ValueLayout.OfInt C_INT = ValueLayout.JAVA_INT.withBitAlignment(32);
 67 
 68         /**
 69          * The {@code long} native type.
 70          */
 71         public static final ValueLayout.OfLong C_LONG = ValueLayout.JAVA_LONG.withBitAlignment(64);
 72 
 73         /**
 74          * The {@code long long} native type.
 75          */
 76         public static final ValueLayout.OfLong C_LONG_LONG = ValueLayout.JAVA_LONG.withBitAlignment(64);
 77 
 78         /**
 79          * The {@code float} native type.
 80          */
 81         public static final ValueLayout.OfFloat C_FLOAT = ValueLayout.JAVA_FLOAT.withBitAlignment(32);
 82 
 83         /**
 84          * The {@code double} native type.
 85          */
 86         public static final ValueLayout.OfDouble C_DOUBLE = ValueLayout.JAVA_DOUBLE.withBitAlignment(64);
 87 
 88         /**
 89          * The {@code T*} native type.
 90          */
 91         public static final ValueLayout.OfAddress C_POINTER = ValueLayout.ADDRESS.withBitAlignment(64);
 92 
 93         /**
 94          * The {@code va_list} native type, as it is passed to a function.
 95          */
 96         public static final ValueLayout.OfAddress C_VA_LIST = SysV.C_POINTER;
 97     }
 98 
 99     /**
100      * This class defines layout constants modelling standard primitive types supported by the x64 Windows ABI.
101      */
102     public static final class Win64 {
103 
104         private Win64() {
105             //just the one
106         }
107 
108         /**
109          * The {@code bool} native type.
110          */
111         public static final ValueLayout.OfBoolean C_BOOL = ValueLayout.JAVA_BOOLEAN;
112 
113         /**
114          * The {@code char} native type.
115          */
116         public static final ValueLayout.OfByte C_CHAR = ValueLayout.JAVA_BYTE;
117 
118         /**
119          * The {@code short} native type.
120          */
121         public static final ValueLayout.OfShort C_SHORT = ValueLayout.JAVA_SHORT.withBitAlignment(16);
122 
123         /**
124          * The {@code int} native type.
125          */
126         public static final ValueLayout.OfInt C_INT = ValueLayout.JAVA_INT.withBitAlignment(32);
127         /**
128          * The {@code long} native type.
129          */
130         public static final ValueLayout.OfInt C_LONG = ValueLayout.JAVA_INT.withBitAlignment(32);
131 
132         /**
133          * The {@code long long} native type.
134          */
135         public static final ValueLayout.OfLong C_LONG_LONG = ValueLayout.JAVA_LONG.withBitAlignment(64);
136 
137         /**
138          * The {@code float} native type.
139          */
140         public static final ValueLayout.OfFloat C_FLOAT = ValueLayout.JAVA_FLOAT.withBitAlignment(32);
141 
142         /**
143          * The {@code double} native type.
144          */
145         public static final ValueLayout.OfDouble C_DOUBLE = ValueLayout.JAVA_DOUBLE.withBitAlignment(64);
146 
147         /**
148          * The {@code T*} native type.
149          */
150         public static final ValueLayout.OfAddress C_POINTER = ValueLayout.ADDRESS.withBitAlignment(64);
151 
152         /**
153          * The {@code va_list} native type, as it is passed to a function.
154          */
155         public static final ValueLayout.OfAddress C_VA_LIST = Win64.C_POINTER;
156     }
157 
158     /**
159      * This class defines layout constants modelling standard primitive types supported by the AArch64 ABI.
160      */
161     public static final class AArch64 {
162 
163         private AArch64() {
164             //just the one
165         }
166 
167         /**
168          * The {@code bool} native type.
169          */
170         public static final ValueLayout.OfBoolean C_BOOL = ValueLayout.JAVA_BOOLEAN;
171 
172         /**
173          * The {@code char} native type.
174          */
175         public static final ValueLayout.OfByte C_CHAR = ValueLayout.JAVA_BYTE;
176 
177         /**
178          * The {@code short} native type.
179          */
180         public static final ValueLayout.OfShort C_SHORT = ValueLayout.JAVA_SHORT.withBitAlignment(16);
181 
182         /**
183          * The {@code int} native type.
184          */
185         public static final ValueLayout.OfInt C_INT = ValueLayout.JAVA_INT.withBitAlignment(32);
186 
187         /**
188          * The {@code long} native type.
189          */
190         public static final ValueLayout.OfLong C_LONG = ValueLayout.JAVA_LONG.withBitAlignment(64);
191 
192         /**
193          * The {@code long long} native type.
194          */
195         public static final ValueLayout.OfLong C_LONG_LONG = ValueLayout.JAVA_LONG.withBitAlignment(64);
196 
197         /**
198          * The {@code float} native type.
199          */
200         public static final ValueLayout.OfFloat C_FLOAT = ValueLayout.JAVA_FLOAT.withBitAlignment(32);
201 
202         /**
203          * The {@code double} native type.
204          */
205         public static final ValueLayout.OfDouble C_DOUBLE = ValueLayout.JAVA_DOUBLE.withBitAlignment(64);
206 
207         /**
208          * The {@code T*} native type.
209          */
210         public static final ValueLayout.OfAddress C_POINTER = ValueLayout.ADDRESS.withBitAlignment(64);
211 
212         /**
213          * The {@code va_list} native type, as it is passed to a function.
214          */
215         public static final ValueLayout.OfAddress C_VA_LIST = AArch64.C_POINTER;
216     }
217 }