]> gitweb.factorcode.org Git - factor.git/blob - basis/cpu/x86/x86.factor
Merge branch 'master' of git://factorcode.org/git/factor
[factor.git] / basis / cpu / x86 / x86.factor
1 ! Copyright (C) 2005, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs alien alien.c-types arrays strings
4 cpu.x86.assembler cpu.x86.assembler.private cpu.architecture
5 kernel kernel.private math memory namespaces make sequences
6 words system layouts combinators math.order fry locals
7 compiler.constants compiler.cfg.registers
8 compiler.cfg.instructions compiler.cfg.intrinsics
9 compiler.cfg.stack-frame compiler.codegen compiler.codegen.fixup ;
10 IN: cpu.x86
11
12 << enable-fixnum-log2 >>
13
14 ! Add some methods to the assembler to be more useful to the backend
15 M: label JMP 0 JMP rc-relative label-fixup ;
16 M: label JUMPcc [ 0 ] dip JUMPcc rc-relative label-fixup ;
17
18 M: x86 two-operand? t ;
19
20 HOOK: stack-reg cpu ( -- reg )
21
22 HOOK: reserved-area-size cpu ( -- n )
23
24 : stack@ ( n -- op ) stack-reg swap [+] ;
25
26 : param@ ( n -- op ) reserved-area-size + stack@ ;
27
28 : spill-integer@ ( n -- op ) spill-integer-offset param@ ;
29
30 : spill-float@ ( n -- op ) spill-float-offset param@ ;
31
32 : gc-root@ ( n -- op ) gc-root-offset param@ ;
33
34 : decr-stack-reg ( n -- )
35     dup 0 = [ drop ] [ stack-reg swap SUB ] if ;
36
37 : incr-stack-reg ( n -- )
38     dup 0 = [ drop ] [ stack-reg swap ADD ] if ;
39
40 : align-stack ( n -- n' )
41     os macosx? cpu x86.64? or [ 16 align ] when ;
42
43 M: x86 stack-frame-size ( stack-frame -- i )
44     (stack-frame-size) 3 cells reserved-area-size + + align-stack ;
45
46 HOOK: temp-reg-1 cpu ( -- reg )
47 HOOK: temp-reg-2 cpu ( -- reg )
48
49 HOOK: param-reg-1 cpu ( -- reg )
50 HOOK: param-reg-2 cpu ( -- reg )
51
52 HOOK: pic-tail-reg cpu ( -- reg )
53
54 M: x86 %load-immediate MOV ;
55
56 M: x86 %load-reference swap 0 MOV rc-absolute-cell rel-immediate ;
57
58 HOOK: ds-reg cpu ( -- reg )
59 HOOK: rs-reg cpu ( -- reg )
60
61 : reg-stack ( n reg -- op ) swap cells neg [+] ;
62
63 GENERIC: loc>operand ( loc -- operand )
64
65 M: ds-loc loc>operand n>> ds-reg reg-stack ;
66 M: rs-loc loc>operand n>> rs-reg reg-stack ;
67
68 M: x86 %peek loc>operand MOV ;
69 M: x86 %replace loc>operand swap MOV ;
70 : (%inc) ( n reg -- ) swap cells dup 0 > [ ADD ] [ neg SUB ] if ; inline
71 M: x86 %inc-d ( n -- ) ds-reg (%inc) ;
72 M: x86 %inc-r ( n -- ) rs-reg (%inc) ;
73
74 M: x86 %call ( word -- ) 0 CALL rc-relative rel-word-pic ;
75
76 : xt-tail-pic-offset ( -- n )
77     #! See the comment in vm/cpu-x86.hpp
78     cell 4 + 1 + ; inline
79
80 M: x86 %jump ( word -- )
81     pic-tail-reg 0 MOV xt-tail-pic-offset rc-absolute-cell rel-here
82     0 JMP rc-relative rel-word-pic-tail ;
83
84 M: x86 %jump-label ( label -- ) 0 JMP rc-relative label-fixup ;
85
86 M: x86 %return ( -- ) 0 RET ;
87
88 : code-alignment ( align -- n )
89     [ building get length dup ] dip align swap - ;
90
91 : align-code ( n -- )
92     0 <repetition> % ;
93
94 M: x86 %dispatch-label ( label -- )
95     0 cell, rc-absolute-cell label-fixup ;
96
97 :: (%slot) ( obj slot tag temp -- op )
98     temp slot obj [+] LEA
99     temp tag neg [+] ; inline
100
101 :: (%slot-imm) ( obj slot tag -- op )
102     obj slot cells tag - [+] ; inline
103
104 M: x86 %slot ( dst obj slot tag temp -- ) (%slot) MOV ;
105 M: x86 %slot-imm ( dst obj slot tag -- ) (%slot-imm) MOV ;
106 M: x86 %set-slot ( src obj slot tag temp -- ) (%slot) swap MOV ;
107 M: x86 %set-slot-imm ( src obj slot tag -- ) (%slot-imm) swap MOV ;
108
109 M: x86 %add     [+] LEA ;
110 M: x86 %add-imm [+] LEA ;
111 M: x86 %sub     nip SUB ;
112 M: x86 %sub-imm neg [+] LEA ;
113 M: x86 %mul     nip swap IMUL2 ;
114 M: x86 %mul-imm nip IMUL2 ;
115 M: x86 %and     nip AND ;
116 M: x86 %and-imm nip AND ;
117 M: x86 %or      nip OR ;
118 M: x86 %or-imm  nip OR ;
119 M: x86 %xor     nip XOR ;
120 M: x86 %xor-imm nip XOR ;
121 M: x86 %shl-imm nip SHL ;
122 M: x86 %shr-imm nip SHR ;
123 M: x86 %sar-imm nip SAR ;
124 M: x86 %not     drop NOT ;
125 M: x86 %log2    BSR ;
126
127 : ?MOV ( dst src -- )
128     2dup = [ 2drop ] [ MOV ] if ; inline
129
130 :: move>args ( src1 src2 -- )
131     {
132         { [ src1 param-reg-2 = ] [ param-reg-1 src2 ?MOV param-reg-1 param-reg-2 XCHG ] }
133         { [ src1 param-reg-1 = ] [ param-reg-2 src2 ?MOV ] }
134         { [ src2 param-reg-1 = ] [ param-reg-2 src1 ?MOV param-reg-1 param-reg-2 XCHG ] }
135         { [ src2 param-reg-2 = ] [ param-reg-1 src1 ?MOV ] }
136         [
137             param-reg-1 src1 MOV
138             param-reg-2 src2 MOV
139         ]
140     } cond ;
141
142 HOOK: %alien-invoke-tail cpu ( func dll -- )
143
144 :: overflow-template ( src1 src2 insn inverse func -- )
145     <label> "no-overflow" set
146     src1 src2 insn call
147     ds-reg [] src1 MOV
148     "no-overflow" get JNO
149     src1 src2 inverse call
150     src1 src2 move>args
151     %prepare-alien-invoke
152     func f %alien-invoke
153     "no-overflow" resolve-label ; inline
154
155 :: overflow-template-tail ( src1 src2 insn inverse func -- )
156     <label> "no-overflow" set
157     src1 src2 insn call
158     "no-overflow" get JNO
159     src1 src2 inverse call
160     src1 src2 move>args
161     %prepare-alien-invoke
162     func f %alien-invoke-tail
163     "no-overflow" resolve-label
164     ds-reg [] src1 MOV
165     0 RET ; inline
166
167 M: x86 %fixnum-add ( src1 src2 -- )
168     [ ADD ] [ SUB ] "overflow_fixnum_add" overflow-template ;
169
170 M: x86 %fixnum-add-tail ( src1 src2 -- )
171     [ ADD ] [ SUB ] "overflow_fixnum_add" overflow-template-tail ;
172
173 M: x86 %fixnum-sub ( src1 src2 -- )
174     [ SUB ] [ ADD ] "overflow_fixnum_subtract" overflow-template ;
175
176 M: x86 %fixnum-sub-tail ( src1 src2 -- )
177     [ SUB ] [ ADD ] "overflow_fixnum_subtract" overflow-template-tail ;
178
179 M:: x86 %fixnum-mul ( src1 src2 temp1 temp2 -- )
180     "no-overflow" define-label
181     temp1 src1 MOV
182     temp1 tag-bits get SAR
183     src2 temp1 IMUL2
184     ds-reg [] temp1 MOV
185     "no-overflow" get JNO
186     src1 src2 move>args
187     param-reg-1 tag-bits get SAR
188     param-reg-2 tag-bits get SAR
189     %prepare-alien-invoke
190     "overflow_fixnum_multiply" f %alien-invoke
191     "no-overflow" resolve-label ;
192
193 M:: x86 %fixnum-mul-tail ( src1 src2 temp1 temp2 -- )
194     "overflow" define-label
195     temp1 src1 MOV
196     temp1 tag-bits get SAR
197     src2 temp1 IMUL2
198     "overflow" get JO
199     ds-reg [] temp1 MOV
200     0 RET
201     "overflow" resolve-label
202     src1 src2 move>args
203     param-reg-1 tag-bits get SAR
204     param-reg-2 tag-bits get SAR
205     %prepare-alien-invoke
206     "overflow_fixnum_multiply" f %alien-invoke-tail ;
207
208 : bignum@ ( reg n -- op )
209     cells bignum tag-number - [+] ; inline
210
211 M:: x86 %integer>bignum ( dst src temp -- )
212     #! on entry, inreg is a signed 32-bit quantity
213     #! exits with tagged ptr to bignum in outreg
214     #! 1 cell header, 1 cell length, 1 cell sign, + digits
215     #! length is the # of digits + sign
216     [
217         "end" define-label
218         ! Load cached zero value
219         dst 0 >bignum %load-reference
220         src 0 CMP
221         ! Is it zero? Then just go to the end and return this zero
222         "end" get JE
223         ! Allocate a bignum
224         dst 4 cells bignum temp %allot
225         ! Write length
226         dst 1 bignum@ 2 tag-fixnum MOV
227         ! Store value
228         dst 3 bignum@ src MOV
229         ! Compute sign
230         temp src MOV
231         temp cell-bits 1- SAR
232         temp 1 AND
233         ! Store sign
234         dst 2 bignum@ temp MOV
235         ! Make negative value positive
236         temp temp ADD
237         temp NEG
238         temp 1 ADD
239         src temp IMUL2
240         ! Store the bignum
241         dst 3 bignum@ temp MOV
242         "end" resolve-label
243     ] with-scope ;
244
245 M:: x86 %bignum>integer ( dst src temp -- )
246     [
247         "end" define-label
248         ! load length
249         temp src 1 bignum@ MOV
250         ! if the length is 1, its just the sign and nothing else,
251         ! so output 0
252         dst 0 MOV
253         temp 1 tag-fixnum CMP
254         "end" get JE
255         ! load the value
256         dst src 3 bignum@ MOV
257         ! load the sign
258         temp src 2 bignum@ MOV
259         ! convert it into -1 or 1
260         temp temp ADD
261         temp NEG
262         temp 1 ADD
263         ! make dst signed
264         temp dst IMUL2
265         "end" resolve-label
266     ] with-scope ;
267
268 M: x86 %add-float nip ADDSD ;
269 M: x86 %sub-float nip SUBSD ;
270 M: x86 %mul-float nip MULSD ;
271 M: x86 %div-float nip DIVSD ;
272
273 M: x86 %integer>float CVTSI2SD ;
274 M: x86 %float>integer CVTTSD2SI ;
275
276 M: x86 %copy ( dst src -- ) ?MOV ;
277
278 M: x86 %copy-float ( dst src -- )
279     2dup = [ 2drop ] [ MOVSD ] if ;
280
281 M: x86 %unbox-float ( dst src -- )
282     float-offset [+] MOVSD ;
283
284 M:: x86 %unbox-any-c-ptr ( dst src temp -- )
285     [
286         { "is-byte-array" "end" "start" } [ define-label ] each
287         dst 0 MOV
288         temp src MOV
289         ! We come back here with displaced aliens
290         "start" resolve-label
291         ! Is the object f?
292         temp \ f tag-number CMP
293         "end" get JE
294         ! Is the object an alien?
295         temp header-offset [+] alien type-number tag-fixnum CMP
296         "is-byte-array" get JNE
297         ! If so, load the offset and add it to the address
298         dst temp alien-offset [+] ADD
299         ! Now recurse on the underlying alien
300         temp temp underlying-alien-offset [+] MOV
301         "start" get JMP
302         "is-byte-array" resolve-label
303         ! Add byte array address to address being computed
304         dst temp ADD
305         ! Add an offset to start of byte array's data
306         dst byte-array-offset ADD
307         "end" resolve-label
308     ] with-scope ;
309
310 M:: x86 %box-float ( dst src temp -- )
311     dst 16 float temp %allot
312     dst float-offset [+] src MOVSD ;
313
314 : alien@ ( reg n -- op ) cells alien tag-number - [+] ;
315
316 M:: x86 %box-alien ( dst src temp -- )
317     [
318         "end" define-label
319         dst \ f tag-number MOV
320         src 0 CMP
321         "end" get JE
322         dst 4 cells alien temp %allot
323         dst 1 alien@ \ f tag-number MOV
324         dst 2 alien@ \ f tag-number MOV
325         ! Store src in alien-offset slot
326         dst 3 alien@ src MOV
327         "end" resolve-label
328     ] with-scope ;
329
330 : small-reg-4 ( reg -- reg' )
331     H{
332         { EAX EAX }
333         { ECX ECX }
334         { EDX EDX }
335         { EBX EBX }
336         { ESP ESP }
337         { EBP EBP }
338         { ESI ESP }
339         { EDI EDI }
340
341         { RAX EAX }
342         { RCX ECX }
343         { RDX EDX }
344         { RBX EBX }
345         { RSP ESP }
346         { RBP EBP }
347         { RSI ESP }
348         { RDI EDI }
349     } at ; inline
350
351 : small-reg-2 ( reg -- reg' )
352     small-reg-4 H{
353         { EAX AX }
354         { ECX CX }
355         { EDX DX }
356         { EBX BX }
357         { ESP SP }
358         { EBP BP }
359         { ESI SI }
360         { EDI DI }
361     } at ; inline
362
363 : small-reg-1 ( reg -- reg' )
364     small-reg-4 {
365         { EAX AL }
366         { ECX CL }
367         { EDX DL }
368         { EBX BL }
369     } at ; inline
370
371 : small-reg ( reg size -- reg' )
372     {
373         { 1 [ small-reg-1 ] }
374         { 2 [ small-reg-2 ] }
375         { 4 [ small-reg-4 ] }
376     } case ;
377
378 : small-regs ( -- regs ) { EAX ECX EDX EBX } ; inline
379
380 : small-reg-that-isn't ( exclude -- reg' )
381     small-regs swap [ small-reg-4 ] map '[ _ memq? not ] find nip ;
382
383 : with-save/restore ( reg quot -- )
384     [ drop PUSH ] [ call ] [ drop POP ] 2tri ; inline
385
386 :: with-small-register ( dst exclude quot: ( new-dst -- ) -- )
387     #! If the destination register overlaps a small register, we
388     #! call the quot with that. Otherwise, we find a small
389     #! register that is not in exclude, and call quot, saving
390     #! and restoring the small register.
391     dst small-reg-4 small-regs memq? [ dst quot call ] [
392         exclude small-reg-that-isn't
393         [ quot call ] with-save/restore
394     ] if ; inline
395
396 M:: x86 %string-nth ( dst src index temp -- )
397     "end" define-label
398     dst { src index temp } [| new-dst |
399         ! Load the least significant 7 bits into new-dst.
400         ! 8th bit indicates whether we have to load from
401         ! the aux vector or not.
402         temp src index [+] LEA
403         new-dst 1 small-reg temp string-offset [+] MOV
404         new-dst new-dst 1 small-reg MOVZX
405         ! Do we have to look at the aux vector?
406         new-dst HEX: 80 CMP
407         "end" get JL
408         ! Yes, this is a non-ASCII character. Load aux vector
409         temp src string-aux-offset [+] MOV
410         new-dst temp XCHG
411         ! Compute index
412         new-dst index ADD
413         new-dst index ADD
414         ! Load high 16 bits
415         new-dst 2 small-reg new-dst byte-array-offset [+] MOV
416         new-dst new-dst 2 small-reg MOVZX
417         new-dst 7 SHL
418         ! Compute code point
419         new-dst temp XOR
420         "end" resolve-label
421         dst new-dst ?MOV
422     ] with-small-register ;
423
424 M:: x86 %set-string-nth-fast ( ch str index temp -- )
425     ch { index str temp } [| new-ch |
426         new-ch ch ?MOV
427         temp str index [+] LEA
428         temp string-offset [+] new-ch 1 small-reg MOV
429     ] with-small-register ;
430
431 :: %alien-integer-getter ( dst src size quot -- )
432     dst { src } [| new-dst |
433         new-dst dup size small-reg dup src [] MOV
434         quot call
435         dst new-dst ?MOV
436     ] with-small-register ; inline
437
438 : %alien-unsigned-getter ( dst src size -- )
439     [ MOVZX ] %alien-integer-getter ; inline
440
441 M: x86 %alien-unsigned-1 1 %alien-unsigned-getter ;
442 M: x86 %alien-unsigned-2 2 %alien-unsigned-getter ;
443
444 : %alien-signed-getter ( dst src size -- )
445     [ MOVSX ] %alien-integer-getter ; inline
446
447 M: x86 %alien-signed-1 1 %alien-signed-getter ;
448 M: x86 %alien-signed-2 2 %alien-signed-getter ;
449 M: x86 %alien-signed-4 4 %alien-signed-getter ;
450
451 M: x86 %alien-unsigned-4 4 [ 2drop ] %alien-integer-getter ;
452
453 M: x86 %alien-cell [] MOV ;
454 M: x86 %alien-float dupd [] MOVSS dup CVTSS2SD ;
455 M: x86 %alien-double [] MOVSD ;
456
457 :: %alien-integer-setter ( ptr value size -- )
458     value { ptr } [| new-value |
459         new-value value ?MOV
460         ptr [] new-value size small-reg MOV
461     ] with-small-register ; inline
462
463 M: x86 %set-alien-integer-1 1 %alien-integer-setter ;
464 M: x86 %set-alien-integer-2 2 %alien-integer-setter ;
465 M: x86 %set-alien-integer-4 4 %alien-integer-setter ;
466 M: x86 %set-alien-cell [ [] ] dip MOV ;
467 M: x86 %set-alien-float dup dup CVTSD2SS [ [] ] dip MOVSS ;
468 M: x86 %set-alien-double [ [] ] dip MOVSD ;
469
470 : load-zone-ptr ( reg -- )
471     #! Load pointer to start of zone array
472     0 MOV "nursery" f rc-absolute-cell rel-dlsym ;
473
474 : load-allot-ptr ( nursery-ptr allot-ptr -- )
475     [ drop load-zone-ptr ] [ swap cell [+] MOV ] 2bi ;
476
477 : inc-allot-ptr ( nursery-ptr n -- )
478     [ cell [+] ] dip 8 align ADD ;
479
480 : store-header ( temp class -- )
481     [ [] ] [ type-number tag-fixnum ] bi* MOV ;
482
483 : store-tagged ( dst tag -- )
484     tag-number OR ;
485
486 M:: x86 %allot ( dst size class nursery-ptr -- )
487     nursery-ptr dst load-allot-ptr
488     dst class store-header
489     dst class store-tagged
490     nursery-ptr size inc-allot-ptr ;
491
492 M:: x86 %write-barrier ( src card# table -- )
493     #! Mark the card pointed to by vreg.
494     ! Mark the card
495     card# src MOV
496     card# card-bits SHR
497     table "cards_offset" f %alien-global
498     table table [] MOV
499     table card# [+] card-mark <byte> MOV
500
501     ! Mark the card deck
502     card# deck-bits card-bits - SHR
503     table "decks_offset" f %alien-global
504     table table [] MOV
505     table card# [+] card-mark <byte> MOV ;
506
507 :: check-nursery ( temp1 temp2 -- )
508     temp1 load-zone-ptr
509     temp2 temp1 cell [+] MOV
510     temp2 1024 ADD
511     temp1 temp1 3 cells [+] MOV
512     temp2 temp1 CMP ;
513
514 GENERIC# save-gc-root 1 ( gc-root operand temp -- )
515
516 M:: spill-slot save-gc-root ( gc-root spill-slot temp -- )
517     temp spill-slot n>> spill-integer@ MOV
518     gc-root gc-root@ temp MOV ;
519
520 M:: word save-gc-root ( gc-root register temp -- )
521     gc-root gc-root@ register MOV ;
522
523 : save-gc-roots ( gc-roots temp -- )
524     '[ _ save-gc-root ] assoc-each ;
525
526 GENERIC# load-gc-root 1 ( gc-root operand temp -- )
527
528 M:: spill-slot load-gc-root ( gc-root spill-slot temp -- )
529     temp gc-root gc-root@ MOV
530     spill-slot n>> spill-integer@ temp MOV ;
531
532 M:: word load-gc-root ( gc-root register temp -- )
533     register gc-root gc-root@ MOV ;
534
535 : load-gc-roots ( gc-roots temp -- )
536     '[ _ load-gc-root ] assoc-each ;
537
538 :: call-gc ( gc-root-count -- )
539     ! Pass pointer to start of GC roots as first parameter
540     param-reg-1 gc-root-base param@ LEA
541     ! Pass number of roots as second parameter
542     param-reg-2 gc-root-count MOV
543     ! Call GC
544     %prepare-alien-invoke
545     "inline_gc" f %alien-invoke ;
546
547 M:: x86 %gc ( temp1 temp2 gc-roots gc-root-count -- )
548     "end" define-label
549     temp1 temp2 check-nursery
550     "end" get JLE
551     gc-roots temp1 save-gc-roots
552     gc-root-count call-gc
553     gc-roots temp1 load-gc-roots
554     "end" resolve-label ;
555
556 M: x86 %alien-global
557     [ 0 MOV ] 2dip rc-absolute-cell rel-dlsym ;
558
559 M: x86 %epilogue ( n -- ) cell - incr-stack-reg ;
560
561 :: %boolean ( dst temp word -- )
562     dst \ f tag-number MOV
563     temp 0 MOV \ t rc-absolute-cell rel-immediate
564     dst temp word execute ; inline
565
566 M: x86 %compare ( dst temp cc src1 src2 -- )
567     CMP {
568         { cc< [ \ CMOVL %boolean ] }
569         { cc<= [ \ CMOVLE %boolean ] }
570         { cc> [ \ CMOVG %boolean ] }
571         { cc>= [ \ CMOVGE %boolean ] }
572         { cc= [ \ CMOVE %boolean ] }
573         { cc/= [ \ CMOVNE %boolean ] }
574     } case ;
575
576 M: x86 %compare-imm ( dst temp cc src1 src2 -- )
577     %compare ;
578
579 M: x86 %compare-float ( dst temp cc src1 src2 -- )
580     UCOMISD {
581         { cc< [ \ CMOVB %boolean ] }
582         { cc<= [ \ CMOVBE %boolean ] }
583         { cc> [ \ CMOVA %boolean ] }
584         { cc>= [ \ CMOVAE %boolean ] }
585         { cc= [ \ CMOVE %boolean ] }
586         { cc/= [ \ CMOVNE %boolean ] }
587     } case ;
588
589 M: x86 %compare-branch ( label cc src1 src2 -- )
590     CMP {
591         { cc< [ JL ] }
592         { cc<= [ JLE ] }
593         { cc> [ JG ] }
594         { cc>= [ JGE ] }
595         { cc= [ JE ] }
596         { cc/= [ JNE ] }
597     } case ;
598
599 M: x86 %compare-imm-branch ( label src1 src2 cc -- )
600     %compare-branch ;
601
602 M: x86 %compare-float-branch ( label cc src1 src2 -- )
603     UCOMISD {
604         { cc< [ JB ] }
605         { cc<= [ JBE ] }
606         { cc> [ JA ] }
607         { cc>= [ JAE ] }
608         { cc= [ JE ] }
609         { cc/= [ JNE ] }
610     } case ;
611
612 M: x86 %spill-integer ( src n -- ) spill-integer@ swap MOV ;
613 M: x86 %reload-integer ( dst n -- ) spill-integer@ MOV ;
614
615 M: x86 %spill-float ( src n -- ) spill-float@ swap MOVSD ;
616 M: x86 %reload-float ( dst n -- ) spill-float@ MOVSD ;
617
618 M: x86 %loop-entry 16 code-alignment [ NOP ] times ;
619
620 M: int-regs %save-param-reg drop [ param@ ] dip MOV ;
621 M: int-regs %load-param-reg drop swap param@ MOV ;
622
623 GENERIC: MOVSS/D ( dst src reg-class -- )
624
625 M: single-float-regs MOVSS/D drop MOVSS ;
626 M: double-float-regs MOVSS/D drop MOVSD ;
627
628 M: float-regs %save-param-reg [ param@ ] 2dip MOVSS/D ;
629 M: float-regs %load-param-reg [ swap param@ ] dip MOVSS/D ;
630
631 GENERIC: push-return-reg ( reg-class -- )
632 GENERIC: load-return-reg ( n reg-class -- )
633 GENERIC: store-return-reg ( n reg-class -- )
634
635 M: x86 %prepare-alien-invoke
636     #! Save Factor stack pointers in case the C code calls a
637     #! callback which does a GC, which must reliably trace
638     #! all roots.
639     temp-reg-1 "stack_chain" f %alien-global
640     temp-reg-1 temp-reg-1 [] MOV
641     temp-reg-1 [] stack-reg MOV
642     temp-reg-1 [] cell SUB
643     temp-reg-1 2 cells [+] ds-reg MOV
644     temp-reg-1 3 cells [+] rs-reg MOV ;
645
646 M: x86 value-struct? drop t ;
647
648 M: x86 small-enough? ( n -- ? )
649     HEX: -80000000 HEX: 7fffffff between? ;
650
651 : next-stack@ ( n -- operand )
652     #! nth parameter from the next stack frame. Used to box
653     #! input values to callbacks; the callback has its own
654     #! stack frame set up, and we want to read the frame
655     #! set up by the caller.
656     stack-frame get total-size>> + stack@ ;