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