]> gitweb.factorcode.org Git - factor.git/blob - basis/cpu/x86/32/32.factor
added code to pass vm ptr to some unboxers
[factor.git] / basis / cpu / x86 / 32 / 32.factor
1 ! Copyright (C) 2005, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: locals alien.c-types alien.syntax arrays kernel fry math
4 namespaces sequences system layouts io vocabs.loader accessors init
5 combinators command-line make compiler compiler.units
6 compiler.constants compiler.alien compiler.codegen
7 compiler.codegen.fixup compiler.cfg.instructions compiler.cfg.builder
8 compiler.cfg.intrinsics compiler.cfg.stack-frame cpu.x86.assembler
9 cpu.x86.assembler.operands cpu.x86 cpu.architecture ;
10 IN: cpu.x86.32
11
12 ! We implement the FFI for Linux, OS X and Windows all at once.
13 ! OS X requires that the stack be 16-byte aligned.
14
15 M: x86.32 machine-registers
16     {
17         { int-regs { EAX ECX EDX EBP EBX } }
18         { float-regs { XMM0 XMM1 XMM2 XMM3 XMM4 XMM5 XMM6 XMM7 } }
19     } ;
20
21 M: x86.32 ds-reg ESI ;
22 M: x86.32 rs-reg EDI ;
23 M: x86.32 stack-reg ESP ;
24 M: x86.32 temp-reg ECX ;
25
26 M:: x86.32 %dispatch ( src temp -- )
27     ! Load jump table base.
28     temp src HEX: ffffffff [+] LEA
29     building get length cell - :> start
30     0 rc-absolute-cell rel-here
31     ! Go
32     temp HEX: 7f [+] JMP
33     building get length :> end
34     ! Fix up the displacement above
35     cell code-alignment
36     [ end start - + building get dup pop* push ]
37     [ align-code ]
38     bi ;
39
40 ! Registers for fastcall
41 M: x86.32 param-reg-1 EAX ;
42 M: x86.32 param-reg-2 EDX ;
43
44 M: x86.32 pic-tail-reg EBX ;
45
46 M: x86.32 reserved-area-size 0 ;
47
48 M: x86.32 %alien-invoke 0 CALL rc-relative rel-dlsym ;
49
50 M: x86.32 %vm-invoke ( function -- )
51     temp-reg 0 MOV rc-absolute-cell rt-vm rel-fixup ! push the vm ptr as an argument
52     temp-reg PUSH
53     f %alien-invoke
54     temp-reg POP ;
55
56 M: x86.32 return-struct-in-registers? ( c-type -- ? )
57     c-type
58     [ return-in-registers?>> ]
59     [ heap-size { 1 2 4 8 } member? ] bi
60     os { linux netbsd solaris } member? not
61     and or ;
62
63 : struct-return@ ( n -- operand )
64     [ next-stack@ ] [ stack-frame get params>> stack@ ] if* ;
65
66 ! On x86, parameters are never passed in registers.
67 M: int-regs return-reg drop EAX ;
68 M: int-regs param-regs drop { } ;
69 M: float-regs param-regs drop { } ;
70
71 GENERIC: push-return-reg ( rep -- )
72 GENERIC: load-return-reg ( n rep -- )
73 GENERIC: store-return-reg ( n rep -- )
74
75 M: int-rep push-return-reg drop EAX PUSH ;
76 M: int-rep load-return-reg drop EAX swap next-stack@ MOV ;
77 M: int-rep store-return-reg drop stack@ EAX MOV ;
78
79 M: float-rep push-return-reg drop ESP 4 SUB ESP [] FSTPS ;
80 M: float-rep load-return-reg drop next-stack@ FLDS ;
81 M: float-rep store-return-reg drop stack@ FSTPS ;
82
83 M: double-rep push-return-reg drop ESP 8 SUB ESP [] FSTPL ;
84 M: double-rep load-return-reg drop next-stack@ FLDL ;
85 M: double-rep store-return-reg drop stack@ FSTPL ;
86
87 : align-sub ( n -- )
88     [ align-stack ] keep - decr-stack-reg ;
89
90 : align-add ( n -- )
91     align-stack incr-stack-reg ;
92
93 : with-aligned-stack ( n quot -- )
94     '[ align-sub @ ] [ align-add ] bi ; inline
95
96 M: x86.32 %prologue ( n -- )
97     dup PUSH
98     0 PUSH rc-absolute-cell rel-this
99     3 cells - decr-stack-reg ;
100
101 M: x86.32 %load-param-reg 3drop ;
102
103 M: x86.32 %save-param-reg 3drop ;
104
105 : (%box) ( n rep -- )
106     #! If n is f, push the return register onto the stack; we
107     #! are boxing a return value of a C function. If n is an
108     #! integer, push [ESP+n] on the stack; we are boxing a
109     #! parameter being passed to a callback from C.
110     over [ load-return-reg ] [ 2drop ] if ;
111
112 M:: x86.32 %box ( n rep func -- )
113     n rep (%box)
114     rep rep-size [
115         rep push-return-reg
116         func f %alien-invoke
117     ] with-aligned-stack ;
118     
119 : (%box-long-long) ( n -- )
120     [
121         EDX over next-stack@ MOV
122         EAX swap cell - next-stack@ MOV 
123     ] when* ;
124
125 M: x86.32 %box-long-long ( n func -- )
126     [ (%box-long-long) ] dip
127     8 [
128         EDX PUSH
129         EAX PUSH
130         f %alien-invoke
131     ] with-aligned-stack ;
132
133 M:: x86.32 %box-large-struct ( n c-type -- )
134     ! Compute destination address
135     ECX n struct-return@ LEA
136     8 [
137         ! Push struct size
138         c-type heap-size PUSH
139         ! Push destination address
140         ECX PUSH
141         ! Copy the struct from the C stack
142         "box_value_struct" f %alien-invoke
143     ] with-aligned-stack ;
144
145 M: x86.32 %prepare-box-struct ( -- )
146     ! Compute target address for value struct return
147     EAX f struct-return@ LEA
148     ! Store it as the first parameter
149     0 stack@ EAX MOV ;
150
151 M: x86.32 %box-small-struct ( c-type -- )
152     #! Box a <= 8-byte struct returned in EAX:EDX. OS X only.
153     12 [
154         heap-size PUSH
155         EDX PUSH
156         EAX PUSH
157         "box_small_struct" f %alien-invoke
158     ] with-aligned-stack ;
159
160 M: x86.32 %prepare-unbox ( -- )
161     #! Move top of data stack to EAX.
162     EAX ESI [] MOV
163     ESI 4 SUB ;
164
165 : call-unbox-func ( func -- )
166     8 [
167         ! push vm ptr
168         temp-reg 0 MOV rc-absolute-cell rt-vm rel-fixup ! push the vm ptr as an argument
169         temp-reg PUSH
170         ! Push parameter
171         EAX PUSH
172         ! Call the unboxer
173         f %alien-invoke
174     ] with-aligned-stack ;
175
176 M: x86.32 %unbox ( n rep func -- )
177     #! The value being unboxed must already be in EAX.
178     #! If n is f, we're unboxing a return value about to be
179     #! returned by the callback. Otherwise, we're unboxing
180     #! a parameter to a C function about to be called.
181     call-unbox-func
182     ! Store the return value on the C stack
183     over [ store-return-reg ] [ 2drop ] if ;
184
185 M: x86.32 %unbox-long-long ( n func -- )
186     call-unbox-func
187     ! Store the return value on the C stack
188     [
189         dup stack@ EAX MOV
190         cell + stack@ EDX MOV
191     ] when* ;
192
193 : %unbox-struct-1 ( -- )
194     #! Alien must be in EAX.
195     4 [
196         EAX PUSH
197         "alien_offset" f %alien-invoke
198         ! Load first cell
199         EAX EAX [] MOV
200     ] with-aligned-stack ;
201
202 : %unbox-struct-2 ( -- )
203     #! Alien must be in EAX.
204     4 [
205         EAX PUSH
206         "alien_offset" f %alien-invoke
207         ! Load second cell
208         EDX EAX 4 [+] MOV
209         ! Load first cell
210         EAX EAX [] MOV
211     ] with-aligned-stack ;
212
213 M: x86 %unbox-small-struct ( size -- )
214     #! Alien must be in EAX.
215     heap-size cell align cell /i {
216         { 1 [ %unbox-struct-1 ] }
217         { 2 [ %unbox-struct-2 ] }
218     } case ;
219
220 M:: x86.32 %unbox-large-struct ( n c-type -- )
221     ! Alien must be in EAX.
222     ! Compute destination address
223     ECX n stack@ LEA
224     12 [
225         ! Push struct size
226         c-type heap-size PUSH
227         ! Push destination address
228         ECX PUSH
229         ! Push source address
230         EAX PUSH
231         ! Copy the struct to the stack
232         "to_value_struct" f %alien-invoke
233     ] with-aligned-stack ;
234
235 M: x86.32 %prepare-alien-indirect ( -- )
236     "unbox_alien" f %alien-invoke
237     EBP EAX MOV ;
238
239 M: x86.32 %alien-indirect ( -- )
240     EBP CALL ;
241
242 M: x86.32 %alien-callback ( quot -- )
243     4 [
244         EAX swap %load-reference
245         EAX PUSH
246         param-reg-2 0 MOV rc-absolute-cell rt-vm rel-fixup 
247         "c_to_factor" f %alien-invoke
248     ] with-aligned-stack ;
249
250 M: x86.32 %callback-value ( ctype -- )
251     ! Align C stack
252     ESP 12 SUB
253     ! Save top of data stack in non-volatile register
254     %prepare-unbox
255     EAX PUSH
256     ! Restore data/call/retain stacks
257     "unnest_stacks" f %alien-invoke
258     ! Place top of data stack in EAX
259     EAX POP
260     ! Restore C stack
261     ESP 12 ADD
262     ! Unbox EAX
263     unbox-return ;
264
265 M: x86.32 %cleanup ( params -- )
266     #! a) If we just called an stdcall function in Windows, it
267     #! cleaned up the stack frame for us. But we don't want that
268     #! so we 'undo' the cleanup since we do that in %epilogue.
269     #! b) If we just called a function returning a struct, we
270     #! have to fix ESP.
271     {
272         {
273             [ dup abi>> "stdcall" = ]
274             [ drop ESP stack-frame get params>> SUB ]
275         } {
276             [ dup return>> large-struct? ]
277             [ drop EAX PUSH ]
278         }
279         [ drop ]
280     } cond ;
281
282 M: x86.32 %callback-return ( n -- )
283     #! a) If the callback is stdcall, we have to clean up the
284     #! caller's stack frame.
285     #! b) If the callback is returning a large struct, we have
286     #! to fix ESP.
287     {
288         { [ dup abi>> "stdcall" = ] [
289             <alien-stack-frame>
290             [ params>> ] [ return>> ] bi +
291         ] }
292         { [ dup return>> large-struct? ] [ drop 4 ] }
293         [ drop 0 ]
294     } cond RET ;
295
296 M: x86.32 dummy-stack-params? f ;
297
298 M: x86.32 dummy-int-params? f ;
299
300 M: x86.32 dummy-fp-params? f ;
301
302 os windows? [
303     cell "longlong" c-type (>>align)
304     cell "ulonglong" c-type (>>align)
305     4 "double" c-type (>>align)
306 ] unless
307
308 "cpu.x86.features" require