1 ! Copyright (C) 2005, 2010 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 math.vectors fry locals compiler.constants
8 byte-arrays io macros quotations classes.algebra compiler
9 compiler.units init vm vocabs.loader
10 compiler.cfg.registers
11 compiler.cfg.instructions
12 compiler.cfg.intrinsics
13 compiler.cfg.comparisons
14 compiler.cfg.stack-frame
15 compiler.codegen.fixup ;
16 QUALIFIED-WITH: alien.c-types c
17 FROM: layouts => cell ;
21 ! Add some methods to the assembler to be more useful to the backend
22 M: label JMP 0 JMP rc-relative label-fixup ;
23 M: label JUMPcc [ 0 ] dip JUMPcc rc-relative label-fixup ;
25 M: x86 vector-regs float-regs ;
27 HOOK: stack-reg cpu ( -- reg )
29 HOOK: reserved-stack-space cpu ( -- n )
31 : stack@ ( n -- op ) stack-reg swap [+] ;
33 : special-offset ( m -- n )
34 reserved-stack-space + ;
36 : spill@ ( n -- op ) spill-offset special-offset stack@ ;
38 : gc-root-offsets ( seq -- seq' )
39 [ n>> spill-offset special-offset cell + ] map f like ;
41 : decr-stack-reg ( n -- )
42 dup 0 = [ drop ] [ stack-reg swap SUB ] if ;
44 : incr-stack-reg ( n -- )
45 dup 0 = [ drop ] [ stack-reg swap ADD ] if ;
47 : align-stack ( n -- n' ) 16 align ;
49 M: x86 stack-frame-size ( stack-frame -- i )
51 reserved-stack-space +
55 HOOK: pic-tail-reg cpu ( -- reg )
57 M: x86 complex-addressing? t ;
59 M: x86 fused-unboxing? t ;
61 M: x86 test-instruction? t ;
63 M: x86 immediate-store? immediate-comparand? ;
65 M: x86 %load-immediate dup 0 = [ drop dup XOR ] [ MOV ] if ;
67 M: x86 %load-reference
68 [ swap 0 MOV rc-absolute-cell rel-literal ]
69 [ \ f type-number MOV ]
72 HOOK: ds-reg cpu ( -- reg )
73 HOOK: rs-reg cpu ( -- reg )
75 : reg-stack ( n reg -- op ) swap cells neg [+] ;
77 GENERIC: loc>operand ( loc -- operand )
79 M: ds-loc loc>operand n>> ds-reg reg-stack ;
80 M: rs-loc loc>operand n>> rs-reg reg-stack ;
82 M: x86 %peek loc>operand MOV ;
84 M: x86 %replace loc>operand swap MOV ;
89 { [ dup not ] [ drop \ f type-number MOV ] }
90 { [ dup fixnum? ] [ tag-fixnum MOV ] }
91 [ [ HEX: ffffffff MOV ] dip rc-absolute rel-literal ]
94 : (%inc) ( n reg -- ) swap cells dup 0 > [ ADD ] [ neg SUB ] if ; inline
95 M: x86 %inc-d ( n -- ) ds-reg (%inc) ;
96 M: x86 %inc-r ( n -- ) rs-reg (%inc) ;
98 M: x86 %call ( word -- ) 0 CALL rc-relative rel-word-pic ;
100 : xt-tail-pic-offset ( -- n )
101 #! See the comment in vm/cpu-x86.hpp
104 HOOK: %prepare-jump cpu ( -- )
106 M: x86 %jump ( word -- )
108 0 JMP rc-relative rel-word-pic-tail ;
110 M: x86 %jump-label ( label -- ) 0 JMP rc-relative label-fixup ;
112 M: x86 %return ( -- ) 0 RET ;
114 : (%slot) ( obj slot scale tag -- op ) neg <indirect> ; inline
115 : (%slot-imm) ( obj slot tag -- op ) slot-offset [+] ; inline
117 M: x86 %slot ( dst obj slot scale tag -- ) (%slot) MOV ;
118 M: x86 %slot-imm ( dst obj slot tag -- ) (%slot-imm) MOV ;
119 M: x86 %set-slot ( src obj slot scale tag -- ) (%slot) swap MOV ;
120 M: x86 %set-slot-imm ( src obj slot tag -- ) (%slot-imm) swap MOV ;
122 :: two-operand ( dst src1 src2 rep -- dst src )
123 dst src2 eq? dst src1 eq? not and [ "Cannot handle this case" throw ] when
127 :: one-operand ( dst src rep -- dst )
131 M: x86 %add 2over eq? [ nip ADD ] [ [+] LEA ] if ;
132 M: x86 %add-imm 2over eq? [ nip ADD ] [ [+] LEA ] if ;
133 M: x86 %sub int-rep two-operand SUB ;
134 M: x86 %sub-imm 2over eq? [ nip SUB ] [ neg [+] LEA ] if ;
135 M: x86 %mul int-rep two-operand IMUL2 ;
136 M: x86 %mul-imm IMUL3 ;
137 M: x86 %and int-rep two-operand AND ;
138 M: x86 %and-imm int-rep two-operand AND ;
139 M: x86 %or int-rep two-operand OR ;
140 M: x86 %or-imm int-rep two-operand OR ;
141 M: x86 %xor int-rep two-operand XOR ;
142 M: x86 %xor-imm int-rep two-operand XOR ;
143 M: x86 %shl-imm int-rep two-operand SHL ;
144 M: x86 %shr-imm int-rep two-operand SHR ;
145 M: x86 %sar-imm int-rep two-operand SAR ;
147 M: x86 %min int-rep two-operand [ CMP ] [ CMOVG ] 2bi ;
148 M: x86 %max int-rep two-operand [ CMP ] [ CMOVL ] 2bi ;
150 M: x86 %not int-rep one-operand NOT ;
151 M: x86 %neg int-rep one-operand NEG ;
153 M: x86 %bit-count POPCNT ;
155 ! A bit of logic to avoid using MOVSS/MOVSD for reg-reg moves
156 ! since this induces partial register stalls
157 GENERIC: copy-register* ( dst src rep -- )
158 GENERIC: copy-memory* ( dst src rep -- )
160 M: int-rep copy-register* drop MOV ;
161 M: tagged-rep copy-register* drop MOV ;
163 M: object copy-memory* copy-register* ;
165 : ?spill-slot ( obj -- obj ) dup spill-slot? [ n>> spill@ ] when ;
167 M: x86 %copy ( dst src rep -- )
168 2over eq? [ 3drop ] [
169 [ [ ?spill-slot ] bi@ ] dip
170 2over [ register? ] both? [ copy-register* ] [ copy-memory* ] if
173 : fixnum-overflow ( label dst src1 src2 cc quot -- )
174 swap [ [ int-rep two-operand ] dip call ] dip
180 M: x86 %fixnum-add ( label dst src1 src2 cc -- )
181 [ ADD ] fixnum-overflow ;
183 M: x86 %fixnum-sub ( label dst src1 src2 cc -- )
184 [ SUB ] fixnum-overflow ;
186 M: x86 %fixnum-mul ( label dst src1 src2 cc -- )
187 [ IMUL2 ] fixnum-overflow ;
189 M: x86 %unbox-alien ( dst src -- )
190 alien-offset [+] MOV ;
192 M:: x86 %unbox-any-c-ptr ( dst src -- )
197 src \ f type-number CMP
199 ! Compute tag in dst register
202 ! Is the object an alien?
203 dst alien type-number CMP
204 ! Add an offset to start of byte array's data
205 dst src byte-array-offset [+] LEA
207 ! If so, load the offset and add it to the address
208 dst src alien-offset [+] MOV
212 : alien@ ( reg n -- op ) cells alien type-number - [+] ;
214 M:: x86 %box-alien ( dst src temp -- )
217 dst \ f type-number MOV
220 dst 5 cells alien temp %allot
221 dst 1 alien@ \ f type-number MOV ! base
222 dst 2 alien@ \ f type-number MOV ! expired
223 dst 3 alien@ src MOV ! displacement
224 dst 4 alien@ src MOV ! address
228 :: %box-displaced-alien/f ( dst displacement -- )
229 dst 1 alien@ \ f type-number MOV
230 dst 3 alien@ displacement MOV
231 dst 4 alien@ displacement MOV ;
233 :: %box-displaced-alien/alien ( dst displacement base temp -- )
234 ! Set new alien's base to base.base
235 temp base 1 alien@ MOV
236 dst 1 alien@ temp MOV
238 ! Compute displacement
239 temp base 3 alien@ MOV
240 temp displacement ADD
241 dst 3 alien@ temp MOV
244 temp base 4 alien@ MOV
245 temp displacement ADD
246 dst 4 alien@ temp MOV ;
248 :: %box-displaced-alien/byte-array ( dst displacement base temp -- )
249 dst 1 alien@ base MOV
250 dst 3 alien@ displacement MOV
251 temp base displacement byte-array-offset [++] LEA
252 dst 4 alien@ temp MOV ;
254 :: %box-displaced-alien/dynamic ( dst displacement base temp -- )
256 "not-alien" define-label
260 temp tag-mask get AND
263 temp \ f type-number CMP
266 ! Yes, it is f. Fill in new object
267 dst displacement %box-displaced-alien/f
271 "not-f" resolve-label
274 temp alien type-number CMP
277 dst displacement base temp %box-displaced-alien/alien
282 ! Is base a byte array? It has to be, by now...
283 "not-alien" resolve-label
285 dst displacement base temp %box-displaced-alien/byte-array ;
287 M:: x86 %box-displaced-alien ( dst displacement base temp base-class -- )
291 ! If displacement is zero, return the base
293 displacement displacement TEST
296 ! Displacement is non-zero, we're going to be allocating a new
298 dst 5 cells alien temp %allot
301 dst 2 alien@ \ f type-number MOV
303 dst displacement base temp
305 { [ base-class \ f class<= ] [ 2drop %box-displaced-alien/f ] }
306 { [ base-class \ alien class<= ] [ %box-displaced-alien/alien ] }
307 { [ base-class \ byte-array class<= ] [ %box-displaced-alien/byte-array ] }
308 [ %box-displaced-alien/dynamic ]
314 ! The 'small-reg' mess is pretty crappy, but its only used on x86-32.
315 ! On x86-64, all registers have 8-bit versions. However, a similar
316 ! problem arises for shifts, where the shift count must be in CL, and
317 ! so one day I will fix this properly by adding precoloring to the
318 ! register allocator.
320 HOOK: has-small-reg? cpu ( reg size -- ? )
322 CONSTANT: have-byte-regs { EAX ECX EDX EBX }
324 M: x86.32 has-small-reg?
326 { 8 [ have-byte-regs member-eq? ] }
331 M: x86.64 has-small-reg? 2drop t ;
333 : small-reg-that-isn't ( exclude -- reg' )
334 [ have-byte-regs ] dip
335 [ native-version-of ] map
336 '[ _ member-eq? not ] find nip ;
338 : with-save/restore ( reg quot -- )
339 [ drop PUSH ] [ call ] [ drop POP ] 2tri ; inline
341 :: with-small-register ( dst exclude size quot: ( new-dst -- ) -- )
342 ! If the destination register overlaps a small register with
343 ! 'size' bits, we call the quot with that. Otherwise, we find a
344 ! small register that is not in exclude, and call quot, saving and
345 ! restoring the small register.
346 dst size has-small-reg? [ dst quot call ] [
347 exclude small-reg-that-isn't
348 [ quot call ] with-save/restore
351 :: %alien-integer-getter ( dst exclude address bits quot -- )
352 dst exclude bits [| new-dst |
353 new-dst dup bits n-bit-version-of dup address MOV
355 dst new-dst int-rep %copy
356 ] with-small-register ; inline
358 : %alien-unsigned-getter ( dst exclude address bits -- )
359 [ MOVZX ] %alien-integer-getter ; inline
361 : %alien-signed-getter ( dst exclude address bits -- )
362 [ MOVSX ] %alien-integer-getter ; inline
364 :: %alien-integer-setter ( value exclude address bits -- )
365 value exclude bits [| new-value |
366 new-value value int-rep %copy
367 address new-value bits n-bit-version-of MOV
368 ] with-small-register ; inline
370 : (%memory) ( base displacement scale offset rep c-type -- exclude address rep c-type )
371 [ [ [ 2array ] 2keep ] 2dip <indirect> ] 2dip ;
373 : (%memory-imm) ( base offset rep c-type -- exclude address rep c-type )
374 [ [ drop 1array ] [ [+] ] 2bi ] 2dip ;
376 : (%load-memory) ( dst exclude address rep c-type -- )
379 { c:char [ 8 %alien-signed-getter ] }
380 { c:uchar [ 8 %alien-unsigned-getter ] }
381 { c:short [ 16 %alien-signed-getter ] }
382 { c:ushort [ 16 %alien-unsigned-getter ] }
383 { c:int [ 32 %alien-signed-getter ] }
384 { c:uint [ 32 [ 2drop ] %alien-integer-getter ] }
386 ] [ [ drop ] 2dip %copy ] ?if ;
388 M: x86 %load-memory ( dst base displacement scale offset rep c-type -- )
389 (%memory) (%load-memory) ;
391 M: x86 %load-memory-imm ( dst base offset rep c-type -- )
392 (%memory-imm) (%load-memory) ;
394 : (%store-memory) ( src exclude address rep c-type -- )
397 { c:char [ 8 %alien-integer-setter ] }
398 { c:uchar [ 8 %alien-integer-setter ] }
399 { c:short [ 16 %alien-integer-setter ] }
400 { c:ushort [ 16 %alien-integer-setter ] }
401 { c:int [ 32 %alien-integer-setter ] }
402 { c:uint [ 32 %alien-integer-setter ] }
404 ] [ [ nip swap ] dip %copy ] ?if ;
406 M: x86 %store-memory ( src base displacement scale offset rep c-type -- )
407 (%memory) (%store-memory) ;
409 M: x86 %store-memory-imm ( src base offset rep c-type -- )
410 (%memory-imm) (%store-memory) ;
412 : shift-count? ( reg -- ? ) { ECX RCX } member-eq? ;
414 :: emit-shift ( dst src quot -- )
423 ECX native-version-of [
425 drop dst CL quot call
430 M: x86 %shl int-rep two-operand [ SHL ] emit-shift ;
431 M: x86 %shr int-rep two-operand [ SHR ] emit-shift ;
432 M: x86 %sar int-rep two-operand [ SAR ] emit-shift ;
434 HOOK: %mov-vm-ptr cpu ( reg -- )
436 HOOK: %vm-field-ptr cpu ( reg offset -- )
438 : load-zone-offset ( nursery-ptr -- )
439 "nursery" vm-field-offset %vm-field-ptr ;
441 : load-allot-ptr ( nursery-ptr allot-ptr -- )
442 [ drop load-zone-offset ] [ swap [] MOV ] 2bi ;
444 : inc-allot-ptr ( nursery-ptr n -- )
445 [ [] ] dip data-alignment get align ADD ;
447 : store-header ( temp class -- )
448 [ [] ] [ type-number tag-header ] bi* MOV ;
450 : store-tagged ( dst tag -- )
453 M:: x86 %allot ( dst size class nursery-ptr -- )
454 nursery-ptr dst load-allot-ptr
455 dst class store-header
456 dst class store-tagged
457 nursery-ptr size inc-allot-ptr ;
459 HOOK: %mark-card cpu ( card temp -- )
460 HOOK: %mark-deck cpu ( card temp -- )
462 :: (%write-barrier) ( temp1 temp2 -- )
464 temp1 temp2 %mark-card
465 temp1 deck-bits card-bits - SHR
466 temp1 temp2 %mark-deck ;
468 M:: x86 %write-barrier ( src slot scale tag temp1 temp2 -- )
469 temp1 src slot scale tag (%slot) LEA
470 temp1 temp2 (%write-barrier) ;
472 M:: x86 %write-barrier-imm ( src slot tag temp1 temp2 -- )
473 temp1 src slot tag (%slot-imm) LEA
474 temp1 temp2 (%write-barrier) ;
476 M:: x86 %check-nursery-branch ( label size cc temp1 temp2 -- )
477 temp1 load-zone-offset
480 temp2 temp1 2 cells [+] CMP
482 { cc<= [ label JLE ] }
483 { cc/<= [ label JG ] }
486 M: x86 %alien-global ( dst symbol library -- )
487 [ 0 MOV ] 2dip rc-absolute-cell rel-dlsym ;
489 M: x86 %epilogue ( n -- ) cell - incr-stack-reg ;
491 :: (%boolean) ( dst temp insn -- )
492 dst \ f type-number MOV
493 temp 0 MOV \ t rc-absolute-cell rel-literal
494 dst temp insn execute ; inline
496 : %boolean ( dst cc temp -- )
498 { cc< [ \ CMOVL (%boolean) ] }
499 { cc<= [ \ CMOVLE (%boolean) ] }
500 { cc> [ \ CMOVG (%boolean) ] }
501 { cc>= [ \ CMOVGE (%boolean) ] }
502 { cc= [ \ CMOVE (%boolean) ] }
503 { cc/= [ \ CMOVNE (%boolean) ] }
506 M:: x86 %compare ( dst src1 src2 cc temp -- )
508 dst cc temp %boolean ;
510 M:: x86 %test ( dst src1 src2 cc temp -- )
512 dst cc temp %boolean ;
514 : (%compare-tagged) ( src1 src2 -- )
515 [ HEX: ffffffff CMP ] dip rc-absolute rel-literal ;
517 M:: x86 %compare-integer-imm ( dst src1 src2 cc temp -- )
519 dst cc temp %boolean ;
521 M:: x86 %test-imm ( dst src1 src2 cc temp -- )
523 dst cc temp %boolean ;
525 : (%compare-imm) ( src1 src2 -- )
527 { [ dup fixnum? ] [ tag-fixnum CMP ] }
528 { [ dup not ] [ drop \ f type-number CMP ] }
529 [ (%compare-tagged) ]
532 M:: x86 %compare-imm ( dst src1 src2 cc temp -- )
533 src1 src2 (%compare-imm)
534 dst cc temp %boolean ;
536 : %branch ( label cc -- )
546 M:: x86 %compare-branch ( label src1 src2 cc -- )
550 M:: x86 %compare-integer-imm-branch ( label src1 src2 cc -- )
554 M:: x86 %test-branch ( label src1 src2 cc -- )
558 M:: x86 %test-imm-branch ( label src1 src2 cc -- )
562 M:: x86 %compare-imm-branch ( label src1 src2 cc -- )
563 src1 src2 (%compare-imm)
566 M:: x86 %spill ( src rep dst -- )
569 M:: x86 %reload ( dst rep src -- )
572 M:: x86 %store-stack-param ( src n rep -- )
573 n reserved-stack-space + stack@ src rep %copy ;
575 : %load-return ( dst rep -- )
576 [ reg-class-of return-regs at first ] keep %load-reg-param ;
578 : %store-return ( dst rep -- )
579 [ reg-class-of return-regs at first ] keep %store-reg-param ;
581 : next-stack@ ( n -- operand )
582 #! nth parameter from the next stack frame. Used to box
583 #! input values to callbacks; the callback has its own
584 #! stack frame set up, and we want to read the frame
585 #! set up by the caller.
586 [ frame-reg ] dip 2 cells + reserved-stack-space + [+] ;
588 M:: x86 %load-stack-param ( dst n rep -- )
589 dst n next-stack@ rep %copy ;
591 M:: x86 %local-allot ( dst size align offset -- )
592 dst offset local-allot-offset special-offset stack@ LEA ;
594 M: x86 %alien-indirect ( src -- )
597 M: x86 %loop-entry 16 alignment [ NOP ] times ;
599 M:: x86 %restore-context ( temp1 temp2 -- )
600 #! Load Factor stack pointers on entry from C to Factor.
602 temp2 stack-reg cell neg [+] LEA
603 temp1 "callstack-top" context-field-offset [+] temp2 MOV
604 ds-reg temp1 "datastack" context-field-offset [+] MOV
605 rs-reg temp1 "retainstack" context-field-offset [+] MOV ;
607 M:: x86 %save-context ( temp1 temp2 -- )
608 #! Save Factor stack pointers in case the C code calls a
609 #! callback which does a GC, which must reliably trace
612 temp2 stack-reg cell neg [+] LEA
613 temp1 "callstack-top" context-field-offset [+] temp2 MOV
614 temp1 "datastack" context-field-offset [+] ds-reg MOV
615 temp1 "retainstack" context-field-offset [+] rs-reg MOV ;
617 M: x86 value-struct? drop t ;
619 M: x86 immediate-arithmetic? ( n -- ? )
620 HEX: -80000000 HEX: 7fffffff between? ;
622 M: x86 immediate-bitwise? ( n -- ? )
623 HEX: -80000000 HEX: 7fffffff between? ;
625 : %cmov-float= ( dst src -- )
627 "no-move" define-label
629 "no-move" get [ JNE ] [ JP ] bi
631 "no-move" resolve-label
634 : %cmov-float/= ( dst src -- )
636 "no-move" define-label
643 "no-move" resolve-label
646 :: (%compare-float) ( dst src1 src2 cc temp compare -- )
648 { cc< [ src2 src1 \ compare call( a b -- ) dst temp \ CMOVA (%boolean) ] }
649 { cc<= [ src2 src1 \ compare call( a b -- ) dst temp \ CMOVAE (%boolean) ] }
650 { cc> [ src1 src2 \ compare call( a b -- ) dst temp \ CMOVA (%boolean) ] }
651 { cc>= [ src1 src2 \ compare call( a b -- ) dst temp \ CMOVAE (%boolean) ] }
652 { cc= [ src1 src2 \ compare call( a b -- ) dst temp \ %cmov-float= (%boolean) ] }
653 { cc<> [ src1 src2 \ compare call( a b -- ) dst temp \ CMOVNE (%boolean) ] }
654 { cc<>= [ src1 src2 \ compare call( a b -- ) dst temp \ CMOVNP (%boolean) ] }
655 { cc/< [ src2 src1 \ compare call( a b -- ) dst temp \ CMOVBE (%boolean) ] }
656 { cc/<= [ src2 src1 \ compare call( a b -- ) dst temp \ CMOVB (%boolean) ] }
657 { cc/> [ src1 src2 \ compare call( a b -- ) dst temp \ CMOVBE (%boolean) ] }
658 { cc/>= [ src1 src2 \ compare call( a b -- ) dst temp \ CMOVB (%boolean) ] }
659 { cc/= [ src1 src2 \ compare call( a b -- ) dst temp \ %cmov-float/= (%boolean) ] }
660 { cc/<> [ src1 src2 \ compare call( a b -- ) dst temp \ CMOVE (%boolean) ] }
661 { cc/<>= [ src1 src2 \ compare call( a b -- ) dst temp \ CMOVP (%boolean) ] }
664 : %jump-float= ( label -- )
666 "no-jump" define-label
669 "no-jump" resolve-label
672 : %jump-float/= ( label -- )
675 :: (%compare-float-branch) ( label src1 src2 cc compare -- )
677 { cc< [ src2 src1 \ compare call( a b -- ) label JA ] }
678 { cc<= [ src2 src1 \ compare call( a b -- ) label JAE ] }
679 { cc> [ src1 src2 \ compare call( a b -- ) label JA ] }
680 { cc>= [ src1 src2 \ compare call( a b -- ) label JAE ] }
681 { cc= [ src1 src2 \ compare call( a b -- ) label %jump-float= ] }
682 { cc<> [ src1 src2 \ compare call( a b -- ) label JNE ] }
683 { cc<>= [ src1 src2 \ compare call( a b -- ) label JNP ] }
684 { cc/< [ src2 src1 \ compare call( a b -- ) label JBE ] }
685 { cc/<= [ src2 src1 \ compare call( a b -- ) label JB ] }
686 { cc/> [ src1 src2 \ compare call( a b -- ) label JBE ] }
687 { cc/>= [ src1 src2 \ compare call( a b -- ) label JB ] }
688 { cc/= [ src1 src2 \ compare call( a b -- ) label %jump-float/= ] }
689 { cc/<> [ src1 src2 \ compare call( a b -- ) label JE ] }
690 { cc/<>= [ src1 src2 \ compare call( a b -- ) label JP ] }
697 "Checking for multimedia extensions... " write flush
698 [ { (sse-version) } compile ] with-optimizer
700 [ sse-string " detected" append print ]
701 [ 20 < "cpu.x86.x87" "cpu.x86.sse" ? require ] bi ;