1 ! Copyright (C) 2005, 2009 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.x86.assembler.operands
5 cpu.x86.features cpu.x86.features.private cpu.architecture kernel
6 kernel.private math memory namespaces make sequences words system
7 layouts combinators math.order fry locals compiler.constants
8 byte-arrays io macros quotations compiler compiler.units init vm
10 compiler.cfg.instructions
11 compiler.cfg.intrinsics
12 compiler.cfg.comparisons
13 compiler.cfg.stack-frame
14 compiler.codegen.fixup ;
15 FROM: layouts => cell ;
19 ! Add some methods to the assembler to be more useful to the backend
20 M: label JMP 0 JMP rc-relative label-fixup ;
21 M: label JUMPcc [ 0 ] dip JUMPcc rc-relative label-fixup ;
23 M: x86 two-operand? t ;
25 M: x86 vector-regs float-regs ;
27 HOOK: stack-reg cpu ( -- reg )
29 HOOK: reserved-area-size cpu ( -- n )
31 : stack@ ( n -- op ) stack-reg swap [+] ;
33 : param@ ( n -- op ) reserved-area-size + stack@ ;
35 : spill@ ( n -- op ) spill-offset param@ ;
37 : gc-root@ ( n -- op ) gc-root-offset param@ ;
39 : decr-stack-reg ( n -- )
40 dup 0 = [ drop ] [ stack-reg swap SUB ] if ;
42 : incr-stack-reg ( n -- )
43 dup 0 = [ drop ] [ stack-reg swap ADD ] if ;
45 : align-stack ( n -- n' )
46 os macosx? cpu x86.64? or [ 16 align ] when ;
48 M: x86 stack-frame-size ( stack-frame -- i )
49 (stack-frame-size) 3 cells reserved-area-size + + align-stack ;
51 ! Must be a volatile register not used for parameter passing, for safe
52 ! use in calls in and out of C
53 HOOK: temp-reg cpu ( -- reg )
55 HOOK: pic-tail-reg cpu ( -- reg )
57 M: x86 %load-immediate dup 0 = [ drop dup XOR ] [ MOV ] if ;
59 M: x86 %load-reference swap 0 MOV rc-absolute-cell rel-immediate ;
61 HOOK: ds-reg cpu ( -- reg )
62 HOOK: rs-reg cpu ( -- reg )
64 : reg-stack ( n reg -- op ) swap cells neg [+] ;
66 GENERIC: loc>operand ( loc -- operand )
68 M: ds-loc loc>operand n>> ds-reg reg-stack ;
69 M: rs-loc loc>operand n>> rs-reg reg-stack ;
71 M: x86 %peek loc>operand MOV ;
72 M: x86 %replace loc>operand swap MOV ;
73 : (%inc) ( n reg -- ) swap cells dup 0 > [ ADD ] [ neg SUB ] if ; inline
74 M: x86 %inc-d ( n -- ) ds-reg (%inc) ;
75 M: x86 %inc-r ( n -- ) rs-reg (%inc) ;
77 M: x86 %call ( word -- ) 0 CALL rc-relative rel-word-pic ;
79 : xt-tail-pic-offset ( -- n )
80 #! See the comment in vm/cpu-x86.hpp
83 M: x86 %jump ( word -- )
84 pic-tail-reg 0 MOV xt-tail-pic-offset rc-absolute-cell rel-here
85 0 JMP rc-relative rel-word-pic-tail ;
87 M: x86 %jump-label ( label -- ) 0 JMP rc-relative label-fixup ;
89 M: x86 %return ( -- ) 0 RET ;
91 : code-alignment ( align -- n )
92 [ building get length dup ] dip align swap - ;
97 :: (%slot-imm) ( obj slot tag -- op )
98 obj slot cells tag - [+] ; inline
100 M: x86 %slot ( dst obj slot -- ) [+] MOV ;
101 M: x86 %slot-imm ( dst obj slot tag -- ) (%slot-imm) MOV ;
102 M: x86 %set-slot ( src obj slot -- ) [+] swap MOV ;
103 M: x86 %set-slot-imm ( src obj slot tag -- ) (%slot-imm) swap MOV ;
105 M: x86 %add 2over eq? [ nip ADD ] [ [+] LEA ] if ;
106 M: x86 %add-imm 2over eq? [ nip ADD ] [ [+] LEA ] if ;
107 M: x86 %sub nip SUB ;
108 M: x86 %sub-imm 2over eq? [ nip SUB ] [ neg [+] LEA ] if ;
109 M: x86 %mul nip swap IMUL2 ;
110 M: x86 %mul-imm IMUL3 ;
111 M: x86 %and nip AND ;
112 M: x86 %and-imm nip AND ;
114 M: x86 %or-imm nip OR ;
115 M: x86 %xor nip XOR ;
116 M: x86 %xor-imm nip XOR ;
117 M: x86 %shl-imm nip SHL ;
118 M: x86 %shr-imm nip SHR ;
119 M: x86 %sar-imm nip SAR ;
121 M: x86 %min nip [ CMP ] [ CMOVG ] 2bi ;
122 M: x86 %max nip [ CMP ] [ CMOVL ] 2bi ;
124 M: x86 %not drop NOT ;
127 GENERIC: copy-register* ( dst src rep -- )
129 M: int-rep copy-register* drop MOV ;
130 M: tagged-rep copy-register* drop MOV ;
131 M: float-rep copy-register* drop MOVSS ;
132 M: double-rep copy-register* drop MOVSD ;
133 M: float-4-rep copy-register* drop MOVUPS ;
134 M: double-2-rep copy-register* drop MOVUPD ;
135 M: vector-rep copy-register* drop MOVDQU ;
137 M: x86 %copy ( dst src rep -- )
138 2over eq? [ 3drop ] [
139 [ [ dup spill-slot? [ n>> spill@ ] when ] bi@ ] dip
143 :: overflow-template ( label dst src1 src2 insn -- )
147 M: x86 %fixnum-add ( label dst src1 src2 -- )
148 [ ADD ] overflow-template ;
150 M: x86 %fixnum-sub ( label dst src1 src2 -- )
151 [ SUB ] overflow-template ;
153 M: x86 %fixnum-mul ( label dst src1 src2 -- )
154 [ swap IMUL2 ] overflow-template ;
156 : bignum@ ( reg n -- op )
157 cells bignum tag-number - [+] ; inline
159 M:: x86 %integer>bignum ( dst src temp -- )
160 #! on entry, inreg is a signed 32-bit quantity
161 #! exits with tagged ptr to bignum in outreg
162 #! 1 cell header, 1 cell length, 1 cell sign, + digits
163 #! length is the # of digits + sign
166 ! Load cached zero value
167 dst 0 >bignum %load-reference
169 ! Is it zero? Then just go to the end and return this zero
172 dst 4 cells bignum temp %allot
174 dst 1 bignum@ 2 tag-fixnum MOV
176 dst 3 bignum@ src MOV
179 temp cell-bits 1 - SAR
182 dst 2 bignum@ temp MOV
183 ! Make negative value positive
189 dst 3 bignum@ temp MOV
193 M:: x86 %bignum>integer ( dst src temp -- )
197 temp src 1 bignum@ MOV
198 ! if the length is 1, its just the sign and nothing else,
201 temp 1 tag-fixnum CMP
204 dst src 3 bignum@ MOV
206 temp src 2 bignum@ MOV
207 ! convert it into -1 or 1
216 M: x86 %add-float nip ADDSD ;
217 M: x86 %sub-float nip SUBSD ;
218 M: x86 %mul-float nip MULSD ;
219 M: x86 %div-float nip DIVSD ;
220 M: x86 %min-float nip MINSD ;
221 M: x86 %max-float nip MAXSD ;
222 M: x86 %sqrt SQRTSD ;
224 M: x86 %single>double-float CVTSS2SD ;
225 M: x86 %double>single-float CVTSD2SS ;
227 M: x86 %integer>float CVTSI2SD ;
228 M: x86 %float>integer CVTTSD2SI ;
230 M: x86 %unbox-float ( dst src -- )
231 float-offset [+] MOVSD ;
233 M:: x86 %box-float ( dst src temp -- )
234 dst 16 float temp %allot
235 dst float-offset [+] src MOVSD ;
237 M:: x86 %box-vector ( dst src rep temp -- )
238 dst rep rep-size 2 cells + byte-array temp %allot
239 16 tag-fixnum dst 1 byte-array tag-number %set-slot-imm
240 dst byte-array-offset [+]
243 M:: x86 %unbox-vector ( dst src rep -- )
244 dst src byte-array-offset [+]
247 MACRO: available-reps ( alist -- )
248 ! Each SSE version adds new representations and supports
250 unzip { } [ append ] accumulate rest swap suffix
251 [ [ 1quotation ] map ] bi@ zip
252 reverse [ { } ] suffix
255 M: x86 %broadcast-vector ( dst src rep -- )
257 { float-4-rep [ [ float-4-rep %copy ] [ drop dup 0 SHUFPS ] 2bi ] }
258 { double-2-rep [ [ double-2-rep %copy ] [ drop dup UNPCKLPD ] 2bi ] }
261 M: x86 %broadcast-vector-reps
263 ! Can't do this with sse1 since it will want to unbox
264 ! a double-precision float and convert to single precision
265 { sse2? { float-4-rep double-2-rep } }
268 M:: x86 %gather-vector-4 ( dst src1 src2 src3 src4 rep -- )
273 dst src1 float-4-rep %copy
281 M: x86 %gather-vector-4-reps
283 ! Can't do this with sse1 since it will want to unbox
284 ! double-precision floats and convert to single precision
285 { sse2? { float-4-rep } }
288 M:: x86 %gather-vector-2 ( dst src1 src2 rep -- )
293 dst src1 double-2-rep %copy
299 M: x86 %gather-vector-2-reps
301 { sse2? { double-2-rep } }
304 M: x86 %add-vector ( dst src1 src2 rep -- )
306 { float-4-rep [ ADDPS ] }
307 { double-2-rep [ ADDPD ] }
308 { char-16-rep [ PADDB ] }
309 { uchar-16-rep [ PADDB ] }
310 { short-8-rep [ PADDW ] }
311 { ushort-8-rep [ PADDW ] }
312 { int-4-rep [ PADDD ] }
313 { uint-4-rep [ PADDD ] }
314 { longlong-2-rep [ PADDQ ] }
315 { ulonglong-2-rep [ PADDQ ] }
318 M: x86 %add-vector-reps
320 { sse? { float-4-rep } }
321 { sse2? { double-2-rep char-16-rep uchar-16-rep short-8-rep ushort-8-rep int-4-rep uint-4-rep longlong-2-rep ulonglong-2-rep } }
324 M: x86 %saturated-add-vector ( dst src1 src2 rep -- )
326 { char-16-rep [ PADDSB ] }
327 { uchar-16-rep [ PADDUSB ] }
328 { short-8-rep [ PADDSW ] }
329 { ushort-8-rep [ PADDUSW ] }
332 M: x86 %saturated-add-vector-reps
334 { sse2? { char-16-rep uchar-16-rep short-8-rep ushort-8-rep } }
337 M: x86 %add-sub-vector ( dst src1 src2 rep -- )
339 { float-4-rep [ ADDSUBPS ] }
340 { double-2-rep [ ADDSUBPD ] }
343 M: x86 %add-sub-vector-reps
345 { sse3? { float-4-rep double-2-rep } }
348 M: x86 %sub-vector ( dst src1 src2 rep -- )
350 { float-4-rep [ SUBPS ] }
351 { double-2-rep [ SUBPD ] }
352 { char-16-rep [ PSUBB ] }
353 { uchar-16-rep [ PSUBB ] }
354 { short-8-rep [ PSUBW ] }
355 { ushort-8-rep [ PSUBW ] }
356 { int-4-rep [ PSUBD ] }
357 { uint-4-rep [ PSUBD ] }
358 { longlong-2-rep [ PSUBQ ] }
359 { ulonglong-2-rep [ PSUBQ ] }
362 M: x86 %sub-vector-reps
364 { sse? { float-4-rep } }
365 { sse2? { double-2-rep char-16-rep uchar-16-rep short-8-rep ushort-8-rep int-4-rep uint-4-rep longlong-2-rep ulonglong-2-rep } }
368 M: x86 %saturated-sub-vector ( dst src1 src2 rep -- )
370 { char-16-rep [ PSUBSB ] }
371 { uchar-16-rep [ PSUBUSB ] }
372 { short-8-rep [ PSUBSW ] }
373 { ushort-8-rep [ PSUBUSW ] }
376 M: x86 %saturated-sub-vector-reps
378 { sse2? { char-16-rep uchar-16-rep short-8-rep ushort-8-rep } }
381 M: x86 %mul-vector ( dst src1 src2 rep -- )
383 { float-4-rep [ MULPS ] }
384 { double-2-rep [ MULPD ] }
385 { short-8-rep [ PMULLW ] }
386 { ushort-8-rep [ PMULLW ] }
387 { int-4-rep [ PMULLD ] }
388 { uint-4-rep [ PMULLD ] }
391 M: x86 %mul-vector-reps
393 { sse? { float-4-rep } }
394 { sse2? { double-2-rep short-8-rep ushort-8-rep } }
395 { sse4.1? { int-4-rep uint-4-rep } }
398 M: x86 %saturated-mul-vector-reps
399 ! No multiplication with saturation on x86
402 M: x86 %div-vector ( dst src1 src2 rep -- )
404 { float-4-rep [ DIVPS ] }
405 { double-2-rep [ DIVPD ] }
408 M: x86 %div-vector-reps
410 { sse? { float-4-rep } }
411 { sse2? { double-2-rep } }
414 M: x86 %min-vector ( dst src1 src2 rep -- )
416 { char-16-rep [ PMINSB ] }
417 { uchar-16-rep [ PMINUB ] }
418 { short-8-rep [ PMINSW ] }
419 { ushort-8-rep [ PMINUW ] }
420 { int-4-rep [ PMINSD ] }
421 { uint-4-rep [ PMINUD ] }
422 { float-4-rep [ MINPS ] }
423 { double-2-rep [ MINPD ] }
426 M: x86 %min-vector-reps
428 { sse? { float-4-rep } }
429 { sse2? { uchar-16-rep short-8-rep double-2-rep short-8-rep uchar-16-rep } }
430 { sse4.1? { char-16-rep ushort-8-rep int-4-rep uint-4-rep } }
433 M: x86 %max-vector ( dst src1 src2 rep -- )
435 { char-16-rep [ PMAXSB ] }
436 { uchar-16-rep [ PMAXUB ] }
437 { short-8-rep [ PMAXSW ] }
438 { ushort-8-rep [ PMAXUW ] }
439 { int-4-rep [ PMAXSD ] }
440 { uint-4-rep [ PMAXUD ] }
441 { float-4-rep [ MAXPS ] }
442 { double-2-rep [ MAXPD ] }
445 M: x86 %max-vector-reps
447 { sse? { float-4-rep } }
448 { sse2? { uchar-16-rep short-8-rep double-2-rep short-8-rep uchar-16-rep } }
449 { sse4.1? { char-16-rep ushort-8-rep int-4-rep uint-4-rep } }
452 M: x86 %horizontal-add-vector ( dst src rep -- )
454 { float-4-rep [ [ float-4-rep %copy ] [ HADDPS ] [ HADDPS ] 2tri ] }
455 { double-2-rep [ [ double-2-rep %copy ] [ HADDPD ] 2bi ] }
458 M: x86 %horizontal-add-vector-reps
460 { sse3? { float-4-rep double-2-rep } }
463 M: x86 %abs-vector ( dst src rep -- )
465 { char-16-rep [ PABSB ] }
466 { short-8-rep [ PABSW ] }
467 { int-4-rep [ PABSD ] }
470 M: x86 %abs-vector-reps
472 { ssse3? { char-16-rep short-8-rep int-4-rep } }
475 M: x86 %sqrt-vector ( dst src rep -- )
477 { float-4-rep [ SQRTPS ] }
478 { double-2-rep [ SQRTPD ] }
481 M: x86 %sqrt-vector-reps
483 { sse? { float-4-rep } }
484 { sse2? { double-2-rep } }
487 M: x86 %and-vector ( dst src1 src2 rep -- )
489 { float-4-rep [ ANDPS ] }
490 { double-2-rep [ ANDPD ] }
494 M: x86 %and-vector-reps
496 { sse? { float-4-rep } }
497 { sse2? { double-2-rep char-16-rep uchar-16-rep short-8-rep ushort-8-rep int-4-rep uint-4-rep longlong-2-rep ulonglong-2-rep } }
500 M: x86 %or-vector ( dst src1 src2 rep -- )
502 { float-4-rep [ ORPS ] }
503 { double-2-rep [ ORPD ] }
507 M: x86 %or-vector-reps
509 { sse? { float-4-rep } }
510 { sse2? { double-2-rep char-16-rep uchar-16-rep short-8-rep ushort-8-rep int-4-rep uint-4-rep longlong-2-rep ulonglong-2-rep } }
513 M: x86 %xor-vector ( dst src1 src2 rep -- )
515 { float-4-rep [ XORPS ] }
516 { double-2-rep [ XORPD ] }
520 M: x86 %xor-vector-reps
522 { sse? { float-4-rep } }
523 { sse2? { double-2-rep char-16-rep uchar-16-rep short-8-rep ushort-8-rep int-4-rep uint-4-rep longlong-2-rep ulonglong-2-rep } }
526 M: x86 %shl-vector ( dst src1 src2 rep -- )
528 { short-8-rep [ PSLLW ] }
529 { ushort-8-rep [ PSLLW ] }
530 { int-4-rep [ PSLLD ] }
531 { uint-4-rep [ PSLLD ] }
532 { longlong-2-rep [ PSLLQ ] }
533 { ulonglong-2-rep [ PSLLQ ] }
536 M: x86 %shl-vector-reps
538 { sse2? { short-8-rep ushort-8-rep int-4-rep uint-4-rep longlong-2-rep ulonglong-2-rep } }
541 M: x86 %shr-vector ( dst src1 src2 rep -- )
543 { short-8-rep [ PSRAW ] }
544 { ushort-8-rep [ PSRLW ] }
545 { int-4-rep [ PSRAD ] }
546 { uint-4-rep [ PSRLD ] }
547 { ulonglong-2-rep [ PSRLQ ] }
550 M: x86 %shr-vector-reps
552 { sse2? { short-8-rep ushort-8-rep int-4-rep uint-4-rep ulonglong-2-rep } }
555 M: x86 %integer>scalar drop MOVD ;
557 M: x86 %scalar>integer drop MOVD ;
559 M: x86 %unbox-alien ( dst src -- )
560 alien-offset [+] MOV ;
562 M:: x86 %unbox-any-c-ptr ( dst src temp -- )
564 { "is-byte-array" "end" "start" } [ define-label ] each
567 ! We come back here with displaced aliens
568 "start" resolve-label
570 temp \ f tag-number CMP
572 ! Is the object an alien?
573 temp header-offset [+] alien type-number tag-fixnum CMP
574 "is-byte-array" get JNE
575 ! If so, load the offset and add it to the address
576 dst temp alien-offset [+] ADD
577 ! Now recurse on the underlying alien
578 temp temp underlying-alien-offset [+] MOV
580 "is-byte-array" resolve-label
581 ! Add byte array address to address being computed
583 ! Add an offset to start of byte array's data
584 dst byte-array-offset ADD
588 : alien@ ( reg n -- op ) cells alien tag-number - [+] ;
590 :: %allot-alien ( dst displacement base temp -- )
591 dst 4 cells alien temp %allot
592 dst 1 alien@ base MOV ! alien
593 dst 2 alien@ \ f tag-number MOV ! expired
594 dst 3 alien@ displacement MOV ! displacement
597 M:: x86 %box-alien ( dst src temp -- )
600 dst \ f tag-number MOV
603 dst src \ f tag-number temp %allot-alien
607 M:: x86 %box-displaced-alien ( dst displacement base displacement' base' base-class -- )
611 ! If displacement is zero, return the base
615 ! Quickly use displacement' before its needed for real, as allot temporary
616 dst 4 cells alien displacement' %allot
617 ! If base is already a displaced alien, unpack it
619 displacement' displacement MOV
620 base \ f tag-number CMP
622 base header-offset [+] alien type-number tag-fixnum CMP
624 ! displacement += base.displacement
625 displacement' base 3 alien@ ADD
627 base' base 1 alien@ MOV
629 dst 1 alien@ base' MOV ! alien
630 dst 2 alien@ \ f tag-number MOV ! expired
631 dst 3 alien@ displacement' MOV ! displacement
635 ! The 'small-reg' mess is pretty crappy, but its only used on x86-32.
636 ! On x86-64, all registers have 8-bit versions. However, a similar
637 ! problem arises for shifts, where the shift count must be in CL, and
638 ! so one day I will fix this properly by adding precoloring to the
639 ! register allocator.
641 HOOK: has-small-reg? cpu ( reg size -- ? )
643 CONSTANT: have-byte-regs { EAX ECX EDX EBX }
645 M: x86.32 has-small-reg?
647 { 8 [ have-byte-regs memq? ] }
652 M: x86.64 has-small-reg? 2drop t ;
654 : small-reg-that-isn't ( exclude -- reg' )
655 [ have-byte-regs ] dip
656 [ native-version-of ] map
657 '[ _ memq? not ] find nip ;
659 : with-save/restore ( reg quot -- )
660 [ drop PUSH ] [ call ] [ drop POP ] 2tri ; inline
662 :: with-small-register ( dst exclude size quot: ( new-dst -- ) -- )
663 ! If the destination register overlaps a small register with
664 ! 'size' bits, we call the quot with that. Otherwise, we find a
665 ! small register that is not in exclude, and call quot, saving and
666 ! restoring the small register.
667 dst size has-small-reg? [ dst quot call ] [
668 exclude small-reg-that-isn't
669 [ quot call ] with-save/restore
672 M:: x86 %string-nth ( dst src index temp -- )
673 ! We request a small-reg of size 8 since those of size 16 are
676 dst { src index temp } 8 [| new-dst |
677 ! Load the least significant 7 bits into new-dst.
678 ! 8th bit indicates whether we have to load from
679 ! the aux vector or not.
680 temp src index [+] LEA
681 new-dst 8-bit-version-of temp string-offset [+] MOV
682 new-dst new-dst 8-bit-version-of MOVZX
683 ! Do we have to look at the aux vector?
686 ! Yes, this is a non-ASCII character. Load aux vector
687 temp src string-aux-offset [+] MOV
693 new-dst 16-bit-version-of new-dst byte-array-offset [+] MOV
694 new-dst new-dst 16-bit-version-of MOVZX
699 dst new-dst int-rep %copy
700 ] with-small-register ;
702 M:: x86 %set-string-nth-fast ( ch str index temp -- )
703 ch { index str temp } 8 [| new-ch |
704 new-ch ch int-rep %copy
705 temp str index [+] LEA
706 temp string-offset [+] new-ch 8-bit-version-of MOV
707 ] with-small-register ;
709 :: %alien-integer-getter ( dst src size quot -- )
710 dst { src } size [| new-dst |
711 new-dst dup size n-bit-version-of dup src [] MOV
713 dst new-dst int-rep %copy
714 ] with-small-register ; inline
716 : %alien-unsigned-getter ( dst src size -- )
717 [ MOVZX ] %alien-integer-getter ; inline
719 M: x86 %alien-unsigned-1 8 %alien-unsigned-getter ;
720 M: x86 %alien-unsigned-2 16 %alien-unsigned-getter ;
721 M: x86 %alien-unsigned-4 32 [ 2drop ] %alien-integer-getter ;
723 : %alien-signed-getter ( dst src size -- )
724 [ MOVSX ] %alien-integer-getter ; inline
726 M: x86 %alien-signed-1 8 %alien-signed-getter ;
727 M: x86 %alien-signed-2 16 %alien-signed-getter ;
728 M: x86 %alien-signed-4 32 %alien-signed-getter ;
730 M: x86 %alien-cell [] MOV ;
731 M: x86 %alien-float [] MOVSS ;
732 M: x86 %alien-double [] MOVSD ;
733 M: x86 %alien-vector [ [] ] dip %copy ;
735 :: %alien-integer-setter ( ptr value size -- )
736 value { ptr } size [| new-value |
737 new-value value int-rep %copy
738 ptr [] new-value size n-bit-version-of MOV
739 ] with-small-register ; inline
741 M: x86 %set-alien-integer-1 8 %alien-integer-setter ;
742 M: x86 %set-alien-integer-2 16 %alien-integer-setter ;
743 M: x86 %set-alien-integer-4 32 %alien-integer-setter ;
744 M: x86 %set-alien-cell [ [] ] dip MOV ;
745 M: x86 %set-alien-float [ [] ] dip MOVSS ;
746 M: x86 %set-alien-double [ [] ] dip MOVSD ;
747 M: x86 %set-alien-vector [ [] ] 2dip %copy ;
749 : shift-count? ( reg -- ? ) { ECX RCX } memq? ;
751 :: emit-shift ( dst src1 src2 quot -- )
760 ECX native-version-of [
762 drop dst CL quot call
767 M: x86 %shl [ SHL ] emit-shift ;
768 M: x86 %shr [ SHR ] emit-shift ;
769 M: x86 %sar [ SAR ] emit-shift ;
771 M: x86 %vm-field-ptr ( dst field -- )
772 [ drop 0 MOV rc-absolute-cell rt-vm rel-fixup ]
773 [ vm-field-offset ADD ] 2bi ;
775 : load-zone-ptr ( reg -- )
776 #! Load pointer to start of zone array
777 "nursery" %vm-field-ptr ;
779 : load-allot-ptr ( nursery-ptr allot-ptr -- )
780 [ drop load-zone-ptr ] [ swap cell [+] MOV ] 2bi ;
782 : inc-allot-ptr ( nursery-ptr n -- )
783 [ cell [+] ] dip 8 align ADD ;
785 : store-header ( temp class -- )
786 [ [] ] [ type-number tag-fixnum ] bi* MOV ;
788 : store-tagged ( dst tag -- )
791 M:: x86 %allot ( dst size class nursery-ptr -- )
792 nursery-ptr dst load-allot-ptr
793 dst class store-header
794 dst class store-tagged
795 nursery-ptr size inc-allot-ptr ;
798 M:: x86 %write-barrier ( src card# table -- )
799 #! Mark the card pointed to by vreg.
803 table "cards_offset" %vm-field-ptr
805 table card# [+] card-mark <byte> MOV
808 card# deck-bits card-bits - SHR
809 table "decks_offset" %vm-field-ptr
811 table card# [+] card-mark <byte> MOV ;
813 M:: x86 %check-nursery ( label temp1 temp2 -- )
815 temp2 temp1 cell [+] MOV
817 temp1 temp1 3 cells [+] MOV
821 M: x86 %save-gc-root ( gc-root register -- ) [ gc-root@ ] dip MOV ;
823 M: x86 %load-gc-root ( gc-root register -- ) swap gc-root@ MOV ;
825 M: x86 %alien-global ( dst symbol library -- )
826 [ 0 MOV ] 2dip rc-absolute-cell rel-dlsym ;
828 M: x86 %epilogue ( n -- ) cell - incr-stack-reg ;
830 :: %boolean ( dst temp word -- )
831 dst \ f tag-number MOV
832 temp 0 MOV \ t rc-absolute-cell rel-immediate
833 dst temp word execute ; inline
835 M:: x86 %compare ( dst src1 src2 cc temp -- )
838 { cc< [ dst temp \ CMOVL %boolean ] }
839 { cc<= [ dst temp \ CMOVLE %boolean ] }
840 { cc> [ dst temp \ CMOVG %boolean ] }
841 { cc>= [ dst temp \ CMOVGE %boolean ] }
842 { cc= [ dst temp \ CMOVE %boolean ] }
843 { cc/= [ dst temp \ CMOVNE %boolean ] }
846 M: x86 %compare-imm ( dst src1 src2 cc temp -- )
849 : %cmov-float= ( dst src -- )
851 "no-move" define-label
853 "no-move" get [ JNE ] [ JP ] bi
855 "no-move" resolve-label
858 : %cmov-float/= ( dst src -- )
860 "no-move" define-label
867 "no-move" resolve-label
870 :: (%compare-float) ( dst src1 src2 cc temp compare -- )
872 { cc< [ src2 src1 \ compare execute( a b -- ) dst temp \ CMOVA %boolean ] }
873 { cc<= [ src2 src1 \ compare execute( a b -- ) dst temp \ CMOVAE %boolean ] }
874 { cc> [ src1 src2 \ compare execute( a b -- ) dst temp \ CMOVA %boolean ] }
875 { cc>= [ src1 src2 \ compare execute( a b -- ) dst temp \ CMOVAE %boolean ] }
876 { cc= [ src1 src2 \ compare execute( a b -- ) dst temp \ %cmov-float= %boolean ] }
877 { cc<> [ src1 src2 \ compare execute( a b -- ) dst temp \ CMOVNE %boolean ] }
878 { cc<>= [ src1 src2 \ compare execute( a b -- ) dst temp \ CMOVNP %boolean ] }
879 { cc/< [ src2 src1 \ compare execute( a b -- ) dst temp \ CMOVBE %boolean ] }
880 { cc/<= [ src2 src1 \ compare execute( a b -- ) dst temp \ CMOVB %boolean ] }
881 { cc/> [ src1 src2 \ compare execute( a b -- ) dst temp \ CMOVBE %boolean ] }
882 { cc/>= [ src1 src2 \ compare execute( a b -- ) dst temp \ CMOVB %boolean ] }
883 { cc/= [ src1 src2 \ compare execute( a b -- ) dst temp \ %cmov-float/= %boolean ] }
884 { cc/<> [ src1 src2 \ compare execute( a b -- ) dst temp \ CMOVE %boolean ] }
885 { cc/<>= [ src1 src2 \ compare execute( a b -- ) dst temp \ CMOVP %boolean ] }
888 M: x86 %compare-float-ordered ( dst src1 src2 cc temp -- )
889 \ COMISD (%compare-float) ;
891 M: x86 %compare-float-unordered ( dst src1 src2 cc temp -- )
892 \ UCOMISD (%compare-float) ;
894 M:: x86 %compare-branch ( label src1 src2 cc -- )
898 { cc<= [ label JLE ] }
900 { cc>= [ label JGE ] }
902 { cc/= [ label JNE ] }
905 M: x86 %compare-imm-branch ( label src1 src2 cc -- )
908 : %jump-float= ( label -- )
910 "no-jump" define-label
913 "no-jump" resolve-label
916 : %jump-float/= ( label -- )
919 :: (%compare-float-branch) ( label src1 src2 cc compare -- )
921 { cc< [ src2 src1 \ compare execute( a b -- ) label JA ] }
922 { cc<= [ src2 src1 \ compare execute( a b -- ) label JAE ] }
923 { cc> [ src1 src2 \ compare execute( a b -- ) label JA ] }
924 { cc>= [ src1 src2 \ compare execute( a b -- ) label JAE ] }
925 { cc= [ src1 src2 \ compare execute( a b -- ) label %jump-float= ] }
926 { cc<> [ src1 src2 \ compare execute( a b -- ) label JNE ] }
927 { cc<>= [ src1 src2 \ compare execute( a b -- ) label JNP ] }
928 { cc/< [ src2 src1 \ compare execute( a b -- ) label JBE ] }
929 { cc/<= [ src2 src1 \ compare execute( a b -- ) label JB ] }
930 { cc/> [ src1 src2 \ compare execute( a b -- ) label JBE ] }
931 { cc/>= [ src1 src2 \ compare execute( a b -- ) label JB ] }
932 { cc/= [ src1 src2 \ compare execute( a b -- ) label %jump-float/= ] }
933 { cc/<> [ src1 src2 \ compare execute( a b -- ) label JE ] }
934 { cc/<>= [ src1 src2 \ compare execute( a b -- ) label JP ] }
937 M: x86 %compare-float-ordered-branch ( label src1 src2 cc -- )
938 \ COMISD (%compare-float-branch) ;
940 M: x86 %compare-float-unordered-branch ( label src1 src2 cc -- )
941 \ UCOMISD (%compare-float-branch) ;
943 M:: x86 %spill ( src rep dst -- ) dst src rep %copy ;
944 M:: x86 %reload ( dst rep src -- ) dst src rep %copy ;
946 M: x86 %loop-entry 16 code-alignment [ NOP ] times ;
948 M:: x86 %save-context ( temp1 temp2 callback-allowed? -- )
949 #! Save Factor stack pointers in case the C code calls a
950 #! callback which does a GC, which must reliably trace
952 temp1 0 MOV rc-absolute-cell rt-vm rel-fixup
953 temp1 temp1 "stack_chain" vm-field-offset [+] MOV
954 temp2 stack-reg cell neg [+] LEA
957 temp1 2 cells [+] ds-reg MOV
958 temp1 3 cells [+] rs-reg MOV
961 M: x86 value-struct? drop t ;
963 M: x86 small-enough? ( n -- ? )
964 HEX: -80000000 HEX: 7fffffff between? ;
966 : next-stack@ ( n -- operand )
967 #! nth parameter from the next stack frame. Used to box
968 #! input values to callbacks; the callback has its own
969 #! stack frame set up, and we want to read the frame
970 #! set up by the caller.
971 stack-frame get total-size>> + stack@ ;
977 :: install-sse2-check ( -- )
980 "This image was built to use SSE2 but your CPU does not support it." print
981 "You will need to bootstrap Factor again." print
985 ] "cpu.x86" add-init-hook ;
987 : enable-sse2 ( version -- )
989 enable-float-intrinsics
992 enable-float-functions
997 [ { sse_version } compile ] with-optimizer
998 "Checking for multimedia extensions: " write sse-version
999 [ sse-string write " detected" print ] [ enable-sse2 ] bi ;