]> gitweb.factorcode.org Git - factor.git/blob - basis/cpu/x86/32/32.factor
Fix ffi-test library to work with Windows SDK. Turns out MINGW has a different ABI...
[factor.git] / basis / cpu / x86 / 32 / 32.factor
1 ! Copyright (C) 2005, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: locals alien.c-types alien.libraries alien.syntax arrays
4 kernel fry math namespaces sequences system layouts io
5 vocabs.loader accessors init combinators command-line make
6 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
11 cpu.architecture vm ;
12 FROM: layouts => cell ;
13 IN: cpu.x86.32
14
15 M: x86.32 machine-registers
16     {
17         { int-regs { EAX ECX EDX EBP EBX } }
18         { float-regs { XMM0 XMM1 XMM2 XMM3 XMM4 XMM5 XMM6 XMM7 } }
19     } ;
20
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 ;
26
27 M: x86.32 %mov-vm-ptr ( reg -- )
28     0 MOV 0 rc-absolute-cell rel-vm ;
29
30 M: x86.32 %vm-field-ptr ( dst field -- )
31     [ 0 MOV ] dip vm-field-offset rc-absolute-cell rel-vm ;
32
33 : local@ ( n -- op )
34     stack-frame get extra-stack-space dup 16 assert= + stack@ ;
35
36 M: x86.32 extra-stack-space calls-vm?>> 16 0 ? ;
37
38 M: x86.32 %mark-card
39     drop HEX: ffffffff [+] card-mark <byte> MOV
40     building get pop
41     rc-absolute-cell rel-cards-offset
42     building get push ;
43
44 M: x86.32 %mark-deck
45     drop HEX: ffffffff [+] card-mark <byte> MOV
46     building get pop
47     rc-absolute-cell rel-decks-offset
48     building get push ;
49
50 M:: x86.32 %dispatch ( src temp -- )
51     ! Load jump table base.
52     temp src HEX: ffffffff [+] LEA
53     building get length :> start
54     0 rc-absolute-cell rel-here
55     ! Go
56     temp HEX: 7f [+] JMP
57     building get length :> end
58     ! Fix up the displacement above
59     cell code-alignment
60     [ end start - + building get dup pop* push ]
61     [ align-code ]
62     bi ;
63
64 M: x86.32 pic-tail-reg EBX ;
65
66 M: x86.32 reserved-stack-space 4 cells ;
67
68 M: x86.32 %alien-invoke 0 CALL rc-relative rel-dlsym ;
69
70 : save-vm-ptr ( n -- )
71     stack@ 0 MOV 0 rc-absolute-cell rel-vm ;
72
73 M: x86.32 return-struct-in-registers? ( c-type -- ? )
74     c-type
75     [ return-in-registers?>> ]
76     [ heap-size { 1 2 4 8 } member? ] bi
77     os { linux netbsd solaris } member? not
78     and or ;
79
80 : struct-return@ ( n -- operand )
81     [ next-stack@ ] [ stack-frame get params>> local@ ] if* ;
82
83 ! On x86, parameters are never passed in registers.
84 M: int-regs return-reg drop EAX ;
85 M: int-regs param-regs drop { } ;
86 M: float-regs param-regs drop { } ;
87
88 GENERIC: load-return-reg ( src rep -- )
89 GENERIC: store-return-reg ( dst rep -- )
90
91 M: int-rep load-return-reg drop EAX swap MOV ;
92 M: int-rep store-return-reg drop EAX MOV ;
93
94 M: float-rep load-return-reg drop FLDS ;
95 M: float-rep store-return-reg drop FSTPS ;
96
97 M: double-rep load-return-reg drop FLDL ;
98 M: double-rep store-return-reg drop FSTPL ;
99
100 M: x86.32 %prologue ( n -- )
101     dup PUSH
102     0 PUSH rc-absolute-cell rel-this
103     3 cells - decr-stack-reg ;
104
105 M: x86.32 %load-param-reg
106     stack-params assert=
107     [ [ EAX ] dip local@ MOV ] dip
108     stack@ EAX MOV ;
109
110 M: x86.32 %save-param-reg 3drop ;
111
112 : (%box) ( n rep -- )
113     #! If n is f, push the return register onto the stack; we
114     #! are boxing a return value of a C function. If n is an
115     #! integer, push [ESP+n] on the stack; we are boxing a
116     #! parameter being passed to a callback from C.
117     over [ [ next-stack@ ] dip load-return-reg ] [ 2drop ] if ;
118
119 M:: x86.32 %box ( n rep func -- )
120     n rep (%box)
121     rep rep-size save-vm-ptr
122     0 stack@ rep store-return-reg
123     func f %alien-invoke ;
124
125 : (%box-long-long) ( n -- )
126     [
127         EDX over next-stack@ MOV
128         EAX swap cell - next-stack@ MOV 
129     ] when* ;
130
131 M: x86.32 %box-long-long ( n func -- )
132     [ (%box-long-long) ] dip
133     8 save-vm-ptr
134     4 stack@ EDX MOV
135     0 stack@ EAX MOV
136     f %alien-invoke ;
137
138 M:: x86.32 %box-large-struct ( n c-type -- )
139     EDX n struct-return@ LEA
140     8 save-vm-ptr
141     4 stack@ c-type heap-size MOV
142     0 stack@ EDX MOV
143     "from_value_struct" f %alien-invoke ;
144
145 M: x86.32 %prepare-box-struct ( -- )
146     ! Compute target address for value struct return
147     EAX f struct-return@ LEA
148     ! Store it as the first parameter
149     0 local@ EAX MOV ;
150
151 M: x86.32 %box-small-struct ( c-type -- )
152     #! Box a <= 8-byte struct returned in EAX:EDX. OS X only.
153     12 save-vm-ptr
154     8 stack@ swap heap-size MOV
155     4 stack@ EDX MOV
156     0 stack@ EAX MOV
157     "from_small_struct" f %alien-invoke ;
158
159 M: x86.32 %pop-stack ( n -- )
160     EAX swap ds-reg reg-stack MOV ;
161
162 M: x86.32 %pop-context-stack ( -- )
163     temp-reg %load-context-datastack
164     EAX temp-reg [] MOV
165     EAX EAX [] MOV
166     temp-reg [] bootstrap-cell SUB ;
167
168 : call-unbox-func ( func -- )
169     4 save-vm-ptr
170     0 stack@ EAX MOV
171     f %alien-invoke ;
172
173 M: x86.32 %unbox ( n rep func -- )
174     #! The value being unboxed must already be in EAX.
175     #! If n is f, we're unboxing a return value about to be
176     #! returned by the callback. Otherwise, we're unboxing
177     #! a parameter to a C function about to be called.
178     call-unbox-func
179     ! Store the return value on the C stack
180     over [ [ local@ ] dip store-return-reg ] [ 2drop ] if ;
181
182 M: x86.32 %unbox-long-long ( n func -- )
183     call-unbox-func
184     ! Store the return value on the C stack
185     [
186         [ local@ EAX MOV ]
187         [ 4 + local@ EDX MOV ] bi
188     ] when* ;
189
190 : %unbox-struct-1 ( -- )
191     #! Alien must be in EAX.
192     4 save-vm-ptr
193     0 stack@ EAX MOV
194     "alien_offset" f %alien-invoke
195     ! Load first cell
196     EAX EAX [] MOV ;
197
198 : %unbox-struct-2 ( -- )
199     #! Alien must be in EAX.
200     4 save-vm-ptr
201     0 stack@ EAX MOV
202     "alien_offset" f %alien-invoke
203     ! Load second cell
204     EDX EAX 4 [+] MOV
205     ! Load first cell
206     EAX EAX [] MOV ;
207
208 M: x86 %unbox-small-struct ( size -- )
209     #! Alien must be in EAX.
210     heap-size cell align cell /i {
211         { 1 [ %unbox-struct-1 ] }
212         { 2 [ %unbox-struct-2 ] }
213     } case ;
214
215 M:: x86.32 %unbox-large-struct ( n c-type -- )
216     ! Alien must be in EAX.
217     ! Compute destination address
218     EDX n local@ LEA
219     12 save-vm-ptr
220     8 stack@ c-type heap-size MOV
221     4 stack@ EDX MOV
222     0 stack@ EAX MOV
223     "to_value_struct" f %alien-invoke ;
224
225 M: x86.32 %nest-stacks ( -- )
226     0 save-vm-ptr
227     "nest_stacks" f %alien-invoke ;
228
229 M: x86.32 %unnest-stacks ( -- )
230     0 save-vm-ptr
231     "unnest_stacks" f %alien-invoke ;
232
233 M: x86.32 %prepare-alien-indirect ( -- )
234     EAX ds-reg [] MOV
235     ds-reg 4 SUB
236     4 save-vm-ptr
237     0 stack@ EAX MOV
238     "pinned_alien_offset" f %alien-invoke
239     EBP EAX MOV ;
240
241 M: x86.32 %alien-indirect ( -- )
242     EBP CALL ;
243
244 M: x86.32 %alien-callback ( quot -- )
245     EAX EDX %restore-context
246     EAX swap %load-reference
247     EAX quot-entry-point-offset [+] CALL
248     EAX EDX %save-context ;
249
250 M: x86.32 %callback-value ( ctype -- )
251     %pop-context-stack
252     4 stack@ EAX MOV
253     0 save-vm-ptr
254     ! Restore data/call/retain stacks
255     "unnest_stacks" f %alien-invoke
256     ! Place former top of data stack back in EAX
257     EAX 4 stack@ MOV
258     ! Unbox EAX
259     unbox-return ;
260
261 GENERIC: float-function-param ( stack-slot dst src -- )
262
263 M:: spill-slot float-function-param ( stack-slot dst src -- )
264     ! We can clobber dst here since its going to contain the
265     ! final result
266     dst src double-rep %copy
267     stack-slot dst double-rep %copy ;
268
269 M: register float-function-param
270     nip double-rep %copy ;
271
272 : float-function-return ( reg -- )
273     ESP [] FSTPL
274     ESP [] MOVSD
275     ESP 16 ADD ;
276
277 M:: x86.32 %unary-float-function ( dst src func -- )
278     ESP -16 [+] dst src float-function-param
279     ESP 16 SUB
280     func "libm" load-library %alien-invoke
281     dst float-function-return ;
282
283 M:: x86.32 %binary-float-function ( dst src1 src2 func -- )
284     ESP -16 [+] dst src1 float-function-param
285     ESP  -8 [+] dst src2 float-function-param
286     ESP 16 SUB
287     func "libm" load-library %alien-invoke
288     dst float-function-return ;
289
290 : stdcall? ( params -- ? )
291     abi>> "stdcall" = ;
292
293 : funny-large-struct-return? ( params -- ? )
294     #! MINGW ABI incompatibility disaster
295     [ return>> large-struct? ]
296     [ abi>> "mingw" = os windows? not or ]
297     bi and ;
298
299 M: x86.32 %cleanup ( params -- )
300     #! a) If we just called an stdcall function in Windows, it
301     #! cleaned up the stack frame for us. But we don't want that
302     #! so we 'undo' the cleanup since we do that in %epilogue.
303     #! b) If we just called a function returning a struct, we
304     #! have to fix ESP.
305     {
306         { [ dup stdcall? ] [ drop ESP stack-frame get params>> SUB ] }
307         { [ dup funny-large-struct-return? ] [ drop EAX PUSH ] }
308         [ drop ]
309     } cond ;
310
311 M:: x86.32 %call-gc ( gc-root-count temp -- )
312     temp gc-root-base special@ LEA
313     8 save-vm-ptr
314     4 stack@ gc-root-count MOV
315     0 stack@ temp MOV
316     "inline_gc" f %alien-invoke ;
317
318 M: x86.32 dummy-stack-params? f ;
319
320 M: x86.32 dummy-int-params? f ;
321
322 M: x86.32 dummy-fp-params? f ;
323
324 M: x86.32 callback-return-rewind ( params -- n )
325     #! a) If the callback is stdcall, we have to clean up the
326     #! caller's stack frame.
327     #! b) If the callback is returning a large struct, we have
328     #! to fix ESP.
329     {
330         { [ dup stdcall? ] [ <alien-stack-frame> [ params>> ] [ return>> ] bi + ] }
331         { [ dup funny-large-struct-return? ] [ drop 4 ] }
332         [ drop 0 ]
333     } cond ;
334
335 ! Dreadful
336 M: object flatten-value-type (flatten-int-type) ;
337
338 check-sse