1 ! (c) 2009 Joe Groff, see BSD license
2 USING: accessors alien alien.c-types alien.complex alien.data grouping
3 alien.strings alien.syntax arrays ascii assocs
4 byte-arrays combinators combinators.short-circuit fry generalizations
5 kernel lexer macros math math.parser namespaces parser sequences
6 splitting stack-checker vectors vocabs.parser words locals
7 io.encodings.ascii io.encodings.string shuffle effects math.ranges
8 math.order sorting strings system alien.libraries ;
11 SINGLETONS: f2c-abi g95-abi gfortran-abi intel-unix-abi intel-windows-abi ;
14 : add-f2c-libraries ( -- )
15 "I77" "libI77.so" "cdecl" add-library
16 "F77" "libF77.so" "cdecl" add-library ;
18 os netbsd? [ add-f2c-libraries ] when
21 : alien>nstring ( alien len encoding -- string )
22 [ memory>byte-array ] dip decode ;
24 ERROR: invalid-fortran-type type ;
26 DEFER: fortran-sig>c-sig
27 DEFER: fortran-ret-type>c-type
28 DEFER: fortran-arg-type>c-type
29 DEFER: fortran-name>symbol-name
31 SYMBOL: library-fortran-abis
33 library-fortran-abis [ H{ } clone ] initialize
37 : lowercase-name-with-underscore ( name -- name' )
39 : lowercase-name-with-extra-underscore ( name -- name' )
40 >lower CHAR: _ over member?
41 [ "__" append ] [ "_" append ] if ;
43 HOOK: fortran-c-abi fortran-abi ( -- abi )
44 M: f2c-abi fortran-c-abi "cdecl" ;
45 M: g95-abi fortran-c-abi "cdecl" ;
46 M: gfortran-abi fortran-c-abi "cdecl" ;
47 M: intel-unix-abi fortran-c-abi "cdecl" ;
48 M: intel-windows-abi fortran-c-abi "cdecl" ;
50 HOOK: real-functions-return-double? fortran-abi ( -- ? )
51 M: f2c-abi real-functions-return-double? t ;
52 M: g95-abi real-functions-return-double? f ;
53 M: gfortran-abi real-functions-return-double? f ;
54 M: intel-unix-abi real-functions-return-double? f ;
55 M: intel-windows-abi real-functions-return-double? f ;
57 HOOK: complex-functions-return-by-value? fortran-abi ( -- ? )
58 M: f2c-abi complex-functions-return-by-value? f ;
59 M: g95-abi complex-functions-return-by-value? f ;
60 M: gfortran-abi complex-functions-return-by-value? t ;
61 M: intel-unix-abi complex-functions-return-by-value? f ;
62 M: intel-windows-abi complex-functions-return-by-value? f ;
64 HOOK: character(1)-maps-to-char? fortran-abi ( -- ? )
65 M: f2c-abi character(1)-maps-to-char? f ;
66 M: g95-abi character(1)-maps-to-char? f ;
67 M: gfortran-abi character(1)-maps-to-char? f ;
68 M: intel-unix-abi character(1)-maps-to-char? t ;
69 M: intel-windows-abi character(1)-maps-to-char? t ;
71 HOOK: mangle-name fortran-abi ( name -- name' )
72 M: f2c-abi mangle-name lowercase-name-with-extra-underscore ;
73 M: g95-abi mangle-name lowercase-name-with-extra-underscore ;
74 M: gfortran-abi mangle-name lowercase-name-with-underscore ;
75 M: intel-unix-abi mangle-name lowercase-name-with-underscore ;
76 M: intel-windows-abi mangle-name >upper ;
78 TUPLE: fortran-type dims size out? ;
80 TUPLE: number-type < fortran-type ;
81 TUPLE: integer-type < number-type ;
82 TUPLE: logical-type < integer-type ;
83 TUPLE: real-type < number-type ;
84 TUPLE: double-precision-type < number-type ;
86 TUPLE: character-type < fortran-type ;
87 TUPLE: misc-type < fortran-type name ;
89 TUPLE: complex-type < number-type ;
90 TUPLE: real-complex-type < complex-type ;
91 TUPLE: double-complex-type < complex-type ;
93 CONSTANT: fortran>c-types H{
94 { "character" character-type }
95 { "integer" integer-type }
96 { "logical" logical-type }
98 { "double-precision" double-precision-type }
99 { "complex" real-complex-type }
100 { "double-complex" double-complex-type }
103 : append-dimensions ( base-c-type type -- c-type )
105 [ product number>string "[" "]" surround append ] when* ;
107 MACRO: size-case-type ( cases -- )
108 [ invalid-fortran-type ] suffix
109 '[ [ size>> _ case ] [ append-dimensions ] bi ] ;
111 : simple-type ( type base-c-type -- c-type )
113 [ dup size>> [ invalid-fortran-type ] [ drop ] if ]
114 [ append-dimensions ] bi ;
116 : new-fortran-type ( out? dims size class -- type )
117 new [ [ (>>size) ] [ (>>dims) ] [ (>>out?) ] tri ] keep ;
119 GENERIC: (fortran-type>c-type) ( type -- c-type )
121 M: f (fortran-type>c-type) drop "void" ;
123 M: integer-type (fortran-type>c-type)
131 M: real-type (fortran-type>c-type)
137 M: real-complex-type (fortran-type>c-type)
139 { f [ "complex-float" ] }
140 { 8 [ "complex-float" ] }
141 { 16 [ "complex-double" ] }
144 M: double-precision-type (fortran-type>c-type)
145 "double" simple-type ;
146 M: double-complex-type (fortran-type>c-type)
147 "complex-double" simple-type ;
148 M: misc-type (fortran-type>c-type)
149 dup name>> simple-type ;
151 : single-char? ( character-type -- ? )
152 { [ drop character(1)-maps-to-char? ] [ dims>> product 1 = ] } 1&& ;
154 : fix-character-type ( character-type -- character-type' )
156 [ dup dims>> [ invalid-fortran-type ] [ dup size>> 1array >>dims f >>size ] if ]
157 [ dup dims>> [ ] [ f >>dims ] if ] if
158 dup single-char? [ f >>dims ] when ;
160 M: character-type (fortran-type>c-type)
161 fix-character-type "char" simple-type ;
163 : dimension>number ( string -- number )
164 dup "*" = [ drop 0 ] [ string>number ] if ;
166 : parse-out ( string -- string' out? )
169 : parse-dims ( string -- string' dim )
171 [ ")" ?tail drop "," split [ [ blank? ] trim dimension>number ] map ] when ;
173 : parse-size ( string -- string' size )
174 "*" split1 dup [ string>number ] when ;
176 : (parse-fortran-type) ( fortran-type-string -- type )
177 parse-out swap parse-dims swap parse-size swap
178 >lower fortran>c-types ?at
179 [ new-fortran-type ] [ misc-type boa ] if ;
181 : parse-fortran-type ( fortran-type-string/f -- type/f )
182 dup [ (parse-fortran-type) ] when ;
184 : c-type>pointer ( c-type -- c-type* )
185 "[" split1 drop "*" append ;
187 GENERIC: added-c-args ( type -- args )
189 M: fortran-type added-c-args drop { } ;
190 M: character-type added-c-args fix-character-type single-char? [ { } ] [ { "long" } ] if ;
192 GENERIC: returns-by-value? ( type -- ? )
194 M: f returns-by-value? drop t ;
195 M: fortran-type returns-by-value? drop f ;
196 M: number-type returns-by-value? dims>> not ;
197 M: character-type returns-by-value? fix-character-type single-char? ;
198 M: complex-type returns-by-value?
199 { [ drop complex-functions-return-by-value? ] [ dims>> not ] } 1&& ;
201 GENERIC: (fortran-ret-type>c-type) ( type -- c-type )
203 M: f (fortran-ret-type>c-type) drop "void" ;
204 M: fortran-type (fortran-ret-type>c-type) (fortran-type>c-type) ;
205 M: real-type (fortran-ret-type>c-type)
206 drop real-functions-return-double? [ "double" ] [ "float" ] if ;
208 GENERIC: (fortran-arg>c-args) ( type -- main-quot added-quot )
210 : args?dims ( type quot -- main-quot added-quot )
211 [ dup dims>> [ drop [ ] [ drop ] ] ] dip if ; inline
213 M: integer-type (fortran-arg>c-args)
216 { f [ [ <int> ] [ drop ] ] }
217 { 1 [ [ <char> ] [ drop ] ] }
218 { 2 [ [ <short> ] [ drop ] ] }
219 { 4 [ [ <int> ] [ drop ] ] }
220 { 8 [ [ <longlong> ] [ drop ] ] }
221 [ invalid-fortran-type ]
225 M: logical-type (fortran-arg>c-args)
226 [ call-next-method [ [ 1 0 ? ] prepend ] dip ] args?dims ;
228 M: real-type (fortran-arg>c-args)
231 { f [ [ <float> ] [ drop ] ] }
232 { 4 [ [ <float> ] [ drop ] ] }
233 { 8 [ [ <double> ] [ drop ] ] }
234 [ invalid-fortran-type ]
238 M: real-complex-type (fortran-arg>c-args)
241 { f [ [ <complex-float> ] [ drop ] ] }
242 { 8 [ [ <complex-float> ] [ drop ] ] }
243 { 16 [ [ <complex-double> ] [ drop ] ] }
244 [ invalid-fortran-type ]
248 M: double-precision-type (fortran-arg>c-args)
249 [ drop [ <double> ] [ drop ] ] args?dims ;
251 M: double-complex-type (fortran-arg>c-args)
252 [ drop [ <complex-double> ] [ drop ] ] args?dims ;
254 M: character-type (fortran-arg>c-args)
255 fix-character-type single-char?
256 [ [ first <char> ] [ drop ] ]
257 [ [ ascii string>alien ] [ length ] ] if ;
259 M: misc-type (fortran-arg>c-args)
262 GENERIC: (fortran-result>) ( type -- quots )
264 : result?dims ( type quot -- quot )
265 [ dup dims>> [ drop { [ ] } ] ] dip if ; inline
267 M: integer-type (fortran-result>)
269 { f [ { [ *int ] } ] }
270 { 1 [ { [ *char ] } ] }
271 { 2 [ { [ *short ] } ] }
272 { 4 [ { [ *int ] } ] }
273 { 8 [ { [ *longlong ] } ] }
274 [ invalid-fortran-type ]
275 } case ] result?dims ;
277 M: logical-type (fortran-result>)
278 [ call-next-method first [ zero? not ] append 1array ] result?dims ;
280 M: real-type (fortran-result>)
282 { f [ { [ *float ] } ] }
283 { 4 [ { [ *float ] } ] }
284 { 8 [ { [ *double ] } ] }
285 [ invalid-fortran-type ]
286 } case ] result?dims ;
288 M: real-complex-type (fortran-result>)
290 { f [ { [ *complex-float ] } ] }
291 { 8 [ { [ *complex-float ] } ] }
292 { 16 [ { [ *complex-double ] } ] }
293 [ invalid-fortran-type ]
294 } case ] result?dims ;
296 M: double-precision-type (fortran-result>)
297 [ drop { [ *double ] } ] result?dims ;
299 M: double-complex-type (fortran-result>)
300 [ drop { [ *complex-double ] } ] result?dims ;
302 M: character-type (fortran-result>)
303 fix-character-type single-char?
304 [ { [ *char 1string ] } ]
305 [ { [ ] [ ascii alien>nstring ] } ] if ;
307 M: misc-type (fortran-result>)
310 GENERIC: (<fortran-result>) ( type -- quot )
312 M: fortran-type (<fortran-result>)
313 (fortran-type>c-type) \ <c-object> [ ] 2sequence ;
315 M: character-type (<fortran-result>)
316 fix-character-type dims>> product dup
317 [ \ <byte-array> ] dip [ ] 3sequence ;
319 : [<fortran-result>] ( return parameters -- quot )
320 [ parse-fortran-type ] dip
321 over returns-by-value?
323 [ [ (<fortran-result>) ] [ length \ ndip [ ] 3sequence ] bi* ] if ;
325 : [fortran-args>c-args] ( parameters -- quot )
327 [ parse-fortran-type (fortran-arg>c-args) 2array ] map flip first2
328 [ [ \ spread [ ] 2sequence ] bi@ 2array ] [ length ] bi
329 \ ncleave [ ] 3sequence
332 :: [fortran-invoke] ( [args>args] return library function parameters -- [args>args] quot )
333 return parameters fortran-sig>c-sig :> c-parameters :> c-return
334 function fortran-name>symbol-name :> c-function
336 c-return library c-function c-parameters \ alien-invoke
338 c-parameters length \ nkeep
341 : [fortran-out-param>] ( parameter -- quot )
343 [ (fortran-result>) ] [ out?>> ] bi
344 [ ] [ [ drop [ drop ] ] map ] if ;
346 : [fortran-return>] ( return -- quot )
348 { [ dup not ] [ drop { } ] }
349 { [ dup returns-by-value? ] [ drop { [ ] } ] }
350 [ (fortran-result>) ]
353 : letters ( -- seq ) CHAR: a CHAR: z [a,b] ;
355 : (shuffle-map) ( return parameters -- ret par )
357 fortran-ret-type>c-type length swap "void" = [ 1 + ] unless
358 letters swap head [ "ret" swap suffix ] map
360 [ fortran-arg-type>c-type nip length 1 + ] map letters swap zip
361 [ first2 letters swap head [ "" 2sequence ] with map ] map concat
364 : (fortran-in-shuffle) ( ret par -- seq )
365 [ second ] sort-with append ;
367 : (fortran-out-shuffle) ( ret par -- seq )
370 : [fortran-result-shuffle] ( return parameters -- quot )
371 (shuffle-map) [ (fortran-in-shuffle) ] [ (fortran-out-shuffle) ] 2bi <effect>
372 \ shuffle-effect [ ] 2sequence ;
374 : [fortran-results>] ( return parameters -- quot )
375 [ [fortran-result-shuffle] ]
376 [ drop [fortran-return>] ]
377 [ nip [ [fortran-out-param>] ] map concat ] 2tri
379 \ spread [ ] 2sequence append ;
381 : (add-fortran-library) ( fortran-abi name -- )
382 library-fortran-abis get-global set-at ;
386 : add-fortran-library ( name soname fortran-abi -- )
387 [ fortran-abi [ fortran-c-abi ] with-variable add-library ]
388 [ nip swap (add-fortran-library) ] 3bi ;
390 : fortran-name>symbol-name ( fortran-name -- c-name )
393 : fortran-type>c-type ( fortran-type -- c-type )
394 parse-fortran-type (fortran-type>c-type) ;
396 : fortran-arg-type>c-type ( fortran-type -- c-type added-args )
398 [ (fortran-type>c-type) c-type>pointer ]
399 [ added-c-args ] bi ;
400 : fortran-ret-type>c-type ( fortran-type -- c-type added-args )
401 parse-fortran-type dup returns-by-value?
402 [ (fortran-ret-type>c-type) { } ] [
404 [ added-c-args ] [ (fortran-type>c-type) c-type>pointer ] bi prefix
407 : fortran-arg-types>c-types ( fortran-types -- c-types )
408 [ length <vector> 1 <vector> ] keep
409 [ fortran-arg-type>c-type swapd [ suffix! ] [ append! ] 2bi* ] each
412 : fortran-sig>c-sig ( fortran-return fortran-args -- c-return c-args )
413 [ fortran-ret-type>c-type ] [ fortran-arg-types>c-types ] bi* append ;
415 : set-fortran-abi ( library -- )
416 library-fortran-abis get-global at fortran-abi set ;
418 : (fortran-invoke) ( return library function parameters -- quot )
420 [ 2nip [<fortran-result>] ]
421 [ nip nip nip [fortran-args>c-args] ]
423 [ 2nip [fortran-results>] ]
424 } 4 ncleave 4 nappend ;
426 MACRO: fortran-invoke ( return library function parameters -- )
427 { [ 2drop nip set-fortran-abi ] [ (fortran-invoke) ] } 4 ncleave ;
429 : parse-arglist ( parameters return -- types effect )
430 [ 2 group unzip [ "," ?tail drop ] map ]
431 [ [ { } ] [ 1array ] if-void ]
434 :: define-fortran-function ( return library function parameters -- )
435 function create-in dup reset-generic
436 return library function parameters return [ "void" ] unless* parse-arglist
437 [ \ fortran-invoke 5 [ ] nsequence ] dip define-declared ;
440 f "c-library" get scan ";" parse-tokens
441 [ "()" subseq? not ] filter define-fortran-function ;
444 scan "c-library" get scan ";" parse-tokens
445 [ "()" subseq? not ] filter define-fortran-function ;
450 [ set-fortran-abi ] bi ;