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