]> gitweb.factorcode.org Git - factor.git/blob - basis/cpu/ppc/ppc.factor
de37cd6ee33b5eee3f25a6153d1280ef823a3070
[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 M: ppc %neg     NEG ;
188
189 :: overflow-template ( label dst src1 src2 insn -- )
190     0 0 LI
191     0 MTXER
192     dst src2 src1 insn call
193     label BO ; inline
194
195 M: ppc %fixnum-add ( label dst src1 src2 -- )
196     [ ADDO. ] overflow-template ;
197
198 M: ppc %fixnum-sub ( label dst src1 src2 -- )
199     [ SUBFO. ] overflow-template ;
200
201 M: ppc %fixnum-mul ( label dst src1 src2 -- )
202     [ MULLWO. ] overflow-template ;
203
204 M: ppc %add-float FADD ;
205 M: ppc %sub-float FSUB ;
206 M: ppc %mul-float FMUL ;
207 M: ppc %div-float FDIV ;
208
209 M:: ppc %integer>float ( dst src -- )
210     HEX: 4330 scratch-reg LIS
211     scratch-reg 1 0 scratch@ STW
212     scratch-reg src MR
213     scratch-reg dup HEX: 8000 XORIS
214     scratch-reg 1 4 scratch@ STW
215     dst 1 0 scratch@ LFD
216     scratch-reg 4503601774854144.0 %load-reference
217     fp-scratch-reg scratch-reg float-offset LFD
218     dst dst fp-scratch-reg FSUB ;
219
220 M:: ppc %float>integer ( dst src -- )
221     fp-scratch-reg src FCTIWZ
222     fp-scratch-reg 1 0 scratch@ STFD
223     dst 1 4 scratch@ LWZ ;
224
225 M: ppc %copy ( dst src rep -- )
226     2over eq? [ 3drop ] [
227         {
228             { int-rep [ MR ] }
229             { double-rep [ FMR ] }
230         } case
231     ] if ;
232
233 M: ppc %unbox-float ( dst src -- ) float-offset LFD ;
234
235 M:: ppc %box-float ( dst src temp -- )
236     dst 16 float temp %allot
237     src dst float-offset STFD ;
238
239 GENERIC: float-function-param* ( dst src -- )
240
241 M: spill-slot float-function-param* [ 1 ] dip n>> spill@ LFD ;
242 M: integer float-function-param* FMR ;
243
244 : float-function-param ( i src -- )
245     [ float-regs param-regs nth ] dip float-function-param* ;
246
247 : float-function-return ( reg -- )
248     float-regs return-reg double-rep %copy ;
249
250 M:: ppc %unary-float-function ( dst src func -- )
251     0 src float-function-param
252     func f %alien-invoke
253     dst float-function-return ;
254
255 M:: ppc %binary-float-function ( dst src1 src2 func -- )
256     0 src1 float-function-param
257     1 src2 float-function-param
258     func f %alien-invoke
259     dst float-function-return ;
260
261 ! Internal format is always double-precision on PowerPC
262 M: ppc %single>double-float double-rep %copy ;
263 M: ppc %double>single-float double-rep %copy ;
264
265 ! VMX/AltiVec not supported yet
266 M: ppc %zero-vector-reps { } ;
267 M: ppc %gather-vector-2-reps { } ;
268 M: ppc %gather-vector-4-reps { } ;
269 M: ppc %shuffle-vector-reps { } ;
270 M: ppc %add-vector-reps { } ;
271 M: ppc %saturated-add-vector-reps { } ;
272 M: ppc %add-sub-vector-reps { } ;
273 M: ppc %sub-vector-reps { } ;
274 M: ppc %saturated-sub-vector-reps { } ;
275 M: ppc %mul-vector-reps { } ;
276 M: ppc %saturated-mul-vector-reps { } ;
277 M: ppc %div-vector-reps { } ;
278 M: ppc %min-vector-reps { } ;
279 M: ppc %max-vector-reps { } ;
280 M: ppc %dot-vector-reps { } ;
281 M: ppc %sqrt-vector-reps { } ;
282 M: ppc %horizontal-add-vector-reps { } ;
283 M: ppc %horizontal-sub-vector-reps { } ;
284 M: ppc %abs-vector-reps { } ;
285 M: ppc %and-vector-reps { } ;
286 M: ppc %andn-vector-reps { } ;
287 M: ppc %or-vector-reps { } ;
288 M: ppc %xor-vector-reps { } ;
289 M: ppc %shl-vector-reps { } ;
290 M: ppc %shr-vector-reps { } ;
291 M: ppc %horizontal-shl-vector-reps { } ;
292 M: ppc %horizontal-shr-vector-reps { } ;
293
294 M: ppc %unbox-alien ( dst src -- )
295     alien-offset LWZ ;
296
297 M:: ppc %unbox-any-c-ptr ( dst src temp -- )
298     [
299         { "is-byte-array" "end" "start" } [ define-label ] each
300         ! Address is computed in dst
301         0 dst LI
302         ! Load object into scratch-reg
303         scratch-reg src MR
304         ! We come back here with displaced aliens
305         "start" resolve-label
306         ! Is the object f?
307         0 scratch-reg \ f tag-number CMPI
308         ! If so, done
309         "end" get BEQ
310         ! Is the object an alien?
311         0 scratch-reg header-offset LWZ
312         0 0 alien type-number tag-fixnum CMPI
313         "is-byte-array" get BNE
314         ! If so, load the offset
315         0 scratch-reg alien-offset LWZ
316         ! Add it to address being computed
317         dst dst 0 ADD
318         ! Now recurse on the underlying alien
319         scratch-reg scratch-reg underlying-alien-offset LWZ
320         "start" get B
321         "is-byte-array" resolve-label
322         ! Add byte array address to address being computed
323         dst dst scratch-reg ADD
324         ! Add an offset to start of byte array's data area
325         dst dst byte-array-offset ADDI
326         "end" resolve-label
327     ] with-scope ;
328
329 : alien@ ( n -- n' ) cells object tag-number - ;
330
331 :: %allot-alien ( dst displacement base temp -- )
332     dst 4 cells alien temp %allot
333     temp \ f tag-number %load-immediate
334     ! Store underlying-alien slot
335     base dst 1 alien@ STW
336     ! Store expired slot
337     temp dst 2 alien@ STW
338     ! Store offset
339     displacement dst 3 alien@ STW ;
340
341 M:: ppc %box-alien ( dst src temp -- )
342     [
343         "f" define-label
344         dst \ f tag-number %load-immediate
345         0 src 0 CMPI
346         "f" get BEQ
347         dst src temp temp %allot-alien
348         "f" resolve-label
349     ] with-scope ;
350
351 M:: ppc %box-displaced-alien ( dst displacement base displacement' base' base-class -- )
352     [
353         "end" define-label
354         "alloc" define-label
355         "simple-case" define-label
356         ! If displacement is zero, return the base
357         dst base MR
358         0 displacement 0 CMPI
359         "end" get BEQ
360         ! Quickly use displacement' before its needed for real, as allot temporary
361         displacement' :> temp
362         dst 4 cells alien temp %allot
363         ! If base is already a displaced alien, unpack it
364         0 base \ f tag-number CMPI
365         "simple-case" get BEQ
366         temp base header-offset LWZ
367         0 temp alien type-number tag-fixnum CMPI
368         "simple-case" get BNE
369         ! displacement += base.displacement
370         temp base 3 alien@ LWZ
371         displacement' displacement temp ADD
372         ! base = base.base
373         base' base 1 alien@ LWZ
374         "alloc" get B
375         "simple-case" resolve-label
376         displacement' displacement MR
377         base' base MR
378         "alloc" resolve-label
379         ! Store underlying-alien slot
380         base' dst 1 alien@ STW
381         ! Store offset
382         displacement' dst 3 alien@ STW
383         ! Store expired slot (its ok to clobber displacement')
384         temp \ f tag-number %load-immediate
385         temp dst 2 alien@ STW
386         "end" resolve-label
387     ] with-scope ;
388
389 M: ppc %alien-unsigned-1 LBZ ;
390 M: ppc %alien-unsigned-2 LHZ ;
391
392 M: ppc %alien-signed-1 [ dup ] 2dip LBZ dup EXTSB ;
393 M: ppc %alien-signed-2 LHA ;
394
395 M: ppc %alien-cell LWZ ;
396
397 M: ppc %alien-float LFS ;
398 M: ppc %alien-double LFD ;
399
400 M: ppc %set-alien-integer-1 swapd STB ;
401 M: ppc %set-alien-integer-2 swapd STH ;
402
403 M: ppc %set-alien-cell swapd STW ;
404
405 M: ppc %set-alien-float swapd STFS ;
406 M: ppc %set-alien-double swapd STFD ;
407
408 : load-zone-ptr ( reg -- )
409     "nursery" %load-vm-field-addr ;
410
411 : load-allot-ptr ( nursery-ptr allot-ptr -- )
412     [ drop load-zone-ptr ] [ swap 4 LWZ ] 2bi ;
413
414 :: inc-allot-ptr ( nursery-ptr allot-ptr n -- )
415     scratch-reg allot-ptr n 8 align ADDI
416     scratch-reg nursery-ptr 4 STW ;
417
418 :: store-header ( dst class -- )
419     class type-number tag-fixnum scratch-reg LI
420     scratch-reg dst 0 STW ;
421
422 : store-tagged ( dst tag -- )
423     dupd tag-number ORI ;
424
425 M:: ppc %allot ( dst size class nursery-ptr -- )
426     nursery-ptr dst load-allot-ptr
427     nursery-ptr dst size inc-allot-ptr
428     dst class store-header
429     dst class store-tagged ;
430
431 : load-cards-offset ( dst -- )
432     [ "cards_offset" %load-vm-field-addr ] [ dup 0 LWZ ] bi ;
433
434 : load-decks-offset ( dst -- )
435     [ "decks_offset" %load-vm-field-addr ] [ dup 0 LWZ ] bi  ;
436
437 M:: ppc %write-barrier ( src card# table -- )
438     card-mark scratch-reg LI
439
440     ! Mark the card
441     table load-cards-offset
442     src card# card-bits SRWI
443     table scratch-reg card# STBX
444
445     ! Mark the card deck
446     table load-decks-offset
447     src card# deck-bits SRWI
448     table scratch-reg card# STBX ;
449
450 M:: ppc %check-nursery ( label temp1 temp2 -- )
451     temp2 load-zone-ptr
452     temp1 temp2 cell LWZ
453     temp2 temp2 3 cells LWZ
454     ! add ALLOT_BUFFER_ZONE to here
455     temp1 temp1 1024 ADDI
456     ! is here >= end?
457     temp1 0 temp2 CMP
458     label BLE ;
459
460 M:: ppc %save-gc-root ( gc-root register -- )
461     register 1 gc-root gc-root@ STW ;
462
463 M:: ppc %load-gc-root ( gc-root register -- )
464     register 1 gc-root gc-root@ LWZ ;
465
466 M:: ppc %call-gc ( gc-root-count temp -- )
467     3 1 gc-root-base local@ ADDI
468     gc-root-count 4 LI
469     "inline_gc" f %alien-invoke ;
470
471 M: ppc %prologue ( n -- )
472     0 11 LOAD32 rc-absolute-ppc-2/2 rel-this
473     0 MFLR
474     {
475         [ [ 1 1 ] dip neg ADDI ]
476         [ [ 11 1 ] dip xt-save STW ]
477         [ 11 LI ]
478         [ [ 11 1 ] dip next-save STW ]
479         [ [ 0 1 ] dip lr-save + STW ]
480     } cleave ;
481
482 M: ppc %epilogue ( n -- )
483     #! At the end of each word that calls a subroutine, we store
484     #! the previous link register value in r0 by popping it off
485     #! the stack, set the link register to the contents of r0,
486     #! and jump to the link register.
487     [ [ 0 1 ] dip lr-save + LWZ ]
488     [ [ 1 1 ] dip ADDI ] bi
489     0 MTLR ;
490
491 :: (%boolean) ( dst temp branch1 branch2 -- )
492     "end" define-label
493     dst \ f tag-number %load-immediate
494     "end" get branch1 execute( label -- )
495     branch2 [ "end" get branch2 execute( label -- ) ] when
496     dst \ t %load-reference
497     "end" get resolve-label ; inline
498
499 :: %boolean ( dst cc temp -- )
500     cc negate-cc order-cc {
501         { cc<  [ dst temp \ BLT f (%boolean) ] }
502         { cc<= [ dst temp \ BLE f (%boolean) ] }
503         { cc>  [ dst temp \ BGT f (%boolean) ] }
504         { cc>= [ dst temp \ BGE f (%boolean) ] }
505         { cc=  [ dst temp \ BEQ f (%boolean) ] }
506         { cc/= [ dst temp \ BNE f (%boolean) ] }
507     } case ;
508
509 : (%compare) ( src1 src2 -- ) [ 0 ] dip CMP ; inline
510 : (%compare-imm) ( src1 src2 -- ) [ 0 ] 2dip CMPI ; inline
511 : (%compare-float-unordered) ( src1 src2 -- ) [ 0 ] dip FCMPU ; inline
512 : (%compare-float-ordered) ( src1 src2 -- ) [ 0 ] dip FCMPO ; inline
513
514 :: (%compare-float) ( src1 src2 cc compare -- branch1 branch2 )
515     cc {
516         { cc<    [ src1 src2 \ compare execute( a b -- ) \ BLT f     ] }
517         { cc<=   [ src1 src2 \ compare execute( a b -- ) \ BLT \ BEQ ] }
518         { cc>    [ src1 src2 \ compare execute( a b -- ) \ BGT f     ] }
519         { cc>=   [ src1 src2 \ compare execute( a b -- ) \ BGT \ BEQ ] }
520         { cc=    [ src1 src2 \ compare execute( a b -- ) \ BEQ f     ] }
521         { cc<>   [ src1 src2 \ compare execute( a b -- ) \ BLT \ BGT ] }
522         { cc<>=  [ src1 src2 \ compare execute( a b -- ) \ BNO f     ] }
523         { cc/<   [ src1 src2 \ compare execute( a b -- ) \ BGE f     ] }
524         { cc/<=  [ src1 src2 \ compare execute( a b -- ) \ BGT \ BO  ] }
525         { cc/>   [ src1 src2 \ compare execute( a b -- ) \ BLE f     ] }
526         { cc/>=  [ src1 src2 \ compare execute( a b -- ) \ BLT \ BO  ] }
527         { cc/=   [ src1 src2 \ compare execute( a b -- ) \ BNE f     ] }
528         { cc/<>  [ src1 src2 \ compare execute( a b -- ) \ BEQ \ BO  ] }
529         { cc/<>= [ src1 src2 \ compare execute( a b -- ) \ BO  f     ] }
530     } case ; inline
531
532 M: ppc %compare [ (%compare) ] 2dip %boolean ;
533
534 M: ppc %compare-imm [ (%compare-imm) ] 2dip %boolean ;
535
536 M:: ppc %compare-float-ordered ( dst src1 src2 cc temp -- )
537     src1 src2 cc negate-cc \ (%compare-float-ordered) (%compare-float) :> branch2 :> branch1
538     dst temp branch1 branch2 (%boolean) ;
539
540 M:: ppc %compare-float-unordered ( dst src1 src2 cc temp -- )
541     src1 src2 cc negate-cc \ (%compare-float-unordered) (%compare-float) :> branch2 :> branch1
542     dst temp branch1 branch2 (%boolean) ;
543
544 :: %branch ( label cc -- )
545     cc order-cc {
546         { cc<  [ label BLT ] }
547         { cc<= [ label BLE ] }
548         { cc>  [ label BGT ] }
549         { cc>= [ label BGE ] }
550         { cc=  [ label BEQ ] }
551         { cc/= [ label BNE ] }
552     } case ;
553
554 M:: ppc %compare-branch ( label src1 src2 cc -- )
555     src1 src2 (%compare)
556     label cc %branch ;
557
558 M:: ppc %compare-imm-branch ( label src1 src2 cc -- )
559     src1 src2 (%compare-imm)
560     label cc %branch ;
561
562 :: (%branch) ( label branch1 branch2 -- )
563     label branch1 execute( label -- )
564     branch2 [ label branch2 execute( label -- ) ] when ; inline
565
566 M:: ppc %compare-float-ordered-branch ( label src1 src2 cc -- )
567     src1 src2 cc \ (%compare-float-ordered) (%compare-float) :> branch2 :> branch1
568     label branch1 branch2 (%branch) ;
569
570 M:: ppc %compare-float-unordered-branch ( label src1 src2 cc -- )
571     src1 src2 cc \ (%compare-float-unordered) (%compare-float) :> branch2 :> branch1
572     label branch1 branch2 (%branch) ;
573
574 : load-from-frame ( dst n rep -- )
575     {
576         { int-rep [ [ 1 ] dip LWZ ] }
577         { float-rep [ [ 1 ] dip LFS ] }
578         { double-rep [ [ 1 ] dip LFD ] }
579         { stack-params [ [ 0 1 ] dip LWZ [ 0 1 ] dip param@ STW ] }
580     } case ;
581
582 : next-param@ ( n -- x ) param@ stack-frame get total-size>> + ;
583
584 : store-to-frame ( src n rep -- )
585     {
586         { int-rep [ [ 1 ] dip STW ] }
587         { float-rep [ [ 1 ] dip STFS ] }
588         { double-rep [ [ 1 ] dip STFD ] }
589         { stack-params [ [ [ 0 1 ] dip next-param@ LWZ 0 1 ] dip STW ] }
590     } case ;
591
592 M: ppc %spill ( src rep dst -- )
593     swap [ n>> spill@ ] dip store-to-frame ;
594
595 M: ppc %reload ( dst rep src -- )
596     swap [ n>> spill@ ] dip load-from-frame ;
597
598 M: ppc %loop-entry ;
599
600 M: int-regs return-reg drop 3 ;
601 M: int-regs param-regs drop { 3 4 5 6 7 8 9 10 } ;
602 M: float-regs return-reg drop 1 ;
603
604 M:: ppc %save-param-reg ( stack reg rep -- )
605     reg stack local@ rep store-to-frame ;
606
607 M:: ppc %load-param-reg ( stack reg rep -- )
608     reg stack local@ rep load-from-frame ;
609
610 M: ppc %prepare-unbox ( -- )
611     ! First parameter is top of stack
612     3 ds-reg 0 LWZ
613     ds-reg dup cell SUBI ;
614
615 M: ppc %unbox ( n rep func -- )
616     ! Value must be in r3
617     ! Call the unboxer
618     f %alien-invoke
619     ! Store the return value on the C stack
620     over [ [ reg-class-of return-reg ] keep %save-param-reg ] [ 2drop ] if ;
621
622 M: ppc %unbox-long-long ( n func -- )
623     ! Value must be in r3:r4
624     ! Call the unboxer
625     f %alien-invoke
626     ! Store the return value on the C stack
627     [
628         [ [ 3 1 ] dip local@ STW ]
629         [ [ 4 1 ] dip cell + local@ STW ] bi
630     ] when* ;
631
632 M: ppc %unbox-large-struct ( n c-type -- )
633     ! Value must be in r3
634     ! Compute destination address and load struct size
635     [ [ 4 1 ] dip local@ ADDI ] [ heap-size 5 LI ] bi*
636     ! Call the function
637     "to_value_struct" f %alien-invoke ;
638
639 M: ppc %box ( n rep func -- )
640     ! If the source is a stack location, load it into freg #0.
641     ! If the source is f, then we assume the value is already in
642     ! freg #0.
643     [ over [ 0 over reg-class-of param-reg swap %load-param-reg ] [ 2drop ] if ] dip
644     f %alien-invoke ;
645
646 M: ppc %box-long-long ( n func -- )
647     [
648         [
649             [ [ 3 1 ] dip local@ LWZ ]
650             [ [ 4 1 ] dip cell + local@ LWZ ] bi
651         ] when*
652     ] dip f %alien-invoke ;
653
654 : struct-return@ ( n -- n )
655     [ stack-frame get params>> ] unless* local@ ;
656
657 M: ppc %prepare-box-struct ( -- )
658     #! Compute target address for value struct return
659     3 1 f struct-return@ ADDI
660     3 1 0 local@ STW ;
661
662 M: ppc %box-large-struct ( n c-type -- )
663     ! If n = f, then we're boxing a returned struct
664     ! Compute destination address and load struct size
665     [ [ 3 1 ] dip struct-return@ ADDI ] [ heap-size 4 LI ] bi*
666     ! Call the function
667     "box_value_struct" f %alien-invoke ;
668
669 M:: ppc %save-context ( temp1 temp2 callback-allowed? -- )
670     #! Save Factor stack pointers in case the C code calls a
671     #! callback which does a GC, which must reliably trace
672     #! all roots.
673     temp1 "stack_chain" %load-vm-field-addr
674     temp1 temp1 0 LWZ
675     1 temp1 0 STW
676     callback-allowed? [
677         ds-reg temp1 8 STW
678         rs-reg temp1 12 STW
679     ] when ;
680
681 M: ppc %alien-invoke ( symbol dll -- )
682     [ 11 ] 2dip %alien-global 11 MTLR BLRL ;
683
684 M: ppc %alien-callback ( quot -- )
685     3 swap %load-reference "c_to_factor" f %alien-invoke ;
686
687 M: ppc %prepare-alien-indirect ( -- )
688     "unbox_alien" f %alien-invoke
689     15 3 MR ;
690
691 M: ppc %alien-indirect ( -- )
692     15 MTLR BLRL ;
693
694 M: ppc %callback-value ( ctype -- )
695     ! Save top of data stack
696     3 ds-reg 0 LWZ
697     3 1 0 local@ STW
698     ! Restore data/call/retain stacks
699     "unnest_stacks" f %alien-invoke
700     ! Restore top of data stack
701     3 1 0 local@ LWZ
702     ! Unbox former top of data stack to return registers
703     unbox-return ;
704
705 M: ppc small-enough? ( n -- ? ) -32768 32767 between? ;
706
707 M: ppc return-struct-in-registers? ( c-type -- ? )
708     c-type return-in-registers?>> ;
709
710 M: ppc %box-small-struct ( c-type -- )
711     #! Box a <= 16-byte struct returned in r3:r4:r5:r6
712     heap-size 7 LI
713     "box_medium_struct" f %alien-invoke ;
714
715 : %unbox-struct-1 ( -- )
716     ! Alien must be in r3.
717     "alien_offset" f %alien-invoke
718     3 3 0 LWZ ;
719
720 : %unbox-struct-2 ( -- )
721     ! Alien must be in r3.
722     "alien_offset" f %alien-invoke
723     4 3 4 LWZ
724     3 3 0 LWZ ;
725
726 : %unbox-struct-4 ( -- )
727     ! Alien must be in r3.
728     "alien_offset" f %alien-invoke
729     6 3 12 LWZ
730     5 3 8 LWZ
731     4 3 4 LWZ
732     3 3 0 LWZ ;
733
734 M: ppc %nest-stacks ( -- )
735     "nest_stacks" f %alien-invoke ;
736
737 M: ppc %unnest-stacks ( -- )
738     "unnest_stacks" f %alien-invoke ;
739
740 M: ppc %unbox-small-struct ( size -- )
741     #! Alien must be in EAX.
742     heap-size cell align cell /i {
743         { 1 [ %unbox-struct-1 ] }
744         { 2 [ %unbox-struct-2 ] }
745         { 4 [ %unbox-struct-4 ] }
746     } case ;
747
748 enable-float-functions
749
750 USE: vocabs.loader
751
752 {
753     { [ os macosx? ] [ "cpu.ppc.macosx" require ] }
754     { [ os linux? ] [ "cpu.ppc.linux" require ] }
755 } cond
756
757 "complex-double" c-type t >>return-in-registers? drop
758
759 [
760     <c-type>
761         [ alien-unsigned-4 c-bool> ] >>getter
762         [ [ >c-bool ] 2dip set-alien-unsigned-4 ] >>setter
763         4 >>size
764         4 >>align
765         "box_boolean" >>boxer
766         "to_boolean" >>unboxer
767     bool define-primitive-type
768 ] with-compilation-unit