1 ! Copyright (C) 2005, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs alien alien.c-types arrays strings
4 cpu.x86.assembler cpu.x86.assembler.private cpu.x86.assembler.operands
5 cpu.architecture kernel kernel.private math memory namespaces make
6 sequences words system layouts combinators math.order fry locals
7 compiler.constants byte-arrays
9 compiler.cfg.instructions
10 compiler.cfg.intrinsics
11 compiler.cfg.comparisons
12 compiler.cfg.stack-frame
14 compiler.codegen.fixup ;
17 << enable-fixnum-log2 >>
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 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 ! Fastcall calling convention
54 HOOK: param-reg-1 cpu ( -- reg )
55 HOOK: param-reg-2 cpu ( -- reg )
57 HOOK: pic-tail-reg cpu ( -- reg )
59 M: x86 %load-immediate dup 0 = [ drop dup XOR ] [ MOV ] if ;
61 M: x86 %load-reference swap 0 MOV rc-absolute-cell rel-immediate ;
63 HOOK: ds-reg cpu ( -- reg )
64 HOOK: rs-reg cpu ( -- reg )
66 : reg-stack ( n reg -- op ) swap cells neg [+] ;
68 GENERIC: loc>operand ( loc -- operand )
70 M: ds-loc loc>operand n>> ds-reg reg-stack ;
71 M: rs-loc loc>operand n>> rs-reg reg-stack ;
73 M: x86 %peek loc>operand MOV ;
74 M: x86 %replace loc>operand swap MOV ;
75 : (%inc) ( n reg -- ) swap cells dup 0 > [ ADD ] [ neg SUB ] if ; inline
76 M: x86 %inc-d ( n -- ) ds-reg (%inc) ;
77 M: x86 %inc-r ( n -- ) rs-reg (%inc) ;
79 M: x86 %call ( word -- ) 0 CALL rc-relative rel-word-pic ;
81 : xt-tail-pic-offset ( -- n )
82 #! See the comment in vm/cpu-x86.hpp
85 M: x86 %jump ( word -- )
86 pic-tail-reg 0 MOV xt-tail-pic-offset rc-absolute-cell rel-here
87 0 JMP rc-relative rel-word-pic-tail ;
89 M: x86 %jump-label ( label -- ) 0 JMP rc-relative label-fixup ;
91 M: x86 %return ( -- ) 0 RET ;
93 : code-alignment ( align -- n )
94 [ building get length dup ] dip align swap - ;
99 :: (%slot) ( obj slot tag temp -- op )
100 temp slot obj [+] LEA
101 temp tag neg [+] ; inline
103 :: (%slot-imm) ( obj slot tag -- op )
104 obj slot cells tag - [+] ; inline
106 M: x86 %slot ( dst obj slot tag temp -- ) (%slot) MOV ;
107 M: x86 %slot-imm ( dst obj slot tag -- ) (%slot-imm) MOV ;
108 M: x86 %set-slot ( src obj slot tag temp -- ) (%slot) swap MOV ;
109 M: x86 %set-slot-imm ( src obj slot tag -- ) (%slot-imm) swap MOV ;
111 M: x86 %add 2over eq? [ nip ADD ] [ [+] LEA ] if ;
112 M: x86 %add-imm 2over eq? [ nip ADD ] [ [+] LEA ] if ;
113 M: x86 %sub nip SUB ;
114 M: x86 %sub-imm 2over eq? [ nip SUB ] [ neg [+] LEA ] if ;
115 M: x86 %mul nip swap IMUL2 ;
116 M: x86 %mul-imm IMUL3 ;
117 M: x86 %and nip AND ;
118 M: x86 %and-imm nip AND ;
120 M: x86 %or-imm nip OR ;
121 M: x86 %xor nip XOR ;
122 M: x86 %xor-imm nip XOR ;
123 M: x86 %shl-imm nip SHL ;
124 M: x86 %shr-imm nip SHR ;
125 M: x86 %sar-imm nip SAR ;
127 M: x86 %min nip [ CMP ] [ CMOVG ] 2bi ;
128 M: x86 %max nip [ CMP ] [ CMOVL ] 2bi ;
130 M: x86 %not drop NOT ;
133 GENERIC: copy-register* ( dst src rep -- )
135 M: int-rep copy-register* drop MOV ;
136 M: tagged-rep copy-register* drop MOV ;
137 M: float-rep copy-register* drop MOVSS ;
138 M: double-rep copy-register* drop MOVSD ;
139 M: float-4-rep copy-register* drop MOVUPS ;
140 M: double-2-rep copy-register* drop MOVUPD ;
141 M: vector-rep copy-register* drop MOVDQU ;
143 : copy-register ( dst src rep -- )
144 2over eq? [ 3drop ] [ copy-register* ] if ;
146 M: x86 %copy ( dst src rep -- ) copy-register ;
148 :: overflow-template ( label dst src1 src2 insn -- )
152 M: x86 %fixnum-add ( label dst src1 src2 -- )
153 [ ADD ] overflow-template ;
155 M: x86 %fixnum-sub ( label dst src1 src2 -- )
156 [ SUB ] overflow-template ;
158 M: x86 %fixnum-mul ( label dst src1 src2 -- )
159 [ swap IMUL2 ] overflow-template ;
161 : bignum@ ( reg n -- op )
162 cells bignum tag-number - [+] ; inline
164 M:: x86 %integer>bignum ( dst src temp -- )
165 #! on entry, inreg is a signed 32-bit quantity
166 #! exits with tagged ptr to bignum in outreg
167 #! 1 cell header, 1 cell length, 1 cell sign, + digits
168 #! length is the # of digits + sign
171 ! Load cached zero value
172 dst 0 >bignum %load-reference
174 ! Is it zero? Then just go to the end and return this zero
177 dst 4 cells bignum temp %allot
179 dst 1 bignum@ 2 tag-fixnum MOV
181 dst 3 bignum@ src MOV
184 temp cell-bits 1 - SAR
187 dst 2 bignum@ temp MOV
188 ! Make negative value positive
194 dst 3 bignum@ temp MOV
198 M:: x86 %bignum>integer ( dst src temp -- )
202 temp src 1 bignum@ MOV
203 ! if the length is 1, its just the sign and nothing else,
206 temp 1 tag-fixnum CMP
209 dst src 3 bignum@ MOV
211 temp src 2 bignum@ MOV
212 ! convert it into -1 or 1
221 M: x86 %add-float nip ADDSD ;
222 M: x86 %sub-float nip SUBSD ;
223 M: x86 %mul-float nip MULSD ;
224 M: x86 %div-float nip DIVSD ;
225 M: x86 %min-float nip MINSD ;
226 M: x86 %max-float nip MAXSD ;
227 M: x86 %sqrt SQRTSD ;
229 M: x86 %single>double-float CVTSS2SD ;
230 M: x86 %double>single-float CVTSD2SS ;
232 M: x86 %integer>float CVTSI2SD ;
233 M: x86 %float>integer CVTTSD2SI ;
235 M: x86 %unbox-float ( dst src -- )
236 float-offset [+] MOVSD ;
238 M:: x86 %box-float ( dst src temp -- )
239 dst 16 float temp %allot
240 dst float-offset [+] src MOVSD ;
242 M:: x86 %box-vector ( dst src rep temp -- )
243 dst rep rep-size 2 cells + byte-array temp %allot
244 16 tag-fixnum dst 1 byte-array tag-number %set-slot-imm
245 dst byte-array-offset [+]
246 src rep copy-register ;
248 M:: x86 %unbox-vector ( dst src rep -- )
249 dst src byte-array-offset [+]
252 M: x86 %broadcast-vector ( dst src rep -- )
254 { float-4-rep [ [ MOVAPS ] [ drop dup 0 SHUFPS ] 2bi ] }
255 { double-2-rep [ [ MOVAPD ] [ drop dup 0 SHUFPD ] 2bi ] }
258 M:: x86 %gather-vector-4 ( dst src1 src2 src3 src4 rep -- )
266 dst src3 HEX: 44 SHUFPS
271 M:: x86 %gather-vector-2 ( dst src1 src2 rep -- )
282 M: x86 %add-vector ( dst src1 src2 rep -- )
284 { float-4-rep [ ADDPS ] }
285 { double-2-rep [ ADDPD ] }
286 { char-16-rep [ PADDB ] }
287 { uchar-16-rep [ PADDB ] }
288 { short-8-rep [ PADDW ] }
289 { ushort-8-rep [ PADDW ] }
290 { int-4-rep [ PADDD ] }
291 { uint-4-rep [ PADDD ] }
294 M: x86 %sub-vector ( dst src1 src2 rep -- )
296 { float-4-rep [ SUBPS ] }
297 { double-2-rep [ SUBPD ] }
298 { char-16-rep [ PSUBB ] }
299 { uchar-16-rep [ PSUBB ] }
300 { short-8-rep [ PSUBW ] }
301 { ushort-8-rep [ PSUBW ] }
302 { int-4-rep [ PSUBD ] }
303 { uint-4-rep [ PSUBD ] }
306 M: x86 %mul-vector ( dst src1 src2 rep -- )
308 { float-4-rep [ MULPS ] }
309 { double-2-rep [ MULPD ] }
310 { int-4-rep [ PMULLW ] }
313 M: x86 %div-vector ( dst src1 src2 rep -- )
315 { float-4-rep [ DIVPS ] }
316 { double-2-rep [ DIVPD ] }
319 M: x86 %min-vector ( dst src1 src2 rep -- )
321 { float-4-rep [ MINPS ] }
322 { double-2-rep [ MINPD ] }
325 M: x86 %max-vector ( dst src1 src2 rep -- )
327 { float-4-rep [ MAXPS ] }
328 { double-2-rep [ MAXPD ] }
331 M: x86 %sqrt-vector ( dst src rep -- )
333 { float-4-rep [ SQRTPS ] }
334 { double-2-rep [ SQRTPD ] }
337 M: x86 %horizontal-add-vector ( dst src rep -- )
339 { float-4-rep [ [ MOVAPS ] [ HADDPS ] [ HADDPS ] 2tri ] }
340 { double-2-rep [ [ MOVAPD ] [ HADDPD ] 2bi ] }
343 M:: x86 %unbox-any-c-ptr ( dst src temp -- )
345 { "is-byte-array" "end" "start" } [ define-label ] each
348 ! We come back here with displaced aliens
349 "start" resolve-label
351 temp \ f tag-number CMP
353 ! Is the object an alien?
354 temp header-offset [+] alien type-number tag-fixnum CMP
355 "is-byte-array" get JNE
356 ! If so, load the offset and add it to the address
357 dst temp alien-offset [+] ADD
358 ! Now recurse on the underlying alien
359 temp temp underlying-alien-offset [+] MOV
361 "is-byte-array" resolve-label
362 ! Add byte array address to address being computed
364 ! Add an offset to start of byte array's data
365 dst byte-array-offset ADD
369 : alien@ ( reg n -- op ) cells alien tag-number - [+] ;
371 :: %allot-alien ( dst displacement base temp -- )
372 dst 4 cells alien temp %allot
373 dst 1 alien@ base MOV ! alien
374 dst 2 alien@ \ f tag-number MOV ! expired
375 dst 3 alien@ displacement MOV ! displacement
378 M:: x86 %box-alien ( dst src temp -- )
381 dst \ f tag-number MOV
384 dst src \ f tag-number temp %allot-alien
388 M:: x86 %box-displaced-alien ( dst displacement base displacement' base' base-class -- )
392 ! If displacement is zero, return the base
396 ! Quickly use displacement' before its needed for real, as allot temporary
397 dst 4 cells alien displacement' %allot
398 ! If base is already a displaced alien, unpack it
400 displacement' displacement MOV
401 base \ f tag-number CMP
403 base header-offset [+] alien type-number tag-fixnum CMP
405 ! displacement += base.displacement
406 displacement' base 3 alien@ ADD
408 base' base 1 alien@ MOV
410 dst 1 alien@ base' MOV ! alien
411 dst 2 alien@ \ f tag-number MOV ! expired
412 dst 3 alien@ displacement' MOV ! displacement
416 ! The 'small-reg' mess is pretty crappy, but its only used on x86-32.
417 ! On x86-64, all registers have 8-bit versions. However, a similar
418 ! problem arises for shifts, where the shift count must be in CL, and
419 ! so one day I will fix this properly by adding precoloring to the
420 ! register allocator.
422 HOOK: has-small-reg? cpu ( reg size -- ? )
424 CONSTANT: have-byte-regs { EAX ECX EDX EBX }
426 M: x86.32 has-small-reg?
428 { 8 [ have-byte-regs memq? ] }
433 M: x86.64 has-small-reg? 2drop t ;
435 : small-reg-that-isn't ( exclude -- reg' )
436 [ have-byte-regs ] dip
437 [ native-version-of ] map
438 '[ _ memq? not ] find nip ;
440 : with-save/restore ( reg quot -- )
441 [ drop PUSH ] [ call ] [ drop POP ] 2tri ; inline
443 :: with-small-register ( dst exclude size quot: ( new-dst -- ) -- )
444 ! If the destination register overlaps a small register with
445 ! 'size' bits, we call the quot with that. Otherwise, we find a
446 ! small register that is not in exclude, and call quot, saving and
447 ! restoring the small register.
448 dst size has-small-reg? [ dst quot call ] [
449 exclude small-reg-that-isn't
450 [ quot call ] with-save/restore
453 : ?MOV ( dst src -- )
454 2dup = [ 2drop ] [ MOV ] if ; inline
456 M:: x86 %string-nth ( dst src index temp -- )
457 ! We request a small-reg of size 8 since those of size 16 are
460 dst { src index temp } 8 [| new-dst |
461 ! Load the least significant 7 bits into new-dst.
462 ! 8th bit indicates whether we have to load from
463 ! the aux vector or not.
464 temp src index [+] LEA
465 new-dst 8-bit-version-of temp string-offset [+] MOV
466 new-dst new-dst 8-bit-version-of MOVZX
467 ! Do we have to look at the aux vector?
470 ! Yes, this is a non-ASCII character. Load aux vector
471 temp src string-aux-offset [+] MOV
477 new-dst 16-bit-version-of new-dst byte-array-offset [+] MOV
478 new-dst new-dst 16-bit-version-of MOVZX
484 ] with-small-register ;
486 M:: x86 %set-string-nth-fast ( ch str index temp -- )
487 ch { index str temp } 8 [| new-ch |
489 temp str index [+] LEA
490 temp string-offset [+] new-ch 8-bit-version-of MOV
491 ] with-small-register ;
493 :: %alien-integer-getter ( dst src size quot -- )
494 dst { src } size [| new-dst |
495 new-dst dup size n-bit-version-of dup src [] MOV
498 ] with-small-register ; inline
500 : %alien-unsigned-getter ( dst src size -- )
501 [ MOVZX ] %alien-integer-getter ; inline
503 M: x86 %alien-unsigned-1 8 %alien-unsigned-getter ;
504 M: x86 %alien-unsigned-2 16 %alien-unsigned-getter ;
505 M: x86 %alien-unsigned-4 32 [ 2drop ] %alien-integer-getter ;
507 : %alien-signed-getter ( dst src size -- )
508 [ MOVSX ] %alien-integer-getter ; inline
510 M: x86 %alien-signed-1 8 %alien-signed-getter ;
511 M: x86 %alien-signed-2 16 %alien-signed-getter ;
512 M: x86 %alien-signed-4 32 %alien-signed-getter ;
514 M: x86 %alien-cell [] MOV ;
515 M: x86 %alien-float [] MOVSS ;
516 M: x86 %alien-double [] MOVSD ;
517 M: x86 %alien-vector [ [] ] dip copy-register ;
519 :: %alien-integer-setter ( ptr value size -- )
520 value { ptr } size [| new-value |
522 ptr [] new-value size n-bit-version-of MOV
523 ] with-small-register ; inline
525 M: x86 %set-alien-integer-1 8 %alien-integer-setter ;
526 M: x86 %set-alien-integer-2 16 %alien-integer-setter ;
527 M: x86 %set-alien-integer-4 32 %alien-integer-setter ;
528 M: x86 %set-alien-cell [ [] ] dip MOV ;
529 M: x86 %set-alien-float [ [] ] dip MOVSS ;
530 M: x86 %set-alien-double [ [] ] dip MOVSD ;
531 M: x86 %set-alien-vector [ [] ] 2dip copy-register ;
533 : shift-count? ( reg -- ? ) { ECX RCX } memq? ;
535 :: emit-shift ( dst src1 src2 quot -- )
544 ECX native-version-of [
546 drop dst CL quot call
551 M: x86 %shl [ SHL ] emit-shift ;
552 M: x86 %shr [ SHR ] emit-shift ;
553 M: x86 %sar [ SAR ] emit-shift ;
555 : load-zone-ptr ( reg -- )
556 #! Load pointer to start of zone array
557 0 MOV "nursery" f rc-absolute-cell rel-dlsym ;
559 : load-allot-ptr ( nursery-ptr allot-ptr -- )
560 [ drop load-zone-ptr ] [ swap cell [+] MOV ] 2bi ;
562 : inc-allot-ptr ( nursery-ptr n -- )
563 [ cell [+] ] dip 8 align ADD ;
565 : store-header ( temp class -- )
566 [ [] ] [ type-number tag-fixnum ] bi* MOV ;
568 : store-tagged ( dst tag -- )
571 M:: x86 %allot ( dst size class nursery-ptr -- )
572 nursery-ptr dst load-allot-ptr
573 dst class store-header
574 dst class store-tagged
575 nursery-ptr size inc-allot-ptr ;
577 M:: x86 %write-barrier ( src card# table -- )
578 #! Mark the card pointed to by vreg.
582 table "cards_offset" f %alien-global
584 table card# [+] card-mark <byte> MOV
587 card# deck-bits card-bits - SHR
588 table "decks_offset" f %alien-global
590 table card# [+] card-mark <byte> MOV ;
592 M:: x86 %check-nursery ( label temp1 temp2 -- )
594 temp2 temp1 cell [+] MOV
596 temp1 temp1 3 cells [+] MOV
600 M: x86 %save-gc-root ( gc-root register -- ) [ gc-root@ ] dip MOV ;
602 M: x86 %load-gc-root ( gc-root register -- ) swap gc-root@ MOV ;
604 M:: x86 %call-gc ( gc-root-count -- )
605 ! Pass pointer to start of GC roots as first parameter
606 param-reg-1 gc-root-base param@ LEA
607 ! Pass number of roots as second parameter
608 param-reg-2 gc-root-count MOV
610 %prepare-alien-invoke
611 "inline_gc" f %alien-invoke ;
614 [ 0 MOV ] 2dip rc-absolute-cell rel-dlsym ;
616 M: x86 %epilogue ( n -- ) cell - incr-stack-reg ;
618 :: %boolean ( dst temp word -- )
619 dst \ f tag-number MOV
620 temp 0 MOV \ t rc-absolute-cell rel-immediate
621 dst temp word execute ; inline
623 M: x86 %compare ( dst src1 src2 cc temp -- )
625 { cc< [ \ CMOVL %boolean ] }
626 { cc<= [ \ CMOVLE %boolean ] }
627 { cc> [ \ CMOVG %boolean ] }
628 { cc>= [ \ CMOVGE %boolean ] }
629 { cc= [ \ CMOVE %boolean ] }
630 { cc/= [ \ CMOVNE %boolean ] }
633 M: x86 %compare-imm ( dst src1 src2 cc temp -- )
636 M: x86 %compare-float ( dst src1 src2 cc temp -- )
637 [ UCOMISD ] 2dip swap {
638 { cc< [ \ CMOVB %boolean ] }
639 { cc<= [ \ CMOVBE %boolean ] }
640 { cc> [ \ CMOVA %boolean ] }
641 { cc>= [ \ CMOVAE %boolean ] }
642 { cc= [ \ CMOVE %boolean ] }
643 { cc/= [ \ CMOVNE %boolean ] }
646 M: x86 %compare-branch ( label src1 src2 cc -- )
656 M: x86 %compare-imm-branch ( label src1 src2 cc -- )
659 M: x86 %compare-float-branch ( label src1 src2 cc -- )
669 M:: x86 %spill ( src rep n -- )
670 n spill@ src rep copy-register ;
672 M:: x86 %reload ( dst rep n -- )
673 dst n spill@ rep copy-register ;
675 M: x86 %loop-entry 16 code-alignment [ NOP ] times ;
677 M: x86 %prepare-alien-invoke
678 #! Save Factor stack pointers in case the C code calls a
679 #! callback which does a GC, which must reliably trace
681 temp-reg "stack_chain" f %alien-global
682 temp-reg temp-reg [] MOV
683 temp-reg [] stack-reg MOV
685 temp-reg 2 cells [+] ds-reg MOV
686 temp-reg 3 cells [+] rs-reg MOV ;
688 M: x86 value-struct? drop t ;
690 M: x86 small-enough? ( n -- ? )
691 HEX: -80000000 HEX: 7fffffff between? ;
693 : next-stack@ ( n -- operand )
694 #! nth parameter from the next stack frame. Used to box
695 #! input values to callbacks; the callback has its own
696 #! stack frame set up, and we want to read the frame
697 #! set up by the caller.
698 stack-frame get total-size>> + stack@ ;
701 enable-float-intrinsics