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