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