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