1 ! Copyright (C) 2005, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: locals alien.c-types alien.libraries alien.syntax arrays
4 kernel fry math namespaces sequences system layouts io
5 vocabs.loader accessors init combinators command-line make
6 compiler compiler.units compiler.constants compiler.alien
7 compiler.codegen compiler.codegen.fixup
8 compiler.cfg.instructions compiler.cfg.builder
9 compiler.cfg.intrinsics compiler.cfg.stack-frame
10 cpu.x86.assembler cpu.x86.assembler.operands cpu.x86
14 M: x86.32 machine-registers
16 { int-regs { EAX ECX EDX EBP EBX } }
17 { float-regs { XMM0 XMM1 XMM2 XMM3 XMM4 XMM5 XMM6 XMM7 } }
20 M: x86.32 ds-reg ESI ;
21 M: x86.32 rs-reg EDI ;
22 M: x86.32 stack-reg ESP ;
23 M: x86.32 temp-reg ECX ;
26 stack-frame get extra-stack-space dup 16 assert= + stack@ ;
28 M: x86.32 extra-stack-space calls-vm?>> 16 0 ? ;
31 drop HEX: ffffffff [+] card-mark <byte> MOV
33 rc-absolute-cell rel-cards-offset
37 drop HEX: ffffffff [+] card-mark <byte> MOV
39 rc-absolute-cell rel-decks-offset
42 M:: x86.32 %dispatch ( src temp -- )
43 ! Load jump table base.
44 temp src HEX: ffffffff [+] LEA
45 building get length cell - :> start
46 0 rc-absolute-cell rel-here
49 building get length :> end
50 ! Fix up the displacement above
52 [ end start - + building get dup pop* push ]
56 ! Registers for fastcall
57 : param-reg-1 ( -- reg ) EAX ;
58 : param-reg-2 ( -- reg ) EDX ;
60 M: x86.32 pic-tail-reg EBX ;
62 M: x86.32 reserved-stack-space 4 cells ;
64 M: x86.32 %alien-invoke 0 CALL rc-relative rel-dlsym ;
66 : save-vm-ptr ( n -- )
67 stack@ 0 MOV 0 rc-absolute-cell rel-vm ;
69 M: x86.32 return-struct-in-registers? ( c-type -- ? )
71 [ return-in-registers?>> ]
72 [ heap-size { 1 2 4 8 } member? ] bi
73 os { linux netbsd solaris } member? not
76 : struct-return@ ( n -- operand )
77 [ next-stack@ ] [ stack-frame get params>> local@ ] if* ;
79 ! On x86, parameters are never passed in registers.
80 M: int-regs return-reg drop EAX ;
81 M: int-regs param-regs drop { } ;
82 M: float-regs param-regs drop { } ;
84 GENERIC: load-return-reg ( src rep -- )
85 GENERIC: store-return-reg ( dst rep -- )
87 M: int-rep load-return-reg drop EAX swap MOV ;
88 M: int-rep store-return-reg drop EAX MOV ;
90 M: float-rep load-return-reg drop FLDS ;
91 M: float-rep store-return-reg drop FSTPS ;
93 M: double-rep load-return-reg drop FLDL ;
94 M: double-rep store-return-reg drop FSTPL ;
96 M: x86.32 %prologue ( n -- )
98 0 PUSH rc-absolute-cell rel-this
99 3 cells - decr-stack-reg ;
101 M: x86.32 %load-param-reg
103 [ [ EAX ] dip local@ MOV ] dip
106 M: x86.32 %save-param-reg 3drop ;
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 [ [ next-stack@ ] dip load-return-reg ] [ 2drop ] if ;
115 M:: x86.32 %box ( n rep func -- )
117 rep rep-size save-vm-ptr
118 0 stack@ rep store-return-reg
119 func f %alien-invoke ;
121 : (%box-long-long) ( n -- )
123 EDX over next-stack@ MOV
124 EAX swap cell - next-stack@ MOV
127 M: x86.32 %box-long-long ( n func -- )
128 [ (%box-long-long) ] dip
134 M:: x86.32 %box-large-struct ( n c-type -- )
135 EDX n struct-return@ LEA
137 4 stack@ c-type heap-size MOV
139 "box_value_struct" f %alien-invoke ;
141 M: x86.32 %prepare-box-struct ( -- )
142 ! Compute target address for value struct return
143 EAX f struct-return@ LEA
144 ! Store it as the first parameter
147 M: x86.32 %box-small-struct ( c-type -- )
148 #! Box a <= 8-byte struct returned in EAX:EDX. OS X only.
150 8 stack@ swap heap-size MOV
153 "box_small_struct" f %alien-invoke ;
155 M: x86.32 %prepare-unbox ( -- )
156 EAX swap ds-reg reg-stack MOV ;
158 : call-unbox-func ( func -- )
163 M: x86.32 %unbox ( n rep func -- )
164 #! The value being unboxed must already be in EAX.
165 #! If n is f, we're unboxing a return value about to be
166 #! returned by the callback. Otherwise, we're unboxing
167 #! a parameter to a C function about to be called.
169 ! Store the return value on the C stack
170 over [ [ local@ ] dip store-return-reg ] [ 2drop ] if ;
172 M: x86.32 %unbox-long-long ( n func -- )
174 ! Store the return value on the C stack
177 [ 4 + local@ EDX MOV ] bi
180 : %unbox-struct-1 ( -- )
181 #! Alien must be in EAX.
184 "alien_offset" f %alien-invoke
188 : %unbox-struct-2 ( -- )
189 #! Alien must be in EAX.
192 "alien_offset" f %alien-invoke
198 M: x86 %unbox-small-struct ( size -- )
199 #! Alien must be in EAX.
200 heap-size cell align cell /i {
201 { 1 [ %unbox-struct-1 ] }
202 { 2 [ %unbox-struct-2 ] }
205 M:: x86.32 %unbox-large-struct ( n c-type -- )
206 ! Alien must be in EAX.
207 ! Compute destination address
210 8 stack@ c-type heap-size MOV
213 "to_value_struct" f %alien-invoke ;
215 M: x86.32 %nest-stacks ( -- )
216 ! Save current frame. See comment in vm/contexts.hpp
217 EAX stack-reg stack-frame get total-size>> 3 cells - [+] LEA
220 "nest_stacks" f %alien-invoke ;
222 M: x86.32 %unnest-stacks ( -- )
224 "unnest_stacks" f %alien-invoke ;
226 M: x86.32 %prepare-alien-indirect ( -- )
228 "unbox_alien" f %alien-invoke
231 M: x86.32 %alien-indirect ( -- )
234 M: x86.32 %alien-callback ( quot -- )
236 param-reg-1 swap %load-reference
237 param-reg-2 %mov-vm-ptr
238 "c_to_factor" f %alien-invoke ;
240 M: x86.32 %callback-value ( ctype -- )
244 ! Restore data/call/retain stacks
245 "unnest_stacks" f %alien-invoke
246 ! Place former top of data stack back in EAX
251 GENERIC: float-function-param ( stack-slot dst src -- )
253 M:: spill-slot float-function-param ( stack-slot dst src -- )
254 ! We can clobber dst here since its going to contain the
256 dst src double-rep %copy
257 stack-slot dst double-rep %copy ;
259 M: register float-function-param
260 nip double-rep %copy ;
262 : float-function-return ( reg -- )
267 M:: x86.32 %unary-float-function ( dst src func -- )
268 ESP -16 [+] dst src float-function-param
270 func "libm" load-library %alien-invoke
271 dst float-function-return ;
273 M:: x86.32 %binary-float-function ( dst src1 src2 func -- )
274 ESP -16 [+] dst src1 float-function-param
275 ESP -8 [+] dst src2 float-function-param
277 func "libm" load-library %alien-invoke
278 dst float-function-return ;
280 M: x86.32 %cleanup ( params -- )
281 #! a) If we just called an stdcall function in Windows, it
282 #! cleaned up the stack frame for us. But we don't want that
283 #! so we 'undo' the cleanup since we do that in %epilogue.
284 #! b) If we just called a function returning a struct, we
288 [ dup abi>> "stdcall" = ]
289 [ drop ESP stack-frame get params>> SUB ]
291 [ dup return>> large-struct? ]
297 M: x86.32 %callback-return ( n -- )
298 #! a) If the callback is stdcall, we have to clean up the
299 #! caller's stack frame.
300 #! b) If the callback is returning a large struct, we have
303 { [ dup abi>> "stdcall" = ] [
305 [ params>> ] [ return>> ] bi +
307 { [ dup return>> large-struct? ] [ drop 4 ] }
311 M:: x86.32 %call-gc ( gc-root-count temp -- )
312 temp gc-root-base special@ LEA
314 4 stack@ gc-root-count MOV
316 "inline_gc" f %alien-invoke ;
318 M: x86.32 dummy-stack-params? f ;
320 M: x86.32 dummy-int-params? f ;
322 M: x86.32 dummy-fp-params? f ;
325 M: object flatten-value-type (flatten-int-type) ;