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