]> gitweb.factorcode.org Git - factor.git/blob - basis/cpu/x86/32/32.factor
moved %(un)nest-stacks out to cpu specific files to eliminate %vm-invoke from compile...
[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 : param-reg-1 ( -- reg ) EAX ;
42 : param-reg-2 ( -- reg ) 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 : push-vm-ptr ( -- )
51     0 PUSH rc-absolute-cell rt-vm rel-fixup ; ! push the vm ptr as an argument
52
53 M: x86.32 return-struct-in-registers? ( c-type -- ? )
54     c-type
55     [ return-in-registers?>> ]
56     [ heap-size { 1 2 4 8 } member? ] bi
57     os { linux netbsd solaris } member? not
58     and or ;
59
60 : struct-return@ ( n -- operand )
61     [ next-stack@ ] [ stack-frame get params>> stack@ ] if* ;
62
63 ! On x86, parameters are never passed in registers.
64 M: int-regs return-reg drop EAX ;
65 M: int-regs param-regs drop { } ;
66 M: float-regs param-regs drop { } ;
67
68 GENERIC: push-return-reg ( rep -- )
69 GENERIC: load-return-reg ( n rep -- )
70 GENERIC: store-return-reg ( n rep -- )
71
72 M: int-rep push-return-reg drop EAX PUSH ;
73 M: int-rep load-return-reg drop EAX swap next-stack@ MOV ;
74 M: int-rep store-return-reg drop stack@ EAX MOV ;
75
76 M: float-rep push-return-reg drop ESP 4 SUB ESP [] FSTPS ;
77 M: float-rep load-return-reg drop next-stack@ FLDS ;
78 M: float-rep store-return-reg drop stack@ FSTPS ;
79
80 M: double-rep push-return-reg drop ESP 8 SUB ESP [] FSTPL ;
81 M: double-rep load-return-reg drop next-stack@ FLDL ;
82 M: double-rep store-return-reg drop stack@ FSTPL ;
83
84 : align-sub ( n -- )
85     [ align-stack ] keep - decr-stack-reg ;
86
87 : align-add ( n -- )
88     align-stack incr-stack-reg ;
89
90 : with-aligned-stack ( n quot -- )
91     '[ align-sub @ ] [ align-add ] bi ; inline
92
93 M: x86.32 %prologue ( n -- )
94     dup PUSH
95     0 PUSH rc-absolute-cell rel-this
96     3 cells - decr-stack-reg ;
97
98 M: x86.32 %load-param-reg 3drop ;
99
100 M: x86.32 %save-param-reg 3drop ;
101
102 : (%box) ( n rep -- )
103     #! If n is f, push the return register onto the stack; we
104     #! are boxing a return value of a C function. If n is an
105     #! integer, push [ESP+n] on the stack; we are boxing a
106     #! parameter being passed to a callback from C.
107     over [ load-return-reg ] [ 2drop ] if ;
108
109 CONSTANT: vm-ptr-size 4
110
111 M:: x86.32 %box ( n rep func -- )
112     n rep (%box)
113     rep rep-size vm-ptr-size + [
114         push-vm-ptr
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 vm-ptr-size + [
128         push-vm-ptr
129         EDX PUSH
130         EAX PUSH
131         f %alien-invoke
132     ] with-aligned-stack ;
133
134 M:: x86.32 %box-large-struct ( n c-type -- )
135     ! Compute destination address
136     EDX n struct-return@ LEA
137     8 vm-ptr-size + [
138         push-vm-ptr
139         ! Push struct size
140         c-type heap-size PUSH
141         ! Push destination address
142         EDX PUSH
143         ! Copy the struct from the C stack
144         "box_value_struct" f %alien-invoke
145     ] with-aligned-stack ;
146
147 M: x86.32 %prepare-box-struct ( -- )
148     ! Compute target address for value struct return
149     EAX f struct-return@ LEA
150     ! Store it as the first parameter
151     0 stack@ EAX MOV ;
152
153 M: x86.32 %box-small-struct ( c-type -- )
154     #! Box a <= 8-byte struct returned in EAX:EDX. OS X only.
155     12 vm-ptr-size + [
156         push-vm-ptr
157         heap-size PUSH
158         EDX PUSH
159         EAX PUSH
160         "box_small_struct" f %alien-invoke
161     ] with-aligned-stack ;
162
163 M: x86.32 %prepare-unbox ( -- )
164     #! Move top of data stack to EAX.
165     EAX ESI [] MOV
166     ESI 4 SUB ;
167
168 : call-unbox-func ( func -- )
169     8 [
170         ! push the vm ptr as an argument
171         push-vm-ptr
172         ! Push parameter
173         EAX PUSH
174         ! Call the unboxer
175         f %alien-invoke
176     ] with-aligned-stack ;
177
178 M: x86.32 %unbox ( n rep func -- )
179     #! The value being unboxed must already be in EAX.
180     #! If n is f, we're unboxing a return value about to be
181     #! returned by the callback. Otherwise, we're unboxing
182     #! a parameter to a C function about to be called.
183     call-unbox-func
184     ! Store the return value on the C stack
185     over [ store-return-reg ] [ 2drop ] if ;
186
187 M: x86.32 %unbox-long-long ( n func -- )
188     call-unbox-func
189     ! Store the return value on the C stack
190     [
191         dup stack@ EAX MOV
192         cell + stack@ EDX MOV
193     ] when* ;
194
195 : %unbox-struct-1 ( -- )
196     #! Alien must be in EAX.
197     4 vm-ptr-size + [
198         push-vm-ptr
199         EAX PUSH
200         "alien_offset" f %alien-invoke
201         ! Load first cell
202         EAX EAX [] MOV
203     ] with-aligned-stack ;
204
205 : %unbox-struct-2 ( -- )
206     #! Alien must be in EAX.
207     4 vm-ptr-size + [
208         push-vm-ptr
209         EAX PUSH
210         "alien_offset" f %alien-invoke
211         ! Load second cell
212         EDX EAX 4 [+] MOV
213         ! Load first cell
214         EAX EAX [] MOV
215     ] with-aligned-stack ;
216
217 M: x86 %unbox-small-struct ( size -- )
218     #! Alien must be in EAX.
219     heap-size cell align cell /i {
220         { 1 [ %unbox-struct-1 ] }
221         { 2 [ %unbox-struct-2 ] }
222     } case ;
223
224 M:: x86.32 %unbox-large-struct ( n c-type -- )
225     ! Alien must be in EAX.
226     ! Compute destination address
227     EDX n stack@ LEA
228     12 vm-ptr-size + [
229         push-vm-ptr
230         ! Push struct size
231         c-type heap-size PUSH
232         ! Push destination address
233         EDX PUSH
234         ! Push source address
235         EAX PUSH
236         ! Copy the struct to the stack
237         "to_value_struct" f %alien-invoke
238     ] with-aligned-stack ;
239
240 M: x86.32 %nest-stacks ( -- )
241     4 [
242         push-vm-ptr
243         "nest_stacks" f %alien-invoke
244     ] with-aligned-stack ;
245
246 M: x86.32 %unnest-stacks ( -- )
247     4 [
248         push-vm-ptr
249         "unnest_stacks" f %alien-invoke
250     ] with-aligned-stack ;
251
252 M: x86.32 %prepare-alien-indirect ( -- )
253     push-vm-ptr "unbox_alien" f %alien-invoke
254     temp-reg POP
255     EBP EAX MOV ;
256
257 M: x86.32 %alien-indirect ( -- )
258     EBP CALL ;
259
260 M: x86.32 %alien-callback ( quot -- )
261     4 [
262         EAX swap %load-reference
263         EAX PUSH
264         param-reg-2 0 MOV rc-absolute-cell rt-vm rel-fixup 
265         "c_to_factor" f %alien-invoke
266     ] with-aligned-stack ;
267
268 M: x86.32 %callback-value ( ctype -- )
269     ! Align C stack
270     ESP 12 SUB
271     ! Save top of data stack in non-volatile register
272     %prepare-unbox
273     EAX PUSH
274     push-vm-ptr
275     ! Restore data/call/retain stacks
276     "unnest_stacks" f %alien-invoke
277     ! Place top of data stack in EAX
278     temp-reg POP
279     EAX POP
280     ! Restore C stack
281     ESP 12 ADD
282     ! Unbox EAX
283     unbox-return ;
284
285
286 M: x86.32 %cleanup ( params -- )
287     #! a) If we just called an stdcall function in Windows, it
288     #! cleaned up the stack frame for us. But we don't want that
289     #! so we 'undo' the cleanup since we do that in %epilogue.
290     #! b) If we just called a function returning a struct, we
291     #! have to fix ESP.
292     {
293         {
294             [ dup abi>> "stdcall" = ]
295             [ drop ESP stack-frame get params>> SUB ]
296         } {
297             [ dup return>> large-struct? ]
298             [ drop EAX PUSH ]
299         }
300         [ drop ]
301     } cond ;
302
303 M: x86.32 %callback-return ( n -- )
304     #! a) If the callback is stdcall, we have to clean up the
305     #! caller's stack frame.
306     #! b) If the callback is returning a large struct, we have
307     #! to fix ESP.
308     {
309         { [ dup abi>> "stdcall" = ] [
310             <alien-stack-frame>
311             [ params>> ] [ return>> ] bi +
312         ] }
313         { [ dup return>> large-struct? ] [ drop 4 ] }
314         [ drop 0 ]
315     } cond RET ;
316
317 M:: x86.32 %call-gc ( gc-root-count temp1 -- )
318     ! USE: prettyprint "PHIL" pprint temp1 pprint temp2 pprint
319     temp1 gc-root-base param@ LEA
320     12 [
321         0 PUSH rc-absolute-cell rt-vm rel-fixup ! push the vm ptr as an argument
322         ! Pass number of roots as second parameter
323         gc-root-count PUSH 
324         ! Pass pointer to start of GC roots as first parameter
325         temp1 PUSH 
326         ! Call GC
327         "inline_gc" f %alien-invoke
328     ] with-aligned-stack ;
329
330 M: x86.32 dummy-stack-params? f ;
331
332 M: x86.32 dummy-int-params? f ;
333
334 M: x86.32 dummy-fp-params? f ;
335
336 os windows? [
337     cell "longlong" c-type (>>align)
338     cell "ulonglong" c-type (>>align)
339     4 "double" c-type (>>align)
340 ] unless
341
342 check-sse