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