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
6 command-line make words compiler compiler.units
7 compiler.constants compiler.alien compiler.codegen
8 compiler.codegen.fixup compiler.cfg.instructions
9 compiler.cfg.builder compiler.cfg.intrinsics
10 compiler.cfg.stack-frame cpu.x86.assembler
11 cpu.x86.assembler.operands cpu.x86 cpu.architecture vm ;
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 immediate-comparand? ( n -- ? )
28 [ call-next-method ] [ word? ] bi or ;
30 M: x86.32 load-double? ( -- ? ) t ;
32 M: x86.32 %load-double ( dst val -- )
33 [ 0 [] MOVSD ] dip rc-absolute rel-float ;
35 M: x86.32 %mov-vm-ptr ( reg -- )
36 0 MOV 0 rc-absolute-cell rel-vm ;
38 M: x86.32 %vm-field ( dst field -- )
39 [ 0 [] MOV ] dip rc-absolute-cell rel-vm ;
41 M: x86.32 %set-vm-field ( dst field -- )
42 [ 0 [] swap MOV ] dip rc-absolute-cell rel-vm ;
44 M: x86.32 %vm-field-ptr ( dst field -- )
45 [ 0 MOV ] dip rc-absolute-cell rel-vm ;
48 stack-frame get extra-stack-space dup 16 assert= + stack@ ;
50 M: x86.32 extra-stack-space calls-vm?>> 16 0 ? ;
53 drop HEX: ffffffff [+] card-mark <byte> MOV
55 rc-absolute-cell rel-cards-offset
59 drop HEX: ffffffff [+] card-mark <byte> MOV
61 rc-absolute-cell rel-decks-offset
64 M:: x86.32 %dispatch ( src temp -- )
65 ! Load jump table base.
66 temp src HEX: ffffffff [+] LEA
67 building get length :> start
68 0 rc-absolute-cell rel-here
71 building get length :> end
72 ! Fix up the displacement above
74 [ end start - + building get dup pop* push ]
78 M: x86.32 pic-tail-reg EDX ;
80 M: x86.32 reserved-stack-space 0 ;
82 M: x86.32 %alien-invoke 0 CALL rc-relative rel-dlsym ;
84 : save-vm-ptr ( n -- )
85 stack@ 0 MOV 0 rc-absolute-cell rel-vm ;
87 M: x86.32 return-struct-in-registers? ( c-type -- ? )
89 [ return-in-registers?>> ]
90 [ heap-size { 1 2 4 8 } member? ] bi
91 os { linux netbsd solaris } member? not
94 : struct-return@ ( n -- operand )
95 [ next-stack@ ] [ stack-frame get params>> local@ ] if* ;
97 ! On x86, parameters are usually never passed in registers, except with Microsoft's
98 ! "thiscall" and "fastcall" abis
99 M: int-regs return-reg drop EAX ;
100 M: float-regs param-regs 2drop { } ;
102 M: int-regs param-regs
104 { thiscall [ { ECX } ] }
105 { fastcall [ { ECX EDX } ] }
109 GENERIC: load-return-reg ( src rep -- )
110 GENERIC: store-return-reg ( dst rep -- )
112 M: stack-params load-return-reg drop EAX swap MOV ;
113 M: stack-params store-return-reg drop EAX MOV ;
115 M: int-rep load-return-reg drop EAX swap MOV ;
116 M: int-rep store-return-reg drop EAX MOV ;
118 M: float-rep load-return-reg drop FLDS ;
119 M: float-rep store-return-reg drop FSTPS ;
121 M: double-rep load-return-reg drop FLDL ;
122 M: double-rep store-return-reg drop FSTPL ;
124 M: x86.32 %prologue ( n -- )
126 0 PUSH rc-absolute-cell rel-this
127 3 cells - decr-stack-reg ;
129 M: x86.32 %prepare-jump
130 pic-tail-reg 0 MOV xt-tail-pic-offset rc-absolute-cell rel-here ;
132 M: stack-params copy-register*
135 { [ dup integer? ] [ EAX swap next-stack@ MOV EAX MOV ] }
136 { [ over integer? ] [ EAX swap MOV param@ EAX MOV ] }
139 M: x86.32 %save-param-reg [ local@ ] 2dip %copy ;
141 M: x86.32 %load-param-reg [ swap local@ ] dip %copy ;
143 : (%box) ( n rep -- )
144 #! If n is f, push the return register onto the stack; we
145 #! are boxing a return value of a C function. If n is an
146 #! integer, push [ESP+n] on the stack; we are boxing a
147 #! parameter being passed to a callback from C.
148 over [ [ local@ ] dip load-return-reg ] [ 2drop ] if ;
150 M:: x86.32 %box ( n rep func -- )
152 rep rep-size save-vm-ptr
153 0 stack@ rep store-return-reg
154 func f %alien-invoke ;
156 : (%box-long-long) ( n -- )
158 EDX over next-stack@ MOV
159 EAX swap cell - next-stack@ MOV
162 M: x86.32 %box-long-long ( n func -- )
163 [ (%box-long-long) ] dip
169 M:: x86.32 %box-large-struct ( n c-type -- )
170 EDX n struct-return@ LEA
172 4 stack@ c-type heap-size MOV
174 "from_value_struct" f %alien-invoke ;
176 M: x86.32 %prepare-box-struct ( -- )
177 ! Compute target address for value struct return
178 EAX f struct-return@ LEA
179 ! Store it as the first parameter
182 M: x86.32 %box-small-struct ( c-type -- )
183 #! Box a <= 8-byte struct returned in EAX:EDX. OS X only.
185 8 stack@ swap heap-size MOV
188 "from_small_struct" f %alien-invoke ;
190 M: x86.32 %pop-stack ( n -- )
191 EAX swap ds-reg reg-stack MOV ;
193 M: x86.32 %pop-context-stack ( -- )
195 EAX temp-reg "datastack" context-field-offset [+] MOV
197 temp-reg "datastack" context-field-offset [+] bootstrap-cell SUB ;
199 : call-unbox-func ( func -- )
204 M: x86.32 %unbox ( n rep func -- )
205 #! The value being unboxed must already be in EAX.
206 #! If n is f, we're unboxing a return value about to be
207 #! returned by the callback. Otherwise, we're unboxing
208 #! a parameter to a C function about to be called.
210 ! Store the return value on the C stack
211 over [ [ local@ ] dip store-return-reg ] [ 2drop ] if ;
213 M: x86.32 %unbox-long-long ( n func -- )
215 ! Store the return value on the C stack
218 [ 4 + local@ EDX MOV ] bi
221 : %unbox-struct-1 ( -- )
222 #! Alien must be in EAX.
225 "alien_offset" f %alien-invoke
229 : %unbox-struct-2 ( -- )
230 #! Alien must be in EAX.
233 "alien_offset" f %alien-invoke
239 M: x86 %unbox-small-struct ( size -- )
240 #! Alien must be in EAX.
241 heap-size cell align cell /i {
242 { 1 [ %unbox-struct-1 ] }
243 { 2 [ %unbox-struct-2 ] }
246 M:: x86.32 %unbox-large-struct ( n c-type -- )
247 ! Alien must be in EAX.
248 ! Compute destination address
251 8 stack@ c-type heap-size MOV
254 "to_value_struct" f %alien-invoke ;
256 M: x86.32 %prepare-alien-indirect ( -- )
261 "pinned_alien_offset" f %alien-invoke
264 M: x86.32 %alien-indirect ( -- )
267 M: x86.32 %begin-callback ( -- )
270 "begin_callback" f %alien-invoke ;
272 M: x86.32 %alien-callback ( quot -- )
273 EAX EDX %restore-context
274 EAX swap %load-reference
275 EAX quot-entry-point-offset [+] CALL
276 EAX EDX %save-context ;
278 M: x86.32 %end-callback ( -- )
280 "end_callback" f %alien-invoke ;
282 M: x86.32 %end-callback-value ( ctype -- )
286 ! Place former top of data stack back in EAX
291 GENERIC: float-function-param ( stack-slot dst src -- )
293 M:: spill-slot float-function-param ( stack-slot dst src -- )
294 ! We can clobber dst here since its going to contain the
296 dst src double-rep %copy
297 stack-slot dst double-rep %copy ;
299 M: register float-function-param
300 nip double-rep %copy ;
302 : float-function-return ( reg -- )
307 M:: x86.32 %unary-float-function ( dst src func -- )
308 ESP -16 [+] dst src float-function-param
310 func "libm" load-library %alien-invoke
311 dst float-function-return ;
313 M:: x86.32 %binary-float-function ( dst src1 src2 func -- )
314 ESP -16 [+] dst src1 float-function-param
315 ESP -8 [+] dst src2 float-function-param
317 func "libm" load-library %alien-invoke
318 dst float-function-return ;
320 : funny-large-struct-return? ( params -- ? )
321 #! MINGW ABI incompatibility disaster
322 [ return>> large-struct? ]
323 [ abi>> mingw = os windows? not or ]
326 : stack-arg-size ( params -- n )
328 alien-parameters flatten-value-types
329 [ _ alloc-parameter 2drop ] each
333 M: x86.32 stack-cleanup ( params -- n )
334 #! a) Functions which are stdcall/fastcall/thiscall have to
335 #! clean up the caller's stack frame.
336 #! b) Functions returning large structs on MINGW have to
339 { [ dup abi>> callee-cleanup? ] [ stack-arg-size ] }
340 { [ dup funny-large-struct-return? ] [ drop 4 ] }
344 M: x86.32 %cleanup ( params -- )
345 stack-cleanup [ ESP swap SUB ] unless-zero ;
347 M:: x86.32 %call-gc ( gc-root-count temp -- )
348 temp gc-root-base special@ LEA
350 4 stack@ gc-root-count MOV
352 "inline_gc" f %alien-invoke ;
354 M: x86.32 dummy-stack-params? f ;
356 M: x86.32 dummy-int-params? f ;
358 M: x86.32 dummy-fp-params? f ;
361 M: object flatten-value-type (flatten-stack-type) ;
362 M: struct-c-type flatten-value-type (flatten-stack-type) ;
363 M: long-long-type flatten-value-type (flatten-stack-type) ;
364 M: c-type flatten-value-type
365 dup rep>> int-rep? [ (flatten-int-type) ] [ (flatten-stack-type) ] if ;
367 M: x86.32 struct-return-pointer-type
368 os linux? void* (stack-value) ? ;