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-integer@ ( n -- op ) spill-integer-offset param@ ;
35 : spill-float@ ( n -- op ) spill-float-offset param@ ;
37 : gc-root@ ( n -- op ) gc-root-offset param@ ;
39 : decr-stack-reg ( n -- )
40 dup 0 = [ drop ] [ stack-reg swap SUB ] if ;
42 : incr-stack-reg ( n -- )
43 dup 0 = [ drop ] [ stack-reg swap ADD ] if ;
45 : align-stack ( n -- n' )
46 os macosx? cpu x86.64? or [ 16 align ] when ;
48 M: x86 stack-frame-size ( stack-frame -- i )
49 (stack-frame-size) 3 cells reserved-area-size + + align-stack ;
51 HOOK: temp-reg-1 cpu ( -- reg )
52 HOOK: temp-reg-2 cpu ( -- reg )
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 ;
126 M: x86 %not drop NOT ;
129 : ?MOV ( dst src -- )
130 2dup = [ 2drop ] [ MOV ] if ; inline
132 :: overflow-template ( label dst src1 src2 insn -- )
136 M: x86 %fixnum-add ( label dst src1 src2 -- )
137 [ ADD ] overflow-template ;
139 M: x86 %fixnum-sub ( label dst src1 src2 -- )
140 [ SUB ] overflow-template ;
142 M: x86 %fixnum-mul ( label dst src1 src2 -- )
143 [ swap IMUL2 ] overflow-template ;
145 : bignum@ ( reg n -- op )
146 cells bignum tag-number - [+] ; inline
148 M:: x86 %integer>bignum ( dst src temp -- )
149 #! on entry, inreg is a signed 32-bit quantity
150 #! exits with tagged ptr to bignum in outreg
151 #! 1 cell header, 1 cell length, 1 cell sign, + digits
152 #! length is the # of digits + sign
155 ! Load cached zero value
156 dst 0 >bignum %load-reference
158 ! Is it zero? Then just go to the end and return this zero
161 dst 4 cells bignum temp %allot
163 dst 1 bignum@ 2 tag-fixnum MOV
165 dst 3 bignum@ src MOV
168 temp cell-bits 1- SAR
171 dst 2 bignum@ temp MOV
172 ! Make negative value positive
178 dst 3 bignum@ temp MOV
182 M:: x86 %bignum>integer ( dst src temp -- )
186 temp src 1 bignum@ MOV
187 ! if the length is 1, its just the sign and nothing else,
190 temp 1 tag-fixnum CMP
193 dst src 3 bignum@ MOV
195 temp src 2 bignum@ MOV
196 ! convert it into -1 or 1
205 M: x86 %add-float nip ADDSD ;
206 M: x86 %sub-float nip SUBSD ;
207 M: x86 %mul-float nip MULSD ;
208 M: x86 %div-float nip DIVSD ;
210 M: x86 %integer>float CVTSI2SD ;
211 M: x86 %float>integer CVTTSD2SI ;
213 M: x86 %copy ( dst src -- ) ?MOV ;
215 M: x86 %copy-float ( dst src -- )
216 2dup = [ 2drop ] [ MOVSD ] if ;
218 M: x86 %unbox-float ( dst src -- )
219 float-offset [+] MOVSD ;
221 M:: x86 %unbox-any-c-ptr ( dst src temp -- )
223 { "is-byte-array" "end" "start" } [ define-label ] each
226 ! We come back here with displaced aliens
227 "start" resolve-label
229 temp \ f tag-number CMP
231 ! Is the object an alien?
232 temp header-offset [+] alien type-number tag-fixnum CMP
233 "is-byte-array" get JNE
234 ! If so, load the offset and add it to the address
235 dst temp alien-offset [+] ADD
236 ! Now recurse on the underlying alien
237 temp temp underlying-alien-offset [+] MOV
239 "is-byte-array" resolve-label
240 ! Add byte array address to address being computed
242 ! Add an offset to start of byte array's data
243 dst byte-array-offset ADD
247 M:: x86 %box-float ( dst src temp -- )
248 dst 16 float temp %allot
249 dst float-offset [+] src MOVSD ;
251 : alien@ ( reg n -- op ) cells alien tag-number - [+] ;
253 M:: x86 %box-alien ( dst src temp -- )
256 dst \ f tag-number MOV
259 dst 4 cells alien temp %allot
260 dst 1 alien@ \ f tag-number MOV
261 dst 2 alien@ \ f tag-number MOV
262 ! Store src in alien-offset slot
267 ! The 'small-reg' mess is pretty crappy, but its only used on x86-32.
268 ! On x86-64, all registers have 8-bit versions. However, a similar
269 ! problem arises for shifts, where the shift count must be in CL, and
270 ! so one day I will fix this properly by adding precoloring to the
271 ! register allocator.
273 HOOK: has-small-reg? cpu ( reg size -- ? )
275 CONSTANT: have-byte-regs { EAX ECX EDX EBX }
277 M: x86.32 has-small-reg?
279 { 8 [ have-byte-regs memq? ] }
284 M: x86.64 has-small-reg? drop t ;
286 : small-reg-that-isn't ( exclude -- reg' )
287 [ have-byte-regs ] dip
288 [ native-version-of ] map
289 '[ _ memq? not ] find nip ;
291 : with-save/restore ( reg quot -- )
292 [ drop PUSH ] [ call ] [ drop POP ] 2tri ; inline
294 :: with-small-register ( dst exclude size quot: ( new-dst -- ) -- )
295 ! If the destination register overlaps a small register with
296 ! 'size' bits, we call the quot with that. Otherwise, we find a
297 ! small register that is not in exclude, and call quot, saving and
298 ! restoring the small register.
299 dst size has-small-reg? [ dst quot call ] [
300 exclude small-reg-that-isn't
301 [ quot call ] with-save/restore
304 M:: x86 %string-nth ( dst src index temp -- )
305 ! We request a small-reg of size 8 since those of size 16 are
308 dst { src index temp } 8 [| new-dst |
309 ! Load the least significant 7 bits into new-dst.
310 ! 8th bit indicates whether we have to load from
311 ! the aux vector or not.
312 temp src index [+] LEA
313 new-dst 8-bit-version-of temp string-offset [+] MOV
314 new-dst new-dst 8-bit-version-of MOVZX
315 ! Do we have to look at the aux vector?
318 ! Yes, this is a non-ASCII character. Load aux vector
319 temp src string-aux-offset [+] MOV
325 new-dst 16-bit-version-of new-dst byte-array-offset [+] MOV
326 new-dst new-dst 16-bit-version-of MOVZX
332 ] with-small-register ;
334 M:: x86 %set-string-nth-fast ( ch str index temp -- )
335 ch { index str temp } 8 [| new-ch |
337 temp str index [+] LEA
338 temp string-offset [+] new-ch 8-bit-version-of MOV
339 ] with-small-register ;
341 :: %alien-integer-getter ( dst src size quot -- )
342 dst { src } size [| new-dst |
343 new-dst dup size n-bit-version-of dup src [] MOV
346 ] with-small-register ; inline
348 : %alien-unsigned-getter ( dst src size -- )
349 [ MOVZX ] %alien-integer-getter ; inline
351 M: x86 %alien-unsigned-1 8 %alien-unsigned-getter ;
352 M: x86 %alien-unsigned-2 16 %alien-unsigned-getter ;
353 M: x86 %alien-unsigned-4 32 [ 2drop ] %alien-integer-getter ;
355 : %alien-signed-getter ( dst src size -- )
356 [ MOVSX ] %alien-integer-getter ; inline
358 M: x86 %alien-signed-1 8 %alien-signed-getter ;
359 M: x86 %alien-signed-2 16 %alien-signed-getter ;
360 M: x86 %alien-signed-4 32 %alien-signed-getter ;
362 M: x86 %alien-cell [] MOV ;
363 M: x86 %alien-float dupd [] MOVSS dup CVTSS2SD ;
364 M: x86 %alien-double [] MOVSD ;
366 :: %alien-integer-setter ( ptr value size -- )
367 value { ptr } size [| new-value |
369 ptr [] new-value size n-bit-version-of MOV
370 ] with-small-register ; inline
372 M: x86 %set-alien-integer-1 8 %alien-integer-setter ;
373 M: x86 %set-alien-integer-2 16 %alien-integer-setter ;
374 M: x86 %set-alien-integer-4 32 %alien-integer-setter ;
375 M: x86 %set-alien-cell [ [] ] dip MOV ;
376 M: x86 %set-alien-float dup dup CVTSD2SS [ [] ] dip MOVSS ;
377 M: x86 %set-alien-double [ [] ] dip MOVSD ;
379 : shift-count? ( reg -- ? ) { ECX RCX } memq? ;
381 :: emit-shift ( dst src1 src2 quot -- )
390 ECX native-version-of [
392 drop dst CL quot call
397 M: x86 %shl [ SHL ] emit-shift ;
398 M: x86 %shr [ SHR ] emit-shift ;
399 M: x86 %sar [ SAR ] emit-shift ;
401 : load-zone-ptr ( reg -- )
402 #! Load pointer to start of zone array
403 0 MOV "nursery" f rc-absolute-cell rel-dlsym ;
405 : load-allot-ptr ( nursery-ptr allot-ptr -- )
406 [ drop load-zone-ptr ] [ swap cell [+] MOV ] 2bi ;
408 : inc-allot-ptr ( nursery-ptr n -- )
409 [ cell [+] ] dip 8 align ADD ;
411 : store-header ( temp class -- )
412 [ [] ] [ type-number tag-fixnum ] bi* MOV ;
414 : store-tagged ( dst tag -- )
417 M:: x86 %allot ( dst size class nursery-ptr -- )
418 nursery-ptr dst load-allot-ptr
419 dst class store-header
420 dst class store-tagged
421 nursery-ptr size inc-allot-ptr ;
423 M:: x86 %write-barrier ( src card# table -- )
424 #! Mark the card pointed to by vreg.
428 table "cards_offset" f %alien-global
430 table card# [+] card-mark <byte> MOV
433 card# deck-bits card-bits - SHR
434 table "decks_offset" f %alien-global
436 table card# [+] card-mark <byte> MOV ;
438 :: check-nursery ( temp1 temp2 -- )
440 temp2 temp1 cell [+] MOV
442 temp1 temp1 3 cells [+] MOV
445 GENERIC# save-gc-root 1 ( gc-root operand temp -- )
447 M:: spill-slot save-gc-root ( gc-root spill-slot temp -- )
448 temp spill-slot n>> spill-integer@ MOV
449 gc-root gc-root@ temp MOV ;
451 M:: word save-gc-root ( gc-root register temp -- )
452 gc-root gc-root@ register MOV ;
454 : save-gc-roots ( gc-roots temp -- )
455 '[ _ save-gc-root ] assoc-each ;
457 GENERIC# load-gc-root 1 ( gc-root operand temp -- )
459 M:: spill-slot load-gc-root ( gc-root spill-slot temp -- )
460 temp gc-root gc-root@ MOV
461 spill-slot n>> spill-integer@ temp MOV ;
463 M:: word load-gc-root ( gc-root register temp -- )
464 register gc-root gc-root@ MOV ;
466 : load-gc-roots ( gc-roots temp -- )
467 '[ _ load-gc-root ] assoc-each ;
469 :: call-gc ( gc-root-count -- )
470 ! Pass pointer to start of GC roots as first parameter
471 param-reg-1 gc-root-base param@ LEA
472 ! Pass number of roots as second parameter
473 param-reg-2 gc-root-count MOV
475 %prepare-alien-invoke
476 "inline_gc" f %alien-invoke ;
478 M:: x86 %gc ( temp1 temp2 gc-roots gc-root-count -- )
480 temp1 temp2 check-nursery
482 gc-roots temp1 save-gc-roots
483 gc-root-count call-gc
484 gc-roots temp1 load-gc-roots
485 "end" resolve-label ;
488 [ 0 MOV ] 2dip rc-absolute-cell rel-dlsym ;
490 M: x86 %epilogue ( n -- ) cell - incr-stack-reg ;
492 :: %boolean ( dst temp word -- )
493 dst \ f tag-number MOV
494 temp 0 MOV \ t rc-absolute-cell rel-immediate
495 dst temp word execute ; inline
497 M: x86 %compare ( dst temp cc src1 src2 -- )
499 { cc< [ \ CMOVL %boolean ] }
500 { cc<= [ \ CMOVLE %boolean ] }
501 { cc> [ \ CMOVG %boolean ] }
502 { cc>= [ \ CMOVGE %boolean ] }
503 { cc= [ \ CMOVE %boolean ] }
504 { cc/= [ \ CMOVNE %boolean ] }
507 M: x86 %compare-imm ( dst temp cc src1 src2 -- )
510 M: x86 %compare-float ( dst temp cc src1 src2 -- )
512 { cc< [ \ CMOVB %boolean ] }
513 { cc<= [ \ CMOVBE %boolean ] }
514 { cc> [ \ CMOVA %boolean ] }
515 { cc>= [ \ CMOVAE %boolean ] }
516 { cc= [ \ CMOVE %boolean ] }
517 { cc/= [ \ CMOVNE %boolean ] }
520 M: x86 %compare-branch ( label cc src1 src2 -- )
530 M: x86 %compare-imm-branch ( label src1 src2 cc -- )
533 M: x86 %compare-float-branch ( label cc src1 src2 -- )
543 M: x86 %spill-integer ( src n -- ) spill-integer@ swap MOV ;
544 M: x86 %reload-integer ( dst n -- ) spill-integer@ MOV ;
546 M: x86 %spill-float ( src n -- ) spill-float@ swap MOVSD ;
547 M: x86 %reload-float ( dst n -- ) spill-float@ MOVSD ;
549 M: x86 %loop-entry 16 code-alignment [ NOP ] times ;
551 M: int-regs %save-param-reg drop [ param@ ] dip MOV ;
552 M: int-regs %load-param-reg drop swap param@ MOV ;
554 GENERIC: MOVSS/D ( dst src reg-class -- )
556 M: single-float-regs MOVSS/D drop MOVSS ;
557 M: double-float-regs MOVSS/D drop MOVSD ;
559 M: float-regs %save-param-reg [ param@ ] 2dip MOVSS/D ;
560 M: float-regs %load-param-reg [ swap param@ ] dip MOVSS/D ;
562 GENERIC: push-return-reg ( reg-class -- )
563 GENERIC: load-return-reg ( n reg-class -- )
564 GENERIC: store-return-reg ( n reg-class -- )
566 M: x86 %prepare-alien-invoke
567 #! Save Factor stack pointers in case the C code calls a
568 #! callback which does a GC, which must reliably trace
570 temp-reg-1 "stack_chain" f %alien-global
571 temp-reg-1 temp-reg-1 [] MOV
572 temp-reg-1 [] stack-reg MOV
573 temp-reg-1 [] cell SUB
574 temp-reg-1 2 cells [+] ds-reg MOV
575 temp-reg-1 3 cells [+] rs-reg MOV ;
577 M: x86 value-struct? drop t ;
579 M: x86 small-enough? ( n -- ? )
580 HEX: -80000000 HEX: 7fffffff between? ;
582 : next-stack@ ( n -- operand )
583 #! nth parameter from the next stack frame. Used to box
584 #! input values to callbacks; the callback has its own
585 #! stack frame set up, and we want to read the frame
586 #! set up by the caller.
587 stack-frame get total-size>> + stack@ ;