]> gitweb.factorcode.org Git - factor.git/blob - basis/alien/fortran/fortran.factor
65e927f85a50d00de4e3cc1602b276ec664db11e
[factor.git] / basis / alien / fortran / fortran.factor
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
10 IN: alien.fortran
11
12 SINGLETONS: f2c-abi g95-abi gfortran-abi intel-unix-abi intel-windows-abi ;
13
14 << 
15 : add-f2c-libraries ( -- )
16     "I77" "libI77.so" "cdecl" add-library
17     "F77" "libF77.so" "cdecl" add-library ;
18
19 os netbsd? [ add-f2c-libraries ] when
20 >>
21
22 : alien>nstring ( alien len encoding -- string )
23     [ memory>byte-array ] dip decode ;
24
25 ERROR: invalid-fortran-type type ;
26
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
31
32 SYMBOL: library-fortran-abis
33 SYMBOL: fortran-abi
34 library-fortran-abis [ H{ } clone ] initialize
35
36 <PRIVATE
37
38 : lowercase-name-with-underscore ( name -- name' )
39     >lower "_" append ;
40 : lowercase-name-with-extra-underscore ( name -- name' )
41     >lower CHAR: _ over member? 
42     [ "__" append ] [ "_" append ] if ;
43
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" ;
50
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 ;
57
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 ;
64
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 ;
71
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 ;
78
79 TUPLE: fortran-type dims size out? ;
80
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 ;
86
87 TUPLE: character-type < fortran-type ;
88 TUPLE: misc-type < fortran-type name ;
89
90 TUPLE: complex-type < number-type ;
91 TUPLE: real-complex-type < complex-type ;
92 TUPLE: double-complex-type < complex-type ;
93
94 CONSTANT: fortran>c-types H{
95     { "character"        character-type        }
96     { "integer"          integer-type          }
97     { "logical"          logical-type          }
98     { "real"             real-type             }
99     { "double-precision" double-precision-type }
100     { "complex"          real-complex-type     }
101     { "double-complex"   double-complex-type   }
102 }
103
104 : append-dimensions ( base-c-type type -- c-type )
105     dims>> [ product 2array ] when* ;
106
107 MACRO: size-case-type ( cases -- )
108     [ invalid-fortran-type ] suffix
109     '[ [ size>> _ case ] [ append-dimensions ] bi ] ;
110
111 : simple-type ( type base-c-type -- c-type )
112     swap
113     [ dup size>> [ invalid-fortran-type ] [ drop ] if ]
114     [ append-dimensions ] bi ;
115
116 : new-fortran-type ( out? dims size class -- type )
117     new [ [ (>>size) ] [ (>>dims) ] [ (>>out?) ] tri ] keep ;
118
119 GENERIC: (fortran-type>c-type) ( type -- c-type )
120
121 M: f (fortran-type>c-type) drop c:void ;
122
123 M: integer-type (fortran-type>c-type)
124     {
125         { f [ c:int      ] }
126         { 1 [ c:char     ] }
127         { 2 [ c:short    ] }
128         { 4 [ c:int      ] }
129         { 8 [ c:longlong ] }
130     } size-case-type ;
131 M: real-type (fortran-type>c-type)
132     {
133         { f [ c:float  ] }
134         { 4 [ c:float  ] }
135         { 8 [ c:double ] }
136     } size-case-type ;
137 M: real-complex-type (fortran-type>c-type)
138     {
139         {  f [ complex-float  ] }
140         {  8 [ complex-float  ] }
141         { 16 [ complex-double ] }
142     } size-case-type ;
143
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 ;
150
151 : single-char? ( character-type -- ? )
152     { [ drop character(1)-maps-to-char? ] [ dims>> product 1 = ] } 1&& ;
153
154 : fix-character-type ( character-type -- character-type' )
155     clone dup size>>
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 ;
159
160 M: character-type (fortran-type>c-type)
161     fix-character-type c:char simple-type ;
162
163 : dimension>number ( string -- number )
164     dup "*" = [ drop 0 ] [ string>number ] if ;
165
166 : parse-out ( string -- string' out? )
167     "!" ?head ;
168
169 : parse-dims ( string -- string' dim )
170     "(" split1 dup
171     [ ")" ?tail drop "," split [ [ blank? ] trim dimension>number ] map ] when ;
172
173 : parse-size ( string -- string' size )
174     "*" split1 dup [ string>number ] when ;
175
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 ;
180
181 : parse-fortran-type ( fortran-type-string/f -- type/f )
182     dup [ (parse-fortran-type) ] when ;
183
184 GENERIC: added-c-args ( type -- args )
185
186 M: fortran-type added-c-args drop { } ;
187 M: character-type added-c-args fix-character-type single-char? [ { } ] [ { c:long } ] if ;
188
189 GENERIC: returns-by-value? ( type -- ? )
190
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&& ;
197
198 GENERIC: (fortran-ret-type>c-type) ( type -- c-type )
199
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 ;
204
205 GENERIC: (fortran-arg>c-args) ( type -- main-quot added-quot )
206
207 : args?dims ( type quot -- main-quot added-quot )
208     [ dup dims>> [ drop [ ] [ drop ] ] ] dip if ; inline
209
210 M: integer-type (fortran-arg>c-args)
211     [
212         size>> {
213             { f [ [ <int>      ] [ drop ] ] }
214             { 1 [ [ <char>     ] [ drop ] ] }
215             { 2 [ [ <short>    ] [ drop ] ] }
216             { 4 [ [ <int>      ] [ drop ] ] }
217             { 8 [ [ <longlong> ] [ drop ] ] }
218             [ invalid-fortran-type ]
219         } case
220     ] args?dims ;
221
222 M: logical-type (fortran-arg>c-args)
223     [ call-next-method [ [ 1 0 ? ] prepend ] dip ] args?dims ;
224
225 M: real-type (fortran-arg>c-args)
226     [
227         size>> {
228             { f [ [ <float>  ] [ drop ] ] }
229             { 4 [ [ <float>  ] [ drop ] ] }
230             { 8 [ [ <double> ] [ drop ] ] }
231             [ invalid-fortran-type ]
232         } case
233     ] args?dims ;
234
235 M: real-complex-type (fortran-arg>c-args)
236     [
237         size>> {
238             {  f [ [ <complex-float>  ] [ drop ] ] }
239             {  8 [ [ <complex-float>  ] [ drop ] ] }
240             { 16 [ [ <complex-double> ] [ drop ] ] }
241             [ invalid-fortran-type ]
242         } case
243     ] args?dims ;
244
245 M: double-precision-type (fortran-arg>c-args)
246     [ drop [ <double> ] [ drop ] ] args?dims ;
247
248 M: double-complex-type (fortran-arg>c-args)
249     [ drop [ <complex-double> ] [ drop ] ] args?dims ;
250
251 M: character-type (fortran-arg>c-args)
252     fix-character-type single-char?
253     [ [ first <char> ] [ drop ] ]
254     [ [ ascii string>alien ] [ length ] ] if ;
255
256 M: misc-type (fortran-arg>c-args)
257     drop [ ] [ drop ] ;
258
259 GENERIC: (fortran-result>) ( type -- quots )
260
261 : result?dims ( type quot -- quot )
262     [ dup dims>> [ drop { [ ] } ] ] dip if ; inline
263
264 M: integer-type (fortran-result>)
265     [ size>> {
266         { f [ { [ *int      ] } ] }
267         { 1 [ { [ *char     ] } ] }
268         { 2 [ { [ *short    ] } ] }
269         { 4 [ { [ *int      ] } ] }
270         { 8 [ { [ *longlong ] } ] }
271         [ invalid-fortran-type ]
272     } case ] result?dims ;
273
274 M: logical-type (fortran-result>)
275     [ call-next-method first [ zero? not ] append 1array ] result?dims ;
276
277 M: real-type (fortran-result>)
278     [ size>> {
279         { f [ { [ *float  ] } ] }
280         { 4 [ { [ *float  ] } ] }
281         { 8 [ { [ *double ] } ] }
282         [ invalid-fortran-type ]
283     } case ] result?dims ;
284
285 M: real-complex-type (fortran-result>)
286     [ size>> {
287         {  f [ { [ *complex-float  ] } ] }
288         {  8 [ { [ *complex-float  ] } ] }
289         { 16 [ { [ *complex-double ] } ] }
290         [ invalid-fortran-type ]
291     } case ] result?dims ;
292
293 M: double-precision-type (fortran-result>)
294     [ drop { [ *double ] } ] result?dims ;
295
296 M: double-complex-type (fortran-result>)
297     [ drop { [ *complex-double ] } ] result?dims ;
298
299 M: character-type (fortran-result>)
300     fix-character-type single-char?
301     [ { [ *char 1string ] } ]
302     [ { [ ] [ ascii alien>nstring ] } ] if ;
303
304 M: misc-type (fortran-result>)
305     drop { [ ] } ;
306
307 GENERIC: (<fortran-result>) ( type -- quot )
308
309 M: fortran-type (<fortran-result>) 
310     (fortran-type>c-type) \ <c-object> [ ] 2sequence ;
311
312 M: character-type (<fortran-result>)
313     fix-character-type dims>> product dup
314     [ \ <byte-array> ] dip [ ] 3sequence ;
315
316 : [<fortran-result>] ( return parameters -- quot )
317     [ parse-fortran-type ] dip
318     over returns-by-value?
319     [ 2drop [ ] ]
320     [ [ (<fortran-result>) ] [ length \ ndip [ ] 3sequence ] bi* ] if ;
321
322 : [fortran-args>c-args] ( parameters -- quot )
323     [ [ ] ] [
324         [ parse-fortran-type (fortran-arg>c-args) 2array ] map flip first2
325         [ [ \ spread [ ] 2sequence ] bi@ 2array ] [ length ] bi 
326         \ ncleave [ ] 3sequence
327     ] if-empty ;
328
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
332     [args>args] 
333     c-return library c-function c-parameters \ alien-invoke
334     5 [ ] nsequence
335     c-parameters length \ nkeep
336     [ ] 3sequence ;
337
338 : [fortran-out-param>] ( parameter -- quot )
339     parse-fortran-type
340     [ (fortran-result>) ] [ out?>> ] bi
341     [ ] [ [ drop [ drop ] ] map ] if ;
342
343 : [fortran-return>] ( return -- quot )
344     parse-fortran-type {
345         { [ dup not ] [ drop { } ] }
346         { [ dup returns-by-value? ] [ drop { [ ] } ] }
347         [ (fortran-result>) ]
348     } cond ;
349
350 : letters ( -- seq ) CHAR: a CHAR: z [a,b] ;
351
352 : (shuffle-map) ( return parameters -- ret par )
353     [
354         fortran-ret-type>c-type length swap void? [ 1 + ] unless
355         letters swap head [ "ret" swap suffix ] map
356     ] [
357         [ fortran-arg-type>c-type nip length 1 + ] map letters swap zip
358         [ first2 letters swap head [ "" 2sequence ] with map ] map concat
359     ] bi* ;
360
361 : (fortran-in-shuffle) ( ret par -- seq )
362     [ second ] sort-with append ;
363
364 : (fortran-out-shuffle) ( ret par -- seq )
365     append ;
366
367 : [fortran-result-shuffle] ( return parameters -- quot )
368     (shuffle-map) [ (fortran-in-shuffle) ] [ (fortran-out-shuffle) ] 2bi <effect>
369     \ shuffle-effect [ ] 2sequence ;
370
371 : [fortran-results>] ( return parameters -- quot )
372     [ [fortran-result-shuffle] ]
373     [ drop [fortran-return>] ]
374     [ nip [ [fortran-out-param>] ] map concat ] 2tri
375     append
376     \ spread [ ] 2sequence append ;
377
378 : (add-fortran-library) ( fortran-abi name -- )
379     library-fortran-abis get-global set-at ;
380
381 PRIVATE>
382
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 ;
386
387 : fortran-name>symbol-name ( fortran-name -- c-name )
388     mangle-name ;
389
390 : fortran-type>c-type ( fortran-type -- c-type )
391     parse-fortran-type (fortran-type>c-type) ;
392
393 : fortran-arg-type>c-type ( fortran-type -- c-type added-args )
394     parse-fortran-type
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) { } ] [
400         c:void swap 
401         [ added-c-args ] [ (fortran-type>c-type) resolve-pointer-type ] bi prefix
402     ] if ;
403
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
407     append >array ;
408
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 ;
411
412 : set-fortran-abi ( library -- )
413     library-fortran-abis get-global at fortran-abi set ;
414
415 : (fortran-invoke) ( return library function parameters -- quot )
416     {
417         [ 2nip [<fortran-result>] ]
418         [ nip nip nip [fortran-args>c-args] ]
419         [ [fortran-invoke] ]
420         [ 2nip [fortran-results>] ]
421     } 4 ncleave 4 nappend ;
422
423 MACRO: fortran-invoke ( return library function parameters -- )
424     { [ 2drop nip set-fortran-abi ] [ (fortran-invoke) ] } 4 ncleave ;
425
426 : parse-arglist ( parameters return -- types effect )
427     [ 2 group unzip [ "," ?tail drop ] map ]
428     [ [ { } ] [ 1array ] if-void ]
429     bi* <effect> ;
430
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 ;
435
436 SYNTAX: SUBROUTINE: 
437     f "c-library" get scan ";" parse-tokens
438     [ "()" subseq? not ] filter define-fortran-function ;
439
440 SYNTAX: FUNCTION:
441     scan "c-library" get scan ";" parse-tokens
442     [ "()" subseq? not ] filter define-fortran-function ;
443
444 SYNTAX: LIBRARY:
445     scan
446     [ "c-library" set ]
447     [ set-fortran-abi ] bi ;
448