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