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