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.alien compiler.codegen.fixup
9 compiler.cfg.instructions compiler.cfg.builder
10 compiler.cfg.intrinsics compiler.cfg.stack-frame
11 cpu.x86.assembler cpu.x86.assembler.operands cpu.x86
13 FROM: layouts => cell ;
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 immediate-comparand? ( obj -- ? ) drop t ;
30 M:: x86.32 %load-vector ( dst val rep -- )
31 dst 0 [] rep copy-memory* val rc-absolute rel-binary-literal ;
33 M: x86.32 %load-float ( dst val -- )
34 <float> float-rep %load-vector ;
36 M: x86.32 %load-double ( dst val -- )
37 <double> double-rep %load-vector ;
39 M: x86.32 %mov-vm-ptr ( reg -- )
40 0 MOV 0 rc-absolute-cell rel-vm ;
42 M: x86.32 %vm-field ( dst field -- )
43 [ 0 [] MOV ] dip rc-absolute-cell rel-vm ;
45 M: x86.32 %set-vm-field ( dst field -- )
46 [ 0 [] swap MOV ] dip rc-absolute-cell rel-vm ;
48 M: x86.32 %vm-field-ptr ( dst field -- )
49 [ 0 MOV ] dip rc-absolute-cell rel-vm ;
52 stack-frame get extra-stack-space dup 16 assert= + stack@ ;
54 M: x86.32 extra-stack-space calls-vm?>> 16 0 ? ;
57 drop HEX: ffffffff [+] card-mark <byte> MOV
59 rc-absolute-cell rel-cards-offset
63 drop HEX: ffffffff [+] card-mark <byte> MOV
65 rc-absolute-cell rel-decks-offset
68 M:: x86.32 %dispatch ( src temp -- )
69 ! Load jump table base.
70 temp src HEX: ffffffff [+] LEA
71 building get length :> start
72 0 rc-absolute-cell rel-here
75 building get length :> end
76 ! Fix up the displacement above
78 [ end start - + building get dup pop* push ]
82 M: x86.32 pic-tail-reg EDX ;
84 M: x86.32 reserved-stack-space 0 ;
86 M: x86.32 %alien-invoke 0 CALL rc-relative rel-dlsym ;
88 : save-vm-ptr ( n -- )
89 stack@ 0 MOV 0 rc-absolute-cell rel-vm ;
91 M: x86.32 return-struct-in-registers? ( c-type -- ? )
93 [ return-in-registers?>> ]
94 [ heap-size { 1 2 4 8 } member? ] bi
95 os { linux netbsd solaris } member? not
98 : struct-return@ ( n -- operand )
99 [ next-stack@ ] [ stack-frame get params>> local@ ] if* ;
101 ! On x86, parameters are usually never passed in registers, except with Microsoft's
102 ! "thiscall" and "fastcall" abis
103 M: int-regs return-reg drop EAX ;
104 M: float-regs param-regs 2drop { } ;
106 M: int-regs param-regs
108 { thiscall [ { ECX } ] }
109 { fastcall [ { ECX EDX } ] }
113 GENERIC: load-return-reg ( src rep -- )
114 GENERIC: store-return-reg ( dst rep -- )
116 M: stack-params load-return-reg drop EAX swap MOV ;
117 M: stack-params store-return-reg drop EAX MOV ;
119 M: int-rep load-return-reg drop EAX swap MOV ;
120 M: int-rep store-return-reg drop EAX MOV ;
122 M: float-rep load-return-reg drop FLDS ;
123 M: float-rep store-return-reg drop FSTPS ;
125 M: double-rep load-return-reg drop FLDL ;
126 M: double-rep store-return-reg drop FSTPL ;
128 M: x86.32 %prologue ( n -- )
130 0 PUSH rc-absolute-cell rel-this
131 3 cells - decr-stack-reg ;
133 M: x86.32 %prepare-jump
134 pic-tail-reg 0 MOV xt-tail-pic-offset rc-absolute-cell rel-here ;
136 M: stack-params copy-register*
139 { [ dup integer? ] [ EAX swap next-stack@ MOV EAX MOV ] }
140 { [ over integer? ] [ EAX swap MOV param@ EAX MOV ] }
143 M: x86.32 %save-param-reg [ local@ ] 2dip %copy ;
145 M: x86.32 %load-param-reg [ swap local@ ] dip %copy ;
147 : (%box) ( n rep -- )
148 #! If n is f, push the return register onto the stack; we
149 #! are boxing a return value of a C function. If n is an
150 #! integer, push [ESP+n] on the stack; we are boxing a
151 #! parameter being passed to a callback from C.
152 over [ [ local@ ] dip load-return-reg ] [ 2drop ] if ;
154 M:: x86.32 %box ( n rep func -- )
156 rep rep-size save-vm-ptr
157 0 stack@ rep store-return-reg
158 func f %alien-invoke ;
160 : (%box-long-long) ( n -- )
162 EDX over next-stack@ MOV
163 EAX swap cell - next-stack@ MOV
166 M: x86.32 %box-long-long ( n func -- )
167 [ (%box-long-long) ] dip
173 M:: x86.32 %box-large-struct ( n c-type -- )
174 EDX n struct-return@ LEA
176 4 stack@ c-type heap-size MOV
178 "from_value_struct" f %alien-invoke ;
180 M: x86.32 %prepare-box-struct ( -- )
181 ! Compute target address for value struct return
182 EAX f struct-return@ LEA
183 ! Store it as the first parameter
186 M: x86.32 %box-small-struct ( c-type -- )
187 #! Box a <= 8-byte struct returned in EAX:EDX. OS X only.
189 8 stack@ swap heap-size MOV
192 "from_small_struct" f %alien-invoke ;
194 M: x86.32 %pop-stack ( n -- )
195 EAX swap ds-reg reg-stack MOV ;
197 M: x86.32 %pop-context-stack ( -- )
199 EAX temp-reg "datastack" context-field-offset [+] MOV
201 temp-reg "datastack" context-field-offset [+] bootstrap-cell SUB ;
203 : call-unbox-func ( func -- )
208 M: x86.32 %unbox ( n rep func -- )
209 #! The value being unboxed must already be in EAX.
210 #! If n is f, we're unboxing a return value about to be
211 #! returned by the callback. Otherwise, we're unboxing
212 #! a parameter to a C function about to be called.
214 ! Store the return value on the C stack
215 over [ [ local@ ] dip store-return-reg ] [ 2drop ] if ;
217 M: x86.32 %unbox-long-long ( n func -- )
219 ! Store the return value on the C stack
222 [ 4 + local@ EDX MOV ] bi
225 : %unbox-struct-1 ( -- )
226 #! Alien must be in EAX.
229 "alien_offset" f %alien-invoke
233 : %unbox-struct-2 ( -- )
234 #! Alien must be in EAX.
237 "alien_offset" f %alien-invoke
243 M: x86 %unbox-small-struct ( size -- )
244 #! Alien must be in EAX.
245 heap-size cell align cell /i {
246 { 1 [ %unbox-struct-1 ] }
247 { 2 [ %unbox-struct-2 ] }
250 M:: x86.32 %unbox-large-struct ( n c-type -- )
251 ! Alien must be in EAX.
252 ! Compute destination address
255 8 stack@ c-type heap-size MOV
258 "to_value_struct" f %alien-invoke ;
260 M: x86.32 %prepare-alien-indirect ( -- )
265 "pinned_alien_offset" f %alien-invoke
268 M: x86.32 %alien-indirect ( -- )
271 M: x86.32 %begin-callback ( -- )
274 "begin_callback" f %alien-invoke ;
276 M: x86.32 %alien-callback ( quot -- )
277 EAX EDX %restore-context
278 EAX swap %load-reference
279 EAX quot-entry-point-offset [+] CALL
280 EAX EDX %save-context ;
282 M: x86.32 %end-callback ( -- )
284 "end_callback" f %alien-invoke ;
286 M: x86.32 %to-nv ( -- ) 4 stack@ EAX MOV ;
288 M: x86.32 %from-nv ( -- ) EAX 4 stack@ MOV ;
290 GENERIC: float-function-param ( stack-slot dst src -- )
292 M:: spill-slot float-function-param ( stack-slot dst src -- )
293 ! We can clobber dst here since its going to contain the
295 dst src double-rep %copy
296 stack-slot dst double-rep %copy ;
298 M: register float-function-param
299 nip double-rep %copy ;
301 : float-function-return ( reg -- )
306 M:: x86.32 %unary-float-function ( dst src func -- )
307 ESP -16 [+] dst src float-function-param
309 func "libm" load-library %alien-invoke
310 dst float-function-return ;
312 M:: x86.32 %binary-float-function ( dst src1 src2 func -- )
313 ESP -16 [+] dst src1 float-function-param
314 ESP -8 [+] dst src2 float-function-param
316 func "libm" load-library %alien-invoke
317 dst float-function-return ;
319 : funny-large-struct-return? ( params -- ? )
320 #! MINGW ABI incompatibility disaster
321 [ return>> large-struct? ]
322 [ abi>> mingw = os windows? not or ]
325 : stack-arg-size ( params -- n )
327 alien-parameters flatten-c-types
328 [ _ alloc-parameter 2drop ] each
332 M: x86.32 stack-cleanup ( params -- n )
333 #! a) Functions which are stdcall/fastcall/thiscall have to
334 #! clean up the caller's stack frame.
335 #! b) Functions returning large structs on MINGW have to
338 { [ dup abi>> callee-cleanup? ] [ stack-arg-size ] }
339 { [ dup funny-large-struct-return? ] [ drop 4 ] }
343 M: x86.32 %cleanup ( params -- )
344 stack-cleanup [ ESP swap SUB ] unless-zero ;
346 M:: x86.32 %call-gc ( gc-roots -- )
348 0 stack@ gc-roots gc-root-offsets %load-reference
349 "inline_gc" f %alien-invoke ;
351 M: x86.32 dummy-stack-params? f ;
353 M: x86.32 dummy-int-params? f ;
355 M: x86.32 dummy-fp-params? f ;
358 M: struct-c-type flatten-c-type stack-params (flatten-c-type) ;
359 M: long-long-type flatten-c-type stack-params (flatten-c-type) ;
360 M: c-type flatten-c-type dup rep>> int-rep? int-rep stack-params ? (flatten-c-type) ;
362 M: x86.32 struct-return-pointer-type
363 os linux? void* (stack-value) ? ;