GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
f77-fcn.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2013 John W. Eaton
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if !defined (octave_f77_fcn_h)
24 #define octave_f77_fcn_h 1
25 
26 #include "quit.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 /* Hack to stringize macro results. */
33 #define xSTRINGIZE(x) #x
34 #define STRINGIZE(x) xSTRINGIZE(x)
35 
36 /* How to print an error for the F77_XFCN macro. */
37 
38 #define F77_XFCN_ERROR(f, F) \
39  (*current_liboctave_error_handler) \
40  ("exception encountered in Fortran subroutine %s", \
41  STRINGIZE (F77_FUNC (f, F)))
42 
43 /* This can be used to call a Fortran subroutine that might call
44  XSTOPX. XSTOPX will call lonjmp with current_context. Once back
45  here, we'll restore the previous context and return. We may also
46  end up here if an interrupt is processed when the Fortran
47  subroutine is called. In that case, we resotre the context and go
48  to the top level. The error_state should be checked immediately
49  after this macro is used. */
50 
51 #define F77_XFCN(f, F, args) \
52  do \
53  { \
54  octave_jmp_buf saved_context; \
55  sig_atomic_t saved_octave_interrupt_immediately = octave_interrupt_immediately; \
56  f77_exception_encountered = 0; \
57  octave_save_current_context (saved_context); \
58  if (octave_set_current_context) \
59  { \
60  octave_interrupt_immediately = saved_octave_interrupt_immediately; \
61  octave_restore_current_context (saved_context); \
62  if (f77_exception_encountered) \
63  F77_XFCN_ERROR (f, F); \
64  else \
65  octave_rethrow_exception (); \
66  } \
67  else \
68  { \
69  octave_interrupt_immediately++; \
70  F77_FUNC (f, F) args; \
71  octave_interrupt_immediately--; \
72  octave_restore_current_context (saved_context); \
73  } \
74  } \
75  while (0)
76 
77 /* So we can check to see if an exception has occurred. */
78 CRUFT_API extern int f77_exception_encountered;
79 
80 #if !defined (F77_FCN)
81 #define F77_FCN(f, F) F77_FUNC (f, F)
82 #endif
83 
84 /*
85 
86 The following macros are used for handling Fortran <-> C calling
87 conventions. They are defined below for three different types of
88 systems, Cray (possibly now obsolete), Visual Fortran, and any system
89 that is compatible with the f2c calling conventions, including g77 and
90 gfortran. Note that gfortran is not completely compatible with the
91 f2c calling conventions, but that we only use the parts that are
92 compatible. For example, f2c and gfortran differ in the way they
93 handle Fortran functions that return complex values, but Octave does
94 not call any Fortran functions like that directly from C or C++.
95 
96 Use these macros to pass character strings from C to Fortran:
97 
98  F77_CHAR_ARG(x)
99  F77_CONST_CHAR_ARG(x)
100  F77_CXX_STRING_ARG(x)
101  F77_CHAR_ARG_LEN(l)
102  F77_CHAR_ARG_DECL
103  F77_CONST_CHAR_ARG_DECL
104  F77_CHAR_ARG_LEN_DECL
105 
106 Use these macros to write C-language functions that accept
107 Fortran-style character strings:
108 
109  F77_CHAR_ARG_DEF(s, len)
110  F77_CONST_CHAR_ARG_DEF(s, len)
111  F77_CHAR_ARG_LEN_DEF(len)
112  F77_CHAR_ARG_USE(s)
113  F77_CHAR_ARG_LEN_USE(s, len)
114 
115 Use this macro to declare the return type of a C-language function
116 that is supposed to act like a Fortran subroutine:
117 
118  F77_RET_T int
119 
120 Use these macros to return from C-language functions that are supposed
121 to act like Fortran subroutines. F77_NORETURN is intended to be used
122 as the last statement of such a function that has been tagged with a
123 "noreturn" attribute. If the compiler supports the "noreturn"
124 attribute or if F77_RET_T is void, then it should expand to nothing so
125 that we avoid warnings about functions tagged as "noreturn"
126 containing a return statement. Otherwise, it should expand to a
127 statement that returns the given value so that we avoid warnings about
128 not returning a value from a function declared to return something.
129 
130  F77_RETURN(retval)
131  F77_NORETURN(retval)
132 
133 */
134 
135 #if defined (F77_USES_CRAY_CALLING_CONVENTION)
136 
137 #include <fortran.h>
138 
139 /* Use these macros to pass character strings from C to Fortran. */
140 #define F77_CHAR_ARG(x) octave_make_cray_ftn_ch_dsc (x, strlen (x))
141 #define F77_CONST_CHAR_ARG(x) \
142  octave_make_cray_const_ftn_ch_dsc (x, strlen (x))
143 #define F77_CHAR_ARG2(x, l) octave_make_cray_ftn_ch_dsc (x, l)
144 #define F77_CONST_CHAR_ARG2(x, l) octave_make_cray_const_ftn_ch_dsc (x, l)
145 #define F77_CXX_STRING_ARG(x) \
146  octave_make_cray_const_ftn_ch_dsc (x.c_str (), x.length ())
147 #define F77_CHAR_ARG_LEN(l)
148 #define F77_CHAR_ARG_DECL octave_cray_ftn_ch_dsc
149 #define F77_CONST_CHAR_ARG_DECL octave_cray_ftn_ch_dsc
150 #define F77_CHAR_ARG_LEN_DECL
151 
152 /* Use these macros to write C-language functions that accept
153  Fortran-style character strings. */
154 #define F77_CHAR_ARG_DEF(s, len) octave_cray_ftn_ch_dsc s
155 #define F77_CONST_CHAR_ARG_DEF(s, len) octave_cray_ftn_ch_dsc s
156 #define F77_CHAR_ARG_LEN_DEF(len)
157 #define F77_CHAR_ARG_USE(s) s.ptr
158 #define F77_CHAR_ARG_LEN_USE(s, len) (s.mask.len>>3)
159 
160 /* Use this macro to declare the return type of a C-language function
161  that is supposed to act like a Fortran subroutine. */
162 #define F77_RET_T int
163 
164 /* Use these macros to return from C-language functions that are
165  supposed to act like Fortran subroutines. F77_NORETURN is intended
166  to be used as the last statement of such a function that has been
167  tagged with a "noreturn" attribute. */
168 #define F77_RETURN(retval) return retval;
169 #if defined (HAVE_ATTR_NORETURN)
170 #define F77_NORETURN(retval)
171 #else
172 #define F77_NORETURN(retval) return retval;
173 #endif
174 
175 /* FIXME -- these should work for SV1 or Y-MP systems but will
176  need to be changed for others. */
177 
178 typedef union
179 {
180  const char *const_ptr;
181  char *ptr;
182  struct
183  {
184  unsigned off : 6;
185  unsigned len : 26;
186  unsigned add : 32;
187  } mask;
188 } octave_cray_descriptor;
189 
190 typedef void *octave_cray_ftn_ch_dsc;
191 
192 #ifdef __cplusplus
193 #define OCTAVE_F77_FCN_INLINE inline
194 #else
195 #define OCTAVE_F77_FCN_INLINE
196 #endif
197 
198 static OCTAVE_F77_FCN_INLINE octave_cray_ftn_ch_dsc
199 octave_make_cray_ftn_ch_dsc (char *ptr_arg, unsigned long len_arg)
200 {
201  octave_cray_descriptor desc;
202  desc.ptr = ptr_arg;
203  desc.mask.len = len_arg << 3;
204  return *((octave_cray_ftn_ch_dsc *) &desc);
205 }
206 
207 static OCTAVE_F77_FCN_INLINE octave_cray_ftn_ch_dsc
208 octave_make_cray_const_ftn_ch_dsc (const char *ptr_arg, unsigned long len_arg)
209 {
210  octave_cray_descriptor desc;
211  desc.const_ptr = ptr_arg;
212  desc.mask.len = len_arg << 3;
213  return *((octave_cray_ftn_ch_dsc *) &desc);
214 }
215 
216 #ifdef __cplusplus
217 #undef OCTAVE_F77_FCN_INLINE
218 #endif
219 
220 #elif defined (F77_USES_VISUAL_FORTRAN_CALLING_CONVENTION)
221 
222 /* Use these macros to pass character strings from C to Fortran. */
223 #define F77_CHAR_ARG(x) x, strlen (x)
224 #define F77_CONST_CHAR_ARG(x) F77_CHAR_ARG (x)
225 #define F77_CHAR_ARG2(x, l) x, l
226 #define F77_CONST_CHAR_ARG2(x, l) F77_CHAR_ARG2 (x, l)
227 #define F77_CXX_STRING_ARG(x) F77_CONST_CHAR_ARG2 (x.c_str (), x.length ())
228 #define F77_CHAR_ARG_LEN(l)
229 #define F77_CHAR_ARG_DECL char *, int
230 #define F77_CONST_CHAR_ARG_DECL const char *, int
231 #define F77_CHAR_ARG_LEN_DECL
232 
233 #define F77_CHAR_ARG_DEF(s, len) char *s, int len
234 #define F77_CONST_CHAR_ARG_DEF(s, len) const char *s, int len
235 #define F77_CHAR_ARG_LEN_DEF(len)
236 #define F77_CHAR_ARG_USE(s) s
237 #define F77_CHAR_ARG_LEN_USE(s, len) len
238 
239 #define F77_RET_T void
240 
241 #define F77_RETURN(retval) return;
242 #define F77_NORETURN(retval)
243 
244 #else
245 
246 /* Assume f2c-compatible calling convention. */
247 
248 #define F77_CHAR_ARG(x) x
249 #define F77_CONST_CHAR_ARG(x) F77_CHAR_ARG (x)
250 #define F77_CHAR_ARG2(x, l) x
251 #define F77_CONST_CHAR_ARG2(x, l) F77_CHAR_ARG2 (x, l)
252 #define F77_CXX_STRING_ARG(x) F77_CONST_CHAR_ARG2 (x.c_str (), x.length ())
253 #define F77_CHAR_ARG_LEN(l) , l
254 #define F77_CHAR_ARG_DECL char *
255 #define F77_CONST_CHAR_ARG_DECL const char *
256 #define F77_CHAR_ARG_LEN_DECL , long
257 
258 #define F77_CHAR_ARG_DEF(s, len) char *s
259 #define F77_CONST_CHAR_ARG_DEF(s, len) const char *s
260 #define F77_CHAR_ARG_LEN_DEF(len) , long len
261 #define F77_CHAR_ARG_USE(s) s
262 #define F77_CHAR_ARG_LEN_USE(s, len) len
263 
264 #define F77_RET_T int
265 
266 #define F77_RETURN(retval) return retval;
267 #if defined (HAVE_ATTR_NORETURN)
268 #define F77_NORETURN(retval)
269 #else
270 #define F77_NORETURN(retval) return retval;
271 #endif
272 
273 #endif
274 
275 
276 /* Build a C string local variable CS from the Fortran string parameter S
277  declared as F77_CHAR_ARG_DEF(s, len) or F77_CONST_CHAR_ARG_DEF(s, len).
278  The string will be cleaned up at the end of the current block.
279  Needs to include <cstring> and <vector>. */
280 
281 #define F77_CSTRING(s, len, cs) \
282  OCTAVE_LOCAL_BUFFER (char, cs, F77_CHAR_ARG_LEN_USE (s, len) + 1); \
283  memcpy (cs, F77_CHAR_ARG_USE (s), F77_CHAR_ARG_LEN_USE (s, len)); \
284  cs[F77_CHAR_ARG_LEN_USE(s, len)] = '\0'
285 
286 
287 extern CRUFT_API F77_RET_T
288 F77_FUNC (xstopx, XSTOPX) (F77_CONST_CHAR_ARG_DECL
290 
291 #ifdef __cplusplus
292 }
293 #endif
294 
295 #endif