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