]> gitweb.factorcode.org Git - factor.git/blob - extra/alien/inline/inline.factor
Fixing failing unit tests in compiler.tree.propagation due to constraints
[factor.git] / extra / alien / inline / inline.factor
1 ! Copyright (C) 2009 Jeremy Hughes.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.inline.compiler alien.inline.types
4 alien.libraries alien.parser arrays assocs effects fry
5 generalizations grouping io.directories io.files
6 io.files.info io.files.temp kernel lexer math math.order
7 math.ranges multiline namespaces sequences source-files
8 splitting strings system vocabs.loader vocabs.parser words
9 alien.c-types alien.structs make parser continuations ;
10 IN: alien.inline
11
12 SYMBOL: c-library
13 SYMBOL: library-is-c++
14 SYMBOL: linker-args
15 SYMBOL: c-strings
16
17 <PRIVATE
18 : cleanup-variables ( -- )
19     { c-library library-is-c++ linker-args c-strings }
20     [ off ] each ;
21
22 : arg-list ( types -- params )
23     CHAR: a swap length CHAR: a + [a,b]
24     [ 1string ] map ;
25
26 : compile-library? ( -- ? )
27     c-library get library-path dup exists? [
28         file get [
29             path>>
30             [ file-info modified>> ] bi@ <=> +lt+ =
31         ] [ drop t ] if*
32     ] [ drop t ] if ;
33
34 : compile-library ( -- )
35     library-is-c++ get [ C++ ] [ C ] if
36     linker-args get
37     c-strings get "\n" join
38     c-library get compile-to-library ;
39
40 : c-library-name ( name -- name' )
41     [ current-vocab name>> % "_" % % ] "" make ;
42 PRIVATE>
43
44 : append-function-body ( prototype-str body -- str )
45     [ swap % " {\n" % % "\n}\n" % ] "" make ;
46
47 : function-types-effect ( -- function types effect )
48     scan scan swap ")" parse-tokens
49     [ "(" subseq? not ] filter swap parse-arglist ;
50
51 : prototype-string ( function types effect -- str )
52     [ [ cify-type ] map ] dip
53     types-effect>params-return cify-type -rot
54     [ " " join ] map ", " join
55     "(" prepend ")" append 3array " " join
56     library-is-c++ get [ "extern \"C\" " prepend ] when ;
57
58 : prototype-string' ( function types return -- str )
59     [ dup arg-list ] <effect> prototype-string ;
60
61 : factor-function ( function types effect -- word quot effect )
62     annotate-effect [ c-library get ] 3dip
63     [ [ factorize-type ] map ] dip
64     types-effect>params-return factorize-type -roll
65     concat make-function ;
66
67 : define-c-library ( name -- )
68     c-library-name [ c-library set ] [ "c-library" set ] bi
69     V{ } clone c-strings set
70     V{ } clone linker-args set ;
71
72 : compile-c-library ( -- )
73     compile-library? [ compile-library ] when
74     c-library get dup library-path "cdecl" add-library ;
75
76 : define-c-function ( function types effect body -- )
77     [
78         [ factor-function define-declared ]
79         [ prototype-string ] 3bi
80     ] dip append-function-body c-strings get push ;
81
82 : define-c-function' ( function effect body -- )
83     [
84         [ in>> ] keep
85         [ factor-function define-declared ]
86         [ out>> prototype-string' ] 3bi
87     ] dip append-function-body c-strings get push ;
88
89 : c-link-to ( str -- )
90     "-l" prepend linker-args get push ;
91
92 : c-use-framework ( str -- )
93     "-framework" swap linker-args get '[ _ push ] bi@ ;
94
95 : c-link-to/use-framework ( str -- )
96     os macosx? [ c-use-framework ] [ c-link-to ] if ;
97
98 : c-include ( str -- )
99     "#include " prepend c-strings get push ;
100
101 : define-c-typedef ( old new -- )
102     [ typedef ] [
103         [ swap "typedef " % % " " % % ";" % ]
104         "" make c-strings get push
105     ] 2bi ;
106
107 : define-c-struct ( name fields -- )
108     [ current-vocab swap define-struct ] [
109         over
110         [
111             "typedef struct " % "_" % % " {\n" %
112             [ first2 swap % " " % % ";\n" % ] each
113             "} " % % ";\n" %
114         ] "" make c-strings get push
115     ] 2bi ;
116
117 : delete-inline-library ( name -- )
118     c-library-name [ remove-library ]
119     [ library-path dup exists? [ delete-file ] [ drop ] if ] bi ;
120
121 : with-c-library ( name quot -- )
122     [ [ define-c-library ] dip call compile-c-library ]
123     [ cleanup-variables ] [ ] cleanup ; inline
124
125 : raw-c ( str -- )
126     [ "\n" % % "\n" % ] "" make c-strings get push ;