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