]> gitweb.factorcode.org Git - factor.git/blob - basis/cpu/x86/32/32.factor
Fix conflict
[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 vm-ptr-size + [
137         push-vm-ptr
138         EDX PUSH
139         EAX PUSH
140         f %alien-invoke
141     ] with-aligned-stack ;
142
143 M:: x86.32 %box-large-struct ( n c-type -- )
144     ! Compute destination address
145     EDX n struct-return@ LEA
146     8 vm-ptr-size + [
147         push-vm-ptr
148         ! Push struct size
149         c-type heap-size PUSH
150         ! Push destination address
151         EDX PUSH
152         ! Copy the struct from the C stack
153         "box_value_struct" f %alien-invoke
154     ] with-aligned-stack ;
155
156 M: x86.32 %prepare-box-struct ( -- )
157     ! Compute target address for value struct return
158     EAX f struct-return@ LEA
159     ! Store it as the first parameter
160     0 stack@ EAX MOV ;
161
162 M: x86.32 %box-small-struct ( c-type -- )
163     #! Box a <= 8-byte struct returned in EAX:EDX. OS X only.
164     12 vm-ptr-size + [
165         push-vm-ptr
166         heap-size PUSH
167         EDX PUSH
168         EAX PUSH
169         "box_small_struct" f %alien-invoke
170     ] with-aligned-stack ;
171
172 M: x86.32 %prepare-unbox ( -- )
173     #! Move top of data stack to EAX.
174     EAX ESI [] MOV
175     ESI 4 SUB ;
176
177 : call-unbox-func ( func -- )
178     8 [
179         ! push the vm ptr as an argument
180         push-vm-ptr
181         ! Push parameter
182         EAX PUSH
183         ! Call the unboxer
184         f %alien-invoke
185     ] with-aligned-stack ;
186
187 M: x86.32 %unbox ( n rep func -- )
188     #! The value being unboxed must already be in EAX.
189     #! If n is f, we're unboxing a return value about to be
190     #! returned by the callback. Otherwise, we're unboxing
191     #! a parameter to a C function about to be called.
192     call-unbox-func
193     ! Store the return value on the C stack
194     over [ store-return-reg ] [ 2drop ] if ;
195
196 M: x86.32 %unbox-long-long ( n func -- )
197     call-unbox-func
198     ! Store the return value on the C stack
199     [
200         dup stack@ EAX MOV
201         cell + stack@ EDX MOV
202     ] when* ;
203
204 : %unbox-struct-1 ( -- )
205     #! Alien must be in EAX.
206     4 vm-ptr-size + [
207         push-vm-ptr
208         EAX PUSH
209         "alien_offset" f %alien-invoke
210         ! Load first cell
211         EAX EAX [] MOV
212     ] with-aligned-stack ;
213
214 : %unbox-struct-2 ( -- )
215     #! Alien must be in EAX.
216     4 vm-ptr-size + [
217         push-vm-ptr
218         EAX PUSH
219         "alien_offset" f %alien-invoke
220         ! Load second cell
221         EDX EAX 4 [+] MOV
222         ! Load first cell
223         EAX EAX [] MOV
224     ] with-aligned-stack ;
225
226 M: x86 %unbox-small-struct ( size -- )
227     #! Alien must be in EAX.
228     heap-size cell align cell /i {
229         { 1 [ %unbox-struct-1 ] }
230         { 2 [ %unbox-struct-2 ] }
231     } case ;
232
233 M:: x86.32 %unbox-large-struct ( n c-type -- )
234     ! Alien must be in EAX.
235     ! Compute destination address
236     EDX n stack@ LEA
237     12 vm-ptr-size + [
238         push-vm-ptr
239         ! Push struct size
240         c-type heap-size PUSH
241         ! Push destination address
242         EDX PUSH
243         ! Push source address
244         EAX PUSH
245         ! Copy the struct to the stack
246         "to_value_struct" f %alien-invoke
247     ] with-aligned-stack ;
248
249 M: x86.32 %prepare-alien-indirect ( -- )
250     push-vm-ptr "unbox_alien" f %alien-invoke
251     temp-reg POP
252     EBP EAX MOV ;
253
254 M: x86.32 %alien-indirect ( -- )
255     EBP CALL ;
256
257 M: x86.32 %alien-callback ( quot -- )
258     4 [
259         EAX swap %load-reference
260         EAX PUSH
261         param-reg-2 0 MOV rc-absolute-cell rt-vm rel-fixup 
262         "c_to_factor" f %alien-invoke
263     ] with-aligned-stack ;
264
265 M: x86.32 %callback-value ( ctype -- )
266     ! Align C stack
267     ESP 12 SUB
268     ! Save top of data stack in non-volatile register
269     %prepare-unbox
270     EAX PUSH
271     push-vm-ptr
272     ! Restore data/call/retain stacks
273     "unnest_stacks" f %alien-invoke
274     ! Place top of data stack in EAX
275     temp-reg POP
276     EAX POP
277     ! Restore C stack
278     ESP 12 ADD
279     ! Unbox EAX
280     unbox-return ;
281
282 M: x86.32 %cleanup ( params -- )
283     #! a) If we just called an stdcall function in Windows, it
284     #! cleaned up the stack frame for us. But we don't want that
285     #! so we 'undo' the cleanup since we do that in %epilogue.
286     #! b) If we just called a function returning a struct, we
287     #! have to fix ESP.
288     {
289         {
290             [ dup abi>> "stdcall" = ]
291             [ drop ESP stack-frame get params>> SUB ]
292         } {
293             [ dup return>> large-struct? ]
294             [ drop EAX PUSH ]
295         }
296         [ drop ]
297     } cond ;
298
299 M: x86.32 %callback-return ( n -- )
300     #! a) If the callback is stdcall, we have to clean up the
301     #! caller's stack frame.
302     #! b) If the callback is returning a large struct, we have
303     #! to fix ESP.
304     {
305         { [ dup abi>> "stdcall" = ] [
306             <alien-stack-frame>
307             [ params>> ] [ return>> ] bi +
308         ] }
309         { [ dup return>> large-struct? ] [ drop 4 ] }
310         [ drop 0 ]
311     } cond RET ;
312
313 M: x86.32 dummy-stack-params? f ;
314
315 M: x86.32 dummy-int-params? f ;
316
317 M: x86.32 dummy-fp-params? f ;
318
319 os windows? [
320     cell "longlong" c-type (>>align)
321     cell "ulonglong" c-type (>>align)
322     4 "double" c-type (>>align)
323 ] unless
324
325 check-sse