1 ! Copyright (C) 2005, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs sequences kernel combinators make math
4 math.order math.ranges system namespaces locals layouts words
5 alien alien.accessors alien.c-types literals cpu.architecture
6 cpu.ppc.assembler cpu.ppc.assembler.backend compiler.cfg.registers
7 compiler.cfg.instructions compiler.cfg.comparisons
8 compiler.codegen.fixup compiler.cfg.intrinsics
9 compiler.cfg.stack-frame compiler.cfg.build-stack-frame
10 compiler.units compiler.constants compiler.codegen ;
11 FROM: cpu.ppc.assembler => B ;
14 ! PowerPC register assignments:
15 ! r2-r12: integer vregs
17 ! r30: integer scratch
21 ! Add some methods to the assembler that are useful to us
22 M: label (B) [ 0 ] 2dip (B) rc-relative-ppc-3 label-fixup ;
23 M: label BC [ 0 BC ] dip rc-relative-ppc-2 label-fixup ;
25 enable-float-intrinsics
28 \ ##integer>float t frame-required? set-word-prop
29 \ ##float>integer t frame-required? set-word-prop
32 M: ppc machine-registers
34 { int-regs $[ 2 12 [a,b] 15 29 [a,b] append ] }
35 { float-regs $[ 0 29 [a,b] ] }
38 CONSTANT: scratch-reg 30
39 CONSTANT: fp-scratch-reg 30
41 M: ppc two-operand? f ;
43 M: ppc %load-immediate ( reg n -- ) swap LOAD ;
45 M: ppc %load-reference ( reg obj -- )
46 [ 0 swap LOAD32 ] [ rc-absolute-ppc-2/2 rel-immediate ] bi* ;
48 M: ppc %alien-global ( register symbol dll -- )
49 [ 0 swap LOAD32 ] 2dip rc-absolute-ppc-2/2 rel-dlsym ;
54 GENERIC: loc-reg ( loc -- reg )
56 M: ds-loc loc-reg drop ds-reg ;
57 M: rs-loc loc-reg drop rs-reg ;
59 : loc>operand ( loc -- reg n )
60 [ loc-reg ] [ n>> cells neg ] bi ; inline
62 M: ppc %peek loc>operand LWZ ;
63 M: ppc %replace loc>operand STW ;
65 : (%inc) ( n reg -- ) dup rot cells ADDI ; inline
67 M: ppc %inc-d ( n -- ) ds-reg (%inc) ;
68 M: ppc %inc-r ( n -- ) rs-reg (%inc) ;
70 HOOK: reserved-area-size os ( -- n )
72 ! The start of the stack frame contains the size of this frame
73 ! as well as the currently executing XT
74 : factor-area-size ( -- n ) 2 cells ; foldable
75 : next-save ( n -- i ) cell - ;
76 : xt-save ( n -- i ) 2 cells - ;
78 ! Next, we have the spill area as well as the FFI parameter area.
79 ! It is safe for them to overlap, since basic blocks with FFI calls
80 ! will never spill -- indeed, basic blocks with FFI calls do not
81 ! use vregs at all, and the FFI call is a stack analysis sync point.
82 ! In the future this will change and the stack frame logic will
83 ! need to be untangled somewhat.
85 : param@ ( n -- x ) reserved-area-size + ; inline
87 : param-save-size ( -- n ) 8 cells ; foldable
90 reserved-area-size param-save-size + + ; inline
92 : spill@ ( n -- offset )
95 ! Some FP intrinsics need a temporary scratch area in the stack
96 ! frame, 8 bytes in size. This is in the param-save area so it
97 ! does not overlap with spill slots.
98 : scratch@ ( n -- offset )
99 stack-frame get total-size>>
105 : gc-root@ ( n -- offset )
106 gc-root-offset local@ ;
108 ! Finally we have the linkage area
109 HOOK: lr-save os ( -- n )
111 M: ppc stack-frame-size ( stack-frame -- i )
118 M: ppc %call ( word -- ) 0 BL rc-relative-ppc-3 rel-word-pic ;
120 M: ppc %jump ( word -- )
121 0 6 LOAD32 8 rc-absolute-ppc-2/2 rel-here
122 0 B rc-relative-ppc-3 rel-word-pic-tail ;
124 M: ppc %jump-label ( label -- ) B ;
125 M: ppc %return ( -- ) BLR ;
127 M:: ppc %dispatch ( src temp -- )
129 4 cells rc-absolute-ppc-2/2 rel-here
134 :: (%slot) ( obj slot tag temp -- reg offset )
136 temp tag neg ; inline
138 : (%slot-imm) ( obj slot tag -- reg offset )
139 [ cells ] dip - ; inline
141 M: ppc %slot ( dst obj slot tag temp -- ) (%slot) LWZ ;
142 M: ppc %slot-imm ( dst obj slot tag -- ) (%slot-imm) LWZ ;
143 M: ppc %set-slot ( src obj slot tag temp -- ) (%slot) STW ;
144 M: ppc %set-slot-imm ( src obj slot tag -- ) (%slot-imm) STW ;
146 M:: ppc %string-nth ( dst src index temp -- )
150 dst temp string-offset LBZ
153 temp src string-aux-offset LWZ
156 temp temp byte-array-offset LHZ
162 M:: ppc %set-string-nth-fast ( ch obj index temp -- )
164 ch temp string-offset STB ;
167 M: ppc %add-imm ADDI ;
168 M: ppc %sub swap SUBF ;
169 M: ppc %sub-imm SUBI ;
171 M: ppc %mul-imm MULLI ;
173 M: ppc %and-imm ANDI ;
177 M: ppc %xor-imm XORI ;
179 M: ppc %shl-imm swapd SLWI ;
181 M: ppc %shr-imm swapd SRWI ;
183 M: ppc %sar-imm SRAWI ;
186 :: overflow-template ( label dst src1 src2 insn -- )
189 dst src2 src1 insn call
192 M: ppc %fixnum-add ( label dst src1 src2 -- )
193 [ ADDO. ] overflow-template ;
195 M: ppc %fixnum-sub ( label dst src1 src2 -- )
196 [ SUBFO. ] overflow-template ;
198 M: ppc %fixnum-mul ( label dst src1 src2 -- )
199 [ MULLWO. ] overflow-template ;
201 : bignum@ ( n -- offset ) cells bignum tag-number - ; inline
203 M:: ppc %integer>bignum ( dst src temp -- )
206 dst 0 >bignum %load-reference
207 ! Is it zero? Then just go to the end and return this zero
211 dst 4 cells bignum temp %allot
214 temp dst 1 bignum@ STW
217 temp temp cell-bits 1 - SRAWI
220 temp dst 2 bignum@ STW
221 ! Make negative value positive
227 temp dst 3 bignum@ STW
231 M:: ppc %bignum>integer ( dst src temp -- )
234 temp src 1 bignum@ LWZ
235 ! if the length is 1, its just the sign and nothing else,
238 0 temp 1 tag-fixnum CMPI
241 dst src 3 bignum@ LWZ
243 temp src 2 bignum@ LWZ
244 ! branchless arithmetic: we want to turn 0 into 1,
249 ! multiply value by sign
254 M: ppc %add-float FADD ;
255 M: ppc %sub-float FSUB ;
256 M: ppc %mul-float FMUL ;
257 M: ppc %div-float FDIV ;
259 M:: ppc %integer>float ( dst src -- )
260 HEX: 4330 scratch-reg LIS
261 scratch-reg 1 0 scratch@ STW
263 scratch-reg dup HEX: 8000 XORIS
264 scratch-reg 1 4 scratch@ STW
266 scratch-reg 4503601774854144.0 %load-reference
267 fp-scratch-reg scratch-reg float-offset LFD
268 dst dst fp-scratch-reg FSUB ;
270 M:: ppc %float>integer ( dst src -- )
271 fp-scratch-reg src FCTIWZ
272 fp-scratch-reg 1 0 scratch@ STFD
273 dst 1 4 scratch@ LWZ ;
275 M: ppc %copy ( dst src rep -- )
278 { double-float-rep [ FMR ] }
281 M: ppc %unbox-float ( dst src -- ) float-offset LFD ;
283 M:: ppc %box-float ( dst src temp -- )
284 dst 16 float temp %allot
285 src dst float-offset STFD ;
287 M:: ppc %unbox-any-c-ptr ( dst src temp -- )
289 { "is-byte-array" "end" "start" } [ define-label ] each
290 ! Address is computed in dst
292 ! Load object into scratch-reg
294 ! We come back here with displaced aliens
295 "start" resolve-label
297 0 scratch-reg \ f tag-number CMPI
300 ! Is the object an alien?
301 0 scratch-reg header-offset LWZ
302 0 0 alien type-number tag-fixnum CMPI
303 "is-byte-array" get BNE
304 ! If so, load the offset
305 0 scratch-reg alien-offset LWZ
306 ! Add it to address being computed
308 ! Now recurse on the underlying alien
309 scratch-reg scratch-reg underlying-alien-offset LWZ
311 "is-byte-array" resolve-label
312 ! Add byte array address to address being computed
313 dst dst scratch-reg ADD
314 ! Add an offset to start of byte array's data area
315 dst dst byte-array-offset ADDI
319 : alien@ ( n -- n' ) cells object tag-number - ;
321 M:: ppc %box-alien ( dst src temp -- )
324 dst \ f tag-number %load-immediate
327 dst 4 cells alien temp %allot
331 temp \ f tag-number %load-immediate
332 temp dst 1 alien@ STW
333 ! Store underlying-alien slot
334 temp dst 2 alien@ STW
338 M: ppc %alien-unsigned-1 0 LBZ ;
339 M: ppc %alien-unsigned-2 0 LHZ ;
341 M: ppc %alien-signed-1 dupd 0 LBZ dup EXTSB ;
342 M: ppc %alien-signed-2 0 LHA ;
344 M: ppc %alien-cell 0 LWZ ;
346 M: ppc %alien-float 0 LFS ;
347 M: ppc %alien-double 0 LFD ;
349 M: ppc %set-alien-integer-1 swap 0 STB ;
350 M: ppc %set-alien-integer-2 swap 0 STH ;
352 M: ppc %set-alien-cell swap 0 STW ;
354 M: ppc %set-alien-float swap 0 STFS ;
355 M: ppc %set-alien-double swap 0 STFD ;
357 : load-zone-ptr ( reg -- )
358 "nursery" f %alien-global ;
360 : load-allot-ptr ( nursery-ptr allot-ptr -- )
361 [ drop load-zone-ptr ] [ swap 4 LWZ ] 2bi ;
363 :: inc-allot-ptr ( nursery-ptr allot-ptr n -- )
364 scratch-reg allot-ptr n 8 align ADDI
365 scratch-reg nursery-ptr 4 STW ;
367 :: store-header ( dst class -- )
368 class type-number tag-fixnum scratch-reg LI
369 scratch-reg dst 0 STW ;
371 : store-tagged ( dst tag -- )
372 dupd tag-number ORI ;
374 M:: ppc %allot ( dst size class nursery-ptr -- )
375 nursery-ptr dst load-allot-ptr
376 nursery-ptr dst size inc-allot-ptr
377 dst class store-header
378 dst class store-tagged ;
380 : load-cards-offset ( dst -- )
381 [ "cards_offset" f %alien-global ] [ dup 0 LWZ ] bi ;
383 : load-decks-offset ( dst -- )
384 [ "decks_offset" f %alien-global ] [ dup 0 LWZ ] bi ;
386 M:: ppc %write-barrier ( src card# table -- )
387 card-mark scratch-reg LI
390 table load-cards-offset
391 src card# card-bits SRWI
392 table scratch-reg card# STBX
395 table load-decks-offset
396 src card# deck-bits SRWI
397 table scratch-reg card# STBX ;
399 M:: ppc %check-nursery ( label temp1 temp2 -- )
402 temp2 temp2 3 cells LWZ
403 ! add ALLOT_BUFFER_ZONE to here
404 temp1 temp1 1024 ADDI
409 M:: ppc %save-gc-root ( gc-root register -- )
410 register 1 gc-root gc-root@ STW ;
412 M:: ppc %load-gc-root ( gc-root register -- )
413 register 1 gc-root gc-root@ LWZ ;
415 M:: ppc %call-gc ( gc-root-count -- )
416 %prepare-alien-invoke
417 3 1 gc-root-base local@ ADDI
419 "inline_gc" f %alien-invoke ;
421 M: ppc %prologue ( n -- )
422 0 11 LOAD32 rc-absolute-ppc-2/2 rel-this
425 [ [ 1 1 ] dip neg ADDI ]
426 [ [ 11 1 ] dip xt-save STW ]
428 [ [ 11 1 ] dip next-save STW ]
429 [ [ 0 1 ] dip lr-save + STW ]
432 M: ppc %epilogue ( n -- )
433 #! At the end of each word that calls a subroutine, we store
434 #! the previous link register value in r0 by popping it off
435 #! the stack, set the link register to the contents of r0,
436 #! and jump to the link register.
437 [ [ 0 1 ] dip lr-save + LWZ ]
438 [ [ 1 1 ] dip ADDI ] bi
441 :: (%boolean) ( dst temp word -- )
443 dst \ f tag-number %load-immediate
444 "end" get word execute
445 dst \ t %load-reference
446 "end" get resolve-label ; inline
448 : %boolean ( dst temp cc -- )
450 { cc< [ \ BLT (%boolean) ] }
451 { cc<= [ \ BLE (%boolean) ] }
452 { cc> [ \ BGT (%boolean) ] }
453 { cc>= [ \ BGE (%boolean) ] }
454 { cc= [ \ BEQ (%boolean) ] }
455 { cc/= [ \ BNE (%boolean) ] }
458 : (%compare) ( src1 src2 -- ) [ 0 ] dip CMP ; inline
459 : (%compare-imm) ( src1 src2 -- ) [ 0 ] 2dip CMPI ; inline
460 : (%compare-float) ( src1 src2 -- ) [ 0 ] dip FCMPU ; inline
462 M: ppc %compare (%compare) %boolean ;
463 M: ppc %compare-imm (%compare-imm) %boolean ;
464 M: ppc %compare-float (%compare-float) %boolean ;
466 : %branch ( label cc -- )
476 M: ppc %compare-branch (%compare) %branch ;
477 M: ppc %compare-imm-branch (%compare-imm) %branch ;
478 M: ppc %compare-float-branch (%compare-float) %branch ;
480 : load-from-frame ( dst n rep -- )
482 { int-rep [ [ 1 ] dip LWZ ] }
483 { single-float-rep [ [ 1 ] dip LFS ] }
484 { double-float-rep [ [ 1 ] dip LFD ] }
485 { stack-params [ [ 0 1 ] dip LWZ [ 0 1 ] dip param@ STW ] }
488 : next-param@ ( n -- x ) param@ stack-frame get total-size>> + ;
490 : store-to-frame ( src n rep -- )
492 { int-rep [ [ 1 ] dip STW ] }
493 { single-float-rep [ [ 1 ] dip STFS ] }
494 { double-float-rep [ [ 1 ] dip STFD ] }
495 { stack-params [ [ [ 0 1 ] dip next-param@ LWZ 0 1 ] dip STW ] }
498 M: ppc %spill ( src n rep -- )
499 [ spill@ ] dip store-to-frame ;
501 M: ppc %reload ( dst n rep -- )
502 [ spill@ ] dip load-from-frame ;
506 M: int-regs return-reg drop 3 ;
507 M: int-regs param-regs drop { 3 4 5 6 7 8 9 10 } ;
508 M: float-regs return-reg drop 1 ;
510 M:: ppc %save-param-reg ( stack reg rep -- )
511 reg stack local@ rep store-to-frame ;
513 M:: ppc %load-param-reg ( stack reg rep -- )
514 reg stack local@ rep load-from-frame ;
516 M: ppc %prepare-unbox ( -- )
517 ! First parameter is top of stack
519 ds-reg dup cell SUBI ;
521 M: ppc %unbox ( n rep func -- )
522 ! Value must be in r3
525 ! Store the return value on the C stack
526 over [ [ reg-class-of return-reg ] keep %save-param-reg ] [ 2drop ] if ;
528 M: ppc %unbox-long-long ( n func -- )
529 ! Value must be in r3:r4
532 ! Store the return value on the C stack
534 [ [ 3 1 ] dip local@ STW ]
535 [ [ 4 1 ] dip cell + local@ STW ] bi
538 M: ppc %unbox-large-struct ( n c-type -- )
539 ! Value must be in r3
540 ! Compute destination address and load struct size
541 [ [ 4 1 ] dip local@ ADDI ] [ heap-size 5 LI ] bi*
543 "to_value_struct" f %alien-invoke ;
545 M: ppc %box ( n rep func -- )
546 ! If the source is a stack location, load it into freg #0.
547 ! If the source is f, then we assume the value is already in
549 [ over [ 0 over reg-class-of param-reg swap %load-param-reg ] [ 2drop ] if ] dip
552 M: ppc %box-long-long ( n func -- )
555 [ [ 3 1 ] dip local@ LWZ ]
556 [ [ 4 1 ] dip cell + local@ LWZ ] bi
558 ] dip f %alien-invoke ;
560 : struct-return@ ( n -- n )
561 [ stack-frame get params>> ] unless* local@ ;
563 M: ppc %prepare-box-struct ( -- )
564 #! Compute target address for value struct return
565 3 1 f struct-return@ ADDI
568 M: ppc %box-large-struct ( n c-type -- )
569 ! If n = f, then we're boxing a returned struct
570 ! Compute destination address and load struct size
571 [ [ 3 1 ] dip struct-return@ ADDI ] [ heap-size 4 LI ] bi*
573 "box_value_struct" f %alien-invoke ;
575 M: ppc %prepare-alien-invoke
576 #! Save Factor stack pointers in case the C code calls a
577 #! callback which does a GC, which must reliably trace
579 scratch-reg "stack_chain" f %alien-global
580 scratch-reg scratch-reg 0 LWZ
582 ds-reg scratch-reg 8 STW
583 rs-reg scratch-reg 12 STW ;
585 M: ppc %alien-invoke ( symbol dll -- )
586 [ 11 ] 2dip %alien-global 11 MTLR BLRL ;
588 M: ppc %alien-callback ( quot -- )
589 3 swap %load-reference "c_to_factor" f %alien-invoke ;
591 M: ppc %prepare-alien-indirect ( -- )
592 "unbox_alien" f %alien-invoke
595 M: ppc %alien-indirect ( -- )
598 M: ppc %callback-value ( ctype -- )
599 ! Save top of data stack
602 ! Restore data/call/retain stacks
603 "unnest_stacks" f %alien-invoke
604 ! Restore top of data stack
606 ! Unbox former top of data stack to return registers
609 M: ppc small-enough? ( n -- ? ) -32768 32767 between? ;
611 M: ppc return-struct-in-registers? ( c-type -- ? )
612 c-type return-in-registers?>> ;
614 M: ppc %box-small-struct ( c-type -- )
615 #! Box a <= 16-byte struct returned in r3:r4:r5:r6
617 "box_medium_struct" f %alien-invoke ;
619 : %unbox-struct-1 ( -- )
620 ! Alien must be in r3.
621 "alien_offset" f %alien-invoke
624 : %unbox-struct-2 ( -- )
625 ! Alien must be in r3.
626 "alien_offset" f %alien-invoke
630 : %unbox-struct-4 ( -- )
631 ! Alien must be in r3.
632 "alien_offset" f %alien-invoke
638 M: ppc %unbox-small-struct ( size -- )
639 #! Alien must be in EAX.
640 heap-size cell align cell /i {
641 { 1 [ %unbox-struct-1 ] }
642 { 2 [ %unbox-struct-2 ] }
643 { 4 [ %unbox-struct-4 ] }
649 { [ os macosx? ] [ "cpu.ppc.macosx" require ] }
650 { [ os linux? ] [ "cpu.ppc.linux" require ] }
653 "complex-double" c-type t >>return-in-registers? drop
657 [ alien-unsigned-4 c-bool> ] >>getter
658 [ [ >c-bool ] 2dip set-alien-unsigned-4 ] >>setter
661 "box_boolean" >>boxer
662 "to_boolean" >>unboxer
663 "bool" define-primitive-type
664 ] with-compilation-unit