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 vector-regs float-regs ;
25 HOOK: stack-reg cpu ( -- reg )
27 HOOK: reserved-area-size cpu ( -- n )
29 : stack@ ( n -- op ) stack-reg swap [+] ;
31 : param@ ( n -- op ) reserved-area-size + stack@ ;
33 : spill@ ( n -- op ) spill-offset param@ ;
35 : gc-root@ ( n -- op ) gc-root-offset param@ ;
37 : decr-stack-reg ( n -- )
38 dup 0 = [ drop ] [ stack-reg swap SUB ] if ;
40 : incr-stack-reg ( n -- )
41 dup 0 = [ drop ] [ stack-reg swap ADD ] if ;
43 : align-stack ( n -- n' )
44 os macosx? cpu x86.64? or [ 16 align ] when ;
46 M: x86 stack-frame-size ( stack-frame -- i )
47 (stack-frame-size) 3 cells reserved-area-size + + align-stack ;
49 ! Must be a volatile register not used for parameter passing, for safe
50 ! use in calls in and out of C
51 HOOK: temp-reg cpu ( -- reg )
53 HOOK: pic-tail-reg cpu ( -- reg )
55 M: x86 %load-immediate dup 0 = [ drop dup XOR ] [ MOV ] if ;
57 M: x86 %load-reference swap 0 MOV rc-absolute-cell rel-immediate ;
59 HOOK: ds-reg cpu ( -- reg )
60 HOOK: rs-reg cpu ( -- reg )
62 : reg-stack ( n reg -- op ) swap cells neg [+] ;
64 GENERIC: loc>operand ( loc -- operand )
66 M: ds-loc loc>operand n>> ds-reg reg-stack ;
67 M: rs-loc loc>operand n>> rs-reg reg-stack ;
69 M: x86 %peek loc>operand MOV ;
70 M: x86 %replace loc>operand swap MOV ;
71 : (%inc) ( n reg -- ) swap cells dup 0 > [ ADD ] [ neg SUB ] if ; inline
72 M: x86 %inc-d ( n -- ) ds-reg (%inc) ;
73 M: x86 %inc-r ( n -- ) rs-reg (%inc) ;
75 M: x86 %call ( word -- ) 0 CALL rc-relative rel-word-pic ;
77 : xt-tail-pic-offset ( -- n )
78 #! See the comment in vm/cpu-x86.hpp
81 M: x86 %jump ( word -- )
82 pic-tail-reg 0 MOV xt-tail-pic-offset rc-absolute-cell rel-here
83 0 JMP rc-relative rel-word-pic-tail ;
85 M: x86 %jump-label ( label -- ) 0 JMP rc-relative label-fixup ;
87 M: x86 %return ( -- ) 0 RET ;
89 : code-alignment ( align -- n )
90 [ building get length dup ] dip align swap - ;
95 :: (%slot-imm) ( obj slot tag -- op )
96 obj slot cells tag - [+] ; inline
98 M: x86 %slot ( dst obj slot -- ) [+] MOV ;
99 M: x86 %slot-imm ( dst obj slot tag -- ) (%slot-imm) MOV ;
100 M: x86 %set-slot ( src obj slot -- ) [+] swap MOV ;
101 M: x86 %set-slot-imm ( src obj slot tag -- ) (%slot-imm) swap MOV ;
103 :: two-operand ( dst src1 src2 rep -- dst src )
104 dst src2 eq? dst src1 eq? not and [ "Cannot handle this case" throw ] when
108 :: one-operand ( dst src rep -- dst )
112 M: x86 %add 2over eq? [ nip ADD ] [ [+] LEA ] if ;
113 M: x86 %add-imm 2over eq? [ nip ADD ] [ [+] LEA ] if ;
114 M: x86 %sub int-rep two-operand SUB ;
115 M: x86 %sub-imm 2over eq? [ nip SUB ] [ neg [+] LEA ] if ;
116 M: x86 %mul int-rep two-operand swap IMUL2 ;
117 M: x86 %mul-imm IMUL3 ;
118 M: x86 %and int-rep two-operand AND ;
119 M: x86 %and-imm int-rep two-operand AND ;
120 M: x86 %or int-rep two-operand OR ;
121 M: x86 %or-imm int-rep two-operand OR ;
122 M: x86 %xor int-rep two-operand XOR ;
123 M: x86 %xor-imm int-rep two-operand XOR ;
124 M: x86 %shl-imm int-rep two-operand SHL ;
125 M: x86 %shr-imm int-rep two-operand SHR ;
126 M: x86 %sar-imm int-rep two-operand SAR ;
128 M: x86 %min int-rep two-operand [ CMP ] [ CMOVG ] 2bi ;
129 M: x86 %max int-rep two-operand [ CMP ] [ CMOVL ] 2bi ;
131 M: x86 %not int-rep one-operand NOT ;
134 GENERIC: copy-register* ( dst src rep -- )
136 M: int-rep copy-register* drop MOV ;
137 M: tagged-rep copy-register* drop MOV ;
138 M: float-rep copy-register* drop MOVSS ;
139 M: double-rep copy-register* drop MOVSD ;
140 M: float-4-rep copy-register* drop MOVUPS ;
141 M: double-2-rep copy-register* drop MOVUPD ;
142 M: vector-rep copy-register* drop MOVDQU ;
144 M: x86 %copy ( dst src rep -- )
145 2over eq? [ 3drop ] [
146 [ [ dup spill-slot? [ n>> spill@ ] when ] bi@ ] dip
150 M: x86 %fixnum-add ( label dst src1 src2 -- )
151 int-rep two-operand ADD JO ;
153 M: x86 %fixnum-sub ( label dst src1 src2 -- )
154 int-rep two-operand SUB JO ;
156 M: x86 %fixnum-mul ( label dst src1 src2 -- )
157 int-rep two-operand swap IMUL2 JO ;
159 M: x86 %unbox-alien ( dst src -- )
160 alien-offset [+] MOV ;
162 M:: x86 %unbox-any-c-ptr ( dst src temp -- )
164 { "is-byte-array" "end" "start" } [ define-label ] each
167 ! We come back here with displaced aliens
168 "start" resolve-label
170 temp \ f tag-number CMP
172 ! Is the object an alien?
173 temp header-offset [+] alien type-number tag-fixnum CMP
174 "is-byte-array" get JNE
175 ! If so, load the offset and add it to the address
176 dst temp alien-offset [+] ADD
177 ! Now recurse on the underlying alien
178 temp temp underlying-alien-offset [+] MOV
180 "is-byte-array" resolve-label
181 ! Add byte array address to address being computed
183 ! Add an offset to start of byte array's data
184 dst byte-array-offset ADD
188 : alien@ ( reg n -- op ) cells alien tag-number - [+] ;
190 :: %allot-alien ( dst displacement base temp -- )
191 dst 4 cells alien temp %allot
192 dst 1 alien@ base MOV ! alien
193 dst 2 alien@ \ f tag-number MOV ! expired
194 dst 3 alien@ displacement MOV ! displacement
197 M:: x86 %box-alien ( dst src temp -- )
200 dst \ f tag-number MOV
203 dst src \ f tag-number temp %allot-alien
207 M:: x86 %box-displaced-alien ( dst displacement base displacement' base' base-class -- )
211 ! If displacement is zero, return the base
215 ! Quickly use displacement' before its needed for real, as allot temporary
216 dst 4 cells alien displacement' %allot
217 ! If base is already a displaced alien, unpack it
219 displacement' displacement MOV
220 base \ f tag-number CMP
222 base header-offset [+] alien type-number tag-fixnum CMP
224 ! displacement += base.displacement
225 displacement' base 3 alien@ ADD
227 base' base 1 alien@ MOV
229 dst 1 alien@ base' MOV ! alien
230 dst 2 alien@ \ f tag-number MOV ! expired
231 dst 3 alien@ displacement' MOV ! displacement
235 ! The 'small-reg' mess is pretty crappy, but its only used on x86-32.
236 ! On x86-64, all registers have 8-bit versions. However, a similar
237 ! problem arises for shifts, where the shift count must be in CL, and
238 ! so one day I will fix this properly by adding precoloring to the
239 ! register allocator.
241 HOOK: has-small-reg? cpu ( reg size -- ? )
243 CONSTANT: have-byte-regs { EAX ECX EDX EBX }
245 M: x86.32 has-small-reg?
247 { 8 [ have-byte-regs memq? ] }
252 M: x86.64 has-small-reg? 2drop t ;
254 : small-reg-that-isn't ( exclude -- reg' )
255 [ have-byte-regs ] dip
256 [ native-version-of ] map
257 '[ _ memq? not ] find nip ;
259 : with-save/restore ( reg quot -- )
260 [ drop PUSH ] [ call ] [ drop POP ] 2tri ; inline
262 :: with-small-register ( dst exclude size quot: ( new-dst -- ) -- )
263 ! If the destination register overlaps a small register with
264 ! 'size' bits, we call the quot with that. Otherwise, we find a
265 ! small register that is not in exclude, and call quot, saving and
266 ! restoring the small register.
267 dst size has-small-reg? [ dst quot call ] [
268 exclude small-reg-that-isn't
269 [ quot call ] with-save/restore
272 M:: x86 %string-nth ( dst src index temp -- )
273 ! We request a small-reg of size 8 since those of size 16 are
276 dst { src index temp } 8 [| new-dst |
277 ! Load the least significant 7 bits into new-dst.
278 ! 8th bit indicates whether we have to load from
279 ! the aux vector or not.
280 temp src index [+] LEA
281 new-dst 8-bit-version-of temp string-offset [+] MOV
282 new-dst new-dst 8-bit-version-of MOVZX
283 ! Do we have to look at the aux vector?
286 ! Yes, this is a non-ASCII character. Load aux vector
287 temp src string-aux-offset [+] MOV
293 new-dst 16-bit-version-of new-dst byte-array-offset [+] MOV
294 new-dst new-dst 16-bit-version-of MOVZX
299 dst new-dst int-rep %copy
300 ] with-small-register ;
302 M:: x86 %set-string-nth-fast ( ch str index temp -- )
303 ch { index str temp } 8 [| new-ch |
304 new-ch ch int-rep %copy
305 temp str index [+] LEA
306 temp string-offset [+] new-ch 8-bit-version-of MOV
307 ] with-small-register ;
309 :: %alien-integer-getter ( dst src size quot -- )
310 dst { src } size [| new-dst |
311 new-dst dup size n-bit-version-of dup src [] MOV
313 dst new-dst int-rep %copy
314 ] with-small-register ; inline
316 : %alien-unsigned-getter ( dst src size -- )
317 [ MOVZX ] %alien-integer-getter ; inline
319 M: x86 %alien-unsigned-1 8 %alien-unsigned-getter ;
320 M: x86 %alien-unsigned-2 16 %alien-unsigned-getter ;
321 M: x86 %alien-unsigned-4 32 [ 2drop ] %alien-integer-getter ;
323 : %alien-signed-getter ( dst src size -- )
324 [ MOVSX ] %alien-integer-getter ; inline
326 M: x86 %alien-signed-1 8 %alien-signed-getter ;
327 M: x86 %alien-signed-2 16 %alien-signed-getter ;
328 M: x86 %alien-signed-4 32 %alien-signed-getter ;
330 M: x86 %alien-cell [] MOV ;
331 M: x86 %alien-float [] MOVSS ;
332 M: x86 %alien-double [] MOVSD ;
333 M: x86 %alien-vector [ [] ] dip %copy ;
335 :: %alien-integer-setter ( ptr value size -- )
336 value { ptr } size [| new-value |
337 new-value value int-rep %copy
338 ptr [] new-value size n-bit-version-of MOV
339 ] with-small-register ; inline
341 M: x86 %set-alien-integer-1 8 %alien-integer-setter ;
342 M: x86 %set-alien-integer-2 16 %alien-integer-setter ;
343 M: x86 %set-alien-integer-4 32 %alien-integer-setter ;
344 M: x86 %set-alien-cell [ [] ] dip MOV ;
345 M: x86 %set-alien-float [ [] ] dip MOVSS ;
346 M: x86 %set-alien-double [ [] ] dip MOVSD ;
347 M: x86 %set-alien-vector [ [] ] 2dip %copy ;
349 : shift-count? ( reg -- ? ) { ECX RCX } memq? ;
351 :: emit-shift ( dst src quot -- )
360 ECX native-version-of [
362 drop dst CL quot call
367 M: x86 %shl int-rep two-operand [ SHL ] emit-shift ;
368 M: x86 %shr int-rep two-operand [ SHR ] emit-shift ;
369 M: x86 %sar int-rep two-operand [ SAR ] emit-shift ;
371 M: x86 %vm-field-ptr ( dst field -- )
372 [ drop 0 MOV rc-absolute-cell rt-vm rel-fixup ]
373 [ vm-field-offset ADD ] 2bi ;
375 : load-zone-ptr ( reg -- )
376 #! Load pointer to start of zone array
377 "nursery" %vm-field-ptr ;
379 : load-allot-ptr ( nursery-ptr allot-ptr -- )
380 [ drop load-zone-ptr ] [ swap cell [+] MOV ] 2bi ;
382 : inc-allot-ptr ( nursery-ptr n -- )
383 [ cell [+] ] dip 8 align ADD ;
385 : store-header ( temp class -- )
386 [ [] ] [ type-number tag-fixnum ] bi* MOV ;
388 : store-tagged ( dst tag -- )
391 M:: x86 %allot ( dst size class nursery-ptr -- )
392 nursery-ptr dst load-allot-ptr
393 dst class store-header
394 dst class store-tagged
395 nursery-ptr size inc-allot-ptr ;
397 M:: x86 %write-barrier ( src card# table -- )
398 #! Mark the card pointed to by vreg.
402 table "cards_offset" %vm-field-ptr
404 table card# [+] card-mark <byte> MOV
407 card# deck-bits card-bits - SHR
408 table "decks_offset" %vm-field-ptr
410 table card# [+] card-mark <byte> MOV ;
412 M:: x86 %check-nursery ( label temp1 temp2 -- )
414 temp2 temp1 cell [+] MOV
416 temp1 temp1 3 cells [+] MOV
420 M: x86 %save-gc-root ( gc-root register -- ) [ gc-root@ ] dip MOV ;
422 M: x86 %load-gc-root ( gc-root register -- ) swap gc-root@ MOV ;
424 M: x86 %alien-global ( dst symbol library -- )
425 [ 0 MOV ] 2dip rc-absolute-cell rel-dlsym ;
427 M: x86 %epilogue ( n -- ) cell - incr-stack-reg ;
429 :: %boolean ( dst temp word -- )
430 dst \ f tag-number MOV
431 temp 0 MOV \ t rc-absolute-cell rel-immediate
432 dst temp word execute ; inline
434 M:: x86 %compare ( dst src1 src2 cc temp -- )
437 { cc< [ dst temp \ CMOVL %boolean ] }
438 { cc<= [ dst temp \ CMOVLE %boolean ] }
439 { cc> [ dst temp \ CMOVG %boolean ] }
440 { cc>= [ dst temp \ CMOVGE %boolean ] }
441 { cc= [ dst temp \ CMOVE %boolean ] }
442 { cc/= [ dst temp \ CMOVNE %boolean ] }
445 M: x86 %compare-imm ( dst src1 src2 cc temp -- )
448 M:: x86 %compare-branch ( label src1 src2 cc -- )
452 { cc<= [ label JLE ] }
454 { cc>= [ label JGE ] }
456 { cc/= [ label JNE ] }
459 M: x86 %compare-imm-branch ( label src1 src2 cc -- )
462 M: x86 %add-float double-rep two-operand ADDSD ;
463 M: x86 %sub-float double-rep two-operand SUBSD ;
464 M: x86 %mul-float double-rep two-operand MULSD ;
465 M: x86 %div-float double-rep two-operand DIVSD ;
466 M: x86 %min-float double-rep two-operand MINSD ;
467 M: x86 %max-float double-rep two-operand MAXSD ;
468 M: x86 %sqrt SQRTSD ;
470 M: x86 %single>double-float CVTSS2SD ;
471 M: x86 %double>single-float CVTSD2SS ;
473 M: x86 %integer>float CVTSI2SD ;
474 M: x86 %float>integer CVTTSD2SI ;
476 M: x86 %unbox-float ( dst src -- )
477 float-offset [+] MOVSD ;
479 M:: x86 %box-float ( dst src temp -- )
480 dst 16 float temp %allot
481 dst float-offset [+] src MOVSD ;
483 : %cmov-float= ( dst src -- )
485 "no-move" define-label
487 "no-move" get [ JNE ] [ JP ] bi
489 "no-move" resolve-label
492 : %cmov-float/= ( dst src -- )
494 "no-move" define-label
501 "no-move" resolve-label
504 :: (%compare-float) ( dst src1 src2 cc temp compare -- )
506 { cc< [ src2 src1 \ compare execute( a b -- ) dst temp \ CMOVA %boolean ] }
507 { cc<= [ src2 src1 \ compare execute( a b -- ) dst temp \ CMOVAE %boolean ] }
508 { cc> [ src1 src2 \ compare execute( a b -- ) dst temp \ CMOVA %boolean ] }
509 { cc>= [ src1 src2 \ compare execute( a b -- ) dst temp \ CMOVAE %boolean ] }
510 { cc= [ src1 src2 \ compare execute( a b -- ) dst temp \ %cmov-float= %boolean ] }
511 { cc<> [ src1 src2 \ compare execute( a b -- ) dst temp \ CMOVNE %boolean ] }
512 { cc<>= [ src1 src2 \ compare execute( a b -- ) dst temp \ CMOVNP %boolean ] }
513 { cc/< [ src2 src1 \ compare execute( a b -- ) dst temp \ CMOVBE %boolean ] }
514 { cc/<= [ src2 src1 \ compare execute( a b -- ) dst temp \ CMOVB %boolean ] }
515 { cc/> [ src1 src2 \ compare execute( a b -- ) dst temp \ CMOVBE %boolean ] }
516 { cc/>= [ src1 src2 \ compare execute( a b -- ) dst temp \ CMOVB %boolean ] }
517 { cc/= [ src1 src2 \ compare execute( a b -- ) dst temp \ %cmov-float/= %boolean ] }
518 { cc/<> [ src1 src2 \ compare execute( a b -- ) dst temp \ CMOVE %boolean ] }
519 { cc/<>= [ src1 src2 \ compare execute( a b -- ) dst temp \ CMOVP %boolean ] }
522 M: x86 %compare-float-ordered ( dst src1 src2 cc temp -- )
523 \ COMISD (%compare-float) ;
525 M: x86 %compare-float-unordered ( dst src1 src2 cc temp -- )
526 \ UCOMISD (%compare-float) ;
528 : %jump-float= ( label -- )
530 "no-jump" define-label
533 "no-jump" resolve-label
536 : %jump-float/= ( label -- )
539 :: (%compare-float-branch) ( label src1 src2 cc compare -- )
541 { cc< [ src2 src1 \ compare execute( a b -- ) label JA ] }
542 { cc<= [ src2 src1 \ compare execute( a b -- ) label JAE ] }
543 { cc> [ src1 src2 \ compare execute( a b -- ) label JA ] }
544 { cc>= [ src1 src2 \ compare execute( a b -- ) label JAE ] }
545 { cc= [ src1 src2 \ compare execute( a b -- ) label %jump-float= ] }
546 { cc<> [ src1 src2 \ compare execute( a b -- ) label JNE ] }
547 { cc<>= [ src1 src2 \ compare execute( a b -- ) label JNP ] }
548 { cc/< [ src2 src1 \ compare execute( a b -- ) label JBE ] }
549 { cc/<= [ src2 src1 \ compare execute( a b -- ) label JB ] }
550 { cc/> [ src1 src2 \ compare execute( a b -- ) label JBE ] }
551 { cc/>= [ src1 src2 \ compare execute( a b -- ) label JB ] }
552 { cc/= [ src1 src2 \ compare execute( a b -- ) label %jump-float/= ] }
553 { cc/<> [ src1 src2 \ compare execute( a b -- ) label JE ] }
554 { cc/<>= [ src1 src2 \ compare execute( a b -- ) label JP ] }
557 M: x86 %compare-float-ordered-branch ( label src1 src2 cc -- )
558 \ COMISD (%compare-float-branch) ;
560 M: x86 %compare-float-unordered-branch ( label src1 src2 cc -- )
561 \ UCOMISD (%compare-float-branch) ;
563 M:: x86 %box-vector ( dst src rep temp -- )
564 dst rep rep-size 2 cells + byte-array temp %allot
565 16 tag-fixnum dst 1 byte-array tag-number %set-slot-imm
566 dst byte-array-offset [+]
569 M:: x86 %unbox-vector ( dst src rep -- )
570 dst src byte-array-offset [+]
573 MACRO: available-reps ( alist -- )
574 ! Each SSE version adds new representations and supports
576 unzip { } [ append ] accumulate rest swap suffix
577 [ [ 1quotation ] map ] bi@ zip
578 reverse [ { } ] suffix
581 M: x86 %broadcast-vector ( dst src rep -- )
583 { float-4-rep [ [ float-4-rep %copy ] [ drop dup 0 SHUFPS ] 2bi ] }
584 { double-2-rep [ [ double-2-rep %copy ] [ drop dup UNPCKLPD ] 2bi ] }
587 M: x86 %broadcast-vector-reps
589 ! Can't do this with sse1 since it will want to unbox
590 ! a double-precision float and convert to single precision
591 { sse2? { float-4-rep double-2-rep } }
594 M:: x86 %gather-vector-4 ( dst src1 src2 src3 src4 rep -- )
597 [ rep float-4-rep eq? ]
599 dst src1 float-4-rep %copy
606 [ rep { int-4-rep uint-4-rep } memq? ]
608 dst src1 int-4-rep %copy
616 M: x86 %gather-vector-4-reps
618 ! Can't do this with sse1 since it will want to unbox
619 ! double-precision floats and convert to single precision
620 { sse2? { float-4-rep int-4-rep uint-4-rep } }
623 M:: x86 %gather-vector-2 ( dst src1 src2 rep -- )
628 dst src1 double-2-rep %copy
634 M: x86 %gather-vector-2-reps
636 { sse2? { double-2-rep } }
639 M: x86 %add-vector ( dst src1 src2 rep -- )
642 { float-4-rep [ ADDPS ] }
643 { double-2-rep [ ADDPD ] }
644 { char-16-rep [ PADDB ] }
645 { uchar-16-rep [ PADDB ] }
646 { short-8-rep [ PADDW ] }
647 { ushort-8-rep [ PADDW ] }
648 { int-4-rep [ PADDD ] }
649 { uint-4-rep [ PADDD ] }
650 { longlong-2-rep [ PADDQ ] }
651 { ulonglong-2-rep [ PADDQ ] }
654 M: x86 %add-vector-reps
656 { sse? { float-4-rep } }
657 { 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 } }
660 M: x86 %saturated-add-vector ( dst src1 src2 rep -- )
663 { char-16-rep [ PADDSB ] }
664 { uchar-16-rep [ PADDUSB ] }
665 { short-8-rep [ PADDSW ] }
666 { ushort-8-rep [ PADDUSW ] }
669 M: x86 %saturated-add-vector-reps
671 { sse2? { char-16-rep uchar-16-rep short-8-rep ushort-8-rep } }
674 M: x86 %add-sub-vector ( dst src1 src2 rep -- )
677 { float-4-rep [ ADDSUBPS ] }
678 { double-2-rep [ ADDSUBPD ] }
681 M: x86 %add-sub-vector-reps
683 { sse3? { float-4-rep double-2-rep } }
686 M: x86 %sub-vector ( dst src1 src2 rep -- )
689 { float-4-rep [ SUBPS ] }
690 { double-2-rep [ SUBPD ] }
691 { char-16-rep [ PSUBB ] }
692 { uchar-16-rep [ PSUBB ] }
693 { short-8-rep [ PSUBW ] }
694 { ushort-8-rep [ PSUBW ] }
695 { int-4-rep [ PSUBD ] }
696 { uint-4-rep [ PSUBD ] }
697 { longlong-2-rep [ PSUBQ ] }
698 { ulonglong-2-rep [ PSUBQ ] }
701 M: x86 %sub-vector-reps
703 { sse? { float-4-rep } }
704 { 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 } }
707 M: x86 %saturated-sub-vector ( dst src1 src2 rep -- )
710 { char-16-rep [ PSUBSB ] }
711 { uchar-16-rep [ PSUBUSB ] }
712 { short-8-rep [ PSUBSW ] }
713 { ushort-8-rep [ PSUBUSW ] }
716 M: x86 %saturated-sub-vector-reps
718 { sse2? { char-16-rep uchar-16-rep short-8-rep ushort-8-rep } }
721 M: x86 %mul-vector ( dst src1 src2 rep -- )
724 { float-4-rep [ MULPS ] }
725 { double-2-rep [ MULPD ] }
726 { short-8-rep [ PMULLW ] }
727 { ushort-8-rep [ PMULLW ] }
728 { int-4-rep [ PMULLD ] }
729 { uint-4-rep [ PMULLD ] }
732 M: x86 %mul-vector-reps
734 { sse? { float-4-rep } }
735 { sse2? { double-2-rep short-8-rep ushort-8-rep } }
736 { sse4.1? { int-4-rep uint-4-rep } }
739 M: x86 %saturated-mul-vector-reps
740 ! No multiplication with saturation on x86
743 M: x86 %div-vector ( dst src1 src2 rep -- )
746 { float-4-rep [ DIVPS ] }
747 { double-2-rep [ DIVPD ] }
750 M: x86 %div-vector-reps
752 { sse? { float-4-rep } }
753 { sse2? { double-2-rep } }
756 M: x86 %min-vector ( dst src1 src2 rep -- )
759 { char-16-rep [ PMINSB ] }
760 { uchar-16-rep [ PMINUB ] }
761 { short-8-rep [ PMINSW ] }
762 { ushort-8-rep [ PMINUW ] }
763 { int-4-rep [ PMINSD ] }
764 { uint-4-rep [ PMINUD ] }
765 { float-4-rep [ MINPS ] }
766 { double-2-rep [ MINPD ] }
769 M: x86 %min-vector-reps
771 { sse? { float-4-rep } }
772 { sse2? { uchar-16-rep short-8-rep double-2-rep short-8-rep uchar-16-rep } }
773 { sse4.1? { char-16-rep ushort-8-rep int-4-rep uint-4-rep } }
776 M: x86 %max-vector ( dst src1 src2 rep -- )
779 { char-16-rep [ PMAXSB ] }
780 { uchar-16-rep [ PMAXUB ] }
781 { short-8-rep [ PMAXSW ] }
782 { ushort-8-rep [ PMAXUW ] }
783 { int-4-rep [ PMAXSD ] }
784 { uint-4-rep [ PMAXUD ] }
785 { float-4-rep [ MAXPS ] }
786 { double-2-rep [ MAXPD ] }
789 M: x86 %max-vector-reps
791 { sse? { float-4-rep } }
792 { sse2? { uchar-16-rep short-8-rep double-2-rep short-8-rep uchar-16-rep } }
793 { sse4.1? { char-16-rep ushort-8-rep int-4-rep uint-4-rep } }
796 M: x86 %horizontal-add-vector ( dst src rep -- )
798 { float-4-rep [ [ float-4-rep %copy ] [ HADDPS ] [ HADDPS ] 2tri ] }
799 { double-2-rep [ [ double-2-rep %copy ] [ HADDPD ] 2bi ] }
802 M: x86 %horizontal-add-vector-reps
804 { sse3? { float-4-rep double-2-rep } }
807 M: x86 %horizontal-shl-vector ( dst src1 src2 rep -- )
810 M: x86 %horizontal-shl-vector-reps
812 { sse2? { char-16-rep uchar-16-rep short-8-rep ushort-8-rep int-4-rep uint-4-rep longlong-2-rep ulonglong-2-rep } }
815 M: x86 %horizontal-shr-vector ( dst src1 src2 rep -- )
818 M: x86 %horizontal-shr-vector-reps
820 { sse2? { char-16-rep uchar-16-rep short-8-rep ushort-8-rep int-4-rep uint-4-rep longlong-2-rep ulonglong-2-rep } }
823 M: x86 %abs-vector ( dst src rep -- )
825 { char-16-rep [ PABSB ] }
826 { short-8-rep [ PABSW ] }
827 { int-4-rep [ PABSD ] }
830 M: x86 %abs-vector-reps
832 { ssse3? { char-16-rep short-8-rep int-4-rep } }
835 M: x86 %sqrt-vector ( dst src rep -- )
837 { float-4-rep [ SQRTPS ] }
838 { double-2-rep [ SQRTPD ] }
841 M: x86 %sqrt-vector-reps
843 { sse? { float-4-rep } }
844 { sse2? { double-2-rep } }
847 M: x86 %and-vector ( dst src1 src2 rep -- )
850 { float-4-rep [ ANDPS ] }
851 { double-2-rep [ ANDPD ] }
855 M: x86 %and-vector-reps
857 { sse? { float-4-rep } }
858 { 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 } }
861 M: x86 %andn-vector ( dst src1 src2 rep -- )
864 { float-4-rep [ ANDNPS ] }
865 { double-2-rep [ ANDNPD ] }
869 M: x86 %andn-vector-reps
871 { sse? { float-4-rep } }
872 { 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 } }
875 M: x86 %or-vector ( dst src1 src2 rep -- )
878 { float-4-rep [ ORPS ] }
879 { double-2-rep [ ORPD ] }
883 M: x86 %or-vector-reps
885 { sse? { float-4-rep } }
886 { 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 } }
889 M: x86 %xor-vector ( dst src1 src2 rep -- )
892 { float-4-rep [ XORPS ] }
893 { double-2-rep [ XORPD ] }
897 M: x86 %xor-vector-reps
899 { sse? { float-4-rep } }
900 { 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 } }
903 M: x86 %shl-vector ( dst src1 src2 rep -- )
906 { short-8-rep [ PSLLW ] }
907 { ushort-8-rep [ PSLLW ] }
908 { int-4-rep [ PSLLD ] }
909 { uint-4-rep [ PSLLD ] }
910 { longlong-2-rep [ PSLLQ ] }
911 { ulonglong-2-rep [ PSLLQ ] }
914 M: x86 %shl-vector-reps
916 { sse2? { short-8-rep ushort-8-rep int-4-rep uint-4-rep longlong-2-rep ulonglong-2-rep } }
919 M: x86 %shr-vector ( dst src1 src2 rep -- )
922 { short-8-rep [ PSRAW ] }
923 { ushort-8-rep [ PSRLW ] }
924 { int-4-rep [ PSRAD ] }
925 { uint-4-rep [ PSRLD ] }
926 { ulonglong-2-rep [ PSRLQ ] }
929 M: x86 %shr-vector-reps
931 { sse2? { short-8-rep ushort-8-rep int-4-rep uint-4-rep ulonglong-2-rep } }
934 M: x86 %integer>scalar drop MOVD ;
936 M: x86 %scalar>integer drop MOVD ;
938 M:: x86 %spill ( src rep dst -- ) dst src rep %copy ;
939 M:: x86 %reload ( dst rep src -- ) dst src rep %copy ;
941 M: x86 %loop-entry 16 code-alignment [ NOP ] times ;
943 M:: x86 %save-context ( temp1 temp2 callback-allowed? -- )
944 #! Save Factor stack pointers in case the C code calls a
945 #! callback which does a GC, which must reliably trace
947 temp1 0 MOV rc-absolute-cell rt-vm rel-fixup
948 temp1 temp1 "stack_chain" vm-field-offset [+] MOV
949 temp2 stack-reg cell neg [+] LEA
952 temp1 2 cells [+] ds-reg MOV
953 temp1 3 cells [+] rs-reg MOV
956 M: x86 value-struct? drop t ;
958 M: x86 small-enough? ( n -- ? )
959 HEX: -80000000 HEX: 7fffffff between? ;
961 : next-stack@ ( n -- operand )
962 #! nth parameter from the next stack frame. Used to box
963 #! input values to callbacks; the callback has its own
964 #! stack frame set up, and we want to read the frame
965 #! set up by the caller.
966 stack-frame get total-size>> + stack@ ;
972 :: install-sse2-check ( -- )
975 "This image was built to use SSE2 but your CPU does not support it." print
976 "You will need to bootstrap Factor again." print
980 ] "cpu.x86" add-init-hook ;
982 : enable-sse2 ( version -- )
984 enable-float-intrinsics
985 enable-float-functions
992 [ { sse_version } compile ] with-optimizer
993 "Checking for multimedia extensions: " write sse-version
994 [ sse-string write " detected" print ] [ enable-sse2 ] bi ;