1 ! (c) 2009 Joe Groff, see BSD license
2 USING: accessors alien alien.c-types alien.complex alien.data
3 alien.parser grouping alien.strings alien.syntax arrays ascii
4 assocs byte-arrays combinators combinators.short-circuit fry
5 generalizations kernel lexer macros math math.parser namespaces
6 parser sequences sequences.generalizations splitting
7 stack-checker vectors vocabs.parser words locals
8 io.encodings.ascii io.encodings.string shuffle effects
9 math.ranges math.order sorting strings system alien.libraries ;
10 QUALIFIED-WITH: alien.c-types c
13 SINGLETONS: f2c-abi g95-abi gfortran-abi intel-unix-abi intel-windows-abi ;
16 : add-f2c-libraries ( -- )
17 "I77" "libI77.so" cdecl add-library
18 "F77" "libF77.so" cdecl add-library ;
20 os netbsd? [ add-f2c-libraries ] when
23 : alien>nstring ( alien len encoding -- string )
24 [ memory>byte-array ] dip decode ;
26 ERROR: invalid-fortran-type type ;
28 DEFER: fortran-sig>c-sig
29 DEFER: fortran-ret-type>c-type
30 DEFER: fortran-arg-type>c-type
31 DEFER: fortran-name>symbol-name
33 SYMBOL: library-fortran-abis
35 library-fortran-abis [ H{ } clone ] initialize
39 : lowercase-name-with-underscore ( name -- name' )
41 : lowercase-name-with-extra-underscore ( name -- name' )
42 >lower CHAR: _ over member?
43 [ "__" append ] [ "_" append ] if ;
45 HOOK: fortran-c-abi fortran-abi ( -- abi )
46 M: f2c-abi fortran-c-abi cdecl ;
47 M: g95-abi fortran-c-abi cdecl ;
48 M: gfortran-abi fortran-c-abi cdecl ;
49 M: intel-unix-abi fortran-c-abi cdecl ;
50 M: intel-windows-abi fortran-c-abi cdecl ;
52 HOOK: real-functions-return-double? fortran-abi ( -- ? )
53 M: f2c-abi real-functions-return-double? t ;
54 M: g95-abi real-functions-return-double? f ;
55 M: gfortran-abi real-functions-return-double? f ;
56 M: intel-unix-abi real-functions-return-double? f ;
57 M: intel-windows-abi real-functions-return-double? f ;
59 HOOK: complex-functions-return-by-value? fortran-abi ( -- ? )
60 M: f2c-abi complex-functions-return-by-value? f ;
61 M: g95-abi complex-functions-return-by-value? f ;
62 M: gfortran-abi complex-functions-return-by-value? t ;
63 M: intel-unix-abi complex-functions-return-by-value? f ;
64 M: intel-windows-abi complex-functions-return-by-value? f ;
66 HOOK: character(1)-maps-to-char? fortran-abi ( -- ? )
67 M: f2c-abi character(1)-maps-to-char? f ;
68 M: g95-abi character(1)-maps-to-char? f ;
69 M: gfortran-abi character(1)-maps-to-char? f ;
70 M: intel-unix-abi character(1)-maps-to-char? t ;
71 M: intel-windows-abi character(1)-maps-to-char? t ;
73 HOOK: mangle-name fortran-abi ( name -- name' )
74 M: f2c-abi mangle-name lowercase-name-with-extra-underscore ;
75 M: g95-abi mangle-name lowercase-name-with-extra-underscore ;
76 M: gfortran-abi mangle-name lowercase-name-with-underscore ;
77 M: intel-unix-abi mangle-name lowercase-name-with-underscore ;
78 M: intel-windows-abi mangle-name >upper ;
80 TUPLE: fortran-type dims size out? ;
82 TUPLE: number-type < fortran-type ;
83 TUPLE: integer-type < number-type ;
84 TUPLE: logical-type < integer-type ;
85 TUPLE: real-type < number-type ;
86 TUPLE: double-precision-type < number-type ;
88 TUPLE: character-type < fortran-type ;
89 TUPLE: misc-type < fortran-type name ;
91 TUPLE: complex-type < number-type ;
92 TUPLE: real-complex-type < complex-type ;
93 TUPLE: double-complex-type < complex-type ;
95 CONSTANT: fortran>c-types H{
96 { "character" character-type }
97 { "integer" integer-type }
98 { "logical" logical-type }
100 { "double-precision" double-precision-type }
101 { "complex" real-complex-type }
102 { "double-complex" double-complex-type }
105 : append-dimensions ( base-c-type type -- c-type )
106 dims>> [ product 2array ] when* ;
108 MACRO: size-case-type ( cases -- )
109 [ invalid-fortran-type ] suffix
110 '[ [ size>> _ case ] [ append-dimensions ] bi ] ;
112 : simple-type ( type base-c-type -- c-type )
114 [ dup size>> [ invalid-fortran-type ] [ drop ] if ]
115 [ append-dimensions ] bi ;
117 : new-fortran-type ( out? dims size class -- type )
118 new [ [ size<< ] [ dims<< ] [ out?<< ] tri ] keep ;
120 GENERIC: (fortran-type>c-type) ( type -- c-type )
122 M: f (fortran-type>c-type) drop c:void ;
124 M: integer-type (fortran-type>c-type)
132 M: real-type (fortran-type>c-type)
138 M: real-complex-type (fortran-type>c-type)
140 { f [ complex-float ] }
141 { 8 [ complex-float ] }
142 { 16 [ complex-double ] }
145 M: double-precision-type (fortran-type>c-type)
146 c:double simple-type ;
147 M: double-complex-type (fortran-type>c-type)
148 complex-double simple-type ;
149 M: misc-type (fortran-type>c-type)
150 dup name>> parse-c-type simple-type ;
152 : single-char? ( character-type -- ? )
153 { [ drop character(1)-maps-to-char? ] [ dims>> product 1 = ] } 1&& ;
155 : fix-character-type ( character-type -- character-type' )
157 [ dup dims>> [ invalid-fortran-type ] [ dup size>> 1array >>dims f >>size ] if ]
158 [ dup dims>> [ ] [ f >>dims ] if ] if
159 dup single-char? [ f >>dims ] when ;
161 M: character-type (fortran-type>c-type)
162 fix-character-type c:char simple-type ;
164 : dimension>number ( string -- number )
165 dup "*" = [ drop 0 ] [ string>number ] if ;
167 : parse-out ( string -- string' out? )
170 : parse-dims ( string -- string' dim )
172 [ ")" ?tail drop "," split [ [ blank? ] trim dimension>number ] map ] when ;
174 : parse-size ( string -- string' size )
175 "*" split1 dup [ string>number ] when ;
177 : (parse-fortran-type) ( fortran-type-string -- type )
178 parse-out swap parse-dims swap parse-size swap
179 >lower fortran>c-types ?at
180 [ new-fortran-type ] [ misc-type boa ] if ;
182 : parse-fortran-type ( fortran-type-string/f -- type/f )
183 dup [ (parse-fortran-type) ] when ;
185 GENERIC: added-c-args ( type -- args )
187 M: fortran-type added-c-args drop { } ;
188 M: character-type added-c-args fix-character-type single-char? [ { } ] [ { c:long } ] if ;
190 GENERIC: returns-by-value? ( type -- ? )
192 M: f returns-by-value? drop t ;
193 M: fortran-type returns-by-value? drop f ;
194 M: number-type returns-by-value? dims>> not ;
195 M: character-type returns-by-value? fix-character-type single-char? ;
196 M: complex-type returns-by-value?
197 { [ drop complex-functions-return-by-value? ] [ dims>> not ] } 1&& ;
199 GENERIC: (fortran-ret-type>c-type) ( type -- c-type )
201 M: f (fortran-ret-type>c-type) drop c:void ;
202 M: fortran-type (fortran-ret-type>c-type) (fortran-type>c-type) ;
203 M: real-type (fortran-ret-type>c-type)
204 drop real-functions-return-double? [ c:double ] [ c:float ] if ;
206 GENERIC: (fortran-arg>c-args) ( type -- main-quot added-quot )
208 : args?dims ( type quot -- main-quot added-quot )
209 [ dup dims>> [ drop [ ] [ drop ] ] ] dip if ; inline
211 M: integer-type (fortran-arg>c-args)
214 { f [ [ <int> ] [ drop ] ] }
215 { 1 [ [ <char> ] [ drop ] ] }
216 { 2 [ [ <short> ] [ drop ] ] }
217 { 4 [ [ <int> ] [ drop ] ] }
218 { 8 [ [ <longlong> ] [ drop ] ] }
219 [ invalid-fortran-type ]
223 M: logical-type (fortran-arg>c-args)
224 [ call-next-method [ [ 1 0 ? ] prepend ] dip ] args?dims ;
226 M: real-type (fortran-arg>c-args)
229 { f [ [ <float> ] [ drop ] ] }
230 { 4 [ [ <float> ] [ drop ] ] }
231 { 8 [ [ <double> ] [ drop ] ] }
232 [ invalid-fortran-type ]
236 M: real-complex-type (fortran-arg>c-args)
239 { f [ [ <complex-float> ] [ drop ] ] }
240 { 8 [ [ <complex-float> ] [ drop ] ] }
241 { 16 [ [ <complex-double> ] [ drop ] ] }
242 [ invalid-fortran-type ]
246 M: double-precision-type (fortran-arg>c-args)
247 [ drop [ <double> ] [ drop ] ] args?dims ;
249 M: double-complex-type (fortran-arg>c-args)
250 [ drop [ <complex-double> ] [ drop ] ] args?dims ;
252 M: character-type (fortran-arg>c-args)
253 fix-character-type single-char?
254 [ [ first <char> ] [ drop ] ]
255 [ [ ascii string>alien ] [ length ] ] if ;
257 M: misc-type (fortran-arg>c-args)
260 GENERIC: (fortran-result>) ( type -- quots )
262 : result?dims ( type quot -- quot )
263 [ dup dims>> [ drop { [ ] } ] ] dip if ; inline
265 M: integer-type (fortran-result>)
267 { f [ { [ *int ] } ] }
268 { 1 [ { [ *char ] } ] }
269 { 2 [ { [ *short ] } ] }
270 { 4 [ { [ *int ] } ] }
271 { 8 [ { [ *longlong ] } ] }
272 [ invalid-fortran-type ]
273 } case ] result?dims ;
275 M: logical-type (fortran-result>)
276 [ call-next-method first [ zero? not ] append 1array ] result?dims ;
278 M: real-type (fortran-result>)
280 { f [ { [ *float ] } ] }
281 { 4 [ { [ *float ] } ] }
282 { 8 [ { [ *double ] } ] }
283 [ invalid-fortran-type ]
284 } case ] result?dims ;
286 M: real-complex-type (fortran-result>)
288 { f [ { [ *complex-float ] } ] }
289 { 8 [ { [ *complex-float ] } ] }
290 { 16 [ { [ *complex-double ] } ] }
291 [ invalid-fortran-type ]
292 } case ] result?dims ;
294 M: double-precision-type (fortran-result>)
295 [ drop { [ *double ] } ] result?dims ;
297 M: double-complex-type (fortran-result>)
298 [ drop { [ *complex-double ] } ] result?dims ;
300 M: character-type (fortran-result>)
301 fix-character-type single-char?
302 [ { [ *char 1string ] } ]
303 [ { [ ] [ ascii alien>nstring ] } ] if ;
305 M: misc-type (fortran-result>)
308 GENERIC: (<fortran-result>) ( type -- quot )
310 M: fortran-type (<fortran-result>)
311 (fortran-type>c-type) \ <c-object> [ ] 2sequence ;
313 M: character-type (<fortran-result>)
314 fix-character-type dims>> product dup
315 [ \ <byte-array> ] dip [ ] 3sequence ;
317 : [<fortran-result>] ( return parameters -- quot )
318 [ parse-fortran-type ] dip
319 over returns-by-value?
321 [ [ (<fortran-result>) ] [ length \ ndip [ ] 3sequence ] bi* ] if ;
323 : [fortran-args>c-args] ( parameters -- quot )
325 [ parse-fortran-type (fortran-arg>c-args) 2array ] map flip first2
326 [ [ \ spread [ ] 2sequence ] bi@ 2array ] [ length ] bi
327 \ ncleave [ ] 3sequence
330 :: [fortran-invoke] ( [args>args] return library function parameters -- [args>args] quot )
331 return parameters fortran-sig>c-sig :> ( c-return c-parameters )
332 function fortran-name>symbol-name :> c-function
334 c-return library c-function c-parameters \ alien-invoke
336 c-parameters length \ nkeep
339 : [fortran-out-param>] ( parameter -- quot )
341 [ (fortran-result>) ] [ out?>> ] bi
342 [ ] [ [ drop [ drop ] ] map ] if ;
344 : [fortran-return>] ( return -- quot )
346 { [ dup not ] [ drop { } ] }
347 { [ dup returns-by-value? ] [ drop { [ ] } ] }
348 [ (fortran-result>) ]
351 : letters ( -- seq ) CHAR: a CHAR: z [a,b] ;
353 : (shuffle-map) ( return parameters -- ret par )
355 fortran-ret-type>c-type length swap void? [ 1 + ] unless
356 letters swap head [ "ret" swap suffix ] map
358 [ fortran-arg-type>c-type nip length 1 + ] map letters swap zip
359 [ first2 letters swap head [ "" 2sequence ] with map ] map concat
362 : (fortran-in-shuffle) ( ret par -- seq )
363 [ second ] sort-with append ;
365 : (fortran-out-shuffle) ( ret par -- seq )
368 : [fortran-result-shuffle] ( return parameters -- quot )
369 (shuffle-map) [ (fortran-in-shuffle) ] [ (fortran-out-shuffle) ] 2bi <effect>
370 \ shuffle-effect [ ] 2sequence ;
372 : [fortran-results>] ( return parameters -- quot )
373 [ [fortran-result-shuffle] ]
374 [ drop [fortran-return>] ]
375 [ nip [ [fortran-out-param>] ] map concat ] 2tri
377 \ spread [ ] 2sequence append ;
379 : (add-fortran-library) ( fortran-abi name -- )
380 library-fortran-abis get-global set-at ;
384 : add-fortran-library ( name soname fortran-abi -- )
385 [ fortran-abi [ fortran-c-abi ] with-variable add-library ]
386 [ nip swap (add-fortran-library) ] 3bi ;
388 : fortran-name>symbol-name ( fortran-name -- c-name )
391 : fortran-type>c-type ( fortran-type -- c-type )
392 parse-fortran-type (fortran-type>c-type) ;
394 : fortran-arg-type>c-type ( fortran-type -- c-type added-args )
396 [ (fortran-type>c-type) <pointer> ]
397 [ added-c-args ] bi ;
398 : fortran-ret-type>c-type ( fortran-type -- c-type added-args )
399 parse-fortran-type dup returns-by-value?
400 [ (fortran-ret-type>c-type) { } ] [
402 [ added-c-args ] [ (fortran-type>c-type) <pointer> ] bi prefix
405 : fortran-arg-types>c-types ( fortran-types -- c-types )
406 [ length <vector> 1 <vector> ] keep
407 [ fortran-arg-type>c-type swapd [ suffix! ] [ append! ] 2bi* ] each
410 : fortran-sig>c-sig ( fortran-return fortran-args -- c-return c-args )
411 [ fortran-ret-type>c-type ] [ fortran-arg-types>c-types ] bi* append ;
413 : set-fortran-abi ( library -- )
414 library-fortran-abis get-global at fortran-abi set ;
416 : (fortran-invoke) ( return library function parameters -- quot )
418 [ 2nip [<fortran-result>] ]
419 [ nip nip nip [fortran-args>c-args] ]
421 [ 2nip [fortran-results>] ]
422 } 4 ncleave 4 nappend ;
424 MACRO: fortran-invoke ( return library function parameters -- )
425 { [ 2drop nip set-fortran-abi ] [ (fortran-invoke) ] } 4 ncleave ;
427 : parse-arglist ( parameters return -- types effect )
428 [ 2 group unzip [ "," ?tail drop ] map ]
429 [ [ { } ] [ 1array ] if-void ]
432 :: define-fortran-function ( return library function parameters -- )
433 function create-in dup reset-generic
434 return library function parameters return [ c:void ] unless* parse-arglist
435 [ \ fortran-invoke 5 [ ] nsequence ] dip define-declared ;
438 f current-library get scan ";" parse-tokens
439 [ "()" subseq? not ] filter define-fortran-function ;
442 scan current-library get scan ";" parse-tokens
443 [ "()" subseq? not ] filter define-fortran-function ;
447 [ current-library set ]
448 [ set-fortran-abi ] bi ;