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
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 :: overflow-template ( label dst src1 src2 insn -- )
137 M: x86 %fixnum-add ( label dst src1 src2 -- )
138 [ ADD ] overflow-template ;
140 M: x86 %fixnum-sub ( label dst src1 src2 -- )
141 [ SUB ] overflow-template ;
143 M: x86 %fixnum-mul ( label dst src1 src2 -- )
144 [ swap IMUL2 ] overflow-template ;
146 : bignum@ ( reg n -- op )
147 cells bignum tag-number - [+] ; inline
149 M:: x86 %integer>bignum ( dst src temp -- )
150 #! on entry, inreg is a signed 32-bit quantity
151 #! exits with tagged ptr to bignum in outreg
152 #! 1 cell header, 1 cell length, 1 cell sign, + digits
153 #! length is the # of digits + sign
156 ! Load cached zero value
157 dst 0 >bignum %load-reference
159 ! Is it zero? Then just go to the end and return this zero
162 dst 4 cells bignum temp %allot
164 dst 1 bignum@ 2 tag-fixnum MOV
166 dst 3 bignum@ src MOV
169 temp cell-bits 1 - SAR
172 dst 2 bignum@ temp MOV
173 ! Make negative value positive
179 dst 3 bignum@ temp MOV
183 M:: x86 %bignum>integer ( dst src temp -- )
187 temp src 1 bignum@ MOV
188 ! if the length is 1, its just the sign and nothing else,
191 temp 1 tag-fixnum CMP
194 dst src 3 bignum@ MOV
196 temp src 2 bignum@ MOV
197 ! convert it into -1 or 1
206 M: x86 %add-float nip ADDSD ;
207 M: x86 %sub-float nip SUBSD ;
208 M: x86 %mul-float nip MULSD ;
209 M: x86 %div-float nip DIVSD ;
210 M: x86 %min-float nip MINSD ;
211 M: x86 %max-float nip MAXSD ;
212 M: x86 %sqrt SQRTSD ;
214 M: x86 %integer>float CVTSI2SD ;
215 M: x86 %float>integer CVTTSD2SI ;
217 GENERIC: copy-register* ( dst src rep -- )
219 M: int-rep copy-register* drop MOV ;
220 M: tagged-rep copy-register* drop MOV ;
221 M: single-float-rep copy-register* drop MOVSS ;
222 M: double-float-rep copy-register* drop MOVSD ;
224 : copy-register ( dst src rep -- )
225 2over eq? [ 3drop ] [ copy-register* ] if ;
227 M: x86 %copy ( dst src rep -- ) copy-register ;
229 M: x86 %unbox-float ( dst src -- )
230 float-offset [+] MOVSD ;
232 M:: x86 %unbox-any-c-ptr ( dst src temp -- )
234 { "is-byte-array" "end" "start" } [ define-label ] each
237 ! We come back here with displaced aliens
238 "start" resolve-label
240 temp \ f tag-number CMP
242 ! Is the object an alien?
243 temp header-offset [+] alien type-number tag-fixnum CMP
244 "is-byte-array" get JNE
245 ! If so, load the offset and add it to the address
246 dst temp alien-offset [+] ADD
247 ! Now recurse on the underlying alien
248 temp temp underlying-alien-offset [+] MOV
250 "is-byte-array" resolve-label
251 ! Add byte array address to address being computed
253 ! Add an offset to start of byte array's data
254 dst byte-array-offset ADD
258 M:: x86 %box-float ( dst src temp -- )
259 dst 16 float temp %allot
260 dst float-offset [+] src MOVSD ;
262 : alien@ ( reg n -- op ) cells alien tag-number - [+] ;
264 :: %allot-alien ( dst displacement base temp -- )
265 dst 4 cells alien temp %allot
266 dst 1 alien@ base MOV ! alien
267 dst 2 alien@ \ f tag-number MOV ! expired
268 dst 3 alien@ displacement MOV ! displacement
271 M:: x86 %box-alien ( dst src temp -- )
274 dst \ f tag-number MOV
277 dst src \ f tag-number temp %allot-alien
281 M:: x86 %box-displaced-alien ( dst displacement base displacement' base' -- )
285 ! If displacement is zero, return the base
289 ! Quickly use displacement' before its needed for real, as allot temporary
290 dst 4 cells alien displacement' %allot
291 ! If base is already a displaced alien, unpack it
293 displacement' displacement MOV
294 base \ f tag-number CMP
296 base header-offset [+] alien type-number tag-fixnum CMP
298 ! displacement += base.displacement
299 displacement' base 3 alien@ ADD
301 base' base 1 alien@ MOV
303 dst 1 alien@ base' MOV ! alien
304 dst 2 alien@ \ f tag-number MOV ! expired
305 dst 3 alien@ displacement' MOV ! displacement
309 ! The 'small-reg' mess is pretty crappy, but its only used on x86-32.
310 ! On x86-64, all registers have 8-bit versions. However, a similar
311 ! problem arises for shifts, where the shift count must be in CL, and
312 ! so one day I will fix this properly by adding precoloring to the
313 ! register allocator.
315 HOOK: has-small-reg? cpu ( reg size -- ? )
317 CONSTANT: have-byte-regs { EAX ECX EDX EBX }
319 M: x86.32 has-small-reg?
321 { 8 [ have-byte-regs memq? ] }
326 M: x86.64 has-small-reg? 2drop t ;
328 : small-reg-that-isn't ( exclude -- reg' )
329 [ have-byte-regs ] dip
330 [ native-version-of ] map
331 '[ _ memq? not ] find nip ;
333 : with-save/restore ( reg quot -- )
334 [ drop PUSH ] [ call ] [ drop POP ] 2tri ; inline
336 :: with-small-register ( dst exclude size quot: ( new-dst -- ) -- )
337 ! If the destination register overlaps a small register with
338 ! 'size' bits, we call the quot with that. Otherwise, we find a
339 ! small register that is not in exclude, and call quot, saving and
340 ! restoring the small register.
341 dst size has-small-reg? [ dst quot call ] [
342 exclude small-reg-that-isn't
343 [ quot call ] with-save/restore
346 : ?MOV ( dst src -- )
347 2dup = [ 2drop ] [ MOV ] if ; inline
349 M:: x86 %string-nth ( dst src index temp -- )
350 ! We request a small-reg of size 8 since those of size 16 are
353 dst { src index temp } 8 [| new-dst |
354 ! Load the least significant 7 bits into new-dst.
355 ! 8th bit indicates whether we have to load from
356 ! the aux vector or not.
357 temp src index [+] LEA
358 new-dst 8-bit-version-of temp string-offset [+] MOV
359 new-dst new-dst 8-bit-version-of MOVZX
360 ! Do we have to look at the aux vector?
363 ! Yes, this is a non-ASCII character. Load aux vector
364 temp src string-aux-offset [+] MOV
370 new-dst 16-bit-version-of new-dst byte-array-offset [+] MOV
371 new-dst new-dst 16-bit-version-of MOVZX
377 ] with-small-register ;
379 M:: x86 %set-string-nth-fast ( ch str index temp -- )
380 ch { index str temp } 8 [| new-ch |
382 temp str index [+] LEA
383 temp string-offset [+] new-ch 8-bit-version-of MOV
384 ] with-small-register ;
386 :: %alien-integer-getter ( dst src size quot -- )
387 dst { src } size [| new-dst |
388 new-dst dup size n-bit-version-of dup src [] MOV
391 ] with-small-register ; inline
393 : %alien-unsigned-getter ( dst src size -- )
394 [ MOVZX ] %alien-integer-getter ; inline
396 M: x86 %alien-unsigned-1 8 %alien-unsigned-getter ;
397 M: x86 %alien-unsigned-2 16 %alien-unsigned-getter ;
398 M: x86 %alien-unsigned-4 32 [ 2drop ] %alien-integer-getter ;
400 : %alien-signed-getter ( dst src size -- )
401 [ MOVSX ] %alien-integer-getter ; inline
403 M: x86 %alien-signed-1 8 %alien-signed-getter ;
404 M: x86 %alien-signed-2 16 %alien-signed-getter ;
405 M: x86 %alien-signed-4 32 %alien-signed-getter ;
407 M: x86 %alien-cell [] MOV ;
408 M: x86 %alien-float dupd [] MOVSS dup CVTSS2SD ;
409 M: x86 %alien-double [] MOVSD ;
411 :: %alien-integer-setter ( ptr value size -- )
412 value { ptr } size [| new-value |
414 ptr [] new-value size n-bit-version-of MOV
415 ] with-small-register ; inline
417 M: x86 %set-alien-integer-1 8 %alien-integer-setter ;
418 M: x86 %set-alien-integer-2 16 %alien-integer-setter ;
419 M: x86 %set-alien-integer-4 32 %alien-integer-setter ;
420 M: x86 %set-alien-cell [ [] ] dip MOV ;
421 M: x86 %set-alien-float dup dup CVTSD2SS [ [] ] dip MOVSS ;
422 M: x86 %set-alien-double [ [] ] dip MOVSD ;
424 : shift-count? ( reg -- ? ) { ECX RCX } memq? ;
426 :: emit-shift ( dst src1 src2 quot -- )
435 ECX native-version-of [
437 drop dst CL quot call
442 M: x86 %shl [ SHL ] emit-shift ;
443 M: x86 %shr [ SHR ] emit-shift ;
444 M: x86 %sar [ SAR ] emit-shift ;
446 : load-zone-ptr ( reg -- )
447 #! Load pointer to start of zone array
448 0 MOV "nursery" f rc-absolute-cell rel-dlsym ;
450 : load-allot-ptr ( nursery-ptr allot-ptr -- )
451 [ drop load-zone-ptr ] [ swap cell [+] MOV ] 2bi ;
453 : inc-allot-ptr ( nursery-ptr n -- )
454 [ cell [+] ] dip 8 align ADD ;
456 : store-header ( temp class -- )
457 [ [] ] [ type-number tag-fixnum ] bi* MOV ;
459 : store-tagged ( dst tag -- )
462 M:: x86 %allot ( dst size class nursery-ptr -- )
463 nursery-ptr dst load-allot-ptr
464 dst class store-header
465 dst class store-tagged
466 nursery-ptr size inc-allot-ptr ;
468 M:: x86 %write-barrier ( src card# table -- )
469 #! Mark the card pointed to by vreg.
473 table "cards_offset" f %alien-global
475 table card# [+] card-mark <byte> MOV
478 card# deck-bits card-bits - SHR
479 table "decks_offset" f %alien-global
481 table card# [+] card-mark <byte> MOV ;
483 M:: x86 %check-nursery ( label temp1 temp2 -- )
485 temp2 temp1 cell [+] MOV
487 temp1 temp1 3 cells [+] MOV
491 M: x86 %save-gc-root ( gc-root register -- ) [ gc-root@ ] dip MOV ;
493 M: x86 %load-gc-root ( gc-root register -- ) swap gc-root@ MOV ;
495 M:: x86 %call-gc ( gc-root-count -- )
496 ! Pass pointer to start of GC roots as first parameter
497 param-reg-1 gc-root-base param@ LEA
498 ! Pass number of roots as second parameter
499 param-reg-2 gc-root-count MOV
501 %prepare-alien-invoke
502 "inline_gc" f %alien-invoke ;
505 [ 0 MOV ] 2dip rc-absolute-cell rel-dlsym ;
507 M: x86 %epilogue ( n -- ) cell - incr-stack-reg ;
509 :: %boolean ( dst temp word -- )
510 dst \ f tag-number MOV
511 temp 0 MOV \ t rc-absolute-cell rel-immediate
512 dst temp word execute ; inline
514 M:: x86 %compare ( dst temp cc src1 src2 -- )
517 { cc< [ dst temp \ CMOVL %boolean ] }
518 { cc<= [ dst temp \ CMOVLE %boolean ] }
519 { cc> [ dst temp \ CMOVG %boolean ] }
520 { cc>= [ dst temp \ CMOVGE %boolean ] }
521 { cc= [ dst temp \ CMOVE %boolean ] }
522 { cc/= [ dst temp \ CMOVNE %boolean ] }
525 M: x86 %compare-imm ( dst temp cc src1 src2 -- )
528 : %cmov-float= ( dst src -- )
530 "no-move" define-label
532 "no-move" get [ JNE ] [ JP ] bi
534 "no-move" resolve-label
537 : %cmov-float/= ( dst src -- )
539 "no-move" define-label
546 "no-move" resolve-label
549 M:: x86 %compare-float ( dst temp cc src1 src2 -- )
551 { cc< [ src2 src1 UCOMISD dst temp \ CMOVA %boolean ] }
552 { cc<= [ src2 src1 UCOMISD dst temp \ CMOVAE %boolean ] }
553 { cc> [ src1 src2 UCOMISD dst temp \ CMOVA %boolean ] }
554 { cc>= [ src1 src2 UCOMISD dst temp \ CMOVAE %boolean ] }
555 { cc= [ src1 src2 COMISD dst temp \ %cmov-float= %boolean ] }
556 { cc<> [ src1 src2 UCOMISD dst temp \ CMOVNE %boolean ] }
557 { cc<>= [ src1 src2 UCOMISD dst temp \ CMOVNP %boolean ] }
558 { cc/< [ src2 src1 COMISD dst temp \ CMOVBE %boolean ] }
559 { cc/<= [ src2 src1 COMISD dst temp \ CMOVB %boolean ] }
560 { cc/> [ src1 src2 COMISD dst temp \ CMOVBE %boolean ] }
561 { cc/>= [ src1 src2 COMISD dst temp \ CMOVB %boolean ] }
562 { cc/= [ src1 src2 COMISD dst temp \ %cmov-float/= %boolean ] }
563 { cc/<> [ src1 src2 COMISD dst temp \ CMOVE %boolean ] }
564 { cc/<>= [ src1 src2 COMISD dst temp \ CMOVP %boolean ] }
567 M:: x86 %compare-branch ( label cc src1 src2 -- )
571 { cc<= [ label JLE ] }
573 { cc>= [ label JGE ] }
575 { cc/= [ label JNE ] }
578 M: x86 %compare-imm-branch ( label src1 src2 cc -- )
581 : %jump-float= ( label -- )
583 "no-jump" define-label
586 "no-jump" resolve-label
589 : %jump-float/= ( label -- )
592 M:: x86 %compare-float-branch ( label cc src1 src2 -- )
594 { cc< [ src2 src1 UCOMISD label JA ] }
595 { cc<= [ src2 src1 UCOMISD label JAE ] }
596 { cc> [ src1 src2 UCOMISD label JA ] }
597 { cc>= [ src1 src2 UCOMISD label JAE ] }
598 { cc= [ src1 src2 COMISD label %jump-float= ] }
599 { cc<> [ src1 src2 UCOMISD label JNE ] }
600 { cc<>= [ src1 src2 UCOMISD label JNP ] }
601 { cc/< [ src2 src1 COMISD label JBE ] }
602 { cc/<= [ src2 src1 COMISD label JB ] }
603 { cc/> [ src1 src2 COMISD label JBE ] }
604 { cc/>= [ src1 src2 COMISD label JB ] }
605 { cc/= [ src1 src2 COMISD label %jump-float/= ] }
606 { cc/<> [ src1 src2 COMISD label JE ] }
607 { cc/<>= [ src1 src2 COMISD label JP ] }
610 M: x86 %spill ( src n rep -- ) [ spill@ swap ] dip copy-register ;
611 M: x86 %reload ( dst n rep -- ) [ spill@ ] dip copy-register ;
613 M: x86 %loop-entry 16 code-alignment [ NOP ] times ;
615 M: x86 %prepare-alien-invoke
616 #! Save Factor stack pointers in case the C code calls a
617 #! callback which does a GC, which must reliably trace
619 temp-reg "stack_chain" f %alien-global
620 temp-reg temp-reg [] MOV
621 temp-reg [] stack-reg MOV
623 temp-reg 2 cells [+] ds-reg MOV
624 temp-reg 3 cells [+] rs-reg MOV ;
626 M: x86 value-struct? drop t ;
628 M: x86 small-enough? ( n -- ? )
629 HEX: -80000000 HEX: 7fffffff between? ;
631 : next-stack@ ( n -- operand )
632 #! nth parameter from the next stack frame. Used to box
633 #! input values to callbacks; the callback has its own
634 #! stack frame set up, and we want to read the frame
635 #! set up by the caller.
636 stack-frame get total-size>> + stack@ ;
639 enable-float-intrinsics
641 enable-float-min/max ;