]> gitweb.factorcode.org Git - factor.git/blob - basis/cpu/ppc/ppc.factor
Fixing failing unit tests in compiler.tree.propagation due to constraints
[factor.git] / basis / cpu / ppc / ppc.factor
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 ;
12 IN: cpu.ppc
13
14 ! PowerPC register assignments:
15 ! r2-r12: integer vregs
16 ! r15-r29
17 ! r30: integer scratch
18 ! f0-f29: float vregs
19 ! f30: float scratch
20
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 ;
24
25 enable-float-intrinsics
26
27 <<
28 \ ##integer>float t frame-required? set-word-prop
29 \ ##float>integer t frame-required? set-word-prop
30 >>
31
32 M: ppc machine-registers
33     {
34         { int-regs $[ 2 12 [a,b] 15 29 [a,b] append ] }
35         { float-regs $[ 0 29 [a,b] ] }
36     } ;
37
38 CONSTANT: scratch-reg 30
39 CONSTANT: fp-scratch-reg 30
40
41 M: ppc two-operand? f ;
42
43 M: ppc %load-immediate ( reg n -- ) swap LOAD ;
44
45 M: ppc %load-reference ( reg obj -- )
46     [ 0 swap LOAD32 ] [ rc-absolute-ppc-2/2 rel-immediate ] bi* ;
47
48 M: ppc %alien-global ( register symbol dll -- )
49     [ 0 swap LOAD32 ] 2dip rc-absolute-ppc-2/2 rel-dlsym ;
50
51 CONSTANT: ds-reg 13
52 CONSTANT: rs-reg 14
53
54 GENERIC: loc-reg ( loc -- reg )
55
56 M: ds-loc loc-reg drop ds-reg ;
57 M: rs-loc loc-reg drop rs-reg ;
58
59 : loc>operand ( loc -- reg n )
60     [ loc-reg ] [ n>> cells neg ] bi ; inline
61
62 M: ppc %peek loc>operand LWZ ;
63 M: ppc %replace loc>operand STW ;
64
65 :: (%inc) ( n reg -- ) reg reg n cells ADDI ; inline
66
67 M: ppc %inc-d ( n -- ) ds-reg (%inc) ;
68 M: ppc %inc-r ( n -- ) rs-reg (%inc) ;
69
70 HOOK: reserved-area-size os ( -- n )
71
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 - ;
77
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.
84
85 : param@ ( n -- x ) reserved-area-size + ; inline
86
87 : param-save-size ( -- n ) 8 cells ; foldable
88
89 : local@ ( n -- x )
90     reserved-area-size param-save-size + + ; inline
91
92 : spill@ ( n -- offset )
93     spill-offset local@ ;
94
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     factor-area-size + ;
100
101 ! GC root area
102 : gc-root@ ( n -- offset )
103     gc-root-offset local@ ;
104
105 ! Finally we have the linkage area
106 HOOK: lr-save os ( -- n )
107
108 M: ppc stack-frame-size ( stack-frame -- i )
109     (stack-frame-size)
110     param-save-size +
111     reserved-area-size +
112     factor-area-size +
113     4 cells align ;
114
115 M: ppc %call ( word -- ) 0 BL rc-relative-ppc-3 rel-word-pic ;
116
117 M: ppc %jump ( word -- )
118     0 6 LOAD32 8 rc-absolute-ppc-2/2 rel-here
119     0 B rc-relative-ppc-3 rel-word-pic-tail ;
120
121 M: ppc %jump-label ( label -- ) B ;
122 M: ppc %return ( -- ) BLR ;
123
124 M:: ppc %dispatch ( src temp -- )
125     0 temp LOAD32
126     4 cells rc-absolute-ppc-2/2 rel-here
127     temp temp src LWZX
128     temp MTCTR
129     BCTR ;
130
131 :: (%slot) ( obj slot tag temp -- reg offset )
132     temp slot obj ADD
133     temp tag neg ; inline
134
135 : (%slot-imm) ( obj slot tag -- reg offset )
136     [ cells ] dip - ; inline
137
138 M: ppc %slot ( dst obj slot tag temp -- ) (%slot) LWZ ;
139 M: ppc %slot-imm ( dst obj slot tag -- ) (%slot-imm) LWZ ;
140 M: ppc %set-slot ( src obj slot tag temp -- ) (%slot) STW ;
141 M: ppc %set-slot-imm ( src obj slot tag -- ) (%slot-imm) STW ;
142
143 M:: ppc %string-nth ( dst src index temp -- )
144     [
145         "end" define-label
146         temp src index ADD
147         dst temp string-offset LBZ
148         0 dst HEX: 80 CMPI
149         "end" get BLT
150         temp src string-aux-offset LWZ
151         temp temp index ADD
152         temp temp index ADD
153         temp temp byte-array-offset LHZ
154         temp temp 7 SLWI
155         dst dst temp XOR
156         "end" resolve-label
157     ] with-scope ;
158
159 M:: ppc %set-string-nth-fast ( ch obj index temp -- )
160     temp obj index ADD
161     ch temp string-offset STB ;
162
163 M: ppc %add     ADD ;
164 M: ppc %add-imm ADDI ;
165 M: ppc %sub     swap SUBF ;
166 M: ppc %sub-imm SUBI ;
167 M: ppc %mul     MULLW ;
168 M: ppc %mul-imm MULLI ;
169 M: ppc %and     AND ;
170 M: ppc %and-imm ANDI ;
171 M: ppc %or      OR ;
172 M: ppc %or-imm  ORI ;
173 M: ppc %xor     XOR ;
174 M: ppc %xor-imm XORI ;
175 M: ppc %shl     SLW ;
176 M: ppc %shl-imm swapd SLWI ;
177 M: ppc %shr     SRW ;
178 M: ppc %shr-imm swapd SRWI ;
179 M: ppc %sar     SRAW ;
180 M: ppc %sar-imm SRAWI ;
181 M: ppc %not     NOT ;
182
183 :: overflow-template ( label dst src1 src2 insn -- )
184     0 0 LI
185     0 MTXER
186     dst src2 src1 insn call
187     label BO ; inline
188
189 M: ppc %fixnum-add ( label dst src1 src2 -- )
190     [ ADDO. ] overflow-template ;
191
192 M: ppc %fixnum-sub ( label dst src1 src2 -- )
193     [ SUBFO. ] overflow-template ;
194
195 M: ppc %fixnum-mul ( label dst src1 src2 -- )
196     [ MULLWO. ] overflow-template ;
197
198 : bignum@ ( n -- offset ) cells bignum tag-number - ; inline
199
200 M:: ppc %integer>bignum ( dst src temp -- )
201     [
202         "end" define-label
203         dst 0 >bignum %load-reference
204         ! Is it zero? Then just go to the end and return this zero
205         0 src 0 CMPI
206         "end" get BEQ
207         ! Allocate a bignum
208         dst 4 cells bignum temp %allot
209         ! Write length
210         2 tag-fixnum temp LI
211         temp dst 1 bignum@ STW
212         ! Compute sign
213         temp src MR
214         temp temp cell-bits 1 - SRAWI
215         temp temp 1 ANDI
216         ! Store sign
217         temp dst 2 bignum@ STW
218         ! Make negative value positive
219         temp temp temp ADD
220         temp temp NEG
221         temp temp 1 ADDI
222         temp src temp MULLW
223         ! Store the bignum
224         temp dst 3 bignum@ STW
225         "end" resolve-label
226     ] with-scope ;
227
228 M:: ppc %bignum>integer ( dst src temp -- )
229     [
230         "end" define-label
231         temp src 1 bignum@ LWZ
232         ! if the length is 1, its just the sign and nothing else,
233         ! so output 0
234         0 dst LI
235         0 temp 1 tag-fixnum CMPI
236         "end" get BEQ
237         ! load the value
238         dst src 3 bignum@ LWZ
239         ! load the sign
240         temp src 2 bignum@ LWZ
241         ! branchless arithmetic: we want to turn 0 into 1,
242         ! and 1 into -1
243         temp temp temp ADD
244         temp temp 1 SUBI
245         temp temp NEG
246         ! multiply value by sign
247         dst dst temp MULLW
248         "end" resolve-label
249     ] with-scope ;
250
251 M: ppc %add-float FADD ;
252 M: ppc %sub-float FSUB ;
253 M: ppc %mul-float FMUL ;
254 M: ppc %div-float FDIV ;
255
256 M:: ppc %integer>float ( dst src -- )
257     HEX: 4330 scratch-reg LIS
258     scratch-reg 1 0 scratch@ STW
259     scratch-reg src MR
260     scratch-reg dup HEX: 8000 XORIS
261     scratch-reg 1 4 scratch@ STW
262     dst 1 0 scratch@ LFD
263     scratch-reg 4503601774854144.0 %load-reference
264     fp-scratch-reg scratch-reg float-offset LFD
265     dst dst fp-scratch-reg FSUB ;
266
267 M:: ppc %float>integer ( dst src -- )
268     fp-scratch-reg src FCTIWZ
269     fp-scratch-reg 1 0 scratch@ STFD
270     dst 1 4 scratch@ LWZ ;
271
272 M: ppc %copy ( dst src rep -- )
273     {
274         { int-rep [ MR ] }
275         { double-rep [ FMR ] }
276     } case ;
277
278 M: ppc %unbox-float ( dst src -- ) float-offset LFD ;
279
280 M:: ppc %box-float ( dst src temp -- )
281     dst 16 float temp %allot
282     src dst float-offset STFD ;
283
284 : float-function-param ( i spill-slot -- )
285     [ float-regs param-regs nth 1 ] [ n>> spill@ ] bi* LFD ;
286
287 : float-function-return ( reg -- )
288     float-regs return-reg 2dup = [ 2drop ] [ FMR ] if ;
289
290 M:: ppc %unary-float-function ( dst src func -- )
291     0 src float-function-param
292     func f %alien-invoke
293     dst float-function-return ;
294
295 M:: ppc %binary-float-function ( dst src1 src2 func -- )
296     0 src1 float-function-param
297     1 src2 float-function-param
298     func f %alien-invoke
299     dst float-function-return ;
300
301 ! Internal format is always double-precision on PowerPC
302 M: ppc %single>double-float FMR ;
303
304 M: ppc %double>single-float FMR ;
305
306 M: ppc %unbox-alien ( dst src -- )
307     alien-offset LWZ ;
308
309 M:: ppc %unbox-any-c-ptr ( dst src temp -- )
310     [
311         { "is-byte-array" "end" "start" } [ define-label ] each
312         ! Address is computed in dst
313         0 dst LI
314         ! Load object into scratch-reg
315         scratch-reg src MR
316         ! We come back here with displaced aliens
317         "start" resolve-label
318         ! Is the object f?
319         0 scratch-reg \ f tag-number CMPI
320         ! If so, done
321         "end" get BEQ
322         ! Is the object an alien?
323         0 scratch-reg header-offset LWZ
324         0 0 alien type-number tag-fixnum CMPI
325         "is-byte-array" get BNE
326         ! If so, load the offset
327         0 scratch-reg alien-offset LWZ
328         ! Add it to address being computed
329         dst dst 0 ADD
330         ! Now recurse on the underlying alien
331         scratch-reg scratch-reg underlying-alien-offset LWZ
332         "start" get B
333         "is-byte-array" resolve-label
334         ! Add byte array address to address being computed
335         dst dst scratch-reg ADD
336         ! Add an offset to start of byte array's data area
337         dst dst byte-array-offset ADDI
338         "end" resolve-label
339     ] with-scope ;
340
341 : alien@ ( n -- n' ) cells object tag-number - ;
342
343 :: %allot-alien ( dst displacement base temp -- )
344     dst 4 cells alien temp %allot
345     temp \ f tag-number %load-immediate
346     ! Store underlying-alien slot
347     base dst 1 alien@ STW
348     ! Store expired slot
349     temp dst 2 alien@ STW
350     ! Store offset
351     displacement dst 3 alien@ STW ;
352
353 M:: ppc %box-alien ( dst src temp -- )
354     [
355         "f" define-label
356         dst \ f tag-number %load-immediate
357         0 src 0 CMPI
358         "f" get BEQ
359         dst src temp temp %allot-alien
360         "f" resolve-label
361     ] with-scope ;
362
363 M:: ppc %box-displaced-alien ( dst displacement base displacement' base' base-class -- )
364     [
365         "end" define-label
366         "alloc" define-label
367         "simple-case" define-label
368         ! If displacement is zero, return the base
369         dst base MR
370         0 displacement 0 CMPI
371         "end" get BEQ
372         ! Quickly use displacement' before its needed for real, as allot temporary
373         displacement' :> temp
374         dst 4 cells alien temp %allot
375         ! If base is already a displaced alien, unpack it
376         0 base \ f tag-number CMPI
377         "simple-case" get BEQ
378         temp base header-offset LWZ
379         0 temp alien type-number tag-fixnum CMPI
380         "simple-case" get BNE
381         ! displacement += base.displacement
382         temp base 3 alien@ LWZ
383         displacement' displacement temp ADD
384         ! base = base.base
385         base' base 1 alien@ LWZ
386         "alloc" get B
387         "simple-case" resolve-label
388         displacement' displacement MR
389         base' base MR
390         "alloc" resolve-label
391         ! Store underlying-alien slot
392         base' dst 1 alien@ STW
393         ! Store offset
394         displacement' dst 3 alien@ STW
395         ! Store expired slot (its ok to clobber displacement')
396         temp \ f tag-number %load-immediate
397         temp dst 2 alien@ STW
398         "end" resolve-label
399     ] with-scope ;
400
401 M: ppc %alien-unsigned-1 0 LBZ ;
402 M: ppc %alien-unsigned-2 0 LHZ ;
403
404 M: ppc %alien-signed-1 dupd 0 LBZ dup EXTSB ;
405 M: ppc %alien-signed-2 0 LHA ;
406
407 M: ppc %alien-cell 0 LWZ ;
408
409 M: ppc %alien-float 0 LFS ;
410 M: ppc %alien-double 0 LFD ;
411
412 M: ppc %set-alien-integer-1 swap 0 STB ;
413 M: ppc %set-alien-integer-2 swap 0 STH ;
414
415 M: ppc %set-alien-cell swap 0 STW ;
416
417 M: ppc %set-alien-float swap 0 STFS ;
418 M: ppc %set-alien-double swap 0 STFD ;
419
420 : load-zone-ptr ( reg -- )
421     "nursery" f %alien-global ;
422
423 : load-allot-ptr ( nursery-ptr allot-ptr -- )
424     [ drop load-zone-ptr ] [ swap 4 LWZ ] 2bi ;
425
426 :: inc-allot-ptr ( nursery-ptr allot-ptr n -- )
427     scratch-reg allot-ptr n 8 align ADDI
428     scratch-reg nursery-ptr 4 STW ;
429
430 :: store-header ( dst class -- )
431     class type-number tag-fixnum scratch-reg LI
432     scratch-reg dst 0 STW ;
433
434 : store-tagged ( dst tag -- )
435     dupd tag-number ORI ;
436
437 M:: ppc %allot ( dst size class nursery-ptr -- )
438     nursery-ptr dst load-allot-ptr
439     nursery-ptr dst size inc-allot-ptr
440     dst class store-header
441     dst class store-tagged ;
442
443 : load-cards-offset ( dst -- )
444     [ "cards_offset" f %alien-global ] [ dup 0 LWZ ] bi ;
445
446 : load-decks-offset ( dst -- )
447     [ "decks_offset" f %alien-global ] [ dup 0 LWZ ] bi  ;
448
449 M:: ppc %write-barrier ( src card# table -- )
450     card-mark scratch-reg LI
451
452     ! Mark the card
453     table load-cards-offset
454     src card# card-bits SRWI
455     table scratch-reg card# STBX
456
457     ! Mark the card deck
458     table load-decks-offset
459     src card# deck-bits SRWI
460     table scratch-reg card# STBX ;
461
462 M:: ppc %check-nursery ( label temp1 temp2 -- )
463     temp2 load-zone-ptr
464     temp1 temp2 cell LWZ
465     temp2 temp2 3 cells LWZ
466     ! add ALLOT_BUFFER_ZONE to here
467     temp1 temp1 1024 ADDI
468     ! is here >= end?
469     temp1 0 temp2 CMP
470     label BLE ;
471
472 M:: ppc %save-gc-root ( gc-root register -- )
473     register 1 gc-root gc-root@ STW ;
474
475 M:: ppc %load-gc-root ( gc-root register -- )
476     register 1 gc-root gc-root@ LWZ ;
477
478 M:: ppc %call-gc ( gc-root-count -- )
479     3 1 gc-root-base local@ ADDI
480     gc-root-count 4 LI
481     "inline_gc" f %alien-invoke ;
482
483 M: ppc %prologue ( n -- )
484     0 11 LOAD32 rc-absolute-ppc-2/2 rel-this
485     0 MFLR
486     {
487         [ [ 1 1 ] dip neg ADDI ]
488         [ [ 11 1 ] dip xt-save STW ]
489         [ 11 LI ]
490         [ [ 11 1 ] dip next-save STW ]
491         [ [ 0 1 ] dip lr-save + STW ]
492     } cleave ;
493
494 M: ppc %epilogue ( n -- )
495     #! At the end of each word that calls a subroutine, we store
496     #! the previous link register value in r0 by popping it off
497     #! the stack, set the link register to the contents of r0,
498     #! and jump to the link register.
499     [ [ 0 1 ] dip lr-save + LWZ ]
500     [ [ 1 1 ] dip ADDI ] bi
501     0 MTLR ;
502
503 :: (%boolean) ( dst temp branch1 branch2 -- )
504     "end" define-label
505     dst \ f tag-number %load-immediate
506     "end" get branch1 execute( label -- )
507     branch2 [ "end" get branch2 execute( label -- ) ] when
508     dst \ t %load-reference
509     "end" get resolve-label ; inline
510
511 :: %boolean ( dst cc temp -- )
512     cc negate-cc order-cc {
513         { cc<  [ dst temp \ BLT f (%boolean) ] }
514         { cc<= [ dst temp \ BLE f (%boolean) ] }
515         { cc>  [ dst temp \ BGT f (%boolean) ] }
516         { cc>= [ dst temp \ BGE f (%boolean) ] }
517         { cc=  [ dst temp \ BEQ f (%boolean) ] }
518         { cc/= [ dst temp \ BNE f (%boolean) ] }
519     } case ;
520
521 : (%compare) ( src1 src2 -- ) [ 0 ] dip CMP ; inline
522 : (%compare-imm) ( src1 src2 -- ) [ 0 ] 2dip CMPI ; inline
523 : (%compare-float-unordered) ( src1 src2 -- ) [ 0 ] dip FCMPU ; inline
524 : (%compare-float-ordered) ( src1 src2 -- ) [ 0 ] dip FCMPO ; inline
525
526 :: (%compare-float) ( src1 src2 cc compare -- branch1 branch2 )
527     cc {
528         { cc<    [ src1 src2 \ compare execute( a b -- ) \ BLT f     ] }
529         { cc<=   [ src1 src2 \ compare execute( a b -- ) \ BLT \ BEQ ] }
530         { cc>    [ src1 src2 \ compare execute( a b -- ) \ BGT f     ] }
531         { cc>=   [ src1 src2 \ compare execute( a b -- ) \ BGT \ BEQ ] }
532         { cc=    [ src1 src2 \ compare execute( a b -- ) \ BEQ f     ] }
533         { cc<>   [ src1 src2 \ compare execute( a b -- ) \ BLT \ BGT ] }
534         { cc<>=  [ src1 src2 \ compare execute( a b -- ) \ BNO f     ] }
535         { cc/<   [ src1 src2 \ compare execute( a b -- ) \ BGE f     ] }
536         { cc/<=  [ src1 src2 \ compare execute( a b -- ) \ BGT \ BO  ] }
537         { cc/>   [ src1 src2 \ compare execute( a b -- ) \ BLE f     ] }
538         { cc/>=  [ src1 src2 \ compare execute( a b -- ) \ BLT \ BO  ] }
539         { cc/=   [ src1 src2 \ compare execute( a b -- ) \ BNE f     ] }
540         { cc/<>  [ src1 src2 \ compare execute( a b -- ) \ BEQ \ BO  ] }
541         { cc/<>= [ src1 src2 \ compare execute( a b -- ) \ BO  f     ] }
542     } case ; inline
543
544 M: ppc %compare [ (%compare) ] 2dip %boolean ;
545
546 M: ppc %compare-imm [ (%compare-imm) ] 2dip %boolean ;
547
548 M:: ppc %compare-float-ordered ( dst src1 src2 cc temp -- )
549     src1 src2 cc negate-cc \ (%compare-float-ordered) (%compare-float) :> branch2 :> branch1
550     dst temp branch1 branch2 (%boolean) ;
551
552 M:: ppc %compare-float-unordered ( dst src1 src2 cc temp -- )
553     src1 src2 cc negate-cc \ (%compare-float-unordered) (%compare-float) :> branch2 :> branch1
554     dst temp branch1 branch2 (%boolean) ;
555
556 :: %branch ( label cc -- )
557     cc order-cc {
558         { cc<  [ label BLT ] }
559         { cc<= [ label BLE ] }
560         { cc>  [ label BGT ] }
561         { cc>= [ label BGE ] }
562         { cc=  [ label BEQ ] }
563         { cc/= [ label BNE ] }
564     } case ;
565
566 M:: ppc %compare-branch ( label src1 src2 cc -- )
567     src1 src2 (%compare)
568     label cc %branch ;
569
570 M:: ppc %compare-imm-branch ( label src1 src2 cc -- )
571     src1 src2 (%compare-imm)
572     label cc %branch ;
573
574 :: (%branch) ( label branch1 branch2 -- )
575     label branch1 execute( label -- )
576     branch2 [ label branch2 execute( label -- ) ] when ; inline
577
578 M:: ppc %compare-float-ordered-branch ( label src1 src2 cc -- )
579     src1 src2 cc \ (%compare-float-ordered) (%compare-float) :> branch2 :> branch1
580     label branch1 branch2 (%branch) ;
581
582 M:: ppc %compare-float-unordered-branch ( label src1 src2 cc -- )
583     src1 src2 cc \ (%compare-float-unordered) (%compare-float) :> branch2 :> branch1
584     label branch1 branch2 (%branch) ;
585
586 : load-from-frame ( dst n rep -- )
587     {
588         { int-rep [ [ 1 ] dip LWZ ] }
589         { float-rep [ [ 1 ] dip LFS ] }
590         { double-rep [ [ 1 ] dip LFD ] }
591         { stack-params [ [ 0 1 ] dip LWZ [ 0 1 ] dip param@ STW ] }
592     } case ;
593
594 : next-param@ ( n -- x ) param@ stack-frame get total-size>> + ;
595
596 : store-to-frame ( src n rep -- )
597     {
598         { int-rep [ [ 1 ] dip STW ] }
599         { float-rep [ [ 1 ] dip STFS ] }
600         { double-rep [ [ 1 ] dip STFD ] }
601         { stack-params [ [ [ 0 1 ] dip next-param@ LWZ 0 1 ] dip STW ] }
602     } case ;
603
604 M: ppc %spill ( src rep n -- )
605     swap [ spill@ ] dip store-to-frame ;
606
607 M: ppc %reload ( dst rep n -- )
608     swap [ spill@ ] dip load-from-frame ;
609
610 M: ppc %loop-entry ;
611
612 M: int-regs return-reg drop 3 ;
613 M: int-regs param-regs drop { 3 4 5 6 7 8 9 10 } ;
614 M: float-regs return-reg drop 1 ;
615
616 M:: ppc %save-param-reg ( stack reg rep -- )
617     reg stack local@ rep store-to-frame ;
618
619 M:: ppc %load-param-reg ( stack reg rep -- )
620     reg stack local@ rep load-from-frame ;
621
622 M: ppc %prepare-unbox ( -- )
623     ! First parameter is top of stack
624     3 ds-reg 0 LWZ
625     ds-reg dup cell SUBI ;
626
627 M: ppc %unbox ( n rep func -- )
628     ! Value must be in r3
629     ! Call the unboxer
630     f %alien-invoke
631     ! Store the return value on the C stack
632     over [ [ reg-class-of return-reg ] keep %save-param-reg ] [ 2drop ] if ;
633
634 M: ppc %unbox-long-long ( n func -- )
635     ! Value must be in r3:r4
636     ! Call the unboxer
637     f %alien-invoke
638     ! Store the return value on the C stack
639     [
640         [ [ 3 1 ] dip local@ STW ]
641         [ [ 4 1 ] dip cell + local@ STW ] bi
642     ] when* ;
643
644 M: ppc %unbox-large-struct ( n c-type -- )
645     ! Value must be in r3
646     ! Compute destination address and load struct size
647     [ [ 4 1 ] dip local@ ADDI ] [ heap-size 5 LI ] bi*
648     ! Call the function
649     "to_value_struct" f %alien-invoke ;
650
651 M: ppc %box ( n rep func -- )
652     ! If the source is a stack location, load it into freg #0.
653     ! If the source is f, then we assume the value is already in
654     ! freg #0.
655     [ over [ 0 over reg-class-of param-reg swap %load-param-reg ] [ 2drop ] if ] dip
656     f %alien-invoke ;
657
658 M: ppc %box-long-long ( n func -- )
659     [
660         [
661             [ [ 3 1 ] dip local@ LWZ ]
662             [ [ 4 1 ] dip cell + local@ LWZ ] bi
663         ] when*
664     ] dip f %alien-invoke ;
665
666 : struct-return@ ( n -- n )
667     [ stack-frame get params>> ] unless* local@ ;
668
669 M: ppc %prepare-box-struct ( -- )
670     #! Compute target address for value struct return
671     3 1 f struct-return@ ADDI
672     3 1 0 local@ STW ;
673
674 M: ppc %box-large-struct ( n c-type -- )
675     ! If n = f, then we're boxing a returned struct
676     ! Compute destination address and load struct size
677     [ [ 3 1 ] dip struct-return@ ADDI ] [ heap-size 4 LI ] bi*
678     ! Call the function
679     "box_value_struct" f %alien-invoke ;
680
681 M:: ppc %save-context ( temp1 temp2 callback-allowed? -- )
682     #! Save Factor stack pointers in case the C code calls a
683     #! callback which does a GC, which must reliably trace
684     #! all roots.
685     temp1 "stack_chain" f %alien-global
686     temp1 temp1 0 LWZ
687     1 temp1 0 STW
688     callback-allowed? [
689         ds-reg temp1 8 STW
690         rs-reg temp1 12 STW
691     ] when ;
692
693 M: ppc %alien-invoke ( symbol dll -- )
694     [ 11 ] 2dip %alien-global 11 MTLR BLRL ;
695
696 M: ppc %alien-callback ( quot -- )
697     3 swap %load-reference "c_to_factor" f %alien-invoke ;
698
699 M: ppc %prepare-alien-indirect ( -- )
700     "unbox_alien" f %alien-invoke
701     15 3 MR ;
702
703 M: ppc %alien-indirect ( -- )
704     15 MTLR BLRL ;
705
706 M: ppc %callback-value ( ctype -- )
707     ! Save top of data stack
708     3 ds-reg 0 LWZ
709     3 1 0 local@ STW
710     ! Restore data/call/retain stacks
711     "unnest_stacks" f %alien-invoke
712     ! Restore top of data stack
713     3 1 0 local@ LWZ
714     ! Unbox former top of data stack to return registers
715     unbox-return ;
716
717 M: ppc small-enough? ( n -- ? ) -32768 32767 between? ;
718
719 M: ppc return-struct-in-registers? ( c-type -- ? )
720     c-type return-in-registers?>> ;
721
722 M: ppc %box-small-struct ( c-type -- )
723     #! Box a <= 16-byte struct returned in r3:r4:r5:r6
724     heap-size 7 LI
725     "box_medium_struct" f %alien-invoke ;
726
727 : %unbox-struct-1 ( -- )
728     ! Alien must be in r3.
729     "alien_offset" f %alien-invoke
730     3 3 0 LWZ ;
731
732 : %unbox-struct-2 ( -- )
733     ! Alien must be in r3.
734     "alien_offset" f %alien-invoke
735     4 3 4 LWZ
736     3 3 0 LWZ ;
737
738 : %unbox-struct-4 ( -- )
739     ! Alien must be in r3.
740     "alien_offset" f %alien-invoke
741     6 3 12 LWZ
742     5 3 8 LWZ
743     4 3 4 LWZ
744     3 3 0 LWZ ;
745
746 M: ppc %unbox-small-struct ( size -- )
747     #! Alien must be in EAX.
748     heap-size cell align cell /i {
749         { 1 [ %unbox-struct-1 ] }
750         { 2 [ %unbox-struct-2 ] }
751         { 4 [ %unbox-struct-4 ] }
752     } case ;
753
754 enable-float-functions
755
756 USE: vocabs.loader
757
758 {
759     { [ os macosx? ] [ "cpu.ppc.macosx" require ] }
760     { [ os linux? ] [ "cpu.ppc.linux" require ] }
761 } cond
762
763 "complex-double" c-type t >>return-in-registers? drop
764
765 [
766     <c-type>
767         [ alien-unsigned-4 c-bool> ] >>getter
768         [ [ >c-bool ] 2dip set-alien-unsigned-4 ] >>setter
769         4 >>size
770         4 >>align
771         "box_boolean" >>boxer
772         "to_boolean" >>unboxer
773     "bool" define-primitive-type
774 ] with-compilation-unit