1 ! (c) 2009 Joe Groff, see BSD license
2 USING: accessors alien alien.c-types alien.complex alien.data alien.parser
3 grouping 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 ;
9 QUALIFIED-WITH: alien.c-types c
12 SINGLETONS: f2c-abi g95-abi gfortran-abi intel-unix-abi intel-windows-abi ;
15 : add-f2c-libraries ( -- )
16 "I77" "libI77.so" "cdecl" add-library
17 "F77" "libF77.so" "cdecl" add-library ;
19 os netbsd? [ add-f2c-libraries ] when
22 : alien>nstring ( alien len encoding -- string )
23 [ memory>byte-array ] dip decode ;
25 ERROR: invalid-fortran-type type ;
27 DEFER: fortran-sig>c-sig
28 DEFER: fortran-ret-type>c-type
29 DEFER: fortran-arg-type>c-type
30 DEFER: fortran-name>symbol-name
32 SYMBOL: library-fortran-abis
34 library-fortran-abis [ H{ } clone ] initialize
38 : lowercase-name-with-underscore ( name -- name' )
40 : lowercase-name-with-extra-underscore ( name -- name' )
41 >lower CHAR: _ over member?
42 [ "__" append ] [ "_" append ] if ;
44 HOOK: fortran-c-abi fortran-abi ( -- abi )
45 M: f2c-abi fortran-c-abi "cdecl" ;
46 M: g95-abi fortran-c-abi "cdecl" ;
47 M: gfortran-abi fortran-c-abi "cdecl" ;
48 M: intel-unix-abi fortran-c-abi "cdecl" ;
49 M: intel-windows-abi fortran-c-abi "cdecl" ;
51 HOOK: real-functions-return-double? fortran-abi ( -- ? )
52 M: f2c-abi real-functions-return-double? t ;
53 M: g95-abi real-functions-return-double? f ;
54 M: gfortran-abi real-functions-return-double? f ;
55 M: intel-unix-abi real-functions-return-double? f ;
56 M: intel-windows-abi real-functions-return-double? f ;
58 HOOK: complex-functions-return-by-value? fortran-abi ( -- ? )
59 M: f2c-abi complex-functions-return-by-value? f ;
60 M: g95-abi complex-functions-return-by-value? f ;
61 M: gfortran-abi complex-functions-return-by-value? t ;
62 M: intel-unix-abi complex-functions-return-by-value? f ;
63 M: intel-windows-abi complex-functions-return-by-value? f ;
65 HOOK: character(1)-maps-to-char? fortran-abi ( -- ? )
66 M: f2c-abi character(1)-maps-to-char? f ;
67 M: g95-abi character(1)-maps-to-char? f ;
68 M: gfortran-abi character(1)-maps-to-char? f ;
69 M: intel-unix-abi character(1)-maps-to-char? t ;
70 M: intel-windows-abi character(1)-maps-to-char? t ;
72 HOOK: mangle-name fortran-abi ( name -- name' )
73 M: f2c-abi mangle-name lowercase-name-with-extra-underscore ;
74 M: g95-abi mangle-name lowercase-name-with-extra-underscore ;
75 M: gfortran-abi mangle-name lowercase-name-with-underscore ;
76 M: intel-unix-abi mangle-name lowercase-name-with-underscore ;
77 M: intel-windows-abi mangle-name >upper ;
79 TUPLE: fortran-type dims size out? ;
81 TUPLE: number-type < fortran-type ;
82 TUPLE: integer-type < number-type ;
83 TUPLE: logical-type < integer-type ;
84 TUPLE: real-type < number-type ;
85 TUPLE: double-precision-type < number-type ;
87 TUPLE: character-type < fortran-type ;
88 TUPLE: misc-type < fortran-type name ;
90 TUPLE: complex-type < number-type ;
91 TUPLE: real-complex-type < complex-type ;
92 TUPLE: double-complex-type < complex-type ;
94 CONSTANT: fortran>c-types H{
95 { "character" character-type }
96 { "integer" integer-type }
97 { "logical" logical-type }
99 { "double-precision" double-precision-type }
100 { "complex" real-complex-type }
101 { "double-complex" double-complex-type }
104 : append-dimensions ( base-c-type type -- c-type )
105 dims>> [ product 2array ] 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 c: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 c: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>> parse-c-type 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 c: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 GENERIC: added-c-args ( type -- args )
186 M: fortran-type added-c-args drop { } ;
187 M: character-type added-c-args fix-character-type single-char? [ { } ] [ { c:long } ] if ;
189 GENERIC: returns-by-value? ( type -- ? )
191 M: f returns-by-value? drop t ;
192 M: fortran-type returns-by-value? drop f ;
193 M: number-type returns-by-value? dims>> not ;
194 M: character-type returns-by-value? fix-character-type single-char? ;
195 M: complex-type returns-by-value?
196 { [ drop complex-functions-return-by-value? ] [ dims>> not ] } 1&& ;
198 GENERIC: (fortran-ret-type>c-type) ( type -- c-type )
200 M: f (fortran-ret-type>c-type) drop c:void ;
201 M: fortran-type (fortran-ret-type>c-type) (fortran-type>c-type) ;
202 M: real-type (fortran-ret-type>c-type)
203 drop real-functions-return-double? [ c:double ] [ c:float ] if ;
205 GENERIC: (fortran-arg>c-args) ( type -- main-quot added-quot )
207 : args?dims ( type quot -- main-quot added-quot )
208 [ dup dims>> [ drop [ ] [ drop ] ] ] dip if ; inline
210 M: integer-type (fortran-arg>c-args)
213 { f [ [ <int> ] [ drop ] ] }
214 { 1 [ [ <char> ] [ drop ] ] }
215 { 2 [ [ <short> ] [ drop ] ] }
216 { 4 [ [ <int> ] [ drop ] ] }
217 { 8 [ [ <longlong> ] [ drop ] ] }
218 [ invalid-fortran-type ]
222 M: logical-type (fortran-arg>c-args)
223 [ call-next-method [ [ 1 0 ? ] prepend ] dip ] args?dims ;
225 M: real-type (fortran-arg>c-args)
228 { f [ [ <float> ] [ drop ] ] }
229 { 4 [ [ <float> ] [ drop ] ] }
230 { 8 [ [ <double> ] [ drop ] ] }
231 [ invalid-fortran-type ]
235 M: real-complex-type (fortran-arg>c-args)
238 { f [ [ <complex-float> ] [ drop ] ] }
239 { 8 [ [ <complex-float> ] [ drop ] ] }
240 { 16 [ [ <complex-double> ] [ drop ] ] }
241 [ invalid-fortran-type ]
245 M: double-precision-type (fortran-arg>c-args)
246 [ drop [ <double> ] [ drop ] ] args?dims ;
248 M: double-complex-type (fortran-arg>c-args)
249 [ drop [ <complex-double> ] [ drop ] ] args?dims ;
251 M: character-type (fortran-arg>c-args)
252 fix-character-type single-char?
253 [ [ first <char> ] [ drop ] ]
254 [ [ ascii string>alien ] [ length ] ] if ;
256 M: misc-type (fortran-arg>c-args)
259 GENERIC: (fortran-result>) ( type -- quots )
261 : result?dims ( type quot -- quot )
262 [ dup dims>> [ drop { [ ] } ] ] dip if ; inline
264 M: integer-type (fortran-result>)
266 { f [ { [ *int ] } ] }
267 { 1 [ { [ *char ] } ] }
268 { 2 [ { [ *short ] } ] }
269 { 4 [ { [ *int ] } ] }
270 { 8 [ { [ *longlong ] } ] }
271 [ invalid-fortran-type ]
272 } case ] result?dims ;
274 M: logical-type (fortran-result>)
275 [ call-next-method first [ zero? not ] append 1array ] result?dims ;
277 M: real-type (fortran-result>)
279 { f [ { [ *float ] } ] }
280 { 4 [ { [ *float ] } ] }
281 { 8 [ { [ *double ] } ] }
282 [ invalid-fortran-type ]
283 } case ] result?dims ;
285 M: real-complex-type (fortran-result>)
287 { f [ { [ *complex-float ] } ] }
288 { 8 [ { [ *complex-float ] } ] }
289 { 16 [ { [ *complex-double ] } ] }
290 [ invalid-fortran-type ]
291 } case ] result?dims ;
293 M: double-precision-type (fortran-result>)
294 [ drop { [ *double ] } ] result?dims ;
296 M: double-complex-type (fortran-result>)
297 [ drop { [ *complex-double ] } ] result?dims ;
299 M: character-type (fortran-result>)
300 fix-character-type single-char?
301 [ { [ *char 1string ] } ]
302 [ { [ ] [ ascii alien>nstring ] } ] if ;
304 M: misc-type (fortran-result>)
307 GENERIC: (<fortran-result>) ( type -- quot )
309 M: fortran-type (<fortran-result>)
310 (fortran-type>c-type) \ <c-object> [ ] 2sequence ;
312 M: character-type (<fortran-result>)
313 fix-character-type dims>> product dup
314 [ \ <byte-array> ] dip [ ] 3sequence ;
316 : [<fortran-result>] ( return parameters -- quot )
317 [ parse-fortran-type ] dip
318 over returns-by-value?
320 [ [ (<fortran-result>) ] [ length \ ndip [ ] 3sequence ] bi* ] if ;
322 : [fortran-args>c-args] ( parameters -- quot )
324 [ parse-fortran-type (fortran-arg>c-args) 2array ] map flip first2
325 [ [ \ spread [ ] 2sequence ] bi@ 2array ] [ length ] bi
326 \ ncleave [ ] 3sequence
329 :: [fortran-invoke] ( [args>args] return library function parameters -- [args>args] quot )
330 return parameters fortran-sig>c-sig :> ( c-return c-parameters )
331 function fortran-name>symbol-name :> c-function
333 c-return library c-function c-parameters \ alien-invoke
335 c-parameters length \ nkeep
338 : [fortran-out-param>] ( parameter -- quot )
340 [ (fortran-result>) ] [ out?>> ] bi
341 [ ] [ [ drop [ drop ] ] map ] if ;
343 : [fortran-return>] ( return -- quot )
345 { [ dup not ] [ drop { } ] }
346 { [ dup returns-by-value? ] [ drop { [ ] } ] }
347 [ (fortran-result>) ]
350 : letters ( -- seq ) CHAR: a CHAR: z [a,b] ;
352 : (shuffle-map) ( return parameters -- ret par )
354 fortran-ret-type>c-type length swap void? [ 1 + ] unless
355 letters swap head [ "ret" swap suffix ] map
357 [ fortran-arg-type>c-type nip length 1 + ] map letters swap zip
358 [ first2 letters swap head [ "" 2sequence ] with map ] map concat
361 : (fortran-in-shuffle) ( ret par -- seq )
362 [ second ] sort-with append ;
364 : (fortran-out-shuffle) ( ret par -- seq )
367 : [fortran-result-shuffle] ( return parameters -- quot )
368 (shuffle-map) [ (fortran-in-shuffle) ] [ (fortran-out-shuffle) ] 2bi <effect>
369 \ shuffle-effect [ ] 2sequence ;
371 : [fortran-results>] ( return parameters -- quot )
372 [ [fortran-result-shuffle] ]
373 [ drop [fortran-return>] ]
374 [ nip [ [fortran-out-param>] ] map concat ] 2tri
376 \ spread [ ] 2sequence append ;
378 : (add-fortran-library) ( fortran-abi name -- )
379 library-fortran-abis get-global set-at ;
383 : add-fortran-library ( name soname fortran-abi -- )
384 [ fortran-abi [ fortran-c-abi ] with-variable add-library ]
385 [ nip swap (add-fortran-library) ] 3bi ;
387 : fortran-name>symbol-name ( fortran-name -- c-name )
390 : fortran-type>c-type ( fortran-type -- c-type )
391 parse-fortran-type (fortran-type>c-type) ;
393 : fortran-arg-type>c-type ( fortran-type -- c-type added-args )
395 [ (fortran-type>c-type) resolve-pointer-type ]
396 [ added-c-args ] bi ;
397 : fortran-ret-type>c-type ( fortran-type -- c-type added-args )
398 parse-fortran-type dup returns-by-value?
399 [ (fortran-ret-type>c-type) { } ] [
401 [ added-c-args ] [ (fortran-type>c-type) resolve-pointer-type ] bi prefix
404 : fortran-arg-types>c-types ( fortran-types -- c-types )
405 [ length <vector> 1 <vector> ] keep
406 [ fortran-arg-type>c-type swapd [ suffix! ] [ append! ] 2bi* ] each
409 : fortran-sig>c-sig ( fortran-return fortran-args -- c-return c-args )
410 [ fortran-ret-type>c-type ] [ fortran-arg-types>c-types ] bi* append ;
412 : set-fortran-abi ( library -- )
413 library-fortran-abis get-global at fortran-abi set ;
415 : (fortran-invoke) ( return library function parameters -- quot )
417 [ 2nip [<fortran-result>] ]
418 [ nip nip nip [fortran-args>c-args] ]
420 [ 2nip [fortran-results>] ]
421 } 4 ncleave 4 nappend ;
423 MACRO: fortran-invoke ( return library function parameters -- )
424 { [ 2drop nip set-fortran-abi ] [ (fortran-invoke) ] } 4 ncleave ;
426 : parse-arglist ( parameters return -- types effect )
427 [ 2 group unzip [ "," ?tail drop ] map ]
428 [ [ { } ] [ 1array ] if-void ]
431 :: define-fortran-function ( return library function parameters -- )
432 function create-in dup reset-generic
433 return library function parameters return [ c:void ] unless* parse-arglist
434 [ \ fortran-invoke 5 [ ] nsequence ] dip define-declared ;
437 f "c-library" get scan ";" parse-tokens
438 [ "()" subseq? not ] filter define-fortran-function ;
441 scan "c-library" get scan ";" parse-tokens
442 [ "()" subseq? not ] filter define-fortran-function ;
447 [ set-fortran-abi ] bi ;