]>
Commit | Line | Data |
---|---|---|
1 | /* Declarations for ISO Fortran binding. | |
2 | Copyright (C) 2018-2024 Free Software Foundation, Inc. | |
3 | Contributed by Daniel Celis Garza <celisdanieljr@gmail.com> | |
4 | ||
5 | This file is part of the GNU Fortran runtime library (libgfortran). | |
6 | ||
7 | Libgfortran is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 3, or (at your option) | |
10 | any later version. | |
11 | ||
12 | Libgfortran is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | Under Section 7 of GPL version 3, you are granted additional | |
18 | permissions described in the GCC Runtime Library Exception, version | |
19 | 3.1, as published by the Free Software Foundation. | |
20 | ||
21 | You should have received a copy of the GNU General Public License and | |
22 | a copy of the GCC Runtime Library Exception along with this program; | |
23 | see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
24 | <http://www.gnu.org/licenses/>. */ | |
25 | ||
26 | #ifndef ISO_FORTRAN_BINDING_H | |
27 | #define ISO_FORTRAN_BINDING_H | |
28 | ||
29 | #ifdef __cplusplus | |
30 | extern "C" { | |
31 | #endif | |
32 | ||
33 | #include <stddef.h> /* Standard ptrdiff_t tand size_t. */ | |
34 | #include <stdint.h> /* Integer types. */ | |
35 | ||
36 | /* Constants, defined as macros. */ | |
37 | #define CFI_VERSION 1 | |
38 | #define CFI_MAX_RANK 15 | |
39 | ||
40 | /* Attributes. */ | |
41 | #define CFI_attribute_pointer 0 | |
42 | #define CFI_attribute_allocatable 1 | |
43 | #define CFI_attribute_other 2 | |
44 | ||
45 | /* Error codes. | |
46 | Note that CFI_FAILURE and CFI_INVALID_STRIDE are specific to GCC | |
47 | and not part of the Fortran standard */ | |
48 | #define CFI_SUCCESS 0 | |
49 | #define CFI_FAILURE 1 | |
50 | #define CFI_ERROR_BASE_ADDR_NULL 2 | |
51 | #define CFI_ERROR_BASE_ADDR_NOT_NULL 3 | |
52 | #define CFI_INVALID_ELEM_LEN 4 | |
53 | #define CFI_INVALID_RANK 5 | |
54 | #define CFI_INVALID_TYPE 6 | |
55 | #define CFI_INVALID_ATTRIBUTE 7 | |
56 | #define CFI_INVALID_EXTENT 8 | |
57 | #define CFI_INVALID_STRIDE 9 | |
58 | #define CFI_INVALID_DESCRIPTOR 10 | |
59 | #define CFI_ERROR_MEM_ALLOCATION 11 | |
60 | #define CFI_ERROR_OUT_OF_BOUNDS 12 | |
61 | ||
62 | /* CFI type definitions. */ | |
63 | typedef ptrdiff_t CFI_index_t; | |
64 | typedef int8_t CFI_rank_t; | |
65 | typedef int8_t CFI_attribute_t; | |
66 | typedef int16_t CFI_type_t; | |
67 | ||
68 | /* CFI_dim_t. */ | |
69 | typedef struct CFI_dim_t | |
70 | { | |
71 | CFI_index_t lower_bound; | |
72 | CFI_index_t extent; | |
73 | CFI_index_t sm; | |
74 | } | |
75 | CFI_dim_t; | |
76 | ||
77 | /* CFI_cdesc_t, C descriptors are cast to this structure as follows: | |
78 | CFI_CDESC_T(CFI_MAX_RANK) foo; | |
79 | CFI_cdesc_t * bar = (CFI_cdesc_t *) &foo; | |
80 | */ | |
81 | typedef struct CFI_cdesc_t | |
82 | { | |
83 | void *base_addr; | |
84 | size_t elem_len; | |
85 | int version; | |
86 | CFI_rank_t rank; | |
87 | CFI_attribute_t attribute; | |
88 | CFI_type_t type; | |
89 | CFI_dim_t dim[]; | |
90 | } | |
91 | CFI_cdesc_t; | |
92 | ||
93 | /* CFI_CDESC_T with an explicit type. */ | |
94 | #define CFI_CDESC_TYPE_T(r, base_type) \ | |
95 | struct { \ | |
96 | base_type *base_addr; \ | |
97 | size_t elem_len; \ | |
98 | int version; \ | |
99 | CFI_rank_t rank; \ | |
100 | CFI_attribute_t attribute; \ | |
101 | CFI_type_t type; \ | |
102 | CFI_dim_t dim[r]; \ | |
103 | } | |
104 | #define CFI_CDESC_T(r) CFI_CDESC_TYPE_T (r, void) | |
105 | ||
106 | /* CFI function declarations. */ | |
107 | extern void *CFI_address (const CFI_cdesc_t *, const CFI_index_t []); | |
108 | extern int CFI_allocate (CFI_cdesc_t *, const CFI_index_t [], const CFI_index_t [], | |
109 | size_t); | |
110 | extern int CFI_deallocate (CFI_cdesc_t *); | |
111 | extern int CFI_establish (CFI_cdesc_t *, void *, CFI_attribute_t, CFI_type_t, size_t, | |
112 | CFI_rank_t, const CFI_index_t []); | |
113 | extern int CFI_is_contiguous (const CFI_cdesc_t *); | |
114 | extern int CFI_section (CFI_cdesc_t *, const CFI_cdesc_t *, const CFI_index_t [], | |
115 | const CFI_index_t [], const CFI_index_t []); | |
116 | extern int CFI_select_part (CFI_cdesc_t *, const CFI_cdesc_t *, size_t, size_t); | |
117 | extern int CFI_setpointer (CFI_cdesc_t *, CFI_cdesc_t *, const CFI_index_t []); | |
118 | ||
119 | /* Types and kind numbers. Allows bitwise and to reveal the intrinsic type of a kind type. It also allows us to find the kind parameter by inverting the bit-shift equation. | |
120 | CFI_type_kind_shift = 8 | |
121 | CFI_intrinsic_type = 0 0 0 0 0 0 0 0 0 0 1 0 | |
122 | CFI_type_kind = 0 0 0 0 0 0 0 0 1 0 0 0 | |
123 | CFI_type_example = CFI_intrinsic_type + (CFI_type_kind << CFI_type_kind_shift) | |
124 | Defining the CFI_type_example. | |
125 | CFI_type_kind = 0 0 0 0 0 0 0 0 1 0 0 0 << CFI_type_kind_shift | |
126 | ------------------------- | |
127 | 1 0 0 0 0 0 0 0 0 0 0 0 + | |
128 | CFI_intrinsic_type = 0 0 0 0 0 0 0 0 0 0 1 0 | |
129 | ------------------------- | |
130 | CFI_type_example = 1 0 0 0 0 0 0 0 0 0 1 0 | |
131 | Finding the intrinsic type with the logical mask. | |
132 | CFI_type_example = 1 0 0 0 0 0 0 0 0 0 1 0 & | |
133 | CFI_type_mask = 0 0 0 0 1 1 1 1 1 1 1 1 | |
134 | ------------------------- | |
135 | CFI_intrinsic_type = 0 0 0 0 0 0 0 0 0 0 1 0 | |
136 | Using the intrinsic type and kind shift to find the kind value of the type. | |
137 | CFI_type_kind = (CFI_type_example - CFI_intrinsic_type) >> CFI_type_kind_shift | |
138 | CFI_type_example = 1 0 0 0 0 0 0 0 0 0 1 0 - | |
139 | CFI_intrinsic_type = 0 0 0 0 0 0 0 0 0 0 1 0 | |
140 | ------------------------- | |
141 | 1 0 0 0 0 0 0 0 0 0 0 0 >> CFI_type_kind_shift | |
142 | ------------------------- | |
143 | CFI_type_kind = 0 0 0 0 0 0 0 0 1 0 0 0 | |
144 | */ | |
145 | #define CFI_type_mask 0xFF | |
146 | #define CFI_type_kind_shift 8 | |
147 | ||
148 | /* Intrinsic types. Their kind number defines their storage size. */ | |
149 | #define CFI_type_Integer 1 | |
150 | #define CFI_type_Logical 2 | |
151 | #define CFI_type_Real 3 | |
152 | #define CFI_type_Complex 4 | |
153 | #define CFI_type_Character 5 | |
154 | ||
155 | /* Types with no kind. */ | |
156 | #define CFI_type_struct 6 | |
157 | #define CFI_type_cptr 7 | |
158 | #define CFI_type_cfunptr 8 | |
159 | #define CFI_type_other -1 | |
160 | ||
161 | /* Types with kind parameter. | |
162 | The kind parameter represents the type's byte size. The exception is | |
163 | real kind = 10, which has byte size of 128 bits but 80 bit precision. | |
164 | Complex variables are double the byte size of their real counterparts. | |
165 | The ucs4_char matches wchar_t if sizeof (wchar_t) == 4. | |
166 | */ | |
167 | #define CFI_type_char (CFI_type_Character + (1 << CFI_type_kind_shift)) | |
168 | #define CFI_type_ucs4_char (CFI_type_Character + (4 << CFI_type_kind_shift)) | |
169 | ||
170 | /* C-Fortran Interoperability types. */ | |
171 | #define CFI_type_signed_char (CFI_type_Integer + (sizeof (char) << CFI_type_kind_shift)) | |
172 | #define CFI_type_short (CFI_type_Integer + (sizeof (short) << CFI_type_kind_shift)) | |
173 | #define CFI_type_int (CFI_type_Integer + (sizeof (int) << CFI_type_kind_shift)) | |
174 | #define CFI_type_long (CFI_type_Integer + (sizeof (long) << CFI_type_kind_shift)) | |
175 | #define CFI_type_long_long (CFI_type_Integer + (sizeof (long long) << CFI_type_kind_shift)) | |
176 | #define CFI_type_size_t (CFI_type_Integer + (sizeof (size_t) << CFI_type_kind_shift)) | |
177 | #define CFI_type_int8_t (CFI_type_Integer + (sizeof (int8_t) << CFI_type_kind_shift)) | |
178 | #define CFI_type_int16_t (CFI_type_Integer + (sizeof (int16_t) << CFI_type_kind_shift)) | |
179 | #define CFI_type_int32_t (CFI_type_Integer + (sizeof (int32_t) << CFI_type_kind_shift)) | |
180 | #define CFI_type_int64_t (CFI_type_Integer + (sizeof (int64_t) << CFI_type_kind_shift)) | |
181 | #define CFI_type_int_least8_t (CFI_type_Integer + (sizeof (int_least8_t) << CFI_type_kind_shift)) | |
182 | #define CFI_type_int_least16_t (CFI_type_Integer + (sizeof (int_least16_t) << CFI_type_kind_shift)) | |
183 | #define CFI_type_int_least32_t (CFI_type_Integer + (sizeof (int_least32_t) << CFI_type_kind_shift)) | |
184 | #define CFI_type_int_least64_t (CFI_type_Integer + (sizeof (int_least64_t) << CFI_type_kind_shift)) | |
185 | #define CFI_type_int_fast8_t (CFI_type_Integer + (sizeof (int_fast8_t) << CFI_type_kind_shift)) | |
186 | #define CFI_type_int_fast16_t (CFI_type_Integer + (sizeof (int_fast16_t) << CFI_type_kind_shift)) | |
187 | #define CFI_type_int_fast32_t (CFI_type_Integer + (sizeof (int_fast32_t) << CFI_type_kind_shift)) | |
188 | #define CFI_type_int_fast64_t (CFI_type_Integer + (sizeof (int_fast64_t) << CFI_type_kind_shift)) | |
189 | #define CFI_type_intmax_t (CFI_type_Integer + (sizeof (intmax_t) << CFI_type_kind_shift)) | |
190 | #define CFI_type_intptr_t (CFI_type_Integer + (sizeof (intptr_t) << CFI_type_kind_shift)) | |
191 | #define CFI_type_ptrdiff_t (CFI_type_Integer + (sizeof (ptrdiff_t) << CFI_type_kind_shift)) | |
192 | #define CFI_type_Bool (CFI_type_Logical + (sizeof (_Bool) << CFI_type_kind_shift)) | |
193 | #define CFI_type_float (CFI_type_Real + (sizeof (float) << CFI_type_kind_shift)) | |
194 | #define CFI_type_double (CFI_type_Real + (sizeof (double) << CFI_type_kind_shift)) | |
195 | #define CFI_type_float_Complex (CFI_type_Complex + (sizeof (float) << CFI_type_kind_shift)) | |
196 | #define CFI_type_double_Complex (CFI_type_Complex + (sizeof (double) << CFI_type_kind_shift)) | |
197 | ||
198 | /* If GCC supports int128_t on this target, it predefines | |
199 | __SIZEOF_INT128__ to 16. */ | |
200 | #if defined(__SIZEOF_INT128__) | |
201 | #if (__SIZEOF_INT128__ == 16) | |
202 | #define CFI_type_int128_t (CFI_type_Integer + (16 << CFI_type_kind_shift)) | |
203 | #define CFI_type_int_least128_t (CFI_type_Integer + (16 << CFI_type_kind_shift)) | |
204 | #define CFI_type_int_fast128_t (CFI_type_Integer + (16 << CFI_type_kind_shift)) | |
205 | #else | |
206 | #error "Can't determine kind of int128_t" | |
207 | #endif | |
208 | #else | |
209 | #define CFI_type_int128_t -2 | |
210 | #define CFI_type_int_least128_t -2 | |
211 | #define CFI_type_int_fast128_t -2 | |
212 | #endif | |
213 | ||
214 | /* The situation with long double support is more complicated; we need to | |
215 | examine the type in more detail to figure out its kind. | |
216 | GCC and some other compilers predefine the __LDBL* macros; otherwise | |
217 | get the parameters we need from float.h. */ | |
218 | ||
219 | #if (defined (__LDBL_MANT_DIG__) \ | |
220 | && defined (__LDBL_MIN_EXP__) \ | |
221 | && defined (__LDBL_MAX_EXP__) \ | |
222 | && defined (__DBL_MANT_DIG__) \ | |
223 | && defined (__DBL_MIN_EXP__) \ | |
224 | && defined (__DBL_MAX_EXP__)) | |
225 | #define __CFI_LDBL_MANT_DIG__ __LDBL_MANT_DIG__ | |
226 | #define __CFI_LDBL_MIN_EXP__ __LDBL_MIN_EXP__ | |
227 | #define __CFI_LDBL_MAX_EXP__ __LDBL_MAX_EXP__ | |
228 | #define __CFI_DBL_MANT_DIG__ __DBL_MANT_DIG__ | |
229 | #define __CFI_DBL_MIN_EXP__ __DBL_MIN_EXP__ | |
230 | #define __CFI_DBL_MAX_EXP__ __DBL_MAX_EXP__ | |
231 | ||
232 | #else | |
233 | #include <float.h> | |
234 | ||
235 | #if (defined (LDBL_MANT_DIG) \ | |
236 | && defined (LDBL_MIN_EXP) \ | |
237 | && defined (LDBL_MAX_EXP) \ | |
238 | && defined (DBL_MANT_DIG) \ | |
239 | && defined (DBL_MIN_EXP) \ | |
240 | && defined (DBL_MAX_EXP)) | |
241 | #define __CFI_LDBL_MANT_DIG__ LDBL_MANT_DIG | |
242 | #define __CFI_LDBL_MIN_EXP__ LDBL_MIN_EXP | |
243 | #define __CFI_LDBL_MAX_EXP__ LDBL_MAX_EXP | |
244 | #define __CFI_DBL_MANT_DIG__ DBL_MANT_DIG | |
245 | #define __CFI_DBL_MIN_EXP__ DBL_MIN_EXP | |
246 | #define __CFI_DBL_MAX_EXP__ DBL_MAX_EXP | |
247 | ||
248 | #else | |
249 | #define CFI_no_long_double 1 | |
250 | ||
251 | #endif /* Definitions from float.h. */ | |
252 | #endif /* Definitions from compiler builtins. */ | |
253 | ||
254 | /* Can't determine anything about long double support? */ | |
255 | #if (defined (CFI_no_long_double)) | |
256 | #define CFI_type_long_double -2 | |
257 | #define CFI_type_long_double_Complex -2 | |
258 | ||
259 | /* Long double is the same kind as double. */ | |
260 | #elif (__CFI_LDBL_MANT_DIG__ == __CFI_DBL_MANT_DIG__ \ | |
261 | && __CFI_LDBL_MIN_EXP__ == __CFI_DBL_MIN_EXP__ \ | |
262 | && __CFI_LDBL_MAX_EXP__ == __CFI_DBL_MAX_EXP__) | |
263 | #define CFI_type_long_double CFI_type_double | |
264 | #define CFI_type_long_double_Complex CFI_type_double_Complex | |
265 | ||
266 | /* This is the 80-bit encoding on x86; Fortran assigns it kind 10. */ | |
267 | #elif ((__CFI_LDBL_MANT_DIG__ == 64 || __CFI_LDBL_MANT_DIG__ == 53) \ | |
268 | && __CFI_LDBL_MIN_EXP__ == -16381 \ | |
269 | && __CFI_LDBL_MAX_EXP__ == 16384) | |
270 | #define CFI_type_long_double (CFI_type_Real + (10 << CFI_type_kind_shift)) | |
271 | #define CFI_type_long_double_Complex (CFI_type_Complex + (10 << CFI_type_kind_shift)) | |
272 | ||
273 | /* This is the 96-bit encoding on m68k; Fortran assigns it kind 10. */ | |
274 | #elif (__CFI_LDBL_MANT_DIG__ == 64 \ | |
275 | && __CFI_LDBL_MIN_EXP__ == -16382 \ | |
276 | && __CFI_LDBL_MAX_EXP__ == 16384) | |
277 | #define CFI_type_long_double (CFI_type_Real + (10 << CFI_type_kind_shift)) | |
278 | #define CFI_type_long_double_Complex (CFI_type_Complex + (10 << CFI_type_kind_shift)) | |
279 | ||
280 | /* This is the IEEE 128-bit encoding, same as _Float128. */ | |
281 | #elif (__CFI_LDBL_MANT_DIG__ == 113 \ | |
282 | && __CFI_LDBL_MIN_EXP__ == -16381 \ | |
283 | && __CFI_LDBL_MAX_EXP__ == 16384) | |
284 | #define CFI_type_long_double (CFI_type_Real + (16 << CFI_type_kind_shift)) | |
285 | #define CFI_type_long_double_Complex (CFI_type_Complex + (16 << CFI_type_kind_shift)) | |
286 | ||
287 | /* This is the IBM128 encoding used on PowerPC; also assigned kind 16. */ | |
288 | #elif (__CFI_LDBL_MANT_DIG__ == 106 \ | |
289 | && __CFI_LDBL_MIN_EXP__ == -968 \ | |
290 | && __CFI_LDBL_MAX_EXP__ == 1024) | |
291 | #define CFI_type_long_double (CFI_type_Real + (16 << CFI_type_kind_shift)) | |
292 | #define CFI_type_long_double_Complex (CFI_type_Complex + (16 << CFI_type_kind_shift)) | |
293 | #define CFI_no_float128 1 | |
294 | ||
295 | /* It's a bug if we get here. If you've got a target that has some other | |
296 | long double encoding, you need add something here for Fortran to | |
297 | recognize it. */ | |
298 | #else | |
299 | #error "Can't determine kind of long double" | |
300 | #endif | |
301 | ||
302 | /* Similarly for _Float128. This always refers to the IEEE encoding | |
303 | and not some other 128-bit representation, so if we already used | |
304 | kind 16 for a non-IEEE representation, this one must be unsupported | |
305 | in Fortran even if it's available in C. */ | |
306 | #if (!defined (CFI_no_float128) \ | |
307 | && defined(__FLT128_MANT_DIG__) && __FLT128_MANT_DIG__ == 113 \ | |
308 | && defined(__FLT128_MIN_EXP__) && __FLT128_MIN_EXP__ == -16381 \ | |
309 | && defined(__FLT128_MAX_EXP__) && __FLT128_MAX_EXP__ == 16384) | |
310 | #define CFI_type_float128 (CFI_type_Real + (16 << CFI_type_kind_shift)) | |
311 | #define CFI_type_float128_Complex (CFI_type_Complex + (16 << CFI_type_kind_shift)) | |
312 | #else | |
313 | #define CFI_type_float128 -2 | |
314 | #define CFI_type_float128_Complex -2 | |
315 | #endif | |
316 | ||
317 | #ifdef __cplusplus | |
318 | } | |
319 | #endif | |
320 | ||
321 | #endif /* ISO_FORTRAN_BINDING_H */ |