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