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 HOOK: small-regs cpu ( -- regs )
269 M: x86.32 small-regs { EAX ECX EDX EBX } ;
270 M: x86.64 small-regs { RAX RCX RDX RBX } ;
272 HOOK: small-reg-native cpu ( reg -- reg' )
274 M: x86.32 small-reg-native small-reg-4 ;
275 M: x86.64 small-reg-native small-reg-8 ;
277 : small-reg-that-isn't ( exclude -- reg' )
278 small-regs swap [ native-version-of ] map '[ _ memq? not ] find nip ;
280 : with-save/restore ( reg quot -- )
281 [ drop PUSH ] [ call ] [ drop POP ] 2tri ; inline
283 :: with-small-register ( dst exclude quot: ( new-dst -- ) -- )
284 #! If the destination register overlaps a small register, we
285 #! call the quot with that. Otherwise, we find a small
286 #! register that is not in exclude, and call quot, saving
287 #! and restoring the small register.
288 dst small-regs memq? [ dst quot call ] [
289 exclude small-reg-that-isn't
290 [ quot call ] with-save/restore
293 : shift-count? ( reg -- ? ) { ECX RCX } memq? ;
295 :: emit-shift ( dst src1 src2 quot -- )
304 ECX native-version-of [
306 drop dst CL quot call
311 M: x86 %shl [ SHL ] emit-shift ;
312 M: x86 %shr [ SHR ] emit-shift ;
313 M: x86 %sar [ SAR ] emit-shift ;
315 M:: x86 %string-nth ( dst src index temp -- )
317 dst { src index temp } [| new-dst |
318 ! Load the least significant 7 bits into new-dst.
319 ! 8th bit indicates whether we have to load from
320 ! the aux vector or not.
321 temp src index [+] LEA
322 new-dst 8-bit-version-of temp string-offset [+] MOV
323 new-dst new-dst 8-bit-version-of MOVZX
324 ! Do we have to look at the aux vector?
327 ! Yes, this is a non-ASCII character. Load aux vector
328 temp src string-aux-offset [+] MOV
334 new-dst 16-bit-version-of new-dst byte-array-offset [+] MOV
335 new-dst new-dst 16-bit-version-of MOVZX
341 ] with-small-register ;
343 M:: x86 %set-string-nth-fast ( ch str index temp -- )
344 ch { index str temp } [| new-ch |
346 temp str index [+] LEA
347 temp string-offset [+] new-ch 8-bit-version-of MOV
348 ] with-small-register ;
350 :: %alien-integer-getter ( dst src size quot -- )
351 dst { src } [| new-dst |
352 new-dst dup size 8 * n-bit-version-of dup src [] MOV
355 ] with-small-register ; inline
357 : %alien-unsigned-getter ( dst src size -- )
358 [ MOVZX ] %alien-integer-getter ; inline
360 M: x86 %alien-unsigned-1 1 %alien-unsigned-getter ;
361 M: x86 %alien-unsigned-2 2 %alien-unsigned-getter ;
363 : %alien-signed-getter ( dst src size -- )
364 [ MOVSX ] %alien-integer-getter ; inline
366 M: x86 %alien-signed-1 1 %alien-signed-getter ;
367 M: x86 %alien-signed-2 2 %alien-signed-getter ;
368 M: x86 %alien-signed-4 4 %alien-signed-getter ;
370 M: x86 %alien-unsigned-4 4 [ 2drop ] %alien-integer-getter ;
372 M: x86 %alien-cell [] MOV ;
373 M: x86 %alien-float dupd [] MOVSS dup CVTSS2SD ;
374 M: x86 %alien-double [] MOVSD ;
376 :: %alien-integer-setter ( ptr value size -- )
377 value { ptr } [| new-value |
379 ptr [] new-value size 8 * n-bit-version-of MOV
380 ] with-small-register ; inline
382 M: x86 %set-alien-integer-1 1 %alien-integer-setter ;
383 M: x86 %set-alien-integer-2 2 %alien-integer-setter ;
384 M: x86 %set-alien-integer-4 4 %alien-integer-setter ;
385 M: x86 %set-alien-cell [ [] ] dip MOV ;
386 M: x86 %set-alien-float dup dup CVTSD2SS [ [] ] dip MOVSS ;
387 M: x86 %set-alien-double [ [] ] dip MOVSD ;
389 : load-zone-ptr ( reg -- )
390 #! Load pointer to start of zone array
391 0 MOV "nursery" f rc-absolute-cell rel-dlsym ;
393 : load-allot-ptr ( nursery-ptr allot-ptr -- )
394 [ drop load-zone-ptr ] [ swap cell [+] MOV ] 2bi ;
396 : inc-allot-ptr ( nursery-ptr n -- )
397 [ cell [+] ] dip 8 align ADD ;
399 : store-header ( temp class -- )
400 [ [] ] [ type-number tag-fixnum ] bi* MOV ;
402 : store-tagged ( dst tag -- )
405 M:: x86 %allot ( dst size class nursery-ptr -- )
406 nursery-ptr dst load-allot-ptr
407 dst class store-header
408 dst class store-tagged
409 nursery-ptr size inc-allot-ptr ;
411 M:: x86 %write-barrier ( src card# table -- )
412 #! Mark the card pointed to by vreg.
416 table "cards_offset" f %alien-global
418 table card# [+] card-mark <byte> MOV
421 card# deck-bits card-bits - SHR
422 table "decks_offset" f %alien-global
424 table card# [+] card-mark <byte> MOV ;
426 :: check-nursery ( temp1 temp2 -- )
428 temp2 temp1 cell [+] MOV
430 temp1 temp1 3 cells [+] MOV
433 GENERIC# save-gc-root 1 ( gc-root operand temp -- )
435 M:: spill-slot save-gc-root ( gc-root spill-slot temp -- )
436 temp spill-slot n>> spill-integer@ MOV
437 gc-root gc-root@ temp MOV ;
439 M:: word save-gc-root ( gc-root register temp -- )
440 gc-root gc-root@ register MOV ;
442 : save-gc-roots ( gc-roots temp -- )
443 '[ _ save-gc-root ] assoc-each ;
445 GENERIC# load-gc-root 1 ( gc-root operand temp -- )
447 M:: spill-slot load-gc-root ( gc-root spill-slot temp -- )
448 temp gc-root gc-root@ MOV
449 spill-slot n>> spill-integer@ temp MOV ;
451 M:: word load-gc-root ( gc-root register temp -- )
452 register gc-root gc-root@ MOV ;
454 : load-gc-roots ( gc-roots temp -- )
455 '[ _ load-gc-root ] assoc-each ;
457 :: call-gc ( gc-root-count -- )
458 ! Pass pointer to start of GC roots as first parameter
459 param-reg-1 gc-root-base param@ LEA
460 ! Pass number of roots as second parameter
461 param-reg-2 gc-root-count MOV
463 %prepare-alien-invoke
464 "inline_gc" f %alien-invoke ;
466 M:: x86 %gc ( temp1 temp2 gc-roots gc-root-count -- )
468 temp1 temp2 check-nursery
470 gc-roots temp1 save-gc-roots
471 gc-root-count call-gc
472 gc-roots temp1 load-gc-roots
473 "end" resolve-label ;
476 [ 0 MOV ] 2dip rc-absolute-cell rel-dlsym ;
478 M: x86 %epilogue ( n -- ) cell - incr-stack-reg ;
480 :: %boolean ( dst temp word -- )
481 dst \ f tag-number MOV
482 temp 0 MOV \ t rc-absolute-cell rel-immediate
483 dst temp word execute ; inline
485 M: x86 %compare ( dst temp cc src1 src2 -- )
487 { cc< [ \ CMOVL %boolean ] }
488 { cc<= [ \ CMOVLE %boolean ] }
489 { cc> [ \ CMOVG %boolean ] }
490 { cc>= [ \ CMOVGE %boolean ] }
491 { cc= [ \ CMOVE %boolean ] }
492 { cc/= [ \ CMOVNE %boolean ] }
495 M: x86 %compare-imm ( dst temp cc src1 src2 -- )
498 M: x86 %compare-float ( dst temp cc src1 src2 -- )
500 { cc< [ \ CMOVB %boolean ] }
501 { cc<= [ \ CMOVBE %boolean ] }
502 { cc> [ \ CMOVA %boolean ] }
503 { cc>= [ \ CMOVAE %boolean ] }
504 { cc= [ \ CMOVE %boolean ] }
505 { cc/= [ \ CMOVNE %boolean ] }
508 M: x86 %compare-branch ( label cc src1 src2 -- )
518 M: x86 %compare-imm-branch ( label src1 src2 cc -- )
521 M: x86 %compare-float-branch ( label cc src1 src2 -- )
531 M: x86 %spill-integer ( src n -- ) spill-integer@ swap MOV ;
532 M: x86 %reload-integer ( dst n -- ) spill-integer@ MOV ;
534 M: x86 %spill-float ( src n -- ) spill-float@ swap MOVSD ;
535 M: x86 %reload-float ( dst n -- ) spill-float@ MOVSD ;
537 M: x86 %loop-entry 16 code-alignment [ NOP ] times ;
539 M: int-regs %save-param-reg drop [ param@ ] dip MOV ;
540 M: int-regs %load-param-reg drop swap param@ MOV ;
542 GENERIC: MOVSS/D ( dst src reg-class -- )
544 M: single-float-regs MOVSS/D drop MOVSS ;
545 M: double-float-regs MOVSS/D drop MOVSD ;
547 M: float-regs %save-param-reg [ param@ ] 2dip MOVSS/D ;
548 M: float-regs %load-param-reg [ swap param@ ] dip MOVSS/D ;
550 GENERIC: push-return-reg ( reg-class -- )
551 GENERIC: load-return-reg ( n reg-class -- )
552 GENERIC: store-return-reg ( n reg-class -- )
554 M: x86 %prepare-alien-invoke
555 #! Save Factor stack pointers in case the C code calls a
556 #! callback which does a GC, which must reliably trace
558 temp-reg-1 "stack_chain" f %alien-global
559 temp-reg-1 temp-reg-1 [] MOV
560 temp-reg-1 [] stack-reg MOV
561 temp-reg-1 [] cell SUB
562 temp-reg-1 2 cells [+] ds-reg MOV
563 temp-reg-1 3 cells [+] rs-reg MOV ;
565 M: x86 value-struct? drop t ;
567 M: x86 small-enough? ( n -- ? )
568 HEX: -80000000 HEX: 7fffffff between? ;
570 : next-stack@ ( n -- operand )
571 #! nth parameter from the next stack frame. Used to box
572 #! input values to callbacks; the callback has its own
573 #! stack frame set up, and we want to read the frame
574 #! set up by the caller.
575 stack-frame get total-size>> + stack@ ;