]> gitweb.factorcode.org Git - factor.git/blob - basis/cpu/x86/32/32.factor
Merge branch 'master' of github.com:erikcharlebois/factor
[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 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 0 ;
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 usually never passed in registers, except with Microsoft's
90 ! "thiscall" and "fastcall" abis
91 M: int-regs return-reg drop EAX ;
92 M: float-regs param-regs 2drop { } ;
93
94 M: int-regs param-regs
95     nip {
96         { thiscall [ { ECX     } ] }
97         { fastcall [ { ECX EDX } ] }
98         [ drop { } ]
99     } case ;
100
101 GENERIC: load-return-reg ( src rep -- )
102 GENERIC: store-return-reg ( dst rep -- )
103
104 M: stack-params load-return-reg drop EAX swap MOV ;
105 M: stack-params store-return-reg drop EAX MOV ;
106
107 M: int-rep load-return-reg drop EAX swap MOV ;
108 M: int-rep store-return-reg drop EAX MOV ;
109
110 M: float-rep load-return-reg drop FLDS ;
111 M: float-rep store-return-reg drop FSTPS ;
112
113 M: double-rep load-return-reg drop FLDL ;
114 M: double-rep store-return-reg drop FSTPL ;
115
116 M: x86.32 %prologue ( n -- )
117     dup PUSH
118     0 PUSH rc-absolute-cell rel-this
119     3 cells - decr-stack-reg ;
120
121 M: x86.32 %prepare-jump
122     pic-tail-reg 0 MOV xt-tail-pic-offset rc-absolute-cell rel-here ;
123
124 M: stack-params copy-register*
125     drop
126     {
127         { [ dup  integer? ] [ EAX swap next-stack@ MOV  EAX MOV ] }
128         { [ over integer? ] [ EAX swap MOV              param@ EAX MOV ] }
129     } cond ;
130
131 M: x86.32 %save-param-reg [ local@ ] 2dip %copy ;
132
133 M: x86.32 %load-param-reg [ swap local@ ] dip %copy ;
134
135 : (%box) ( n rep -- )
136     #! If n is f, push the return register onto the stack; we
137     #! are boxing a return value of a C function. If n is an
138     #! integer, push [ESP+n] on the stack; we are boxing a
139     #! parameter being passed to a callback from C.
140     over [ [ local@ ] dip load-return-reg ] [ 2drop ] if ;
141
142 M:: x86.32 %box ( n rep func -- )
143     n rep (%box)
144     rep rep-size save-vm-ptr
145     0 stack@ rep store-return-reg
146     func f %alien-invoke ;
147
148 : (%box-long-long) ( n -- )
149     [
150         EDX over next-stack@ MOV
151         EAX swap cell - next-stack@ MOV 
152     ] when* ;
153
154 M: x86.32 %box-long-long ( n func -- )
155     [ (%box-long-long) ] dip
156     8 save-vm-ptr
157     4 stack@ EDX MOV
158     0 stack@ EAX MOV
159     f %alien-invoke ;
160
161 M:: x86.32 %box-large-struct ( n c-type -- )
162     EDX n struct-return@ LEA
163     8 save-vm-ptr
164     4 stack@ c-type heap-size MOV
165     0 stack@ EDX MOV
166     "from_value_struct" f %alien-invoke ;
167
168 M: x86.32 %prepare-box-struct ( -- )
169     ! Compute target address for value struct return
170     EAX f struct-return@ LEA
171     ! Store it as the first parameter
172     0 local@ EAX MOV ;
173
174 M: x86.32 %box-small-struct ( c-type -- )
175     #! Box a <= 8-byte struct returned in EAX:EDX. OS X only.
176     12 save-vm-ptr
177     8 stack@ swap heap-size MOV
178     4 stack@ EDX MOV
179     0 stack@ EAX MOV
180     "from_small_struct" f %alien-invoke ;
181
182 M: x86.32 %pop-stack ( n -- )
183     EAX swap ds-reg reg-stack MOV ;
184
185 M: x86.32 %pop-context-stack ( -- )
186     temp-reg %context
187     EAX temp-reg "datastack" context-field-offset [+] MOV
188     EAX EAX [] MOV
189     temp-reg "datastack" context-field-offset [+] bootstrap-cell SUB ;
190
191 : call-unbox-func ( func -- )
192     4 save-vm-ptr
193     0 stack@ EAX MOV
194     f %alien-invoke ;
195
196 M: x86.32 %unbox ( n rep func -- )
197     #! The value being unboxed must already be in EAX.
198     #! If n is f, we're unboxing a return value about to be
199     #! returned by the callback. Otherwise, we're unboxing
200     #! a parameter to a C function about to be called.
201     call-unbox-func
202     ! Store the return value on the C stack
203     over [ [ local@ ] dip store-return-reg ] [ 2drop ] if ;
204
205 M: x86.32 %unbox-long-long ( n func -- )
206     call-unbox-func
207     ! Store the return value on the C stack
208     [
209         [ local@ EAX MOV ]
210         [ 4 + local@ EDX MOV ] bi
211     ] when* ;
212
213 : %unbox-struct-1 ( -- )
214     #! Alien must be in EAX.
215     4 save-vm-ptr
216     0 stack@ EAX MOV
217     "alien_offset" f %alien-invoke
218     ! Load first cell
219     EAX EAX [] MOV ;
220
221 : %unbox-struct-2 ( -- )
222     #! Alien must be in EAX.
223     4 save-vm-ptr
224     0 stack@ EAX MOV
225     "alien_offset" f %alien-invoke
226     ! Load second cell
227     EDX EAX 4 [+] MOV
228     ! Load first cell
229     EAX EAX [] MOV ;
230
231 M: x86 %unbox-small-struct ( size -- )
232     #! Alien must be in EAX.
233     heap-size cell align cell /i {
234         { 1 [ %unbox-struct-1 ] }
235         { 2 [ %unbox-struct-2 ] }
236     } case ;
237
238 M:: x86.32 %unbox-large-struct ( n c-type -- )
239     ! Alien must be in EAX.
240     ! Compute destination address
241     EDX n local@ LEA
242     12 save-vm-ptr
243     8 stack@ c-type heap-size MOV
244     4 stack@ EDX MOV
245     0 stack@ EAX MOV
246     "to_value_struct" f %alien-invoke ;
247
248 M: x86.32 %prepare-alien-indirect ( -- )
249     EAX ds-reg [] MOV
250     ds-reg 4 SUB
251     4 save-vm-ptr
252     0 stack@ EAX MOV
253     "pinned_alien_offset" f %alien-invoke
254     EBP EAX MOV ;
255
256 M: x86.32 %alien-indirect ( -- )
257     EBP CALL ;
258
259 M: x86.32 %begin-callback ( -- )
260     0 save-vm-ptr
261     ESP 4 [+] 0 MOV
262     "begin_callback" f %alien-invoke ;
263
264 M: x86.32 %alien-callback ( quot -- )
265     EAX EDX %restore-context
266     EAX swap %load-reference
267     EAX quot-entry-point-offset [+] CALL
268     EAX EDX %save-context ;
269
270 M: x86.32 %end-callback ( -- )
271     0 save-vm-ptr
272     "end_callback" f %alien-invoke ;
273
274 M: x86.32 %end-callback-value ( ctype -- )
275     %pop-context-stack
276     4 stack@ EAX MOV
277     %end-callback
278     ! Place former top of data stack back in EAX
279     EAX 4 stack@ MOV
280     ! Unbox EAX
281     unbox-return ;
282
283 GENERIC: float-function-param ( stack-slot dst src -- )
284
285 M:: spill-slot float-function-param ( stack-slot dst src -- )
286     ! We can clobber dst here since its going to contain the
287     ! final result
288     dst src double-rep %copy
289     stack-slot dst double-rep %copy ;
290
291 M: register float-function-param
292     nip double-rep %copy ;
293
294 : float-function-return ( reg -- )
295     ESP [] FSTPL
296     ESP [] MOVSD
297     ESP 16 ADD ;
298
299 M:: x86.32 %unary-float-function ( dst src func -- )
300     ESP -16 [+] dst src float-function-param
301     ESP 16 SUB
302     func "libm" load-library %alien-invoke
303     dst float-function-return ;
304
305 M:: x86.32 %binary-float-function ( dst src1 src2 func -- )
306     ESP -16 [+] dst src1 float-function-param
307     ESP  -8 [+] dst src2 float-function-param
308     ESP 16 SUB
309     func "libm" load-library %alien-invoke
310     dst float-function-return ;
311
312 : funny-large-struct-return? ( params -- ? )
313     #! MINGW ABI incompatibility disaster
314     [ return>> large-struct? ]
315     [ abi>> mingw = os windows? not or ]
316     bi and ;
317
318 : stack-arg-size ( params -- n )
319     dup abi>> '[
320         alien-parameters flatten-value-types
321         [ _ alloc-parameter 2drop ] each
322         stack-params get
323     ] with-param-regs ;
324
325 M: x86.32 stack-cleanup ( params -- n )
326     #! a) Functions which are stdcall/fastcall/thiscall have to
327     #! clean up the caller's stack frame.
328     #! b) Functions returning large structs on MINGW have to
329     #! fix ESP.
330     {
331         { [ dup abi>> callee-cleanup? ] [ stack-arg-size ] }
332         { [ dup funny-large-struct-return? ] [ drop 4 ] }
333         [ drop 0 ]
334     } cond ;
335
336 M: x86.32 %cleanup ( params -- )
337     stack-cleanup [ ESP swap SUB ] unless-zero ;
338
339 M:: x86.32 %call-gc ( gc-root-count temp -- )
340     temp gc-root-base special@ LEA
341     8 save-vm-ptr
342     4 stack@ gc-root-count MOV
343     0 stack@ temp MOV
344     "inline_gc" f %alien-invoke ;
345
346 M: x86.32 dummy-stack-params? f ;
347
348 M: x86.32 dummy-int-params? f ;
349
350 M: x86.32 dummy-fp-params? f ;
351
352 ! Dreadful
353 M: object flatten-value-type (flatten-stack-type) ;
354 M: struct-c-type flatten-value-type (flatten-stack-type) ;
355 M: long-long-type flatten-value-type (flatten-stack-type) ;
356 M: c-type flatten-value-type
357     dup rep>> int-rep? [ (flatten-int-type) ] [ (flatten-stack-type) ] if ;
358
359 M: x86.32 struct-return-pointer-type
360     os linux? void* (stack-value) ? ;
361
362 check-sse