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 ] [ copy-register* ] if ;
140 :: overflow-template ( label dst src1 src2 insn -- )
144 M: x86 %fixnum-add ( label dst src1 src2 -- )
145 [ ADD ] overflow-template ;
147 M: x86 %fixnum-sub ( label dst src1 src2 -- )
148 [ SUB ] overflow-template ;
150 M: x86 %fixnum-mul ( label dst src1 src2 -- )
151 [ swap IMUL2 ] overflow-template ;
153 : bignum@ ( reg n -- op )
154 cells bignum tag-number - [+] ; inline
156 M:: x86 %integer>bignum ( dst src temp -- )
157 #! on entry, inreg is a signed 32-bit quantity
158 #! exits with tagged ptr to bignum in outreg
159 #! 1 cell header, 1 cell length, 1 cell sign, + digits
160 #! length is the # of digits + sign
163 ! Load cached zero value
164 dst 0 >bignum %load-reference
166 ! Is it zero? Then just go to the end and return this zero
169 dst 4 cells bignum temp %allot
171 dst 1 bignum@ 2 tag-fixnum MOV
173 dst 3 bignum@ src MOV
176 temp cell-bits 1 - SAR
179 dst 2 bignum@ temp MOV
180 ! Make negative value positive
186 dst 3 bignum@ temp MOV
190 M:: x86 %bignum>integer ( dst src temp -- )
194 temp src 1 bignum@ MOV
195 ! if the length is 1, its just the sign and nothing else,
198 temp 1 tag-fixnum CMP
201 dst src 3 bignum@ MOV
203 temp src 2 bignum@ MOV
204 ! convert it into -1 or 1
213 M: x86 %add-float nip ADDSD ;
214 M: x86 %sub-float nip SUBSD ;
215 M: x86 %mul-float nip MULSD ;
216 M: x86 %div-float nip DIVSD ;
217 M: x86 %min-float nip MINSD ;
218 M: x86 %max-float nip MAXSD ;
219 M: x86 %sqrt SQRTSD ;
221 M: x86 %single>double-float CVTSS2SD ;
222 M: x86 %double>single-float CVTSD2SS ;
224 M: x86 %integer>float CVTSI2SD ;
225 M: x86 %float>integer CVTTSD2SI ;
227 M: x86 %unbox-float ( dst src -- )
228 float-offset [+] MOVSD ;
230 M:: x86 %box-float ( dst src temp -- )
231 dst 16 float temp %allot
232 dst float-offset [+] src MOVSD ;
234 M:: x86 %box-vector ( dst src rep temp -- )
235 dst rep rep-size 2 cells + byte-array temp %allot
236 16 tag-fixnum dst 1 byte-array tag-number %set-slot-imm
237 dst byte-array-offset [+]
240 M:: x86 %unbox-vector ( dst src rep -- )
241 dst src byte-array-offset [+]
244 MACRO: available-reps ( alist -- )
245 ! Each SSE version adds new representations and supports
247 unzip { } [ append ] accumulate rest swap suffix
248 [ [ 1quotation ] map ] bi@ zip
249 reverse [ { } ] suffix
252 M: x86 %broadcast-vector ( dst src rep -- )
254 { float-4-rep [ [ float-4-rep %copy ] [ drop dup 0 SHUFPS ] 2bi ] }
255 { double-2-rep [ [ double-2-rep %copy ] [ drop dup UNPCKLPD ] 2bi ] }
258 M: x86 %broadcast-vector-reps
260 ! Can't do this with sse1 since it will want to unbox
261 ! a double-precision float and convert to single precision
262 { sse2? { float-4-rep double-2-rep } }
265 M:: x86 %gather-vector-4 ( dst src1 src2 src3 src4 rep -- )
270 dst src1 float-4-rep %copy
278 M: x86 %gather-vector-4-reps
280 ! Can't do this with sse1 since it will want to unbox
281 ! double-precision floats and convert to single precision
282 { sse2? { float-4-rep } }
285 M:: x86 %gather-vector-2 ( dst src1 src2 rep -- )
290 dst src1 double-2-rep %copy
296 M: x86 %gather-vector-2-reps
298 { sse2? { double-2-rep } }
301 M: x86 %add-vector ( dst src1 src2 rep -- )
303 { float-4-rep [ ADDPS ] }
304 { double-2-rep [ ADDPD ] }
305 { char-16-rep [ PADDB ] }
306 { uchar-16-rep [ PADDB ] }
307 { short-8-rep [ PADDW ] }
308 { ushort-8-rep [ PADDW ] }
309 { int-4-rep [ PADDD ] }
310 { uint-4-rep [ PADDD ] }
311 { longlong-2-rep [ PADDQ ] }
312 { ulonglong-2-rep [ PADDQ ] }
315 M: x86 %add-vector-reps
317 { sse? { float-4-rep } }
318 { 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 } }
321 M: x86 %saturated-add-vector ( dst src1 src2 rep -- )
323 { char-16-rep [ PADDSB ] }
324 { uchar-16-rep [ PADDUSB ] }
325 { short-8-rep [ PADDSW ] }
326 { ushort-8-rep [ PADDUSW ] }
329 M: x86 %saturated-add-vector-reps
331 { sse2? { char-16-rep uchar-16-rep short-8-rep ushort-8-rep } }
334 M: x86 %add-sub-vector ( dst src1 src2 rep -- )
336 { float-4-rep [ ADDSUBPS ] }
337 { double-2-rep [ ADDSUBPD ] }
340 M: x86 %add-sub-vector-reps
342 { sse3? { float-4-rep double-2-rep } }
345 M: x86 %sub-vector ( dst src1 src2 rep -- )
347 { float-4-rep [ SUBPS ] }
348 { double-2-rep [ SUBPD ] }
349 { char-16-rep [ PSUBB ] }
350 { uchar-16-rep [ PSUBB ] }
351 { short-8-rep [ PSUBW ] }
352 { ushort-8-rep [ PSUBW ] }
353 { int-4-rep [ PSUBD ] }
354 { uint-4-rep [ PSUBD ] }
355 { longlong-2-rep [ PSUBQ ] }
356 { ulonglong-2-rep [ PSUBQ ] }
359 M: x86 %sub-vector-reps
361 { sse? { float-4-rep } }
362 { 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 } }
365 M: x86 %saturated-sub-vector ( dst src1 src2 rep -- )
367 { char-16-rep [ PSUBSB ] }
368 { uchar-16-rep [ PSUBUSB ] }
369 { short-8-rep [ PSUBSW ] }
370 { ushort-8-rep [ PSUBUSW ] }
373 M: x86 %saturated-sub-vector-reps
375 { sse2? { char-16-rep uchar-16-rep short-8-rep ushort-8-rep } }
378 M: x86 %mul-vector ( dst src1 src2 rep -- )
380 { float-4-rep [ MULPS ] }
381 { double-2-rep [ MULPD ] }
382 { short-8-rep [ PMULLW ] }
383 { ushort-8-rep [ PMULLW ] }
384 { int-4-rep [ PMULLD ] }
385 { uint-4-rep [ PMULLD ] }
388 M: x86 %mul-vector-reps
390 { sse? { float-4-rep } }
391 { sse2? { double-2-rep short-8-rep ushort-8-rep } }
392 { sse4.1? { int-4-rep uint-4-rep } }
395 M: x86 %saturated-mul-vector-reps
396 ! No multiplication with saturation on x86
399 M: x86 %div-vector ( dst src1 src2 rep -- )
401 { float-4-rep [ DIVPS ] }
402 { double-2-rep [ DIVPD ] }
405 M: x86 %div-vector-reps
407 { sse? { float-4-rep } }
408 { sse2? { double-2-rep } }
411 M: x86 %min-vector ( dst src1 src2 rep -- )
413 { char-16-rep [ PMINSB ] }
414 { uchar-16-rep [ PMINUB ] }
415 { short-8-rep [ PMINSW ] }
416 { ushort-8-rep [ PMINUW ] }
417 { int-4-rep [ PMINSD ] }
418 { uint-4-rep [ PMINUD ] }
419 { float-4-rep [ MINPS ] }
420 { double-2-rep [ MINPD ] }
423 M: x86 %min-vector-reps
425 { sse? { float-4-rep } }
426 { sse2? { uchar-16-rep short-8-rep double-2-rep short-8-rep uchar-16-rep } }
427 { sse4.1? { char-16-rep ushort-8-rep int-4-rep uint-4-rep } }
430 M: x86 %max-vector ( dst src1 src2 rep -- )
432 { char-16-rep [ PMAXSB ] }
433 { uchar-16-rep [ PMAXUB ] }
434 { short-8-rep [ PMAXSW ] }
435 { ushort-8-rep [ PMAXUW ] }
436 { int-4-rep [ PMAXSD ] }
437 { uint-4-rep [ PMAXUD ] }
438 { float-4-rep [ MAXPS ] }
439 { double-2-rep [ MAXPD ] }
442 M: x86 %max-vector-reps
444 { sse? { float-4-rep } }
445 { sse2? { uchar-16-rep short-8-rep double-2-rep short-8-rep uchar-16-rep } }
446 { sse4.1? { char-16-rep ushort-8-rep int-4-rep uint-4-rep } }
449 M: x86 %horizontal-add-vector ( dst src rep -- )
451 { float-4-rep [ [ float-4-rep %copy ] [ HADDPS ] [ HADDPS ] 2tri ] }
452 { double-2-rep [ [ double-2-rep %copy ] [ HADDPD ] 2bi ] }
455 M: x86 %horizontal-add-vector-reps
457 { sse3? { float-4-rep double-2-rep } }
460 M: x86 %abs-vector ( dst src rep -- )
462 { char-16-rep [ PABSB ] }
463 { short-8-rep [ PABSW ] }
464 { int-4-rep [ PABSD ] }
467 M: x86 %abs-vector-reps
469 { ssse3? { char-16-rep short-8-rep int-4-rep } }
472 M: x86 %sqrt-vector ( dst src rep -- )
474 { float-4-rep [ SQRTPS ] }
475 { double-2-rep [ SQRTPD ] }
478 M: x86 %sqrt-vector-reps
480 { sse? { float-4-rep } }
481 { sse2? { double-2-rep } }
484 M: x86 %and-vector ( dst src1 src2 rep -- )
486 { float-4-rep [ ANDPS ] }
487 { double-2-rep [ ANDPD ] }
491 M: x86 %and-vector-reps
493 { sse? { float-4-rep } }
494 { 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 } }
497 M: x86 %or-vector ( dst src1 src2 rep -- )
499 { float-4-rep [ ORPS ] }
500 { double-2-rep [ ORPD ] }
504 M: x86 %or-vector-reps
506 { sse? { float-4-rep } }
507 { 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 } }
510 M: x86 %xor-vector ( dst src1 src2 rep -- )
512 { float-4-rep [ XORPS ] }
513 { double-2-rep [ XORPD ] }
517 M: x86 %xor-vector-reps
519 { sse? { float-4-rep } }
520 { 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 } }
523 M: x86 %shl-vector ( dst src1 src2 rep -- )
525 { short-8-rep [ PSLLW ] }
526 { ushort-8-rep [ PSLLW ] }
527 { int-4-rep [ PSLLD ] }
528 { uint-4-rep [ PSLLD ] }
529 { longlong-2-rep [ PSLLQ ] }
530 { ulonglong-2-rep [ PSLLQ ] }
533 M: x86 %shl-vector-reps
535 { sse2? { short-8-rep ushort-8-rep int-4-rep uint-4-rep longlong-2-rep ulonglong-2-rep } }
538 M: x86 %shr-vector ( dst src1 src2 rep -- )
540 { short-8-rep [ PSRAW ] }
541 { ushort-8-rep [ PSRLW ] }
542 { int-4-rep [ PSRAD ] }
543 { uint-4-rep [ PSRLD ] }
544 { ulonglong-2-rep [ PSRLQ ] }
547 M: x86 %shr-vector-reps
549 { sse2? { short-8-rep ushort-8-rep int-4-rep uint-4-rep ulonglong-2-rep } }
552 M: x86 %integer>scalar drop MOVD ;
554 M: x86 %scalar>integer drop MOVD ;
556 M: x86 %unbox-alien ( dst src -- )
557 alien-offset [+] MOV ;
559 M:: x86 %unbox-any-c-ptr ( dst src temp -- )
561 { "is-byte-array" "end" "start" } [ define-label ] each
564 ! We come back here with displaced aliens
565 "start" resolve-label
567 temp \ f tag-number CMP
569 ! Is the object an alien?
570 temp header-offset [+] alien type-number tag-fixnum CMP
571 "is-byte-array" get JNE
572 ! If so, load the offset and add it to the address
573 dst temp alien-offset [+] ADD
574 ! Now recurse on the underlying alien
575 temp temp underlying-alien-offset [+] MOV
577 "is-byte-array" resolve-label
578 ! Add byte array address to address being computed
580 ! Add an offset to start of byte array's data
581 dst byte-array-offset ADD
585 : alien@ ( reg n -- op ) cells alien tag-number - [+] ;
587 :: %allot-alien ( dst displacement base temp -- )
588 dst 4 cells alien temp %allot
589 dst 1 alien@ base MOV ! alien
590 dst 2 alien@ \ f tag-number MOV ! expired
591 dst 3 alien@ displacement MOV ! displacement
594 M:: x86 %box-alien ( dst src temp -- )
597 dst \ f tag-number MOV
600 dst src \ f tag-number temp %allot-alien
604 M:: x86 %box-displaced-alien ( dst displacement base displacement' base' base-class -- )
608 ! If displacement is zero, return the base
612 ! Quickly use displacement' before its needed for real, as allot temporary
613 dst 4 cells alien displacement' %allot
614 ! If base is already a displaced alien, unpack it
616 displacement' displacement MOV
617 base \ f tag-number CMP
619 base header-offset [+] alien type-number tag-fixnum CMP
621 ! displacement += base.displacement
622 displacement' base 3 alien@ ADD
624 base' base 1 alien@ MOV
626 dst 1 alien@ base' MOV ! alien
627 dst 2 alien@ \ f tag-number MOV ! expired
628 dst 3 alien@ displacement' MOV ! displacement
632 ! The 'small-reg' mess is pretty crappy, but its only used on x86-32.
633 ! On x86-64, all registers have 8-bit versions. However, a similar
634 ! problem arises for shifts, where the shift count must be in CL, and
635 ! so one day I will fix this properly by adding precoloring to the
636 ! register allocator.
638 HOOK: has-small-reg? cpu ( reg size -- ? )
640 CONSTANT: have-byte-regs { EAX ECX EDX EBX }
642 M: x86.32 has-small-reg?
644 { 8 [ have-byte-regs memq? ] }
649 M: x86.64 has-small-reg? 2drop t ;
651 : small-reg-that-isn't ( exclude -- reg' )
652 [ have-byte-regs ] dip
653 [ native-version-of ] map
654 '[ _ memq? not ] find nip ;
656 : with-save/restore ( reg quot -- )
657 [ drop PUSH ] [ call ] [ drop POP ] 2tri ; inline
659 :: with-small-register ( dst exclude size quot: ( new-dst -- ) -- )
660 ! If the destination register overlaps a small register with
661 ! 'size' bits, we call the quot with that. Otherwise, we find a
662 ! small register that is not in exclude, and call quot, saving and
663 ! restoring the small register.
664 dst size has-small-reg? [ dst quot call ] [
665 exclude small-reg-that-isn't
666 [ quot call ] with-save/restore
669 M:: x86 %string-nth ( dst src index temp -- )
670 ! We request a small-reg of size 8 since those of size 16 are
673 dst { src index temp } 8 [| new-dst |
674 ! Load the least significant 7 bits into new-dst.
675 ! 8th bit indicates whether we have to load from
676 ! the aux vector or not.
677 temp src index [+] LEA
678 new-dst 8-bit-version-of temp string-offset [+] MOV
679 new-dst new-dst 8-bit-version-of MOVZX
680 ! Do we have to look at the aux vector?
683 ! Yes, this is a non-ASCII character. Load aux vector
684 temp src string-aux-offset [+] MOV
690 new-dst 16-bit-version-of new-dst byte-array-offset [+] MOV
691 new-dst new-dst 16-bit-version-of MOVZX
696 dst new-dst int-rep %copy
697 ] with-small-register ;
699 M:: x86 %set-string-nth-fast ( ch str index temp -- )
700 ch { index str temp } 8 [| new-ch |
701 new-ch ch int-rep %copy
702 temp str index [+] LEA
703 temp string-offset [+] new-ch 8-bit-version-of MOV
704 ] with-small-register ;
706 :: %alien-integer-getter ( dst src size quot -- )
707 dst { src } size [| new-dst |
708 new-dst dup size n-bit-version-of dup src [] MOV
710 dst new-dst int-rep %copy
711 ] with-small-register ; inline
713 : %alien-unsigned-getter ( dst src size -- )
714 [ MOVZX ] %alien-integer-getter ; inline
716 M: x86 %alien-unsigned-1 8 %alien-unsigned-getter ;
717 M: x86 %alien-unsigned-2 16 %alien-unsigned-getter ;
718 M: x86 %alien-unsigned-4 32 [ 2drop ] %alien-integer-getter ;
720 : %alien-signed-getter ( dst src size -- )
721 [ MOVSX ] %alien-integer-getter ; inline
723 M: x86 %alien-signed-1 8 %alien-signed-getter ;
724 M: x86 %alien-signed-2 16 %alien-signed-getter ;
725 M: x86 %alien-signed-4 32 %alien-signed-getter ;
727 M: x86 %alien-cell [] MOV ;
728 M: x86 %alien-float [] MOVSS ;
729 M: x86 %alien-double [] MOVSD ;
730 M: x86 %alien-vector [ [] ] dip %copy ;
732 :: %alien-integer-setter ( ptr value size -- )
733 value { ptr } size [| new-value |
734 new-value value int-rep %copy
735 ptr [] new-value size n-bit-version-of MOV
736 ] with-small-register ; inline
738 M: x86 %set-alien-integer-1 8 %alien-integer-setter ;
739 M: x86 %set-alien-integer-2 16 %alien-integer-setter ;
740 M: x86 %set-alien-integer-4 32 %alien-integer-setter ;
741 M: x86 %set-alien-cell [ [] ] dip MOV ;
742 M: x86 %set-alien-float [ [] ] dip MOVSS ;
743 M: x86 %set-alien-double [ [] ] dip MOVSD ;
744 M: x86 %set-alien-vector [ [] ] 2dip %copy ;
746 : shift-count? ( reg -- ? ) { ECX RCX } memq? ;
748 :: emit-shift ( dst src1 src2 quot -- )
757 ECX native-version-of [
759 drop dst CL quot call
764 M: x86 %shl [ SHL ] emit-shift ;
765 M: x86 %shr [ SHR ] emit-shift ;
766 M: x86 %sar [ SAR ] emit-shift ;
768 M: x86 %vm-field-ptr ( dst field -- )
769 [ drop 0 MOV rc-absolute-cell rt-vm rel-fixup ]
770 [ vm-field-offset ADD ] 2bi ;
772 : load-zone-ptr ( reg -- )
773 #! Load pointer to start of zone array
774 "nursery" %vm-field-ptr ;
776 : load-allot-ptr ( nursery-ptr allot-ptr -- )
777 [ drop load-zone-ptr ] [ swap cell [+] MOV ] 2bi ;
779 : inc-allot-ptr ( nursery-ptr n -- )
780 [ cell [+] ] dip 8 align ADD ;
782 : store-header ( temp class -- )
783 [ [] ] [ type-number tag-fixnum ] bi* MOV ;
785 : store-tagged ( dst tag -- )
788 M:: x86 %allot ( dst size class nursery-ptr -- )
789 nursery-ptr dst load-allot-ptr
790 dst class store-header
791 dst class store-tagged
792 nursery-ptr size inc-allot-ptr ;
795 M:: x86 %write-barrier ( src card# table -- )
796 #! Mark the card pointed to by vreg.
800 table "cards_offset" %vm-field-ptr
802 table card# [+] card-mark <byte> MOV
805 card# deck-bits card-bits - SHR
806 table "decks_offset" %vm-field-ptr
808 table card# [+] card-mark <byte> MOV ;
810 M:: x86 %check-nursery ( label temp1 temp2 -- )
812 temp2 temp1 cell [+] MOV
814 temp1 temp1 3 cells [+] MOV
818 M: x86 %save-gc-root ( gc-root register -- ) [ gc-root@ ] dip MOV ;
820 M: x86 %load-gc-root ( gc-root register -- ) swap gc-root@ MOV ;
822 M: x86 %alien-global ( dst symbol library -- )
823 [ 0 MOV ] 2dip rc-absolute-cell rel-dlsym ;
825 M: x86 %epilogue ( n -- ) cell - incr-stack-reg ;
827 :: %boolean ( dst temp word -- )
828 dst \ f tag-number MOV
829 temp 0 MOV \ t rc-absolute-cell rel-immediate
830 dst temp word execute ; inline
832 M:: x86 %compare ( dst src1 src2 cc temp -- )
835 { cc< [ dst temp \ CMOVL %boolean ] }
836 { cc<= [ dst temp \ CMOVLE %boolean ] }
837 { cc> [ dst temp \ CMOVG %boolean ] }
838 { cc>= [ dst temp \ CMOVGE %boolean ] }
839 { cc= [ dst temp \ CMOVE %boolean ] }
840 { cc/= [ dst temp \ CMOVNE %boolean ] }
843 M: x86 %compare-imm ( dst src1 src2 cc temp -- )
846 : %cmov-float= ( dst src -- )
848 "no-move" define-label
850 "no-move" get [ JNE ] [ JP ] bi
852 "no-move" resolve-label
855 : %cmov-float/= ( dst src -- )
857 "no-move" define-label
864 "no-move" resolve-label
867 :: (%compare-float) ( dst src1 src2 cc temp compare -- )
869 { cc< [ src2 src1 \ compare execute( a b -- ) dst temp \ CMOVA %boolean ] }
870 { cc<= [ src2 src1 \ compare execute( a b -- ) dst temp \ CMOVAE %boolean ] }
871 { cc> [ src1 src2 \ compare execute( a b -- ) dst temp \ CMOVA %boolean ] }
872 { cc>= [ src1 src2 \ compare execute( a b -- ) dst temp \ CMOVAE %boolean ] }
873 { cc= [ src1 src2 \ compare execute( a b -- ) dst temp \ %cmov-float= %boolean ] }
874 { cc<> [ src1 src2 \ compare execute( a b -- ) dst temp \ CMOVNE %boolean ] }
875 { cc<>= [ src1 src2 \ compare execute( a b -- ) dst temp \ CMOVNP %boolean ] }
876 { cc/< [ src2 src1 \ compare execute( a b -- ) dst temp \ CMOVBE %boolean ] }
877 { cc/<= [ src2 src1 \ compare execute( a b -- ) dst temp \ CMOVB %boolean ] }
878 { cc/> [ src1 src2 \ compare execute( a b -- ) dst temp \ CMOVBE %boolean ] }
879 { cc/>= [ src1 src2 \ compare execute( a b -- ) dst temp \ CMOVB %boolean ] }
880 { cc/= [ src1 src2 \ compare execute( a b -- ) dst temp \ %cmov-float/= %boolean ] }
881 { cc/<> [ src1 src2 \ compare execute( a b -- ) dst temp \ CMOVE %boolean ] }
882 { cc/<>= [ src1 src2 \ compare execute( a b -- ) dst temp \ CMOVP %boolean ] }
885 M: x86 %compare-float-ordered ( dst src1 src2 cc temp -- )
886 \ COMISD (%compare-float) ;
888 M: x86 %compare-float-unordered ( dst src1 src2 cc temp -- )
889 \ UCOMISD (%compare-float) ;
891 M:: x86 %compare-branch ( label src1 src2 cc -- )
895 { cc<= [ label JLE ] }
897 { cc>= [ label JGE ] }
899 { cc/= [ label JNE ] }
902 M: x86 %compare-imm-branch ( label src1 src2 cc -- )
905 : %jump-float= ( label -- )
907 "no-jump" define-label
910 "no-jump" resolve-label
913 : %jump-float/= ( label -- )
916 :: (%compare-float-branch) ( label src1 src2 cc compare -- )
918 { cc< [ src2 src1 \ compare execute( a b -- ) label JA ] }
919 { cc<= [ src2 src1 \ compare execute( a b -- ) label JAE ] }
920 { cc> [ src1 src2 \ compare execute( a b -- ) label JA ] }
921 { cc>= [ src1 src2 \ compare execute( a b -- ) label JAE ] }
922 { cc= [ src1 src2 \ compare execute( a b -- ) label %jump-float= ] }
923 { cc<> [ src1 src2 \ compare execute( a b -- ) label JNE ] }
924 { cc<>= [ src1 src2 \ compare execute( a b -- ) label JNP ] }
925 { cc/< [ src2 src1 \ compare execute( a b -- ) label JBE ] }
926 { cc/<= [ src2 src1 \ compare execute( a b -- ) label JB ] }
927 { cc/> [ src1 src2 \ compare execute( a b -- ) label JBE ] }
928 { cc/>= [ src1 src2 \ compare execute( a b -- ) label JB ] }
929 { cc/= [ src1 src2 \ compare execute( a b -- ) label %jump-float/= ] }
930 { cc/<> [ src1 src2 \ compare execute( a b -- ) label JE ] }
931 { cc/<>= [ src1 src2 \ compare execute( a b -- ) label JP ] }
934 M: x86 %compare-float-ordered-branch ( label src1 src2 cc -- )
935 \ COMISD (%compare-float-branch) ;
937 M: x86 %compare-float-unordered-branch ( label src1 src2 cc -- )
938 \ UCOMISD (%compare-float-branch) ;
940 M:: x86 %spill ( src rep n -- )
941 n spill@ src rep %copy ;
943 M:: x86 %reload ( dst rep n -- )
944 dst n spill@ 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
996 [ { sse_version } compile ] with-optimizer
997 "Checking for multimedia extensions: " write sse-version
998 [ sse-string write " detected" print ] [ enable-sse2 ] bi ;