OSDN Git Service

2004-12-01 Aaron W. LaFramboise <aaronavay62@aaronwl.com>
[pf3gnuchains/gcc-fork.git] / libgfortran / libgfortran.h
1 /* Common declarations for all of libgfor.
2    Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
3    Contributed by Paul Brook <paul@nowt.org>, and
4    Andy Vaught <andy@xena.eas.asu.edu>
5
6 This file is part of the GNU Fortran 95 runtime library (libgfor).
7
8 Libgfor is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 Libgfor is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with libgfor; see the file COPYING.LIB.  If not,
20 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.  */
22
23
24 #ifndef LIBGFOR_H
25 #define LIBGFOR_H
26
27 #include <math.h>
28 #include <stddef.h>
29
30 #ifndef M_PI
31 #define M_PI 3.14159265358979323846264338327
32 #endif
33
34 #include "config.h"
35
36 #if HAVE_COMPLEX_H
37 # include <complex.h>
38 #else
39 #define complex __complex__
40 #endif
41
42 #if HAVE_STDINT_H
43 #include <stdint.h>
44 #endif
45
46 #if HAVE_INTTYPES_H
47 #include <inttypes.h>
48 #endif
49
50 #if !defined(HAVE_STDINT_H) && !defined(HAVE_INTTYPES_H) && defined(TARGET_ILP32)
51 typedef char int8_t;
52 typedef short int16_t;
53 typedef int int32_t;
54 typedef long long int64_t;
55 typedef unsigned char uint8_t;
56 typedef unsigned short uint16_t;
57 typedef unsigned int uint32_t;
58 typedef unsigned long long uint64_t;
59 #endif
60
61 #if HAVE_SYS_TYPES_H
62 #include <sys/types.h>
63 #endif
64 typedef off_t gfc_offset;
65
66 #ifndef NULL
67 #define NULL (void *) 0
68 #endif
69
70 #ifndef __GNUC__
71 #define __attribute__(x)
72 #endif
73
74 /* For a library, a standard prefix is a requirement in order to
75    partition the namespace.  It's ugly to look at and a pain to type,
76    so we hide it behind macros.  */
77 #define prefix(x) _gfortran_ ## x
78
79 /* The only reliable way to get the offset of a field in a struct
80    in a system independent way is via this macro.  */
81 #ifndef offsetof
82 #define offsetof(TYPE, MEMBER)  ((size_t) &((TYPE *) 0)->MEMBER)
83 #endif
84
85 /* The isfinite macro is only available with C99, but some non-C99
86    systems still provide fpclassify, and there is a `finite' function
87    in BSD.  When isfinite is not available, try to use one of the
88    alternatives, or bail out.  */
89 #if !defined(isfinite)
90 static inline int
91 isfinite (double x)
92 {
93 #if defined(fpclassify)
94   return (fpclassify(x) != FP_NAN && fpclassify(x) != FP_INFINITE);
95 #elif defined(HAVE_FINITE)
96   return finite (x);
97 #else
98 #error "libgfortran needs isfinite, fpclassify, or finite"
99 #endif
100 }
101 #endif /* !defined(isfinite)  */
102
103 /* TODO: find the C99 version of these an move into above ifdef.  */
104 #define REALPART(z) (__real__(z))
105 #define IMAGPART(z) (__imag__(z))
106 #define COMPLEX_ASSIGN(z_, r_, i_) {__real__(z_) = (r_); __imag__(z_) = (i_);}
107
108 typedef int32_t GFC_INTEGER_4;
109 typedef int64_t GFC_INTEGER_8;
110 typedef uint32_t GFC_UINTEGER_4;
111 typedef uint64_t GFC_UINTEGER_8;
112 typedef GFC_INTEGER_4 GFC_LOGICAL_4;
113 typedef GFC_INTEGER_8 GFC_LOGICAL_8;
114 typedef float GFC_REAL_4;
115 typedef double GFC_REAL_8;
116 typedef complex float GFC_COMPLEX_4;
117 typedef complex double GFC_COMPLEX_8;
118
119 /* The following two definitions must be consistent with the types used
120    by the compiler.  */
121 /* The type used of array indices, amongst other things.  */
122 typedef size_t index_type;
123 /* The type used for the lengths of character variables.  */
124 typedef GFC_INTEGER_4 gfc_charlen_type;
125
126 /* This will be 0 on little-endian machines and one on big-endian machines.  */
127 #define l8_to_l4_offset prefix(l8_to_l4_offset)
128 extern int l8_to_l4_offset;
129
130 #define GFOR_POINTER_L8_TO_L4(p8) \
131   (l8_to_l4_offset + (GFC_LOGICAL_4 *)(p8))
132
133 #define GFC_INTEGER_4_HUGE \
134   (GFC_INTEGER_4)((((GFC_UINTEGER_4)1) << 31) - 1)
135 #define GFC_INTEGER_8_HUGE \
136   (GFC_INTEGER_8)((((GFC_UINTEGER_8)1) << 63) - 1)
137 #define GFC_REAL_4_HUGE FLT_MAX
138 #define GFC_REAL_8_HUGE DBL_MAX
139
140 #ifndef GFC_MAX_DIMENSIONS
141 #define GFC_MAX_DIMENSIONS 7
142 #endif
143
144 typedef struct descriptor_dimension
145 {
146   index_type stride;
147   index_type lbound;
148   index_type ubound;
149 }
150 descriptor_dimension;
151
152 #define GFC_ARRAY_DESCRIPTOR(r, type) \
153 struct {\
154   type *data;\
155   type *base;\
156   index_type dtype;\
157   descriptor_dimension dim[r];\
158 }
159
160 /* Commonly used array descriptor types.  */
161 typedef GFC_ARRAY_DESCRIPTOR (GFC_MAX_DIMENSIONS, void) gfc_array_void;
162 typedef GFC_ARRAY_DESCRIPTOR (GFC_MAX_DIMENSIONS, char) gfc_array_char;
163 typedef GFC_ARRAY_DESCRIPTOR (GFC_MAX_DIMENSIONS, GFC_INTEGER_4) gfc_array_i4;
164 typedef GFC_ARRAY_DESCRIPTOR (GFC_MAX_DIMENSIONS, GFC_INTEGER_8) gfc_array_i8;
165 typedef GFC_ARRAY_DESCRIPTOR (GFC_MAX_DIMENSIONS, GFC_REAL_4) gfc_array_r4;
166 typedef GFC_ARRAY_DESCRIPTOR (GFC_MAX_DIMENSIONS, GFC_REAL_8) gfc_array_r8;
167 typedef GFC_ARRAY_DESCRIPTOR (GFC_MAX_DIMENSIONS, GFC_COMPLEX_4) gfc_array_c4;
168 typedef GFC_ARRAY_DESCRIPTOR (GFC_MAX_DIMENSIONS, GFC_COMPLEX_8) gfc_array_c8;
169 typedef GFC_ARRAY_DESCRIPTOR (GFC_MAX_DIMENSIONS, GFC_LOGICAL_4) gfc_array_l4;
170 typedef GFC_ARRAY_DESCRIPTOR (GFC_MAX_DIMENSIONS, GFC_LOGICAL_8) gfc_array_l8;
171
172 #define GFC_DTYPE_RANK_MASK 0x07
173 #define GFC_DTYPE_TYPE_SHIFT 3
174 #define GFC_DTYPE_TYPE_MASK 0x38
175 #define GFC_DTYPE_SIZE_SHIFT 6
176
177 enum
178 {
179   GFC_DTYPE_UNKNOWN = 0,
180   GFC_DTYPE_INTEGER,
181   /* TODO: recognize logical types.  */
182   GFC_DTYPE_LOGICAL,
183   GFC_DTYPE_REAL,
184   GFC_DTYPE_COMPLEX,
185   GFC_DTYPE_DERIVED,
186   GFC_DTYPE_CHARACTER
187 };
188
189 #define GFC_DESCRIPTOR_RANK(desc) ((desc)->dtype & GFC_DTYPE_RANK_MASK)
190 #define GFC_DESCRIPTOR_TYPE(desc) (((desc)->dtype & GFC_DTYPE_TYPE_MASK) \
191                                    >> GFC_DTYPE_TYPE_SHIFT)
192 #define GFC_DESCRIPTOR_SIZE(desc) ((desc)->dtype >> GFC_DTYPE_SIZE_SHIFT)
193 #define GFC_DESCRIPTOR_DATA(desc) ((desc)->data)
194 #define GFC_DESCRIPTOR_DTYPE(desc) ((desc)->dtype)
195
196 /* Runtime library include.  */
197 #define stringize(x) expand_macro(x)
198 #define expand_macro(x) # x
199
200 /* Runtime options structure.  */
201
202 typedef struct
203 {
204   int stdin_unit, stdout_unit, optional_plus;
205   int allocate_init_flag, allocate_init_value;
206   int locus;
207
208   int separator_len;
209   const char *separator;
210
211   int mem_check;
212   int use_stderr, all_unbuffered, default_recl;
213
214   int fpu_round, fpu_precision, fpu_invalid, fpu_denormal, fpu_zerodiv,
215     fpu_overflow, fpu_underflow, fpu_precision_loss;
216
217   int sighup, sigint;
218 }
219 options_t;
220
221
222 #define options prefix(options)
223 extern options_t options;
224
225
226 /* Structure for statement options.  */
227
228 typedef struct
229 {
230   const char *name;
231   int value;
232 }
233 st_option;
234
235 /* Runtime errors.  The EOR and EOF errors are required to be negative.  */
236
237 typedef enum
238 {
239   ERROR_FIRST = -3,             /* Marker for the first error.  */
240   ERROR_EOR = -2,
241   ERROR_END = -1,
242   ERROR_OK = 0,                 /* Indicates success, must be zero.  */
243   ERROR_OS,                     /* Operating system error, more info in errno.  */
244   ERROR_OPTION_CONFLICT,
245   ERROR_BAD_OPTION,
246   ERROR_MISSING_OPTION,
247   ERROR_ALREADY_OPEN,
248   ERROR_BAD_UNIT,
249   ERROR_FORMAT,
250   ERROR_BAD_ACTION,
251   ERROR_ENDFILE,
252   ERROR_BAD_US,
253   ERROR_READ_VALUE,
254   ERROR_READ_OVERFLOW,
255   ERROR_LAST                    /* Not a real error, the last error # + 1.  */
256 }
257 error_codes;
258
259
260 /* The filename and line number don't go inside the globals structure.
261    They are set by the rest of the program and must be linked to.  */
262
263 #define line prefix(line)
264 extern unsigned line;           /* Location of the current libray call (optional).  */
265
266 #define filename prefix(filename)
267 extern char *filename;
268
269 /* Avoid conflicting prototypes of alloca() in system headers by using 
270    GCC's builtin alloca().  */
271
272 #define gfc_alloca(x)  __builtin_alloca(x)
273
274
275 /* main.c */
276
277 #define library_start prefix(library_start)
278 void library_start (void);
279
280 #define library_end prefix(library_end)
281 void library_end (void);
282
283 #define set_args prefix(set_args)
284 void set_args (int, char **);
285
286 #define get_args prefix(get_args)
287 void get_args (int *, char ***);
288
289
290 /* error.c */
291 #define itoa prefix(itoa)
292 char *itoa (int64_t);
293
294 #define xtoa prefix(xtoa)
295 char *xtoa (uint64_t);
296
297 #define os_error prefix(os_error)
298 void os_error (const char *) __attribute__ ((noreturn));
299
300 #define show_locus prefix(show_locus)
301 void show_locus (void);
302
303 #define runtime_error prefix(runtime_error)
304 void runtime_error (const char *) __attribute__ ((noreturn));
305
306 #define internal_error prefix(internal_error)
307 void internal_error (const char *) __attribute__ ((noreturn));
308
309 #define get_oserror prefix(get_oserror)
310 const char *get_oserror (void);
311
312 #define write_error prefix(write_error)
313 void write_error (const char *);
314
315 #define sys_exit prefix(sys_exit)
316 void sys_exit (int) __attribute__ ((noreturn));
317
318 #define st_printf prefix(st_printf)
319 int st_printf (const char *, ...) __attribute__ ((format (printf, 1, 2)));
320
321 #define st_sprintf prefix(st_sprintf)
322 void st_sprintf (char *, const char *, ...) __attribute__ ((format (printf, 2, 3)));
323
324 #define translate_error prefix(translate_error)
325 const char *translate_error (int);
326
327 #define generate_error prefix(generate_error)
328 void generate_error (int, const char *);
329
330
331 /* memory.c */
332
333 #define memory_init     prefix(memory_init)
334 void memory_init (void);
335
336 #define runtime_cleanup prefix(runtime_cleanup)
337 void runtime_cleanup (void);
338
339 #define get_mem         prefix(get_mem)
340 void *get_mem (size_t) __attribute__ ((malloc));
341
342 #define free_mem        prefix(free_mem)
343 void free_mem (void *);
344
345 #define internal_malloc_size    prefix(internal_malloc_size)
346 void *internal_malloc_size (size_t);
347
348 #define internal_malloc prefix(internal_malloc)
349 void *internal_malloc (GFC_INTEGER_4);
350
351 #define internal_malloc64 prefix(internal_malloc64)
352 void *internal_malloc64 (GFC_INTEGER_8);
353
354 #define internal_free   prefix(internal_free)
355 void internal_free (void *);
356
357 #define allocate        prefix(allocate)
358 void allocate (void **, GFC_INTEGER_4, GFC_INTEGER_4 *);
359
360 #define allocate64      prefix(allocate64)
361 void allocate64 (void **, GFC_INTEGER_8, GFC_INTEGER_4 *);
362
363 #define deallocate      prefix(deallocate)
364 void deallocate (void **, GFC_INTEGER_4 *);
365
366
367 /* environ.c */
368
369 #define check_buffered prefix(check_buffered)
370 int check_buffered (int);
371
372 #define init_variables prefix(init_variables)
373 void init_variables (void);
374
375 #define show_variables prefix(show_variables)
376 void show_variables (void);
377
378
379 /* string.c */
380
381 #define find_option prefix(find_option)
382 int find_option (const char *, int, st_option *, const char *);
383
384 #define fstrlen prefix(fstrlen)
385 int fstrlen (const char *, int);
386
387 #define fstrcpy prefix(fstrcpy)
388 void fstrcpy (char *, int, const char *, int);
389
390 #define cf_strcpy prefix(cf_strcpy)
391 void cf_strcpy (char *, int, const char *);
392
393 /* io.c */
394
395 #define init_units prefix(init_units)
396 void init_units (void);
397
398 #define close_units prefix(close_units)
399 void close_units (void);
400
401 /* stop.c */
402 #define stop_numeric prefix(stop_numeric)
403 void stop_numeric (GFC_INTEGER_4);
404
405 /* reshape_packed.c */
406 #define reshape_packed prefix(reshape_packed)
407 void reshape_packed (char *, index_type, const char *, index_type,
408                      const char *, index_type);
409
410 /* Repacking functions.  */
411 #define internal_pack prefix(internal_pack)
412 void *internal_pack (gfc_array_char *);
413
414 #define internal_unpack prefix(internal_unpack)
415 void internal_unpack (gfc_array_char *, const void *);
416
417 #define internal_pack_4 prefix(internal_pack_4)
418 GFC_INTEGER_4 *internal_pack_4 (gfc_array_i4 *);
419
420 #define internal_pack_8 prefix(internal_pack_8)
421 GFC_INTEGER_8 *internal_pack_8 (gfc_array_i8 *);
422
423 #define internal_unpack_4 prefix(internal_unpack_4)
424 void internal_unpack_4 (gfc_array_i4 *, const GFC_INTEGER_4 *);
425
426 #define internal_unpack_8 prefix(internal_unpack_8)
427 void internal_unpack_8 (gfc_array_i8 *, const GFC_INTEGER_8 *);
428
429 /* date_and_time.c */
430
431 #define date_and_time prefix(date_and_time)
432 void date_and_time (char *, char *, char *, gfc_array_i4 *,
433                    GFC_INTEGER_4, GFC_INTEGER_4, GFC_INTEGER_4);
434
435 /* string_intrinsics.c */
436
437 #define compare_string prefix(compare_string)
438 GFC_INTEGER_4 compare_string (GFC_INTEGER_4, const char *,
439                               GFC_INTEGER_4, const char *);
440
441 /* random.c */
442
443 #define random_seed prefix(random_seed)
444 void random_seed (GFC_INTEGER_4 * size, gfc_array_i4 * put,
445                   gfc_array_i4 * get);
446
447 /* normalize.c */
448
449 #define normalize_r4_i4 prefix(normalize_r4_i4)
450 GFC_REAL_4 normalize_r4_i4 (GFC_UINTEGER_4, GFC_UINTEGER_4);
451
452 #define normalize_r8_i8 prefix(normalize_r8_i8)
453 GFC_REAL_8 normalize_r8_i8 (GFC_UINTEGER_8, GFC_UINTEGER_8);
454
455 /* size.c */
456
457 typedef GFC_ARRAY_DESCRIPTOR (GFC_MAX_DIMENSIONS, void) array_t;
458
459 #define size0 prefix(size0)
460 index_type size0 (const array_t * array); 
461
462 #endif  /* LIBGFOR_H  */
463