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