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