]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/cfg/builder/alien/alien.factor
core: Rename iota to <iota> so we can have TUPLE: iota ... ; instead of TUPLE: iota...
[factor.git] / basis / compiler / cfg / builder / alien / alien.factor
1 ! Copyright (C) 2008, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types alien.libraries alien.strings arrays
4 assocs classes.struct combinators compiler.cfg compiler.cfg.builder
5 compiler.cfg.builder.alien.boxing compiler.cfg.builder.alien.params
6 compiler.cfg.hats compiler.cfg.instructions compiler.cfg.registers
7 compiler.cfg.stacks compiler.cfg.stacks.local compiler.errors
8 compiler.tree cpu.architecture fry kernel layouts make math namespaces
9 sequences sequences.generalizations words ;
10 IN: compiler.cfg.builder.alien
11
12 : with-param-regs ( abi quot -- reg-values stack-values )
13     '[
14         param-regs init-regs
15         0 stack-params set
16         V{ } clone reg-values set
17         V{ } clone stack-values set
18         @
19         reg-values get
20         stack-values get
21         stack-params get
22         struct-return-area get
23     ] with-scope
24     struct-return-area set
25     stack-params set ; inline
26
27 : unbox-parameters ( parameters -- vregs reps )
28     [
29         [ length <iota> <reversed> ] keep
30         [ [ <ds-loc> peek-loc ] [ base-type ] bi* unbox-parameter ]
31         2 2 mnmap [ concat ] bi@
32     ]
33     [ length neg <ds-loc> inc-stack ] bi ;
34
35 : prepare-struct-caller ( vregs reps return -- vregs' reps' return-vreg/f )
36     dup large-struct? [
37         heap-size cell f ^^local-allot [
38             '[ _ prefix ]
39             [ int-rep struct-return-on-stack? f 3array prefix ] bi*
40         ] keep
41     ] [ drop f ] if ;
42
43 : (caller-parameters) ( vregs reps -- )
44     [ first3 next-parameter ] 2each ;
45
46 : caller-parameters ( params -- reg-inputs stack-inputs )
47     [ abi>> ] [ parameters>> ] [ return>> ] tri
48     '[
49         _ unbox-parameters
50         _ prepare-struct-caller struct-return-area set
51         (caller-parameters)
52     ] with-param-regs ;
53
54 : prepare-caller-return ( params -- reg-outputs )
55     return>> [ { } ] [ base-type load-return ] if-void ;
56
57 : caller-stack-cleanup ( params stack-size -- cleanup )
58     swap [ return>> ] [ abi>> ] bi stack-cleanup ;
59
60 : check-dlsym ( symbol library/f -- )
61     {
62         { [ dup library-dll dll-valid? not ] [
63             [ library-dll dll-path ] [ dlerror>> ] bi
64             cfg get word>> no-such-library-error drop
65         ] }
66         { [ 2dup library-dll dlsym not ] [
67             drop dlerror cfg get word>> no-such-symbol-error
68         ] }
69         [ 2drop ]
70     } cond ;
71
72 : caller-linkage ( params -- symbol dll/f )
73     [ function>> ] [ library>> lookup-library ] bi
74     2dup check-dlsym library-dll ;
75
76 : caller-return ( params -- )
77     return>> [ ] [
78         [
79             building get last reg-outputs>>
80             flip [ { } { } ] [ first2 ] if-empty
81         ] dip
82         base-type box-return ds-push
83     ] if-void ;
84
85 : params>alien-insn-params ( params --
86                              varargs? reg-inputs stack-inputs
87                              reg-outputs dead-outputs
88                              cleanup stack-size )
89     {
90         [ varargs?>> ]
91         [ caller-parameters ]
92         [ prepare-caller-return { } ]
93         [ stack-params get [ caller-stack-cleanup ] keep ]
94     } cleave ;
95
96 M: #alien-invoke emit-node ( block node -- block' )
97     params>>
98     [
99         [ params>alien-insn-params ]
100         [ caller-linkage ] bi
101         <gc-map> ##alien-invoke,
102     ]
103     [ caller-return ] bi ;
104
105 M: #alien-indirect emit-node ( block node -- block' )
106     params>>
107     [
108         [ ds-pop ^^unbox-any-c-ptr ] dip
109         params>alien-insn-params
110         <gc-map> ##alien-indirect,
111     ]
112     [ caller-return ] bi ;
113
114 M: #alien-assembly emit-node ( block node -- block' )
115     params>>
116     [
117         [ params>alien-insn-params ]
118         [ quot>> ] bi
119         ##alien-assembly,
120     ]
121     [ caller-return ] bi ;
122
123 : callee-parameter ( rep on-stack? odd-register? -- dst )
124     [ next-vreg dup ] 3dip next-parameter ;
125
126 : prepare-struct-callee ( c-type -- vreg )
127     large-struct?
128     [ int-rep struct-return-on-stack? f callee-parameter ] [ f ] if ;
129
130 : (callee-parameters) ( params -- vregs reps )
131     [ flatten-parameter-type ] map
132     [ [ [ first3 callee-parameter ] map ] map ]
133     [ [ keys ] map ] bi ;
134
135 : box-parameters ( vregs reps params -- )
136     parameters>> [ base-type box-parameter ds-push ] 3each ;
137
138 : callee-parameters ( params -- vregs reps reg-outputs stack-outputs )
139     [ abi>> ] [ return>> ] [ parameters>> ] tri
140     '[
141         _ prepare-struct-callee struct-return-area set
142         _ [ base-type ] map (callee-parameters)
143     ] with-param-regs ;
144
145 : callee-return ( params -- reg-inputs )
146     return>> [ { } ] [
147         [ ds-pop ] dip
148         base-type unbox-return store-return
149     ] if-void ;
150
151 : emit-callback-body ( block nodes -- block' )
152     dup last #return? t assert= but-last emit-nodes ;
153
154 : emit-callback-inputs ( params -- )
155     [ callee-parameters ##callback-inputs, ] keep box-parameters ;
156
157 : callback-stack-cleanup ( params -- )
158     [ xt>> ]
159     [ [ stack-params get ] dip [ return>> ] [ abi>> ] bi stack-cleanup ] bi
160     "stack-cleanup" set-word-prop ;
161
162 : emit-callback-return ( block params -- )
163     swap [ callee-return ##callback-outputs, ] [ drop ] if ;
164
165 : emit-callback-outputs ( block params -- )
166     [ emit-callback-return ] keep callback-stack-cleanup ;
167
168 M: #alien-callback emit-node ( block node -- block' )
169     dup params>> xt>> dup
170     [
171         t cfg get frame-pointer?<<
172         begin-word
173         over params>> emit-callback-inputs
174         over child>> emit-callback-body
175         [ swap params>> emit-callback-outputs ] keep
176         [ end-word drop ] when*
177     ] with-cfg-builder ;