1 ! Copyright (C) 2005, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: locals alien alien.c-types alien.libraries alien.syntax
4 arrays kernel fry math namespaces sequences system layouts io
5 vocabs.loader accessors init classes.struct combinators command-line
6 make 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
12 FROM: layouts => cell ;
13 FROM: alien.private => fastcall ;
16 M: x86.32 machine-registers
18 { int-regs { EAX ECX EDX EBP EBX } }
19 { float-regs { XMM0 XMM1 XMM2 XMM3 XMM4 XMM5 XMM6 XMM7 } }
22 M: x86.32 ds-reg ESI ;
23 M: x86.32 rs-reg EDI ;
24 M: x86.32 stack-reg ESP ;
25 M: x86.32 frame-reg EBP ;
26 M: x86.32 temp-reg ECX ;
28 M: x86.32 %mov-vm-ptr ( reg -- )
29 0 MOV 0 rc-absolute-cell rel-vm ;
31 M: x86.32 %vm-field ( dst field -- )
32 [ 0 [] MOV ] dip rc-absolute-cell rel-vm ;
34 M: x86.32 %set-vm-field ( dst field -- )
35 [ 0 [] swap MOV ] dip rc-absolute-cell rel-vm ;
37 M: x86.32 %vm-field-ptr ( dst field -- )
38 [ 0 MOV ] dip rc-absolute-cell rel-vm ;
41 stack-frame get extra-stack-space dup 16 assert= + stack@ ;
43 M: x86.32 extra-stack-space calls-vm?>> 16 0 ? ;
46 drop HEX: ffffffff [+] card-mark <byte> MOV
48 rc-absolute-cell rel-cards-offset
52 drop HEX: ffffffff [+] card-mark <byte> MOV
54 rc-absolute-cell rel-decks-offset
57 M:: x86.32 %dispatch ( src temp -- )
58 ! Load jump table base.
59 temp src HEX: ffffffff [+] LEA
60 building get length :> start
61 0 rc-absolute-cell rel-here
64 building get length :> end
65 ! Fix up the displacement above
67 [ end start - + building get dup pop* push ]
71 M: x86.32 pic-tail-reg EBX ;
73 M: x86.32 reserved-stack-space 0 ;
75 M: x86.32 %alien-invoke 0 CALL rc-relative rel-dlsym ;
77 : save-vm-ptr ( n -- )
78 stack@ 0 MOV 0 rc-absolute-cell rel-vm ;
80 M: x86.32 return-struct-in-registers? ( c-type -- ? )
82 [ return-in-registers?>> ]
83 [ heap-size { 1 2 4 8 } member? ] bi
84 os { linux netbsd solaris } member? not
87 : struct-return@ ( n -- operand )
88 [ next-stack@ ] [ stack-frame get params>> local@ ] if* ;
90 ! On x86, parameters are usually never passed in registers, except with Microsoft's
91 ! "thiscall" and "fastcall" abis
92 M: int-regs return-reg drop EAX ;
93 M: float-regs param-regs 2drop { } ;
95 M: int-regs param-regs
97 { thiscall [ { ECX } ] }
98 { fastcall [ { ECX EDX } ] }
102 GENERIC: load-return-reg ( src rep -- )
103 GENERIC: store-return-reg ( dst rep -- )
105 M: stack-params load-return-reg drop EAX swap MOV ;
106 M: stack-params store-return-reg drop EAX MOV ;
108 M: int-rep load-return-reg drop EAX swap MOV ;
109 M: int-rep store-return-reg drop EAX MOV ;
111 M: float-rep load-return-reg drop FLDS ;
112 M: float-rep store-return-reg drop FSTPS ;
114 M: double-rep load-return-reg drop FLDL ;
115 M: double-rep store-return-reg drop FSTPL ;
117 M: x86.32 %prologue ( n -- )
119 0 PUSH rc-absolute-cell rel-this
120 3 cells - decr-stack-reg ;
122 M: x86.32 %prepare-jump
123 pic-tail-reg 0 MOV xt-tail-pic-offset rc-absolute-cell rel-here ;
125 M: stack-params copy-register*
128 { [ dup integer? ] [ EAX swap next-stack@ MOV EAX MOV ] }
129 { [ over integer? ] [ EAX swap MOV param@ EAX MOV ] }
132 M: x86.32 %save-param-reg
133 dup stack-params? [ 3drop ] [ [ param@ ] 2dip %copy ] if ;
135 M: x86.32 %load-param-reg [ swap local@ ] dip %copy ;
137 : (%box) ( n rep -- )
138 #! If n is f, push the return register onto the stack; we
139 #! are boxing a return value of a C function. If n is an
140 #! integer, push [ESP+n] on the stack; we are boxing a
141 #! parameter being passed to a callback from C.
142 over [ [ next-stack@ ] dip load-return-reg ] [ 2drop ] if ;
144 M:: x86.32 %box ( n rep func -- )
146 rep rep-size save-vm-ptr
147 0 stack@ rep store-return-reg
148 func f %alien-invoke ;
150 : (%box-long-long) ( n -- )
152 EDX over next-stack@ MOV
153 EAX swap cell - next-stack@ MOV
156 M: x86.32 %box-long-long ( n func -- )
157 [ (%box-long-long) ] dip
163 M:: x86.32 %box-large-struct ( n c-type -- )
164 EDX n struct-return@ LEA
166 4 stack@ c-type heap-size MOV
168 "from_value_struct" f %alien-invoke ;
170 M: x86.32 %prepare-box-struct ( -- )
171 ! Compute target address for value struct return
172 EAX f struct-return@ LEA
173 ! Store it as the first parameter
176 M: x86.32 %box-small-struct ( c-type -- )
177 #! Box a <= 8-byte struct returned in EAX:EDX. OS X only.
179 8 stack@ swap heap-size MOV
182 "from_small_struct" f %alien-invoke ;
184 M: x86.32 %pop-stack ( n -- )
185 EAX swap ds-reg reg-stack MOV ;
187 M: x86.32 %pop-context-stack ( -- )
189 EAX temp-reg "datastack" context-field-offset [+] MOV
191 temp-reg "datastack" context-field-offset [+] bootstrap-cell SUB ;
193 : call-unbox-func ( func -- )
198 M: x86.32 %unbox ( n rep func -- )
199 #! The value being unboxed must already be in EAX.
200 #! If n is f, we're unboxing a return value about to be
201 #! returned by the callback. Otherwise, we're unboxing
202 #! a parameter to a C function about to be called.
204 ! Store the return value on the C stack
205 over [ [ local@ ] dip store-return-reg ] [ 2drop ] if ;
207 M: x86.32 %unbox-long-long ( n func -- )
209 ! Store the return value on the C stack
212 [ 4 + local@ EDX MOV ] bi
215 : %unbox-struct-1 ( -- )
216 #! Alien must be in EAX.
219 "alien_offset" f %alien-invoke
223 : %unbox-struct-2 ( -- )
224 #! Alien must be in EAX.
227 "alien_offset" f %alien-invoke
233 M: x86 %unbox-small-struct ( size -- )
234 #! Alien must be in EAX.
235 heap-size cell align cell /i {
236 { 1 [ %unbox-struct-1 ] }
237 { 2 [ %unbox-struct-2 ] }
240 M:: x86.32 %unbox-large-struct ( n c-type -- )
241 ! Alien must be in EAX.
242 ! Compute destination address
245 8 stack@ c-type heap-size MOV
248 "to_value_struct" f %alien-invoke ;
250 M: x86.32 %prepare-alien-indirect ( -- )
255 "pinned_alien_offset" f %alien-invoke
258 M: x86.32 %alien-indirect ( -- )
261 M: x86.32 %begin-callback ( -- )
264 "begin_callback" f %alien-invoke ;
266 M: x86.32 %alien-callback ( quot -- )
267 EAX EDX %restore-context
268 EAX swap %load-reference
269 EAX quot-entry-point-offset [+] CALL
270 EAX EDX %save-context ;
272 M: x86.32 %end-callback ( -- )
274 "end_callback" f %alien-invoke ;
276 M: x86.32 %end-callback-value ( ctype -- )
280 ! Place former top of data stack back in EAX
285 GENERIC: float-function-param ( stack-slot dst src -- )
287 M:: spill-slot float-function-param ( stack-slot dst src -- )
288 ! We can clobber dst here since its going to contain the
290 dst src double-rep %copy
291 stack-slot dst double-rep %copy ;
293 M: register float-function-param
294 nip double-rep %copy ;
296 : float-function-return ( reg -- )
301 M:: x86.32 %unary-float-function ( dst src func -- )
302 ESP -16 [+] dst src float-function-param
304 func "libm" load-library %alien-invoke
305 dst float-function-return ;
307 M:: x86.32 %binary-float-function ( dst src1 src2 func -- )
308 ESP -16 [+] dst src1 float-function-param
309 ESP -8 [+] dst src2 float-function-param
311 func "libm" load-library %alien-invoke
312 dst float-function-return ;
314 : funny-large-struct-return? ( params -- ? )
315 #! MINGW ABI incompatibility disaster
316 [ return>> large-struct? ]
317 [ abi>> mingw = os windows? not or ]
320 : callee-cleanup? ( abi -- ? )
321 { stdcall fastcall thiscall } member? ;
323 : stack-arg-size ( params -- n )
325 alien-parameters flatten-value-types
326 [ _ alloc-parameter 2drop ] each
330 M: x86.32 %cleanup ( params -- )
331 #! a) If we just called a stdcall function in Windows, it
332 #! cleaned up the stack frame for us. But we don't want that
333 #! so we 'undo' the cleanup since we do that in %epilogue.
334 #! b) If we just called a function returning a struct, we
337 { [ dup abi>> callee-cleanup? ] [ stack-arg-size ESP swap SUB ] }
338 { [ dup funny-large-struct-return? ] [ drop EAX PUSH ] }
342 M:: x86.32 %call-gc ( gc-root-count temp -- )
343 temp gc-root-base special@ LEA
345 4 stack@ gc-root-count MOV
347 "inline_gc" f %alien-invoke ;
349 M: x86.32 dummy-stack-params? f ;
351 M: x86.32 dummy-int-params? f ;
353 M: x86.32 dummy-fp-params? f ;
355 M: x86.32 callback-return-rewind ( params -- n )
356 #! a) If the callback is stdcall, we have to clean up the
357 #! caller's stack frame.
358 #! b) If the callback is returning a large struct, we have
361 { [ dup stdcall? ] [ <alien-stack-frame> [ params>> ] [ return>> ] bi + ] }
362 { [ dup funny-large-struct-return? ] [ drop 4 ] }
367 M: object flatten-value-type (flatten-stack-type) ;
368 M: struct-c-type flatten-value-type (flatten-stack-type) ;
369 M: long-long-type flatten-value-type (flatten-stack-type) ;
370 M: c-type flatten-value-type
371 dup rep>> int-rep? [ (flatten-int-type) ] [ (flatten-stack-type) ] if ;
373 M: x86.32 struct-return-pointer-type (stack-value) ;