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? [ "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 ] [ copy-register* ] if ;
147 M: x86 %fixnum-add ( label dst src1 src2 -- )
148 int-rep two-operand ADD JO ;
150 M: x86 %fixnum-sub ( label dst src1 src2 -- )
151 int-rep two-operand SUB JO ;
153 M: x86 %fixnum-mul ( label dst src1 src2 -- )
154 int-rep two-operand swap IMUL2 JO ;
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 double-rep two-operand ADDSD ;
217 M: x86 %sub-float double-rep two-operand SUBSD ;
218 M: x86 %mul-float double-rep two-operand MULSD ;
219 M: x86 %div-float double-rep two-operand DIVSD ;
220 M: x86 %min-float double-rep two-operand MINSD ;
221 M: x86 %max-float double-rep two-operand 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 -- )
307 { float-4-rep [ ADDPS ] }
308 { double-2-rep [ ADDPD ] }
309 { char-16-rep [ PADDB ] }
310 { uchar-16-rep [ PADDB ] }
311 { short-8-rep [ PADDW ] }
312 { ushort-8-rep [ PADDW ] }
313 { int-4-rep [ PADDD ] }
314 { uint-4-rep [ PADDD ] }
315 { longlong-2-rep [ PADDQ ] }
316 { ulonglong-2-rep [ PADDQ ] }
319 M: x86 %add-vector-reps
321 { sse? { float-4-rep } }
322 { 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 } }
325 M: x86 %saturated-add-vector ( dst src1 src2 rep -- )
328 { char-16-rep [ PADDSB ] }
329 { uchar-16-rep [ PADDUSB ] }
330 { short-8-rep [ PADDSW ] }
331 { ushort-8-rep [ PADDUSW ] }
334 M: x86 %saturated-add-vector-reps
336 { sse2? { char-16-rep uchar-16-rep short-8-rep ushort-8-rep } }
339 M: x86 %add-sub-vector ( dst src1 src2 rep -- )
342 { float-4-rep [ ADDSUBPS ] }
343 { double-2-rep [ ADDSUBPD ] }
346 M: x86 %add-sub-vector-reps
348 { sse3? { float-4-rep double-2-rep } }
351 M: x86 %sub-vector ( dst src1 src2 rep -- )
354 { float-4-rep [ SUBPS ] }
355 { double-2-rep [ SUBPD ] }
356 { char-16-rep [ PSUBB ] }
357 { uchar-16-rep [ PSUBB ] }
358 { short-8-rep [ PSUBW ] }
359 { ushort-8-rep [ PSUBW ] }
360 { int-4-rep [ PSUBD ] }
361 { uint-4-rep [ PSUBD ] }
362 { longlong-2-rep [ PSUBQ ] }
363 { ulonglong-2-rep [ PSUBQ ] }
366 M: x86 %sub-vector-reps
368 { sse? { float-4-rep } }
369 { 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 } }
372 M: x86 %saturated-sub-vector ( dst src1 src2 rep -- )
375 { char-16-rep [ PSUBSB ] }
376 { uchar-16-rep [ PSUBUSB ] }
377 { short-8-rep [ PSUBSW ] }
378 { ushort-8-rep [ PSUBUSW ] }
381 M: x86 %saturated-sub-vector-reps
383 { sse2? { char-16-rep uchar-16-rep short-8-rep ushort-8-rep } }
386 M: x86 %mul-vector ( dst src1 src2 rep -- )
389 { float-4-rep [ MULPS ] }
390 { double-2-rep [ MULPD ] }
391 { short-8-rep [ PMULLW ] }
392 { ushort-8-rep [ PMULLW ] }
393 { int-4-rep [ PMULLD ] }
394 { uint-4-rep [ PMULLD ] }
397 M: x86 %mul-vector-reps
399 { sse? { float-4-rep } }
400 { sse2? { double-2-rep short-8-rep ushort-8-rep } }
401 { sse4.1? { int-4-rep uint-4-rep } }
404 M: x86 %saturated-mul-vector-reps
405 ! No multiplication with saturation on x86
408 M: x86 %div-vector ( dst src1 src2 rep -- )
411 { float-4-rep [ DIVPS ] }
412 { double-2-rep [ DIVPD ] }
415 M: x86 %div-vector-reps
417 { sse? { float-4-rep } }
418 { sse2? { double-2-rep } }
421 M: x86 %min-vector ( dst src1 src2 rep -- )
424 { char-16-rep [ PMINSB ] }
425 { uchar-16-rep [ PMINUB ] }
426 { short-8-rep [ PMINSW ] }
427 { ushort-8-rep [ PMINUW ] }
428 { int-4-rep [ PMINSD ] }
429 { uint-4-rep [ PMINUD ] }
430 { float-4-rep [ MINPS ] }
431 { double-2-rep [ MINPD ] }
434 M: x86 %min-vector-reps
436 { sse? { float-4-rep } }
437 { sse2? { uchar-16-rep short-8-rep double-2-rep short-8-rep uchar-16-rep } }
438 { sse4.1? { char-16-rep ushort-8-rep int-4-rep uint-4-rep } }
441 M: x86 %max-vector ( dst src1 src2 rep -- )
444 { char-16-rep [ PMAXSB ] }
445 { uchar-16-rep [ PMAXUB ] }
446 { short-8-rep [ PMAXSW ] }
447 { ushort-8-rep [ PMAXUW ] }
448 { int-4-rep [ PMAXSD ] }
449 { uint-4-rep [ PMAXUD ] }
450 { float-4-rep [ MAXPS ] }
451 { double-2-rep [ MAXPD ] }
454 M: x86 %max-vector-reps
456 { sse? { float-4-rep } }
457 { sse2? { uchar-16-rep short-8-rep double-2-rep short-8-rep uchar-16-rep } }
458 { sse4.1? { char-16-rep ushort-8-rep int-4-rep uint-4-rep } }
461 M: x86 %horizontal-add-vector ( dst src rep -- )
463 { float-4-rep [ [ float-4-rep %copy ] [ HADDPS ] [ HADDPS ] 2tri ] }
464 { double-2-rep [ [ double-2-rep %copy ] [ HADDPD ] 2bi ] }
467 M: x86 %horizontal-add-vector-reps
469 { sse3? { float-4-rep double-2-rep } }
472 M: x86 %abs-vector ( dst src rep -- )
474 { char-16-rep [ PABSB ] }
475 { short-8-rep [ PABSW ] }
476 { int-4-rep [ PABSD ] }
479 M: x86 %abs-vector-reps
481 { ssse3? { char-16-rep short-8-rep int-4-rep } }
484 M: x86 %sqrt-vector ( dst src rep -- )
486 { float-4-rep [ SQRTPS ] }
487 { double-2-rep [ SQRTPD ] }
490 M: x86 %sqrt-vector-reps
492 { sse? { float-4-rep } }
493 { sse2? { double-2-rep } }
496 M: x86 %and-vector ( dst src1 src2 rep -- )
499 { float-4-rep [ ANDPS ] }
500 { double-2-rep [ ANDPD ] }
504 M: x86 %and-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 %or-vector ( dst src1 src2 rep -- )
513 { float-4-rep [ ORPS ] }
514 { double-2-rep [ ORPD ] }
518 M: x86 %or-vector-reps
520 { sse? { float-4-rep } }
521 { 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 } }
524 M: x86 %xor-vector ( dst src1 src2 rep -- )
527 { float-4-rep [ XORPS ] }
528 { double-2-rep [ XORPD ] }
532 M: x86 %xor-vector-reps
534 { sse? { float-4-rep } }
535 { 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 } }
538 M: x86 %shl-vector ( dst src1 src2 rep -- )
541 { short-8-rep [ PSLLW ] }
542 { ushort-8-rep [ PSLLW ] }
543 { int-4-rep [ PSLLD ] }
544 { uint-4-rep [ PSLLD ] }
545 { longlong-2-rep [ PSLLQ ] }
546 { ulonglong-2-rep [ PSLLQ ] }
549 M: x86 %shl-vector-reps
551 { sse2? { short-8-rep ushort-8-rep int-4-rep uint-4-rep longlong-2-rep ulonglong-2-rep } }
554 M: x86 %shr-vector ( dst src1 src2 rep -- )
557 { short-8-rep [ PSRAW ] }
558 { ushort-8-rep [ PSRLW ] }
559 { int-4-rep [ PSRAD ] }
560 { uint-4-rep [ PSRLD ] }
561 { ulonglong-2-rep [ PSRLQ ] }
564 M: x86 %shr-vector-reps
566 { sse2? { short-8-rep ushort-8-rep int-4-rep uint-4-rep ulonglong-2-rep } }
569 M: x86 %integer>scalar drop MOVD ;
571 M: x86 %scalar>integer drop MOVD ;
573 M: x86 %unbox-alien ( dst src -- )
574 alien-offset [+] MOV ;
576 M:: x86 %unbox-any-c-ptr ( dst src temp -- )
578 { "is-byte-array" "end" "start" } [ define-label ] each
581 ! We come back here with displaced aliens
582 "start" resolve-label
584 temp \ f tag-number CMP
586 ! Is the object an alien?
587 temp header-offset [+] alien type-number tag-fixnum CMP
588 "is-byte-array" get JNE
589 ! If so, load the offset and add it to the address
590 dst temp alien-offset [+] ADD
591 ! Now recurse on the underlying alien
592 temp temp underlying-alien-offset [+] MOV
594 "is-byte-array" resolve-label
595 ! Add byte array address to address being computed
597 ! Add an offset to start of byte array's data
598 dst byte-array-offset ADD
602 : alien@ ( reg n -- op ) cells alien tag-number - [+] ;
604 :: %allot-alien ( dst displacement base temp -- )
605 dst 4 cells alien temp %allot
606 dst 1 alien@ base MOV ! alien
607 dst 2 alien@ \ f tag-number MOV ! expired
608 dst 3 alien@ displacement MOV ! displacement
611 M:: x86 %box-alien ( dst src temp -- )
614 dst \ f tag-number MOV
617 dst src \ f tag-number temp %allot-alien
621 M:: x86 %box-displaced-alien ( dst displacement base displacement' base' base-class -- )
625 ! If displacement is zero, return the base
629 ! Quickly use displacement' before its needed for real, as allot temporary
630 dst 4 cells alien displacement' %allot
631 ! If base is already a displaced alien, unpack it
633 displacement' displacement MOV
634 base \ f tag-number CMP
636 base header-offset [+] alien type-number tag-fixnum CMP
638 ! displacement += base.displacement
639 displacement' base 3 alien@ ADD
641 base' base 1 alien@ MOV
643 dst 1 alien@ base' MOV ! alien
644 dst 2 alien@ \ f tag-number MOV ! expired
645 dst 3 alien@ displacement' MOV ! displacement
649 ! The 'small-reg' mess is pretty crappy, but its only used on x86-32.
650 ! On x86-64, all registers have 8-bit versions. However, a similar
651 ! problem arises for shifts, where the shift count must be in CL, and
652 ! so one day I will fix this properly by adding precoloring to the
653 ! register allocator.
655 HOOK: has-small-reg? cpu ( reg size -- ? )
657 CONSTANT: have-byte-regs { EAX ECX EDX EBX }
659 M: x86.32 has-small-reg?
661 { 8 [ have-byte-regs memq? ] }
666 M: x86.64 has-small-reg? 2drop t ;
668 : small-reg-that-isn't ( exclude -- reg' )
669 [ have-byte-regs ] dip
670 [ native-version-of ] map
671 '[ _ memq? not ] find nip ;
673 : with-save/restore ( reg quot -- )
674 [ drop PUSH ] [ call ] [ drop POP ] 2tri ; inline
676 :: with-small-register ( dst exclude size quot: ( new-dst -- ) -- )
677 ! If the destination register overlaps a small register with
678 ! 'size' bits, we call the quot with that. Otherwise, we find a
679 ! small register that is not in exclude, and call quot, saving and
680 ! restoring the small register.
681 dst size has-small-reg? [ dst quot call ] [
682 exclude small-reg-that-isn't
683 [ quot call ] with-save/restore
686 M:: x86 %string-nth ( dst src index temp -- )
687 ! We request a small-reg of size 8 since those of size 16 are
690 dst { src index temp } 8 [| new-dst |
691 ! Load the least significant 7 bits into new-dst.
692 ! 8th bit indicates whether we have to load from
693 ! the aux vector or not.
694 temp src index [+] LEA
695 new-dst 8-bit-version-of temp string-offset [+] MOV
696 new-dst new-dst 8-bit-version-of MOVZX
697 ! Do we have to look at the aux vector?
700 ! Yes, this is a non-ASCII character. Load aux vector
701 temp src string-aux-offset [+] MOV
707 new-dst 16-bit-version-of new-dst byte-array-offset [+] MOV
708 new-dst new-dst 16-bit-version-of MOVZX
713 dst new-dst int-rep %copy
714 ] with-small-register ;
716 M:: x86 %set-string-nth-fast ( ch str index temp -- )
717 ch { index str temp } 8 [| new-ch |
718 new-ch ch int-rep %copy
719 temp str index [+] LEA
720 temp string-offset [+] new-ch 8-bit-version-of MOV
721 ] with-small-register ;
723 :: %alien-integer-getter ( dst src size quot -- )
724 dst { src } size [| new-dst |
725 new-dst dup size n-bit-version-of dup src [] MOV
727 dst new-dst int-rep %copy
728 ] with-small-register ; inline
730 : %alien-unsigned-getter ( dst src size -- )
731 [ MOVZX ] %alien-integer-getter ; inline
733 M: x86 %alien-unsigned-1 8 %alien-unsigned-getter ;
734 M: x86 %alien-unsigned-2 16 %alien-unsigned-getter ;
735 M: x86 %alien-unsigned-4 32 [ 2drop ] %alien-integer-getter ;
737 : %alien-signed-getter ( dst src size -- )
738 [ MOVSX ] %alien-integer-getter ; inline
740 M: x86 %alien-signed-1 8 %alien-signed-getter ;
741 M: x86 %alien-signed-2 16 %alien-signed-getter ;
742 M: x86 %alien-signed-4 32 %alien-signed-getter ;
744 M: x86 %alien-cell [] MOV ;
745 M: x86 %alien-float [] MOVSS ;
746 M: x86 %alien-double [] MOVSD ;
747 M: x86 %alien-vector [ [] ] dip %copy ;
749 :: %alien-integer-setter ( ptr value size -- )
750 value { ptr } size [| new-value |
751 new-value value int-rep %copy
752 ptr [] new-value size n-bit-version-of MOV
753 ] with-small-register ; inline
755 M: x86 %set-alien-integer-1 8 %alien-integer-setter ;
756 M: x86 %set-alien-integer-2 16 %alien-integer-setter ;
757 M: x86 %set-alien-integer-4 32 %alien-integer-setter ;
758 M: x86 %set-alien-cell [ [] ] dip MOV ;
759 M: x86 %set-alien-float [ [] ] dip MOVSS ;
760 M: x86 %set-alien-double [ [] ] dip MOVSD ;
761 M: x86 %set-alien-vector [ [] ] 2dip %copy ;
763 : shift-count? ( reg -- ? ) { ECX RCX } memq? ;
765 :: emit-shift ( dst src1 src2 quot -- )
774 ECX native-version-of [
776 drop dst CL quot call
781 M: x86 %shl [ SHL ] emit-shift ;
782 M: x86 %shr [ SHR ] emit-shift ;
783 M: x86 %sar [ SAR ] emit-shift ;
785 M: x86 %vm-field-ptr ( dst field -- )
786 [ drop 0 MOV rc-absolute-cell rt-vm rel-fixup ]
787 [ vm-field-offset ADD ] 2bi ;
789 : load-zone-ptr ( reg -- )
790 #! Load pointer to start of zone array
791 "nursery" %vm-field-ptr ;
793 : load-allot-ptr ( nursery-ptr allot-ptr -- )
794 [ drop load-zone-ptr ] [ swap cell [+] MOV ] 2bi ;
796 : inc-allot-ptr ( nursery-ptr n -- )
797 [ cell [+] ] dip 8 align ADD ;
799 : store-header ( temp class -- )
800 [ [] ] [ type-number tag-fixnum ] bi* MOV ;
802 : store-tagged ( dst tag -- )
805 M:: x86 %allot ( dst size class nursery-ptr -- )
806 nursery-ptr dst load-allot-ptr
807 dst class store-header
808 dst class store-tagged
809 nursery-ptr size inc-allot-ptr ;
812 M:: x86 %write-barrier ( src card# table -- )
813 #! Mark the card pointed to by vreg.
817 table "cards_offset" %vm-field-ptr
819 table card# [+] card-mark <byte> MOV
822 card# deck-bits card-bits - SHR
823 table "decks_offset" %vm-field-ptr
825 table card# [+] card-mark <byte> MOV ;
827 M:: x86 %check-nursery ( label temp1 temp2 -- )
829 temp2 temp1 cell [+] MOV
831 temp1 temp1 3 cells [+] MOV
835 M: x86 %save-gc-root ( gc-root register -- ) [ gc-root@ ] dip MOV ;
837 M: x86 %load-gc-root ( gc-root register -- ) swap gc-root@ MOV ;
839 M: x86 %alien-global ( dst symbol library -- )
840 [ 0 MOV ] 2dip rc-absolute-cell rel-dlsym ;
842 M: x86 %epilogue ( n -- ) cell - incr-stack-reg ;
844 :: %boolean ( dst temp word -- )
845 dst \ f tag-number MOV
846 temp 0 MOV \ t rc-absolute-cell rel-immediate
847 dst temp word execute ; inline
849 M:: x86 %compare ( dst src1 src2 cc temp -- )
852 { cc< [ dst temp \ CMOVL %boolean ] }
853 { cc<= [ dst temp \ CMOVLE %boolean ] }
854 { cc> [ dst temp \ CMOVG %boolean ] }
855 { cc>= [ dst temp \ CMOVGE %boolean ] }
856 { cc= [ dst temp \ CMOVE %boolean ] }
857 { cc/= [ dst temp \ CMOVNE %boolean ] }
860 M: x86 %compare-imm ( dst src1 src2 cc temp -- )
863 : %cmov-float= ( dst src -- )
865 "no-move" define-label
867 "no-move" get [ JNE ] [ JP ] bi
869 "no-move" resolve-label
872 : %cmov-float/= ( dst src -- )
874 "no-move" define-label
881 "no-move" resolve-label
884 :: (%compare-float) ( dst src1 src2 cc temp compare -- )
886 { cc< [ src2 src1 \ compare execute( a b -- ) dst temp \ CMOVA %boolean ] }
887 { cc<= [ src2 src1 \ compare execute( a b -- ) dst temp \ CMOVAE %boolean ] }
888 { cc> [ src1 src2 \ compare execute( a b -- ) dst temp \ CMOVA %boolean ] }
889 { cc>= [ src1 src2 \ compare execute( a b -- ) dst temp \ CMOVAE %boolean ] }
890 { cc= [ src1 src2 \ compare execute( a b -- ) dst temp \ %cmov-float= %boolean ] }
891 { cc<> [ src1 src2 \ compare execute( a b -- ) dst temp \ CMOVNE %boolean ] }
892 { cc<>= [ src1 src2 \ compare execute( a b -- ) dst temp \ CMOVNP %boolean ] }
893 { cc/< [ src2 src1 \ compare execute( a b -- ) dst temp \ CMOVBE %boolean ] }
894 { cc/<= [ src2 src1 \ compare execute( a b -- ) dst temp \ CMOVB %boolean ] }
895 { cc/> [ src1 src2 \ compare execute( a b -- ) dst temp \ CMOVBE %boolean ] }
896 { cc/>= [ src1 src2 \ compare execute( a b -- ) dst temp \ CMOVB %boolean ] }
897 { cc/= [ src1 src2 \ compare execute( a b -- ) dst temp \ %cmov-float/= %boolean ] }
898 { cc/<> [ src1 src2 \ compare execute( a b -- ) dst temp \ CMOVE %boolean ] }
899 { cc/<>= [ src1 src2 \ compare execute( a b -- ) dst temp \ CMOVP %boolean ] }
902 M: x86 %compare-float-ordered ( dst src1 src2 cc temp -- )
903 \ COMISD (%compare-float) ;
905 M: x86 %compare-float-unordered ( dst src1 src2 cc temp -- )
906 \ UCOMISD (%compare-float) ;
908 M:: x86 %compare-branch ( label src1 src2 cc -- )
912 { cc<= [ label JLE ] }
914 { cc>= [ label JGE ] }
916 { cc/= [ label JNE ] }
919 M: x86 %compare-imm-branch ( label src1 src2 cc -- )
922 : %jump-float= ( label -- )
924 "no-jump" define-label
927 "no-jump" resolve-label
930 : %jump-float/= ( label -- )
933 :: (%compare-float-branch) ( label src1 src2 cc compare -- )
935 { cc< [ src2 src1 \ compare execute( a b -- ) label JA ] }
936 { cc<= [ src2 src1 \ compare execute( a b -- ) label JAE ] }
937 { cc> [ src1 src2 \ compare execute( a b -- ) label JA ] }
938 { cc>= [ src1 src2 \ compare execute( a b -- ) label JAE ] }
939 { cc= [ src1 src2 \ compare execute( a b -- ) label %jump-float= ] }
940 { cc<> [ src1 src2 \ compare execute( a b -- ) label JNE ] }
941 { cc<>= [ src1 src2 \ compare execute( a b -- ) label JNP ] }
942 { cc/< [ src2 src1 \ compare execute( a b -- ) label JBE ] }
943 { cc/<= [ src2 src1 \ compare execute( a b -- ) label JB ] }
944 { cc/> [ src1 src2 \ compare execute( a b -- ) label JBE ] }
945 { cc/>= [ src1 src2 \ compare execute( a b -- ) label JB ] }
946 { cc/= [ src1 src2 \ compare execute( a b -- ) label %jump-float/= ] }
947 { cc/<> [ src1 src2 \ compare execute( a b -- ) label JE ] }
948 { cc/<>= [ src1 src2 \ compare execute( a b -- ) label JP ] }
951 M: x86 %compare-float-ordered-branch ( label src1 src2 cc -- )
952 \ COMISD (%compare-float-branch) ;
954 M: x86 %compare-float-unordered-branch ( label src1 src2 cc -- )
955 \ UCOMISD (%compare-float-branch) ;
957 M:: x86 %spill ( src rep n -- )
958 n spill@ src rep %copy ;
960 M:: x86 %reload ( dst rep n -- )
961 dst n spill@ rep %copy ;
963 M: x86 %loop-entry 16 code-alignment [ NOP ] times ;
965 M:: x86 %save-context ( temp1 temp2 callback-allowed? -- )
966 #! Save Factor stack pointers in case the C code calls a
967 #! callback which does a GC, which must reliably trace
969 temp1 0 MOV rc-absolute-cell rt-vm rel-fixup
970 temp1 temp1 "stack_chain" vm-field-offset [+] MOV
971 temp2 stack-reg cell neg [+] LEA
974 temp1 2 cells [+] ds-reg MOV
975 temp1 3 cells [+] rs-reg MOV
978 M: x86 value-struct? drop t ;
980 M: x86 small-enough? ( n -- ? )
981 HEX: -80000000 HEX: 7fffffff between? ;
983 : next-stack@ ( n -- operand )
984 #! nth parameter from the next stack frame. Used to box
985 #! input values to callbacks; the callback has its own
986 #! stack frame set up, and we want to read the frame
987 #! set up by the caller.
988 stack-frame get total-size>> + stack@ ;
994 :: install-sse2-check ( -- )
997 "This image was built to use SSE2 but your CPU does not support it." print
998 "You will need to bootstrap Factor again." print
1002 ] "cpu.x86" add-init-hook ;
1004 : enable-sse2 ( version -- )
1006 enable-float-intrinsics
1008 enable-float-min/max
1013 [ { sse_version } compile ] with-optimizer
1014 "Checking for multimedia extensions: " write sse-version
1015 [ sse-string write " detected" print ] [ enable-sse2 ] bi ;