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 ;
15 M: x86.32 machine-registers
17 { int-regs { EAX ECX EDX EBP EBX } }
18 { float-regs { XMM0 XMM1 XMM2 XMM3 XMM4 XMM5 XMM6 XMM7 } }
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 frame-reg EBP ;
25 M: x86.32 temp-reg ECX ;
27 M: x86.32 %mov-vm-ptr ( reg -- )
28 0 MOV 0 rc-absolute-cell rel-vm ;
30 M: x86.32 %vm-field ( dst field -- )
31 [ 0 [] MOV ] dip rc-absolute-cell rel-vm ;
33 M: x86.32 %set-vm-field ( dst field -- )
34 [ 0 [] swap MOV ] dip rc-absolute-cell rel-vm ;
36 M: x86.32 %vm-field-ptr ( dst field -- )
37 [ 0 MOV ] dip rc-absolute-cell rel-vm ;
40 stack-frame get extra-stack-space dup 16 assert= + stack@ ;
42 M: x86.32 extra-stack-space calls-vm?>> 16 0 ? ;
45 drop HEX: ffffffff [+] card-mark <byte> MOV
47 rc-absolute-cell rel-cards-offset
51 drop HEX: ffffffff [+] card-mark <byte> MOV
53 rc-absolute-cell rel-decks-offset
56 M:: x86.32 %dispatch ( src temp -- )
57 ! Load jump table base.
58 temp src HEX: ffffffff [+] LEA
59 building get length :> start
60 0 rc-absolute-cell rel-here
63 building get length :> end
64 ! Fix up the displacement above
66 [ end start - + building get dup pop* push ]
70 M: x86.32 pic-tail-reg EDX ;
72 M: x86.32 reserved-stack-space 0 ;
74 M: x86.32 %alien-invoke 0 CALL rc-relative rel-dlsym ;
76 : save-vm-ptr ( n -- )
77 stack@ 0 MOV 0 rc-absolute-cell rel-vm ;
79 M: x86.32 return-struct-in-registers? ( c-type -- ? )
81 [ return-in-registers?>> ]
82 [ heap-size { 1 2 4 8 } member? ] bi
83 os { linux netbsd solaris } member? not
86 : struct-return@ ( n -- operand )
87 [ next-stack@ ] [ stack-frame get params>> local@ ] if* ;
89 ! On x86, parameters are usually never passed in registers, except with Microsoft's
90 ! "thiscall" and "fastcall" abis
91 M: int-regs return-reg drop EAX ;
92 M: float-regs param-regs 2drop { } ;
94 M: int-regs param-regs
96 { thiscall [ { ECX } ] }
97 { fastcall [ { ECX EDX } ] }
101 GENERIC: load-return-reg ( src rep -- )
102 GENERIC: store-return-reg ( dst rep -- )
104 M: stack-params load-return-reg drop EAX swap MOV ;
105 M: stack-params store-return-reg drop EAX MOV ;
107 M: int-rep load-return-reg drop EAX swap MOV ;
108 M: int-rep store-return-reg drop EAX MOV ;
110 M: float-rep load-return-reg drop FLDS ;
111 M: float-rep store-return-reg drop FSTPS ;
113 M: double-rep load-return-reg drop FLDL ;
114 M: double-rep store-return-reg drop FSTPL ;
116 M: x86.32 %prologue ( n -- )
118 0 PUSH rc-absolute-cell rel-this
119 3 cells - decr-stack-reg ;
121 M: x86.32 %prepare-jump
122 pic-tail-reg 0 MOV xt-tail-pic-offset rc-absolute-cell rel-here ;
124 M: stack-params copy-register*
127 { [ dup integer? ] [ EAX swap next-stack@ MOV EAX MOV ] }
128 { [ over integer? ] [ EAX swap MOV param@ EAX MOV ] }
131 M: x86.32 %save-param-reg [ local@ ] 2dip %copy ;
133 M: x86.32 %load-param-reg [ swap local@ ] dip %copy ;
135 : (%box) ( n rep -- )
136 #! If n is f, push the return register onto the stack; we
137 #! are boxing a return value of a C function. If n is an
138 #! integer, push [ESP+n] on the stack; we are boxing a
139 #! parameter being passed to a callback from C.
140 over [ [ local@ ] dip load-return-reg ] [ 2drop ] if ;
142 M:: x86.32 %box ( n rep func -- )
144 rep rep-size save-vm-ptr
145 0 stack@ rep store-return-reg
146 func f %alien-invoke ;
148 : (%box-long-long) ( n -- )
150 EDX over next-stack@ MOV
151 EAX swap cell - next-stack@ MOV
154 M: x86.32 %box-long-long ( n func -- )
155 [ (%box-long-long) ] dip
161 M:: x86.32 %box-large-struct ( n c-type -- )
162 EDX n struct-return@ LEA
164 4 stack@ c-type heap-size MOV
166 "from_value_struct" f %alien-invoke ;
168 M: x86.32 %prepare-box-struct ( -- )
169 ! Compute target address for value struct return
170 EAX f struct-return@ LEA
171 ! Store it as the first parameter
174 M: x86.32 %box-small-struct ( c-type -- )
175 #! Box a <= 8-byte struct returned in EAX:EDX. OS X only.
177 8 stack@ swap heap-size MOV
180 "from_small_struct" f %alien-invoke ;
182 M: x86.32 %pop-stack ( n -- )
183 EAX swap ds-reg reg-stack MOV ;
185 M: x86.32 %pop-context-stack ( -- )
187 EAX temp-reg "datastack" context-field-offset [+] MOV
189 temp-reg "datastack" context-field-offset [+] bootstrap-cell SUB ;
191 : call-unbox-func ( func -- )
196 M: x86.32 %unbox ( n rep func -- )
197 #! The value being unboxed must already be in EAX.
198 #! If n is f, we're unboxing a return value about to be
199 #! returned by the callback. Otherwise, we're unboxing
200 #! a parameter to a C function about to be called.
202 ! Store the return value on the C stack
203 over [ [ local@ ] dip store-return-reg ] [ 2drop ] if ;
205 M: x86.32 %unbox-long-long ( n func -- )
207 ! Store the return value on the C stack
210 [ 4 + local@ EDX MOV ] bi
213 : %unbox-struct-1 ( -- )
214 #! Alien must be in EAX.
217 "alien_offset" f %alien-invoke
221 : %unbox-struct-2 ( -- )
222 #! Alien must be in EAX.
225 "alien_offset" f %alien-invoke
231 M: x86 %unbox-small-struct ( size -- )
232 #! Alien must be in EAX.
233 heap-size cell align cell /i {
234 { 1 [ %unbox-struct-1 ] }
235 { 2 [ %unbox-struct-2 ] }
238 M:: x86.32 %unbox-large-struct ( n c-type -- )
239 ! Alien must be in EAX.
240 ! Compute destination address
243 8 stack@ c-type heap-size MOV
246 "to_value_struct" f %alien-invoke ;
248 M: x86.32 %prepare-alien-indirect ( -- )
253 "pinned_alien_offset" f %alien-invoke
256 M: x86.32 %alien-indirect ( -- )
259 M: x86.32 %begin-callback ( -- )
262 "begin_callback" f %alien-invoke ;
264 M: x86.32 %alien-callback ( quot -- )
265 EAX EDX %restore-context
266 EAX swap %load-reference
267 EAX quot-entry-point-offset [+] CALL
268 EAX EDX %save-context ;
270 M: x86.32 %end-callback ( -- )
272 "end_callback" f %alien-invoke ;
274 M: x86.32 %end-callback-value ( ctype -- )
278 ! Place former top of data stack back in EAX
283 GENERIC: float-function-param ( stack-slot dst src -- )
285 M:: spill-slot float-function-param ( stack-slot dst src -- )
286 ! We can clobber dst here since its going to contain the
288 dst src double-rep %copy
289 stack-slot dst double-rep %copy ;
291 M: register float-function-param
292 nip double-rep %copy ;
294 : float-function-return ( reg -- )
299 M:: x86.32 %unary-float-function ( dst src func -- )
300 ESP -16 [+] dst src float-function-param
302 func "libm" load-library %alien-invoke
303 dst float-function-return ;
305 M:: x86.32 %binary-float-function ( dst src1 src2 func -- )
306 ESP -16 [+] dst src1 float-function-param
307 ESP -8 [+] dst src2 float-function-param
309 func "libm" load-library %alien-invoke
310 dst float-function-return ;
312 : funny-large-struct-return? ( params -- ? )
313 #! MINGW ABI incompatibility disaster
314 [ return>> large-struct? ]
315 [ abi>> mingw = os windows? not or ]
318 : stack-arg-size ( params -- n )
320 alien-parameters flatten-value-types
321 [ _ alloc-parameter 2drop ] each
325 M: x86.32 stack-cleanup ( params -- n )
326 #! a) Functions which are stdcall/fastcall/thiscall have to
327 #! clean up the caller's stack frame.
328 #! b) Functions returning large structs on MINGW have to
331 { [ dup abi>> callee-cleanup? ] [ stack-arg-size ] }
332 { [ dup funny-large-struct-return? ] [ drop 4 ] }
336 M: x86.32 %cleanup ( params -- )
337 stack-cleanup [ ESP swap SUB ] unless-zero ;
339 M:: x86.32 %call-gc ( gc-root-count temp -- )
340 temp gc-root-base special@ LEA
342 4 stack@ gc-root-count MOV
344 "inline_gc" f %alien-invoke ;
346 M: x86.32 dummy-stack-params? f ;
348 M: x86.32 dummy-int-params? f ;
350 M: x86.32 dummy-fp-params? f ;
353 M: object flatten-value-type (flatten-stack-type) ;
354 M: struct-c-type flatten-value-type (flatten-stack-type) ;
355 M: long-long-type flatten-value-type (flatten-stack-type) ;
356 M: c-type flatten-value-type
357 dup rep>> int-rep? [ (flatten-int-type) ] [ (flatten-stack-type) ] if ;
359 M: x86.32 struct-return-pointer-type
360 os linux? void* (stack-value) ? ;