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 object-immediates? ( -- ? ) t ;
32 M: x86.32 %load-double ( dst val -- )
33 [ 0 [] MOVSD ] dip rc-absolute rel-float ;
35 M:: x86.32 %load-vector ( dst val rep -- )
36 dst 0 [] rep copy-memory* val rc-absolute rel-byte-array ;
38 M: x86.32 %mov-vm-ptr ( reg -- )
39 0 MOV 0 rc-absolute-cell rel-vm ;
41 M: x86.32 %vm-field ( dst field -- )
42 [ 0 [] MOV ] dip rc-absolute-cell rel-vm ;
44 M: x86.32 %set-vm-field ( dst field -- )
45 [ 0 [] swap MOV ] dip rc-absolute-cell rel-vm ;
47 M: x86.32 %vm-field-ptr ( dst field -- )
48 [ 0 MOV ] dip rc-absolute-cell rel-vm ;
51 stack-frame get extra-stack-space dup 16 assert= + stack@ ;
53 M: x86.32 extra-stack-space calls-vm?>> 16 0 ? ;
56 drop HEX: ffffffff [+] card-mark <byte> MOV
58 rc-absolute-cell rel-cards-offset
62 drop HEX: ffffffff [+] card-mark <byte> MOV
64 rc-absolute-cell rel-decks-offset
67 M:: x86.32 %dispatch ( src temp -- )
68 ! Load jump table base.
69 temp src HEX: ffffffff [+] LEA
70 building get length :> start
71 0 rc-absolute-cell rel-here
74 building get length :> end
75 ! Fix up the displacement above
77 [ end start - + building get dup pop* push ]
81 M: x86.32 pic-tail-reg EDX ;
83 M: x86.32 reserved-stack-space 0 ;
85 M: x86.32 %alien-invoke 0 CALL rc-relative rel-dlsym ;
87 : save-vm-ptr ( n -- )
88 stack@ 0 MOV 0 rc-absolute-cell rel-vm ;
90 M: x86.32 return-struct-in-registers? ( c-type -- ? )
92 [ return-in-registers?>> ]
93 [ heap-size { 1 2 4 8 } member? ] bi
94 os { linux netbsd solaris } member? not
97 : struct-return@ ( n -- operand )
98 [ next-stack@ ] [ stack-frame get params>> local@ ] if* ;
100 ! On x86, parameters are usually never passed in registers, except with Microsoft's
101 ! "thiscall" and "fastcall" abis
102 M: int-regs return-reg drop EAX ;
103 M: float-regs param-regs 2drop { } ;
105 M: int-regs param-regs
107 { thiscall [ { ECX } ] }
108 { fastcall [ { ECX EDX } ] }
112 GENERIC: load-return-reg ( src rep -- )
113 GENERIC: store-return-reg ( dst rep -- )
115 M: stack-params load-return-reg drop EAX swap MOV ;
116 M: stack-params store-return-reg drop EAX MOV ;
118 M: int-rep load-return-reg drop EAX swap MOV ;
119 M: int-rep store-return-reg drop EAX MOV ;
121 M: float-rep load-return-reg drop FLDS ;
122 M: float-rep store-return-reg drop FSTPS ;
124 M: double-rep load-return-reg drop FLDL ;
125 M: double-rep store-return-reg drop FSTPL ;
127 M: x86.32 %prologue ( n -- )
129 0 PUSH rc-absolute-cell rel-this
130 3 cells - decr-stack-reg ;
132 M: x86.32 %prepare-jump
133 pic-tail-reg 0 MOV xt-tail-pic-offset rc-absolute-cell rel-here ;
135 M: stack-params copy-register*
138 { [ dup integer? ] [ EAX swap next-stack@ MOV EAX MOV ] }
139 { [ over integer? ] [ EAX swap MOV param@ EAX MOV ] }
142 M: x86.32 %save-param-reg [ local@ ] 2dip %copy ;
144 M: x86.32 %load-param-reg [ swap local@ ] dip %copy ;
146 : (%box) ( n rep -- )
147 #! If n is f, push the return register onto the stack; we
148 #! are boxing a return value of a C function. If n is an
149 #! integer, push [ESP+n] on the stack; we are boxing a
150 #! parameter being passed to a callback from C.
151 over [ [ local@ ] dip load-return-reg ] [ 2drop ] if ;
153 M:: x86.32 %box ( n rep func -- )
155 rep rep-size save-vm-ptr
156 0 stack@ rep store-return-reg
157 func f %alien-invoke ;
159 : (%box-long-long) ( n -- )
161 EDX over next-stack@ MOV
162 EAX swap cell - next-stack@ MOV
165 M: x86.32 %box-long-long ( n func -- )
166 [ (%box-long-long) ] dip
172 M:: x86.32 %box-large-struct ( n c-type -- )
173 EDX n struct-return@ LEA
175 4 stack@ c-type heap-size MOV
177 "from_value_struct" f %alien-invoke ;
179 M: x86.32 %prepare-box-struct ( -- )
180 ! Compute target address for value struct return
181 EAX f struct-return@ LEA
182 ! Store it as the first parameter
185 M: x86.32 %box-small-struct ( c-type -- )
186 #! Box a <= 8-byte struct returned in EAX:EDX. OS X only.
188 8 stack@ swap heap-size MOV
191 "from_small_struct" f %alien-invoke ;
193 M: x86.32 %pop-stack ( n -- )
194 EAX swap ds-reg reg-stack MOV ;
196 M: x86.32 %pop-context-stack ( -- )
198 EAX temp-reg "datastack" context-field-offset [+] MOV
200 temp-reg "datastack" context-field-offset [+] bootstrap-cell SUB ;
202 : call-unbox-func ( func -- )
207 M: x86.32 %unbox ( n rep func -- )
208 #! The value being unboxed must already be in EAX.
209 #! If n is f, we're unboxing a return value about to be
210 #! returned by the callback. Otherwise, we're unboxing
211 #! a parameter to a C function about to be called.
213 ! Store the return value on the C stack
214 over [ [ local@ ] dip store-return-reg ] [ 2drop ] if ;
216 M: x86.32 %unbox-long-long ( n func -- )
218 ! Store the return value on the C stack
221 [ 4 + local@ EDX MOV ] bi
224 : %unbox-struct-1 ( -- )
225 #! Alien must be in EAX.
228 "alien_offset" f %alien-invoke
232 : %unbox-struct-2 ( -- )
233 #! Alien must be in EAX.
236 "alien_offset" f %alien-invoke
242 M: x86 %unbox-small-struct ( size -- )
243 #! Alien must be in EAX.
244 heap-size cell align cell /i {
245 { 1 [ %unbox-struct-1 ] }
246 { 2 [ %unbox-struct-2 ] }
249 M:: x86.32 %unbox-large-struct ( n c-type -- )
250 ! Alien must be in EAX.
251 ! Compute destination address
254 8 stack@ c-type heap-size MOV
257 "to_value_struct" f %alien-invoke ;
259 M: x86.32 %prepare-alien-indirect ( -- )
264 "pinned_alien_offset" f %alien-invoke
267 M: x86.32 %alien-indirect ( -- )
270 M: x86.32 %begin-callback ( -- )
273 "begin_callback" f %alien-invoke ;
275 M: x86.32 %alien-callback ( quot -- )
276 EAX EDX %restore-context
277 EAX swap %load-reference
278 EAX quot-entry-point-offset [+] CALL
279 EAX EDX %save-context ;
281 M: x86.32 %end-callback ( -- )
283 "end_callback" f %alien-invoke ;
285 M: x86.32 %end-callback-value ( ctype -- )
289 ! Place former top of data stack back in EAX
294 GENERIC: float-function-param ( stack-slot dst src -- )
296 M:: spill-slot float-function-param ( stack-slot dst src -- )
297 ! We can clobber dst here since its going to contain the
299 dst src double-rep %copy
300 stack-slot dst double-rep %copy ;
302 M: register float-function-param
303 nip double-rep %copy ;
305 : float-function-return ( reg -- )
310 M:: x86.32 %unary-float-function ( dst src func -- )
311 ESP -16 [+] dst src float-function-param
313 func "libm" load-library %alien-invoke
314 dst float-function-return ;
316 M:: x86.32 %binary-float-function ( dst src1 src2 func -- )
317 ESP -16 [+] dst src1 float-function-param
318 ESP -8 [+] dst src2 float-function-param
320 func "libm" load-library %alien-invoke
321 dst float-function-return ;
323 : funny-large-struct-return? ( params -- ? )
324 #! MINGW ABI incompatibility disaster
325 [ return>> large-struct? ]
326 [ abi>> mingw = os windows? not or ]
329 : stack-arg-size ( params -- n )
331 alien-parameters flatten-value-types
332 [ _ alloc-parameter 2drop ] each
336 M: x86.32 stack-cleanup ( params -- n )
337 #! a) Functions which are stdcall/fastcall/thiscall have to
338 #! clean up the caller's stack frame.
339 #! b) Functions returning large structs on MINGW have to
342 { [ dup abi>> callee-cleanup? ] [ stack-arg-size ] }
343 { [ dup funny-large-struct-return? ] [ drop 4 ] }
347 M: x86.32 %cleanup ( params -- )
348 stack-cleanup [ ESP swap SUB ] unless-zero ;
350 M:: x86.32 %call-gc ( gc-roots -- )
352 0 stack@ gc-roots gc-root-offsets %load-reference
353 "inline_gc" f %alien-invoke ;
355 M: x86.32 dummy-stack-params? f ;
357 M: x86.32 dummy-int-params? f ;
359 M: x86.32 dummy-fp-params? f ;
362 M: object flatten-value-type (flatten-stack-type) ;
363 M: struct-c-type flatten-value-type (flatten-stack-type) ;
364 M: long-long-type flatten-value-type (flatten-stack-type) ;
365 M: c-type flatten-value-type
366 dup rep>> int-rep? [ (flatten-int-type) ] [ (flatten-stack-type) ] if ;
368 M: x86.32 struct-return-pointer-type
369 os linux? void* (stack-value) ? ;