OSDN Git Service

* libgfortran.h (itoa): Rename to gfc_itoa.
[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 #include "c99_protos.h"
36
37 #if HAVE_COMPLEX_H
38 # include <complex.h>
39 #else
40 #define complex __complex__
41 #endif
42
43 #if HAVE_IEEEFP_H
44 #include <ieeefp.h>
45 #endif
46
47 #if HAVE_STDINT_H
48 #include <stdint.h>
49 #endif
50
51 #if HAVE_INTTYPES_H
52 #include <inttypes.h>
53 #endif
54
55 #if !defined(HAVE_STDINT_H) && !defined(HAVE_INTTYPES_H) && defined(TARGET_ILP32)
56 typedef char int8_t;
57 typedef short int16_t;
58 typedef int int32_t;
59 typedef long long int64_t;
60 typedef unsigned char uint8_t;
61 typedef unsigned short uint16_t;
62 typedef unsigned int uint32_t;
63 typedef unsigned long long uint64_t;
64 #endif
65
66 #if HAVE_SYS_TYPES_H
67 #include <sys/types.h>
68 #endif
69 typedef off_t gfc_offset;
70
71 #ifndef NULL
72 #define NULL (void *) 0
73 #endif
74
75 #ifndef __GNUC__
76 #define __attribute__(x)
77 #endif
78
79 /* For a library, a standard prefix is a requirement in order to partition
80    the namespace.  IPREFIX is for symbols intended to be internal to the
81    library.  */
82 #define PREFIX(x)       _gfortran_ ## x
83 #define IPREFIX(x)      _gfortrani_ ## x
84
85 /* Magic to rename a symbol at the compiler level.  You continue to refer
86    to the symbol as OLD in the source, but it'll be named NEW in the asm.  */
87 #define sym_rename(old, new) sym_rename1(old, __USER_LABEL_PREFIX__, new)
88 #define sym_rename1(old, ulp, new) sym_rename2(old, ulp, new)
89 #define sym_rename2(old, ulp, new) extern __typeof(old) old __asm__(#ulp #new)
90
91 /* There are several classifications of routines:
92
93      (1) Symbols used only within the library,
94      (2) Symbols to be exported from the library,
95      (3) Symbols to be exported from the library, but
96          also used inside the library.
97
98    By telling the compiler about these different classifications we can
99    tightly control the interface seen by the user, and get better code
100    from the compiler at the same time.
101
102    One of the following should be used immediately after the declaration
103    of each symbol:
104
105      internal_proto     Marks a symbol used only within the library,
106                         and adds IPREFIX to the assembly-level symbol
107                         name.  The later is important for maintaining
108                         the namespace partition for the static library.
109
110      export_proto       Marks a symbol to be exported, and adds PREFIX
111                         to the assembly-level symbol name.
112
113      export_proto_np    Marks a symbol to be exported without adding PREFIX.
114
115      iexport_proto      Marks a function to be exported, but with the 
116                         understanding that it can be used inside as well.
117
118      iexport_data_proto Similarly, marks a data symbol to be exported.
119                         Unfortunately, some systems can't play the hidden
120                         symbol renaming trick on data symbols, thanks to
121                         the horribleness of COPY relocations.
122
123    If iexport_proto or iexport_data_proto is used, you must also use
124    iexport or iexport_data after the *definition* of the symbol.  */
125
126 #if defined(HAVE_ATTRIBUTE_VISIBILITY)
127 # define internal_proto(x) \
128         sym_rename(x, IPREFIX (x)) __attribute__((__visibility__("hidden")))
129 #else
130 # define internal_proto(x)      sym_rename(x, IPREFIX(x))
131 #endif
132
133 #if defined(HAVE_ATTRIBUTE_VISIBILITY) && defined(HAVE_ATTRIBUTE_ALIAS)
134 # define export_proto(x)        sym_rename(x, PREFIX(x))
135 # define export_proto_np(x)     extern char swallow_semicolon
136 # define iexport_proto(x)       internal_proto(x)
137 # define iexport(x)             iexport1(x, __USER_LABEL_PREFIX__, IPREFIX(x))
138 # define iexport1(x,p,y)        iexport2(x,p,y)
139 # define iexport2(x,p,y) \
140         extern __typeof(x) PREFIX(x) __attribute__((__alias__(#p #y)))
141 /* ??? We're not currently building a dll, and it's wrong to add dllexport
142    to objects going into a static library archive.  */
143 #elif 0 && defined(HAVE_ATTRIBUTE_DLLEXPORT)
144 # define export_proto_np(x)     extern __typeof(x) x __attribute__((dllexport))
145 # define export_proto(x)    sym_rename(x, PREFIX(x)) __attribute__((dllexport))
146 # define iexport_proto(x)       export_proto(x)
147 # define iexport(x)             extern char swallow_semicolon
148 #else
149 # define export_proto(x)        sym_rename(x, PREFIX(x))
150 # define export_proto_np(x)     extern char swallow_semicolon
151 # define iexport_proto(x)       export_proto(x)
152 # define iexport(x)             extern char swallow_semicolon
153 #endif
154
155 /* TODO: detect the case when we *can* hide the symbol.  */
156 #define iexport_data_proto(x)   export_proto(x)
157 #define iexport_data(x)         extern char swallow_semicolon
158
159 /* The only reliable way to get the offset of a field in a struct
160    in a system independent way is via this macro.  */
161 #ifndef offsetof
162 #define offsetof(TYPE, MEMBER)  ((size_t) &((TYPE *) 0)->MEMBER)
163 #endif
164
165 /* The isfinite macro is only available with C99, but some non-C99
166    systems still provide fpclassify, and there is a `finite' function
167    in BSD.  When isfinite is not available, try to use one of the
168    alternatives, or bail out.  */
169 #if !defined(isfinite)
170 static inline int
171 isfinite (double x)
172 {
173 #if defined(fpclassify)
174   return (fpclassify(x) != FP_NAN && fpclassify(x) != FP_INFINITE);
175 #elif defined(HAVE_FINITE)
176   return finite (x);
177 #else
178 #error "libgfortran needs isfinite, fpclassify, or finite"
179 #endif
180 }
181 #endif /* !defined(isfinite)  */
182
183 /* TODO: find the C99 version of these an move into above ifdef.  */
184 #define REALPART(z) (__real__(z))
185 #define IMAGPART(z) (__imag__(z))
186 #define COMPLEX_ASSIGN(z_, r_, i_) {__real__(z_) = (r_); __imag__(z_) = (i_);}
187
188 typedef int8_t GFC_INTEGER_1;
189 typedef int16_t GFC_INTEGER_2;
190 typedef int32_t GFC_INTEGER_4;
191 typedef int64_t GFC_INTEGER_8;
192 typedef uint32_t GFC_UINTEGER_4;
193 typedef uint64_t GFC_UINTEGER_8;
194 typedef GFC_INTEGER_4 GFC_LOGICAL_4;
195 typedef GFC_INTEGER_8 GFC_LOGICAL_8;
196 typedef float GFC_REAL_4;
197 typedef double GFC_REAL_8;
198 typedef complex float GFC_COMPLEX_4;
199 typedef complex double GFC_COMPLEX_8;
200
201 /* The following two definitions must be consistent with the types used
202    by the compiler.  */
203 /* The type used of array indices, amongst other things.  */
204 typedef size_t index_type;
205 /* The type used for the lengths of character variables.  */
206 typedef GFC_INTEGER_4 gfc_charlen_type;
207
208 /* This will be 0 on little-endian machines and one on big-endian machines.  */
209 extern int l8_to_l4_offset;
210 internal_proto(l8_to_l4_offset);
211
212 #define GFOR_POINTER_L8_TO_L4(p8) \
213   (l8_to_l4_offset + (GFC_LOGICAL_4 *)(p8))
214
215 #define GFC_INTEGER_4_HUGE \
216   (GFC_INTEGER_4)((((GFC_UINTEGER_4)1) << 31) - 1)
217 #define GFC_INTEGER_8_HUGE \
218   (GFC_INTEGER_8)((((GFC_UINTEGER_8)1) << 63) - 1)
219 #define GFC_REAL_4_HUGE FLT_MAX
220 #define GFC_REAL_8_HUGE DBL_MAX
221
222 #ifndef GFC_MAX_DIMENSIONS
223 #define GFC_MAX_DIMENSIONS 7
224 #endif
225
226 typedef struct descriptor_dimension
227 {
228   index_type stride;
229   index_type lbound;
230   index_type ubound;
231 }
232 descriptor_dimension;
233
234 #define GFC_ARRAY_DESCRIPTOR(r, type) \
235 struct {\
236   type *data;\
237   type *base;\
238   index_type dtype;\
239   descriptor_dimension dim[r];\
240 }
241
242 /* Commonly used array descriptor types.  */
243 typedef GFC_ARRAY_DESCRIPTOR (GFC_MAX_DIMENSIONS, void) gfc_array_void;
244 typedef GFC_ARRAY_DESCRIPTOR (GFC_MAX_DIMENSIONS, char) gfc_array_char;
245 typedef GFC_ARRAY_DESCRIPTOR (GFC_MAX_DIMENSIONS, GFC_INTEGER_4) gfc_array_i4;
246 typedef GFC_ARRAY_DESCRIPTOR (GFC_MAX_DIMENSIONS, GFC_INTEGER_8) gfc_array_i8;
247 typedef GFC_ARRAY_DESCRIPTOR (GFC_MAX_DIMENSIONS, GFC_REAL_4) gfc_array_r4;
248 typedef GFC_ARRAY_DESCRIPTOR (GFC_MAX_DIMENSIONS, GFC_REAL_8) gfc_array_r8;
249 typedef GFC_ARRAY_DESCRIPTOR (GFC_MAX_DIMENSIONS, GFC_COMPLEX_4) gfc_array_c4;
250 typedef GFC_ARRAY_DESCRIPTOR (GFC_MAX_DIMENSIONS, GFC_COMPLEX_8) gfc_array_c8;
251 typedef GFC_ARRAY_DESCRIPTOR (GFC_MAX_DIMENSIONS, GFC_LOGICAL_4) gfc_array_l4;
252 typedef GFC_ARRAY_DESCRIPTOR (GFC_MAX_DIMENSIONS, GFC_LOGICAL_8) gfc_array_l8;
253
254 #define GFC_DTYPE_RANK_MASK 0x07
255 #define GFC_DTYPE_TYPE_SHIFT 3
256 #define GFC_DTYPE_TYPE_MASK 0x38
257 #define GFC_DTYPE_SIZE_SHIFT 6
258
259 enum
260 {
261   GFC_DTYPE_UNKNOWN = 0,
262   GFC_DTYPE_INTEGER,
263   /* TODO: recognize logical types.  */
264   GFC_DTYPE_LOGICAL,
265   GFC_DTYPE_REAL,
266   GFC_DTYPE_COMPLEX,
267   GFC_DTYPE_DERIVED,
268   GFC_DTYPE_CHARACTER
269 };
270
271 #define GFC_DESCRIPTOR_RANK(desc) ((desc)->dtype & GFC_DTYPE_RANK_MASK)
272 #define GFC_DESCRIPTOR_TYPE(desc) (((desc)->dtype & GFC_DTYPE_TYPE_MASK) \
273                                    >> GFC_DTYPE_TYPE_SHIFT)
274 #define GFC_DESCRIPTOR_SIZE(desc) ((desc)->dtype >> GFC_DTYPE_SIZE_SHIFT)
275 #define GFC_DESCRIPTOR_DATA(desc) ((desc)->data)
276 #define GFC_DESCRIPTOR_DTYPE(desc) ((desc)->dtype)
277
278 /* Runtime library include.  */
279 #define stringize(x) expand_macro(x)
280 #define expand_macro(x) # x
281
282 /* Runtime options structure.  */
283
284 typedef struct
285 {
286   int stdin_unit, stdout_unit, optional_plus;
287   int allocate_init_flag, allocate_init_value;
288   int locus;
289
290   int separator_len;
291   const char *separator;
292
293   int mem_check;
294   int use_stderr, all_unbuffered, default_recl;
295
296   int fpu_round, fpu_precision, fpu_invalid, fpu_denormal, fpu_zerodiv,
297     fpu_overflow, fpu_underflow, fpu_precision_loss;
298
299   int sighup, sigint;
300 }
301 options_t;
302
303
304 extern options_t options;
305 internal_proto(options);
306
307
308 /* Structure for statement options.  */
309
310 typedef struct
311 {
312   const char *name;
313   int value;
314 }
315 st_option;
316
317 /* Runtime errors.  The EOR and EOF errors are required to be negative.  */
318
319 typedef enum
320 {
321   ERROR_FIRST = -3,             /* Marker for the first error.  */
322   ERROR_EOR = -2,
323   ERROR_END = -1,
324   ERROR_OK = 0,                 /* Indicates success, must be zero.  */
325   ERROR_OS,                     /* Operating system error, more info in errno.  */
326   ERROR_OPTION_CONFLICT,
327   ERROR_BAD_OPTION,
328   ERROR_MISSING_OPTION,
329   ERROR_ALREADY_OPEN,
330   ERROR_BAD_UNIT,
331   ERROR_FORMAT,
332   ERROR_BAD_ACTION,
333   ERROR_ENDFILE,
334   ERROR_BAD_US,
335   ERROR_READ_VALUE,
336   ERROR_READ_OVERFLOW,
337   ERROR_LAST                    /* Not a real error, the last error # + 1.  */
338 }
339 error_codes;
340
341
342 /* The filename and line number don't go inside the globals structure.
343    They are set by the rest of the program and must be linked to.  */
344
345 /* Location of the current library call (optional).  */
346 extern unsigned line;
347 iexport_data_proto(line);
348
349 extern char *filename;
350 iexport_data_proto(filename);
351
352 /* Avoid conflicting prototypes of alloca() in system headers by using 
353    GCC's builtin alloca().  */
354 #define gfc_alloca(x)  __builtin_alloca(x)
355
356
357 /* main.c */
358
359 extern void library_start (void);
360 internal_proto(library_start);
361
362 extern void library_end (void);
363 internal_proto(library_end);
364
365 extern void set_args (int, char **);
366 export_proto(set_args);
367
368 extern void get_args (int *, char ***);
369 internal_proto(get_args);
370
371 /* error.c */
372
373 extern char *gfc_itoa (int64_t);
374 internal_proto(gfc_itoa);
375
376 extern char *xtoa (uint64_t);
377 internal_proto(xtoa);
378
379 extern void os_error (const char *) __attribute__ ((noreturn));
380 internal_proto(os_error);
381
382 extern void show_locus (void);
383 internal_proto(show_locus);
384
385 extern void runtime_error (const char *) __attribute__ ((noreturn));
386 iexport_proto(runtime_error);
387
388 extern void internal_error (const char *) __attribute__ ((noreturn));
389 internal_proto(internal_error);
390
391 extern const char *get_oserror (void);
392 internal_proto(get_oserror);
393
394 extern void sys_exit (int) __attribute__ ((noreturn));
395 internal_proto(sys_exit);
396
397 extern int st_printf (const char *, ...)
398   __attribute__ ((format (printf, 1, 2)));
399 internal_proto(st_printf);
400
401 extern void st_sprintf (char *, const char *, ...)
402   __attribute__ ((format (printf, 2, 3)));
403 internal_proto(st_sprintf);
404
405 extern const char *translate_error (int);
406 internal_proto(translate_error);
407
408 extern void generate_error (int, const char *);
409 internal_proto(generate_error);
410
411 /* memory.c */
412
413 extern void *get_mem (size_t) __attribute__ ((malloc));
414 internal_proto(get_mem);
415
416 extern void free_mem (void *);
417 internal_proto(free_mem);
418
419 extern void *internal_malloc_size (size_t);
420 internal_proto(internal_malloc_size);
421
422 extern void internal_free (void *);
423 iexport_proto(internal_free);
424
425 /* environ.c */
426
427 extern int check_buffered (int);
428 internal_proto(check_buffered);
429
430 extern void init_variables (void);
431 internal_proto(init_variables);
432
433 extern void show_variables (void);
434 internal_proto(show_variables);
435
436 /* string.c */
437
438 extern int find_option (const char *, int, st_option *, const char *);
439 internal_proto(find_option);
440
441 extern int fstrlen (const char *, int);
442 internal_proto(fstrlen);
443
444 extern void fstrcpy (char *, int, const char *, int);
445 internal_proto(fstrcpy);
446
447 extern void cf_strcpy (char *, int, const char *);
448 internal_proto(cf_strcpy);
449
450 /* io.c */
451
452 extern void init_units (void);
453 internal_proto(init_units);
454
455 extern void close_units (void);
456 internal_proto(close_units);
457
458 /* stop.c */
459
460 extern void stop_numeric (GFC_INTEGER_4);
461 iexport_proto(stop_numeric);
462
463 /* reshape_packed.c */
464
465 extern void reshape_packed (char *, index_type, const char *, index_type,
466                             const char *, index_type);
467 internal_proto(reshape_packed);
468
469 /* Repacking functions.  */
470
471 /* ??? These four aren't currently used by the compiler, though we
472    certainly could do so.  */
473 GFC_INTEGER_4 *internal_pack_4 (gfc_array_i4 *);
474 internal_proto(internal_pack_4);
475
476 GFC_INTEGER_8 *internal_pack_8 (gfc_array_i8 *);
477 internal_proto(internal_pack_8);
478
479 extern void internal_unpack_4 (gfc_array_i4 *, const GFC_INTEGER_4 *);
480 internal_proto(internal_unpack_4);
481
482 extern void internal_unpack_8 (gfc_array_i8 *, const GFC_INTEGER_8 *);
483 internal_proto(internal_unpack_8);
484
485 /* string_intrinsics.c */
486
487 extern GFC_INTEGER_4 compare_string (GFC_INTEGER_4, const char *,
488                                      GFC_INTEGER_4, const char *);
489 iexport_proto(compare_string);
490
491 /* random.c */
492
493 extern void random_seed (GFC_INTEGER_4 * size, gfc_array_i4 * put,
494                          gfc_array_i4 * get);
495 iexport_proto(random_seed);
496
497 /* normalize.c */
498
499 extern GFC_REAL_4 normalize_r4_i4 (GFC_UINTEGER_4, GFC_UINTEGER_4);
500 internal_proto(normalize_r4_i4);
501
502 extern GFC_REAL_8 normalize_r8_i8 (GFC_UINTEGER_8, GFC_UINTEGER_8);
503 internal_proto(normalize_r8_i8);
504
505 /* size.c */
506
507 typedef GFC_ARRAY_DESCRIPTOR (GFC_MAX_DIMENSIONS, void) array_t;
508
509 extern index_type size0 (const array_t * array); 
510 iexport_proto(size0);
511
512 #endif  /* LIBGFOR_H  */